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.5 by root, Mon Jan 2 17:20:00 2006 UTC vs.
Revision 1.7 by root, Mon Jan 2 19:05:05 2006 UTC

3rxvtperl - rxvt-unicode's embedded perl interpreter 3rxvtperl - 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
10globally.
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
13thus must be encoded as UTF-8. 10thus 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
21On startup, @@RXVT_NAME@@ will scan F<@@RXVT_LIBDIR@@/urxvt/perl-ext/>
22for files and will load them. Everytime a terminal object gets created,
23the directory specified by the C<perl-lib> resource will be additionally
24scanned.
25
26Each script will only ever be loaded once, even in @@RXVT_NAME@@d, where
27scripts will be shared for all terminals.
28
29Hooks in scripts specified by C<perl-lib> will only be called for the
30terminals created with that specific option value.
31
32=head2 General API Considerations
33
34All objects (such as terminals, time watchers etc.) are typical
35reference-to-hash objects. The hash can be used to store anything you
36like. All members starting with an underscore (such as C<_ptr> or
37C<_hook>) are reserved for internal uses and must not be accessed or
38modified).
39
40When objects are destroyed on the C++ side, the perl object hashes are
41emptied, so its best to store related objects such as time watchers and
42the like inside the terminal object so they get destroyed as soon as the
43terminal is destroyed.
44
24=head2 Hooks 45=head2 Hooks
25 46
26The following subroutines can be declared in loaded scripts, and will be called 47The following subroutines can be declared in loaded scripts, and will be called
27whenever the relevant event happens. 48whenever the relevant event happens.
28 49
120costs! The only time this is acceptable is when the terminal process 141costs! The only time this is acceptable is when the terminal process
121starts up. 142starts up.
122 143
123=item urxvt::warn $string 144=item urxvt::warn $string
124 145
125Calls C<rxvt_warn> witht eh given string which should not include a 146Calls C<rxvt_warn> with the given string which should not include a
126newline. The module also overwrites the C<warn> builtin with a function 147newline. The module also overwrites the C<warn> builtin with a function
127that calls this function. 148that calls this function.
128 149
129Using this function has the advantage that its output ends up in the 150Using this function has the advantage that its output ends up in the
130correct place, e.g. on stderr of the connecting urxvtc client. 151correct place, e.g. on stderr of the connecting urxvtc client.
131
132=item $cellwidth = urxvt::wcswidth $string
133
134Returns the number of screen-cells this string would need. Correctly
135accounts for wide and combining characters.
136 152
137=item $time = urxvt::NOW 153=item $time = urxvt::NOW
138 154
139Returns the "current time" (as per the event loop). 155Returns 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
163my $verbosity = $ENV{URXVT_PERL_VERBOSITY} || 10; 179my $verbosity = $ENV{URXVT_PERL_VERBOSITY};
164 180
165sub verbose { 181sub 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
170my @invoke_cb; 186my %hook_global;
187my @hook_count;
171 188
172# called by the rxvt core 189# called by the rxvt core
173sub invoke { 190sub 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
191sub register_package($) { 210sub 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
205my $script_pkg = "script0000"; 225my $script_pkg = "script0000";
206my %script_pkg; 226my %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
209sub load_script($) { 229sub 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
230load_script $_ for grep -f $_, <$LIBDIR/perl-ext/*>; 248sub 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
258sub on_init {
259 my ($term) = @_;
260
261 my $libdir = $term->resource ("perl_lib");
262
263 load_scripts $libdir
264 if defined $libdir;
265}
266
267sub 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
259 answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont 313 answerbackstring backgroundPixmap backspace_key boldFont boldItalicFont
260 borderLess color cursorBlink cursorUnderline cutchars delete_key 314 borderLess color cursorBlink cursorUnderline cutchars delete_key
261 display_name embed ext_bwidth fade font geometry hold iconName 315 display_name embed ext_bwidth fade font geometry hold iconName
262 imFont imLocale inputMethod insecure int_bwidth intensityStyles 316 imFont imLocale inputMethod insecure int_bwidth intensityStyles
263 italicFont jumpScroll lineSpace loginShell mapAlert menu meta8 317 italicFont jumpScroll lineSpace loginShell mapAlert menu meta8
264 modifier mouseWheelScrollPage name pastableTabs path pointerBlank 318 modifier mouseWheelScrollPage name pastableTabs path perl perl_eval
265 pointerBlankDelay preeditType print_pipe pty_fd reverseVideo saveLines 319 perl_lib pointerBlank pointerBlankDelay preeditType print_pipe pty_fd
266 scrollBar scrollBar_align scrollBar_floating scrollBar_right 320 reverseVideo saveLines scrollBar scrollBar_align scrollBar_floating
267 scrollBar_thickness scrollTtyKeypress scrollTtyOutput scrollWithBuffer 321 scrollBar_right scrollBar_thickness scrollTtyKeypress scrollTtyOutput
268 scrollstyle secondaryScreen secondaryScroll selectstyle shade term_name 322 scrollWithBuffer scrollstyle secondaryScreen secondaryScroll selectstyle
269 title transparent transparent_all tripleclickwords utmpInhibit 323 shade term_name title transparent transparent_all tripleclickwords
270 visualBell 324 utmpInhibit visualBell
271 325
272=cut 326=cut
273 327
274sub urxvt::term::resource($$;$) { 328sub urxvt::term::resource($$;$) {
275 my ($self, $name) = (shift, shift); 329 my ($self, $name) = (shift, shift);
305 my ($self, $x, $y, $text) = @_; 359 my ($self, $x, $y, $text) = @_;
306 360
307 my @lines = split /\n/, $text; 361 my @lines = split /\n/, $text;
308 362
309 my $w = 0; 363 my $w = 0;
310 for (map urxvt::wcswidth $_, @lines) { 364 for (map $self->strwidth ($_), @lines) {
311 $w = $_ if $w < $_; 365 $w = $_ if $w < $_;
312 } 366 }
313 367
314 $self->scr_overlay_new ($x, $y, $w, scalar @lines); 368 $self->scr_overlay_new ($x, $y, $w, scalar @lines);
315 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines; 369 $self->scr_overlay_set (0, $_, $lines[$_]) for 0.. $#lines;
332position. 386position.
333 387
334=item $term->scr_overlay_set ($x, $y, $text) 388=item $term->scr_overlay_set ($x, $y, $text)
335 389
336Write a string at the given position into the overlay. 390Write a string at the given position into the overlay.
391
392=item $cellwidth = $term->strwidth $string
393
394Returns the number of screen-cells this string would need. Correctly
395accounts for wide and combining characters.
396
397=item $octets = $term->locale_encode $string
398
399Convert the given text string into the corresponding locale encoding.
400
401=item $string = $term->locale_decode $octets
402
403Convert the given locale-encoded octets into a perl string.
404
405=item $term->tt_write ($octets)
406
407Write the octets given in C<$data> to the tty (i.e. as program input). To
408pass characters instead of octets, you should convetr you strings first to
409the locale-specific encoding using C<< $term->locale_encode >>.
337 410
338=back 411=back
339 412
340=head2 The C<urxvt::timer> Class 413=head2 The C<urxvt::timer> Class
341 414

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines