-
Notifications
You must be signed in to change notification settings - Fork 26
/
ReadLine.pm
3617 lines (3130 loc) · 103 KB
/
ReadLine.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package POE::Wheel::ReadLine;
use warnings;
use strict;
BEGIN { eval { require bytes } and bytes->import; }
use vars qw($VERSION);
$VERSION = '1.289'; # NOTE - Should be #.### (three decimal places)
use Carp qw( croak carp );
use Symbol qw(gensym);
use POE qw( Wheel );
use base qw(POE::Wheel);
use POSIX ();
if ($^O eq "MSWin32") {
die "$^O cannot run " . __PACKAGE__;
}
# After a massive hackathon on Cygwin/perl/windows/POE it was determined that
# having TERM='dumb' is worthless and VERY problematic to work-around...
# Actually, the problem lies deep in Term::Cap's internals - it blows up
# when we try to do "$termcap->Trequire( qw( cl ku kd kl kr) )" in new()
# eval{} will not catch the croak() in the sub
# Cygwin v1.7.1-1 on Windows Server 2008 64bit with Perl v5.10.1 with Term::Cap v1.12 with POE v1.287
# For detailed info, please consult RT#55365
# sorry about the defined $ENV{TERM} check, it was needed to make sure we spew no warnings...
if ($^O eq 'cygwin' and defined $ENV{TERM} and $ENV{TERM} eq 'dumb') {
die "$^O with TERM='$ENV{TERM}' cannot run " . __PACKAGE__;
}
# Things we'll need to interact with the terminal.
use Term::Cap ();
use Term::ReadKey qw( ReadKey ReadMode GetTerminalSize );
my $initialised = 0;
my $termcap; # Termcap entry.
my $tc_bell; # How to ring the terminal.
my $tc_visual_bell; # How to ring the terminal.
my $tc_has_ce; # Termcap can clear to end of line.
# Private STDIN and STDOUT.
my $stdin = gensym();
open($stdin, "<&STDIN") or die "Can't open private STDIN: $!";
my $stdout = gensym;
open($stdout, ">&STDOUT") or die "Can't open private STDOUT: $!";
# Offsets into $self.
sub SELF_INPUT () { 0 }
sub SELF_CURSOR_INPUT () { 1 }
sub SELF_EVENT_INPUT () { 2 }
sub SELF_READING_LINE () { 3 }
sub SELF_STATE_READ () { 4 }
sub SELF_PROMPT () { 5 }
sub SELF_HIST_LIST () { 6 }
sub SELF_HIST_INDEX () { 7 }
sub SELF_INPUT_HOLD () { 8 }
sub SELF_KEY_BUILD () { 9 }
sub SELF_INSERT_MODE () { 10 }
sub SELF_PUT_MODE () { 11 }
sub SELF_PUT_BUFFER () { 12 }
sub SELF_IDLE_TIME () { 13 }
sub SELF_STATE_IDLE () { 14 }
sub SELF_HAS_TIMER () { 15 }
sub SELF_CURSOR_DISPLAY () { 16 }
sub SELF_UNIQUE_ID () { 17 }
sub SELF_KEYMAP () { 18 }
sub SELF_OPTIONS () { 19 }
sub SELF_APP () { 20 }
sub SELF_ALL_KEYMAPS () { 21 }
sub SELF_PENDING () { 22 }
sub SELF_COUNT () { 23 }
sub SELF_MARK () { 24 }
sub SELF_MARKLIST () { 25 }
sub SELF_KILL_RING () { 26 }
sub SELF_LAST () { 27 }
sub SELF_PENDING_FN () { 28 }
sub SELF_SOURCE () { 29 }
sub SELF_SEARCH () { 30 }
sub SELF_SEARCH_PROMPT () { 31 }
sub SELF_SEARCH_MAP () { 32 }
sub SELF_PREV_PROMPT () { 33 }
sub SELF_SEARCH_DIR () { 34 }
sub SELF_SEARCH_KEY () { 35 }
sub SELF_UNDO () { 36 }
sub CRIMSON_SCOPE_HACK ($) { 0 }
#------------------------------------------------------------------------------
# Build a hash of input characters and their "normalized" display
# versions. ISO Latin-1 characters (8th bit set "ASCII") are
# mishandled. European users, please forgive me. If there's a good
# way to handle this-- perhaps this is an interesting use for
# Unicode-- please let me know.
my (%normalized_character, @normalized_extra_width);
#------------------------------------------------------------------------------
# Gather information about the user's terminal. This just keeps
# getting uglier.
my $ospeed = undef;
my $termios = undef;
my $term = undef;
my $tc_left = undef;
my $trk_cols = undef;
my $trk_rows = undef;
sub _curs_left {
my $amount = shift;
if ($tc_left eq "LE") {
$termcap->Tgoto($tc_left, 1, $amount, $stdout);
return;
}
for (1..$amount) {
$termcap->Tgoto($tc_left, 1, 1, $stdout);
}
}
our $defuns = {
"abort" => \&rl_abort,
"accept-line" => \&rl_accept_line,
"backward-char" => \&rl_backward_char,
"backward-delete-char" => \&rl_backward_delete_char,
"backward-kill-line" => \&rl_unix_line_discard, # reuse emacs
"backward-kill-word" => \&rl_backward_kill_word,
"backward-word" => \&rl_backward_word,
"beginning-of-history" => \&rl_beginning_of_history,
"beginning-of-line" => \&rl_beginning_of_line,
"capitalize-word" => \&rl_capitalize_word,
"character-search" => \&rl_character_search,
"character-search-backward" => \&rl_character_search_backward,
"clear-screen" => \&rl_clear_screen,
"complete" => \&rl_complete,
"copy-region-as-kill" => \&rl_copy_region_as_kill,
"delete-char" => \&rl_delete_char,
"delete-horizontal-space" => \&rl_delete_horizontal_space,
"digit-argument" => \&rl_digit_argument,
"ding" => \&rl_ding,
"downcase-word" => \&rl_downcase_word,
"dump-key" => \&rl_dump_key,
"dump-macros" => \&rl_dump_macros,
"dump-variables" => \&rl_dump_variables,
"emacs-editing-mode" => \&rl_emacs_editing_mode,
"end-of-history" => \&rl_end_of_history,
"end-of-line" => \&rl_end_of_line,
"forward-char" => \&rl_forward_char,
"forward-search-history" => \&rl_forward_search_history,
"forward-word" => \&rl_forward_word,
"insert-comment" => \&rl_insert_comment,
"insert-completions" => \&rl_insert_completions,
"insert-macro" => \&rl_insert_macro,
"interrupt" => \&rl_interrupt,
"isearch-again" => \&rl_isearch_again,
"kill-line" => \&rl_kill_line,
"kill-region" => \&rl_kill_region,
"kill-whole-line" => \&rl_kill_whole_line,
"kill-word" => \&rl_kill_word,
"next-history" => \&rl_next_history,
"non-incremental-forward-search-history" => \&rl_non_incremental_forward_search_history,
"non-incremental-reverse-search-history" => \&rl_non_incremental_reverse_search_history,
"overwrite-mode" => \&rl_overwrite_mode,
"poe-wheel-debug" => \&rl_poe_wheel_debug,
"possible-completions" => \&rl_possible_completions,
"previous-history" => \&rl_previous_history,
"quoted-insert" => \&rl_quoted_insert,
"re-read-init-file" => \&rl_re_read_init_file,
"redraw-current-line" => \&rl_redraw_current_line,
"reverse-search-history" => \&rl_reverse_search_history,
"revert-line" => \&rl_revert_line,
"search-abort" => \&rl_search_abort,
"search-finish" => \&rl_search_finish,
"search-key" => \&rl_search_key,
"self-insert" => \&rl_self_insert,
"set-keymap" => \&rl_set_keymap,
"set-mark" => \&rl_set_mark,
"tab-insert" => \&rl_ding, # UNIMPLEMENTED
"tilde-expand" => \&rl_tilde_expand,
"transpose-chars" => \&rl_transpose_chars,
"transpose-words" => \&rl_transpose_words,
"undo" => \&rl_undo,
"unix-line-discard" => \&rl_unix_line_discard,
"unix-word-rubout" => \&rl_unix_word_rubout,
"upcase-word" => \&rl_upcase_word,
"vi-append-eol" => \&rl_vi_append_eol,
"vi-append-mode" => \&rl_vi_append_mode,
"vi-arg-digit" => \&rl_vi_arg_digit,
"vi-change-case" => \&rl_vi_change_case,
"vi-change-char" => \&rl_vi_change_char,
"vi-change-to" => \&rl_vi_change_to,
"vi-char-search" => \&rl_vi_char_search,
"vi-column" => \&rl_vi_column,
"vi-complete" => \&rl_vi_cmplete,
"vi-delete" => \&rl_vi_delete,
"vi-delete-to" => \&rl_vi_delete_to,
"vi-editing-mode" => \&rl_vi_editing_mode,
"vi-end-spec" => \&rl_vi_end_spec,
"vi-end-word" => \&rl_vi_end_word,
"vi-eof-maybe" => \&rl_vi_eof_maybe,
"vi-fetch-history" => \&rl_beginning_of_history, # re-use emacs version
"vi-first-print" => \&rl_vi_first_print,
"vi-goto-mark" => \&rl_vi_goto_mark,
"vi-insert-beg" => \&rl_vi_insert_beg,
"vi-insertion-mode" => \&rl_vi_insertion_mode,
"vi-match" => \&rl_vi_match,
"vi-movement-mode" => \&rl_vi_movement_mode,
"vi-next-word" => \&rl_vi_next_word,
"vi-prev-word" => \&rl_vi_prev_word,
"vi-put" => \&rl_vi_put,
"vi-redo" => \&rl_vi_redo,
"vi-replace" => \&rl_vi_replace,
"vi-search" => \&rl_vi_search,
"vi-search-accept" => \&rl_vi_search_accept,
"vi-search-again" => \&rl_vi_search_again,
"vi-search-key" => \&rl_vi_search_key,
"vi-set-mark" => \&rl_vi_set_mark,
"vi-spec-beginning-of-line" => \&rl_vi_spec_beginning_of_line,
"vi-spec-end-of-line" => \&rl_vi_spec_end_of_line,
"vi-spec-first-print" => \&rl_vi_spec_first_print,
"vi-spec-forward-char" => \&rl_vi_spec_forward_char,
"vi-spec-mark" => \&rl_vi_spec_mark,
"vi-spec-word" => \&rl_vi_spec_word,
"vi-subst" => \&rl_vi_subst,
"vi-tilde-expand" => \&rl_vi_tilde_expand,
"vi-undo" => \&rl_undo, # re-use emacs version
"vi-yank-arg" => \&rl_vi_yank_arg,
"vi-yank-to" => \&rl_vi_yank_to,
"yank" => \&rl_yank,
"yank-last-arg" => \&rl_yank_last_arg,
"yank-nth-arg" => \&rl_yank_nth_arg,
"yank-pop" => \&rl_yank_pop,
};
# what functions are for counting
my @fns_counting = (
'rl_vi_arg_digit',
'rl_digit_argument',
'rl_universal-argument',
);
# what functions are purely for movement...
my @fns_movement = (
'rl_beginning_of_line',
'rl_backward_char',
'rl_forward_char',
'rl_backward_word',
'rl_forward_word',
'rl_end_of_line',
'rl_character_search',
'rl_character_search_backward',
'rl_vi_prev_word',
'rl_vi_next_word',
'rl_vi_goto_mark',
'rl_vi_end_word',
'rl_vi_column',
'rl_vi_first_print',
'rl_vi_char_search',
'rl_vi_spec_char_search',
'rl_vi_spec_end_of_line',
'rl_vi_spec_beginning_of_line',
'rl_vi_spec_first_print',
'rl_vi_spec_word',
'rl_vi_spec_mark',
);
# the list of functions that we don't want to record for
# later redo usage in vi mode.
my @fns_anon = (
'rl_vi_redo',
@fns_counting,
@fns_movement,
);
my $defaults_inputrc = <<'INPUTRC';
set comment-begin #
INPUTRC
my $emacs_inputrc = <<'INPUTRC';
C-a: beginning-of-line
C-b: backward-char
C-c: interrupt
C-d: delete-char
C-e: end-of-line
C-f: forward-char
C-g: abort
C-h: backward-delete-char
C-i: complete
C-j: accept-line
C-k: kill-line
C-l: clear-screen
C-m: accept-line
C-n: next-history
C-p: previous-history
C-q: poe-wheel-debug
C-r: reverse-search-history
C-s: forward-search-history
C-t: transpose-chars
C-u: unix-line-discard
C-v: quoted-insert
C-w: unix-word-rubout
C-y: yank
C-]: character-search
C-_: undo
del: backward-delete-char
rubout: backward-delete-char
M-C-g: abort
M-C-h: backward-kill-word
M-C-i: tab-insert
M-C-j: vi-editing-mode
M-C-r: revert-line
M-C-y: yank-nth-arg
M-C-[: complete
M-C-]: character-search-backward
M-space: set-mark
M-#: insert-comment
M-&: tilde-expand
M-*: insert-completions
M--: digit-argument
M-.: yank-last-arg
M-0: digit-argument
M-1: digit-argument
M-2: digit-argument
M-3: digit-argument
M-4: digit-argument
M-5: digit-argument
M-6: digit-argument
M-7: digit-argument
M-8: digit-argument
M-9: digit-argument
M-<: beginning-of-history
M->: end-of-history
M-?: possible-completions
M-b: backward-word
M-c: capitalize-word
M-d: kill-word
M-f: forward-word
M-l: downcase-word
M-n: non-incremental-forward-search-history
M-p: non-incremental-reverse-search-history
M-r: revert-line
M-t: transpose-words
M-u: upcase-word
M-y: yank-pop
M-\: delete-horizontal-space
M-~: tilde-expand
M-del: backward-kill-word
M-_: yank-last-arg
C-xC-r: re-read-init-file
C-xC-g: abort
C-xDel: backward-kill-line
C-xm: dump-macros
C-xv: dump-variables
C-xk: dump-key
home: beginning-of-line
end: end-of-line
ins: overwrite-mode
del: delete-char
left: backward-char
right: forward-char
up: previous-history
down: next-history
bs: backward-delete-char
INPUTRC
my $vi_inputrc = <<'INPUTRC';
# VI uses two keymaps, depending on which mode we're in.
set keymap vi-insert
C-d: vi-eof-maybe
C-h: backward-delete-char
C-i: complete
C-j: accept-line
C-m: accept-line
C-r: reverse-search-history
C-s: forward-search-history
C-t: transpose-chars
C-u: unix-line-discard
C-v: quoted-insert
C-w: unix-word-rubout
C-y: yank
C-[: vi-movement-mode
C-_: undo
C-?: backward-delete-char
set keymap vi-command
C-d: vi-eof-maybe
C-e: emacs-editing-mode
C-g: abort
C-h: backward-char
C-j: accept-line
C-k: kill-line
C-l: clear-screen
C-m: accept-line
C-n: next-history
C-p: previous-history
C-q: quoted-insert
C-r: reverse-search-history
C-s: forward-search-history
C-t: transpose-chars
C-u: unix-line-discard
C-v: quoted-insert
C-w: unix-word-rubout
C-y: yank
C-_: vi-undo
" ": forward-char
"#": insert-comment
"$": end-of-line
"%": vi-match
"&": vi-tilde-expand
"*": vi-complete
"+": next-history
",": vi-char-search
"-": previous-history
".": vi-redo
"/": vi-search
"0": vi-arg-digit
"1": vi-arg-digit
"2": vi-arg-digit
"3": vi-arg-digit
"4": vi-arg-digit
"5": vi-arg-digit
"6": vi-arg-digit
"7": vi-arg-digit
"8": vi-arg-digit
"9": vi-arg-digit
";": vi-char-search
"=": vi-complete
"?": vi-search
A: vi-append-eol
B: vi-prev-word
C: vi-change-to
D: vi-delete-to
E: vi-end-word
F: vi-char-search
G: vi-fetch-history
I: vi-insert-beg
N: vi-search-again
P: vi-put
R: vi-replace
S: vi-subst
T: vi-char-search
U: revert-line
W: vi-next-word
X: backward-delete-char
Y: vi-yank-to
"\": vi-complete
"^": vi-first-print
"_": vi-yank-arg
"`": vi-goto-mark
a: vi-append-mode
b: backward-word
c: vi-change-to
d: vi-delete-to
e: vi-end-word
h: backward-char
i: vi-insertion-mode
j: next-history
k: previous-history
l: forward-char
m: vi-set-mark
n: vi-search-again
p: vi-put
r: vi-change-char
s: vi-subst
t: vi-char-search
w: vi-next-word
x: vi-delete
y: vi-yank-to
"|": vi-column
"~": vi-change-case
set keymap vi-specification
"^": vi-spec-first-print
"`": vi-spec-mark
"$": vi-spec-end-of-line
"0": vi-spec-beginning-of-line
"1": vi-arg-digit
"2": vi-arg-digit
"3": vi-arg-digit
"4": vi-arg-digit
"5": vi-arg-digit
"6": vi-arg-digit
"7": vi-arg-digit
"8": vi-arg-digit
"9": vi-arg-digit
w: vi-spec-word
t: vi-spec-forward-char
INPUTRC
my $search_inputrc = <<'INPUTRC';
set keymap isearch
C-r: isearch-again
C-s: isearch-again
set keymap vi-search
C-j: vi-search-accept
C-m: vi-search-accept
INPUTRC
#------------------------------------------------------------------------------
# Helper functions.
sub _vislength {
return 0 unless $_[0];
my $len = length($_[0]);
while ($_[0] =~ m{(\\\[.*?\\\])}g) {
$len -= length($1);
}
return $len;
}
# Wipe the current input line.
sub _wipe_input_line {
my ($self) = shift;
# Clear the current prompt and input, and home the cursor.
_curs_left( $self->[SELF_CURSOR_DISPLAY] + _vislength($self->[SELF_PROMPT]));
if ( $tc_has_ce ) {
print $stdout $termcap->Tputs( 'ce', 1 );
} else {
my $wlen = _vislength($self->[SELF_PROMPT]) + _display_width($self->[SELF_INPUT]);
print $stdout ( ' ' x $wlen);
_curs_left($wlen);
}
}
# Helper to flush any buffered output.
sub _flush_output_buffer {
my ($self) = shift;
# Flush anything buffered.
if ( @{ $self->[SELF_PUT_BUFFER] } ) {
print $stdout @{ $self->[SELF_PUT_BUFFER] };
# Do not change the interior arrayref, or the event handlers will
# become confused.
@{ $self->[SELF_PUT_BUFFER] } = ();
}
}
# Set up the prompt and input line like nothing happened.
sub _repaint_input_line {
my ($self) = shift;
my $sp = $self->[SELF_PROMPT];
$sp =~ s{\\[\[\]]}{}g;
print $stdout $sp, _normalize($self->[SELF_INPUT]);
if ( $self->[SELF_CURSOR_INPUT] != length( $self->[SELF_INPUT]) ) {
_curs_left(
_display_width($self->[SELF_INPUT]) - $self->[SELF_CURSOR_DISPLAY]
);
}
}
sub _clear_to_end {
my ($self) = @_;
if (length $self->[SELF_INPUT]) {
if ($tc_has_ce) {
print $stdout $termcap->Tputs( 'ce', 1 );
} else {
my $display_width = _display_width($self->[SELF_INPUT]);
print $stdout ' ' x $display_width;
_curs_left($display_width);
}
}
}
sub _delete_chars {
my ($self, $from, $howmany) = @_;
# sanitize input
if ($howmany < 0) {
$from -= $howmany;
$howmany = -$howmany;
if ($from < 0) {
$howmany -= $from;
$from = 0;
}
}
my $old = substr($self->[SELF_INPUT], $from, $howmany);
my $killed_width = _display_width($old);
substr($self->[SELF_INPUT], $from, $howmany) = '';
if ($self->[SELF_CURSOR_INPUT] > $from) {
my $newdisp = length(_normalize(substr($self->[SELF_INPUT], 0, $from)));
_curs_left($self->[SELF_CURSOR_DISPLAY] - $newdisp);
$self->[SELF_CURSOR_INPUT] = $from;
$self->[SELF_CURSOR_DISPLAY] = $newdisp;
}
my $normal_remaining = _normalize(substr($self->[SELF_INPUT], $from));
print $stdout $normal_remaining;
my $normal_remaining_length = length($normal_remaining);
if ($tc_has_ce) {
print $stdout $termcap->Tputs( 'ce', 1 );
} else {
print $stdout ' ' x $killed_width;
$normal_remaining_length += $killed_width;
}
_curs_left($normal_remaining_length)
if $normal_remaining_length;
return $old;
}
sub _search {
my ($self, $rebuild) = @_;
if ($rebuild) {
$self->_wipe_input_line;
$self->_build_search_prompt;
}
# find in history....
my $found = 0;
for (
my $i = $self->[SELF_HIST_INDEX];
$i < scalar @{$self->[SELF_HIST_LIST]} && $i >= 0;
$i += $self->[SELF_SEARCH_DIR]
) {
next unless $self->[SELF_HIST_LIST]->[$i] =~ /$self->[SELF_SEARCH]/;
$self->[SELF_HIST_INDEX] = $i;
$self->[SELF_INPUT] = $self->[SELF_HIST_LIST]->[$i];
$self->[SELF_CURSOR_INPUT] = 0;
$self->[SELF_CURSOR_DISPLAY] = 0;
$self->[SELF_UNDO] = [ $self->[SELF_INPUT], 0, 0 ]; # reset undo info
$found++;
last;
}
$self->rl_ding unless $found;
$self->_repaint_input_line;
}
# Return a normalized version of a string. This includes destroying
# 8th-bit-set characters, turning them into strange multi-byte
# sequences. Apologies to everyone; please let me know of a portable
# way to deal with this.
sub _normalize {
local $_ = shift;
s/([^ -~])/$normalized_character{$1}/g;
return $_;
}
sub _readable_key {
my ($raw_key) = @_;
my @text = ();
foreach my $l (split(//, $raw_key)) {
if (ord($l) == 0x1B) {
push(@text, 'Meta-');
next;
}
if (ord($l) < 32) {
push(@text, 'Control-' . chr(ord($l)+64));
next;
}
if (ord($l) > 127) {
my $l = ord($l)-128;
if ($l < 32) {
$l = "Control-" . chr($l+64);
}
push(@text, 'Meta-' . chr($l));
next;
}
if (ord($l) == 127) {
push @text, "^?";
next;
}
push(@text, $l);
}
return join("", @text);
}
# Calculate the display width of a string. The display width is
# sometimes wider than the actual string because some characters are
# represented on the terminal as multiple characters.
sub _display_width {
local $_ = shift;
my $width = length;
$width += $normalized_extra_width[ord] foreach (m/([\x00-\x1F\x7F-\xFF])/g);
return $width;
}
sub _build_search_prompt {
my ($self) = @_;
$self->[SELF_PROMPT] = $self->[SELF_SEARCH_PROMPT];
$self->[SELF_PROMPT] =~ s{%s}{$self->[SELF_SEARCH]};
}
sub _global_init {
return if $initialised;
# Get the terminal speed for Term::Cap.
$termios = POSIX::Termios->new();
$termios->getattr();
$ospeed = $termios->getospeed() || eval { POSIX::B38400() } || 0;
# Get the current terminal's capabilities.
$term = $ENV{TERM} || 'vt100';
$termcap = eval { Term::Cap->Tgetent( { TERM => $term, OSPEED => $ospeed } ) };
die "could not find termcap entry for ``$term'': $!" unless defined $termcap;
# Require certain capabilities.
$termcap->Trequire( qw( cl ku kd kl kr) );
# Cursor movement.
$tc_left = "LE";
eval { $termcap->Trequire($tc_left) };
if ($@) {
$tc_left = "le";
eval { $termcap->Trequire($tc_left) };
if ($@) {
# try out to see if we have a better terminfo defun.
# it may well not work (hence eval the lot), but it's worth a shot
eval {
my @tc = `infocmp -C $term`;
chomp(@tc);
splice(@tc, 0, 1); # remove header line
$ENV{TERMCAP} = join("", @tc);
$termcap = Term::Cap->Tgetent( { TERM => $term, OSPEED => $ospeed } );
$termcap->Trequire($tc_left);
};
}
die "POE::Wheel::ReadLine requires a termcap that supports LE or le" if $@;
}
# Terminal size.
# We initialize the values once on start-up,
# and then from then on, we check them on every entry into
# the input state engine (so that we have valid values) and
# before handing control back to the user (so that they get
# an up-to-date value).
eval { ($trk_cols, $trk_rows) = GetTerminalSize($stdout) };
($trk_cols, $trk_rows) = (80, 25) if $@;
# Configuration...
# Some things are optional.
eval { $termcap->Trequire( 'ce' ) };
$tc_has_ce = 1 unless $@;
# o/` You can ring my bell, ring my bell. o/`
my $bell = $termcap->Tputs( bl => 1 );
$bell = $termcap->Tputs( vb => 1 ) unless defined $bell;
$tc_bell = (defined $bell) ? $bell : '';
$bell = $termcap->Tputs( vb => 1 ) || '';
$tc_visual_bell = $bell;
my $convert_meta = 1;
for (my $ord = 0; $ord < 256; $ord++) {
my $str = chr($ord);
if ($ord > 127) {
if ($convert_meta) {
$str = "^[";
if (($ord - 128) < 32) {
$str .= "^" . lc(chr($ord-128+64));
} else {
$str .= lc(chr($ord-128));
}
} else {
$str = sprintf "<%2x>", $ord;
}
} elsif ($ord < 32) {
$str = '^' . lc(chr($ord+64));
}
elsif ($ord == 127) {
$str = "^?";
}
$normalized_character{chr($ord)} = $str;
$normalized_extra_width[$ord] = length ( $str ) - 1;
}
$initialised++;
}
#------------------------------------------------------------------------------
# The methods themselves.
# Create a new ReadLine wheel.
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
croak "$class requires a working Kernel" unless defined $poe_kernel;
my $input_event = delete $params{InputEvent};
croak "$class requires an InputEvent parameter" unless defined $input_event;
my $put_mode = delete $params{PutMode};
$put_mode = 'idle' unless defined $put_mode;
croak "$class PutMode must be either 'immediate', 'idle', or 'after'"
unless $put_mode =~ /^(immediate|idle|after)$/;
my $idle_time = delete $params{IdleTime};
$idle_time = 2 unless defined $idle_time;
my $app = delete($params{AppName}) || delete($params{appname});
delete $params{appname}; # in case AppName was present
$app ||= 'poe-readline';
if (scalar keys %params) {
carp(
"unknown parameters in $class constructor call: ",
join(', ', keys %params)
);
}
my $self = undef;
if (ref $proto) {
$self = bless [], $class;
@$self = @$proto;
$self->[SELF_SOURCE] = $proto;
# ensure we're not bound to the old handler
$poe_kernel->select_read($stdin);
} else {
$self = bless [
'', # SELF_INPUT
0, # SELF_CURSOR_INPUT
$input_event, # SELF_EVENT_INPUT
0, # SELF_READING_LINE
undef, # SELF_STATE_READ
'>', # SELF_PROMPT
[ ], # SELF_HIST_LIST
0, # SELF_HIST_INDEX
'', # SELF_INPUT_HOLD
'', # SELF_KEY_BUILD
1, # SELF_INSERT_MODE
$put_mode, # SELF_PUT_MODE
[ ], # SELF_PUT_BUFFER
$idle_time, # SELF_IDLE_TIME
undef, # SELF_STATE_IDLE
0, # SELF_HAS_TIMER
0, # SELF_CURSOR_DISPLAY
&POE::Wheel::allocate_wheel_id(), # SELF_UNIQUE_ID
undef, # SELF_KEYMAP
{ }, # SELF_OPTIONS
$app, # SELF_APP
{}, # SELF_ALL_KEYMAPS
undef, # SELF_PENDING
0, # SELF_COUNT
0, # SELF_MARK
{}, # SELF_MARKLIST
[], # SELF_KILL_RING
'', # SELF_LAST
undef, # SELF_PENDING_FN
undef, # SELF_SOURCE
'', # SELF_SEARCH
undef, # SELF_SEARCH_PROMPT
undef, # SELF_SEARCH_MAP
'', # SELF_PREV_PROMPT
0, # SELF_SEARCH_DIR
'', # SELF_SEARCH_KEY
[], # SELF_UNDO
], $class;
_global_init();
$self->rl_re_read_init_file();
}
# Turn off $stdout buffering.
select((select($stdout), $| = 1)[0]);
# Set up the event handlers. Idle goes first.
$self->[SELF_STATE_IDLE] = (
ref($self) . "(" . $self->[SELF_UNIQUE_ID] . ") -> input timeout"
);
$self->[SELF_STATE_READ] = (
ref($self) . "(" . $self->[SELF_UNIQUE_ID] . ") -> select read"
);
# TODO - The following hack breaks a circular reference on $self.
{
my $weak_self = $self;
use Scalar::Util qw(weaken);
weaken $weak_self;
$poe_kernel->state(
$self->[SELF_STATE_IDLE],
sub { _idle_state($weak_self, @_[1..$#_]) }
);
$poe_kernel->state(
$self->[SELF_STATE_READ],
sub { _read_state($weak_self, @_[1..$#_]) }
);
}
return $self;
}
#------------------------------------------------------------------------------
# Destroy the ReadLine wheel. Clean up the terminal.
sub DESTROY {
my $self = shift;
return unless $initialised;
# Stop selecting on the handle.
$poe_kernel->select_read($stdin);
# Detach our tentacles from the parent session.
if ($self->[SELF_STATE_READ]) {
$poe_kernel->state($self->[SELF_STATE_READ]);
$self->[SELF_STATE_READ] = undef;
}
if ($self->[SELF_STATE_IDLE]) {
$poe_kernel->alarm($self->[SELF_STATE_IDLE]);
$poe_kernel->state($self->[SELF_STATE_IDLE]);
$self->[SELF_STATE_IDLE] = undef;
}
# tell the terminal that we want to leave 'application' mode
print $termcap->Tputs('ke' => 1) if $termcap->Tputs('ke');
# Restore the console.
ReadMode('restore');
&POE::Wheel::free_wheel_id($self->[SELF_UNIQUE_ID]);
}
#------------------------------------------------------------------------------
# Redefine the idle handler. This also uses stupid closure tricks.
# See the comments for &_define_read_state for more information about
# these closure tricks.
sub _idle_state {
my ($self) = $_[OBJECT];
if (@{$self->[SELF_PUT_BUFFER]}) {
$self->_wipe_input_line;
$self->_flush_output_buffer;
$self->_repaint_input_line;
}
# No more timer.
$self->[SELF_HAS_TIMER] = 0;
}
sub _read_state {
my ($self, $k) = @_[OBJECT, KERNEL];
# Read keys, non-blocking, as long as there are some.
while (defined(my $raw_key = ReadKey(-1))) {
# Not reading a line; discard the input.
next unless $self->[SELF_READING_LINE];
# Update the timer on significant input.
if ( $self->[SELF_PUT_MODE] eq 'idle' ) {
$k->delay( $self->[SELF_STATE_IDLE], $self->[SELF_IDLE_TIME] );
$self->[SELF_HAS_TIMER] = 1;
}
push(
@{$self->[SELF_UNDO]}, [
$self->[SELF_INPUT],
$self->[SELF_CURSOR_INPUT],
$self->[SELF_CURSOR_DISPLAY]
]
);
# Build-multi character codes and make the keystroke printable.
$self->[SELF_KEY_BUILD] .= $raw_key;
$raw_key = $self->[SELF_KEY_BUILD];
my $key = _normalize($raw_key);
if ($self->[SELF_PENDING_FN]) {
my $old = $self->[SELF_INPUT];
my $oldref = $self->[SELF_PENDING_FN];
push(
@{$self->[SELF_UNDO]}, [
$old,
$self->[SELF_CURSOR_INPUT],
$self->[SELF_CURSOR_DISPLAY]
]
);
$self->[SELF_PENDING_FN]->($self, $key, $raw_key);
pop(@{$self->[SELF_UNDO]}) if ($old eq $self->[SELF_INPUT]);
$self->[SELF_KEY_BUILD] = '';
if ($self->[SELF_PENDING_FN] && "$self->[SELF_PENDING_FN]" eq $oldref) {
$self->[SELF_PENDING_FN] = undef;
}
next;
}
# Keep glomming keystrokes until they stop existing in the