ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/urxvt.pm
Revision: 1.6
Committed: Mon Jan 2 18:20:23 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.5: +71 -18 lines
Log Message:
*** empty log message ***

File Contents

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