-
Notifications
You must be signed in to change notification settings - Fork 16
/
Readline.pm
2684 lines (2054 loc) · 107 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
use v6;
use NativeCall;
#
# XXX &cglobal.signature -> (Any $libname, Any $symbol, Any $target-type)
#
=begin pod
=begin NAME
Readline - GNU Readline binding for perl6
=end NAME
=begin SYNOPSIS
my $readline = Readline.new;
while my $response = $readline.readline( "prompt here (<cr> to exit)> " ) {
$readline.add-history( $response ) if $response ~~ /\S/;
say "[$response]";
}
=end SYNOPSIS
=begin DESCRIPTION
A thin OO wrapper around the GNU Readline library. It exposes every function that the current Readline library does, along with some of the structs for history and keymap manipulation from the header files, so that you can peek into the internals. Most people will just use the basic C<readline> and C<add-history> functions, but the more advanced keymapping, history and completion bindings are available for use.
Method names are taken verbatim from the GNU Readline library, with one exception. Library calls use '_', Readline methods use '-'. This helps keep the two layers separate, and is a not-so-subtle reminder that you're using perl6 when using the library.
The documentation in the METHODS section is a verbatim paste of the appropriate bits from the GNU Readline documentation, so for further explanation of the methods, especially callbacks and completion, please see the L<GNU Readline> documentation.
Any chapter references in the documentation refer to the GNU Readline or GNU History manual.
=end DESCRIPTION
=begin LIBRARY
By default the Perl 6 module searches for libreadline.so.* and takes the most recent match it can find.
If you're on OS X, it searches for libreadline.*.dylib.
On OpenBSD it searches for libereadline.so.*.
While I'd prefer to use L<LibraryCheck>'s technique of just attempting to link to a library, it doesn't seem to work inside of the C<is native(&func)> attribute. So instead, it defaults to v7 (the current version as of 2018-07-14) and searches for other versions along a fixed set of library paths, taken from bug reports.
I'll eventually put this into a proper library-find method.
=end LIBRARY
=begin METHODS
=item readline( Str $prompt ) returns Str
=item initialize( ) returns int32
Initialize or re-initialize Readline's internal state. It's not strictly necessary to call this; readline() calls it before reading any input.
=item ding( ) returns int32
Ring the terminal bell, obeying the setting of C<$bell-style>.
=begin History
These methods let you add, delete and manipulate history entries. See the L<examples/echo.pl6> script for usage.
=item add-history( Str $history )
Place string C<$history> at the end of the history list. The associated data field (if any) is set to NULL.
=item using-history( )
Begin a session in which the history functions might be used. This initializes the interactive variables.
Author's note - C<add-history()> works fine without this call, maybe it's for methods that require state.
=item history-get-history-state( ) returns HISTORY_STATE
Return a structure describing the current state of the input history.
=item history-set-history-state( HISTORY_STATE $state )
Set the state of the history list according to C<$state>.
=item add-history-time( Str $timestamp )
Change the time stamp associated with the most recent history entry to C<$timestamp>.
=item remove-history( int32 $which ) returns HIST_ENTRY
Remove history entry at offset C<$which> from the history. The removed element is returned so you can free the line, data, and containing structure.
=item free-history-entry( HIST_ENTRY $entry ) returns Str # histdata_t
Free the history entry C<$entry> and any history library private data associated with it. Returns the application-specific data so the caller can dispose of it.
=item replace-history-entry( int32 $which, Str $line, Str $data ) returns HIST_ENTRY # histdata_t $data ) returns HIST_ENTRY
Make the history entry at offset C<$which> have C<$line> and C<$data>. This returns the old entry so the caller can dispose of any application-specific data. In the case of an invalid C<$which>, a NULL pointer is returned.
=item clear-history( )
Clear the history list by deleting all the entries.
=item stifle-history( int32 $max )
Stifle the history list, remembering only the last C<$max> entries.
=item unstifle-history( )
Stop stifling the history. This returns the previously-set maximum number of history entries (as set by stifle_history()). The value is positive if the history was stifled, negative if it wasn't.
=item history-is-stifled( ) returns Bool
Returns True if the history is stifled, False if not. The C version returns non-zero if the history is stifled, this gets converted to a Perl6 boolean.
=item history-list( ) returns CArray[HIST_ENTRY]
Return a NULL terminated array of HIST_ENTRY * which is the current input history. Element 0 of this list is the beginning of time. If there is no history, return NULL.
=item where-history( ) returns int32
Returns the offset of the current history element.
=item current-history( int32 $which ) returns HIST_ENTRY
Return the history entry at the position C<$which>, as determined by C<where-history()>. If there is no entry there, return a NULL pointer.
=item history-get( int32 $which ) returns HIST_ENTRY
Return the history entry at position C<$which>, starting from C<$history_base> (see section 2.4 History Variables). If there is no entry there, or if offset is greater than the history length, return a NULL pointer.
=item history-get-time( HIST_ENTRY $entry ) returns time_t
Return the time stamp associated with the history entry C<$entry>.
=item history-total-bytes( ) returns int32
Return the number of bytes that the primary history entries are using. This function returns the sum of the lengths of all the lines in the history.
=item history-set-pos( int32 $pos ) returns int32
Set the current history offset to C<$pos>, an absolute index into the list. Returns 1 on success, 0 if C<$pos> is less than zero or greater than the number of history entries.
=item previous-history( ) returns HIST_ENTRY
Back up the current history offset to the previous history entry, and return a pointer to that entry. If there is no previous entry, return a NULL pointer.
=item next-history( ) returns HIST_ENTRY
Move the current history offset forward to the next history entry, and return the a pointer to that entry. If there is no next entry, return a NULL pointer.
=item history-search( Str $text, int32 $pos ) returns int32
Search the history for C<$text>, starting at history offset C<$pos>. If direction is less than 0, then the search is through previous entries, otherwise through subsequent entries. If string is found, then the current history index is set to that history entry, and the value returned is the offset in the line of the entry where string was found. Otherwise, nothing is changed, and a -1 is returned.
=item history-search-prefix( Str $prefix, int32 $pos ) returns int32
Search the history for a line prefixed with C<$prefix>, starting at history offset C<$pos>. The search is anchored: matching lines must begin with C<$prefix>. If direction is less than 0, then the search is through previous entries, otherwise through subsequent entries. If string is found, then the current history index is set to that entry, and the return value is 0. Otherwise, nothing is changed, and a -1 is returned.
=item history-search-pos( Str $text, int32 $pos, int32 $dir ) returns int32
Search for C<$text> in the history list, starting at C<$pos>, an absolute index into the list. If C<$direction> is negative, the search proceeds backward from C<$pos>, otherwise forward. Returns the absolute index of the history element where C<$text> was found, or -1 otherwise.
=item read-history( Str $filename ) returns int32
Add the contents of C<$filename> to the history list, a line at a time. If C<$filename> is Empty, then read from `~/.history'. Returns 0 if successful, or errno if not.
=item read-history-range( Str $filename, int32 $from, int32 $to ) returns int32
Read a range of lines from C<$filename>, adding them to the history list. Start reading at line C<$from> and end at C<$to>. If C<$from> is zero, start at the beginning. If C<$to> is less than C<$from>, then read until the end of the file. If filename is Empty, then read from `~/.history'. Returns 0 if successful, or errno if not.
=item write-history( Str $filename ) returns int32
Write the current history to C<$filename>, overwriting C<$filename> if necessary. If C<$filename> is NULL, then write the history list to `~/.history'. Returns 0 on success, or errno on a read or write error.
=item append-history( int32 $offset, Str $filename ) returns int32
Append the elements starting at C<$offset> of the history list to C<$filename>. If C<$filename> is Empty, then append to `~/.history'. Returns 0 on success, or errno on a read or write error.
=item history-truncate-file( Str $filename, int32 $nLines ) returns int32
Truncate the history file C<$filename>, leaving only the last C<$nLines> lines. If C<$filename> is Empty, then `~/.history' is truncated. Returns 0 on success, or errno on failure.
=item history-expand( Str $string, Pointer[Str] $output ) returns int32
Expand C<$string>, placing the result into C<$output>, a pointer to a string (see section 1.1 History Expansion). Returns:
0 If no expansions took place (or, if the only change in the text was the removal of escape characters preceding the history expansion character);
1 if expansions did take place;
-1 if there was an error in expansion;
2 if the returned line should be displayed, but not executed, as with the :p modifier (see section 1.1.3 Modifiers).
If an error occurred in expansion, then output contains a descriptive error message.
=item history-arg-extract( int32 $first, int32 $last, Str $string ) returns Str
Extract a string segment consisting of the C<$first> through C<$last> arguments present in C<$string>. Arguments are split using C<history_tokenize()>.
=item get-history-event( Str $string, Pointer[int32] $cIndex, Str $delimiting-quote ) returns Str
Returns the text of the history event beginning C<$$cIndex> characters into C<$string>. C<$$cIndex> is modified to point to after the event specifier. At function entry, C<$cIndex> points to the index into string where the history event specification begins. qchar is a character that is allowed to end the event specification in addition to the "normal" terminating characters.
Editor's note: C<$$cIndex> may be a byte offset into C<$string> rather than a character offset, so Unicode users beware.
Editor's note - C<$delimiting-quote> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_get_history_event()> instead.
DELIMITING_QUOTE is a character that is allowed to end the string specification for what to search for in addition to the normal characters `:', ` ', `\t', `\n', and sometimes `?'.
=item history-tokenize( Str $string ) returns CArray[Str]
Return an array of tokens parsed out of C<$string>, much as the shell might. The tokens are split on the characters in the C<$history_word_delimiters> variable, and shell quoting conventions are obeyed.
=end History
=begin Keymap
These methods let you manipulate the built-in key mappings.
=item make-bare-keymap( ) returns Keymap
Returns a new, empty keymap. The space for the keymap is allocated with malloc(); the caller should free it by calling rl_free_keymap() when done.
=item copy-keymap( Keymap $map ) returns Keymap
Return a new keymap which is a copy of C<$map>.
=item make-keymap( ) returns Keymap
Return a new keymap with the printing characters bound to C<$rl_insert>, the lowercase Meta characters bound to run their equivalents, and the Meta digits bound to produce numeric arguments.
=item discard-keymap( Keymap $map )
Free the storage associated with the data in C<$map>. The caller should free the pointer to C<$map>.
=item free-keymap( Keymap $map )
Free all storage associated with C<$map>. This calls C<rl_discard_keymap()> to free subordindate keymaps and macros.
=item get-keymap-by-name( Str $name ) returns Keymap
Return the keymap matching C<$name>. C<$name> is one which would be supplied in a set keymap inputrc line (see section 1.3 Readline Init File).
=item get-keymap( ) returns Keymap
Returns the currently active keymap.
=item get-keymap-name( Keymap $map ) returns Str
Return the name matching C<$map>. name is one which would be supplied in a set keymap inputrc line (see section 1.3 Readline Init File).
=item set-keymap( Keymap $map )
Makes C<$map> the currently active keymap.
=end Keymap
=begin Callback
These functions let you use L<Readline> as an interactive event loop.
=item callback-handler-install( Str $prompt, &callback (Str) )
Set up the terminal for readline I/O and display the initial expanded value of C<$prompt>. Save the value of lhandler to use as a handler function to call when a complete line of input has been entered. The handler function receives the text of the line as an argument.
=item callback-read-char( )
Whenever an application determines that keyboard input is available, it should call rl_callback_read_char(), which will read the next character from the current input source. If that character completes the line, rl_callback_read_char will invoke the lhandler function installed by rl_callback_handler_install to process the line. Before calling the lhandler function, the terminal settings are reset to the values they had before calling rl_callback_handler_install. If the lhandler function returns, and the line handler remains installed, the terminal settings are modified for Readline's use again. EOF is indicated by calling lhandler with a NULL line.
=item callback-handler-remove( )
Restore the terminal to its initial state and remove the line handler. This may be called from within a callback as well as independently. If the handler installed by rl_callback_handler_install does not exit the program, either this function or the function referred to by the value of rl_deprep_term_function should be called before the program exits to reset the terminal settings.
=end Callback
=begin Prompt
These methods manage the readline prompt.
=item set-prompt( Str $prompt ) returns int32
Make Readline use C<$prompt> for subsequent redisplay. This calls C<rl_expand_prompt()> to expand the prompt and sets C<$rl_prompt> to the result.
=item expand-prompt( Str $prompt ) returns int32
Expand any special character sequences in C<$prompt> and set up the local Readline prompt redisplay variables. This function is called by C<readline()>. It may also be called to expand the primary prompt if the C<rl_on_new_line_with_prompt()> function or C<$rl-already_prompted> variable is used. It returns the number of visible characters on the last line of the (possibly multi-line) prompt. Applications may indicate that the prompt contains characters that take up no physical screen space when displayed by bracketing a sequence of such characters with the special markers C<RL_PROMPT_START_IGNORE> and C<RL_PROMPT_END_IGNORE> (declared in `readline.h' and exposed in the Readline module as constants.) This may be used to embed terminal-specific escape sequences in prompts.
=end Prompt
=begin Binding
=item bind-key( Str $key, &callback (int32, int32 --> int32) ) returns int32
rl_bind_key() takes two arguments: C<$key> is the character that you want to bind, and the callback function is called when key is pressed. Binding TAB to C<rl_insert()> makes TAB insert itself. C<rl_bind_key()> returns non-zero if key is not a valid ASCII character code (between 0 and 255).
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_bind_key()> instead.
=item bind-key-in-map( Str $key, &callback (int32, int32 --> int32), Keymap $map ) returns int32
Bind C<$key> to function in C<$map>. Returns non-zero in the case of an invalid key.
Editor's note - C<$delimiting-quote> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_bind_key_in_map()> instead.
=item unbind-key( Str $key ) returns Bool
Bind C<$key> to the null function in the currently active keymap. Returns C<False> in case of error.
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_unbind_key()> instead.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_unbind_key()> function rather than the Perl6 layer.
=item unbind-key-in-map( Str $key, Keymap $map ) returns Bool
Bind C<$key> to the null function in C<$map>. Returns False in case of error.
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_unbind_key_in_map()> instead.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_unbind_key_in_map()> function rather than the Perl6 layer.
=item bind-key-if-unbound( Str $key, &callback (int32, int32 --> int32) ) returns Bool
Binds C<$key> to function if it is not already bound in the currently active keymap. Returns False in the case of an invalid key or if key is already bound.
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_bind_key_if_unbound()> instead.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_key_if_unbound()> function rather than the Perl6 layer.
=item bind-key-if-unbound-in-map( Str $key, &callback (int32, int32 --> int32), Keymap $map ) returns Bool
Binds C<$key> to function if it is not already bound in C<$map>. Returns False in the case of an invalid key or if C<$key> is already bound.
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_bind_key_if_unbound_in_map()> instead.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_keyseq()> function rather than the Perl6 layer.
=item unbind-function-in-map ( &callback (int32, int32 --> int32), Keymap $map ) returns int32
Unbind all keys that execute function in C<$map>.
=item bind-keyseq( Str $keyseq, &callback (int32, int32 --> int32) ) returns Bool
Bind the key sequence represented by the string C<$keyseq> to the function function, beginning in the current keymap. This makes new keymaps as necessary. Returns False if C<$keyseq> is invalid.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_keyseq()> function rather than the Perl6 layer.
=item bind-keyseq-in-map( Str $keyseq, &callback (int32, int32 --> int32), Keymap $map ) returns Bool
Bind the key sequence represented by the string C<$keyseq> to the callback function. This makes new keymaps as necessary. Initial bindings are performed in map. The return value is False if C<$keyseq> is invalid.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_keyseq_in_map()> function rather than the Perl6 layer.
=item bind-keyseq-if-unbound( Str $keyseq, &callback (int32, int32 --> int32) ) returns Bool
Binds keyseq to function if it is not already bound in the currently active keymap. Returns False in the case of an invalid keyseq or if C<$keyseq> is already bound.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_keyseq_if_unbound()> function rather than the Perl6 layer.
=item bind-keyseq-if-unbound-in-map( Str $str, &callback (int32, int32 --> int32), Keymap $map ) returns Bool
Binds keyseq to function if it is not already bound in map. Returns non-zero in the case of an invalid keyseq or if keyseq is already bound.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_bind_keyseq_if_unbound_in_map()> function rather than the Perl6 layer.
=item generic-bind( int32 $i, Str $keyseq, Str $t, Keymap $map ) returns int32
Bind the key sequence represented by the string C<$keyseq> to the arbitrary pointer data. type says what kind of data is pointed to by data; this can be a function (ISFUNC), a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps as necessary. The initial keymap in which to do bindings is map.
=end Binding
=item add-defun( Str $str, &callback (int32, int32 --> int32), Str $key ) returns int32
Add name to the list of named functions. Make function be the function that gets called. If C<$key> is not -1, then bind it to function using rl_bind_key().
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_add_defun()> instead.
Using this function alone is sufficient for most applications. It is the recommended way to add a few functions to the default functions that Readline has built in. If you need to do something other than adding a function to Readline, you may need to use the underlying functions described below.
=item variable-value( Str $variable ) returns Str
Return a string representing the value of the Readline variable C<$variable>. For boolean variables, this string is either `on' or `off'.
=item variable-bind( Str $variable, Str $value ) returns int32
Make the Readline variable C<$variable> have value C<$alue>. This behaves as if the readline command `set variable value' had been executed in an inputrc file (see section 1.3.1 Readline Init File Syntax).
=item set-key( Str $str, &callback (int32, int32 --> int32), Keymap $map )
Equivalent to C<rl-bind-keyseq-in-map()>.
=item macro-bind( Str $keyseq, Str $macro, Keymap $map ) returns int32
Bind the key sequence C<$keyseq> to invoke the macro C<$macro>. The binding is performed in C<$map>. When C<$keyseq> is invoked, the macro will be inserted into the line. This function is deprecated; use C<rl_generic_bind()> instead.
=item named-function( Str $s ) returns &callback (int32, int32 --> int32)
Return the function with name name.
=item function-of-keyseq( Str $keyseq, Keymap $map, Pointer[int32] $type ) returns &callback (int32, int32 --> int32)
Return the function invoked by C<$keyseq> in keymap C<$map>. If C<$map> is Nil, the current keymap is used. If C<$type> is not Nil, the type of the object is returned in the int variable it points to (one of ISFUNC, ISKMAP, or ISMACR).
=item list-funmap-names( )
Print the names of all bindable Readline functions to rl_outstream.
=item invoking-keyseqs-in-map( Pointer[&callback (int32, int32 --> int32)] $p-cmd, Keymap $map ) returns CArray[Str]
Return an array of strings representing the key sequences used to invoke function in the keymap C<$map>.
=item invoking-keyseqs( Pointer[&callback (int32, int32 --> int32)] $p-cmd ) returns CArray[Str]
Return an array of strings representing the key sequences used to invoke function in the current keymap.
=item function-dumper( Bool $readable )
Print the readline function names and the key sequences currently bound to them to C<$rl_outstream>. If C<$readable> is True, the list is formatted in such a way that it can be made part of an inputrc file and re-read.
Editor's note - The Perl6 layer translates True values of C<$readable> to non-zero, so if you want to specify a particular int32 value, please use the underlying C<rl_function_dumper()> call.
=item macro-dumper( Bool $readable )
Print the key sequences bound to macros and their values, using the current keymap, to C<$rl_outstream>. If C<$readable> is True, the list is formatted in such a way that it can be made part of an inputrc file and re-read.
Editor's note - The Perl6 layer translates True values of C<$readable> to non-zero, so if you want to specify a particular int32 value, please use the underlying C<rl_macro_dumper()> call.
=item variable-dumper( Bool $readable )
Print the readline variable names and their current values to C<$rl_outstream>. If readable is True, the list is formatted in such a way that it can be made part of an inputrc file and re-read.
Editor's note - The Perl6 layer translates True values of C<$readable> to non-zero, so if you want to specify a particular int32 value, please use the underlying C<rl_variable_dumper()> call.
=item read-init-file( Str $filename )
Read keybindings and variable assignments from C<$filename> (see section 1.3 Readline Init File).
=item parse-and-bind( Str $line ) returns int32
Parse C<$line> as if it had been read from the inputrc file and perform any key bindings and variable assignments found (see section 1.3 Readline Init File).
=item add-funmap-entry( Str $name, &callback (int32, int32 --> int32) ) returns int32
Add C<$name> to the list of bindable Readline command names, and make function the function to be called when name is invoked.
=item funmap-names( ) returns CArray[Str]
Return a NULL terminated array of known function names. The array is sorted. The array itself is allocated, but not the strings inside. You should free the array, but not the pointers, using free or rl_free when you are done.
=item push-macro-input( Str $macro )
Cause C<$macro> to be inserted into the line, as if it had been invoked by a key bound to a macro. Not especially useful; use C<rl_insert_text()> instead.
=item free-undo-list( )
Free the existing undo list.
=item do-undo( ) returns int32
Undo the first thing on the undo list. Returns False if there was nothing to undo, True if something was undone.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_do_undo()> function rather than the Perl6 layer.
=item begin-undo-group( ) returns int32
Begins saving undo information in a group construct. The undo information usually comes from calls to rl_insert_text() and rl_delete_text(), but could be the result of calls to rl_add_undo().
=item end-undo-group( ) returns int32
Closes the current undo group started with rl_begin_undo_group (). There should be one call to rl_end_undo_group() for each call to rl_begin_undo_group().
=item modifying( int32 $start, int32 $end ) returns int32
Tell Readline to save the text between C<$start> and C<$end> as a single undo unit. It is assumed that you will subsequently modify that text.
=item redisplay( )
Change what's displayed on the screen to reflect the current contents of rl_line_buffer.
=item on-new-line( ) returns int32
Tell the update functions that we have moved onto a new (empty) line, usually after outputting a newline.
=item on-new-line-with-prompt( ) returns int32
Tell the update functions that we have moved onto a new line, with C<$rl_prompt> already displayed. This could be used by applications that want to output the prompt string themselves, but still need Readline to know the prompt string length for redisplay. It should be used after setting rl_already_prompted.
=item forced-update-display( ) returns int32
Force the line to be updated and redisplayed, whether or not Readline thinks the screen display is correct.
=item clear-message( ) returns int32
Clear the message in the echo area. If the prompt was saved with a call to rl_save_prompt before the last call to rl_message, call rl_restore_prompt before calling this function.
=item reset-line-state( ) returns int32
Reset the display state to a clean state and redisplay the current line starting on a new line.
=item crlf( ) returns int32
Move the cursor to the start of the next screen line.
=item show-char( Str $c ) returns int32
Display character c on rl_outstream. If Readline has not been set to display meta characters directly, this will convert meta characters to a meta-prefixed key sequence. This is intended for use by applications which wish to do their own redisplay.
Editor's note - C<$key> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_show_char()> instead.
=item save-prompt( )
Save the local Readline prompt display state in preparation for displaying a new message in the message area with rl_message().
=item restore-prompt( )
Restore the local Readline prompt display state saved by the most recent call to rl_save_prompt. if rl_save_prompt was called to save the prompt before a call to rl_message, this function should be called before the corresponding call to rl_clear_message.
=item replace-line( Str $text, int32 $clear-undo )
Replace the contents of C<$rl_line_buffer> with C<$text>. The point and mark are preserved, if possible. If C<$clear-undo> is non-zero, the undo list associated with the current line is cleared.
=item insert-text( Str $text ) returns int32
Insert C<$text> into the line at the current cursor position. Returns the number of characters inserted.
=item delete-text( int32 $start, int32 $end ) returns int32
Delete the text between C<$start> and C<$end> in the current line. Returns the number of characters deleted.
=item kill-text( int32 $start, int32 $end ) returns int32
Copy the text between C<$start> and C<$end> in the current line to the kill ring, appending or prepending to the last kill if the last command was a kill command. The text is deleted. If C<$start> is less than C<$end>, the text is appended, otherwise prepended. If the last command was not a kill, a new kill ring slot is used.
=item copy-text( int32 $start, int32 $end ) returns Str
Return a copy of the text between C<$start> and C<$end> in the current line.
=item prep-terminal( int32 $meta-flag )
Modify the terminal settings for Readline's use, so readline() can read a single character at a time from the keyboard. The C<$meta-flag> argument should be non-zero if Readline should read eight-bit input.
=item deprep-terminal( )
Undo the effects of C<rl_prep_terminal()>, leaving the terminal in the state in which it was before the most recent call to C<rl_prep_terminal()>.
=item tty-set-default-bindings( Keymap $map )
Read the operating system's terminal editing characters (as would be displayed by stty) to their Readline equivalents. The bindings are performed in keymap C<$map>.
=item tty-unset-default-bindings( Keymap $map )
Reset the bindings manipulated by C<$rl_tty_set_default_bindings> so that the terminal editing characters are bound to C<$rl_insert>. The bindings are performed in keymap C<$map>.
=item reset-terminal( Str $terminal-name ) returns int32
Reinitialize Readline's idea of the terminal settings using C<$terminal-name> as the terminal type (e.g., vt100). If terminal_name is NULL, the value of the TERM environment variable is used.
=item resize-terminal( )
Update Readline's internal screen size by reading values from the kernel.
=item set-screen-size( int32 $rows, int32 $cols )
Set Readline's idea of the terminal size to C<$rows> rows and C<$cols> columns. If either C<$rows> or C<$cols> is less than or equal to 0, Readline's idea of that terminal dimension is unchanged.
If an application does not want to install a SIGWINCH handler, but is still interested in the screen dimensions, Readline's idea of the screen size may be queried.
=item get-screen-size( Pointer[int32] $rows, Pointer[int32] $cols )
Return Readline's idea of the terminal's size in the variables pointed to by the arguments.
=item reset-screen-size( )
Cause Readline to reobtain the screen size and recalculate its dimensions.
=item get-termcap( Str $cap ) returns Str
Retrieve the string value of the termcap capability C<$cap>. Readline fetches the termcap entry for the current terminal name and uses those capabilities to move around the screen line and perform other terminal-specific operations, like erasing a line. Readline does not use all of a terminal's capabilities, and this function will return values for only those capabilities Readline uses.
=item extend-line-buffer( int32 $len )
Ensure that rl_line_buffer has enough space to hold C<$len> characters, possibly reallocating it if necessary.
=item alphabetic( Str $c ) returns Bool
Return True if c is an alphabetic character.
Editor's note - The underlying C function returns non-zero on error, this is mapped onto a Perl6 Bool type. If you want the original behavior, call the underlying C<rl_alphabetic()> function rather than the Perl6 layer.
Editor's note - C<$c> is an integer in the underlying C API - If you want to use the actual C function call, please call C<rl_alphabetic()> instead.
=item free( Pointer $mem )
Deallocate the memory pointed to by C<$mem>. C<$mem> must have been allocated by malloc.
=begin Signals
These methods manipulate signal handling for L<Readline>.
=item set-signals( ) returns int32
Install Readline's signal handler for SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGALRM, SIGTSTP, SIGTTIN, SIGTTOU, and SIGWINCH, depending on the values of rl_catch_signals and rl_catch_sigwinch.
=item clear-signals( ) returns int32
Remove all of the Readline signal handlers installed by C<rl_set_signals()>.
=item cleanup-after-signal( )
This function will reset the state of the terminal to what it was before readline() was called, and remove the Readline signal handlers for all signals, depending on the values of C<$rl-catch-signals> and C<$rl-catch-sigwinch>.
=item reset-after-signal( )
This will reinitialize the terminal and reinstall any Readline signal handlers, depending on the values of C<$rl_catch_signals> and C<$rl_catch_sigwinch>.
If an application does not wish Readline to catch SIGWINCH, it may call C<rl_resize_terminal()> or C<rl_set_screen_size()> to force Readline to update its idea of the terminal size when a SIGWINCH is received.
=item free-line-state( )
This will free any partial state associated with the current input line (undo information, any partial history entry, any partially-entered keyboard macro, and any partially-entered numeric argument). This should be called before C<rl_cleanup_after_signal()>. The Readline signal handler for SIGINT calls this to abort the current input line.
=item echo-signal( int32 $c ) # XXX not in v6
If an application wishes to install its own signal handlers, but still have readline display characters that generate signals, calling this function with sig set to SIGINT, SIGQUIT, or SIGTSTP will display the character generating that signal.
=end Signals
=item set-paren-blink-timeout( int32 $c ) returns int32
Set the time interval (in microseconds) that Readline waits when showing a balancing character when blink-matching-paren has been enabled.
=item complete-internal( int32 $what-to-do ) returns int32
Complete the word at or before point. C<$what-to-do> says what to do with the completion. A value of `?' means list the possible completions. `TAB' means do standard completion. `*' means insert all of the possible completions. `!' means to display all of the possible completions, if there is more than one, as well as performing partial completion. `@' is similar to `!', but possible completions are not listed if the possible completions share a common prefix.
=item username-completion-function ( Str $text, int32 $i ) returns Str # XXX doesn't exist?
A completion generator for usernames. C<$text> contains a partial username preceded by a random character (usually `~'). As with all completion generators, state is zero on the first call and non-zero for subsequent calls.
=item filename-completion-function ( Str $text, int32 $i ) returns Str # XXX Doesn't exist?
A generator function for filename completion in the general case. C<$text> is a partial filename. The Bash source is a useful reference for writing application-specific completion functions (the Bash completion functions call this and other Readline functions).
=item completion-mode( Pointer[&callback (int32, int32 --> int32)] $cfunc ) returns int32
Returns the appropriate value to pass to C<rl_complete_internal()> depending on whether C<$cfunc> was called twice in succession and the values of the show-all-if-ambiguous and show-all-if-unmodified variables. Application-specific completion functions may use this function to present the same interface as rl_complete().
=item save-state( readline_state $sp ) returns int32
Save a snapshot of Readline's internal state to C<$sp>. The contents of the readline_state structure are documented in `readline.h'. The caller is responsible for allocating the structure.
=item tilde-expand( Str $str ) returns Str
Return a new string which is the result of tilde-expanding C<$str>.
=item tilde-expand-word( Str $filename ) returns Str
Do the work of tilde expansion on C<$filename>. C<$filename> starts with a tilde. If there is no expansion, call C<$tilde_expansion_failure_hook>.
=item tilde-find-word( Str $word, int32 $offset, Pointer[int32] $p-end-offset ) returns Str
Find the portion of the string beginning with ~ that should be expanded.
=item restore-state( readline_state $sp ) returns int32
Restore Readline's internal state to that stored in C<$sp>, which must have been saved by a call to C<rl_save_state()>. The contents of the readline_state structure are documented in `readline.h'. The caller is responsible for freeing the structure.
=end METHODS
=end pod
class Readline:ver<0.1.5> {
sub LIBREADLINE {
my $library = 'readline';
my $library-match = rx/:i libreadline\.so\.(\d+) $/;
my $version = v7;
# Collect library paths from arbitrary OSen to search.
#
my constant LIBRARY-PATHS = (
'/lib/x86_64-linux-gnu', # Author's VM
'/usr/local/lib', # Generic path
'/usr/lib64', # Slackware 14 among others
'/usr/lib', # Generic path
'/lib' # even more generic.
);
my @library-path = grep { .IO.e }, LIBRARY-PATHS;
given $*VM.osname {
when 'openbsd' {
$library = 'ereadline';
$library-match = rx/:i libereadline\.so\.(\d+) $/;
$version = v1.0;
my sub tgetnum(Str --> int32) is native('ncurses') { * }
tgetnum('');
}
when 'darwin' {
$library-match = rx/:i libreadline\.(\d+)\.dylib $/;
}
}
# Search each of the LIBRARY-PATHS paths for libreadline.
#
for @library-path -> $path {
# Filter out everything but libreadline.{so,dylib}
# Sort it so the last entry is the latest
#
my @dir = sort dir( $path, :test( $library-match ) );
next unless @dir;
@dir[*-1] ~~ $library-match;
$version = Version.new( ( $0 ) );
last;
}
given $*DISTRO.name {
when 'slackware' {
if $version ~~ v6 {
my sub tgetnum(Str --> int32) is native('ncurses') { * }
tgetnum('');
}
}
}
( $library, $version )
}
# Embedded typedefs here
#
# my class histdata_t is repr('CPointer') { }; # typedef char *histdata_t;
my class time_t is repr('CPointer') { }; # XXX probably already a native type.
my class Keymap is repr('CPointer') { }; # typedef KEYMAP_ENTRY *Keymap;
# typedef void rl_vcpfunc_t (char *);
#my class rl_command_func_t is repr('CPointer') { }; #typedef int rl_command_func_t (int, int);
my class rl_compentry_func_t is repr('CPointer') { }; #typedef char *rl_compentry_func_t (const char *, int);
constant meta_character_threshold = 0x07f; # Larger than this is Meta.
constant meta_character_bit = 0x080; # x0000000, must be on.
constant largest_char = 255; # Largest character value.
sub META_CHAR( $c ) {
ord( $c ) > meta_character_threshold && ord( $c ) <= largest_char
}
sub META( $c ) {
ord( $c ) | meta_character_bit
}
sub UNMETA( $c ) {
ord( $c ) & ~meta_character_bit
}
############################################################################
#
# history.h -- the names of functions that you can call in history.
#
# The structure used to store a history entry.
#
my class HIST_ENTRY is repr('CStruct') {
has Str $.line; # char *line;
has Str $.timestamp; # char *timestamp;
# has histdata_t $.data; # histdata_t data;
has Str $.data; # histdata_t is really char*
}
# Size of the history-library-managed space in history entry HS.
#
sub HISTENT_BYTES( $hs ) {
$hs.line.length + $hs.timestamp.length
}
# A structure used to pass the current state of the history stuff around.
#
my class HISTORY_STATE is repr('CStruct') {
has Pointer $.entries; # Pointer to an array of HIST_ENTRY types.
has int $.offset; # The location pointer within this array.
has int $.length; # Number of elements within this array.
has int $.size; # Number of slots allocated to this array.
has int $.flags;
}
# Flag values for the `flags' member of HISTORY_STATE.
#
constant HS_STIFLED = 0x01;
# Initialization and state management.
#
# Begin a session in which the history functions might be used. This
# just initializes the interactive variables.
#
sub using_history( )
is native( LIBREADLINE ) { * }
method using-history( ) {
using_history() }
# Return the current HISTORY_STATE of the history.
#
sub history_get_history_state( )
returns HISTORY_STATE
is native( LIBREADLINE ) { * }
method history-get-history-state( )
returns HISTORY_STATE {
history_get_history_state() }
# Set the state of the current history array to STATE.
#
sub history_set_history_state( HISTORY_STATE )
is native( LIBREADLINE ) { * }
method history-set-history-state( HISTORY_STATE $state ) {
history_set_history_state( $state ) }
# Manage the history list.
#
# Place STRING at the end of the history list.
# The associated data field (if any) is set to NULL.
#
sub add_history( Str ) is export
is native( LIBREADLINE ) { * }
method add-history( Str $history ) {
add_history( $history ) }
# Change the timestamp associated with the most recent history entry to
# STRING.
#
sub add_history_time( Str )
is native( LIBREADLINE ) { * }
method add-history-time( Str $timestamp ) {
add_history_time( $timestamp ) }
# A reasonably useless function, only here for completeness. WHICH
# is the magic number that tells us which element to delete. The
# elements are numbered from 0.
#
sub remove_history( int32 )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
method remove-history( int32 $which )
returns HIST_ENTRY {
remove_history( $which ) }
# Free the history entry H and return any application-specific data
# associated with it.
#
sub free_history_entry( HIST_ENTRY )
# returns histdata_t
returns Str
is native( LIBREADLINE ) { * }
method free-history-entry( HIST_ENTRY $entry )
# returns histdata_t {
returns Str {
free_history_entry( $entry ) }
# sub replace_history_entry( int32, Str, histdata_t )
sub replace_history_entry( int32, Str, Str )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
# method replace-history-entry( int32 $which, Str $line, histdata_t $data )
method replace-history-entry( int32 $which, Str $line, Str $data )
returns HIST_ENTRY {
replace_history_entry( $which, $line, $data ) }
sub clear_history( )
is native( LIBREADLINE ) { * }
method clear-history( ) {
clear_history() }
sub stifle_history( int32 )
is native( LIBREADLINE ) { * }
method stifle-history( int32 $max ) {
stifle_history( $max ) }
sub unstifle_history( )
is native( LIBREADLINE ) { * }
method unstifle-history( ) {
unstifle_history() }
sub history_is_stifled( )
returns int32
is native( LIBREADLINE ) { * }
method history-is-stifled( )
returns Bool {
history_is_stifled() ?? True !! False }
# Information about the history list.
#
sub history_list( )
returns CArray[HIST_ENTRY]
is native( LIBREADLINE ) { * }
method history-list( )
returns CArray[HIST_ENTRY] {
history_list() }
sub where_history( )
returns int32
is native( LIBREADLINE ) { * }
method where-history( )
returns int32 {
where_history() }
sub current_history( int32 )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
method current-history( int32 $which )
returns HIST_ENTRY {
current_history( $which ) }
sub history_get( int32 )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
method history-get( int32 $which )
returns HIST_ENTRY {
history_get( $which ) }
sub history_get_time( HIST_ENTRY )
returns time_t
is native( LIBREADLINE ) { * }
method history-get-time( HIST_ENTRY $h )
returns time_t {
history_get_time( $h ) }
sub history_total_bytes( )
returns int32
is native( LIBREADLINE ) { * }
method history-total-bytes( )
returns int32 {
history_total_bytes( ) }
# Moving around the history list.
#
sub history_set_pos( int32 )
returns int32
is native( LIBREADLINE ) { * }
method history-set-pos( int32 $pos )
returns int32 {
history_set_pos( $pos ) }
sub previous_history( )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
method previous-history( )
returns HIST_ENTRY {
previous_history( ) }
sub next_history( )
returns HIST_ENTRY
is native( LIBREADLINE ) { * }
method next-history( )
returns HIST_ENTRY {
next_history( ) }
# Searching the history list.
#
sub history_search( Str, int32 )
returns int32
is native( LIBREADLINE ) { * }
method history-search( Str $text, int32 $pos )
returns int32 {
history_search( $text, $pos ) }
sub history_search_prefix( Str, int32 )
returns int32
is native( LIBREADLINE ) { * }
method history-search-prefix( Str $text, int32 $pos )
returns int32 {
history_search_prefix( $text, $pos ) }
sub history_search_pos( Str, int32, int32 )
returns int32
is native( LIBREADLINE ) { * }
method history-search-pos( Str $text, int32 $pos, int32 $dir )
returns int32 {
history_search_pos( $text, $pos, $dir ) }
# Managing the history file.
#
sub read_history( Str )
returns int32
is native( LIBREADLINE ) { * }
method read-history( Str $text )
returns int32 {
my $rv = read_history( $text );
$rv == 0 ?? True !! $rv }
sub read_history_range( Str, int32, int32 )
returns int32
is native( LIBREADLINE ) { * }
method read-history-range( Str $text, int32 $from, int32 $to )
returns int32 {
read_history_range( $text, $from, $to ) }
sub write_history( Str )
returns int32
is native( LIBREADLINE ) { * }
method write-history( Str $filename )
returns int32 {
my $rv = write_history( $filename );
$rv == 0 ?? True !! $rv }
sub append_history( int32, Str )
returns int32