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

File Contents

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