1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | urxvt - rxvt-unicode's embedded perl interpreter |
3 | @@RXVT_NAME@@perl - rxvt-unicode's embedded perl interpreter |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | Put your scripts into $LIBDIR/perl-init/, they will be loaded automatically. |
7 | # create a file grab_test in $HOME: |
8 | |
|
|
9 | Each script will only be loaded once, even in urxvtd, and will be valid |
|
|
10 | globally. |
|
|
11 | |
|
|
12 | Scripts are evaluated in a 'use strict' and 'use utf8' environment, and |
|
|
13 | thus must be written in utf-8. |
|
|
14 | |
8 | |
15 | sub on_sel_grab { |
9 | sub on_sel_grab { |
16 | warn "you selected ", $_[0]->selection; |
10 | warn "you selected ", $_[0]->selection; |
|
|
11 | () |
17 | } |
12 | } |
18 | |
13 | |
19 | 1 |
14 | # start a @@RXVT_NAME@@ using it: |
|
|
15 | |
|
|
16 | @@RXVT_NAME@@ --perl-lib $HOME -pe grab_test |
20 | |
17 | |
21 | =head1 DESCRIPTION |
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. |
22 | |
41 | |
23 | =head2 Hooks |
42 | =head2 Hooks |
24 | |
43 | |
25 | The following subroutines can be declared in loaded scripts, and will be called |
44 | The following subroutines can be declared in loaded scripts, and will be called |
26 | whenever the relevant event happens. |
45 | whenever the relevant event happens. |
… | |
… | |
105 | |
124 | |
106 | =item on_refresh_end $term |
125 | =item on_refresh_end $term |
107 | |
126 | |
108 | Called just after the screen gets redrawn. See C<on_refresh_begin>. |
127 | Called just after the screen gets redrawn. See C<on_refresh_begin>. |
109 | |
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 | |
110 | =back |
135 | =back |
111 | |
136 | |
112 | =head2 Functions in the C<urxvt> Package |
137 | =head2 Functions in the C<urxvt> Package |
113 | |
138 | |
114 | =over 4 |
139 | =over 4 |
… | |
… | |
119 | costs! The only time this is acceptable is when the terminal process |
144 | costs! The only time this is acceptable is when the terminal process |
120 | starts up. |
145 | starts up. |
121 | |
146 | |
122 | =item urxvt::warn $string |
147 | =item urxvt::warn $string |
123 | |
148 | |
124 | Calls C<rxvt_warn> witht eh given string which should not include a |
149 | Calls C<rxvt_warn> with the given string which should not include a |
125 | newline. The module also overwrites the C<warn> builtin with a function |
150 | newline. The module also overwrites the C<warn> builtin with a function |
126 | that calls this function. |
151 | that calls this function. |
127 | |
152 | |
128 | Using this function has the advantage that its output ends up in the |
153 | Using this function has the advantage that its output ends up in the |
129 | correct place, e.g. on stderr of the connecting urxvtc client. |
154 | correct place, e.g. on stderr of the connecting urxvtc client. |
130 | |
|
|
131 | =item $cellwidth = urxvt::wcswidth $string |
|
|
132 | |
|
|
133 | Returns the number of screen-cells this string would need. Correctly |
|
|
134 | accounts for wide and combining characters. |
|
|
135 | |
155 | |
136 | =item $time = urxvt::NOW |
156 | =item $time = urxvt::NOW |
137 | |
157 | |
138 | Returns the "current time" (as per the event loop). |
158 | Returns the "current time" (as per the event loop). |
139 | |
159 | |
… | |
… | |
157 | unless $msg =~ /\n$/; |
177 | unless $msg =~ /\n$/; |
158 | urxvt::warn ($msg); |
178 | urxvt::warn ($msg); |
159 | }; |
179 | }; |
160 | } |
180 | } |
161 | |
181 | |
|
|
182 | my @hook_count; |
162 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; |
183 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; |
163 | |
184 | |
164 | sub verbose { |
185 | sub verbose { |
165 | my ($level, $msg) = @_; |
186 | my ($level, $msg) = @_; |
166 | warn "$msg\n"; #d# |
187 | warn "$msg\n" if $level <= $verbosity; |
167 | } |
188 | } |
168 | |
189 | |
169 | my @invoke_cb; |
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 | } |
170 | |
232 | |
171 | # called by the rxvt core |
233 | # called by the rxvt core |
172 | sub invoke { |
234 | sub invoke { |
173 | local $term = shift; |
235 | local $term = shift; |
174 | my $htype = shift; |
236 | my $htype = shift; |
175 | |
237 | |
176 | my $cb = $invoke_cb[$htype]; |
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; |
177 | |
262 | |
178 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
263 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
179 | if $verbosity >= 10; |
264 | if $verbosity >= 10; |
180 | |
265 | |
181 | while (my ($k, $v) = each %$cb) { |
266 | while (my ($k, $v) = each %$cb) { |
… | |
… | |
183 | } |
268 | } |
184 | |
269 | |
185 | 0 |
270 | 0 |
186 | } |
271 | } |
187 | |
272 | |
188 | # find on_xxx subs in the package and register them |
273 | =back |
189 | # as hooks |
|
|
190 | sub register_package($) { |
|
|
191 | my ($pkg) = @_; |
|
|
192 | |
274 | |
193 | for my $hook (0.. $#HOOKNAME) { |
275 | =head2 The C<urxvt::term> Class |
194 | my $name = $HOOKNAME[$hook]; |
|
|
195 | |
276 | |
196 | my $ref = $pkg->can ("on_" . lc $name) |
277 | =over 4 |
197 | or next; |
|
|
198 | |
278 | |
199 | $invoke_cb[$hook]{$ref*1} = $ref; |
279 | =item $value = $term->resource ($name[, $newval]) |
200 | set_should_invoke $hook, 1; |
280 | |
201 | } |
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; |
202 | } |
319 | } |
203 | |
|
|
204 | my $script_pkg = "script0000"; |
|
|
205 | my %script_pkg; |
|
|
206 | |
|
|
207 | # load a single script into its own package, once only |
|
|
208 | sub load_script($) { |
|
|
209 | my ($path) = @_; |
|
|
210 | |
|
|
211 | $script_pkg{$path} ||= do { |
|
|
212 | my $pkg = $script_pkg++; |
|
|
213 | verbose 3, "loading script '$path' into package '$pkg'"; |
|
|
214 | |
|
|
215 | open my $fh, "<:raw", $path |
|
|
216 | or die "$path: $!"; |
|
|
217 | |
|
|
218 | eval "package $pkg; use strict; use utf8;\n" |
|
|
219 | . "#line 1 \"$path\"\n" |
|
|
220 | . do { local $/; <$fh> } |
|
|
221 | or die "$path: $@"; |
|
|
222 | |
|
|
223 | register_package $pkg; |
|
|
224 | |
|
|
225 | $pkg |
|
|
226 | }; |
|
|
227 | } |
|
|
228 | |
|
|
229 | load_script $_ for grep -f $_, <$LIBDIR/perl-ext/*>; |
|
|
230 | |
|
|
231 | |
|
|
232 | =back |
|
|
233 | |
|
|
234 | =head2 The C<urxvt::term> Class |
|
|
235 | |
|
|
236 | =over 4 |
|
|
237 | |
320 | |
238 | =item ($row, $col) = $term->selection_mark ([$row, $col]) |
321 | =item ($row, $col) = $term->selection_mark ([$row, $col]) |
239 | |
322 | |
240 | =item ($row, $col) = $term->selection_beg ([$row, $col]) |
323 | =item ($row, $col) = $term->selection_beg ([$row, $col]) |
241 | |
324 | |
… | |
… | |
263 | my ($self, $x, $y, $text) = @_; |
346 | my ($self, $x, $y, $text) = @_; |
264 | |
347 | |
265 | my @lines = split /\n/, $text; |
348 | my @lines = split /\n/, $text; |
266 | |
349 | |
267 | my $w = 0; |
350 | my $w = 0; |
268 | for (map urxvt::wcswidth $_, @lines) { |
351 | for (map $self->strwidth ($_), @lines) { |
269 | $w = $_ if $w < $_; |
352 | $w = $_ if $w < $_; |
270 | } |
353 | } |
271 | |
354 | |
272 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
355 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
273 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
356 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
… | |
… | |
291 | |
374 | |
292 | =item $term->scr_overlay_set ($x, $y, $text) |
375 | =item $term->scr_overlay_set ($x, $y, $text) |
293 | |
376 | |
294 | Write a string at the given position into the overlay. |
377 | Write a string at the given position into the overlay. |
295 | |
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 convert your strings first |
|
|
396 | to the locale-specific encoding using C<< $term->locale_encode >>. |
|
|
397 | |
|
|
398 | =item $nrow = $term->nrow |
|
|
399 | |
|
|
400 | =item $ncol = $term->ncol |
|
|
401 | |
|
|
402 | Return the number of rows/columns of the terminal window (i.e. as |
|
|
403 | specified by C<-geometry>, excluding any scrollback). |
|
|
404 | |
|
|
405 | =item $nsaved = $term->nsaved |
|
|
406 | |
|
|
407 | Returns the number of lines in the scrollback buffer. |
|
|
408 | |
|
|
409 | =item $view_start = $term->view_start ([$newvalue]) |
|
|
410 | |
|
|
411 | Returns the negative row number of the topmost line. Minimum value is |
|
|
412 | C<0>, which displays the normal terminal contents. Larger values scroll |
|
|
413 | this many lines into the scrollback buffer. |
|
|
414 | |
|
|
415 | =item $text = $term->ROW_t ($row_number[, $new_text[, $start_col]]) |
|
|
416 | |
|
|
417 | Returns the text of the entire row with number C<$row_number>. Row C<0> |
|
|
418 | is the topmost terminal line, row C<< $term->$ncol-1 >> is the bottommost |
|
|
419 | terminal line. The scrollback buffer starts at line C<-1> and extends to |
|
|
420 | line C<< -$term->nsaved >>. |
|
|
421 | |
|
|
422 | If C<$new_text> is specified, it will replace characters in the current |
|
|
423 | line, starting at column C<$start_col> (default C<0>), which is useful |
|
|
424 | to replace only parts of a line. The font iindex in the rendition will |
|
|
425 | automatically be updated. |
|
|
426 | |
|
|
427 | C<$text> is in a special encoding: tabs and wide characters that use more |
|
|
428 | than one cell when displayed are padded with urxvt::NOCHAR characters |
|
|
429 | (C<chr 65535>). Characters with combining characters and other characters |
|
|
430 | that do not fit into the normal tetx encoding will be replaced with |
|
|
431 | characters in the private use area. |
|
|
432 | |
|
|
433 | You have to obey this encoding when changing text. The advantage is |
|
|
434 | that C<substr> and similar functions work on screen cells and not on |
|
|
435 | characters. |
|
|
436 | |
|
|
437 | The methods C<< $term->special_encode >> and C<< $term->special_decode >> |
|
|
438 | can be used to convert normal strings into this encoding and vice versa. |
|
|
439 | |
|
|
440 | =item $rend = $term->ROW_r ($row_number[, $new_rend[, $start_col]]) |
|
|
441 | |
|
|
442 | Like C<< $term->ROW_t >>, but returns an arrayref with rendition |
|
|
443 | bitsets. Rendition bitsets contain information about colour, font, font |
|
|
444 | styles and similar information. See also C<< $term->ROW_t >>. |
|
|
445 | |
|
|
446 | When setting rendition, the font mask will be ignored. |
|
|
447 | |
|
|
448 | See the section on RENDITION, below. |
|
|
449 | |
|
|
450 | =item $length = $term->ROW_l ($row_number[, $new_length]) |
|
|
451 | |
|
|
452 | Returns the number of screen cells that are in use ("the line length"). If |
|
|
453 | it is C<-1>, then the line is part of a multiple-row logical "line", which |
|
|
454 | means all characters are in use and it is continued on the next row. |
|
|
455 | |
|
|
456 | =item $text = $term->special_encode $string |
|
|
457 | |
|
|
458 | Converts a perl string into the special encoding used by rxvt-unicode, |
|
|
459 | where one character corresponds to one screen cell. See |
|
|
460 | C<< $term->ROW_t >> for details. |
|
|
461 | |
|
|
462 | =item $string = $term->special_decode $text |
|
|
463 | |
|
|
464 | Converts rxvt-unicodes text reprsentation into a perl string. See |
|
|
465 | C<< $term->ROW_t >> for details. |
|
|
466 | |
296 | =back |
467 | =back |
|
|
468 | |
|
|
469 | =head2 RENDITION |
|
|
470 | |
|
|
471 | Rendition bitsets contain information about colour, font, font styles and |
|
|
472 | similar information for each screen cell. |
|
|
473 | |
|
|
474 | The following "macros" deal with changes in rendition sets. You should |
|
|
475 | never just create a bitset, you should always modify an existing one, |
|
|
476 | as they contain important information required for correct operation of |
|
|
477 | rxvt-unicode. |
|
|
478 | |
|
|
479 | =over 4 |
|
|
480 | |
|
|
481 | =item $rend = urxvt::DEFAULT_RSTYLE |
|
|
482 | |
|
|
483 | Returns the default rendition, as used when the terminal is starting up or |
|
|
484 | being reset. Useful as a base |
|
|
485 | |
|
|
486 | =back |
|
|
487 | |
|
|
488 | =cut |
297 | |
489 | |
298 | =head2 The C<urxvt::timer> Class |
490 | =head2 The C<urxvt::timer> Class |
299 | |
491 | |
300 | This class implements timer watchers/events. Time is represented as a |
492 | This class implements timer watchers/events. Time is represented as a |
301 | fractional number of seconds since the epoch. Example: |
493 | fractional number of seconds since the epoch. Example: |
… | |
… | |
390 | |
582 | |
391 | Stop watching for events on the given filehandle. |
583 | Stop watching for events on the given filehandle. |
392 | |
584 | |
393 | =back |
585 | =back |
394 | |
586 | |
|
|
587 | =head1 ENVIRONMENT |
|
|
588 | |
|
|
589 | =head2 URXVT_PERL_VERBOSITY |
|
|
590 | |
|
|
591 | This variable controls the verbosity level of the perl extension. Higher |
|
|
592 | numbers indicate more verbose output. |
|
|
593 | |
|
|
594 | =over 4 |
|
|
595 | |
|
|
596 | =item 0 - only fatal messages |
|
|
597 | |
|
|
598 | =item 3 - script loading and management |
|
|
599 | |
|
|
600 | =item 10 - all events received |
|
|
601 | |
|
|
602 | =back |
|
|
603 | |
395 | =head1 AUTHOR |
604 | =head1 AUTHOR |
396 | |
605 | |
397 | Marc Lehmann <pcg@goof.com> |
606 | Marc Lehmann <pcg@goof.com> |
398 | http://software.schmorp.de/pkg/rxvt-unicode |
607 | http://software.schmorp.de/pkg/rxvt-unicode |
399 | |
608 | |