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.2 by root, Mon Jan 2 15:36:27 2006 UTC vs.
Revision 1.9 by root, Mon Jan 2 20:40:20 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* Put your scripts into F<@@RXVT_LIBDIR@@/urxvt/perl/>, they will be loaded automatically.
8 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 9* Scripts are evaluated in a 'use strict' and 'use utf8' environment, and
13thus must be written in utf-8. 10thus 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
21Everytime a terminal object gets created, scripts specified via the
22C<perl> resource are associated with it.
23
24Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where
25scripts will be shared (But not enabled) for all terminals.
26
27=head2 General API Considerations
28
29All objects (such as terminals, time watchers etc.) are typical
30reference-to-hash objects. The hash can be used to store anything you
31like. All members starting with an underscore (such as C<_ptr> or
32C<_hook>) are reserved for internal uses and must not be accessed or
33modified).
34
35When objects are destroyed on the C++ side, the perl object hashes are
36emptied, so its best to store related objects such as time watchers and
37the like inside the terminal object so they get destroyed as soon as the
38terminal is destroyed.
22 39
23=head2 Hooks 40=head2 Hooks
24 41
25The following subroutines can be declared in loaded scripts, and will be called 42The following subroutines can be declared in loaded scripts, and will be called
26whenever the relevant event happens. 43whenever the relevant event happens.
119costs! The only time this is acceptable is when the terminal process 136costs! The only time this is acceptable is when the terminal process
120starts up. 137starts up.
121 138
122=item urxvt::warn $string 139=item urxvt::warn $string
123 140
124Calls C<rxvt_warn> witht eh given string which should not include a 141Calls C<rxvt_warn> with the given string which should not include a
125newline. The module also overwrites the C<warn> builtin with a function 142newline. The module also overwrites the C<warn> builtin with a function
126that calls this function. 143that calls this function.
127 144
128Using this function has the advantage that its output ends up in the 145Using this function has the advantage that its output ends up in the
129correct place, e.g. on stderr of the connecting urxvtc client. 146correct 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 147
136=item $time = urxvt::NOW 148=item $time = urxvt::NOW
137 149
138Returns the "current time" (as per the event loop). 150Returns the "current time" (as per the event loop).
139 151
157 unless $msg =~ /\n$/; 169 unless $msg =~ /\n$/;
158 urxvt::warn ($msg); 170 urxvt::warn ($msg);
159 }; 171 };
160} 172}
161 173
174my @hook_count;
162my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; 175my $verbosity = $ENV{URXVT_PERL_VERBOSITY};
163 176
164sub verbose { 177sub verbose {
165 my ($level, $msg) = @_; 178 my ($level, $msg) = @_;
166 warn "$msg\n"; #d# 179 warn "$msg\n" if $level <= $verbosity;
167} 180}
168 181
169my @invoke_cb; 182# find on_xxx subs in the package and register them
183# as hooks
184sub register_package($) {
185 my ($pkg) = @_;
186
187 for my $htype (0.. $#HOOKNAME) {
188 my $name = $HOOKNAME[$htype];
189
190 my $ref = $pkg->can ("on_" . lc $name)
191 or next;
192
193 $term->{_hook}[$htype]{$ref*1} = $ref;
194 $hook_count[$htype]++
195 or set_should_invoke $htype, 1;
196 }
197}
198
199my $script_pkg = "script0000";
200my %script_pkg;
201
202# load a single script into its own package, once only
203sub script_package($) {
204 my ($path) = @_;
205
206 $script_pkg{$path} ||= do {
207 my $pkg = "urxvt::" . ($script_pkg++);
208
209 verbose 3, "loading script '$path' into package '$pkg'";
210
211 open my $fh, "<:raw", $path
212 or die "$path: $!";
213
214 my $source = "package $pkg; use strict; use utf8;\n"
215 . "#line 1 \"$path\"\n{\n"
216 . (do { local $/; <$fh> })
217 . "\n};\n1";
218
219 eval $source or die "$path: $@";
220
221 $pkg
222 }
223}
170 224
171# called by the rxvt core 225# called by the rxvt core
172sub invoke { 226sub invoke {
173 local $term = shift; 227 local $term = shift;
174 my $htype = shift; 228 my $htype = shift;
175 229
176 my $cb = $invoke_cb[$htype]; 230 if ($htype == 0) { # INIT
231 my @dirs = ((split /:/, $term->resource ("perl_lib")), "$LIBDIR/perl");
232
233 for my $ext (split /:/, $term->resource ("perl_ext")) {
234 my @files = grep -f $_, map "$_/$ext", @dirs;
235
236 if (@files) {
237 register_package script_package $files[0];
238 } else {
239 warn "perl extension '$ext' not found in perl library search path\n";
240 }
241 }
242
243 } elsif ($htype == 1) { # DESTROY
244 if (my $hook = $term->{_hook}) {
245 for my $htype (0..$#$hook) {
246 $hook_count[$htype] -= scalar keys %{ $hook->[$htype] || {} }
247 or set_should_invoke $htype, 0;
248 }
249 }
250 }
251
252 my $cb = $term->{_hook}[$htype]
253 or return;
177 254
178 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")" 255 verbose 10, "$HOOKNAME[$htype] (" . (join ", ", $term, @_) . ")"
179 if $verbosity >= 10; 256 if $verbosity >= 10;
180 257
181 while (my ($k, $v) = each %$cb) { 258 while (my ($k, $v) = each %$cb) {
183 } 260 }
184 261
185 0 262 0
186} 263}
187 264
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-ext/*>;
230
231
232=back 265=back
233 266
234=head2 The C<urxvt::term> Class 267=head2 The C<urxvt::term> Class
235 268
236=over 4 269=over 4
270
271=item $value = $term->resource ($name[, $newval])
272
273Returns the current resource value associated with a given name and
274optionally sets a new value. Setting values is most useful in the C<init>
275hook. Unset resources are returned and accepted as C<undef>.
276
277The new value must be properly encoded to a suitable character encoding
278before passing it to this method. Similarly, the returned value may need
279to be converted from the used encoding to text.
280
281Resource names are as defined in F<src/rsinc.h>. Colours can be specified
282as resource names of the form C<< color+<index> >>, e.g. C<color+5>. (will
283likely change).
284
285Please note that resource strings will currently only be freed when the
286terminal is destroyed, so changing options frequently will eat memory.
287
288Here is a a likely non-exhaustive list of resource names, not all of which
289are supported in every build, please see the source to see the actual
290list:
291
292 answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont
293 borderLess color cursorBlink cursorUnderline cutchars delete_key
294 display_name embed ext_bwidth fade font geometry hold iconName
295 imFont imLocale inputMethod insecure int_bwidth intensityStyles
296 italicFont jumpScroll lineSpace loginShell mapAlert menu meta8 modifier
297 mouseWheelScrollPage name pastableTabs path perl_eval perl_ext
298 perl_lib pointerBlank pointerBlankDelay preeditType print_pipe pty_fd
299 reverseVideo saveLines scrollBar scrollBar_align scrollBar_floating
300 scrollBar_right scrollBar_thickness scrollTtyKeypress scrollTtyOutput
301 scrollWithBuffer scrollstyle secondaryScreen secondaryScroll selectstyle
302 shade term_name title transparent transparent_all tripleclickwords
303 utmpInhibit visualBell
304
305=cut
306
307sub urxvt::term::resource($$;$) {
308 my ($self, $name) = (shift, shift);
309 unshift @_, $self, $name, ($name =~ s/\s*\+\s*(\d+)$// ? $1 : 0);
310 goto &urxvt::term::_resource;
311}
237 312
238=item ($row, $col) = $term->selection_mark ([$row, $col]) 313=item ($row, $col) = $term->selection_mark ([$row, $col])
239 314
240=item ($row, $col) = $term->selection_beg ([$row, $col]) 315=item ($row, $col) = $term->selection_beg ([$row, $col])
241 316
263 my ($self, $x, $y, $text) = @_; 338 my ($self, $x, $y, $text) = @_;
264 339
265 my @lines = split /\n/, $text; 340 my @lines = split /\n/, $text;
266 341
267 my $w = 0; 342 my $w = 0;
268 for (map urxvt::wcswidth $_, @lines) { 343 for (map $self->strwidth ($_), @lines) {
269 $w = $_ if $w < $_; 344 $w = $_ if $w < $_;
270 } 345 }
271 346
272 $self->scr_overlay_new ($x, $y, $w, scalar @lines); 347 $self->scr_overlay_new ($x, $y, $w, scalar @lines);
273 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; 348 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines;
290position. 365position.
291 366
292=item $term->scr_overlay_set ($x, $y, $text) 367=item $term->scr_overlay_set ($x, $y, $text)
293 368
294Write a string at the given position into the overlay. 369Write a string at the given position into the overlay.
370
371=item $cellwidth = $term->strwidth $string
372
373Returns the number of screen-cells this string would need. Correctly
374accounts for wide and combining characters.
375
376=item $octets = $term->locale_encode $string
377
378Convert the given text string into the corresponding locale encoding.
379
380=item $string = $term->locale_decode $octets
381
382Convert the given locale-encoded octets into a perl string.
383
384=item $term->tt_write ($octets)
385
386Write the octets given in C<$data> to the tty (i.e. as program input). To
387pass characters instead of octets, you should convetr you strings first to
388the locale-specific encoding using C<< $term->locale_encode >>.
295 389
296=back 390=back
297 391
298=head2 The C<urxvt::timer> Class 392=head2 The C<urxvt::timer> Class
299 393
390 484
391Stop watching for events on the given filehandle. 485Stop watching for events on the given filehandle.
392 486
393=back 487=back
394 488
489=head1 ENVIRONMENT
490
491=head2 URXVT_PERL_VERBOSITY
492
493This variable controls the verbosity level of the perl extension. Higher
494numbers indicate more verbose output.
495
496=over 4
497
498=item 0 - only fatal messages
499
500=item 3 - script loading and management
501
502=item 10 - all events received
503
504=back
505
395=head1 AUTHOR 506=head1 AUTHOR
396 507
397 Marc Lehmann <pcg@goof.com> 508 Marc Lehmann <pcg@goof.com>
398 http://software.schmorp.de/pkg/rxvt-unicode 509 http://software.schmorp.de/pkg/rxvt-unicode
399 510

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines