Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | #!/usr/local/bin/perl |
2 | # | |
3 | # XS.pm : perl function definition for Term::ReadLine::Gnu | |
4 | # | |
5 | # $Id: XS.pm,v 1.18 2002-03-29 23:12:28-05 hiroo Exp $ | |
6 | # | |
7 | # Copyright (c) 2001 Hiroo Hayashi. All rights reserved. | |
8 | # | |
9 | # This program is free software; you can redistribute it and/or | |
10 | # modify it under the same terms as Perl itself. | |
11 | ||
12 | package Term::ReadLine::Gnu::XS; | |
13 | ||
14 | use Carp; | |
15 | use strict; | |
16 | use AutoLoader 'AUTOLOAD'; | |
17 | ||
18 | # make aliases | |
19 | use vars qw(%Attribs); | |
20 | *Attribs = \%Term::ReadLine::Gnu::Attribs; | |
21 | ||
22 | use vars qw(*read_history); | |
23 | *read_history = \&read_history_range; | |
24 | ||
25 | # alias for 8 characters limitation imposed by AutoSplit | |
26 | use vars qw(*rl_unbind_key *rl_unbind_function *rl_unbind_command | |
27 | *history_list *history_arg_extract); | |
28 | *rl_unbind_key = \&unbind_key; | |
29 | *rl_unbind_function = \&unbind_function; | |
30 | *rl_unbind_command = \&unbind_command; | |
31 | *history_list = \&hist_list; | |
32 | *history_arg_extract = \&hist_arg_extract; | |
33 | ||
34 | # For backward compatibility. Using these name (*_in_map) is deprecated. | |
35 | use vars qw(*rl_unbind_function_in_map *rl_unbind_command_in_map); | |
36 | *rl_unbind_function_in_map = \&unbind_function; | |
37 | *rl_unbind_command_in_map = \&unbind_command; | |
38 | ||
39 | rl_add_defun('history-expand-line', \&history_expand_line); | |
40 | # bind operate-and-get-next to \C-o by default for the compatibility | |
41 | # with bash and Term::ReadLine::Perl | |
42 | rl_add_defun('operate-and-get-next', \&operate_and_get_next, ord "\co"); | |
43 | rl_add_defun('display-readline-version', \&display_readline_version); | |
44 | rl_add_defun('change-ornaments', \&change_ornaments); | |
45 | ||
46 | # for ornaments() | |
47 | ||
48 | # Prompt-start, prompt-end, command-line-start, command-line-end | |
49 | # -- zero-width beautifies to emit around prompt and the command line. | |
50 | # string encoded: | |
51 | my $rl_term_set = ',,,'; | |
52 | ||
53 | # These variables are used by completion functions. Don't use for | |
54 | # other purpose. | |
55 | my $_i; | |
56 | my @_matches; | |
57 | my @_tstrs; | |
58 | my $_tstrs_init = 0; | |
59 | ||
60 | 1; | |
61 | ||
62 | # Uncomment the following line to enable AutoSplit. If you are using | |
63 | # AutoLoader.pm distributed with Perl 5.004 or earlier, you must | |
64 | # update AutoLoader.pm due to its bug. | |
65 | ||
66 | #__END__ | |
67 | \f | |
68 | ||
69 | # | |
70 | # Readline Library function wrappers | |
71 | # | |
72 | ||
73 | # Convert keymap name to Keymap if the argument is not reference to Keymap | |
74 | sub _str2map ($) { | |
75 | return ref $_[0] ? $_[0] | |
76 | : (rl_get_keymap_by_name($_[0]) || carp "unknown keymap name \`$_[0]\'\n"); | |
77 | } | |
78 | ||
79 | # Convert function name to Function if the argument is not reference | |
80 | # to Function | |
81 | sub _str2fn ($) { | |
82 | return ref $_[0] ? $_[0] | |
83 | : (rl_named_function($_[0]) || carp "unknown function name \`$_[0]\'\n"); | |
84 | } | |
85 | ||
86 | sub rl_copy_keymap ($) { return _rl_copy_keymap(_str2map($_[0])); } | |
87 | sub rl_discard_keymap ($) { return _rl_discard_keymap(_str2map($_[0])); } | |
88 | sub rl_set_keymap ($) { return _rl_set_keymap(_str2map($_[0])); } | |
89 | ||
90 | sub rl_bind_key ($$;$) { | |
91 | if (defined $_[2]) { | |
92 | return _rl_bind_key($_[0], _str2fn($_[1]), _str2map($_[2])); | |
93 | } else { | |
94 | return _rl_bind_key($_[0], _str2fn($_[1])); | |
95 | } | |
96 | } | |
97 | ||
98 | # rl_unbind_key | |
99 | sub unbind_key ($;$) { | |
100 | if (defined $_[1]) { | |
101 | return _rl_unbind_key($_[0], _str2map($_[1])); | |
102 | } else { | |
103 | return _rl_unbind_key($_[0]); | |
104 | } | |
105 | } | |
106 | ||
107 | # rl_unbind_function | |
108 | sub unbind_function ($;$) { | |
109 | # libreadline.* in Debian GNU/Linux 2.0 tells wrong value as '2.1-bash' | |
110 | my ($version) = $Attribs{library_version} | |
111 | =~ /(\d+\.\d+)/; | |
112 | if ($version < 2.2) { | |
113 | carp "rl_unbind_function() is not supported. Ignored\n"; | |
114 | return; | |
115 | } | |
116 | if (defined $_[1]) { | |
117 | return _rl_unbind_function($_[0], _str2map($_[1])); | |
118 | } else { | |
119 | return _rl_unbind_function($_[0]); | |
120 | } | |
121 | } | |
122 | ||
123 | # rl_unbind_command | |
124 | sub unbind_command ($;$) { | |
125 | my ($version) = $Attribs{library_version} | |
126 | =~ /(\d+\.\d+)/; | |
127 | if ($version < 2.2) { | |
128 | carp "rl_unbind_command() is not supported. Ignored\n"; | |
129 | return; | |
130 | } | |
131 | if (defined $_[1]) { | |
132 | return _rl_unbind_command($_[0], _str2map($_[1])); | |
133 | } else { | |
134 | return _rl_unbind_command($_[0]); | |
135 | } | |
136 | } | |
137 | ||
138 | sub rl_set_key ($$;$) { | |
139 | my ($version) = $Attribs{library_version} | |
140 | =~ /(\d+\.\d+)/; | |
141 | if ($version < 4.2) { | |
142 | carp "rl_set_key() is not supported. Ignored\n"; | |
143 | return; | |
144 | } | |
145 | if (defined $_[2]) { | |
146 | return _rl_set_key($_[0], _str2fn($_[1]), _str2map($_[2])); | |
147 | } else { | |
148 | return _rl_set_key($_[0], _str2fn($_[1])); | |
149 | } | |
150 | } | |
151 | ||
152 | sub rl_macro_bind ($$;$) { | |
153 | my ($version) = $Attribs{library_version} | |
154 | =~ /(\d+\.\d+)/; | |
155 | if (defined $_[2]) { | |
156 | return _rl_macro_bind($_[0], $_[1], _str2map($_[2])); | |
157 | } else { | |
158 | return _rl_macro_bind($_[0], $_[1]); | |
159 | } | |
160 | } | |
161 | ||
162 | sub rl_generic_bind ($$$;$) { | |
163 | if ($_[0] == Term::ReadLine::Gnu::ISFUNC) { | |
164 | if (defined $_[3]) { | |
165 | _rl_generic_bind_function($_[1], _str2fn($_[2]), _str2map($_[3])); | |
166 | } else { | |
167 | _rl_generic_bind_function($_[1], _str2fn($_[2])); | |
168 | } | |
169 | } elsif ($_[0] == Term::ReadLine::Gnu::ISKMAP) { | |
170 | if (defined $_[3]) { | |
171 | _rl_generic_bind_keymap($_[1], _str2map($_[2]), _str2map($_[3])); | |
172 | } else { | |
173 | _rl_generic_bind_keymap($_[1], _str2map($_[2])); | |
174 | } | |
175 | } elsif ($_[0] == Term::ReadLine::Gnu::ISMACR) { | |
176 | if (defined $_[3]) { | |
177 | _rl_generic_bind_macro($_[1], $_[2], _str2map($_[3])); | |
178 | } else { | |
179 | _rl_generic_bind_macro($_[1], $_[2]); | |
180 | } | |
181 | } else { | |
182 | carp("Term::ReadLine::Gnu::rl_generic_bind: invalid \`type\'\n"); | |
183 | } | |
184 | } | |
185 | ||
186 | sub rl_call_function ($;$$) { | |
187 | if (defined $_[2]) { | |
188 | return _rl_call_function(_str2fn($_[0]), $_[1], $_[2]); | |
189 | } elsif (defined $_[1]) { | |
190 | return _rl_call_function(_str2fn($_[0]), $_[1]); | |
191 | } else { | |
192 | return _rl_call_function(_str2fn($_[0])); | |
193 | } | |
194 | } | |
195 | ||
196 | sub rl_invoking_keyseqs ($;$) { | |
197 | if (defined $_[1]) { | |
198 | return _rl_invoking_keyseqs(_str2fn($_[0]), _str2map($_[1])); | |
199 | } else { | |
200 | return _rl_invoking_keyseqs(_str2fn($_[0])); | |
201 | } | |
202 | } | |
203 | ||
204 | sub rl_add_funmap_entry ($$) { | |
205 | my ($version) = $Attribs{library_version} | |
206 | =~ /(\d+\.\d+)/; | |
207 | if ($version < 4.2) { | |
208 | carp "rl_add_funmap_entry() is not supported. Ignored\n"; | |
209 | return; | |
210 | } | |
211 | return _rl_add_funmap_entry($_[0], _str2fn($_[1])); | |
212 | } | |
213 | ||
214 | sub rl_tty_set_default_bindings (;$) { | |
215 | if (defined $_[0]) { | |
216 | return _rl_tty_set_defaut_bindings(_str2map($_[1])); | |
217 | } else { | |
218 | return _rl_tty_set_defaut_bindings(); | |
219 | } | |
220 | } | |
221 | ||
222 | sub rl_message { | |
223 | my $fmt = shift; | |
224 | my $line = sprintf($fmt, @_); | |
225 | _rl_message($line); | |
226 | } | |
227 | ||
228 | # | |
229 | # for compatibility with Term::ReadLine::Perl | |
230 | # | |
231 | sub rl_filename_list { | |
232 | my ($text) = @_; | |
233 | ||
234 | # lcd : lowest common denominator | |
235 | my ($lcd, @matches) = rl_completion_matches($text, | |
236 | \&rl_filename_completion_function); | |
237 | return @matches ? @matches : $lcd; | |
238 | } | |
239 | ||
240 | # | |
241 | # History Library function wrappers | |
242 | # | |
243 | # history_list | |
244 | sub hist_list () { | |
245 | my ($i, $history_base, $history_length, @d); | |
246 | $history_base = $Attribs{history_base}; | |
247 | $history_length = $Attribs{history_length}; | |
248 | for ($i = $history_base; $i < $history_base + $history_length; $i++) { | |
249 | push(@d, history_get($i)); | |
250 | } | |
251 | @d; | |
252 | } | |
253 | ||
254 | # history_arg_extract | |
255 | sub hist_arg_extract ( ;$$$ ) { | |
256 | my ($line, $first, $last) = @_; | |
257 | $line = $_ unless defined $line; | |
258 | $first = 0 unless defined $first; | |
259 | $last = ord '$' unless defined $last; # ' | |
260 | $first = ord '$' if defined $first and $first eq '$'; # ' | |
261 | $last = ord '$' if defined $last and $last eq '$'; # ' | |
262 | &_history_arg_extract($line, $first, $last); | |
263 | } | |
264 | ||
265 | sub get_history_event ( $$;$ ) { | |
266 | _get_history_event($_[0], $_[1], defined $_[2] ? ord $_[2] : 0); | |
267 | } | |
268 | ||
269 | # | |
270 | # Ornaments | |
271 | # | |
272 | ||
273 | # This routine originates in Term::ReadLine.pm. | |
274 | ||
275 | # Debian GNU/Linux discourages users from using /etc/termcap. A | |
276 | # subroutine ornaments() defined in Term::ReadLine.pm uses | |
277 | # Term::Caps.pm which requires /etc/termcap. | |
278 | ||
279 | # This module calls termcap (or its compatible) library, which the GNU | |
280 | # Readline Library already uses, instead of Term::Caps.pm. | |
281 | ||
282 | # Some terminals do not support 'ue' (underline end). | |
283 | use vars qw(%term_no_ue); | |
284 | %term_no_ue = ( kterm => 1 ); | |
285 | ||
286 | sub ornaments { | |
287 | return $rl_term_set unless @_; | |
288 | $rl_term_set = shift; | |
289 | $rl_term_set ||= ',,,'; | |
290 | $rl_term_set = $term_no_ue{$ENV{TERM}} ? 'us,me,,' : 'us,ue,,' | |
291 | if $rl_term_set eq '1'; | |
292 | my @ts = split /,/, $rl_term_set, 4; | |
293 | my @rl_term_set | |
294 | = map { | |
295 | # non-printing characters must be informed to readline | |
296 | my $t; | |
297 | ($_ and $t = tgetstr($_)) | |
298 | ? (Term::ReadLine::Gnu::RL_PROMPT_START_IGNORE | |
299 | . $t | |
300 | . Term::ReadLine::Gnu::RL_PROMPT_END_IGNORE) | |
301 | : ''; | |
302 | } @ts; | |
303 | $Attribs{term_set} = \@rl_term_set; | |
304 | return $rl_term_set; | |
305 | } | |
306 | ||
307 | # | |
308 | # a sample custom function | |
309 | # | |
310 | ||
311 | # The equivalent of the Bash shell M-^ history-expand-line editing | |
312 | # command. | |
313 | ||
314 | # This routine was borrowed from bash. | |
315 | sub history_expand_line { | |
316 | my ($count, $key) = @_; | |
317 | my ($expanded, $new_line) = history_expand($Attribs{line_buffer}); | |
318 | if ($expanded > 0) { | |
319 | rl_modifying(0, $Attribs{end}); # save undo information | |
320 | $Attribs{line_buffer} = $new_line; | |
321 | } elsif ($expanded < 0) { | |
322 | my $OUT = $Attribs{outstream}; | |
323 | print $OUT "\n$new_line\n"; | |
324 | rl_on_new_line(); | |
325 | } # $expanded == 0 : no change | |
326 | } | |
327 | ||
328 | # The equivalent of the Korn shell C-o operate-and-get-next-history-line | |
329 | # editing command. | |
330 | ||
331 | # This routine was borrowed from bash. | |
332 | sub operate_and_get_next { | |
333 | my ($count, $key) = @_; | |
334 | ||
335 | my $saved_history_line_to_use = -1; | |
336 | my $old_rl_startup_hook; | |
337 | ||
338 | # Accept the current line. | |
339 | rl_call_function('accept-line', 1, $key); | |
340 | ||
341 | # Find the current line, and find the next line to use. */ | |
342 | my $where = where_history(); | |
343 | if ((history_is_stifled() | |
344 | && ($Attribs{history_length} >= $Attribs{max_input_history})) | |
345 | || ($where >= $Attribs{history_length} - 1)) { | |
346 | $saved_history_line_to_use = $where; | |
347 | } else { | |
348 | $saved_history_line_to_use = $where + 1; | |
349 | } | |
350 | $old_rl_startup_hook = $Attribs{startup_hook}; | |
351 | $Attribs{startup_hook} = sub { | |
352 | if ($saved_history_line_to_use >= 0) { | |
353 | rl_call_function('previous-history', | |
354 | $Attribs{history_length} | |
355 | - $saved_history_line_to_use, | |
356 | 0); | |
357 | $Attribs{startup_hook} = $old_rl_startup_hook; | |
358 | $saved_history_line_to_use = -1; | |
359 | } | |
360 | }; | |
361 | } | |
362 | ||
363 | sub display_readline_version { # show version | |
364 | my($count, $key) = @_; # ignored in this function | |
365 | my $OUT = $Attribs{outstream}; | |
366 | print $OUT | |
367 | ("\nTerm::ReadLine::Gnu version: $Term::ReadLine::Gnu::VERSION"); | |
368 | print $OUT | |
369 | ("\nGNU Readline Library version: $Attribs{library_version}\n"); | |
370 | rl_on_new_line(); | |
371 | } | |
372 | ||
373 | # sample function of rl_message() | |
374 | sub change_ornaments { | |
375 | my($count, $key) = @_; # ignored in this function | |
376 | rl_save_prompt; | |
377 | rl_message("[S]tandout, [U]nderlining, [B]old, [R]everse, [V]isible bell: "); | |
378 | my $c = chr rl_read_key; | |
379 | if ($c =~ /s/i) { | |
380 | ornaments('so,me,,'); | |
381 | } elsif ($c =~ /u/i) { | |
382 | ornaments('us,me,,'); | |
383 | } elsif ($c =~ /b/i) { | |
384 | ornaments('md,me,,'); | |
385 | } elsif ($c =~ /r/i) { | |
386 | ornaments('mr,me,,'); | |
387 | } elsif ($c =~ /v/i) { | |
388 | ornaments('vb,,,'); | |
389 | } else { | |
390 | rl_ding; | |
391 | } | |
392 | rl_restore_prompt; | |
393 | rl_clear_message; | |
394 | } | |
395 | ||
396 | # | |
397 | # for tkRunning | |
398 | # | |
399 | sub Tk_getc { | |
400 | &Term::ReadLine::Tk::Tk_loop | |
401 | if $Term::ReadLine::toloop && defined &Tk::DoOneEvent; | |
402 | my $FILE = $Attribs{instream}; | |
403 | return rl_getc($FILE); | |
404 | } | |
405 | ||
406 | # redisplay function for secret input like password | |
407 | # usage: | |
408 | # $a->{redisplay_function} = $a->{shadow_redisplay}; | |
409 | # $line = $t->readline("password> "); | |
410 | sub shadow_redisplay { | |
411 | @_tstrs = _tgetstrs() unless $_tstrs_init; | |
412 | my $OUT = $Attribs{outstream}; | |
413 | my $oldfh = select($OUT); $| = 1; select($oldfh); | |
414 | print $OUT ($_tstrs[0], # carriage return | |
415 | $_tstrs[1], # clear to EOL | |
416 | $Attribs{prompt}, '*' x length($Attribs{line_buffer})); | |
417 | print $OUT ($_tstrs[2] # cursor left | |
418 | x (length($Attribs{line_buffer}) - $Attribs{point})); | |
419 | $oldfh = select($OUT); $| = 0; select($oldfh); | |
420 | } | |
421 | ||
422 | sub _tgetstrs { | |
423 | my @s = (tgetstr('cr'), # carriage return | |
424 | tgetstr('ce'), # clear to EOL | |
425 | tgetstr('le')); # cursor left | |
426 | warn <<"EOM" unless (defined($s[0]) && defined($s[1]) && defined($s[2])); | |
427 | Your terminal 'TERM=$ENV{TERM}' does not support enough function. | |
428 | Check if your environment variable 'TERM' is set correctly. | |
429 | EOM | |
430 | # suppress warning "Use of uninitialized value in print at ..." | |
431 | $s[0] = $s[0] || ''; $s[1] = $s[1] || ''; $s[2] = $s[2] || ''; | |
432 | $_tstrs_init = 1; | |
433 | return @s; | |
434 | } | |
435 | ||
436 | # callback handler wrapper function for CallbackHandlerInstall method | |
437 | sub _ch_wrapper { | |
438 | my $line = shift; | |
439 | ||
440 | if (defined $line) { | |
441 | if ($Attribs{do_expand}) { | |
442 | my $result; | |
443 | ($result, $line) = history_expand($line); | |
444 | my $outstream = $Attribs{outstream}; | |
445 | print $outstream "$line\n" if ($result); | |
446 | ||
447 | # return without adding line into history | |
448 | if ($result < 0 || $result == 2) { | |
449 | return ''; # don't return `undef' which means EOF. | |
450 | } | |
451 | } | |
452 | ||
453 | # add to history buffer | |
454 | add_history($line) | |
455 | if ($Attribs{MinLength} > 0 | |
456 | && length($line) >= $Attribs{MinLength}); | |
457 | } | |
458 | &{$Attribs{_callback_handler}}($line); | |
459 | } | |
460 | ||
461 | # | |
462 | # List Completion Function | |
463 | # | |
464 | sub list_completion_function ( $$ ) { | |
465 | my($text, $state) = @_; | |
466 | ||
467 | $_i = $state ? $_i + 1 : 0; # clear counter at the first call | |
468 | my $cw = $Attribs{completion_word}; | |
469 | for (; $_i <= $#{$cw}; $_i++) { | |
470 | return $cw->[$_i] if ($cw->[$_i] =~ /^\Q$text/); | |
471 | } | |
472 | return undef; | |
473 | } | |
474 | ||
475 | # | |
476 | # wrapper completion function of 'completion_function' | |
477 | # for compatibility with Term::ReadLine::Perl | |
478 | # | |
479 | sub _trp_completion_function ( $$ ) { | |
480 | my($text, $state) = @_; | |
481 | ||
482 | my $cf; | |
483 | return undef unless defined ($cf = $Attribs{completion_function}); | |
484 | ||
485 | if ($state) { | |
486 | $_i++; | |
487 | } else { | |
488 | # the first call | |
489 | $_i = 0; # clear index | |
490 | @_matches = &$cf($text, | |
491 | $Attribs{line_buffer}, | |
492 | $Attribs{point} - length($text)); | |
493 | # return here since $#_matches is 0 instead of -1 when | |
494 | # @_matches = undef | |
495 | return undef unless defined $_matches[0]; | |
496 | } | |
497 | ||
498 | for (; $_i <= $#_matches; $_i++) { | |
499 | return $_matches[$_i] if ($_matches[$_i] =~ /^\Q$text/); | |
500 | } | |
501 | return undef; | |
502 | } | |
503 | ||
504 | 1; | |
505 | ||
506 | __END__ |