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