… | |
… | |
3 | rxvtperl - 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 F<@@RXVT_LIBDIR@@/urxvt/perl-ext/>, they will be loaded automatically. |
7 | * Put your scripts into F<@@RXVT_LIBDIR@@/urxvt/perl-ext/>, they will be loaded automatically. |
8 | |
|
|
9 | * Each script will only be loaded once, even in urxvtd, and will be valid |
|
|
10 | globally. |
|
|
11 | |
8 | |
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 encoded as UTF-8. |
10 | thus must be encoded as UTF-8. |
14 | |
11 | |
15 | sub on_sel_grab { |
12 | sub on_sel_grab { |
… | |
… | |
19 | |
16 | |
20 | 1 |
17 | 1 |
21 | |
18 | |
22 | =head1 DESCRIPTION |
19 | =head1 DESCRIPTION |
23 | |
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 | |
24 | =head2 Hooks |
45 | =head2 Hooks |
25 | |
46 | |
26 | 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 |
27 | whenever the relevant event happens. |
48 | whenever the relevant event happens. |
28 | |
49 | |
… | |
… | |
120 | 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 |
121 | starts up. |
142 | starts up. |
122 | |
143 | |
123 | =item urxvt::warn $string |
144 | =item urxvt::warn $string |
124 | |
145 | |
125 | 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 |
126 | 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 |
127 | that calls this function. |
148 | that calls this function. |
128 | |
149 | |
129 | 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 |
130 | correct place, e.g. on stderr of the connecting urxvtc client. |
151 | correct place, e.g. on stderr of the connecting urxvtc client. |
131 | |
|
|
132 | =item $cellwidth = urxvt::wcswidth $string |
|
|
133 | |
|
|
134 | Returns the number of screen-cells this string would need. Correctly |
|
|
135 | accounts for wide and combining characters. |
|
|
136 | |
152 | |
137 | =item $time = urxvt::NOW |
153 | =item $time = urxvt::NOW |
138 | |
154 | |
139 | Returns the "current time" (as per the event loop). |
155 | Returns the "current time" (as per the event loop). |
140 | |
156 | |
… | |
… | |
158 | unless $msg =~ /\n$/; |
174 | unless $msg =~ /\n$/; |
159 | urxvt::warn ($msg); |
175 | urxvt::warn ($msg); |
160 | }; |
176 | }; |
161 | } |
177 | } |
162 | |
178 | |
163 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; |
179 | my $verbosity = $ENV{URXVT_PERL_VERBOSITY}; |
164 | |
180 | |
165 | sub verbose { |
181 | sub verbose { |
166 | my ($level, $msg) = @_; |
182 | my ($level, $msg) = @_; |
167 | warn "$msg\n"; #d# |
183 | warn "$msg\n" if $level < $verbosity; |
168 | } |
184 | } |
169 | |
185 | |
170 | my @invoke_cb; |
186 | my %hook_global; |
|
|
187 | my @hook_count; |
171 | |
188 | |
172 | # called by the rxvt core |
189 | # called by the rxvt core |
173 | sub invoke { |
190 | sub invoke { |
174 | local $term = shift; |
191 | local $term = shift; |
175 | my $htype = shift; |
192 | my $htype = shift; |
176 | |
193 | |
177 | my $cb = $invoke_cb[$htype]; |
|
|
178 | |
|
|
179 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
194 | verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" |
180 | if $verbosity >= 10; |
195 | if $verbosity >= 10; |
181 | |
196 | |
|
|
197 | for my $cb ($hook_global{_hook}[$htype], $term->{_hook}[$htype]) { |
|
|
198 | $cb or next; |
|
|
199 | |
182 | while (my ($k, $v) = each %$cb) { |
200 | while (my ($k, $v) = each %$cb) { |
183 | return 1 if $v->($term, @_); |
201 | return 1 if $v->($term, @_); |
|
|
202 | } |
184 | } |
203 | } |
185 | |
204 | |
186 | 0 |
205 | 0 |
187 | } |
206 | } |
188 | |
207 | |
189 | # find on_xxx subs in the package and register them |
208 | # find on_xxx subs in the package and register them |
190 | # as hooks |
209 | # as hooks |
191 | sub register_package($) { |
210 | sub register_package($) { |
192 | my ($pkg) = @_; |
211 | my ($pkg) = @_; |
193 | |
212 | |
194 | for my $hook (0.. $#HOOKNAME) { |
213 | for my $htype (0.. $#HOOKNAME) { |
195 | my $name = $HOOKNAME[$hook]; |
214 | my $name = $HOOKNAME[$htype]; |
196 | |
215 | |
197 | my $ref = $pkg->can ("on_" . lc $name) |
216 | my $ref = $pkg->can ("on_" . lc $name) |
198 | or next; |
217 | or next; |
199 | |
218 | |
200 | $invoke_cb[$hook]{$ref*1} = $ref; |
219 | $term->{_hook}[$htype]{$ref*1} = $ref; |
|
|
220 | $hook_count[$htype]++ |
201 | set_should_invoke $hook, 1; |
221 | or set_should_invoke $htype, 1; |
202 | } |
222 | } |
203 | } |
223 | } |
204 | |
224 | |
205 | my $script_pkg = "script0000"; |
225 | my $script_pkg = "script0000"; |
206 | my %script_pkg; |
226 | my %script_pkg; |
207 | |
227 | |
208 | # load a single script into its own package, once only |
228 | # load a single script into its own package, once only |
209 | sub load_script($) { |
229 | sub script_package($) { |
210 | my ($path) = @_; |
230 | my ($path) = @_; |
211 | |
231 | |
212 | $script_pkg{$path} ||= do { |
232 | $script_pkg{$path} ||= do { |
213 | my $pkg = $script_pkg++; |
233 | my $pkg = $script_pkg++; |
214 | verbose 3, "loading script '$path' into package '$pkg'"; |
234 | verbose 3, "loading script '$path' into package '$pkg'"; |
… | |
… | |
219 | eval "package $pkg; use strict; use utf8;\n" |
239 | eval "package $pkg; use strict; use utf8;\n" |
220 | . "#line 1 \"$path\"\n" |
240 | . "#line 1 \"$path\"\n" |
221 | . do { local $/; <$fh> } |
241 | . do { local $/; <$fh> } |
222 | or die "$path: $@"; |
242 | or die "$path: $@"; |
223 | |
243 | |
224 | register_package $pkg; |
|
|
225 | |
|
|
226 | $pkg |
244 | $pkg |
227 | }; |
245 | } |
228 | } |
246 | } |
229 | |
247 | |
230 | load_script $_ for grep -f $_, <$LIBDIR/perl-ext/*>; |
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 | } |
231 | |
285 | |
232 | =back |
286 | =back |
233 | |
287 | |
234 | =head2 The C<urxvt::term> Class |
288 | =head2 The C<urxvt::term> Class |
235 | |
289 | |
… | |
… | |
249 | as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will |
303 | as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will |
250 | likely change). |
304 | likely change). |
251 | |
305 | |
252 | Please note that resource strings will currently only be freed when the |
306 | Please note that resource strings will currently only be freed when the |
253 | terminal is destroyed, so changing options frequently will eat memory. |
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 |
254 | |
325 | |
255 | =cut |
326 | =cut |
256 | |
327 | |
257 | sub urxvt::term::resource($$;$) { |
328 | sub urxvt::term::resource($$;$) { |
258 | my ($self, $name) = (shift, shift); |
329 | my ($self, $name) = (shift, shift); |
… | |
… | |
288 | my ($self, $x, $y, $text) = @_; |
359 | my ($self, $x, $y, $text) = @_; |
289 | |
360 | |
290 | my @lines = split /\n/, $text; |
361 | my @lines = split /\n/, $text; |
291 | |
362 | |
292 | my $w = 0; |
363 | my $w = 0; |
293 | for (map urxvt::wcswidth $_, @lines) { |
364 | for (map $self->strwidth ($_), @lines) { |
294 | $w = $_ if $w < $_; |
365 | $w = $_ if $w < $_; |
295 | } |
366 | } |
296 | |
367 | |
297 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
368 | $self->scr_overlay_new ($x, $y, $w, scalar @lines); |
298 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
369 | $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; |
… | |
… | |
315 | position. |
386 | position. |
316 | |
387 | |
317 | =item $term->scr_overlay_set ($x, $y, $text) |
388 | =item $term->scr_overlay_set ($x, $y, $text) |
318 | |
389 | |
319 | 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 >>. |
320 | |
410 | |
321 | =back |
411 | =back |
322 | |
412 | |
323 | =head2 The C<urxvt::timer> Class |
413 | =head2 The C<urxvt::timer> Class |
324 | |
414 | |