ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/urxvt.pm
Revision: 1.10
Committed: Mon Jan 2 20:47:52 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.9: +8 -6 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 # 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 =back
130
131 =head2 Functions in the C<urxvt> Package
132
133 =over 4
134
135 =item urxvt::fatal $errormessage
136
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
139 starts up.
140
141 =item urxvt::warn $string
142
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.
146
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.
149
150 =item $time = urxvt::NOW
151
152 Returns the "current time" (as per the event loop).
153
154 =cut
155
156 package urxvt;
157
158 use strict;
159
160 our $term;
161 our @HOOKNAME;
162 our $LIBDIR;
163
164 BEGIN {
165 urxvt->bootstrap;
166
167 # overwrite perl's warn
168 *CORE::GLOBAL::warn = sub {
169 my $msg = join "", @_;
170 $msg .= "\n"
171 unless $msg =~ /\n$/;
172 urxvt::warn ($msg);
173 };
174 }
175
176 my @hook_count;
177 my $verbosity = $ENV{URXVT_PERL_VERBOSITY};
178
179 sub verbose {
180 my ($level, $msg) = @_;
181 warn "$msg\n" if $level <= $verbosity;
182 }
183
184 # find on_xxx subs in the package and register them
185 # as hooks
186 sub register_package($) {
187 my ($pkg) = @_;
188
189 for my $htype (0.. $#HOOKNAME) {
190 my $name = $HOOKNAME[$htype];
191
192 my $ref = $pkg->can ("on_" . lc $name)
193 or next;
194
195 $term->{_hook}[$htype]{$ref*1} = $ref;
196 $hook_count[$htype]++
197 or set_should_invoke $htype, 1;
198 }
199 }
200
201 my $script_pkg = "script0000";
202 my %script_pkg;
203
204 # load a single script into its own package, once only
205 sub script_package($) {
206 my ($path) = @_;
207
208 $script_pkg{$path} ||= do {
209 my $pkg = "urxvt::" . ($script_pkg++);
210
211 verbose 3, "loading script '$path' into package '$pkg'";
212
213 open my $fh, "<:raw", $path
214 or die "$path: $!";
215
216 my $source = "package $pkg; use strict; use utf8;\n"
217 . "#line 1 \"$path\"\n{\n"
218 . (do { local $/; <$fh> })
219 . "\n};\n1";
220
221 eval $source or die "$path: $@";
222
223 $pkg
224 }
225 }
226
227 # called by the rxvt core
228 sub invoke {
229 local $term = shift;
230 my $htype = shift;
231
232 if ($htype == 0) { # INIT
233 my @dirs = ((split /:/, $term->resource ("perl_lib")), "$LIBDIR/perl");
234
235 for my $ext (split /:/, $term->resource ("perl_ext")) {
236 my @files = grep -f $_, map "$_/$ext", @dirs;
237
238 if (@files) {
239 register_package script_package $files[0];
240 } else {
241 warn "perl extension '$ext' not found in perl library search path\n";
242 }
243 }
244
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;
250 }
251 }
252 }
253
254 my $cb = $term->{_hook}[$htype]
255 or return;
256
257 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")"
258 if $verbosity >= 10;
259
260 while (my ($k, $v) = each %$cb) {
261 return 1 if $v->($term, @_);
262 }
263
264 0
265 }
266
267 =back
268
269 =head2 The C<urxvt::term> Class
270
271 =over 4
272
273 =item $value = $term->resource ($name[, $newval])
274
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>.
278
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.
282
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
285 likely change).
286
287 Please note that resource strings will currently only be freed when the
288 terminal is destroyed, so changing options frequently will eat memory.
289
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
292 list:
293
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
306
307 =cut
308
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;
313 }
314
315 =item ($row, $col) = $term->selection_mark ([$row, $col])
316
317 =item ($row, $col) = $term->selection_beg ([$row, $col])
318
319 =item ($row, $col) = $term->selection_end ([$row, $col])
320
321 Return the current values of the selection mark, begin or end positions,
322 and optionally set them to new values.
323
324 =item $success = $term->selection_grab ($eventtime)
325
326 Try to request the primary selection from the server (for example, as set
327 by the next method).
328
329 =item $oldtext = $term->selection ([$newtext])
330
331 Return the current selection text and optionally replace it by C<$newtext>.
332
333 =item $term->scr_overlay ($x, $y, $text)
334
335 Create a simple multi-line overlay box. See the next method for details.
336
337 =cut
338
339 sub urxvt::term::scr_overlay {
340 my ($self, $x, $y, $text) = @_;
341
342 my @lines = split /\n/, $text;
343
344 my $w = 0;
345 for (map $self->strwidth ($_), @lines) {
346 $w = $_ if $w < $_;
347 }
348
349 $self->scr_overlay_new ($x, $y, $w, scalar @lines);
350 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines;
351 }
352
353 =item $term->scr_overlay_new ($x, $y, $width, $height)
354
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,
358 respectively.
359
360 =item $term->scr_overlay_off
361
362 Switch the overlay off again.
363
364 =item $term->scr_overlay_set_char ($x, $y, $char, $rend = OVERLAY_RSTYLE)
365
366 Put a single character (specified numerically) at the given overlay
367 position.
368
369 =item $term->scr_overlay_set ($x, $y, $text)
370
371 Write a string at the given position into the overlay.
372
373 =item $cellwidth = $term->strwidth $string
374
375 Returns the number of screen-cells this string would need. Correctly
376 accounts for wide and combining characters.
377
378 =item $octets = $term->locale_encode $string
379
380 Convert the given text string into the corresponding locale encoding.
381
382 =item $string = $term->locale_decode $octets
383
384 Convert the given locale-encoded octets into a perl string.
385
386 =item $term->tt_write ($octets)
387
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 >>.
391
392 =back
393
394 =head2 The C<urxvt::timer> Class
395
396 This class implements timer watchers/events. Time is represented as a
397 fractional number of seconds since the epoch. Example:
398
399 # create a digital clock display in upper right corner
400 $term->{timer} = urxvt::timer
401 ->new
402 ->start (urxvt::NOW)
403 ->cb (sub {
404 my ($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);
409 });
410
411 =over 4
412
413 =item $timer = new urxvt::timer
414
415 Create a new timer object in stopped state.
416
417 =item $timer = $timer->cb (sub { my ($timer) = @_; ... })
418
419 Set the callback to be called when the timer triggers.
420
421 =item $tstamp = $timer->at
422
423 Return the time this watcher will fire next.
424
425 =item $timer = $timer->set ($tstamp)
426
427 Set the time the event is generated to $tstamp.
428
429 =item $timer = $timer->start
430
431 Start the timer.
432
433 =item $timer = $timer->start ($tstamp)
434
435 Set the event trigger time to C<$tstamp> and start the timer.
436
437 =item $timer = $timer->stop
438
439 Stop the timer.
440
441 =back
442
443 =head2 The C<urxvt::iow> Class
444
445 This class implements io watchers/events. Example:
446
447 $term->{socket} = ...
448 $term->{iow} = urxvt::iow
449 ->new
450 ->fd (fileno $term->{socket})
451 ->events (1) # wait for read data
452 ->start
453 ->cb (sub {
454 my ($iow, $revents) = @_;
455 # $revents must be 1 here, no need to check
456 sysread $term->{socket}, my $buf, 8192
457 or end-of-file;
458 });
459
460
461 =over 4
462
463 =item $iow = new urxvt::iow
464
465 Create a new io watcher object in stopped state.
466
467 =item $iow = $iow->cb (sub { my ($iow, $reventmask) = @_; ... })
468
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.
471
472 =item $iow = $iow->fd ($fd)
473
474 Set the filedescriptor (not handle) to watch.
475
476 =item $iow = $iow->events ($eventmask)
477
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.
480
481 =item $iow = $iow->start
482
483 Start watching for requested events on the given handle.
484
485 =item $iow = $iow->stop
486
487 Stop watching for events on the given filehandle.
488
489 =back
490
491 =head1 ENVIRONMENT
492
493 =head2 URXVT_PERL_VERBOSITY
494
495 This variable controls the verbosity level of the perl extension. Higher
496 numbers indicate more verbose output.
497
498 =over 4
499
500 =item 0 - only fatal messages
501
502 =item 3 - script loading and management
503
504 =item 10 - all events received
505
506 =back
507
508 =head1 AUTHOR
509
510 Marc Lehmann <pcg@goof.com>
511 http://software.schmorp.de/pkg/rxvt-unicode
512
513 =cut
514
515 1