1 | #! perl |
1 | #! perl |
|
|
2 | |
|
|
3 | #:META:X_RESOURCE:%.:string:autotransform expression |
|
|
4 | |
|
|
5 | =head1 NAME |
|
|
6 | |
|
|
7 | selection-autotransform - automatically transform select text |
|
|
8 | |
|
|
9 | =head1 DESCRIPTION |
|
|
10 | |
|
|
11 | This selection allows you to do automatic transforms on a selection |
|
|
12 | whenever a selection is made. |
|
|
13 | |
|
|
14 | It works by specifying perl snippets (most useful is a single C<s///> |
|
|
15 | operator) that modify C<$_> as resources: |
|
|
16 | |
|
|
17 | URxvt.selection-autotransform.0: transform |
|
|
18 | URxvt.selection-autotransform.1: transform |
|
|
19 | ... |
|
|
20 | |
|
|
21 | For example, the following will transform selections of the form |
|
|
22 | C<filename:number>, often seen in compiler messages, into C<vi +$filename |
|
|
23 | $word>: |
|
|
24 | |
|
|
25 | URxvt.selection-autotransform.0: s/^([^:[:space:]]+):(\\d+):?$/vi +$2 \\Q$1\\E\\x0d/ |
|
|
26 | |
|
|
27 | And this example matches the same,but replaces it with vi-commands you can |
|
|
28 | paste directly into your (vi :) editor: |
|
|
29 | |
|
|
30 | URxvt.selection-autotransform.0: s/^([^:[:space:]]+(\\d+):?$/:e \\Q$1\\E\\x0d:$2\\x0d/ |
|
|
31 | |
|
|
32 | Of course, this can be modified to suit your needs and your editor :) |
|
|
33 | |
|
|
34 | To expand the example above to typical perl error messages ("XXX at |
|
|
35 | FILENAME line YYY."), you need a slightly more elaborate solution: |
|
|
36 | |
|
|
37 | URxvt.selection.pattern-0: ( at .*? line \\d+[,.]) |
|
|
38 | URxvt.selection-autotransform.0: s/^ at (.*?) line (\\d+)[,.]$/:e \\Q$1\E\\x0d:$2\\x0d/ |
|
|
39 | |
|
|
40 | The first line tells the selection code to treat the unchanging part of |
|
|
41 | every error message as a selection pattern, and the second line transforms |
|
|
42 | the message into vi commands to load the file. |
|
|
43 | |
|
|
44 | =cut |
2 | |
45 | |
3 | sub msg { |
46 | sub msg { |
4 | my ($self, $msg) = @_; |
47 | my ($self, $msg) = @_; |
5 | |
48 | |
6 | my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
49 | my $overlay = $self->overlay (0, 0, $self->strwidth ($msg), 1); |
7 | $overlay->set (0, 0, $msg); |
50 | $overlay->set (0, 0, $msg); |
8 | my $iow; $iow = urxvt::timer->new->start (urxvt::NOW + 2)->cb (sub { |
51 | my $iow; $iow = urxvt::timer->new->after (2)->cb (sub { |
9 | undef $overlay; |
52 | undef $overlay; |
10 | undef $iow; |
53 | undef $iow; |
11 | }); |
54 | }); |
12 | } |
55 | } |
13 | |
56 | |
14 | sub on_init { |
57 | sub on_init { |
15 | my ($self) = @_; |
58 | my ($self) = @_; |
16 | |
59 | |
17 | unless (urxvt::safe) { |
|
|
18 | warn "running with elevated privileges, ignoring selection-autotransform patterns"; |
|
|
19 | return; |
|
|
20 | } |
|
|
21 | |
|
|
22 | for (my $idx = 0; defined (my $res = urxvt::untaint $self->x_resource ("selection-autotransform.$idx")); $idx++) { |
60 | for (my $idx = 0; defined (my $res = $self->x_resource ("%.$idx")); $idx++) { |
23 | warn "<<<$idx:$res>>>\n";#d# |
61 | $res = $self->locale_decode ($res); |
24 | my $transform = eval "sub { $res }"; |
62 | my $transform = eval "sub { $res }"; |
25 | |
63 | |
26 | if ($transform) { |
64 | if ($transform) { |
27 | push @{ $self->{transforms} }, $transform; |
65 | push @{ $self->{transforms} }, $transform; |
28 | } else { |
66 | } else { |
29 | warn "$res: $@"; |
67 | warn "$res: $@"; |
30 | } |
68 | } |
31 | } |
69 | } |
32 | |
70 | |
|
|
71 | $self->{enabled} = 1; |
|
|
72 | |
|
|
73 | push @{ $self->{term}{option_popup_hook} }, sub { |
|
|
74 | ("autotransform" => $self->{enabled}, sub { $self->{enabled} = shift }) |
|
|
75 | }; |
|
|
76 | |
33 | () |
77 | () |
34 | } |
78 | } |
35 | |
79 | |
36 | sub on_sel_grab { |
80 | sub on_sel_grab { |
37 | my ($self) = @_; |
81 | my ($self) = @_; |
|
|
82 | |
|
|
83 | $self->{enabled} |
|
|
84 | or return; |
38 | |
85 | |
39 | my $text = $self->selection; |
86 | my $text = $self->selection; |
40 | local $_ = $text; |
87 | local $_ = $text; |
41 | |
88 | |
42 | for my $transform (@{ $self->{transforms} }) { |
89 | for my $transform (@{ $self->{transforms} }) { |
… | |
… | |
46 | s/[\x00-\x1f\x80-\x9f]/·/g; |
93 | s/[\x00-\x1f\x80-\x9f]/·/g; |
47 | $self->msg ($self->special_encode ("auto-transformed to $_")); |
94 | $self->msg ($self->special_encode ("auto-transformed to $_")); |
48 | last; |
95 | last; |
49 | } |
96 | } |
50 | } |
97 | } |
51 | |
98 | |
52 | () |
99 | () |
53 | } |
100 | } |
54 | |
101 | |