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