3 rxvtperl - rxvt-unicode's embedded perl interpreter
7 # create a file grab_test in $HOME:
10 warn "you selected ", $_[0]->selection;
14 # start a @@RXVT_NAME@@ using it:
16 @@RXVT_NAME@@ --perl-lib $HOME -pe grab_test
20 Everytime a terminal object gets created, scripts specified via the
21 C<perl> resource are loaded and associated with it.
23 Scripts are compiled in a 'use strict' and 'use utf8' environment, and
24 thus must be encoded as UTF-8.
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.
29 =head2 General API Considerations
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
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.
44 The following subroutines can be declared in loaded scripts, and will be called
45 whenever the relevant event happens.
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.
51 When in doubt, return a false value (preferably C<()>).
57 Called after a new terminal object has been initialized, but before
58 windows are created or the command gets run.
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
68 Called at the very end of initialisation of a new terminal, just before
69 returning to the mainloop.
71 =item on_sel_make $term, $eventtime
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.
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 >>.
80 =item on_sel_grab $term, $eventtime
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 >>.
86 Returning a true value aborts selection grabbing. It will still be hilighted.
88 =item on_focus_in $term
90 Called whenever the window gets the keyboard focus, before urxvt does
93 =item on_focus_out $term
95 Called wheneever the window loses keyboard focus, before urxvt does focus
98 =item on_view_change $term, $offset
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.
104 =item on_scroll_back $term, $lines, $saved
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.
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.
114 =item on_tty_activity $term *NYI*
116 Called whenever the program(s) running in the urxvt window send output.
118 =item on_refresh_begin $term
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.
125 =item on_refresh_end $term
127 Called just after the screen gets redrawn. See C<on_refresh_begin>.
131 =head2 Functions in the C<urxvt> Package
135 =item urxvt::fatal $errormessage
137 Fatally aborts execution with the given error message. Avoid at all
138 costs! The only time this is acceptable is when the terminal process
141 =item urxvt::warn $string
143 Calls C<rxvt_warn> with the given string which should not include a
144 newline. The module also overwrites the C<warn> builtin with a function
145 that calls this function.
147 Using this function has the advantage that its output ends up in the
148 correct place, e.g. on stderr of the connecting urxvtc client.
150 =item $time = urxvt::NOW
152 Returns the "current time" (as per the event loop).
167 # overwrite perl's warn
168 *CORE::GLOBAL::warn = sub {
169 my $msg = join "", @_;
171 unless $msg =~ /\n$/;
177 my $verbosity = $ENV{URXVT_PERL_VERBOSITY};
180 my ($level, $msg) = @_;
181 warn "$msg\n" if $level <= $verbosity;
184 # find on_xxx subs in the package and register them
186 sub register_package($) {
189 for my $htype (0.. $#HOOKNAME) {
190 my $name = $HOOKNAME[$htype];
192 my $ref = $pkg->can ("on_" . lc $name)
195 $term->{_hook}[$htype]{$ref*1} = $ref;
196 $hook_count[$htype]++
197 or set_should_invoke $htype, 1;
201 my $script_pkg = "script0000";
204 # load a single script into its own package, once only
205 sub script_package($) {
208 $script_pkg{$path} ||= do {
209 my $pkg = "urxvt::" . ($script_pkg++);
211 verbose 3, "loading script '$path' into package '$pkg'";
213 open my $fh, "<:raw", $path
216 my $source = "package $pkg; use strict; use utf8;\n"
217 . "#line 1 \"$path\"\n{\n"
218 . (do { local $/; <$fh> })
221 eval $source or die "$path: $@";
227 # called by the rxvt core
232 if ($htype == 0) { # INIT
233 my @dirs = ((split /:/, $term->resource ("perl_lib")), "$LIBDIR/perl");
235 for my $ext (split /:/, $term->resource ("perl_ext")) {
236 my @files = grep -f $_, map "$_/$ext", @dirs;
239 register_package script_package $files[0];
241 warn "perl extension '$ext' not found in perl library search path\n";
245 } elsif ($htype == 1) { # DESTROY
246 if (my $hook = $term->{_hook}) {
247 for my $htype (0..$#$hook) {
248 $hook_count[$htype] -= scalar keys %{ $hook->[$htype] || {} }
249 or set_should_invoke $htype, 0;
254 my $cb = $term->{_hook}[$htype]
257 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")"
260 while (my ($k, $v) = each %$cb) {
261 return 1 if $v->($term, @_);
269 =head2 The C<urxvt::term> Class
273 =item $value = $term->resource ($name[, $newval])
275 Returns the current resource value associated with a given name and
276 optionally sets a new value. Setting values is most useful in the C<init>
277 hook. Unset resources are returned and accepted as C<undef>.
279 The new value must be properly encoded to a suitable character encoding
280 before passing it to this method. Similarly, the returned value may need
281 to be converted from the used encoding to text.
283 Resource names are as defined in F<src/rsinc.h>. Colours can be specified
284 as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will
287 Please note that resource strings will currently only be freed when the
288 terminal is destroyed, so changing options frequently will eat memory.
290 Here is a a likely non-exhaustive list of resource names, not all of which
291 are supported in every build, please see the source to see the actual
294 answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont
295 borderLess color cursorBlink cursorUnderline cutchars delete_key
296 display_name embed ext_bwidth fade font geometry hold iconName
297 imFont imLocale inputMethod insecure int_bwidth intensityStyles
298 italicFont jumpScroll lineSpace loginShell mapAlert menu meta8 modifier
299 mouseWheelScrollPage name pastableTabs path perl_eval perl_ext
300 perl_lib pointerBlank pointerBlankDelay preeditType print_pipe pty_fd
301 reverseVideo saveLines scrollBar scrollBar_align scrollBar_floating
302 scrollBar_right scrollBar_thickness scrollTtyKeypress scrollTtyOutput
303 scrollWithBuffer scrollstyle secondaryScreen secondaryScroll selectstyle
304 shade term_name title transparent transparent_all tripleclickwords
305 utmpInhibit visualBell
309 sub urxvt::term::resource($$;$) {
310 my ($self, $name) = (shift, shift);
311 unshift @_, $self, $name, ($name =~ s/\s*\+\s*(\d+)$// ? $1 : 0);
312 goto &urxvt::term::_resource;
315 =item ($row, $col) = $term->selection_mark ([$row, $col])
317 =item ($row, $col) = $term->selection_beg ([$row, $col])
319 =item ($row, $col) = $term->selection_end ([$row, $col])
321 Return the current values of the selection mark, begin or end positions,
322 and optionally set them to new values.
324 =item $success = $term->selection_grab ($eventtime)
326 Try to request the primary selection from the server (for example, as set
329 =item $oldtext = $term->selection ([$newtext])
331 Return the current selection text and optionally replace it by C<$newtext>.
333 =item $term->scr_overlay ($x, $y, $text)
335 Create a simple multi-line overlay box. See the next method for details.
339 sub urxvt::term::scr_overlay {
340 my ($self, $x, $y, $text) = @_;
342 my @lines = split /\n/, $text;
345 for (map $self->strwidth ($_), @lines) {
349 $self->scr_overlay_new ($x, $y, $w, scalar @lines);
350 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines;
353 =item $term->scr_overlay_new ($x, $y, $width, $height)
355 Create a new (empty) overlay at the given position with the given
356 width/height. A border will be put around the box. If either C<$x> or
357 C<$y> is negative, then this is counted from the right/bottom side,
360 =item $term->scr_overlay_off
362 Switch the overlay off again.
364 =item $term->scr_overlay_set_char ($x, $y, $char, $rend = OVERLAY_RSTYLE)
366 Put a single character (specified numerically) at the given overlay
369 =item $term->scr_overlay_set ($x, $y, $text)
371 Write a string at the given position into the overlay.
373 =item $cellwidth = $term->strwidth $string
375 Returns the number of screen-cells this string would need. Correctly
376 accounts for wide and combining characters.
378 =item $octets = $term->locale_encode $string
380 Convert the given text string into the corresponding locale encoding.
382 =item $string = $term->locale_decode $octets
384 Convert the given locale-encoded octets into a perl string.
386 =item $term->tt_write ($octets)
388 Write the octets given in C<$data> to the tty (i.e. as program input). To
389 pass characters instead of octets, you should convetr you strings first to
390 the locale-specific encoding using C<< $term->locale_encode >>.
394 =head2 The C<urxvt::timer> Class
396 This class implements timer watchers/events. Time is represented as a
397 fractional number of seconds since the epoch. Example:
399 # create a digital clock display in upper right corner
400 $term->{timer} = urxvt::timer
405 my $time = $timer->at;
406 $timer->start ($time + 1);
407 $self->scr_overlay (-1, 0,
408 POSIX::strftime "%H:%M:%S", localtime $time);
413 =item $timer = new urxvt::timer
415 Create a new timer object in stopped state.
417 =item $timer = $timer->cb (sub { my ($timer) = @_; ... })
419 Set the callback to be called when the timer triggers.
421 =item $tstamp = $timer->at
423 Return the time this watcher will fire next.
425 =item $timer = $timer->set ($tstamp)
427 Set the time the event is generated to $tstamp.
429 =item $timer = $timer->start
433 =item $timer = $timer->start ($tstamp)
435 Set the event trigger time to C<$tstamp> and start the timer.
437 =item $timer = $timer->stop
443 =head2 The C<urxvt::iow> Class
445 This class implements io watchers/events. Example:
447 $term->{socket} = ...
448 $term->{iow} = urxvt::iow
450 ->fd (fileno $term->{socket})
451 ->events (1) # wait for read data
454 my ($iow, $revents) = @_;
455 # $revents must be 1 here, no need to check
456 sysread $term->{socket}, my $buf, 8192
463 =item $iow = new urxvt::iow
465 Create a new io watcher object in stopped state.
467 =item $iow = $iow->cb (sub { my ($iow, $reventmask) = @_; ... })
469 Set the callback to be called when io events are triggered. C<$reventmask>
470 is a bitset as described in the C<events> method.
472 =item $iow = $iow->fd ($fd)
474 Set the filedescriptor (not handle) to watch.
476 =item $iow = $iow->events ($eventmask)
478 Set the event mask to watch. Bit #0 (value C<1>) enables watching for read
479 data, Bit #1 (value C<2>) enables watching for write data.
481 =item $iow = $iow->start
483 Start watching for requested events on the given handle.
485 =item $iow = $iow->stop
487 Stop watching for events on the given filehandle.
493 =head2 URXVT_PERL_VERBOSITY
495 This variable controls the verbosity level of the perl extension. Higher
496 numbers indicate more verbose output.
500 =item 0 - only fatal messages
502 =item 3 - script loading and management
504 =item 10 - all events received
510 Marc Lehmann <pcg@goof.com>
511 http://software.schmorp.de/pkg/rxvt-unicode