ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/urxvt.pm
(Generate patch)

Comparing rxvt-unicode/src/urxvt.pm (file contents):
Revision 1.1 by root, Mon Jan 2 15:35:43 2006 UTC vs.
Revision 1.10 by root, Mon Jan 2 20:47:52 2006 UTC

1=head1 NAME 1=head1 NAME
2 2
3urxvt - rxvt-unicode's embedded perl interpreter 3rxvtperl - rxvt-unicode's embedded perl interpreter
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7Put your scripts into $LIBDIR/perl-init/, they will be loaded automatically. 7 # create a file grab_test in $HOME:
8
9Each script will only be loaded once, even in urxvtd, and will be valid
10globally.
11
12Scripts are evaluated in a 'use strict' and 'use utf8' environment, and
13thus 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
20Everytime a terminal object gets created, scripts specified via the
21C<perl> resource are loaded and associated with it.
22
23Scripts are compiled in a 'use strict' and 'use utf8' environment, and
24thus must be encoded as UTF-8.
25
26Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where
27scripts will be shared (But not enabled) for all terminals.
28
29=head2 General API Considerations
30
31All objects (such as terminals, time watchers etc.) are typical
32reference-to-hash objects. The hash can be used to store anything you
33like. All members starting with an underscore (such as C<_ptr> or
34C<_hook>) are reserved for internal uses and must not be accessed or
35modified).
36
37When objects are destroyed on the C++ side, the perl object hashes are
38emptied, so its best to store related objects such as time watchers and
39the like inside the terminal object so they get destroyed as soon as the
40terminal is destroyed.
22 41
23=head2 Hooks 42=head2 Hooks
24 43
25The following subroutines can be declared in loaded scripts, and will be called 44The following subroutines can be declared in loaded scripts, and will be called
26whenever the relevant event happens. 45whenever the relevant event happens.
119costs! The only time this is acceptable is when the terminal process 138costs! The only time this is acceptable is when the terminal process
120starts up. 139starts up.
121 140
122=item urxvt::warn $string 141=item urxvt::warn $string
123 142
124Calls C<rxvt_warn> witht eh given string which should not include a 143Calls C<rxvt_warn> with the given string which should not include a
125newline. The module also overwrites the C<warn> builtin with a function 144newline. The module also overwrites the C<warn> builtin with a function
126that calls this function. 145that calls this function.
127 146
128Using this function has the advantage that its output ends up in the 147Using this function has the advantage that its output ends up in the
129correct place, e.g. on stderr of the connecting urxvtc client. 148correct place, e.g. on stderr of the connecting urxvtc client.
130
131=item $cellwidth = urxvt::wcswidth $string
132
133Returns the number of screen-cells this string would need. Correctly
134accounts for wide and combining characters.
135 149
136=item $time = urxvt::NOW 150=item $time = urxvt::NOW
137 151
138Returns the "current time" (as per the event loop). 152Returns the "current time" (as per the event loop).
139 153
157 unless $msg =~ /\n$/; 171 unless $msg =~ /\n$/;
158 urxvt::warn ($msg); 172 urxvt::warn ($msg);
159 }; 173 };
160} 174}
161 175
176my @hook_count;
162my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; 177my $verbosity = $ENV{URXVT_PERL_VERBOSITY};
163 178
164sub verbose { 179sub verbose {
165 my ($level, $msg) = @_; 180 my ($level, $msg) = @_;
166 warn "$msg\n"; #d# 181 warn "$msg\n" if $level <= $verbosity;
167} 182}
168 183
169my @invoke_cb; 184# find on_xxx subs in the package and register them
185# as hooks
186sub 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
201my $script_pkg = "script0000";
202my %script_pkg;
203
204# load a single script into its own package, once only
205sub 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}
170 226
171# called by the rxvt core 227# called by the rxvt core
172sub invoke { 228sub invoke {
173 local $term = shift; 229 local $term = shift;
174 my $htype = shift; 230 my $htype = shift;
175 231
176 my $cb = $invoke_cb[$htype]; 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;
177 256
178 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" 257 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")"
179 if $verbosity >= 10; 258 if $verbosity >= 10;
180 259
181 while (my ($k, $v) = each %$cb) { 260 while (my ($k, $v) = each %$cb) {
183 } 262 }
184 263
185 0 264 0
186} 265}
187 266
188# find on_xxx subs in the package and register them
189# as hooks
190sub register_package($) {
191 my ($pkg) = @_;
192
193 for my $hook (0.. $#HOOKNAME) {
194 my $name = $HOOKNAME[$hook];
195
196 my $ref = $pkg->can ("on_" . lc $name)
197 or next;
198
199 $invoke_cb[$hook]{$ref*1} = $ref;
200 set_should_invoke $hook, 1;
201 }
202}
203
204my $script_pkg = "script0000";
205my %script_pkg;
206
207# load a single script into its own package, once only
208sub 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
229load_script $_ for grep -f $_, <$LIBDIR/perl-init/*>;
230
231
232=back 267=back
233 268
234=head2 The C<urxvt::term> Class 269=head2 The C<urxvt::term> Class
235 270
236=over 4 271=over 4
272
273=item $value = $term->resource ($name[, $newval])
274
275Returns the current resource value associated with a given name and
276optionally sets a new value. Setting values is most useful in the C<init>
277hook. Unset resources are returned and accepted as C<undef>.
278
279The new value must be properly encoded to a suitable character encoding
280before passing it to this method. Similarly, the returned value may need
281to be converted from the used encoding to text.
282
283Resource names are as defined in F<src/rsinc.h>. Colours can be specified
284as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will
285likely change).
286
287Please note that resource strings will currently only be freed when the
288terminal is destroyed, so changing options frequently will eat memory.
289
290Here is a a likely non-exhaustive list of resource names, not all of which
291are supported in every build, please see the source to see the actual
292list:
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
309sub 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}
237 314
238=item ($row, $col) = $term->selection_mark ([$row, $col]) 315=item ($row, $col) = $term->selection_mark ([$row, $col])
239 316
240=item ($row, $col) = $term->selection_beg ([$row, $col]) 317=item ($row, $col) = $term->selection_beg ([$row, $col])
241 318
263 my ($self, $x, $y, $text) = @_; 340 my ($self, $x, $y, $text) = @_;
264 341
265 my @lines = split /\n/, $text; 342 my @lines = split /\n/, $text;
266 343
267 my $w = 0; 344 my $w = 0;
268 for (map urxvt::wcswidth $_, @lines) { 345 for (map $self->strwidth ($_), @lines) {
269 $w = $_ if $w < $_; 346 $w = $_ if $w < $_;
270 } 347 }
271 348
272 $self->scr_overlay_new ($x, $y, $w, scalar @lines); 349 $self->scr_overlay_new ($x, $y, $w, scalar @lines);
273 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; 350 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines;
290position. 367position.
291 368
292=item $term->scr_overlay_set ($x, $y, $text) 369=item $term->scr_overlay_set ($x, $y, $text)
293 370
294Write a string at the given position into the overlay. 371Write a string at the given position into the overlay.
372
373=item $cellwidth = $term->strwidth $string
374
375Returns the number of screen-cells this string would need. Correctly
376accounts for wide and combining characters.
377
378=item $octets = $term->locale_encode $string
379
380Convert the given text string into the corresponding locale encoding.
381
382=item $string = $term->locale_decode $octets
383
384Convert the given locale-encoded octets into a perl string.
385
386=item $term->tt_write ($octets)
387
388Write the octets given in C<$data> to the tty (i.e. as program input). To
389pass characters instead of octets, you should convetr you strings first to
390the locale-specific encoding using C<< $term->locale_encode >>.
295 391
296=back 392=back
297 393
298=head2 The C<urxvt::timer> Class 394=head2 The C<urxvt::timer> Class
299 395
390 486
391Stop watching for events on the given filehandle. 487Stop watching for events on the given filehandle.
392 488
393=back 489=back
394 490
491=head1 ENVIRONMENT
492
493=head2 URXVT_PERL_VERBOSITY
494
495This variable controls the verbosity level of the perl extension. Higher
496numbers 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
395=head1 AUTHOR 508=head1 AUTHOR
396 509
397 Marc Lehmann <pcg@goof.com> 510 Marc Lehmann <pcg@goof.com>
398 http://software.schmorp.de/pkg/rxvt-unicode 511 http://software.schmorp.de/pkg/rxvt-unicode
399 512

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines