1 |
=head1 NAME |
2 |
|
3 |
@@RXVT_NAME@@perl - rxvt-unicode's embedded perl interpreter |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
# create a file grab_test in $HOME: |
8 |
|
9 |
sub on_sel_grab { |
10 |
warn "you selected ", $_[0]->selection; |
11 |
() |
12 |
} |
13 |
|
14 |
# start a @@RXVT_NAME@@ using it: |
15 |
|
16 |
@@RXVT_NAME@@ --perl-lib $HOME -pe grab_test |
17 |
|
18 |
=head1 DESCRIPTION |
19 |
|
20 |
Everytime a terminal object gets created, scripts specified via the |
21 |
C<perl> resource are loaded and associated with it. |
22 |
|
23 |
Scripts are compiled in a 'use strict' and 'use utf8' environment, and |
24 |
thus must be encoded as UTF-8. |
25 |
|
26 |
Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where |
27 |
scripts will be shared (But not enabled) for all terminals. |
28 |
|
29 |
=head2 General API Considerations |
30 |
|
31 |
All objects (such as terminals, time watchers etc.) are typical |
32 |
reference-to-hash objects. The hash can be used to store anything you |
33 |
like. All members starting with an underscore (such as C<_ptr> or |
34 |
C<_hook>) are reserved for internal uses and must not be accessed or |
35 |
modified). |
36 |
|
37 |
When objects are destroyed on the C++ side, the perl object hashes are |
38 |
emptied, so its best to store related objects such as time watchers and |
39 |
the like inside the terminal object so they get destroyed as soon as the |
40 |
terminal is destroyed. |
41 |
|
42 |
=head2 Hooks |
43 |
|
44 |
The following subroutines can be declared in loaded scripts, and will be called |
45 |
whenever the relevant event happens. |
46 |
|
47 |
All of them must return a boolean value. If it is true, then the event |
48 |
counts as being I<consumed>, and the invocation of other hooks is skipped, |
49 |
and the relevant action might not be carried out by the C++ code. |
50 |
|
51 |
When in doubt, return a false value (preferably C<()>). |
52 |
|
53 |
=over 4 |
54 |
|
55 |
=item on_init $term |
56 |
|
57 |
Called after a new terminal object has been initialized, but before |
58 |
windows are created or the command gets run. |
59 |
|
60 |
=item on_reset $term |
61 |
|
62 |
Called after the screen is "reset" for any reason, such as resizing or |
63 |
control sequences. Here is where you can react on changes to size-related |
64 |
variables. |
65 |
|
66 |
=item on_start $term |
67 |
|
68 |
Called at the very end of initialisation of a new terminal, just before |
69 |
returning to the mainloop. |
70 |
|
71 |
=item on_sel_make $term, $eventtime |
72 |
|
73 |
Called whenever a selection has been made by the user, but before the |
74 |
selection text is copied, so changes to the beginning, end or type of the |
75 |
selection will be honored. |
76 |
|
77 |
Returning a true value aborts selection making by urxvt, in which case you |
78 |
have to make a selection yourself by calling C<< $term->selection_grab >>. |
79 |
|
80 |
=item on_sel_grab $term, $eventtime |
81 |
|
82 |
Called whenever a selection has been copied, but before the selection is |
83 |
requested from the server. The selection text can be queried and changed |
84 |
by calling C<< $term->selection >>. |
85 |
|
86 |
Returning a true value aborts selection grabbing. It will still be hilighted. |
87 |
|
88 |
=item on_focus_in $term |
89 |
|
90 |
Called whenever the window gets the keyboard focus, before urxvt does |
91 |
focus in processing. |
92 |
|
93 |
=item on_focus_out $term |
94 |
|
95 |
Called wheneever the window loses keyboard focus, before urxvt does focus |
96 |
out processing. |
97 |
|
98 |
=item on_view_change $term, $offset |
99 |
|
100 |
Called whenever the view offset changes, i..e the user or program |
101 |
scrolls. Offset C<0> means display the normal terminal, positive values |
102 |
show this many lines of scrollback. |
103 |
|
104 |
=item on_scroll_back $term, $lines, $saved |
105 |
|
106 |
Called whenever lines scroll out of the terminal area into the scrollback |
107 |
buffer. C<$lines> is the number of lines scrolled out and may be larger |
108 |
than the scroll back buffer or the terminal. |
109 |
|
110 |
It is called before lines are scrolled out (so rows 0 .. min ($lines - 1, |
111 |
$nrow - 1) represent the lines to be scrolled out). C<$saved> is the total |
112 |
number of lines that will be in the scrollback buffer. |
113 |
|
114 |
=item on_tty_activity $term *NYI* |
115 |
|
116 |
Called whenever the program(s) running in the urxvt window send output. |
117 |
|
118 |
=item on_refresh_begin $term |
119 |
|
120 |
Called just before the screen gets redrawn. Can be used for overlay |
121 |
or similar effects by modify terminal contents in refresh_begin, and |
122 |
restoring them in refresh_end. The built-in overlay and selection display |
123 |
code is run after this hook, and takes precedence. |
124 |
|
125 |
=item on_refresh_end $term |
126 |
|
127 |
Called just after the screen gets redrawn. See C<on_refresh_begin>. |
128 |
|
129 |
=item on_keyboard_command $term, $string |
130 |
|
131 |
Called whenever the user presses a key combination that has a |
132 |
C<perl:string> action bound to it (see description of the B<keysym> |
133 |
resource in the @@RXVT_NAME@@(1) manpage). |
134 |
|
135 |
=back |
136 |
|
137 |
=head2 Functions in the C<urxvt> Package |
138 |
|
139 |
=over 4 |
140 |
|
141 |
=item urxvt::fatal $errormessage |
142 |
|
143 |
Fatally aborts execution with the given error message. Avoid at all |
144 |
costs! The only time this is acceptable is when the terminal process |
145 |
starts up. |
146 |
|
147 |
=item urxvt::warn $string |
148 |
|
149 |
Calls C<rxvt_warn> with the given string which should not include a |
150 |
newline. The module also overwrites the C<warn> builtin with a function |
151 |
that calls this function. |
152 |
|
153 |
Using this function has the advantage that its output ends up in the |
154 |
correct place, e.g. on stderr of the connecting urxvtc client. |
155 |
|
156 |
=item $time = urxvt::NOW |
157 |
|
158 |
Returns the "current time" (as per the event loop). |
159 |
|
160 |
=cut |
161 |
|
162 |
package urxvt; |
163 |
|
164 |
use strict; |
165 |
|
166 |
our $term; |
167 |
our @HOOKNAME; |
168 |
our $LIBDIR; |
169 |
|
170 |
BEGIN { |
171 |
urxvt->bootstrap; |
172 |
|
173 |
# overwrite perl's warn |
174 |
*CORE::GLOBAL::warn = sub { |
175 |
my $msg = join "", @_; |
176 |
$msg .= "\n" |
177 |
unless $msg =~ /\n$/; |
178 |
urxvt::warn ($msg); |
179 |
}; |
180 |
} |
181 |
|
182 |
my @hook_count; |
183 |
my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; |
184 |
|
185 |
sub verbose { |
186 |
my ($level, $msg) = @_; |
187 |
warn "$msg\n" if $level <= $verbosity; |
188 |
} |
189 |
|
190 |
# find on_xxx subs in the package and register them |
191 |
# as hooks |
192 |
sub register_package($) { |
193 |
my ($pkg) = @_; |
194 |
|
195 |
for my $htype (0.. $#HOOKNAME) { |
196 |
my $name = $HOOKNAME[$htype]; |
197 |
|
198 |
my $ref = $pkg->can ("on_" . lc $name) |
199 |
or next; |
200 |
|
201 |
$term->{_hook}[$htype]{$ref*1} = $ref; |
202 |
$hook_count[$htype]++ |
203 |
or set_should_invoke $htype, 1; |
204 |
} |
205 |
} |
206 |
|
207 |
my $script_pkg = "script0000"; |
208 |
my %script_pkg; |
209 |
|
210 |
# load a single script into its own package, once only |
211 |
sub script_package($) { |
212 |
my ($path) = @_; |
213 |
|
214 |
$script_pkg{$path} ||= do { |
215 |
my $pkg = "urxvt::" . ($script_pkg++); |
216 |
|
217 |
verbose 3, "loading script '$path' into package '$pkg'"; |
218 |
|
219 |
open my $fh, "<:raw", $path |
220 |
or die "$path: $!"; |
221 |
|
222 |
my $source = "package $pkg; use strict; use utf8;\n" |
223 |
. "#line 1 \"$path\"\n{\n" |
224 |
. (do { local $/; <$fh> }) |
225 |
. "\n};\n1"; |
226 |
|
227 |
eval $source or die "$path: $@"; |
228 |
|
229 |
$pkg |
230 |
} |
231 |
} |
232 |
|
233 |
# called by the rxvt core |
234 |
sub invoke { |
235 |
local $term = shift; |
236 |
my $htype = shift; |
237 |
|
238 |
if ($htype == 0) { # INIT |
239 |
my @dirs = ((split /:/, $term->resource ("perl_lib")), "$LIBDIR/perl"); |
240 |
|
241 |
for my $ext (split /:/, $term->resource ("perl_ext")) { |
242 |
my @files = grep -f $_, map "$_/$ext", @dirs; |
243 |
|
244 |
if (@files) { |
245 |
register_package script_package $files[0]; |
246 |
} else { |
247 |
warn "perl extension '$ext' not found in perl library search path\n"; |
248 |
} |
249 |
} |
250 |
|
251 |
} elsif ($htype == 1) { # DESTROY |
252 |
if (my $hook = $term->{_hook}) { |
253 |
for my $htype (0..$#$hook) { |
254 |
$hook_count[$htype] -= scalar keys %{ $hook->[$htype] || {} } |
255 |
or set_should_invoke $htype, 0; |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
my $cb = $term->{_hook}[$htype] |
261 |
or return; |
262 |
|
263 |
verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
264 |
if $verbosity >= 10; |
265 |
|
266 |
while (my ($k, $v) = each %$cb) { |
267 |
return 1 if $v->($term, @_); |
268 |
} |
269 |
|
270 |
0 |
271 |
} |
272 |
|
273 |
=back |
274 |
|
275 |
=head2 The C<urxvt::term> Class |
276 |
|
277 |
=over 4 |
278 |
|
279 |
=item $value = $term->resource ($name[, $newval]) |
280 |
|
281 |
Returns the current resource value associated with a given name and |
282 |
optionally sets a new value. Setting values is most useful in the C<init> |
283 |
hook. Unset resources are returned and accepted as C<undef>. |
284 |
|
285 |
The new value must be properly encoded to a suitable character encoding |
286 |
before passing it to this method. Similarly, the returned value may need |
287 |
to be converted from the used encoding to text. |
288 |
|
289 |
Resource names are as defined in F<src/rsinc.h>. Colours can be specified |
290 |
as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will |
291 |
likely change). |
292 |
|
293 |
Please note that resource strings will currently only be freed when the |
294 |
terminal is destroyed, so changing options frequently will eat memory. |
295 |
|
296 |
Here is a a likely non-exhaustive list of resource names, not all of which |
297 |
are supported in every build, please see the source to see the actual |
298 |
list: |
299 |
|
300 |
answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont |
301 |
borderLess color cursorBlink cursorUnderline cutchars delete_key |
302 |
display_name embed ext_bwidth fade font geometry hold iconName |
303 |
imFont imLocale inputMethod insecure int_bwidth intensityStyles |
304 |
italicFont jumpScroll lineSpace loginShell mapAlert menu meta8 modifier |
305 |
mouseWheelScrollPage name pastableTabs path perl_eval perl_ext |
306 |
perl_lib pointerBlank pointerBlankDelay preeditType print_pipe pty_fd |
307 |
reverseVideo saveLines scrollBar scrollBar_align scrollBar_floating |
308 |
scrollBar_right scrollBar_thickness scrollTtyKeypress scrollTtyOutput |
309 |
scrollWithBuffer scrollstyle secondaryScreen secondaryScroll selectstyle |
310 |
shade term_name title transparent transparent_all tripleclickwords |
311 |
utmpInhibit visualBell |
312 |
|
313 |
=cut |
314 |
|
315 |
sub urxvt::term::resource($$;$) { |
316 |
my ($self, $name) = (shift, shift); |
317 |
unshift @_, $self, $name, ($name =~ s/\s*\+\s*(\d+)$// ? $1 : 0); |
318 |
goto &urxvt::term::_resource; |
319 |
} |
320 |
|
321 |
=item ($row, $col) = $term->selection_mark ([$row, $col]) |
322 |
|
323 |
=item ($row, $col) = $term->selection_beg ([$row, $col]) |
324 |
|
325 |
=item ($row, $col) = $term->selection_end ([$row, $col]) |
326 |
|
327 |
Return the current values of the selection mark, begin or end positions, |
328 |
and optionally set them to new values. |
329 |
|
330 |
=item $success = $term->selection_grab ($eventtime) |
331 |
|
332 |
Try to request the primary selection from the server (for example, as set |
333 |
by the next method). |
334 |
|
335 |
=item $oldtext = $term->selection ([$newtext]) |
336 |
|
337 |
Return the current selection text and optionally replace it by C<$newtext>. |
338 |
|
339 |
=item $term->scr_overlay ($x, $y, $text) |
340 |
|
341 |
Create a simple multi-line overlay box. See the next method for details. |
342 |
|
343 |
=cut |
344 |
|
345 |
sub urxvt::term::scr_overlay { |
346 |
my ($self, $x, $y, $text) = @_; |
347 |
|
348 |
my @lines = split /\n/, $text; |
349 |
|
350 |
my $w = 0; |
351 |
for (map $self->strwidth ($_), @lines) { |
352 |
$w = $_ if $w < $_; |
353 |
} |
354 |
|
355 |
$self->scr_overlay_new ($x, $y, $w, scalar @lines); |
356 |
$self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
357 |
} |
358 |
|
359 |
=item $term->scr_overlay_new ($x, $y, $width, $height) |
360 |
|
361 |
Create a new (empty) overlay at the given position with the given |
362 |
width/height. A border will be put around the box. If either C<$x> or |
363 |
C<$y> is negative, then this is counted from the right/bottom side, |
364 |
respectively. |
365 |
|
366 |
=item $term->scr_overlay_off |
367 |
|
368 |
Switch the overlay off again. |
369 |
|
370 |
=item $term->scr_overlay_set_char ($x, $y, $char, $rend = OVERLAY_RSTYLE) |
371 |
|
372 |
Put a single character (specified numerically) at the given overlay |
373 |
position. |
374 |
|
375 |
=item $term->scr_overlay_set ($x, $y, $text) |
376 |
|
377 |
Write a string at the given position into the overlay. |
378 |
|
379 |
=item $cellwidth = $term->strwidth $string |
380 |
|
381 |
Returns the number of screen-cells this string would need. Correctly |
382 |
accounts for wide and combining characters. |
383 |
|
384 |
=item $octets = $term->locale_encode $string |
385 |
|
386 |
Convert the given text string into the corresponding locale encoding. |
387 |
|
388 |
=item $string = $term->locale_decode $octets |
389 |
|
390 |
Convert the given locale-encoded octets into a perl string. |
391 |
|
392 |
=item $term->tt_write ($octets) |
393 |
|
394 |
Write the octets given in C<$data> to the tty (i.e. as program input). To |
395 |
pass characters instead of octets, you should convetr you strings first to |
396 |
the locale-specific encoding using C<< $term->locale_encode >>. |
397 |
|
398 |
=back |
399 |
|
400 |
=head2 The C<urxvt::timer> Class |
401 |
|
402 |
This class implements timer watchers/events. Time is represented as a |
403 |
fractional number of seconds since the epoch. Example: |
404 |
|
405 |
# create a digital clock display in upper right corner |
406 |
$term->{timer} = urxvt::timer |
407 |
->new |
408 |
->start (urxvt::NOW) |
409 |
->cb (sub { |
410 |
my ($timer) = @_; |
411 |
my $time = $timer->at; |
412 |
$timer->start ($time + 1); |
413 |
$self->scr_overlay (-1, 0, |
414 |
POSIX::strftime "%H:%M:%S", localtime $time); |
415 |
}); |
416 |
|
417 |
=over 4 |
418 |
|
419 |
=item $timer = new urxvt::timer |
420 |
|
421 |
Create a new timer object in stopped state. |
422 |
|
423 |
=item $timer = $timer->cb (sub { my ($timer) = @_; ... }) |
424 |
|
425 |
Set the callback to be called when the timer triggers. |
426 |
|
427 |
=item $tstamp = $timer->at |
428 |
|
429 |
Return the time this watcher will fire next. |
430 |
|
431 |
=item $timer = $timer->set ($tstamp) |
432 |
|
433 |
Set the time the event is generated to $tstamp. |
434 |
|
435 |
=item $timer = $timer->start |
436 |
|
437 |
Start the timer. |
438 |
|
439 |
=item $timer = $timer->start ($tstamp) |
440 |
|
441 |
Set the event trigger time to C<$tstamp> and start the timer. |
442 |
|
443 |
=item $timer = $timer->stop |
444 |
|
445 |
Stop the timer. |
446 |
|
447 |
=back |
448 |
|
449 |
=head2 The C<urxvt::iow> Class |
450 |
|
451 |
This class implements io watchers/events. Example: |
452 |
|
453 |
$term->{socket} = ... |
454 |
$term->{iow} = urxvt::iow |
455 |
->new |
456 |
->fd (fileno $term->{socket}) |
457 |
->events (1) # wait for read data |
458 |
->start |
459 |
->cb (sub { |
460 |
my ($iow, $revents) = @_; |
461 |
# $revents must be 1 here, no need to check |
462 |
sysread $term->{socket}, my $buf, 8192 |
463 |
or end-of-file; |
464 |
}); |
465 |
|
466 |
|
467 |
=over 4 |
468 |
|
469 |
=item $iow = new urxvt::iow |
470 |
|
471 |
Create a new io watcher object in stopped state. |
472 |
|
473 |
=item $iow = $iow->cb (sub { my ($iow, $reventmask) = @_; ... }) |
474 |
|
475 |
Set the callback to be called when io events are triggered. C<$reventmask> |
476 |
is a bitset as described in the C<events> method. |
477 |
|
478 |
=item $iow = $iow->fd ($fd) |
479 |
|
480 |
Set the filedescriptor (not handle) to watch. |
481 |
|
482 |
=item $iow = $iow->events ($eventmask) |
483 |
|
484 |
Set the event mask to watch. Bit #0 (value C<1>) enables watching for read |
485 |
data, Bit #1 (value C<2>) enables watching for write data. |
486 |
|
487 |
=item $iow = $iow->start |
488 |
|
489 |
Start watching for requested events on the given handle. |
490 |
|
491 |
=item $iow = $iow->stop |
492 |
|
493 |
Stop watching for events on the given filehandle. |
494 |
|
495 |
=back |
496 |
|
497 |
=head1 ENVIRONMENT |
498 |
|
499 |
=head2 URXVT_PERL_VERBOSITY |
500 |
|
501 |
This variable controls the verbosity level of the perl extension. Higher |
502 |
numbers indicate more verbose output. |
503 |
|
504 |
=over 4 |
505 |
|
506 |
=item 0 - only fatal messages |
507 |
|
508 |
=item 3 - script loading and management |
509 |
|
510 |
=item 10 - all events received |
511 |
|
512 |
=back |
513 |
|
514 |
=head1 AUTHOR |
515 |
|
516 |
Marc Lehmann <pcg@goof.com> |
517 |
http://software.schmorp.de/pkg/rxvt-unicode |
518 |
|
519 |
=cut |
520 |
|
521 |
1 |