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.16 |
# "find interetsing things"-patterns |
13 |
root |
1.15 |
my @mark_patterns = ( |
14 |
root |
1.22 |
qr{([[:word:]]+)}, |
15 |
root |
1.9 |
|
16 |
root |
1.17 |
# common "parentheses" |
17 |
root |
1.18 |
qr{(?:^|\s) ‘ ([^‘’]+?) ’ (?:\s|\)|$)}x, |
18 |
|
|
qr{(?:^|\s) ` ([^`']+?) ' (?:\s|\)|$)}x, |
19 |
|
|
qr{ \{ ([^{}]+?) \} }x, |
20 |
|
|
qr{ \[ ([^{}]+?) \] }x, |
21 |
root |
1.22 |
qr{ \( ([^()]+?) \) }x, |
22 |
root |
1.14 |
|
23 |
root |
1.20 |
# 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 |
root |
1.17 |
# shell-like argument quoting, basically always matches |
30 |
root |
1.11 |
qr{\G [\ \t|&;<>()] *( |
31 |
root |
1.9 |
(?: |
32 |
root |
1.11 |
[^\\"'\ \t|&;<>()]+ |
33 |
root |
1.9 |
| \\. |
34 |
root |
1.13 |
| " (?: [^\\"]+ | \\. )* " |
35 |
root |
1.9 |
| ' [^']* ' |
36 |
|
|
)+ |
37 |
root |
1.14 |
)}x, |
38 |
root |
1.8 |
); |
39 |
|
|
|
40 |
root |
1.16 |
# "correct obvious? crap"-patterns |
41 |
root |
1.15 |
my @simplify_patterns = ( |
42 |
|
|
qr{^"([^\\"'\ \t|&;<>()*?]+)"$}, # "simple" => simple |
43 |
root |
1.16 |
qr{^(.*)[,\-]$}, # strip off trailing , and - |
44 |
root |
1.15 |
); |
45 |
root |
1.14 |
|
46 |
root |
1.6 |
sub on_sel_extend { |
47 |
root |
1.21 |
my ($self, $time) = @_; |
48 |
root |
1.8 |
|
49 |
|
|
my ($row, $col) = $self->selection_mark; |
50 |
|
|
my $line = $self->line ($row); |
51 |
|
|
my $text = $line->t; |
52 |
root |
1.20 |
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 |
root |
1.8 |
|
58 |
root |
1.15 |
for my $regex (@mark_patterns) { |
59 |
root |
1.8 |
while ($text =~ /$regex/g) { |
60 |
root |
1.20 |
if ($-[1] <= $markofs and $markofs <= $+[1]) { |
61 |
root |
1.16 |
my $ofs = $-[1]; |
62 |
root |
1.15 |
my $match = $1; |
63 |
|
|
|
64 |
root |
1.20 |
push @matches, [$ofs, length $match]; |
65 |
|
|
|
66 |
root |
1.15 |
for my $regex (@simplify_patterns) { |
67 |
|
|
if ($match =~ $regex) { |
68 |
|
|
$match = $1; |
69 |
root |
1.16 |
$ofs += $-[1]; |
70 |
root |
1.15 |
} |
71 |
|
|
} |
72 |
|
|
|
73 |
root |
1.20 |
push @matches, [$ofs, length $match]; |
74 |
root |
1.8 |
} |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
root |
1.21 |
# whole line |
79 |
|
|
push @matches, [0, ($line->end - $line->beg + 1) * $self->ncol]; |
80 |
|
|
|
81 |
root |
1.20 |
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 |
root |
1.21 |
() |
92 |
root |
1.5 |
} |