1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | urxvt - rxvt-unicode's embedded perl interpreter |
3 | rxvtperl - 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 | * Put your scripts into F<@@RXVT_LIBDIR@@/urxvt/perl-ext/>, they will be loaded automatically. |
8 | |
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 |
9 | * Scripts are evaluated in a 'use strict' and 'use utf8' environment, and |
13 | thus must be written in utf-8. |
10 | thus must be encoded as UTF-8. |
14 | |
11 | |
15 | sub on_sel_grab { |
12 | sub on_sel_grab { |
16 | warn "you selected ", $_[0]->selection; |
13 | warn "you selected ", $_[0]->selection; |
|
|
14 | () |
17 | } |
15 | } |
18 | |
16 | |
19 | 1 |
17 | 1 |
20 | |
18 | |
21 | =head1 DESCRIPTION |
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. |
22 | |
44 | |
23 | =head2 Hooks |
45 | =head2 Hooks |
24 | |
46 | |
25 | The following subroutines can be declared in loaded scripts, and will be called |
47 | The following subroutines can be declared in loaded scripts, and will be called |
26 | whenever the relevant event happens. |
48 | whenever the relevant event happens. |
… | |
… | |
119 | costs! The only time this is acceptable is when the terminal process |
141 | costs! The only time this is acceptable is when the terminal process |
120 | starts up. |
142 | starts up. |
121 | |
143 | |
122 | =item urxvt::warn $string |
144 | =item urxvt::warn $string |
123 | |
145 | |
124 | Calls C<rxvt_warn> witht eh given string which should not include a |
146 | 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 |
147 | newline. The module also overwrites the C<warn> builtin with a function |
126 | that calls this function. |
148 | that calls this function. |
127 | |
149 | |
128 | Using this function has the advantage that its output ends up in the |
150 | 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. |
151 | 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 | |
152 | |
136 | =item $time = urxvt::NOW |
153 | =item $time = urxvt::NOW |
137 | |
154 | |
138 | Returns the "current time" (as per the event loop). |
155 | Returns the "current time" (as per the event loop). |
139 | |
156 | |
… | |
… | |
157 | unless $msg =~ /\n$/; |
174 | unless $msg =~ /\n$/; |
158 | urxvt::warn ($msg); |
175 | urxvt::warn ($msg); |
159 | }; |
176 | }; |
160 | } |
177 | } |
161 | |
178 | |
162 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; |
179 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; |
163 | |
180 | |
164 | sub verbose { |
181 | sub verbose { |
165 | my ($level, $msg) = @_; |
182 | my ($level, $msg) = @_; |
166 | warn "$msg\n"; #d# |
183 | warn "$msg\n" if $level < $verbosity; |
167 | } |
184 | } |
168 | |
185 | |
169 | my @invoke_cb; |
186 | my %hook_global; |
|
|
187 | my @hook_count; |
170 | |
188 | |
171 | # called by the rxvt core |
189 | # called by the rxvt core |
172 | sub invoke { |
190 | sub invoke { |
173 | local $term = shift; |
191 | local $term = shift; |
174 | my $htype = shift; |
192 | my $htype = shift; |
175 | |
193 | |
176 | my $cb = $invoke_cb[$htype]; |
|
|
177 | |
|
|
178 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
194 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
179 | if $verbosity >= 10; |
195 | if $verbosity >= 10; |
180 | |
196 | |
|
|
197 | for my $cb ($hook_global{_hook}[$htype], $term->{_hook}[$htype]) { |
|
|
198 | $cb or next; |
|
|
199 | |
181 | while (my ($k, $v) = each %$cb) { |
200 | while (my ($k, $v) = each %$cb) { |
182 | return 1 if $v->($term, @_); |
201 | return 1 if $v->($term, @_); |
|
|
202 | } |
183 | } |
203 | } |
184 | |
204 | |
185 | 0 |
205 | 0 |
186 | } |
206 | } |
187 | |
207 | |
188 | # find on_xxx subs in the package and register them |
208 | # find on_xxx subs in the package and register them |
189 | # as hooks |
209 | # as hooks |
190 | sub register_package($) { |
210 | sub register_package($) { |
191 | my ($pkg) = @_; |
211 | my ($pkg) = @_; |
192 | |
212 | |
193 | for my $hook (0.. $#HOOKNAME) { |
213 | for my $htype (0.. $#HOOKNAME) { |
194 | my $name = $HOOKNAME[$hook]; |
214 | my $name = $HOOKNAME[$htype]; |
195 | |
215 | |
196 | my $ref = $pkg->can ("on_" . lc $name) |
216 | my $ref = $pkg->can ("on_" . lc $name) |
197 | or next; |
217 | or next; |
198 | |
218 | |
199 | $invoke_cb[$hook]{$ref*1} = $ref; |
219 | $term->{_hook}[$htype]{$ref*1} = $ref; |
|
|
220 | $hook_count[$htype]++ |
200 | set_should_invoke $hook, 1; |
221 | or set_should_invoke $htype, 1; |
201 | } |
222 | } |
202 | } |
223 | } |
203 | |
224 | |
204 | my $script_pkg = "script0000"; |
225 | my $script_pkg = "script0000"; |
205 | my %script_pkg; |
226 | my %script_pkg; |
206 | |
227 | |
207 | # load a single script into its own package, once only |
228 | # load a single script into its own package, once only |
208 | sub load_script($) { |
229 | sub script_package($) { |
209 | my ($path) = @_; |
230 | my ($path) = @_; |
210 | |
231 | |
211 | $script_pkg{$path} ||= do { |
232 | $script_pkg{$path} ||= do { |
212 | my $pkg = $script_pkg++; |
233 | my $pkg = $script_pkg++; |
213 | verbose 3, "loading script '$path' into package '$pkg'"; |
234 | verbose 3, "loading script '$path' into package '$pkg'"; |
… | |
… | |
218 | eval "package $pkg; use strict; use utf8;\n" |
239 | eval "package $pkg; use strict; use utf8;\n" |
219 | . "#line 1 \"$path\"\n" |
240 | . "#line 1 \"$path\"\n" |
220 | . do { local $/; <$fh> } |
241 | . do { local $/; <$fh> } |
221 | or die "$path: $@"; |
242 | or die "$path: $@"; |
222 | |
243 | |
223 | register_package $pkg; |
|
|
224 | |
|
|
225 | $pkg |
244 | $pkg |
226 | }; |
245 | } |
227 | } |
246 | } |
228 | |
247 | |
229 | load_script $_ for grep -f $_, <$LIBDIR/perl-init/*>; |
248 | sub load_scripts($) { |
|
|
249 | my ($dir) = @_; |
230 | |
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 | } |
231 | |
285 | |
232 | =back |
286 | =back |
233 | |
287 | |
234 | =head2 The C<urxvt::term> Class |
288 | =head2 The C<urxvt::term> Class |
235 | |
289 | |
236 | =over 4 |
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 | } |
237 | |
333 | |
238 | =item ($row, $col) = $term->selection_mark ([$row, $col]) |
334 | =item ($row, $col) = $term->selection_mark ([$row, $col]) |
239 | |
335 | |
240 | =item ($row, $col) = $term->selection_beg ([$row, $col]) |
336 | =item ($row, $col) = $term->selection_beg ([$row, $col]) |
241 | |
337 | |
… | |
… | |
263 | my ($self, $x, $y, $text) = @_; |
359 | my ($self, $x, $y, $text) = @_; |
264 | |
360 | |
265 | my @lines = split /\n/, $text; |
361 | my @lines = split /\n/, $text; |
266 | |
362 | |
267 | my $w = 0; |
363 | my $w = 0; |
268 | for (map urxvt::wcswidth $_, @lines) { |
364 | for (map $self->strwidth ($_), @lines) { |
269 | $w = $_ if $w < $_; |
365 | $w = $_ if $w < $_; |
270 | } |
366 | } |
271 | |
367 | |
272 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
368 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
273 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
369 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
… | |
… | |
290 | position. |
386 | position. |
291 | |
387 | |
292 | =item $term->scr_overlay_set ($x, $y, $text) |
388 | =item $term->scr_overlay_set ($x, $y, $text) |
293 | |
389 | |
294 | Write a string at the given position into the overlay. |
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 >>. |
295 | |
410 | |
296 | =back |
411 | =back |
297 | |
412 | |
298 | =head2 The C<urxvt::timer> Class |
413 | =head2 The C<urxvt::timer> Class |
299 | |
414 | |
… | |
… | |
390 | |
505 | |
391 | Stop watching for events on the given filehandle. |
506 | Stop watching for events on the given filehandle. |
392 | |
507 | |
393 | =back |
508 | =back |
394 | |
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 | |
395 | =head1 AUTHOR |
527 | =head1 AUTHOR |
396 | |
528 | |
397 | Marc Lehmann <pcg@goof.com> |
529 | Marc Lehmann <pcg@goof.com> |
398 | http://software.schmorp.de/pkg/rxvt-unicode |
530 | http://software.schmorp.de/pkg/rxvt-unicode |
399 | |
531 | |