blib/lib/POE/Wheel/ReadLine.pm untested

filter: Statementstmt 0.0% 0 / 1546 Branchbran 0.0% 0 / 526 Conditioncond 0.0% 0 / 149 MC/DCmcdc 0.0% 0 / 88 Subroutinesub 0.0% 0 / 200 Podpod 0.0% 0 / 189 totaltotal 0.0% 0 / 2698 CC 262 SCAR 36.9
CC
262
Cov
0%
_parse_inputrc
342.0
_global_init
240.0
rl_complete
132.0
CRAP 40.1
1 package POE::Wheel::ReadLine;
2  
3 use warnings;
4 use strict;
5 BEGIN { eval { require bytes } and bytes->import; }
6  
7 use vars qw($VERSION @ISA);
8 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
9  
10 use Carp qw( croak carp );
11 use Symbol qw(gensym);
12 use POE qw( Wheel );
13 push @ISA, qw(POE::Wheel);
14 use POSIX ();
15  
16 if ($^O eq "MSWin32") {
17 die "$^O cannot run " . __PACKAGE__;
18 }
19  
20 # After a massive hackathon on Cygwin/perl/windows/POE it was determined that
21 # having TERM='dumb' is worthless and VERY problematic to work-around...
22 # Actually, the problem lies deep in Term::Cap's internals - it blows up
23 # when we try to do "$termcap->Trequire( qw( cl ku kd kl kr) )" in new()
24 # eval{} will not catch the croak() in the sub
25 # Cygwin v1.7.1-1 on Windows Server 2008 64bit with Perl v5.10.1 with Term::Cap v1.12 with POE v1.287
26 # For detailed info, please consult RT#55365
27 # sorry about the defined $ENV{TERM} check, it was needed to make sure we spew no warnings...
28 if ($^O eq 'cygwin' and defined $ENV{TERM} and $ENV{TERM} eq 'dumb') {
29 die "$^O with TERM='$ENV{TERM}' cannot run " . __PACKAGE__;
30 }
31  
32 # Things we'll need to interact with the terminal.
33 use Term::Cap ();
34 use Term::ReadKey qw( ReadKey ReadMode GetTerminalSize );
35  
36 my $initialised = 0;
37 my $termcap; # Termcap entry.
38 my $tc_bell; # How to ring the terminal.
39 my $tc_visual_bell; # How to ring the terminal.
40 my $tc_has_ce; # Termcap can clear to end of line.
41  
42 # Note: we use 2-arg open() below because Perl 5.6 doesn't recognize
43 # the '>&' and '<&' modes with a 3-arg open()
44  
45 # Private STDIN and STDOUT.
46 my $stdin = gensym();
47 open($stdin, "<&STDIN") or die "Can't open private STDIN: $!";
48  
49 my $stdout = gensym;
50 open($stdout, ">&STDOUT") or die "Can't open private STDOUT: $!";
51  
52 # Offsets into $self.
53 sub SELF_INPUT () { 0 }
54 sub SELF_CURSOR_INPUT () { 1 }
55 sub SELF_EVENT_INPUT () { 2 }
56 sub SELF_READING_LINE () { 3 }
57 sub SELF_STATE_READ () { 4 }
58 sub SELF_PROMPT () { 5 }
59 sub SELF_HIST_LIST () { 6 }
60 sub SELF_HIST_INDEX () { 7 }
61 sub SELF_INPUT_HOLD () { 8 }
62 sub SELF_KEY_BUILD () { 9 }
63 sub SELF_INSERT_MODE () { 10 }
64 sub SELF_PUT_MODE () { 11 }
65 sub SELF_PUT_BUFFER () { 12 }
66 sub SELF_IDLE_TIME () { 13 }
67 sub SELF_STATE_IDLE () { 14 }
68 sub SELF_HAS_TIMER () { 15 }
69 sub SELF_CURSOR_DISPLAY () { 16 }
70 sub SELF_UNIQUE_ID () { 17 }
71 sub SELF_KEYMAP () { 18 }
72 sub SELF_OPTIONS () { 19 }
73 sub SELF_APP () { 20 }
74 sub SELF_ALL_KEYMAPS () { 21 }
75 sub SELF_PENDING () { 22 }
76 sub SELF_COUNT () { 23 }
77 sub SELF_MARK () { 24 }
78 sub SELF_MARKLIST () { 25 }
79 sub SELF_KILL_RING () { 26 }
80 sub SELF_LAST () { 27 }
81 sub SELF_PENDING_FN () { 28 }
82 sub SELF_SOURCE () { 29 }
83 sub SELF_SEARCH () { 30 }
84 sub SELF_SEARCH_PROMPT () { 31 }
85 sub SELF_SEARCH_MAP () { 32 }
86 sub SELF_PREV_PROMPT () { 33 }
87 sub SELF_SEARCH_DIR () { 34 }
88 sub SELF_SEARCH_KEY () { 35 }
89 sub SELF_UNDO () { 36 }
90  
91 sub CRIMSON_SCOPE_HACK ($) { 0 }
92  
93 #------------------------------------------------------------------------------
94  
95 # Build a hash of input characters and their "normalized" display
96 # versions. ISO Latin-1 characters (8th bit set "ASCII") are
97 # mishandled. European users, please forgive me. If there's a good
98 # way to handle this-- perhaps this is an interesting use for
99 # Unicode-- please let me know.
100  
101 my (%normalized_character, @normalized_extra_width);
102  
103 #------------------------------------------------------------------------------
104 # Gather information about the user's terminal. This just keeps
105 # getting uglier.
106  
107 my $ospeed = undef;
108 my $termios = undef;
109 my $term = undef;
110 my $tc_left = undef;
111 my $trk_cols = undef;
112 my $trk_rows = undef;
113  
114 sub _curs_left {
115 my $amount = shift;
116  
117 if ($tc_left eq "LE") {
118 $termcap->Tgoto($tc_left, 1, $amount, $stdout);
119 return;
120 }
121  
122 for (1..$amount) {
123 $termcap->Tgoto($tc_left, 1, 1, $stdout);
124 }
125 }
126  
127  
128 our $defuns = {
129 "abort" => \&rl_abort,
130 "accept-line" => \&rl_accept_line,
131 "backward-char" => \&rl_backward_char,
132 "backward-delete-char" => \&rl_backward_delete_char,
133 "backward-kill-line" => \&rl_unix_line_discard, # reuse emacs
134 "backward-kill-word" => \&rl_backward_kill_word,
135 "backward-word" => \&rl_backward_word,
136 "beginning-of-history" => \&rl_beginning_of_history,
137 "beginning-of-line" => \&rl_beginning_of_line,
138 "capitalize-word" => \&rl_capitalize_word,
139 "character-search" => \&rl_character_search,
140 "character-search-backward" => \&rl_character_search_backward,
141 "clear-screen" => \&rl_clear_screen,
142 "complete" => \&rl_complete,
143 "copy-region-as-kill" => \&rl_copy_region_as_kill,
144 "delete-char" => \&rl_delete_char,
145 "delete-horizontal-space" => \&rl_delete_horizontal_space,
146 "digit-argument" => \&rl_digit_argument,
147 "ding" => \&rl_ding,
148 "downcase-word" => \&rl_downcase_word,
149 "dump-key" => \&rl_dump_key,
150 "dump-macros" => \&rl_dump_macros,
151 "dump-variables" => \&rl_dump_variables,
152 "emacs-editing-mode" => \&rl_emacs_editing_mode,
153 "end-of-history" => \&rl_end_of_history,
154 "end-of-line" => \&rl_end_of_line,
155 "forward-char" => \&rl_forward_char,
156 "forward-search-history" => \&rl_forward_search_history,
157 "forward-word" => \&rl_forward_word,
158 "insert-comment" => \&rl_insert_comment,
159 "insert-completions" => \&rl_insert_completions,
160 "insert-macro" => \&rl_insert_macro,
161 "interrupt" => \&rl_interrupt,
162 "isearch-again" => \&rl_isearch_again,
163 "kill-line" => \&rl_kill_line,
164 "kill-region" => \&rl_kill_region,
165 "kill-whole-line" => \&rl_kill_whole_line,
166 "kill-word" => \&rl_kill_word,
167 "next-history" => \&rl_next_history,
168 "non-incremental-forward-search-history" => \&rl_non_incremental_forward_search_history,
169 "non-incremental-reverse-search-history" => \&rl_non_incremental_reverse_search_history,
170 "overwrite-mode" => \&rl_overwrite_mode,
171 "poe-wheel-debug" => \&rl_poe_wheel_debug,
172 "possible-completions" => \&rl_possible_completions,
173 "previous-history" => \&rl_previous_history,
174 "quoted-insert" => \&rl_quoted_insert,
175 "re-read-init-file" => \&rl_re_read_init_file,
176 "redraw-current-line" => \&rl_redraw_current_line,
177 "reverse-search-history" => \&rl_reverse_search_history,
178 "revert-line" => \&rl_revert_line,
179 "search-abort" => \&rl_search_abort,
180 "search-finish" => \&rl_search_finish,
181 "search-key" => \&rl_search_key,
182 "self-insert" => \&rl_self_insert,
183 "set-keymap" => \&rl_set_keymap,
184 "set-mark" => \&rl_set_mark,
185 "tab-insert" => \&rl_ding, # UNIMPLEMENTED
186 "tilde-expand" => \&rl_tilde_expand,
187 "transpose-chars" => \&rl_transpose_chars,
188 "transpose-words" => \&rl_transpose_words,
189 "undo" => \&rl_undo,
190 "unix-line-discard" => \&rl_unix_line_discard,
191 "unix-word-rubout" => \&rl_unix_word_rubout,
192 "upcase-word" => \&rl_upcase_word,
193 "vi-append-eol" => \&rl_vi_append_eol,
194 "vi-append-mode" => \&rl_vi_append_mode,
195 "vi-arg-digit" => \&rl_vi_arg_digit,
196 "vi-change-case" => \&rl_vi_change_case,
197 "vi-change-char" => \&rl_vi_change_char,
198 "vi-change-to" => \&rl_vi_change_to,
199 "vi-char-search" => \&rl_vi_char_search,
200 "vi-column" => \&rl_vi_column,
201 "vi-complete" => \&rl_vi_cmplete,
202 "vi-delete" => \&rl_vi_delete,
203 "vi-delete-to" => \&rl_vi_delete_to,
204 "vi-editing-mode" => \&rl_vi_editing_mode,
205 "vi-end-spec" => \&rl_vi_end_spec,
206 "vi-end-word" => \&rl_vi_end_word,
207 "vi-eof-maybe" => \&rl_vi_eof_maybe,
208 "vi-fetch-history" => \&rl_beginning_of_history, # re-use emacs version
209 "vi-first-print" => \&rl_vi_first_print,
210 "vi-goto-mark" => \&rl_vi_goto_mark,
211 "vi-insert-beg" => \&rl_vi_insert_beg,
212 "vi-insertion-mode" => \&rl_vi_insertion_mode,
213 "vi-match" => \&rl_vi_match,
214 "vi-movement-mode" => \&rl_vi_movement_mode,
215 "vi-next-word" => \&rl_vi_next_word,
216 "vi-prev-word" => \&rl_vi_prev_word,
217 "vi-put" => \&rl_vi_put,
218 "vi-redo" => \&rl_vi_redo,
219 "vi-replace" => \&rl_vi_replace,
220 "vi-search" => \&rl_vi_search,
221 "vi-search-accept" => \&rl_vi_search_accept,
222 "vi-search-again" => \&rl_vi_search_again,
223 "vi-search-key" => \&rl_vi_search_key,
224 "vi-set-mark" => \&rl_vi_set_mark,
225 "vi-spec-beginning-of-line" => \&rl_vi_spec_beginning_of_line,
226 "vi-spec-end-of-line" => \&rl_vi_spec_end_of_line,
227 "vi-spec-first-print" => \&rl_vi_spec_first_print,
228 "vi-spec-forward-char" => \&rl_vi_spec_forward_char,
229 "vi-spec-mark" => \&rl_vi_spec_mark,
230 "vi-spec-word" => \&rl_vi_spec_word,
231 "vi-subst" => \&rl_vi_subst,
232 "vi-tilde-expand" => \&rl_vi_tilde_expand,
233 "vi-undo" => \&rl_undo, # re-use emacs version
234 "vi-yank-arg" => \&rl_vi_yank_arg,
235 "vi-yank-to" => \&rl_vi_yank_to,
236 "yank" => \&rl_yank,
237 "yank-last-arg" => \&rl_yank_last_arg,
238 "yank-nth-arg" => \&rl_yank_nth_arg,
239 "yank-pop" => \&rl_yank_pop,
240 };
241  
242 # what functions are for counting
243 my @fns_counting = (
244 'rl_vi_arg_digit',
245 'rl_digit_argument',
246 'rl_universal-argument',
247 );
248  
249 # what functions are purely for movement...
250 my @fns_movement = (
251 'rl_beginning_of_line',
252 'rl_backward_char',
253 'rl_forward_char',
254 'rl_backward_word',
255 'rl_forward_word',
256 'rl_end_of_line',
257 'rl_character_search',
258 'rl_character_search_backward',
259 'rl_vi_prev_word',
260 'rl_vi_next_word',
261 'rl_vi_goto_mark',
262 'rl_vi_end_word',
263 'rl_vi_column',
264 'rl_vi_first_print',
265 'rl_vi_char_search',
266 'rl_vi_spec_char_search',
267 'rl_vi_spec_end_of_line',
268 'rl_vi_spec_beginning_of_line',
269 'rl_vi_spec_first_print',
270 'rl_vi_spec_word',
271 'rl_vi_spec_mark',
272 );
273  
274 # the list of functions that we don't want to record for
275 # later redo usage in vi mode.
276 my @fns_anon = (
277 'rl_vi_redo',
278 @fns_counting,
279 @fns_movement,
280 );
281  
282  
283 my $defaults_inputrc = <<'INPUTRC';
284 set comment-begin #
285 INPUTRC
286  
287 my $emacs_inputrc = <<'INPUTRC';
288 C-a: beginning-of-line
289 C-b: backward-char
290 C-c: interrupt
291 C-d: delete-char
292 C-e: end-of-line
293 C-f: forward-char
294 C-g: abort
295 C-h: backward-delete-char
296 C-i: complete
297 C-j: accept-line
298 C-k: kill-line
299 C-l: clear-screen
300 C-m: accept-line
301 C-n: next-history
302 C-p: previous-history
303 C-q: poe-wheel-debug
304 C-r: reverse-search-history
305 C-s: forward-search-history
306 C-t: transpose-chars
307 C-u: unix-line-discard
308 C-v: quoted-insert
309 C-w: unix-word-rubout
310 C-y: yank
311 C-]: character-search
312 C-_: undo
313 del: backward-delete-char
314 rubout: backward-delete-char
315  
316 M-C-g: abort
317 M-C-h: backward-kill-word
318 M-C-i: tab-insert
319 M-C-j: vi-editing-mode
320 M-C-r: revert-line
321 M-C-y: yank-nth-arg
322 M-C-[: complete
323 M-C-]: character-search-backward
324 M-space: set-mark
325 M-#: insert-comment
326 M-&: tilde-expand
327 M-*: insert-completions
328 M--: digit-argument
329 M-.: yank-last-arg
330 M-0: digit-argument
331 M-1: digit-argument
332 M-2: digit-argument
333 M-3: digit-argument
334 M-4: digit-argument
335 M-5: digit-argument
336 M-6: digit-argument
337 M-7: digit-argument
338 M-8: digit-argument
339 M-9: digit-argument
340 M-<: beginning-of-history
341 M->: end-of-history
342 M-?: possible-completions
343  
344 M-b: backward-word
345 M-c: capitalize-word
346 M-d: kill-word
347 M-f: forward-word
348 M-l: downcase-word
349 M-n: non-incremental-forward-search-history
350 M-p: non-incremental-reverse-search-history
351 M-r: revert-line
352 M-t: transpose-words
353 M-u: upcase-word
354 M-y: yank-pop
355 M-\: delete-horizontal-space
356 M-~: tilde-expand
357 M-del: backward-kill-word
358 M-_: yank-last-arg
359  
360 C-xC-r: re-read-init-file
361 C-xC-g: abort
362 C-xDel: backward-kill-line
363 C-xm: dump-macros
364 C-xv: dump-variables
365 C-xk: dump-key
366  
367 home: beginning-of-line
368 end: end-of-line
369 ins: overwrite-mode
370 del: delete-char
371 left: backward-char
372 right: forward-char
373 up: previous-history
374 down: next-history
375 bs: backward-delete-char
376 INPUTRC
377  
378 my $vi_inputrc = <<'INPUTRC';
379  
380 # VI uses two keymaps, depending on which mode we're in.
381 set keymap vi-insert
382  
383 C-d: vi-eof-maybe
384 C-h: backward-delete-char
385 C-i: complete
386 C-j: accept-line
387 C-m: accept-line
388 C-r: reverse-search-history
389 C-s: forward-search-history
390 C-t: transpose-chars
391 C-u: unix-line-discard
392 C-v: quoted-insert
393 C-w: unix-word-rubout
394 C-y: yank
395 C-[: vi-movement-mode
396 C-_: undo
397 C-?: backward-delete-char
398  
399 set keymap vi-command
400 C-d: vi-eof-maybe
401 C-e: emacs-editing-mode
402 C-g: abort
403 C-h: backward-char
404 C-j: accept-line
405 C-k: kill-line
406 C-l: clear-screen
407 C-m: accept-line
408 C-n: next-history
409 C-p: previous-history
410 C-q: quoted-insert
411 C-r: reverse-search-history
412 C-s: forward-search-history
413 C-t: transpose-chars
414 C-u: unix-line-discard
415 C-v: quoted-insert
416 C-w: unix-word-rubout
417 C-y: yank
418 C-_: vi-undo
419 " ": forward-char
420 "#": insert-comment
421 "$": end-of-line
422 "%": vi-match
423 "&": vi-tilde-expand
424 "*": vi-complete
425 "+": next-history
426 ",": vi-char-search
427 "-": previous-history
428 ".": vi-redo
429 "/": vi-search
430 "0": vi-arg-digit
431 "1": vi-arg-digit
432 "2": vi-arg-digit
433 "3": vi-arg-digit
434 "4": vi-arg-digit
435 "5": vi-arg-digit
436 "6": vi-arg-digit
437 "7": vi-arg-digit
438 "8": vi-arg-digit
439 "9": vi-arg-digit
440 ";": vi-char-search
441 "=": vi-complete
442 "?": vi-search
443 A: vi-append-eol
444 B: vi-prev-word
445 C: vi-change-to
446 D: vi-delete-to
447 E: vi-end-word
448 F: vi-char-search
449 G: vi-fetch-history
450 I: vi-insert-beg
451 N: vi-search-again
452 P: vi-put
453 R: vi-replace
454 S: vi-subst
455 T: vi-char-search
456 U: revert-line
457 W: vi-next-word
458 X: backward-delete-char
459 Y: vi-yank-to
460 "\": vi-complete
461 "^": vi-first-print
462 "_": vi-yank-arg
463 "`": vi-goto-mark
464 a: vi-append-mode
465 b: backward-word
466 c: vi-change-to
467 d: vi-delete-to
468 e: vi-end-word
469 h: backward-char
470 i: vi-insertion-mode
471 j: next-history
472 k: previous-history
473 l: forward-char
474 m: vi-set-mark
475 n: vi-search-again
476 p: vi-put
477 r: vi-change-char
478 s: vi-subst
479 t: vi-char-search
480 w: vi-next-word
481 x: vi-delete
482 y: vi-yank-to
483 "|": vi-column
484 "~": vi-change-case
485  
486 set keymap vi-specification
487 "^": vi-spec-first-print
488 "`": vi-spec-mark
489 "$": vi-spec-end-of-line
490 "0": vi-spec-beginning-of-line
491 "1": vi-arg-digit
492 "2": vi-arg-digit
493 "3": vi-arg-digit
494 "4": vi-arg-digit
495 "5": vi-arg-digit
496 "6": vi-arg-digit
497 "7": vi-arg-digit
498 "8": vi-arg-digit
499 "9": vi-arg-digit
500 w: vi-spec-word
501 t: vi-spec-forward-char
502  
503 INPUTRC
504  
505 my $search_inputrc = <<'INPUTRC';
506 set keymap isearch
507 C-r: isearch-again
508 C-s: isearch-again
509  
510 set keymap vi-search
511 C-j: vi-search-accept
512 C-m: vi-search-accept
513 INPUTRC
514  
515 #------------------------------------------------------------------------------
516 # Helper functions.
517  
518 sub _vislength {
519 return 0 unless $_[0];
520 my $len = length($_[0]);
521 while ($_[0] =~ m{(\\\[.*?\\\])}g) {
522 $len -= length($1);
523 }
524 return $len;
525 }
526  
527 # Wipe the current input line.
528 sub _wipe_input_line {
529 my ($self) = shift;
530  
531 # Clear the current prompt and input, and home the cursor.
532 _curs_left( $self->[SELF_CURSOR_DISPLAY] + _vislength($self->[SELF_PROMPT]));
533  
534 if ( $tc_has_ce ) {
535 print $stdout $termcap->Tputs( 'ce', 1 );
536 } else {
537 my $wlen = _vislength($self->[SELF_PROMPT]) + _display_width($self->[SELF_INPUT]);
538 print $stdout ( ' ' x $wlen);
539 _curs_left($wlen);
540 }
541 }
542  
543 # Helper to flush any buffered output.
544 sub _flush_output_buffer {
545 my ($self) = shift;
546  
547 # Flush anything buffered.
548 if ( @{ $self->[SELF_PUT_BUFFER] } ) {
549 print $stdout @{ $self->[SELF_PUT_BUFFER] };
550  
551 # Do not change the interior arrayref, or the event handlers will
552 # become confused.
553 @{ $self->[SELF_PUT_BUFFER] } = ();
554 }
555 }
556  
557 # Set up the prompt and input line like nothing happened.
558 sub _repaint_input_line {
559 my ($self) = shift;
560 my $sp = $self->[SELF_PROMPT];
561 $sp =~ s{\\[\[\]]}{}g;
562 print $stdout $sp, _normalize($self->[SELF_INPUT]);
563  
564 if ( $self->[SELF_CURSOR_INPUT] != length( $self->[SELF_INPUT]) ) {
565 _curs_left(
566 _display_width($self->[SELF_INPUT]) - $self->[SELF_CURSOR_DISPLAY]
567 );
568 }
569 }
570  
571 sub _clear_to_end {
572 my ($self) = @_;
573 if (length $self->[SELF_INPUT]) {
574 if ($tc_has_ce) {
575 print $stdout $termcap->Tputs( 'ce', 1 );
576 } else {
577 my $display_width = _display_width($self->[SELF_INPUT]);
578 print $stdout ' ' x $display_width;
579 _curs_left($display_width);
580 }
581 }
582 }
583  
584 sub _delete_chars {
585 my ($self, $from, $howmany) = @_;
586 # sanitize input
587 if ($howmany < 0) {
588 $from -= $howmany;
589 $howmany = -$howmany;
590 if ($from < 0) {
591 $howmany -= $from;
592 $from = 0;
593 }
594 }
595  
596 my $old = substr($self->[SELF_INPUT], $from, $howmany);
597 my $killed_width = _display_width($old);
598 substr($self->[SELF_INPUT], $from, $howmany) = '';
599 if ($self->[SELF_CURSOR_INPUT] > $from) {
600 my $newdisp = length(_normalize(substr($self->[SELF_INPUT], 0, $from)));
601 _curs_left($self->[SELF_CURSOR_DISPLAY] - $newdisp);
602 $self->[SELF_CURSOR_INPUT] = $from;
603 $self->[SELF_CURSOR_DISPLAY] = $newdisp;
604 }
605  
606 my $normal_remaining = _normalize(substr($self->[SELF_INPUT], $from));
607 print $stdout $normal_remaining;
608 my $normal_remaining_length = length($normal_remaining);
609  
610 if ($tc_has_ce) {
611 print $stdout $termcap->Tputs( 'ce', 1 );
612 } else {
613 print $stdout ' ' x $killed_width;
614 $normal_remaining_length += $killed_width;
615 }
616  
617 _curs_left($normal_remaining_length)
618 if $normal_remaining_length;
619  
620 return $old;
621 }
622  
623 sub _search {
624 my ($self, $rebuild) = @_;
625 if ($rebuild) {
626 $self->_wipe_input_line;
627 $self->_build_search_prompt;
628 }
629 # find in history....
630 my $found = 0;
631 for (
632 my $i = $self->[SELF_HIST_INDEX];
633 $i < scalar @{$self->[SELF_HIST_LIST]} && $i >= 0;
634 $i += $self->[SELF_SEARCH_DIR]
635 ) {
636 next unless $self->[SELF_HIST_LIST]->[$i] =~ /$self->[SELF_SEARCH]/;
637  
638 $self->[SELF_HIST_INDEX] = $i;
639 $self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[$i];
640 $self->[SELF_CURSOR_INPUT] = 0;
641 $self->[SELF_CURSOR_DISPLAY] = 0;
642 $self->[SELF_UNDO] = [ $self->[SELF_INPUT], 0, 0 ]; # reset undo info
643 $found++;
644 last;
645 }
646 $self->rl_ding unless $found;
647 $self->_repaint_input_line;
648 }
649  
650 # Return a normalized version of a string. This includes destroying
651 # 8th-bit-set characters, turning them into strange multi-byte
652 # sequences. Apologies to everyone; please let me know of a portable
653 # way to deal with this.
654 sub _normalize {
655 local $_ = shift;
656 s/([^ -~])/$normalized_character{$1}/g;
657 return $_;
658 }
659  
660 sub _readable_key {
661 my ($raw_key) = @_;
662 my @text = ();
663 foreach my $l (split(//, $raw_key)) {
664 if (ord($l) == 0x1B) {
665 push(@text, 'Meta-');
666 next;
667 }
668 if (ord($l) < 32) {
669 push(@text, 'Control-' . chr(ord($l)+64));
670 next;
671 }
672 if (ord($l) > 127) {
673 my $l = ord($l)-128;
674 if ($l < 32) {
675 $l = "Control-" . chr($l+64);
676 }
677 push(@text, 'Meta-' . chr($l));
678 next;
679 }
680 if (ord($l) == 127) {
681 push @text, "^?";
682 next;
683 }
684 push(@text, $l);
685 }
686 return join("", @text);
687 }
688  
689 # Calculate the display width of a string. The display width is
690 # sometimes wider than the actual string because some characters are
691 # represented on the terminal as multiple characters.
692  
693 sub _display_width {
694 local $_ = shift;
695 my $width = length;
696 $width += $normalized_extra_width[ord] foreach (m/([\x00-\x1F\x7F-\xFF])/g);
697 return $width;
698 }
699  
700 sub _build_search_prompt {
701 my ($self) = @_;
702 $self->[SELF_PROMPT] = $self->[SELF_SEARCH_PROMPT];
703 $self->[SELF_PROMPT] =~ s{%s}{$self->[SELF_SEARCH]};
704 }
705  
706 sub _global_init {
707 return if $initialised;
708  
709 # Get the terminal speed for Term::Cap.
710 $termios = POSIX::Termios->new();
711 $termios->getattr();
712 $ospeed = $termios->getospeed() || eval { POSIX::B38400() } || 0;
713  
714 # Get the current terminal's capabilities.
715 $term = $ENV{TERM} || 'vt100';
716 $termcap = eval { Term::Cap->Tgetent( { TERM => $term, OSPEED => $ospeed } ) };
717 die "could not find termcap entry for ``$term'': $!" unless defined $termcap;
718  
719 # Require certain capabilities.
720 $termcap->Trequire( qw( cl ku kd kl kr) );
721  
722 # Cursor movement.
723 $tc_left = "LE";
724 eval { $termcap->Trequire($tc_left) };
725 if ($@) {
726 $tc_left = "le";
727 eval { $termcap->Trequire($tc_left) };
728 if ($@) {
729 # try out to see if we have a better terminfo defun.
730 # it may well not work (hence eval the lot), but it's worth a shot
731 eval {
732 my @tc = `infocmp -C $term`;
733 chomp(@tc);
734 splice(@tc, 0, 1); # remove header line
735 $ENV{TERMCAP} = join("", @tc);
736 $termcap = Term::Cap->Tgetent( { TERM => $term, OSPEED => $ospeed } );
737 $termcap->Trequire($tc_left);
738 };
739 }
740 die "POE::Wheel::ReadLine requires a termcap that supports LE or le" if $@;
741 }
742  
743 # Terminal size.
744 # We initialize the values once on start-up,
745 # and then from then on, we check them on every entry into
746 # the input state engine (so that we have valid values) and
747 # before handing control back to the user (so that they get
748 # an up-to-date value).
749 eval { ($trk_cols, $trk_rows) = GetTerminalSize($stdout) };
750 ($trk_cols, $trk_rows) = (80, 25) if $@;
751  
752 # Configuration...
753 # Some things are optional.
754 eval { $termcap->Trequire( 'ce' ) };
755 $tc_has_ce = 1 unless $@;
756  
757 # o/` You can ring my bell, ring my bell. o/`
758 my $bell = $termcap->Tputs( bl => 1 );
759 $bell = $termcap->Tputs( vb => 1 ) unless defined $bell;
760 $tc_bell = (defined $bell) ? $bell : '';
761 $bell = $termcap->Tputs( vb => 1 ) || '';
762 $tc_visual_bell = $bell;
763  
764 my $convert_meta = 1;
765 for (my $ord = 0; $ord < 256; $ord++) {
766 my $str = chr($ord);
767 if ($ord > 127) {
768 if ($convert_meta) {
769 $str = "^[";
770 if (($ord - 128) < 32) {
771 $str .= "^" . lc(chr($ord-128+64));
772 } else {
773 $str .= lc(chr($ord-128));
774 }
775 } else {
776 $str = sprintf "<%2x>", $ord;
777 }
778 } elsif ($ord < 32) {
779 $str = '^' . lc(chr($ord+64));
780 }
781 elsif ($ord == 127) {
782 $str = "^?";
783 }
784 $normalized_character{chr($ord)} = $str;
785 $normalized_extra_width[$ord] = length ( $str ) - 1;
786 }
787 $initialised++;
788 }
789  
790 #------------------------------------------------------------------------------
791 # The methods themselves.
792  
793 # Create a new ReadLine wheel.
794 sub new {
795 my $proto = shift;
796 my $class = ref($proto) || $proto;
797  
798 my %params = @_;
799 croak "$class requires a working Kernel" unless defined $poe_kernel;
800  
801 my $input_event = delete $params{InputEvent};
802 croak "$class requires an InputEvent parameter" unless defined $input_event;
803  
804 my $put_mode = delete $params{PutMode};
805 $put_mode = 'idle' unless defined $put_mode;
806 croak "$class PutMode must be either 'immediate', 'idle', or 'after'"
807 unless $put_mode =~ /^(immediate|idle|after)$/;
808  
809 my $idle_time = delete $params{IdleTime};
810 $idle_time = 2 unless defined $idle_time;
811  
812 my $app = delete($params{AppName}) || delete($params{appname});
813 delete $params{appname}; # in case AppName was present
814 $app ||= 'poe-readline';
815  
816 if (scalar keys %params) {
817 carp(
818 "unknown parameters in $class constructor call: ",
819 join(', ', keys %params)
820 );
821 }
822  
823 my $self = undef;
824 if (ref $proto) {
825 $self = bless [], $class;
826 @$self = @$proto;
827 $self->[SELF_SOURCE] = $proto;
828 # ensure we're not bound to the old handler
829 $poe_kernel->select_read($stdin);
830 } else {
831 $self = bless [
832 '', # SELF_INPUT
833 0, # SELF_CURSOR_INPUT
834 $input_event, # SELF_EVENT_INPUT
835 0, # SELF_READING_LINE
836 undef, # SELF_STATE_READ
837 '>', # SELF_PROMPT
838 [ ], # SELF_HIST_LIST
839 0, # SELF_HIST_INDEX
840 '', # SELF_INPUT_HOLD
841 '', # SELF_KEY_BUILD
842 1, # SELF_INSERT_MODE
843 $put_mode, # SELF_PUT_MODE
844 [ ], # SELF_PUT_BUFFER
845 $idle_time, # SELF_IDLE_TIME
846 undef, # SELF_STATE_IDLE
847 0, # SELF_HAS_TIMER
848 0, # SELF_CURSOR_DISPLAY
849 &POE::Wheel::allocate_wheel_id(), # SELF_UNIQUE_ID
850 undef, # SELF_KEYMAP
851 { }, # SELF_OPTIONS
852 $app, # SELF_APP
853 {}, # SELF_ALL_KEYMAPS
854 undef, # SELF_PENDING
855 0, # SELF_COUNT
856 0, # SELF_MARK
857 {}, # SELF_MARKLIST
858 [], # SELF_KILL_RING
859 '', # SELF_LAST
860 undef, # SELF_PENDING_FN
861 undef, # SELF_SOURCE
862 '', # SELF_SEARCH
863 undef, # SELF_SEARCH_PROMPT
864 undef, # SELF_SEARCH_MAP
865 '', # SELF_PREV_PROMPT
866 0, # SELF_SEARCH_DIR
867 '', # SELF_SEARCH_KEY
868 [], # SELF_UNDO
869 ], $class;
870  
871 _global_init();
872 $self->rl_re_read_init_file();
873 }
874  
875 # Turn off $stdout buffering.
876 select((select($stdout), $| = 1)[0]);
877  
878 # Set up the event handlers. Idle goes first.
879 $self->[SELF_STATE_IDLE] = (
880 ref($self) . "(" . $self->[SELF_UNIQUE_ID] . ") -> input timeout"
881 );
882  
883 $self->[SELF_STATE_READ] = (
884 ref($self) . "(" . $self->[SELF_UNIQUE_ID] . ") -> select read"
885 );
886  
887 # TODO - The following hack breaks a circular reference on $self.
888 {
889 my $weak_self = $self;
890 use Scalar::Util qw(weaken);
891 weaken $weak_self;
892  
893 $poe_kernel->state(
894 $self->[SELF_STATE_IDLE],
895 sub { _idle_state($weak_self, @_[1..$#_]) }
896 );
897  
898 $poe_kernel->state(
899 $self->[SELF_STATE_READ],
900 sub { _read_state($weak_self, @_[1..$#_]) }
901 );
902 }
903  
904 return $self;
905 }
906  
907 #------------------------------------------------------------------------------
908 # Destroy the ReadLine wheel. Clean up the terminal.
909  
910 sub DESTROY {
911 my $self = shift;
912  
913 return unless $initialised;
914  
915 # Stop selecting on the handle.
916 $poe_kernel->select_read($stdin);
917  
918 # Detach our tentacles from the parent session.
919 if ($self->[SELF_STATE_READ]) {
920 $poe_kernel->state($self->[SELF_STATE_READ]);
921 $self->[SELF_STATE_READ] = undef;
922 }
923  
924 if ($self->[SELF_STATE_IDLE]) {
925 $poe_kernel->alarm($self->[SELF_STATE_IDLE]);
926 $poe_kernel->state($self->[SELF_STATE_IDLE]);
927 $self->[SELF_STATE_IDLE] = undef;
928 }
929  
930 # tell the terminal that we want to leave 'application' mode
931 print $termcap->Tputs('ke' => 1) if $termcap->Tputs('ke');
932 # Restore the console.
933 ReadMode('restore');
934  
935 &POE::Wheel::free_wheel_id($self->[SELF_UNIQUE_ID]);
936 }
937  
938 #------------------------------------------------------------------------------
939 # Redefine the idle handler. This also uses stupid closure tricks.
940 # See the comments for &_define_read_state for more information about
941 # these closure tricks.
942  
943 sub _idle_state {
944 my ($self) = $_[OBJECT];
945  
946 if (@{$self->[SELF_PUT_BUFFER]}) {
947 $self->_wipe_input_line;
948 $self->_flush_output_buffer;
949 $self->_repaint_input_line;
950 }
951  
952 # No more timer.
953 $self->[SELF_HAS_TIMER] = 0;
954 }
955  
956 sub _read_state {
957 my ($self, $k) = @_[OBJECT, KERNEL];
958  
959 # Read keys, non-blocking, as long as there are some.
960 while (defined(my $raw_key = ReadKey(-1))) {
961  
962 # Not reading a line; discard the input.
963 next unless $self->[SELF_READING_LINE];
964  
965 # Update the timer on significant input.
966 if ( $self->[SELF_PUT_MODE] eq 'idle' ) {
967 $k->delay( $self->[SELF_STATE_IDLE], $self->[SELF_IDLE_TIME] );
968 $self->[SELF_HAS_TIMER] = 1;
969 }
970  
971 push(
972 @{$self->[SELF_UNDO]}, [
973 $self->[SELF_INPUT],
974 $self->[SELF_CURSOR_INPUT],
975 $self->[SELF_CURSOR_DISPLAY]
976 ]
977 );
978  
979 # Build-multi character codes and make the keystroke printable.
980 $self->[SELF_KEY_BUILD] .= $raw_key;
981 $raw_key = $self->[SELF_KEY_BUILD];
982 my $key = _normalize($raw_key);
983  
984 if ($self->[SELF_PENDING_FN]) {
985 my $old = $self->[SELF_INPUT];
986 my $oldref = $self->[SELF_PENDING_FN];
987 push(
988 @{$self->[SELF_UNDO]}, [
989 $old,
990 $self->[SELF_CURSOR_INPUT],
991 $self->[SELF_CURSOR_DISPLAY]
992 ]
993 );
994 $self->[SELF_PENDING_FN]->($self, $key, $raw_key);
995 pop(@{$self->[SELF_UNDO]}) if ($old eq $self->[SELF_INPUT]);
996 $self->[SELF_KEY_BUILD] = '';
997 if ($self->[SELF_PENDING_FN] && "$self->[SELF_PENDING_FN]" eq $oldref) {
998 $self->[SELF_PENDING_FN] = undef;
999 }
1000 next;
1001 }
1002  
1003 # Keep glomming keystrokes until they stop existing in the
1004 # hash of meta prefixes.
1005 next if exists $self->[SELF_KEYMAP]->{prefix}->{$raw_key};
1006  
1007 # PROCESS KEY
1008 my $old = $self->[SELF_INPUT];
1009 push(
1010 @{$self->[SELF_UNDO]}, [
1011 $old,
1012 $self->[SELF_CURSOR_INPUT],
1013 $self->[SELF_CURSOR_DISPLAY]
1014 ]
1015 );
1016 $self->[SELF_KEY_BUILD] = '';
1017 $self->_apply_key($key, $raw_key);
1018  
1019 pop(@{$self->[SELF_UNDO]}) if ($old eq $self->[SELF_INPUT]);
1020 }
1021 }
1022  
1023 sub _apply_key {
1024 my ($self, $key, $raw_key) = @_;
1025 my $mapping = $self->[SELF_KEYMAP];
1026 my $fn = $mapping->{default};
1027  
1028 if (exists $mapping->{binding}->{$raw_key}) {
1029 $fn = $mapping->{binding}->{$raw_key};
1030 }
1031  
1032 # print "\r\ninvoking $fn for $key\r\n";$self->_repaint_input_line;
1033 if ($self->[SELF_COUNT] && !grep { $_ eq $fn } @fns_counting) {
1034 $self->[SELF_COUNT] = int($self->[SELF_COUNT]);
1035 $self->[SELF_COUNT] ||= 1;
1036 while ($self->[SELF_COUNT] > 0) {
1037 if (ref $fn) {
1038 $self->$fn($key, $raw_key);
1039 } else {
1040 &{$defuns->{$fn}}($self, $key, $raw_key);
1041 }
1042 $self->[SELF_COUNT]--;
1043 }
1044 $self->[SELF_COUNT] = "";
1045 } else {
1046 if (ref $fn) {
1047 $self->$fn($key, $raw_key);
1048 } else {
1049 &{$defuns->{$fn}}($self, $key, $raw_key);
1050 }
1051 }
1052 $self->[SELF_LAST] = $fn unless grep { $_ eq $fn } @fns_anon;
1053 }
1054  
1055 # Send a prompt; get a line.
1056 sub get {
1057 my ($self, $prompt) = @_;
1058  
1059 # Already reading a line here, people. Sheesh!
1060 if ($self->[SELF_READING_LINE]) {
1061 # Let's update the prompt if the user changed it
1062 if (defined $prompt && $prompt ne $self->[SELF_PROMPT]) {
1063 $self->_wipe_input_line;
1064 $self->[SELF_PROMPT] = $prompt;
1065 $self->_repaint_input_line;
1066 }
1067 return;
1068 }
1069  
1070 # recheck the terminal size every prompt, in case the size
1071 # has changed
1072 eval { ($trk_cols, $trk_rows) = GetTerminalSize($stdout) };
1073 ($trk_cols, $trk_rows) = (80, 25) if $@;
1074  
1075 ReadMode('ultra-raw');
1076 # Tell the terminal that we want to be in 'application' mode.
1077 print $termcap->Tputs('ks' => 1) if $termcap->Tputs('ks');
1078  
1079 # Set up for the read.
1080 $self->[SELF_READING_LINE] = 1;
1081 $self->[SELF_PROMPT] = $prompt if defined $prompt;
1082 $self->[SELF_INPUT] = '';
1083 $self->[SELF_CURSOR_INPUT] = 0;
1084 $self->[SELF_CURSOR_DISPLAY] = 0;
1085 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
1086 $self->[SELF_INSERT_MODE] = 1;
1087 $self->[SELF_UNDO] = [];
1088 $self->[SELF_LAST] = '';
1089  
1090 # Watch the filehandle. STDIN is made blocking to avoid buffer
1091 # overruns when put()ing large quantities of data.
1092 # TODO - Why does it matter to STDOUT whether STDIN is blocking?
1093 # TODO - Why does AIX require STDIN to be non-blocking?
1094 $poe_kernel->select($stdin, $self->[SELF_STATE_READ]);
1095 $stdin->blocking(1) unless $^O eq 'aix';
1096  
1097 my $sp = $self->[SELF_PROMPT];
1098 $sp =~ s{\\[\[\]]}{}g;
1099  
1100 print $stdout $sp;
1101 }
1102  
1103 # Write a line on the terminal.
1104 sub put {
1105 my $self = shift;
1106 my @lines = map { $_ . "\x0D\x0A" } @_;
1107  
1108 # Write stuff immediately under certain conditions: (1) The wheel is
1109 # in immediate mode. (2) The wheel currently isn't reading a line.
1110 # (3) The wheel is in idle mode, and there.
1111  
1112 if (
1113 $self->[SELF_PUT_MODE] eq 'immediate' or
1114 !$self->[SELF_READING_LINE] or
1115 ( $self->[SELF_PUT_MODE] eq 'idle' and !$self->[SELF_HAS_TIMER] )
1116 ) {
1117  
1118 # Only clear the input line if we're reading input already
1119 $self->_wipe_input_line if ($self->[SELF_READING_LINE]);
1120  
1121 # Print the new stuff.
1122 $self->_flush_output_buffer;
1123 print $stdout @lines;
1124  
1125 # Only repaint the input if we're reading a line.
1126 $self->_repaint_input_line if ($self->[SELF_READING_LINE]);
1127  
1128 return;
1129 }
1130  
1131 # Otherwise buffer stuff.
1132 push @{$self->[SELF_PUT_BUFFER]}, @lines;
1133  
1134 # Set a timer, if timed.
1135 if ( $self->[SELF_PUT_MODE] eq 'idle' and !$self->[SELF_HAS_TIMER] ) {
1136 $poe_kernel->delay( $self->[SELF_STATE_IDLE], $self->[SELF_IDLE_TIME] );
1137 $self->[SELF_HAS_TIMER] = 1;
1138 }
1139 }
1140  
1141 # Clear the screen.
1142 sub clear {
1143 my $self = shift;
1144 $termcap->Tputs( cl => 1, $stdout );
1145 }
1146  
1147 sub terminal_size {
1148 return ($trk_cols, $trk_rows);
1149 }
1150  
1151 # Add things to the edit history.
1152 sub add_history {
1153 my $self = shift;
1154 push @{$self->[SELF_HIST_LIST]}, @_;
1155 }
1156  
1157 # RCC 2008-06-15. Backwards compatibility.
1158 *addhistory = *add_history;
1159  
1160 sub get_history {
1161 my $self = shift;
1162 return @{$self->[SELF_HIST_LIST]};
1163 }
1164  
1165 # RCC 2008-06-15. Backwards compatibility.
1166 *GetHistory = *get_history;
1167  
1168 sub write_history {
1169 my ($self, $file) = @_;
1170 $file ||= "$ENV{HOME}/.history";
1171 open(HIST, ">$file") || return undef;
1172 print HIST join("\n", @{$self->[SELF_HIST_LIST]}) . "\n";
1173 close(HIST);
1174 return 1;
1175 }
1176  
1177 # RCC 2008-06-15. Backwards compatibility.
1178 *WriteHistory = *write_history;
1179  
1180 sub read_history {
1181 my ($self, $file, $from, $to) = @_;
1182 $from ||= 0;
1183 $to = -1 unless defined $to;
1184 $file ||= "$ENV{HOME}/.history";
1185 open(HIST, $file) or return undef;
1186 my @hist = <HIST>;
1187 close(HIST);
1188 my $line = 0;
1189 foreach my $h (@hist) {
1190 chomp($h);
1191 $self->add_history($h) if ($line >= $from && ($to < $from || $line <= $to));
1192 $line++;
1193 }
1194 return 1;
1195 }
1196  
1197 # RCC 2008-06-15. Backwards compatibility.
1198 *ReadHistory = *read_history;
1199  
1200 sub history_truncate_file {
1201 my ($self, $file, $lines) = @_;
1202 $lines ||= 0;
1203 $file ||= "$ENV{HOME}/.history";
1204 open(HIST, $file) or return undef;
1205 my @hist = <HIST>;
1206 close(HIST);
1207 chomp(@hist);
1208  
1209 if ((scalar @hist) > $lines) {
1210 open(HIST, ">$file") or return undef;
1211 if ($lines) {
1212 splice(@hist, 0, (scalar @hist)-$lines);
1213 @{$self->[SELF_HIST_LIST]} = @hist;
1214 print HIST "$_\n" foreach @hist;
1215 } else {
1216 @{$self->[SELF_HIST_LIST]} = ();
1217 }
1218 close(HIST);
1219 }
1220 return 1;
1221 }
1222  
1223 # Get the wheel's ID.
1224 sub ID {
1225 return $_[0]->[SELF_UNIQUE_ID];
1226 }
1227  
1228 sub attribs {
1229 my ($self) = @_;
1230 return $self->[SELF_OPTIONS];
1231 }
1232  
1233 # RCC 2008-06-15. Backwards compatibility.
1234 *Attribs = *attribs;
1235  
1236 sub option {
1237 my ($self, $arg) = @_;
1238 $arg = lc($arg);
1239 return "" unless exists $self->[SELF_OPTIONS]->{$arg};
1240 return $self->[SELF_OPTIONS]->{$arg};
1241 }
1242  
1243 sub _init_keymap {
1244 my ($self, $default, @names) = @_;
1245 my $name = $names[0];
1246 if (!exists $defuns->{$default}) {
1247 die("cannot initialise keymap $name, since default function $default is unknown")
1248 }
1249 my $map = POE::Wheel::ReadLine::Keymap->init(
1250 default => $default,
1251 name => $name,
1252 termcap => $termcap
1253 );
1254 foreach my $n (@names) {
1255 $self->[SELF_ALL_KEYMAPS]->{$n} = $map;
1256 }
1257 return $map;
1258 }
1259  
1260 sub rl_re_read_init_file {
1261 my ($self) = @_;
1262  
1263 $self->_init_keymap('self-insert', 'emacs');
1264 $self->_init_keymap('ding', 'vi-command', 'vi');
1265 $self->_init_keymap('self-insert', 'vi-insert');
1266  
1267 # searching
1268 my $isearch = $self->_init_keymap('search-finish', 'isearch');
1269 my $vi_search = $self->_init_keymap('search-finish', 'vi-search');
1270 $self->_parse_inputrc($search_inputrc);
1271  
1272 # A keymap to take the VI range specification commands
1273 # used by the -to commands (e.g. change-to, etc)
1274 $self->_init_keymap('vi-end-spec', 'vi-specification');
1275  
1276 $self->_parse_inputrc($defaults_inputrc);
1277  
1278 $self->rl_set_keymap('vi');
1279 $self->_parse_inputrc($vi_inputrc);
1280  
1281 $self->rl_set_keymap('emacs');
1282 $self->_parse_inputrc($emacs_inputrc);
1283  
1284 my $personal = exists $ENV{INPUTRC} ? $ENV{INPUTRC} : "$ENV{HOME}/.inputrc";
1285 foreach my $file ($personal) {
1286 my $input = "";
1287 if (open(IN, $file)) {
1288 local $/ = undef;
1289 $input = <IN>;
1290 close(IN);
1291 $self->_parse_inputrc($input);
1292 }
1293 }
1294  
1295 if (!$self->option('editing-mode')) {
1296 $self->[SELF_OPTIONS]->{'editing-mode'} = 'emacs';
1297 }
1298  
1299 if ($self->option('editing-mode') eq 'vi') {
1300 # by default, start in insert mode already
1301 $self->rl_set_keymap('vi-insert');
1302 }
1303  
1304 my $isearch_term = $self->option('isearch-terminators') || 'C-[ C-J';
1305 foreach my $key (split(/\s+/, $isearch_term)) {
1306 $isearch->bind_key($key, 'search-abort');
1307 }
1308 foreach my $key (ord(' ') .. ord('~')) {
1309 $isearch->bind_key('"' . chr($key) . '"', 'search-key');
1310 $vi_search->bind_key('"' . chr($key) . '"', 'vi-search-key');
1311 }
1312 }
1313  
1314 sub _parse_inputrc {
1315 my ($self, $input, $depth) = @_;
1316 $depth ||= 0;
1317 my @cond = (); # allows us to nest conditionals.
1318  
1319 foreach my $line (split(/\n+/, $input)) {
1320 next if $line =~ /^#/;
1321 if ($line =~ /^\$(.*)/) {
1322 my (@parms) = split(/[ \t+=]/,$1);
1323 if ($parms[0] eq 'if') {
1324 my $bool = 0;
1325 if ($parms[1] eq 'mode') {
1326 if ($self->option('editing-mode') eq $parms[2]) {
1327 $bool = 1;
1328 }
1329 } elsif ($parms[1] eq 'term') {
1330 my ($half, $full) = ($ENV{TERM} =~ /^([^-]*)(-.*)?$/);
1331 if ($half eq $parms[2] || ($full && $full eq $parms[2])) {
1332 $bool = 1;
1333 }
1334 } elsif ($parms[1] eq $self->[SELF_APP]) {
1335 $bool = 1;
1336 }
1337 push(@cond, $bool);
1338 } elsif ($parms[0] eq 'else') {
1339 $cond[$#cond] = not $cond[$#cond];
1340 } elsif ($parms[0] eq 'endif') {
1341 pop(@cond);
1342 } elsif ($parms[0] eq 'include') {
1343 if ($depth > 10) {
1344 print STDERR "WARNING: ignoring ``include $parms[1] directive, since we're too deep''";
1345 } else {
1346 my $fh = gensym;
1347 if (open $fh, "< $parms[1]\0") {
1348 my $contents = do { local $/; <$fh> };
1349 close $fh;
1350 $self->_parse_inputrc($contents, $depth+1);
1351 }
1352 }
1353 }
1354 } else {
1355 next if (scalar @cond and not $cond[$#cond]);
1356 if ($line =~ /^set\s+([\S]+)\s+([\S]+)/) {
1357 my ($var,$val) = ($1, $2);
1358 $self->[SELF_OPTIONS]->{lc($var)} = $val;
1359 my $fn = "rl_set_" . lc($var);
1360 $fn =~ s{-}{_}g;
1361 if ($self->can($fn)) {
1362 $self->$fn($self->[SELF_OPTIONS]->{$var});
1363 }
1364 } elsif ($line =~ /^([^:]+):\s*(.*)/) {
1365 my ($seq, $fn) = ($1, lc($2));
1366 chomp($fn);
1367 $self->[SELF_KEYMAP]->bind_key($seq, $fn);
1368 }
1369 }
1370 }
1371 }
1372  
1373 # take a key and output it in a form nice to read...
1374 sub _dump_key_line {
1375 my ($self, $key, $raw_key) = @_;
1376 if (exists $self->[SELF_KEYMAP]->{prefix}->{$raw_key}) {
1377 $self->[SELF_PENDING_FN] = sub {
1378 my ($s, $k, $rk) = @_;
1379 $s->_dump_key_line($key.$k, $raw_key.$rk);
1380 };
1381 return;
1382 }
1383  
1384 my $fn = $self->[SELF_KEYMAP]->{default};
1385 if (exists $self->[SELF_KEYMAP]->{binding}->{$raw_key}) {
1386 $fn = $self->[SELF_KEYMAP]->{binding}->{$raw_key};
1387 }
1388 if (ref $fn) {
1389 $fn = "[coderef]";
1390 }
1391  
1392 print "\x0D\x0A" . _readable_key($raw_key) . ": " . $fn . "\x0D\x0A";
1393 $self->_repaint_input_line;
1394 }
1395  
1396 sub bind_key {
1397 my ($self, $seq, $fn, $map) = @_;
1398 $map ||= $self->[SELF_KEYMAP];
1399 $map->bind_key($seq, $fn);
1400 }
1401  
1402 sub add_defun {
1403 my ($self, $name, $fn) = @_;
1404 $defuns->{$name} = $fn;
1405 }
1406  
1407 # -----------------------------------------------------
1408 # Any variable assignments that we care about
1409 # -----------------------------------------------------
1410 sub rl_set_keymap {
1411 my ($self, $arg) = @_;
1412 $arg = lc($arg);
1413 if (exists $self->[SELF_ALL_KEYMAPS]->{$arg}) {
1414 $self->[SELF_KEYMAP] = $self->[SELF_ALL_KEYMAPS]->{$arg};
1415 $self->[SELF_OPTIONS]->{keymap} = $self->[SELF_KEYMAP]->{name};
1416 }
1417 # always reset overstrike mode on keymap change
1418 $self->[SELF_INSERT_MODE] = 1;
1419 }
1420  
1421 # ----------------------------------------------------
1422 # From here on, we have the helper functions which can
1423 # be bound to keys. The functions are named after the
1424 # readline counterparts.
1425 # ----------------------------------------------------
1426  
1427 sub rl_self_insert {
1428 my ($self, $key, $raw_key) = @_;
1429  
1430 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
1431 if ($self->[SELF_INSERT_MODE]) {
1432 # Insert.
1433 my $normal = _normalize(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]));
1434 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 0) = $raw_key;
1435 print $stdout $key, $normal;
1436 $self->[SELF_CURSOR_INPUT] += length($raw_key);
1437 $self->[SELF_CURSOR_DISPLAY] += length($key);
1438 _curs_left(length($normal));
1439 } else {
1440 # Overstrike.
1441 my $replaced_width = _display_width(
1442 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], length($raw_key))
1443 );
1444 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], length($raw_key)) = $raw_key;
1445  
1446 print $stdout $key;
1447 $self->[SELF_CURSOR_INPUT] += length($raw_key);
1448 $self->[SELF_CURSOR_DISPLAY] += length($key);
1449  
1450 # Expand or shrink the display if unequal replacement.
1451 if (length($key) != $replaced_width) {
1452 my $rest = _normalize(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]));
1453 # Erase trailing screen cruft if it's shorter.
1454 if (length($key) < $replaced_width) {
1455 $rest .= ' ' x ($replaced_width - length($key));
1456 }
1457 print $stdout $rest;
1458 _curs_left(length($rest));
1459 }
1460 }
1461 } else {
1462 # Append.
1463 print $stdout $key;
1464 $self->[SELF_INPUT] .= $raw_key;
1465 $self->[SELF_CURSOR_INPUT] += length($raw_key);
1466 $self->[SELF_CURSOR_DISPLAY] += length($key);
1467 }
1468 }
1469  
1470 sub rl_insert_macro {
1471 my ($self, $key) = @_;
1472 my $macro = $self->[SELF_KEYMAP]->{macros}->{$key};
1473 $macro =~ s{\\a}{$tc_bell}g;
1474 $macro =~ s{\\r}{\r}g;
1475 $macro =~ s{\\n}{\n}g;
1476 $macro =~ s{\\t}{\t}g;
1477 $self->rl_self_insert($macro, $macro);
1478 }
1479  
1480 sub rl_insert_comment {
1481 my ($self) = @_;
1482 my $comment = $self->option('comment-begin');
1483 $self->_wipe_input_line;
1484 if ($self->[SELF_COUNT]) {
1485 if (substr($self->[SELF_INPUT], 0, length($comment)) eq $comment) {
1486 substr($self->[SELF_INPUT], 0, length($comment)) = "";
1487 } else {
1488 $self->[SELF_INPUT] = $comment . $self->[SELF_INPUT];
1489 }
1490 $self->[SELF_COUNT] = 0;
1491 } else {
1492 $self->[SELF_INPUT] = $comment . $self->[SELF_INPUT];
1493 }
1494 $self->_repaint_input_line;
1495 $self->rl_accept_line;
1496 }
1497  
1498 sub rl_revert_line {
1499 my ($self) = @_;
1500 return $self->rl_ding unless scalar @{$self->[SELF_UNDO]};
1501 $self->_wipe_input_line;
1502 (
1503 $self->[SELF_INPUT],
1504 $self->[SELF_CURSOR_INPUT],
1505 $self->[SELF_CURSOR_DISPLAY]
1506 ) = @{$self->[SELF_UNDO]->[0]};
1507 $self->[SELF_UNDO] = [];
1508 $self->_repaint_input_line;
1509 }
1510  
1511 sub rl_yank_last_arg {
1512 my ($self) = @_;
1513 if ($self->[SELF_HIST_INDEX] == 0) {
1514 return $self->rl_ding;
1515 }
1516 if ($self->[SELF_COUNT]) {
1517 return &rl_yank_nth_arg;
1518 }
1519 my $prev = $self->[SELF_HIST_LIST]->[$self->[SELF_HIST_INDEX]-1];
1520 my ($arg) = ($prev =~ m{(\S+)$});
1521 $self->rl_self_insert($arg, $arg);
1522 1;
1523 }
1524  
1525 sub rl_yank_nth_arg {
1526 my ($self) = @_;
1527 if ($self->[SELF_HIST_INDEX] == 0) {
1528 return $self->rl_ding;
1529 }
1530 my $prev = $self->[SELF_HIST_LIST]->[$self->[SELF_HIST_INDEX]-1];
1531 my @args = split(/\s+/, $prev);
1532 my $pos = $self->[SELF_COUNT] || 1;
1533 $self->[SELF_COUNT] = 0;
1534 if ($pos < 0) {
1535 $pos = (scalar @args) + $pos;
1536 }
1537 if ($pos > scalar @args || $pos < 0) {
1538 return $self->rl_ding;
1539 }
1540 $self->rl_self_insert($args[$pos], $args[$pos]);
1541 }
1542  
1543 sub rl_dump_key {
1544 my ($self) = @_;
1545 $self->[SELF_PENDING_FN] = sub {
1546 my ($s,$k,$rk) = @_;
1547 $s->_dump_key_line($k, $rk);
1548 };
1549 }
1550  
1551 sub rl_dump_macros {
1552 my ($self) = @_;
1553 print $stdout "\x0D\x0A";
1554 my $c = 0;
1555 foreach my $macro (keys %{$self->[SELF_KEYMAP]->{macros}}) {
1556 print $stdout '"' . _normalize($macro) . "\": \"$self->[SELF_KEYMAP]->{macros}->{$macro}\"\x0D\x0A";
1557 $c++;
1558 }
1559 if (!$c) {
1560 print "# no macros defined\x0D\x0A";
1561 }
1562 $self->_repaint_input_line;
1563 }
1564  
1565 sub rl_dump_variables {
1566 my ($self) = @_;
1567 print $stdout "\x0D\x0A";
1568 my $c = 0;
1569 foreach my $var (keys %{$self->[SELF_OPTIONS]}) {
1570 print $stdout "set $var $self->[SELF_OPTIONS]->{$var}\x0D\x0A";
1571 $c++;
1572 }
1573 if (!$c) {
1574 print "# no variables defined\x0D\x0A";
1575 }
1576 $self->_repaint_input_line;
1577 }
1578  
1579 sub rl_set_mark {
1580 my ($self) = @_;
1581 if ($self->[SELF_COUNT]) {
1582 $self->[SELF_MARK] = $self->[SELF_COUNT];
1583 } else {
1584 $self->[SELF_MARK] = $self->[SELF_CURSOR_INPUT];
1585 }
1586 $self->[SELF_COUNT] = 0;
1587 }
1588  
1589 sub rl_digit_argument {
1590 my ($self, $key) = @_;
1591 $self->[SELF_COUNT] .= $key;
1592 }
1593  
1594 sub rl_beginning_of_line {
1595 my ($self, $key) = @_;
1596 if ($self->[SELF_CURSOR_INPUT]) {
1597 _curs_left($self->[SELF_CURSOR_DISPLAY]);
1598 $self->[SELF_CURSOR_DISPLAY] = $self->[SELF_CURSOR_INPUT] = 0;
1599 }
1600 }
1601  
1602 sub rl_end_of_line {
1603 my ($self, $key) = @_;
1604 my $max = length($self->[SELF_INPUT]);
1605 $max-- if ($self->[SELF_KEYMAP]->{name} =~ /vi/);
1606 if ($self->[SELF_CURSOR_INPUT] < $max) {
1607 my $right_string = substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]);
1608 print _normalize($right_string);
1609 my $right = _display_width($right_string);
1610 if ($self->[SELF_KEYMAP]->{name} =~ /vi/) {
1611 $self->[SELF_CURSOR_DISPLAY] += $right - 1;
1612 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]) - 1;
1613 _curs_left(1);
1614 } else {
1615 $self->[SELF_CURSOR_DISPLAY] += $right;
1616 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
1617 }
1618 }
1619 }
1620  
1621 sub rl_backward_char {
1622 my ($self, $key) = @_;
1623 if ($self->[SELF_CURSOR_INPUT]) {
1624 $self->[SELF_CURSOR_INPUT]--;
1625 my $left = _display_width(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1));
1626 _curs_left($left);
1627 $self->[SELF_CURSOR_DISPLAY] -= $left;
1628 }
1629 else {
1630 $self->rl_ding;
1631 }
1632 }
1633  
1634 sub rl_forward_char {
1635 my ($self, $key) = @_;
1636 my $max = length($self->[SELF_INPUT]);
1637 $max-- if ($self->[SELF_KEYMAP]->{name} =~ /vi/);
1638 if ($self->[SELF_CURSOR_INPUT] < $max) {
1639 my $normal = _normalize(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1));
1640 print $stdout $normal;
1641 $self->[SELF_CURSOR_INPUT]++;
1642 $self->[SELF_CURSOR_DISPLAY] += length($normal);
1643 } else {
1644 $self->rl_ding;
1645 }
1646 }
1647  
1648 sub rl_forward_word {
1649 my ($self, $key) = @_;
1650 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\W*\w+)/) {
1651 $self->[SELF_CURSOR_INPUT] += length($1);
1652 my $right = _display_width($1);
1653 print _normalize($1);
1654 $self->[SELF_CURSOR_DISPLAY] += $right;
1655 } else {
1656 $self->rl_ding;
1657 }
1658 }
1659  
1660 sub rl_backward_word {
1661 my ($self, $key) = @_;
1662 if (substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]) =~ /(\w+\W*)$/) {
1663 $self->[SELF_CURSOR_INPUT] -= length($1);
1664 my $left = _display_width($1);
1665 _curs_left($left);
1666 $self->[SELF_CURSOR_DISPLAY] -= $left;
1667 } else {
1668 $self->rl_ding;
1669 }
1670 }
1671  
1672 sub rl_backward_kill_word {
1673 my ($self) = @_;
1674 if ($self->[SELF_CURSOR_INPUT]) {
1675 substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]) =~ /(\w*\W*)$/;
1676 my $kill = $self->_delete_chars($self->[SELF_CURSOR_INPUT] - length($1), length($1));
1677 push(@{$self->[SELF_KILL_RING]}, $kill);
1678 } else {
1679 $self->rl_ding;
1680 }
1681 }
1682  
1683 sub rl_kill_region {
1684 my ($self) = @_;
1685 my $kill = $self->_delete_chars($self->[SELF_CURSOR_INPUT], $self->[SELF_CURSOR_INPUT] - $self->[SELF_MARK]);
1686 push(@{$self->[SELF_KILL_RING]}, $kill);
1687 }
1688  
1689 sub rl_kill_word {
1690 my ($self, $key) = @_;
1691 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
1692 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\W*\w*\W*)/;
1693 my $kill = $self->_delete_chars($self->[SELF_CURSOR_INPUT], length($1));
1694 push(@{$self->[SELF_KILL_RING]}, $kill);
1695 } else {
1696 $self->rl_ding;
1697 }
1698 }
1699  
1700 sub rl_kill_line {
1701 my ($self, $key) = @_;
1702 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
1703 my $kill = $self->_delete_chars($self->[SELF_CURSOR_INPUT], length($self->[SELF_INPUT]) - $self->[SELF_CURSOR_INPUT]);
1704 push(@{$self->[SELF_KILL_RING]}, $kill);
1705 } else {
1706 $self->rl_ding;
1707 }
1708 }
1709  
1710 sub rl_unix_word_rubout {
1711 my ($self, $key) = @_;
1712 if ($self->[SELF_CURSOR_INPUT]) {
1713 substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]) =~ /(\S*\s*)$/;
1714 my $kill = $self->_delete_chars($self->[SELF_CURSOR_INPUT] - length($1), length($1));
1715 push(@{$self->[SELF_KILL_RING]}, $kill);
1716 } else {
1717 $self->rl_ding;
1718 }
1719 }
1720  
1721 sub rl_delete_horizontal_space {
1722 my ($self) = @_;
1723 substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]) =~ /(\s*)$/;
1724 my $left = length($1);
1725 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)/;
1726 my $right = length($1);
1727  
1728 if ($left + $right) {
1729 $self->_delete_chars($self->[SELF_CURSOR_INPUT] - $left, $left + $right);
1730 } else {
1731 $self->rl_ding;
1732 }
1733 }
1734  
1735 sub rl_copy_region_as_kill {
1736 my ($self) = @_;
1737 my $from = $self->[SELF_CURSOR_INPUT];
1738 my $howmany = $self->[SELF_CURSOR_INPUT] - $self->[SELF_MARK];
1739 if ($howmany < 0) {
1740 $from -= $howmany;
1741 $howmany = -$howmany;
1742 if ($from < 0) {
1743 $howmany -= $from;
1744 $from = 0;
1745 }
1746 }
1747 my $old = substr($self->[SELF_INPUT], $from, $howmany);
1748 push(@{$self->[SELF_KILL_RING]}, $old);
1749 }
1750  
1751 sub rl_abort {
1752 my ($self, $key) = @_;
1753 print $stdout uc($key), "\x0D\x0A";
1754 $poe_kernel->select_read($stdin);
1755 if ($self->[SELF_HAS_TIMER]) {
1756 $poe_kernel->delay( $self->[SELF_STATE_IDLE] );
1757 $self->[SELF_HAS_TIMER] = 0;
1758 }
1759 $poe_kernel->yield(
1760 $self->[SELF_EVENT_INPUT],
1761 undef, 'cancel', $self->[SELF_UNIQUE_ID]
1762 );
1763 $self->[SELF_READING_LINE] = 0;
1764 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
1765 $self->_flush_output_buffer;
1766 }
1767  
1768 sub rl_interrupt {
1769 my ($self, $key) = @_;
1770 print $stdout uc($key), "\x0D\x0A";
1771 $poe_kernel->select_read($stdin);
1772 if ($self->[SELF_HAS_TIMER]) {
1773 $poe_kernel->delay( $self->[SELF_STATE_IDLE] );
1774 $self->[SELF_HAS_TIMER] = 0;
1775 }
1776 $poe_kernel->yield( $self->[SELF_EVENT_INPUT], undef, 'interrupt', $self->[SELF_UNIQUE_ID] );
1777 $self->[SELF_READING_LINE] = 0;
1778 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
1779  
1780 $self->_flush_output_buffer;
1781 }
1782  
1783 # Delete a character. On an empty line, it throws an
1784 # "eot" exception, just like Term::ReadLine does.
1785 sub rl_delete_char {
1786 my ($self, $key) = @_;
1787 if (length $self->[SELF_INPUT] == 0) {
1788 print $stdout uc($key), "\x0D\x0A";
1789 $poe_kernel->select_read($stdin);
1790 if ($self->[SELF_HAS_TIMER]) {
1791 $poe_kernel->delay( $self->[SELF_STATE_IDLE] );
1792 $self->[SELF_HAS_TIMER] = 0;
1793 }
1794 $poe_kernel->yield(
1795 $self->[SELF_EVENT_INPUT],
1796 undef, "eot", $self->[SELF_UNIQUE_ID]
1797 );
1798 $self->[SELF_READING_LINE] = 0;
1799 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
1800  
1801 $self->_flush_output_buffer;
1802 return;
1803 }
1804  
1805 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
1806 $self->_delete_chars($self->[SELF_CURSOR_INPUT], 1);
1807 } else {
1808 $self->rl_ding;
1809 }
1810 }
1811  
1812 sub rl_backward_delete_char {
1813 my ($self, $key) = @_;
1814 if ($self->[SELF_CURSOR_INPUT]) {
1815 $self->_delete_chars($self->[SELF_CURSOR_INPUT]-1, 1);
1816 } else {
1817 $self->rl_ding;
1818 }
1819 }
1820  
1821 sub rl_accept_line {
1822 my ($self, $key) = @_;
1823 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
1824 my $right_string = substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]);
1825 print _normalize($right_string);
1826 my $right = _display_width($right_string);
1827 $self->[SELF_CURSOR_DISPLAY] += $right;
1828 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
1829 }
1830 # home the cursor.
1831 $self->[SELF_CURSOR_DISPLAY] = 0;
1832 $self->[SELF_CURSOR_INPUT] = 0;
1833 print $stdout "\x0D\x0A";
1834 $poe_kernel->select_read($stdin);
1835 if ($self->[SELF_HAS_TIMER]) {
1836 $poe_kernel->delay( $self->[SELF_STATE_IDLE] );
1837 $self->[SELF_HAS_TIMER] = 0;
1838 }
1839 $poe_kernel->yield( $self->[SELF_EVENT_INPUT], $self->[SELF_INPUT], $self->[SELF_UNIQUE_ID] );
1840 $self->[SELF_READING_LINE] = 0;
1841 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
1842 $self->_flush_output_buffer;
1843 ReadMode('restore');
1844 eval { ($trk_cols, $trk_rows) = GetTerminalSize($stdout) };
1845 ($trk_cols, $trk_rows) = (80, 25) if $@;
1846 if ($self->[SELF_KEYMAP]->{name} =~ /vi/) {
1847 $self->rl_set_keymap('vi-insert');
1848 }
1849 }
1850  
1851 sub rl_clear_screen {
1852 my ($self, $key) = @_;
1853 my $left = _display_width(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]));
1854 $termcap->Tputs( 'cl', 1, $stdout );
1855 my $sp = $self->[SELF_PROMPT];
1856 $sp =~ s{\\[\[\]]}{}g;
1857 print $stdout $sp, _normalize($self->[SELF_INPUT]);
1858 _curs_left($left) if $left;
1859 }
1860  
1861 sub rl_transpose_chars {
1862 my ($self, $key) = @_;
1863 if (length($self->[SELF_INPUT]) > 1 && length($self->[SELF_INPUT]) == $self->[SELF_CURSOR_INPUT]) {
1864 my $transposition = reverse substr($self->[SELF_INPUT], -2, 2);
1865 substr($self->[SELF_INPUT], -2, 2) = $transposition;
1866 _curs_left(_display_width($transposition));
1867 print $stdout _normalize($transposition);
1868 } elsif (length($self->[SELF_INPUT]) > 1 && $self->[SELF_CURSOR_INPUT] > 0) {
1869 my $width_left = _display_width(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT] - 1, 1));
1870  
1871 my $transposition = reverse substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT] - 1, 2);
1872 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT] - 1, 2) = $transposition;
1873  
1874 _curs_left($width_left);
1875 print $stdout _normalize($transposition);
1876 _curs_left($width_left);
1877 } else {
1878 $self->rl_ding;
1879 }
1880 }
1881  
1882 sub rl_transpose_words {
1883 my ($self, $key) = @_;
1884  
1885 my $cursor_sub_one = $self->[SELF_CURSOR_INPUT] - 1;
1886 if ($self->[SELF_INPUT] =~ s/^(.{0,$cursor_sub_one})(?<!\S)(\S+)(\s+)(\S+)/$1$4$3$2/) {
1887 $termcap->Tgoto('LE', 1, $self->[SELF_CURSOR_DISPLAY] - _display_width($1), $stdout);
1888 print $stdout _normalize($4 . $3 . $2);
1889 $self->[SELF_CURSOR_INPUT] = length($1 . $2 . $3 . $4);
1890 $self->[SELF_CURSOR_DISPLAY] = _display_width($1 . $2 . $3 . $4);
1891 } else {
1892 $self->rl_ding;
1893 }
1894 }
1895  
1896 sub rl_unix_line_discard {
1897 my ($self, $key) = @_;
1898 if (length $self->[SELF_INPUT]) {
1899 my $kill = $self->_delete_chars(0, $self->[SELF_CURSOR_INPUT]);
1900 push(@{$self->[SELF_KILL_RING]}, $kill);
1901 } else {
1902 $self->rl_ding;
1903 }
1904 }
1905  
1906 sub rl_kill_whole_line {
1907 my ($self, $key) = @_;
1908 if (length $self->[SELF_INPUT]) {
1909 # Back up to the beginning of the line.
1910 if ($self->[SELF_CURSOR_INPUT]) {
1911 _curs_left($self->[SELF_CURSOR_DISPLAY]);
1912 $self->[SELF_CURSOR_DISPLAY] = $self->[SELF_CURSOR_INPUT] = 0;
1913 }
1914 $self->_clear_to_end;
1915  
1916 # Clear the input buffer.
1917 push(@{$self->[SELF_KILL_RING]}, $self->[SELF_INPUT]);
1918 $self->[SELF_INPUT] = '';
1919 } else {
1920 $self->rl_ding;
1921 }
1922 }
1923  
1924 sub rl_yank {
1925 my ($self) = @_;
1926 my $pos = scalar @{$self->[SELF_KILL_RING]};
1927 return $self->rl_ding unless ($pos);
1928  
1929 $pos--;
1930 $self->rl_self_insert($self->[SELF_KILL_RING]->[$pos], $self->[SELF_KILL_RING]->[$pos]);
1931 }
1932  
1933 sub rl_yank_pop {
1934 my ($self) = @_;
1935 return $self->rl_ding unless ($self->[SELF_LAST] =~ /yank/);
1936 my $pos = scalar @{$self->[SELF_KILL_RING]};
1937 return $self->rl_ding unless ($pos);
1938  
1939 my $top = pop @{$self->[SELF_KILL_RING]};
1940 unshift(@{$self->[SELF_KILL_RING]}, $top);
1941 $self->rl_yank;
1942 }
1943  
1944 sub rl_previous_history {
1945 my ($self, $key) = @_;
1946 if ($self->[SELF_HIST_INDEX]) {
1947 # Moving away from a new input line; save it in case
1948 # we return.
1949 if ($self->[SELF_HIST_INDEX] == @{$self->[SELF_HIST_LIST]}) {
1950 $self->[SELF_INPUT_HOLD] = $self->[SELF_INPUT];
1951 }
1952  
1953 # Move cursor to start of input.
1954 if ($self->[SELF_CURSOR_INPUT]) {
1955 _curs_left($self->[SELF_CURSOR_DISPLAY]);
1956 }
1957 $self->_clear_to_end;
1958  
1959 # Move the history cursor back, set the new input
1960 # buffer, and show what the user's editing. Set the
1961 # cursor to the end of the new line.
1962 my $normal;
1963 print $stdout $normal = _normalize($self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[--$self->[SELF_HIST_INDEX]]);
1964 $self->[SELF_UNDO] = [ [ $self->[SELF_INPUT], 0, 0 ] ]; # reset undo info
1965 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
1966 $self->[SELF_CURSOR_DISPLAY] = length($normal);
1967 $self->rl_backward_char if (length($self->[SELF_INPUT]) && $self->[SELF_KEYMAP]->{name} =~ /vi/);
1968 } else {
1969 # At top of history list.
1970 $self->rl_ding;
1971 }
1972 }
1973  
1974 sub rl_next_history {
1975 my ($self, $key) = @_;
1976 if ($self->[SELF_HIST_INDEX] < @{$self->[SELF_HIST_LIST]}) {
1977 # Move cursor to start of input.
1978 if ($self->[SELF_CURSOR_INPUT]) {
1979 _curs_left($self->[SELF_CURSOR_DISPLAY]);
1980 }
1981 $self->_clear_to_end;
1982  
1983 my $normal;
1984 if (++$self->[SELF_HIST_INDEX] == @{$self->[SELF_HIST_LIST]}) {
1985 # Just past the end of the history. Whatever was
1986 # there when we left it.
1987 print $stdout $normal = _normalize($self->[SELF_INPUT] = $self->[SELF_INPUT_HOLD]);
1988 } else {
1989 # There's something in the history list. Make that
1990 # the current line.
1991 print $stdout $normal = _normalize($self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[$self->[SELF_HIST_INDEX]]);
1992 }
1993  
1994 $self->[SELF_UNDO] = [ [ $self->[SELF_INPUT], 0, 0 ] ]; # reset undo info
1995 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
1996 $self->[SELF_CURSOR_DISPLAY] = length($normal);
1997 $self->rl_backward_char if (length($self->[SELF_INPUT]) && $self->[SELF_KEYMAP]->{name} =~ /vi/);
1998 } else {
1999 $self->rl_ding;
2000 }
2001 }
2002  
2003 sub rl_beginning_of_history {
2004 my ($self) = @_;
2005 # First in history.
2006 if ($self->[SELF_HIST_INDEX]) {
2007 # Moving away from a new input line; save it in case
2008 # we return.
2009 if ($self->[SELF_HIST_INDEX] == @{$self->[SELF_HIST_LIST]}) {
2010 $self->[SELF_INPUT_HOLD] = $self->[SELF_INPUT];
2011 }
2012  
2013 # Move cursor to start of input.
2014 if ($self->[SELF_CURSOR_INPUT]) {
2015 _curs_left($self->[SELF_CURSOR_DISPLAY]);
2016 }
2017 $self->_clear_to_end;
2018  
2019 # Move the history cursor back, set the new input
2020 # buffer, and show what the user's editing. Set the
2021 # cursor to the end of the new line.
2022 print $stdout my $normal =
2023 _normalize($self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[$self->[SELF_HIST_INDEX] = 0]);
2024 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
2025 $self->[SELF_CURSOR_DISPLAY] = length($normal);
2026 $self->[SELF_UNDO] = [ [ $self->[SELF_INPUT], 0, 0 ] ]; # reset undo info
2027 } else {
2028 # At top of history list.
2029 $self->rl_ding;
2030 }
2031 }
2032  
2033 sub rl_end_of_history {
2034 my ($self) = @_;
2035 if ($self->[SELF_HIST_INDEX] != @{$self->[SELF_HIST_LIST]} - 1) {
2036  
2037 # Moving away from a new input line; save it in case
2038 # we return.
2039 if ($self->[SELF_HIST_INDEX] == @{$self->[SELF_HIST_LIST]}) {
2040 $self->[SELF_INPUT_HOLD] = $self->[SELF_INPUT];
2041 }
2042  
2043 # Move cursor to start of input.
2044 if ($self->[SELF_CURSOR_INPUT]) {
2045 _curs_left($self->[SELF_CURSOR_DISPLAY]);
2046 }
2047 $self->_clear_to_end;
2048  
2049 # Move the edit line down to the last history line.
2050 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]} - 1;
2051 print $stdout my $normal = _normalize($self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[$self->[SELF_HIST_INDEX]]);
2052 $self->[SELF_CURSOR_INPUT] = length($self->[SELF_INPUT]);
2053 $self->[SELF_CURSOR_DISPLAY] = length($normal);
2054 $self->[SELF_UNDO] = [ [ $self->[SELF_INPUT], 0, 0 ] ]; # reset undo info
2055 } else {
2056 $self->rl_ding;
2057 }
2058 }
2059  
2060 sub rl_forward_search_history {
2061 my ($self, $key) = @_;
2062 $self->_wipe_input_line;
2063 $self->[SELF_PREV_PROMPT] = $self->[SELF_PROMPT];
2064 $self->[SELF_SEARCH_PROMPT] = '(forward-i-search)`%s\': ';
2065 $self->[SELF_SEARCH_MAP] = $self->[SELF_KEYMAP];
2066 $self->[SELF_SEARCH_DIR] = +1;
2067 $self->[SELF_SEARCH_KEY] = $key;
2068 $self->_build_search_prompt;
2069 $self->_repaint_input_line;
2070 $self->rl_set_keymap('isearch');
2071 }
2072  
2073 sub rl_reverse_search_history {
2074 my ($self, $key) = @_;
2075 $self->_wipe_input_line;
2076 $self->[SELF_PREV_PROMPT] = $self->[SELF_PROMPT];
2077 $self->[SELF_SEARCH_PROMPT] = '(reverse-i-search)`%s\': ';
2078 $self->[SELF_SEARCH_MAP] = $self->[SELF_KEYMAP];
2079 $self->[SELF_SEARCH_DIR] = -1;
2080 $self->[SELF_SEARCH_KEY] = $key;
2081 # start at the previous line...
2082 $self->[SELF_HIST_INDEX]-- if $self->[SELF_HIST_INDEX];
2083 $self->_build_search_prompt;
2084 $self->_repaint_input_line;
2085 $self->rl_set_keymap('isearch');
2086 }
2087  
2088 sub rl_capitalize_word {
2089 my ($self, $key) = @_;
2090 # Capitalize from cursor on.
2091 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)(\S+)/) {
2092 # Track leading space, and uppercase word.
2093 my $space = $1; $space = '' unless defined $space;
2094 my $word = ucfirst(lc($2));
2095  
2096 # Replace text with the uppercase version.
2097 substr(
2098 $self->[SELF_INPUT],
2099 $self->[SELF_CURSOR_INPUT] + length($space),
2100 length($word)
2101 ) = $word;
2102  
2103 # Display the new text; move the cursor after it.
2104 print $stdout $space, _normalize($word);
2105 $self->[SELF_CURSOR_INPUT] += length($space . $word);
2106 $self->[SELF_CURSOR_DISPLAY] += length($space) + _display_width($word);
2107 } else {
2108 $self->rl_ding;
2109 }
2110 }
2111  
2112 sub rl_upcase_word {
2113 my ($self, $key) = @_;
2114 # Uppercase from cursor on.
2115 # Modeled after capitalize.
2116 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)(\S+)/) {
2117 my $space = $1; $space = '' unless defined $space;
2118 my $word = uc($2);
2119 substr(
2120 $self->[SELF_INPUT],
2121 $self->[SELF_CURSOR_INPUT] + length($space),
2122 length($word)
2123 ) = $word;
2124 print $stdout $space, _normalize($word);
2125 $self->[SELF_CURSOR_INPUT] += length($space . $word);
2126 $self->[SELF_CURSOR_DISPLAY] += length($space) + _display_width($word);
2127 } else {
2128 $self->rl_ding;
2129 }
2130 }
2131  
2132  
2133 sub rl_downcase_word {
2134 my ($self, $key) = @_;
2135 # Lowercase from cursor on.
2136 # Modeled after capitalize.
2137 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)(\S+)/) {
2138 my $space = $1; $space = '' unless defined $space;
2139 my $word = lc($2);
2140 substr(
2141 $self->[SELF_INPUT],
2142 $self->[SELF_CURSOR_INPUT] + length($space),
2143 length($word)
2144 ) = $word;
2145 print $stdout $space, _normalize($word);
2146 $self->[SELF_CURSOR_INPUT] += length($space . $word);
2147 $self->[SELF_CURSOR_DISPLAY] += length($space) + _display_width($word);
2148 } else {
2149 $self->rl_ding;
2150 }
2151 }
2152  
2153 sub rl_quoted_insert {
2154 my ($self, $key) = @_;
2155 $self->[SELF_PENDING_FN] = sub {
2156 my ($s,$k,$rk) = @_;
2157 $s->rl_self_insert($k, $rk);
2158 };
2159 }
2160  
2161 sub rl_overwrite_mode {
2162 my ($self, $key) = @_;
2163 $self->[SELF_INSERT_MODE] = !$self->[SELF_INSERT_MODE];
2164 if ($self->[SELF_COUNT]) {
2165 if ($self->[SELF_COUNT] > 0) {
2166 $self->[SELF_INSERT_MODE] = 0;
2167 } else {
2168 $self->[SELF_INSERT_MODE] = 1;
2169 }
2170 }
2171 }
2172  
2173 sub rl_vi_replace {
2174 my ($self) = @_;
2175 $self->rl_vi_insertion_mode;
2176 $self->rl_overwrite_mode;
2177 }
2178  
2179 sub rl_tilde_expand {
2180 my ($self) = @_;
2181 my $pre = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2182 my ($append) = (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\w+)/);
2183 my ($left,$user) = ("$pre$append" =~ /^(.*)~(\S+)$/);
2184 if ($user) {
2185 my $dir = (getpwnam($user))[7];
2186 if (!$dir) {
2187 print "\x0D\x0Ausername '$user' not found\x0D\x0A";
2188 $self->_repaint_input_line;
2189 return $self->rl_ding;
2190 }
2191 $self->_wipe_input_line;
2192 substr($self->[SELF_INPUT], length($left), length($user) + 1) = $dir; # +1 for tilde
2193 $self->[SELF_CURSOR_INPUT] += length($dir) - length($user) - 1;
2194 $self->[SELF_CURSOR_DISPLAY] += length($dir) - length($user) - 1;
2195 $self->_repaint_input_line;
2196 return 1;
2197 } else {
2198 return $self->rl_ding;
2199 }
2200 }
2201  
2202 sub _complete_match {
2203 my ($self) = @_;
2204 my $lookfor = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2205 $lookfor =~ /(\S+)$/;
2206 $lookfor = defined($1) ? $1 : "";
2207 my $point = $self->[SELF_CURSOR_INPUT] - length($lookfor);
2208  
2209 my @clist = ();
2210 if ($self->option("completion_function")) {
2211 my $fn = $self->[SELF_OPTIONS]->{completion_function};
2212 @clist = &$fn($lookfor, $self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]);
2213 }
2214 my @poss = @clist;
2215 if ($lookfor) {
2216 my $l = length $lookfor;
2217 @poss = grep { substr($_, 0, $l) eq $lookfor } @clist;
2218 }
2219  
2220 return @poss;
2221 }
2222  
2223 sub _complete_list {
2224 my ($self, @poss) = @_;
2225 my $width = 0;
2226 if ($self->option('print-completions-horizontally') eq 'on') {
2227 map { $width = (length($_) > $width) ? length($_) : $width } @poss;
2228 my $cols = int($trk_cols / $width);
2229 $cols = int($trk_cols / ($width+$cols)); # ensure enough room for spaces
2230 $width = int($trk_cols / $cols);
2231  
2232 print $stdout "\x0D\x0A";
2233 my $c = 0;
2234 foreach my $word (@poss) {
2235 print $stdout $word . (" " x ($width - length($word)));
2236 if (++$c == $cols) {
2237 print $stdout "\x0D\x0A";
2238 $c = 0;
2239 }
2240 }
2241 print "\x0D\x0A" if $c;
2242 } else {
2243 print "\x0D\x0A";
2244 foreach my $word (@poss) {
2245 print $stdout $word . "\x0D\x0A";
2246 }
2247 }
2248 $self->_repaint_input_line;
2249 }
2250  
2251 sub rl_possible_completions {
2252 my ($self, $key) = @_;
2253  
2254 my @poss = $self->_complete_match;
2255 if (scalar @poss == 0) {
2256 return $self->rl_ding;
2257 }
2258 $self->_complete_list(@poss);
2259 }
2260  
2261 sub rl_complete {
2262 my ($self, $key) = @_;
2263  
2264 my $lookfor = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2265 $lookfor =~ /(\S+)$/;
2266 $lookfor = defined($1) ? $1 : "";
2267 my $point = $self->[SELF_CURSOR_INPUT] - length($lookfor);
2268 my @poss = $self->_complete_match;
2269 if (scalar @poss == 0) {
2270 return $self->rl_ding;
2271 }
2272  
2273 if (scalar @poss == 1) {
2274 substr($self->[SELF_INPUT], $point, $self->[SELF_CURSOR_INPUT]) = $poss[0];
2275 my $rest = substr($self->[SELF_INPUT], $point+length($lookfor));
2276 print $stdout $rest;
2277 _curs_left(length($rest)-length($poss[0]));
2278 $self->[SELF_CURSOR_INPUT] += length($poss[0])-length($lookfor);
2279 $self->[SELF_CURSOR_DISPLAY] += length($poss[0])-length($lookfor);
2280 return 1;
2281 }
2282  
2283 # so at this point, we have multiple possibilities
2284 # find out how much more is in common with the possibilities.
2285 my $max = length($lookfor);
2286 while (1) {
2287 my $letter = undef;
2288 my $ok = 1;
2289 foreach my $p (@poss) {
2290 if ((length $p) < $max) {
2291 $ok = 0;
2292 last;
2293 }
2294 if (!$letter) {
2295 $letter = substr($p, $max, 1);
2296 next;
2297 }
2298 if (substr($p, $max, 1) ne $letter) {
2299 $ok = 0;
2300 last;
2301 }
2302 }
2303 if ($ok) {
2304 $max++;
2305 } else {
2306 last;
2307 }
2308 }
2309 if ($max > length($lookfor)) {
2310 my $partial = substr($poss[0], 0, $max);
2311 substr($self->[SELF_INPUT], $point, $self->[SELF_CURSOR_INPUT]) = $partial;
2312 my $rest = substr($self->[SELF_INPUT], $point+length($lookfor));
2313 print $stdout $rest;
2314 _curs_left(length($rest)-length($partial));
2315 $self->[SELF_CURSOR_INPUT] += length($partial)-length($lookfor);
2316 $self->[SELF_CURSOR_DISPLAY] += length($partial)-length($lookfor);
2317 return $self->rl_ding if @poss == 1;
2318 }
2319  
2320 if ($self->[SELF_LAST] !~ /complete/ && !$self->option('show-all-if-ambiguous')) {
2321 return $self->rl_ding;
2322 }
2323 $self->_complete_list(@poss);
2324 return 0;
2325 }
2326  
2327 sub rl_insert_completions {
2328 my ($self) = @_;
2329 my @poss = $self->_complete_match;
2330 if (scalar @poss == 0) {
2331 return $self->rl_ding;
2332 }
2333 # need to back up the current text
2334 my $lookfor = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2335 $lookfor =~ /(\S+)$/;
2336 $lookfor = $1;
2337 my $point = length($lookfor);
2338 while ($point--) {
2339 $self->rl_backward_delete_char;
2340 }
2341 my $text = join(" ", @poss);
2342 $self->rl_self_insert($text, $text);
2343 }
2344  
2345 sub rl_ding {
2346 my ($self) = @_;
2347 if (!$self->option('bell-style') || $self->option('bell-style') eq 'audible') {
2348 print $stdout $tc_bell;
2349 } elsif ($self->option('bell-style') eq 'visible') {
2350 print $stdout $tc_visual_bell;
2351 }
2352 return 0;
2353 }
2354  
2355 sub rl_redraw_current_line {
2356 my ($self) = @_;
2357 $self->_wipe_input_line;
2358 $self->_repaint_input_line;
2359 }
2360  
2361 sub rl_poe_wheel_debug {
2362 my ($self, $key) = @_;
2363 my $left = _display_width(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]));
2364 my $sp = $self->[SELF_PROMPT];
2365 $sp =~ s{\\[\[\]]}{}g;
2366 print( $stdout
2367 "\x0D\x0A",
2368 "ID=$self->[SELF_UNIQUE_ID] ",
2369 "cursor_input($self->[SELF_CURSOR_INPUT]) ",
2370 "cursor_display($self->[SELF_CURSOR_DISPLAY]) ",
2371 "term_columns($trk_cols)\x0D\x0A",
2372 $sp, _normalize($self->[SELF_INPUT])
2373 );
2374 _curs_left($left) if $left;
2375 }
2376  
2377 sub rl_vi_movement_mode {
2378 my ($self) = @_;
2379 $self->rl_set_keymap('vi');
2380 $self->rl_backward_char if ($self->[SELF_INPUT]);
2381 }
2382  
2383 sub rl_vi_append_mode {
2384 my ($self) = @_;
2385 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
2386 # we can't just call forward-char, coz we don't want bell to ring.
2387 my $normal = _normalize(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1));
2388 print $stdout $normal;
2389 $self->[SELF_CURSOR_INPUT]++;
2390 $self->[SELF_CURSOR_DISPLAY] += length($normal);
2391 }
2392 $self->rl_set_keymap('vi-insert');
2393 }
2394  
2395 sub rl_vi_append_eol {
2396 my ($self) = @_;
2397 $self->rl_end_of_line;
2398 $self->rl_vi_append_mode;
2399 }
2400  
2401 sub rl_vi_insertion_mode {
2402 my ($self) = @_;
2403 $self->rl_set_keymap('vi-insert');
2404 }
2405  
2406 sub rl_vi_insert_beg {
2407 my ($self) = @_;
2408 $self->rl_beginning_of_line;
2409 $self->rl_vi_insertion_mode;
2410 }
2411  
2412 sub rl_vi_editing_mode {
2413 my ($self) = @_;
2414 $self->rl_set_keymap('vi');
2415 }
2416  
2417 sub rl_emacs_editing_mode {
2418 my ($self) = @_;
2419 $self->rl_set_keymap('emacs');
2420 }
2421  
2422 sub rl_vi_eof_maybe {
2423 my ($self, $key) = @_;
2424 if (length $self->[SELF_INPUT] == 0) {
2425 print $stdout uc($key), "\x0D\x0A";
2426 $poe_kernel->select_read($stdin);
2427 if ($self->[SELF_HAS_TIMER]) {
2428 $poe_kernel->delay( $self->[SELF_STATE_IDLE] );
2429 $self->[SELF_HAS_TIMER] = 0;
2430 }
2431 $poe_kernel->yield(
2432 $self->[SELF_EVENT_INPUT],
2433 undef, "eot", $self->[SELF_UNIQUE_ID]
2434 );
2435 $self->[SELF_READING_LINE] = 0;
2436 $self->[SELF_HIST_INDEX] = @{$self->[SELF_HIST_LIST]};
2437  
2438 $self->_flush_output_buffer;
2439 return 0;
2440 } else {
2441 return $self->rl_ding;
2442 }
2443 }
2444  
2445 sub rl_vi_change_case {
2446 my ($self) = @_;
2447 my $char = substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1);
2448 if ($char lt 'a') {
2449 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1) = lc($char);
2450 } else {
2451 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1) = uc($char);
2452 }
2453 $self->rl_forward_char;
2454 }
2455  
2456 sub rl_vi_prev_word {
2457 &rl_backward_word;
2458 }
2459  
2460 sub rl_vi_next_word {
2461 my ($self, $key) = @_;
2462 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*\S+\s)/) {
2463 $self->[SELF_CURSOR_INPUT] += length($1);
2464 my $right = _display_width($1);
2465 print _normalize($1);
2466 $self->[SELF_CURSOR_DISPLAY] += $right;
2467 } else {
2468 return $self->rl_ding;
2469 }
2470 }
2471  
2472 sub rl_vi_end_word {
2473 my ($self, $key) = @_;
2474 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
2475 $self->rl_forward_char;
2476 if (substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*\S+)/) {
2477 $self->[SELF_CURSOR_INPUT] += length($1)-1;
2478 my $right = _display_width($1);
2479 print _normalize($1);
2480 $self->[SELF_CURSOR_DISPLAY] += $right-1;
2481 _curs_left(1);
2482 }
2483 } else {
2484 return $self->rl_ding;
2485 }
2486 }
2487  
2488 sub rl_vi_column {
2489 my ($self) = @_;
2490 $self->[SELF_COUNT] ||= 0;
2491 $self->rl_beginning_of_line;
2492 while ($self->[SELF_COUNT]--) {
2493 $self->rl_forward_char;
2494 }
2495 $self->[SELF_COUNT] = 0;
2496 }
2497  
2498 sub rl_vi_match {
2499 my ($self) = @_;
2500 return $self->rl_ding unless $self->[SELF_INPUT];
2501 # what paren are we after? look forwards down the line for the closest
2502 my $pos = $self->[SELF_CURSOR_INPUT];
2503 my $where = substr($self->[SELF_INPUT], $pos);
2504 my ($adrift) = ($where =~ m/([^\(\)\{\}\[\]]*)/);
2505 my $paren = substr($where, length($adrift), 1);
2506 $pos += length($adrift);
2507  
2508 return $self->rl_ding unless $paren;
2509 my $what_to_do = {
2510 '(' => [ ')', 1 ],
2511 '{' => [ '}', 1 ],
2512 '[' => [ ']', 1 ],
2513 ')' => [ '(', -1 ],
2514 '}' => [ '{', -1 ],
2515 ']' => [ '[', -1 ],
2516 }->{$paren};
2517 my($opp,$dir) = @{$what_to_do};
2518 my $level = 1;
2519 while ($level) {
2520 if ($dir > 0) {
2521 return $self->rl_ding if ($pos == length($self->[SELF_INPUT]));
2522 $pos++;
2523 } else {
2524 return $self->rl_ding unless $pos;
2525 $pos--;
2526 }
2527 my $c = substr($self->[SELF_INPUT], $pos, 1);
2528 if ($c eq $opp) {
2529 $level--;
2530 } elsif ($c eq $paren) {
2531 $level++
2532 }
2533 }
2534 $self->[SELF_COUNT] = $pos;
2535 $self->rl_vi_column;
2536 return 1;
2537 }
2538  
2539 sub rl_vi_first_print {
2540 my ($self) = @_;
2541 $self->rl_beginning_of_line;
2542 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)/;
2543 if (length($1)) {
2544 $self->[SELF_CURSOR_INPUT] += length($1);
2545 my $right = _display_width($1);
2546 print _normalize($1);
2547 $self->[SELF_CURSOR_DISPLAY] += $right;
2548 }
2549 }
2550  
2551 sub rl_vi_delete {
2552 my ($self) = @_;
2553 if ($self->[SELF_CURSOR_INPUT] < length($self->[SELF_INPUT])) {
2554 $self->_delete_chars($self->[SELF_CURSOR_INPUT], 1);
2555 if ($self->[SELF_INPUT] && $self->[SELF_CURSOR_INPUT] >= length($self->[SELF_INPUT])) {
2556 $self->[SELF_CURSOR_INPUT]--;
2557 $self->[SELF_CURSOR_DISPLAY]--;
2558 _curs_left(1);
2559 }
2560 } else {
2561 return $self->rl_ding;
2562 }
2563 }
2564  
2565 sub rl_vi_put {
2566 my ($self, $key) = @_;
2567 my $pos = scalar @{$self->[SELF_KILL_RING]};
2568 return $self->rl_ding unless ($pos);
2569 $pos--;
2570 if ($self->[SELF_INPUT] && $key eq 'p') {
2571 my $normal = _normalize(substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], 1));
2572 print $stdout $normal;
2573 $self->[SELF_CURSOR_INPUT]++;
2574 $self->[SELF_CURSOR_DISPLAY] += length($normal);
2575 }
2576 $self->rl_self_insert($self->[SELF_KILL_RING]->[$pos], $self->[SELF_KILL_RING]->[$pos]);
2577 if ($self->[SELF_CURSOR_INPUT] >= length($self->[SELF_INPUT])) {
2578 $self->[SELF_CURSOR_INPUT]--;
2579 $self->[SELF_CURSOR_DISPLAY]--;
2580 _curs_left(1);
2581 }
2582 }
2583  
2584 sub rl_vi_yank_arg {
2585 my ($self) = @_;
2586 $self->rl_vi_append_mode;
2587 if ($self->rl_yank_last_arg) {
2588 $self->rl_set_keymap('vi-insert');
2589 } else {
2590 $self->rl_set_keymap('vi-command');
2591 }
2592 }
2593  
2594 sub rl_vi_end_spec {
2595 my ($self) = @_;
2596 $self->[SELF_PENDING] = undef;
2597 $self->rl_ding;
2598 $self->rl_set_keymap('vi');
2599 }
2600  
2601 sub rl_vi_spec_end_of_line {
2602 my ($self) = @_;
2603 $self->rl_set_keymap('vi');
2604 $self->_vi_apply_spec($self->[SELF_CURSOR_INPUT], length($self->[SELF_INPUT]) - $self->[SELF_CURSOR_INPUT]);
2605 }
2606  
2607 sub rl_vi_spec_beginning_of_line {
2608 my ($self) = @_;
2609 $self->rl_set_keymap('vi');
2610 $self->_vi_apply_spec(0, $self->[SELF_CURSOR_INPUT]);
2611 }
2612  
2613 sub rl_vi_spec_first_print {
2614 my ($self) = @_;
2615 substr($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT]) =~ /^(\s*)/;
2616 my $len = length($1) || 0;
2617 my $from = $self->[SELF_CURSOR_INPUT];
2618 if ($from > $len) {
2619 my $tmp = $from;
2620 $from = $len;
2621 $len = $tmp - $from;
2622 }
2623 $self->_vi_apply_spec($from, $len);
2624 }
2625  
2626  
2627 sub rl_vi_spec_word {
2628 my ($self) = @_;
2629  
2630 my $from = $self->[SELF_CURSOR_INPUT];
2631 my $len = length($self->[SELF_INPUT]) - $from + 1;
2632 if (substr($self->[SELF_INPUT], $from) =~ /^(\s*\S+\s)/) {
2633 my $word = $1;
2634 $len = length($word);
2635 }
2636 $self->rl_set_keymap('vi');
2637 $self->_vi_apply_spec($from, $len);
2638 }
2639  
2640 sub rl_character_search {
2641 my ($self) = @_;
2642 $self->[SELF_PENDING_FN] = sub {
2643 my ($s, $key) = @_;
2644 return $s->rl_ding unless substr($s->[SELF_INPUT], $s->[SELF_CURSOR_INPUT]) =~ /(.*)$key/;
2645 $s->[SELF_COUNT] = $s->[SELF_INPUT] + length($1);
2646 $s->vi_column;
2647 };
2648 }
2649  
2650 sub rl_character_search_backward {
2651 my ($self) = @_;
2652 $self->[SELF_PENDING_FN] = sub {
2653 my ($s, $key) = @_;
2654 return $s->rl_ding unless substr($s->[SELF_INPUT], 0, $s->[SELF_CURSOR_INPUT]) =~ /$key([^$key])*$/;
2655 $s->[SELF_COUNT] = $s->[SELF_INPUT] - length($1);
2656 $s->vi_column;
2657 };
2658 }
2659  
2660 sub rl_vi_spec_forward_char {
2661 my ($self) = @_;
2662 $self->[SELF_PENDING_FN] = sub {
2663 my ($s, $key) = @_;
2664 return $s->rl_ding unless substr($s->[SELF_INPUT], $s->[SELF_CURSOR_INPUT]) =~ /(.*)$key/;
2665 $s->_vi_apply_spec($s->[SELF_CURSOR_INPUT], length($1));
2666 };
2667 }
2668  
2669 sub rl_vi_spec_mark {
2670 my ($self) = @_;
2671  
2672 $self->[SELF_PENDING_FN] = sub {
2673 my ($s, $key) = @_;
2674 return $s->rl_ding unless exists $s->[SELF_MARKLIST]->{$key};
2675 my $pos = $s->[SELF_CURSOR_INPUT];
2676 my $len = $s->[SELF_MARKLIST]->{$key} - $s->[SELF_CURSOR_INPUT];
2677 if ($len < 0) {
2678 $pos += $len;
2679 $len = -$len;
2680 }
2681 $s->_vi_apply_spec($pos, $len);
2682 };
2683 }
2684  
2685 sub _vi_apply_spec {
2686 my ($self, $from, $howmany) = @_;
2687 $self->[SELF_PENDING]->($self, $from, $howmany);
2688 $self->[SELF_PENDING] = undef if ($self->[SELF_COUNT] <= 1);
2689 }
2690  
2691 sub rl_vi_yank_to {
2692 my ($self, $key) = @_;
2693 $self->[SELF_PENDING] = sub {
2694 my ($s, $from, $howmany) = @_;
2695 push(@{$s->[SELF_KILL_RING]}, substr($s->[SELF_INPUT], $from, $howmany));
2696 };
2697 if ($key eq 'Y') {
2698 $self->rl_vi_spec_end_of_line;
2699 } else {
2700 $self->rl_set_keymap('vi-specification');
2701 }
2702 }
2703  
2704 sub rl_vi_delete_to {
2705 my ($self, $key) = @_;
2706 $self->[SELF_PENDING] = sub {
2707 my ($s, $from, $howmany) = @_;
2708 $s->_delete_chars($from, $howmany);
2709 if ($s->[SELF_INPUT] && $s->[SELF_CURSOR_INPUT] >= length($s->[SELF_INPUT])) {
2710 $s->[SELF_CURSOR_INPUT]--;
2711 $s->[SELF_CURSOR_DISPLAY]--;
2712 _curs_left(1);
2713 }
2714 $s->rl_set_keymap('vi');
2715 };
2716 if ($key eq 'D') {
2717 $self->rl_vi_spec_end_of_line;
2718 } else {
2719 $self->rl_set_keymap('vi-specification');
2720 }
2721 }
2722  
2723 sub rl_vi_change_to {
2724 my ($self, $key) = @_;
2725 $self->[SELF_PENDING] = sub {
2726 my ($s, $from, $howmany) = @_;
2727 $s->_delete_chars($from, $howmany);
2728 $s->rl_set_keymap('vi-insert');
2729 };
2730 if ($key eq 'C') {
2731 $self->rl_vi_spec_end_of_line;
2732 } else {
2733 $self->rl_set_keymap('vi-specification');
2734 }
2735 }
2736  
2737 sub rl_vi_arg_digit {
2738 my ($self, $key) = @_;
2739 if ($key == '0' && !$self->[SELF_COUNT]) {
2740 $self->rl_beginning_of_line;
2741 } else {
2742 $self->[SELF_COUNT] .= $key;
2743 }
2744 }
2745  
2746 sub rl_vi_tilde_expand {
2747 my ($self) = @_;
2748 if ($self->rl_tilde_expand) {
2749 $self->rl_vi_append_mode;
2750 }
2751 }
2752  
2753 sub rl_vi_complete {
2754 my ($self) = @_;
2755 if ($self->rl_complete) {
2756 $self->rl_set_keymap('vi-insert');
2757 }
2758 }
2759  
2760 sub rl_vi_goto_mark {
2761 my ($self) = @_;
2762 $self->[SELF_PENDING_FN] = sub {
2763 my ($s, $key) = @_;
2764 return $s->rl_ding unless exists $s->[SELF_MARKLIST]->{$key};
2765 $s->[SELF_COUNT] = $s->[SELF_MARKLIST]->{$key};
2766 $s->rl_vi_column;
2767 };
2768 }
2769  
2770 sub rl_vi_set_mark {
2771 my ($self) = @_;
2772 $self->[SELF_PENDING_FN] = sub {
2773 my ($s, $key) = @_;
2774 return $s->rl_ding unless ($key >= 'a' && $key <= 'z');
2775 $s->[SELF_MARKLIST]->{$key} = $s->[SELF_CURSOR_INPUT];
2776 };
2777 }
2778  
2779 sub rl_search_abort {
2780 my ($self) = @_;
2781 $self->_wipe_input_line;
2782 $self->[SELF_PROMPT] = $self->[SELF_PREV_PROMPT];
2783 $self->_repaint_input_line;
2784 $self->[SELF_KEYMAP] = $self->[SELF_SEARCH_MAP];
2785 $self->[SELF_SEARCH_MAP] = undef;
2786 $self->[SELF_SEARCH] = undef;
2787 }
2788  
2789 sub rl_search_finish {
2790 my ($self, $key, $raw) = @_;
2791 $self->_wipe_input_line;
2792 $self->[SELF_PROMPT] = $self->[SELF_PREV_PROMPT];
2793 $self->_repaint_input_line;
2794 $self->[SELF_KEYMAP] = $self->[SELF_SEARCH_MAP];
2795 $self->[SELF_SEARCH_MAP] = undef;
2796 $self->[SELF_SEARCH] = undef;
2797 $self->_apply_key($key, $raw);
2798 }
2799  
2800 sub rl_search_key {
2801 my ($self, $key) = @_;
2802 $self->[SELF_SEARCH] .= $key;
2803 $self->_search(1);
2804 }
2805  
2806 sub rl_vi_search_key {
2807 my ($self, $key) = @_;
2808 $self->rl_self_insert($key, $key);
2809 }
2810  
2811 sub rl_vi_search {
2812 my ($self, $key) = @_;
2813 $self->_wipe_input_line;
2814 $self->[SELF_SEARCH_MAP] = $self->[SELF_KEYMAP];
2815 if ($key eq '/' && $self->[SELF_HIST_INDEX] < scalar @{$self->[SELF_HIST_LIST]}) {
2816 $self->[SELF_SEARCH_DIR] = -1;
2817 } else {
2818 $self->[SELF_SEARCH_DIR] = +1;
2819 }
2820 $self->[SELF_SEARCH_KEY] = $key;
2821 $self->[SELF_INPUT] = $key;
2822 $self->[SELF_CURSOR_INPUT] = 1;
2823 $self->[SELF_CURSOR_DISPLAY] = 1;
2824 $self->_repaint_input_line;
2825 $self->rl_set_keymap('vi-search');
2826 }
2827  
2828 sub rl_vi_search_accept {
2829 my ($self) = @_;
2830 $self->_wipe_input_line;
2831 $self->[SELF_CURSOR_INPUT] = 0;
2832 $self->[SELF_CURSOR_DISPLAY] = 0;
2833 $self->[SELF_INPUT] =~ s{^[/?]}{};
2834 $self->[SELF_SEARCH] = $self->[SELF_INPUT] if $self->[SELF_INPUT];
2835 $self->_search(0);
2836 $self->[SELF_KEYMAP] = $self->[SELF_SEARCH_MAP];
2837 $self->[SELF_SEARCH_MAP] = undef;
2838 }
2839  
2840 sub rl_vi_search_again {
2841 my ($self, $key) = @_;
2842 return $self->rl_ding unless $self->[SELF_SEARCH];
2843 $self->[SELF_HIST_INDEX] += $self->[SELF_SEARCH_DIR];
2844 if ($self->[SELF_HIST_INDEX] < 0) {
2845 $self->[SELF_HIST_INDEX] = 0;
2846 return $self->rl_ding;
2847 } elsif ($self->[SELF_HIST_INDEX] >= scalar @{$self->[SELF_HIST_LIST]}) {
2848 $self->[SELF_HIST_INDEX] = (scalar @{$self->[SELF_HIST_LIST]}) - 1;
2849 return $self->rl_ding;
2850 }
2851 $self->_wipe_input_line;
2852 $self->_search(0);
2853 }
2854  
2855 sub rl_isearch_again {
2856 my ($self, $key) = @_;
2857 if ($key ne $self->[SELF_SEARCH_KEY]) {
2858 $self->[SELF_SEARCH_KEY] = $key;
2859 $self->[SELF_SEARCH_DIR] = -$self->[SELF_SEARCH_DIR];
2860 }
2861 $self->[SELF_HIST_INDEX] += $self->[SELF_SEARCH_DIR];
2862 if ($self->[SELF_HIST_INDEX] < 0) {
2863 $self->[SELF_HIST_INDEX] = 0;
2864 return $self->rl_ding;
2865 } elsif ($self->[SELF_HIST_INDEX] >= scalar @{$self->[SELF_HIST_LIST]}) {
2866 $self->[SELF_HIST_INDEX] = (scalar @{$self->[SELF_HIST_LIST]}) - 1;
2867 return $self->rl_ding;
2868 }
2869 $self->_search(1);
2870 }
2871  
2872 sub rl_non_incremental_forward_search_history {
2873 my ($self) = @_;
2874 $self->_wipe_input_line;
2875 $self->[SELF_CURSOR_INPUT] = 0;
2876 $self->[SELF_CURSOR_DISPLAY] = 0;
2877 $self->[SELF_SEARCH_DIR] = +1;
2878 $self->[SELF_SEARCH] = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2879 $self->_search(0);
2880 }
2881  
2882 sub rl_non_incremental_reverse_search_history {
2883 my ($self) = @_;
2884 $self->[SELF_HIST_INDEX] --;
2885 if ($self->[SELF_HIST_INDEX] < 0) {
2886 $self->[SELF_HIST_INDEX] = 0;
2887 return $self->rl_ding;
2888 }
2889 $self->_wipe_input_line;
2890 $self->[SELF_CURSOR_INPUT] = 0;
2891 $self->[SELF_CURSOR_DISPLAY] = 0;
2892 $self->[SELF_SEARCH_DIR] = -1;
2893 $self->[SELF_SEARCH] = substr($self->[SELF_INPUT], 0, $self->[SELF_CURSOR_INPUT]);
2894 $self->_search(0);
2895 }
2896  
2897 sub rl_undo {
2898 my ($self) = @_;
2899 $self->rl_ding unless scalar @{$self->[SELF_UNDO]};
2900 my $tuple = pop @{$self->[SELF_UNDO]};
2901 ($self->[SELF_INPUT], $self->[SELF_CURSOR_INPUT], $self->[SELF_CURSOR_DISPLAY]) = @$tuple;
2902 }
2903  
2904 sub rl_vi_redo {
2905 my ($self, $key) = @_;
2906 return $self->rl_ding unless $self->[SELF_LAST];
2907 my $fn = $self->[SELF_LAST];
2908 $self->$fn();
2909 }
2910  
2911 sub rl_vi_char_search {
2912 my ($self, $key) = @_;
2913 $self->[SELF_PENDING_FN] = sub {
2914 my ($s,$k,$rk) = @_;
2915 $rk = "\\" . $rk if ($rk !~ /\w/);
2916 return $s->rl_ding unless substr($s->[SELF_INPUT], $s->[SELF_CURSOR_INPUT]) =~ /([^$rk]*)$rk/;
2917 $s->[SELF_COUNT] = $s->[SELF_CURSOR_INPUT] + length($1);
2918 $s->rl_vi_column;
2919 };
2920 }
2921  
2922 sub rl_vi_change_char {
2923 my ($self, $key) = @_;
2924 $self->[SELF_PENDING_FN] = sub {
2925 my ($s,$k,$rk) = @_;
2926 $s->rl_delete_char;
2927 $s->rl_self_insert($k,$rk);
2928 $s->rl_backward_char;
2929 };
2930 }
2931  
2932 sub rl_vi_subst {
2933 my ($self, $key) = @_;
2934 if ($key eq 's') {
2935 $self->rl_vi_delete;
2936 } else {
2937 $self->rl_beginning_of_line;
2938 $self->rl_kill_line;
2939 }
2940 $self->rl_vi_insertion_mode;
2941 }
2942  
2943 # ============================================================
2944 # THE KEYMAP CLASS ITSELF
2945 # ============================================================
2946  
2947 package POE::Wheel::ReadLine::Keymap;
2948  
2949 my %english_to_termcap = (
2950 'up' => 'ku',
2951 'down' => 'kd',
2952 'left' => 'kl',
2953 'right' => 'kr',
2954 'insert' => 'kI',
2955 'ins' => 'kI',
2956 'delete' => 'kD',
2957 'del' => 'kD',
2958 'home' => 'kh',
2959 'end' => 'kH',
2960 'backspace' => 'kb',
2961 'bs' => 'kb',
2962 );
2963  
2964 my %english_to_key = (
2965 'space' => " ",
2966 'esc' => "\e",
2967 'escape' => "\e",
2968 'tab' => "\cI",
2969 'ret' => "\cJ",
2970 'return' => "\cJ",
2971 'newline' => "\cM",
2972 'lfd' => "\cL",
2973 'rubout' => chr(127),
2974 );
2975  
2976 sub init {
2977 my ($proto, %opts) = @_;
2978 my $class = ref($proto) || $proto;
2979  
2980 my $default = delete $opts{default} or die("no default specified for keymap");
2981 my $name = delete $opts{name} or die("no name specified for keymap");
2982 my $termcap = delete $opts{termcap} or die("no termcap specified for keymap");
2983  
2984 my $self = {
2985 name => $name,
2986 default => $default,
2987 binding => {},
2988 prefix => {},
2989 termcap => $termcap,
2990 };
2991  
2992 return bless $self, $class;
2993 }
2994  
2995 sub decode {
2996 my ($self, $seq) = @_;
2997 if (exists $english_to_termcap{lc($seq)}) {
2998 my $key = $self->{termcap}->Tputs($english_to_termcap{lc($seq)}, 1);
2999 $seq = defined($key) ? $key : "";
3000 } elsif (exists $english_to_key{lc($seq)}) {
3001 $seq = $english_to_key{lc($seq)};
3002 }
3003  
3004 return $seq;
3005 }
3006  
3007 sub control {
3008 my $c = shift;
3009 return chr(0x7F) if $c eq "?";
3010 return chr(ord(uc($c))-64);
3011 }
3012  
3013 sub meta { return "\x1B" . $_[0] };
3014 sub bind_key {
3015 my ($self, $inseq, $fn) = @_;
3016 my $seq = $inseq;
3017 my $macro = undef;
3018 if (!ref $fn) {
3019 if ($fn =~ /^["'](.*)['"]$/) {
3020 # A macro
3021 $macro = $1;
3022 $fn = 'insert-macro';
3023 } else {
3024 if (!exists $POE::Wheel::ReadLine::defuns->{$fn}) {
3025 print "ignoring $inseq, since function '$fn' is not known\r\n";
3026 next;
3027 }
3028 }
3029 }
3030  
3031 # Need to parse key sequence into a trivial lookup form.
3032 if ($seq =~ s{^"(.*)"$}{$1}) {
3033 $seq =~ s{\\C-(.)}{control($1)}ge;
3034 $seq =~ s{\\M-(.)}{meta($1)}ge;
3035 $seq =~ s{\\e}{\x1B}g;
3036 $seq =~ s{\\\\}{\\}g;
3037 $seq =~ s{\\"}{"}g;
3038 $seq =~ s{\\'}{'}g;
3039 } else {
3040 my $orig = $seq;
3041 do {
3042 $orig = $seq;
3043 $seq =~ s{(\w*)$}{$self->decode($1)}ge;
3044 # horrible regex, coz we need to work backwards, to allow
3045 # for things like C-M-r, or C-xC-x
3046 $seq =~ s{C(ontrol)?-(.)([^-]*)$}{control($2).$3}ge;
3047 $seq =~ s{M(eta)?-(.)([^-]*)$}{meta($2).$3}ge;
3048 } while ($seq ne $orig);
3049 }
3050  
3051 $self->{binding}->{$seq} = $fn if length $seq;
3052 $self->{macros}->{$seq} = $macro if $macro;
3053 #print "bound $inseq (" . POE::Wheel::ReadLine::_normalize($seq) . ") to $fn in map $self->{name}\r\n";
3054  
3055 if (length($seq) > 1) {
3056 # XXX: Should store rawkey prefixes, to avoid the ^ problem.
3057 # requires converting seq into raw, then applying normalize
3058 # later on for binding. May not need last step if we keep
3059 # everything as raw.
3060 # Some keystrokes generate multi-byte sequences. Record the prefixes
3061 # for multi-byte sequences so the keystroke builder knows it's in the
3062 # middle of something.
3063 while (length($seq) > 1) {
3064 chop $seq;
3065 $self->{prefix}->{$seq}++;
3066 }
3067 }
3068 }
3069  
3070 1;
3071  
3072 __END__