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