ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection
Revision: 1.22
Committed: Thu Jan 12 10:21:50 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.21: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 sub on_keyboard_command {
4 my ($self, $cmd) = @_;
5
6 $cmd eq "selection:rot13"
7 and $self->selection (map { y/A-Za-z/N-ZA-Mn-za-m/; $_ } $self->selection);
8
9 ()
10 }
11
12 # "find interetsing things"-patterns
13 my @mark_patterns = (
14 qr{([[:word:]]+)},
15
16 # common "parentheses"
17 qr{(?:^|\s) ‘ ([^‘’]+?) ’ (?:\s|\)|$)}x,
18 qr{(?:^|\s) ` ([^`']+?) ' (?:\s|\)|$)}x,
19 qr{ \{ ([^{}]+?) \} }x,
20 qr{ \[ ([^{}]+?) \] }x,
21 qr{ \( ([^()]+?) \) }x,
22
23 # urls, just a heuristic
24 qr{(
25 (?:https?|ftp|news|mailto|file)://[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~]+
26 [ab-zA-Z0-9\-\@;\/?:&=%\$_+!*\x27()~] # exclude some trailing characters (heuristic)
27 )}x,
28
29 # shell-like argument quoting, basically always matches
30 qr{\G [\ \t|&;<>()] *(
31 (?:
32 [^\\"'\ \t|&;<>()]+
33 | \\.
34 | " (?: [^\\"]+ | \\. )* "
35 | ' [^']* '
36 )+
37 )}x,
38 );
39
40 # "correct obvious? crap"-patterns
41 my @simplify_patterns = (
42 qr{^"([^\\"'\ \t|&;<>()*?]+)"$}, # "simple" => simple
43 qr{^(.*)[,\-]$}, # strip off trailing , and -
44 );
45
46 sub on_sel_extend {
47 my ($self, $time) = @_;
48
49 my ($row, $col) = $self->selection_mark;
50 my $line = $self->line ($row);
51 my $text = $line->t;
52 my $markofs = $line->offset_of ($row, $col);
53 my $curlen = $line->offset_of ($self->selection_end)
54 - $line->offset_of ($self->selection_beg);
55
56 my @matches;
57
58 for my $regex (@mark_patterns) {
59 while ($text =~ /$regex/g) {
60 if ($-[1] <= $markofs and $markofs <= $+[1]) {
61 my $ofs = $-[1];
62 my $match = $1;
63
64 push @matches, [$ofs, length $match];
65
66 for my $regex (@simplify_patterns) {
67 if ($match =~ $regex) {
68 $match = $1;
69 $ofs += $-[1];
70 }
71 }
72
73 push @matches, [$ofs, length $match];
74 }
75 }
76 }
77
78 # whole line
79 push @matches, [0, ($line->end - $line->beg + 1) * $self->ncol];
80
81 for (sort { $a->[1] <=> $b->[1] or $b->[0] <=> $a->[0] } @matches) {
82 my ($ofs, $len) = @$_;
83
84 next if $len <= $curlen;
85
86 $self->selection_beg ($line->coord_of ($ofs));
87 $self->selection_end ($line->coord_of ($ofs + $len));
88 return 1;
89 }
90
91 ()
92 }