1 |
#! perl |
2 |
use Digest::MD5 qw/md5_hex/; |
3 |
|
4 |
my $timers = {}; |
5 |
my $pastebin_cmd; |
6 |
my $pastebin_url; |
7 |
|
8 |
sub on_start { |
9 |
my ($self) = @_; |
10 |
$pastebin_cmd = |
11 |
(urxvt::untaint $self->x_resource ("selection-pastebin-cmd")) |
12 |
or "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
13 |
|
14 |
$pastebin_url = |
15 |
(urxvt::untaint $self->x_resource ("selection-pastebin-url")) |
16 |
or "http://www.ta-sa.org/files/txt/"; |
17 |
(); |
18 |
} |
19 |
|
20 |
sub upload_paste { |
21 |
my ($self) = @_; |
22 |
|
23 |
my $txt = $self->selection; |
24 |
my $h = md5_hex ($txt); |
25 |
|
26 |
my $fn = "/tmp/$h.txt"; |
27 |
|
28 |
my $msg = "uploaded $h.txt"; |
29 |
|
30 |
if (open my $o, ">" . $fn) { |
31 |
print $o $txt; |
32 |
close $o; |
33 |
} else { |
34 |
$msg = "couldn't write $fn: $!"; |
35 |
} |
36 |
|
37 |
my $cmd = $pastebin_cmd; |
38 |
$cmd =~ s/%/$fn/; |
39 |
unless (system ($cmd) == 0) { |
40 |
$msg = "couldn't upload, '$cmd' failed"; |
41 |
} |
42 |
|
43 |
$self->selection ($pastebin_url . "$h.txt"); |
44 |
|
45 |
my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); |
46 |
$ov->set (0, 0, $msg); |
47 |
|
48 |
$timers->{t1} = |
49 |
urxvt::timer |
50 |
->new |
51 |
->start ((int urxvt::NOW) + 5) # make sure we update "on" the second |
52 |
->interval (1) |
53 |
->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); |
54 |
} |
55 |
|
56 |
sub on_keyboard_command { |
57 |
my ($self, $cmd) = @_; |
58 |
|
59 |
$cmd eq "selection:rot13" |
60 |
and $self->selection (map { y/A-Za-z/N-ZA-Mn-za-m/; $_ } $self->selection); |
61 |
|
62 |
$cmd eq "selection:remote-pastebin" |
63 |
and upload_paste ($self); |
64 |
|
65 |
() |
66 |
} |
67 |
|
68 |
sub on_init { |
69 |
my ($self) = @_; |
70 |
|
71 |
for (my $idx = 0; defined (my $res = $self->x_resource ("selection.pattern-$idx")); $idx++) { |
72 |
no re 'eval'; # just to be sure |
73 |
$res = utf8::encode $self->locale_decode ($res); |
74 |
push @{ $self->{patterns} }, qr/$res/; |
75 |
} |
76 |
|
77 |
() |
78 |
} |
79 |
|
80 |
# "find interetsing things"-patterns |
81 |
my @mark_patterns = ( |
82 |
# common types of "parentheses" |
83 |
qr{ (?<![^[:space:]]) ‘ ([^‘’]+) ’ (?![^[:space]]) }x, |
84 |
qr{ (?<![^[:space:]]) ` ([^`']+) ' (?![^[:space]]) }x, |
85 |
qr{ (?<![^[:space:]]) (" [^[:space:]] [^"]* ") }x, |
86 |
qr{ (" [^"]* [^[:space:]] ") (?![^[:space]]) }x, |
87 |
qr{ \< ([^<>[:space:]]+) \> }x, |
88 |
qr{ \{ ([^{}[:space:]]+) \} }x, |
89 |
qr{ \[ ([^{}[:space:]]+) \] }x, |
90 |
qr{ \( ([^()[:space:]]+) \) }x, |
91 |
|
92 |
# urls, just a heuristic |
93 |
qr{( |
94 |
(?:https?|ftp|news|mailto|file)://[ab-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27(),~]+ |
95 |
[ab-zA-Z0-9\-\@;\/?:&=%\$_+!*\x27()~] # exclude some trailing characters (heuristic) |
96 |
)}x, |
97 |
|
98 |
# shell-like argument quoting, basically always matches |
99 |
qr{\G [\ \t|&;<>()] *( |
100 |
(?: |
101 |
[^\\"'\ \t|&;<>()]+ |
102 |
| \\. |
103 |
| " (?: [^\\"]+ | \\. )* " |
104 |
| ' [^']* ' |
105 |
)+ |
106 |
)}x, |
107 |
); |
108 |
|
109 |
# "correct obvious? crap"-patterns |
110 |
my @simplify_patterns = ( |
111 |
qr{^"([^\\"'\ \t|&;<>()*?]+)"$}, # "simple" => simple |
112 |
qr{^(.*)[,\-]$}, # strip off trailing , and - |
113 |
); |
114 |
|
115 |
sub on_sel_extend { |
116 |
my ($self, $time) = @_; |
117 |
|
118 |
my ($row, $col) = $self->selection_mark; |
119 |
my $line = $self->line ($row); |
120 |
my $text = $line->t; |
121 |
my $markofs = $line->offset_of ($row, $col); |
122 |
my $curlen = $line->offset_of ($self->selection_end) |
123 |
- $line->offset_of ($self->selection_beg); |
124 |
|
125 |
my @matches; |
126 |
|
127 |
# not doing matches in unicode mode helps speed |
128 |
# enourmously here. working in utf-8 should be |
129 |
# equivalent due to the magic of utf-8 encoding. |
130 |
utf8::encode $text; |
131 |
study $text; # _really_ helps, too :) |
132 |
|
133 |
for my $regex (@mark_patterns, @{ $self->{patterns} }) { |
134 |
while ($text =~ /$regex/g) { |
135 |
if ($-[1] <= $markofs and $markofs <= $+[1]) { |
136 |
my $ofs = $-[1]; |
137 |
my $match = $1; |
138 |
|
139 |
for my $regex (@simplify_patterns) { |
140 |
if ($match =~ $regex) { |
141 |
$match = $1; |
142 |
$ofs += $-[1]; |
143 |
} |
144 |
} |
145 |
|
146 |
push @matches, [$ofs, length $match]; |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
# whole line |
152 |
push @matches, [0, ($line->end - $line->beg + 1) * $self->ncol]; |
153 |
|
154 |
for (sort { $a->[1] <=> $b->[1] or $b->[0] <=> $a->[0] } @matches) { |
155 |
my ($ofs, $len) = @$_; |
156 |
|
157 |
next if $len <= $curlen; |
158 |
|
159 |
$self->selection_beg ($line->coord_of ($ofs)); |
160 |
$self->selection_end ($line->coord_of ($ofs + $len)); |
161 |
return 1; |
162 |
} |
163 |
|
164 |
() |
165 |
} |