Revert "Sync readline/ to version 7.0 alpha"
[deliverable/binutils-gdb.git] / readline / doc / rltech.texi
CommitLineData
b585a9fa
EZ
1@comment %**start of header (This is for running Texinfo on a region.)
2@setfilename rltech.info
3@comment %**end of header (This is for running Texinfo on a region.)
b585a9fa
EZ
4
5@ifinfo
6This document describes the GNU Readline Library, a utility for aiding
7in the consistency of user interface across discrete programs that need
8to provide a command line interface.
9
5836a818 10Copyright (C) 1988--2011 Free Software Foundation, Inc.
b585a9fa
EZ
11
12Permission is granted to make and distribute verbatim copies of
13this manual provided the copyright notice and this permission notice
14pare preserved on all copies.
15
16@ignore
17Permission is granted to process this file through TeX and print the
18results, provided the printed document carries copying permission
19notice identical to this one except for the removal of this paragraph
20(this paragraph not being relevant to the printed manual).
21@end ignore
22
23Permission is granted to copy and distribute modified versions of this
24manual under the conditions for verbatim copying, provided that the entire
25resulting derived work is distributed under the terms of a permission
26notice identical to this one.
27
28Permission is granted to copy and distribute translations of this manual
29into another language, under the above conditions for modified versions,
30except that this permission notice may be stated in a translation approved
31by the Foundation.
32@end ifinfo
33
34@node Programming with GNU Readline
35@chapter Programming with GNU Readline
36
37This chapter describes the interface between the @sc{gnu} Readline Library and
38other programs. If you are a programmer, and you wish to include the
39features found in @sc{gnu} Readline
40such as completion, line editing, and interactive history manipulation
41in your own programs, this section is for you.
42
43@menu
44* Basic Behavior:: Using the default behavior of Readline.
45* Custom Functions:: Adding your own functions to Readline.
46* Readline Variables:: Variables accessible to custom
47 functions.
48* Readline Convenience Functions:: Functions which Readline supplies to
49 aid in writing your own custom
50 functions.
51* Readline Signal Handling:: How Readline behaves when it receives signals.
52* Custom Completers:: Supplanting or supplementing Readline's
53 completion functions.
54@end menu
55
56@node Basic Behavior
57@section Basic Behavior
58
59Many programs provide a command line interface, such as @code{mail},
60@code{ftp}, and @code{sh}. For such programs, the default behaviour of
61Readline is sufficient. This section describes how to use Readline in
62the simplest way possible, perhaps to replace calls in your code to
63@code{gets()} or @code{fgets()}.
64
65@findex readline
66@cindex readline, function
67
68The function @code{readline()} prints a prompt @var{prompt}
69and then reads and returns a single line of text from the user.
70If @var{prompt} is @code{NULL} or the empty string, no prompt is displayed.
71The line @code{readline} returns is allocated with @code{malloc()};
72the caller should @code{free()} the line when it has finished with it.
73The declaration for @code{readline} in ANSI C is
74
75@example
76@code{char *readline (const char *@var{prompt});}
77@end example
78
79@noindent
80So, one might say
81@example
82@code{char *line = readline ("Enter a line: ");}
83@end example
84@noindent
85in order to read a line of text from the user.
86The line returned has the final newline removed, so only the
87text remains.
88
89If @code{readline} encounters an @code{EOF} while reading the line, and the
90line is empty at that point, then @code{(char *)NULL} is returned.
91Otherwise, the line is ended just as if a newline had been typed.
92
93If you want the user to be able to get at the line later, (with
94@key{C-p} for example), you must call @code{add_history()} to save the
95line away in a @dfn{history} list of such lines.
96
97@example
98@code{add_history (line)};
99@end example
100
101@noindent
102For full details on the GNU History Library, see the associated manual.
103
104It is preferable to avoid saving empty lines on the history list, since
105users rarely have a burning need to reuse a blank line. Here is
106a function which usefully replaces the standard @code{gets()} library
107function, and has the advantage of no static buffer to overflow:
108
109@example
110/* A static variable for holding the line. */
111static char *line_read = (char *)NULL;
112
113/* Read a string, and return a pointer to it.
114 Returns NULL on EOF. */
115char *
116rl_gets ()
117@{
118 /* If the buffer has already been allocated,
119 return the memory to the free pool. */
120 if (line_read)
121 @{
122 free (line_read);
123 line_read = (char *)NULL;
124 @}
125
126 /* Get a line from the user. */
127 line_read = readline ("");
128
129 /* If the line has any text in it,
130 save it on the history. */
131 if (line_read && *line_read)
132 add_history (line_read);
133
134 return (line_read);
135@}
136@end example
137
138This function gives the user the default behaviour of @key{TAB}
139completion: completion on file names. If you do not want Readline to
140complete on filenames, you can change the binding of the @key{TAB} key
141with @code{rl_bind_key()}.
142
143@example
144@code{int rl_bind_key (int @var{key}, rl_command_func_t *@var{function});}
145@end example
146
147@code{rl_bind_key()} takes two arguments: @var{key} is the character that
148you want to bind, and @var{function} is the address of the function to
149call when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert()}
150makes @key{TAB} insert itself.
151@code{rl_bind_key()} returns non-zero if @var{key} is not a valid
152ASCII character code (between 0 and 255).
153
154Thus, to disable the default @key{TAB} behavior, the following suffices:
155@example
156@code{rl_bind_key ('\t', rl_insert);}
157@end example
158
159This code should be executed once at the start of your program; you
160might write a function called @code{initialize_readline()} which
161performs this and other desired initializations, such as installing
162custom completers (@pxref{Custom Completers}).
163
164@node Custom Functions
165@section Custom Functions
166
167Readline provides many functions for manipulating the text of
168the line, but it isn't possible to anticipate the needs of all
169programs. This section describes the various functions and variables
170defined within the Readline library which allow a user program to add
171customized functionality to Readline.
172
173Before declaring any functions that customize Readline's behavior, or
174using any functionality Readline provides in other code, an
175application writer should include the file @code{<readline/readline.h>}
176in any file that uses Readline's features. Since some of the definitions
177in @code{readline.h} use the @code{stdio} library, the file
178@code{<stdio.h>} should be included before @code{readline.h}.
179
180@code{readline.h} defines a C preprocessor variable that should
181be treated as an integer, @code{RL_READLINE_VERSION}, which may
182be used to conditionally compile application code depending on
183the installed Readline version. The value is a hexadecimal
184encoding of the major and minor version numbers of the library,
185of the form 0x@var{MMmm}. @var{MM} is the two-digit major
186version number; @var{mm} is the two-digit minor version number.
187For Readline 4.2, for example, the value of
188@code{RL_READLINE_VERSION} would be @code{0x0402}.
189
190@menu
191* Readline Typedefs:: C declarations to make code readable.
192* Function Writing:: Variables and calling conventions.
193@end menu
194
195@node Readline Typedefs
196@subsection Readline Typedefs
197
5836a818 198For readabilty, we declare a number of new object types, all pointers
b585a9fa
EZ
199to functions.
200
201The reason for declaring these new types is to make it easier to write
202code describing pointers to C functions with appropriately prototyped
203arguments and return values.
204
205For instance, say we want to declare a variable @var{func} as a pointer
206to a function which takes two @code{int} arguments and returns an
207@code{int} (this is the type of all of the Readline bindable functions).
208Instead of the classic C declaration
209
210@code{int (*func)();}
211
212@noindent
213or the ANSI-C style declaration
214
215@code{int (*func)(int, int);}
216
217@noindent
218we may write
219
220@code{rl_command_func_t *func;}
221
222The full list of function pointer types available is
223
224@table @code
225@item typedef int rl_command_func_t (int, int);
226
227@item typedef char *rl_compentry_func_t (const char *, int);
228
229@item typedef char **rl_completion_func_t (const char *, int, int);
230
231@item typedef char *rl_quote_func_t (char *, int, char *);
232
233@item typedef char *rl_dequote_func_t (char *, int);
234
235@item typedef int rl_compignore_func_t (char **);
236
237@item typedef void rl_compdisp_func_t (char **, int, int);
238
239@item typedef int rl_hook_func_t (void);
240
241@item typedef int rl_getc_func_t (FILE *);
242
243@item typedef int rl_linebuf_func_t (char *, int);
244
245@item typedef int rl_intfunc_t (int);
246@item #define rl_ivoidfunc_t rl_hook_func_t
247@item typedef int rl_icpfunc_t (char *);
248@item typedef int rl_icppfunc_t (char **);
249
250@item typedef void rl_voidfunc_t (void);
251@item typedef void rl_vintfunc_t (int);
252@item typedef void rl_vcpfunc_t (char *);
253@item typedef void rl_vcppfunc_t (char **);
254
255@end table
256
257@node Function Writing
258@subsection Writing a New Function
259
260In order to write new functions for Readline, you need to know the
261calling conventions for keyboard-invoked functions, and the names of the
262variables that describe the current state of the line read so far.
263
264The calling sequence for a command @code{foo} looks like
265
266@example
267@code{int foo (int count, int key)}
268@end example
269
270@noindent
271where @var{count} is the numeric argument (or 1 if defaulted) and
272@var{key} is the key that invoked this function.
273
274It is completely up to the function as to what should be done with the
275numeric argument. Some functions use it as a repeat count, some
276as a flag, and others to choose alternate behavior (refreshing the current
277line as opposed to refreshing the screen, for example). Some choose to
278ignore it. In general, if a
279function uses the numeric argument as a repeat count, it should be able
280to do something useful with both negative and positive arguments.
281At the very least, it should be aware that it can be passed a
282negative argument.
283
284A command function should return 0 if its action completes successfully,
5836a818 285and a non-zero value if some error occurs.
b585a9fa
EZ
286This is the convention obeyed by all of the builtin Readline bindable
287command functions.
288
289@node Readline Variables
290@section Readline Variables
291
292These variables are available to function writers.
293
294@deftypevar {char *} rl_line_buffer
295This is the line gathered so far. You are welcome to modify the
296contents of the line, but see @ref{Allowing Undoing}. The
297function @code{rl_extend_line_buffer} is available to increase
298the memory allocated to @code{rl_line_buffer}.
299@end deftypevar
300
301@deftypevar int rl_point
302The offset of the current cursor position in @code{rl_line_buffer}
303(the @emph{point}).
304@end deftypevar
305
306@deftypevar int rl_end
307The number of characters present in @code{rl_line_buffer}. When
308@code{rl_point} is at the end of the line, @code{rl_point} and
309@code{rl_end} are equal.
310@end deftypevar
311
312@deftypevar int rl_mark
313The @var{mark} (saved position) in the current line. If set, the mark
314and point define a @emph{region}.
315@end deftypevar
316
317@deftypevar int rl_done
318Setting this to a non-zero value causes Readline to return the current
319line immediately.
320@end deftypevar
321
322@deftypevar int rl_num_chars_to_read
323Setting this to a positive value before calling @code{readline()} causes
324Readline to return after accepting that many characters, rather
325than reading up to a character bound to @code{accept-line}.
326@end deftypevar
327
328@deftypevar int rl_pending_input
329Setting this to a value makes it the next keystroke read. This is a
330way to stuff a single character into the input stream.
331@end deftypevar
332
333@deftypevar int rl_dispatching
334Set to a non-zero value if a function is being called from a key binding;
335zero otherwise. Application functions can test this to discover whether
336they were called directly or by Readline's dispatching mechanism.
337@end deftypevar
338
339@deftypevar int rl_erase_empty_line
340Setting this to a non-zero value causes Readline to completely erase
341the current line, including any prompt, any time a newline is typed as
342the only character on an otherwise-empty line. The cursor is moved to
343the beginning of the newly-blank line.
344@end deftypevar
345
346@deftypevar {char *} rl_prompt
347The prompt Readline uses. This is set from the argument to
348@code{readline()}, and should not be assigned to directly.
349The @code{rl_set_prompt()} function (@pxref{Redisplay}) may
350be used to modify the prompt string after calling @code{readline()}.
351@end deftypevar
352
cc88a640
JK
353@deftypevar {char *} rl_display_prompt
354The string displayed as the prompt. This is usually identical to
355@var{rl_prompt}, but may be changed temporarily by functions that
356use the prompt string as a message area, such as incremental search.
357@end deftypevar
358
b585a9fa
EZ
359@deftypevar int rl_already_prompted
360If an application wishes to display the prompt itself, rather than have
361Readline do it the first time @code{readline()} is called, it should set
362this variable to a non-zero value after displaying the prompt.
363The prompt must also be passed as the argument to @code{readline()} so
364the redisplay functions can update the display properly.
365The calling application is responsible for managing the value; Readline
366never sets it.
367@end deftypevar
368
369@deftypevar {const char *} rl_library_version
370The version number of this revision of the library.
371@end deftypevar
372
373@deftypevar int rl_readline_version
374An integer encoding the current version of the library. The encoding is
375of the form 0x@var{MMmm}, where @var{MM} is the two-digit major version
376number, and @var{mm} is the two-digit minor version number.
377For example, for Readline-4.2, @code{rl_readline_version} would have the
378value 0x0402.
379@end deftypevar
380
381@deftypevar {int} rl_gnu_readline_p
382Always set to 1, denoting that this is @sc{gnu} readline rather than some
383emulation.
384@end deftypevar
385
386@deftypevar {const char *} rl_terminal_name
387The terminal type, used for initialization. If not set by the application,
388Readline sets this to the value of the @env{TERM} environment variable
389the first time it is called.
390@end deftypevar
391
392@deftypevar {const char *} rl_readline_name
393This variable is set to a unique name by each application using Readline.
394The value allows conditional parsing of the inputrc file
395(@pxref{Conditional Init Constructs}).
396@end deftypevar
397
398@deftypevar {FILE *} rl_instream
399The stdio stream from which Readline reads input.
400If @code{NULL}, Readline defaults to @var{stdin}.
401@end deftypevar
402
403@deftypevar {FILE *} rl_outstream
404The stdio stream to which Readline performs output.
405If @code{NULL}, Readline defaults to @var{stdout}.
406@end deftypevar
407
408@deftypevar int rl_prefer_env_winsize
409If non-zero, Readline gives values found in the @env{LINES} and
410@env{COLUMNS} environment variables greater precedence than values fetched
411from the kernel when computing the screen dimensions.
412@end deftypevar
413
414@deftypevar {rl_command_func_t *} rl_last_func
415The address of the last command function Readline executed. May be used to
416test whether or not a function is being executed twice in succession, for
417example.
418@end deftypevar
419
420@deftypevar {rl_hook_func_t *} rl_startup_hook
421If non-zero, this is the address of a function to call just
422before @code{readline} prints the first prompt.
423@end deftypevar
424
425@deftypevar {rl_hook_func_t *} rl_pre_input_hook
426If non-zero, this is the address of a function to call after
427the first prompt has been printed and just before @code{readline}
428starts reading input characters.
429@end deftypevar
430
431@deftypevar {rl_hook_func_t *} rl_event_hook
432If non-zero, this is the address of a function to call periodically
433when Readline is waiting for terminal input.
434By default, this will be called at most ten times a second if there
435is no keyboard input.
436@end deftypevar
437
438@deftypevar {rl_getc_func_t *} rl_getc_function
439If non-zero, Readline will call indirectly through this pointer
440to get a character from the input stream. By default, it is set to
441@code{rl_getc}, the default Readline character input function
442(@pxref{Character Input}).
443@end deftypevar
444
445@deftypevar {rl_voidfunc_t *} rl_redisplay_function
446If non-zero, Readline will call indirectly through this pointer
447to update the display with the current contents of the editing buffer.
448By default, it is set to @code{rl_redisplay}, the default Readline
449redisplay function (@pxref{Redisplay}).
450@end deftypevar
451
452@deftypevar {rl_vintfunc_t *} rl_prep_term_function
453If non-zero, Readline will call indirectly through this pointer
454to initialize the terminal. The function takes a single argument, an
455@code{int} flag that says whether or not to use eight-bit characters.
456By default, this is set to @code{rl_prep_terminal}
457(@pxref{Terminal Management}).
458@end deftypevar
459
460@deftypevar {rl_voidfunc_t *} rl_deprep_term_function
461If non-zero, Readline will call indirectly through this pointer
462to reset the terminal. This function should undo the effects of
463@code{rl_prep_term_function}.
464By default, this is set to @code{rl_deprep_terminal}
465(@pxref{Terminal Management}).
466@end deftypevar
467
468@deftypevar {Keymap} rl_executing_keymap
469This variable is set to the keymap (@pxref{Keymaps}) in which the
470currently executing readline function was found.
471@end deftypevar
472
473@deftypevar {Keymap} rl_binding_keymap
474This variable is set to the keymap (@pxref{Keymaps}) in which the
475last key binding occurred.
476@end deftypevar
477
478@deftypevar {char *} rl_executing_macro
479This variable is set to the text of any currently-executing macro.
480@end deftypevar
481
482@deftypevar {int} rl_readline_state
483A variable with bit values that encapsulate the current Readline state.
484A bit is set with the @code{RL_SETSTATE} macro, and unset with the
485@code{RL_UNSETSTATE} macro. Use the @code{RL_ISSTATE} macro to test
486whether a particular state bit is set. Current state bits include:
487
488@table @code
489@item RL_STATE_NONE
5836a818 490Readline has not yet been called, nor has it begun to intialize.
b585a9fa
EZ
491@item RL_STATE_INITIALIZING
492Readline is initializing its internal data structures.
493@item RL_STATE_INITIALIZED
494Readline has completed its initialization.
495@item RL_STATE_TERMPREPPED
496Readline has modified the terminal modes to do its own input and redisplay.
497@item RL_STATE_READCMD
498Readline is reading a command from the keyboard.
499@item RL_STATE_METANEXT
500Readline is reading more input after reading the meta-prefix character.
501@item RL_STATE_DISPATCHING
502Readline is dispatching to a command.
503@item RL_STATE_MOREINPUT
504Readline is reading more input while executing an editing command.
505@item RL_STATE_ISEARCH
506Readline is performing an incremental history search.
507@item RL_STATE_NSEARCH
508Readline is performing a non-incremental history search.
509@item RL_STATE_SEARCH
510Readline is searching backward or forward through the history for a string.
511@item RL_STATE_NUMERICARG
512Readline is reading a numeric argument.
513@item RL_STATE_MACROINPUT
514Readline is currently getting its input from a previously-defined keyboard
515macro.
516@item RL_STATE_MACRODEF
517Readline is currently reading characters defining a keyboard macro.
518@item RL_STATE_OVERWRITE
519Readline is in overwrite mode.
520@item RL_STATE_COMPLETING
521Readline is performing word completion.
522@item RL_STATE_SIGHANDLER
523Readline is currently executing the readline signal handler.
524@item RL_STATE_UNDOING
525Readline is performing an undo.
cc88a640
JK
526@item RL_STATE_INPUTPENDING
527Readline has input pending due to a call to @code{rl_execute_next()}.
528@item RL_STATE_TTYCSAVED
529Readline has saved the values of the terminal's special characters.
530@item RL_STATE_CALLBACK
531Readline is currently using the alternate (callback) interface
532(@pxref{Alternate Interface}).
533@item RL_STATE_VIMOTION
534Readline is reading the argument to a vi-mode "motion" command.
535@item RL_STATE_MULTIKEY
536Readline is reading a multiple-keystroke command.
537@item RL_STATE_VICMDONCE
538Readline has entered vi command (movement) mode at least one time during
539the current call to @code{readline()}.
b585a9fa
EZ
540@item RL_STATE_DONE
541Readline has read a key sequence bound to @code{accept-line}
542and is about to return the line to the caller.
543@end table
544
545@end deftypevar
546
547@deftypevar {int} rl_explicit_arg
548Set to a non-zero value if an explicit numeric argument was specified by
549the user. Only valid in a bindable command function.
550@end deftypevar
551
552@deftypevar {int} rl_numeric_arg
553Set to the value of any numeric argument explicitly specified by the user
554before executing the current Readline function. Only valid in a bindable
555command function.
556@end deftypevar
557
558@deftypevar {int} rl_editing_mode
559Set to a value denoting Readline's current editing mode. A value of
560@var{1} means Readline is currently in emacs mode; @var{0}
561means that vi mode is active.
562@end deftypevar
563
564
565@node Readline Convenience Functions
566@section Readline Convenience Functions
567
568@menu
569* Function Naming:: How to give a function you write a name.
570* Keymaps:: Making keymaps.
571* Binding Keys:: Changing Keymaps.
572* Associating Function Names and Bindings:: Translate function names to
573 key sequences.
574* Allowing Undoing:: How to make your functions undoable.
575* Redisplay:: Functions to control line display.
576* Modifying Text:: Functions to modify @code{rl_line_buffer}.
577* Character Input:: Functions to read keyboard input.
578* Terminal Management:: Functions to manage terminal settings.
579* Utility Functions:: Generally useful functions and hooks.
580* Miscellaneous Functions:: Functions that don't fall into any category.
581* Alternate Interface:: Using Readline in a `callback' fashion.
582* A Readline Example:: An example Readline function.
583@end menu
584
585@node Function Naming
586@subsection Naming a Function
587
588The user can dynamically change the bindings of keys while using
589Readline. This is done by representing the function with a descriptive
590name. The user is able to type the descriptive name when referring to
591the function. Thus, in an init file, one might find
592
593@example
594Meta-Rubout: backward-kill-word
595@end example
596
597This binds the keystroke @key{Meta-Rubout} to the function
598@emph{descriptively} named @code{backward-kill-word}. You, as the
599programmer, should bind the functions you write to descriptive names as
600well. Readline provides a function for doing that:
601
602@deftypefun int rl_add_defun (const char *name, rl_command_func_t *function, int key)
603Add @var{name} to the list of named functions. Make @var{function} be
604the function that gets called. If @var{key} is not -1, then bind it to
605@var{function} using @code{rl_bind_key()}.
606@end deftypefun
607
608Using this function alone is sufficient for most applications.
609It is the recommended way to add a few functions to the default
610functions that Readline has built in.
611If you need to do something other than adding a function to Readline,
612you may need to use the underlying functions described below.
613
614@node Keymaps
615@subsection Selecting a Keymap
616
617Key bindings take place on a @dfn{keymap}. The keymap is the
618association between the keys that the user types and the functions that
619get run. You can make your own keymaps, copy existing keymaps, and tell
620Readline which keymap to use.
621
622@deftypefun Keymap rl_make_bare_keymap (void)
623Returns a new, empty keymap. The space for the keymap is allocated with
624@code{malloc()}; the caller should free it by calling
cc88a640 625@code{rl_free_keymap()} when done.
b585a9fa
EZ
626@end deftypefun
627
628@deftypefun Keymap rl_copy_keymap (Keymap map)
629Return a new keymap which is a copy of @var{map}.
630@end deftypefun
631
632@deftypefun Keymap rl_make_keymap (void)
633Return a new keymap with the printing characters bound to rl_insert,
634the lowercase Meta characters bound to run their equivalents, and
635the Meta digits bound to produce numeric arguments.
636@end deftypefun
637
638@deftypefun void rl_discard_keymap (Keymap keymap)
cc88a640
JK
639Free the storage associated with the data in @var{keymap}.
640The caller should free @var{keymap}.
641@end deftypefun
642
643@deftypefun void rl_free_keymap (Keymap keymap)
644Free all storage associated with @var{keymap}. This calls
645@code{rl_discard_keymap} to free subordindate keymaps and macros.
b585a9fa
EZ
646@end deftypefun
647
648Readline has several internal keymaps. These functions allow you to
649change which keymap is active.
650
651@deftypefun Keymap rl_get_keymap (void)
652Returns the currently active keymap.
653@end deftypefun
654
655@deftypefun void rl_set_keymap (Keymap keymap)
656Makes @var{keymap} the currently active keymap.
657@end deftypefun
658
659@deftypefun Keymap rl_get_keymap_by_name (const char *name)
660Return the keymap matching @var{name}. @var{name} is one which would
661be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
662@end deftypefun
663
664@deftypefun {char *} rl_get_keymap_name (Keymap keymap)
665Return the name matching @var{keymap}. @var{name} is one which would
666be supplied in a @code{set keymap} inputrc line (@pxref{Readline Init File}).
667@end deftypefun
668
669@node Binding Keys
670@subsection Binding Keys
671
672Key sequences are associate with functions through the keymap.
673Readline has several internal keymaps: @code{emacs_standard_keymap},
674@code{emacs_meta_keymap}, @code{emacs_ctlx_keymap},
675@code{vi_movement_keymap}, and @code{vi_insertion_keymap}.
676@code{emacs_standard_keymap} is the default, and the examples in
677this manual assume that.
678
679Since @code{readline()} installs a set of default key bindings the first
680time it is called, there is always the danger that a custom binding
681installed before the first call to @code{readline()} will be overridden.
682An alternate mechanism is to install custom key bindings in an
683initialization function assigned to the @code{rl_startup_hook} variable
684(@pxref{Readline Variables}).
685
686These functions manage key bindings.
687
688@deftypefun int rl_bind_key (int key, rl_command_func_t *function)
689Binds @var{key} to @var{function} in the currently active keymap.
690Returns non-zero in the case of an invalid @var{key}.
691@end deftypefun
692
693@deftypefun int rl_bind_key_in_map (int key, rl_command_func_t *function, Keymap map)
694Bind @var{key} to @var{function} in @var{map}.
695Returns non-zero in the case of an invalid @var{key}.
696@end deftypefun
697
698@deftypefun int rl_bind_key_if_unbound (int key, rl_command_func_t *function)
699Binds @var{key} to @var{function} if it is not already bound in the
700currently active keymap.
701Returns non-zero in the case of an invalid @var{key} or if @var{key} is
702already bound.
703@end deftypefun
704
705@deftypefun int rl_bind_key_if_unbound_in_map (int key, rl_command_func_t *function, Keymap map)
706Binds @var{key} to @var{function} if it is not already bound in @var{map}.
707Returns non-zero in the case of an invalid @var{key} or if @var{key} is
708already bound.
709@end deftypefun
710
711@deftypefun int rl_unbind_key (int key)
712Bind @var{key} to the null function in the currently active keymap.
713Returns non-zero in case of error.
714@end deftypefun
715
716@deftypefun int rl_unbind_key_in_map (int key, Keymap map)
717Bind @var{key} to the null function in @var{map}.
718Returns non-zero in case of error.
719@end deftypefun
720
721@deftypefun int rl_unbind_function_in_map (rl_command_func_t *function, Keymap map)
722Unbind all keys that execute @var{function} in @var{map}.
723@end deftypefun
724
725@deftypefun int rl_unbind_command_in_map (const char *command, Keymap map)
726Unbind all keys that are bound to @var{command} in @var{map}.
727@end deftypefun
728
729@deftypefun int rl_bind_keyseq (const char *keyseq, rl_command_func_t *function)
730Bind the key sequence represented by the string @var{keyseq} to the function
731@var{function}, beginning in the current keymap.
732This makes new keymaps as necessary.
733The return value is non-zero if @var{keyseq} is invalid.
734@end deftypefun
735
736@deftypefun int rl_bind_keyseq_in_map (const char *keyseq, rl_command_func_t *function, Keymap map)
737Bind the key sequence represented by the string @var{keyseq} to the function
738@var{function}. This makes new keymaps as necessary.
739Initial bindings are performed in @var{map}.
740The return value is non-zero if @var{keyseq} is invalid.
741@end deftypefun
742
743@deftypefun int rl_set_key (const char *keyseq, rl_command_func_t *function, Keymap map)
744Equivalent to @code{rl_bind_keyseq_in_map}.
745@end deftypefun
746
747@deftypefun int rl_bind_keyseq_if_unbound (const char *keyseq, rl_command_func_t *function)
748Binds @var{keyseq} to @var{function} if it is not already bound in the
749currently active keymap.
750Returns non-zero in the case of an invalid @var{keyseq} or if @var{keyseq} is
751already bound.
752@end deftypefun
753
754@deftypefun int rl_bind_keyseq_if_unbound_in_map (const char *keyseq, rl_command_func_t *function, Keymap map)
755Binds @var{keyseq} to @var{function} if it is not already bound in @var{map}.
756Returns non-zero in the case of an invalid @var{keyseq} or if @var{keyseq} is
757already bound.
758@end deftypefun
759
760@deftypefun int rl_generic_bind (int type, const char *keyseq, char *data, Keymap map)
761Bind the key sequence represented by the string @var{keyseq} to the arbitrary
762pointer @var{data}. @var{type} says what kind of data is pointed to by
763@var{data}; this can be a function (@code{ISFUNC}), a macro
764(@code{ISMACR}), or a keymap (@code{ISKMAP}). This makes new keymaps as
765necessary. The initial keymap in which to do bindings is @var{map}.
766@end deftypefun
767
768@deftypefun int rl_parse_and_bind (char *line)
769Parse @var{line} as if it had been read from the @code{inputrc} file and
770perform any key bindings and variable assignments found
771(@pxref{Readline Init File}).
772@end deftypefun
773
774@deftypefun int rl_read_init_file (const char *filename)
775Read keybindings and variable assignments from @var{filename}
776(@pxref{Readline Init File}).
777@end deftypefun
778
779@node Associating Function Names and Bindings
780@subsection Associating Function Names and Bindings
781
782These functions allow you to find out what keys invoke named functions
783and the functions invoked by a particular key sequence. You may also
784associate a new function name with an arbitrary function.
785
786@deftypefun {rl_command_func_t *} rl_named_function (const char *name)
787Return the function with name @var{name}.
788@end deftypefun
789
790@deftypefun {rl_command_func_t *} rl_function_of_keyseq (const char *keyseq, Keymap map, int *type)
791Return the function invoked by @var{keyseq} in keymap @var{map}.
792If @var{map} is @code{NULL}, the current keymap is used. If @var{type} is
793not @code{NULL}, the type of the object is returned in the @code{int} variable
794it points to (one of @code{ISFUNC}, @code{ISKMAP}, or @code{ISMACR}).
795@end deftypefun
796
797@deftypefun {char **} rl_invoking_keyseqs (rl_command_func_t *function)
798Return an array of strings representing the key sequences used to
799invoke @var{function} in the current keymap.
800@end deftypefun
801
802@deftypefun {char **} rl_invoking_keyseqs_in_map (rl_command_func_t *function, Keymap map)
803Return an array of strings representing the key sequences used to
804invoke @var{function} in the keymap @var{map}.
805@end deftypefun
806
807@deftypefun void rl_function_dumper (int readable)
808Print the readline function names and the key sequences currently
809bound to them to @code{rl_outstream}. If @var{readable} is non-zero,
810the list is formatted in such a way that it can be made part of an
811@code{inputrc} file and re-read.
812@end deftypefun
813
814@deftypefun void rl_list_funmap_names (void)
815Print the names of all bindable Readline functions to @code{rl_outstream}.
816@end deftypefun
817
818@deftypefun {const char **} rl_funmap_names (void)
819Return a NULL terminated array of known function names. The array is
820sorted. The array itself is allocated, but not the strings inside. You
cc88a640
JK
821should free the array, but not the pointers, using @code{free} or
822@code{rl_free} when you are done.
b585a9fa
EZ
823@end deftypefun
824
825@deftypefun int rl_add_funmap_entry (const char *name, rl_command_func_t *function)
826Add @var{name} to the list of bindable Readline command names, and make
827@var{function} the function to be called when @var{name} is invoked.
828@end deftypefun
829
830@node Allowing Undoing
831@subsection Allowing Undoing
832
833Supporting the undo command is a painless thing, and makes your
834functions much more useful. It is certainly easy to try
835something if you know you can undo it.
836
837If your function simply inserts text once, or deletes text once, and
838uses @code{rl_insert_text()} or @code{rl_delete_text()} to do it, then
839undoing is already done for you automatically.
840
841If you do multiple insertions or multiple deletions, or any combination
842of these operations, you should group them together into one operation.
843This is done with @code{rl_begin_undo_group()} and
844@code{rl_end_undo_group()}.
845
846The types of events that can be undone are:
847
848@smallexample
849enum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @};
850@end smallexample
851
852Notice that @code{UNDO_DELETE} means to insert some text, and
853@code{UNDO_INSERT} means to delete some text. That is, the undo code
854tells what to undo, not how to undo it. @code{UNDO_BEGIN} and
855@code{UNDO_END} are tags added by @code{rl_begin_undo_group()} and
856@code{rl_end_undo_group()}.
857
858@deftypefun int rl_begin_undo_group (void)
859Begins saving undo information in a group construct. The undo
860information usually comes from calls to @code{rl_insert_text()} and
861@code{rl_delete_text()}, but could be the result of calls to
862@code{rl_add_undo()}.
863@end deftypefun
864
865@deftypefun int rl_end_undo_group (void)
866Closes the current undo group started with @code{rl_begin_undo_group
867()}. There should be one call to @code{rl_end_undo_group()}
868for each call to @code{rl_begin_undo_group()}.
869@end deftypefun
870
871@deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)
872Remember how to undo an event (according to @var{what}). The affected
873text runs from @var{start} to @var{end}, and encompasses @var{text}.
874@end deftypefun
875
876@deftypefun void rl_free_undo_list (void)
877Free the existing undo list.
878@end deftypefun
879
880@deftypefun int rl_do_undo (void)
881Undo the first thing on the undo list. Returns @code{0} if there was
882nothing to undo, non-zero if something was undone.
883@end deftypefun
884
885Finally, if you neither insert nor delete text, but directly modify the
886existing text (e.g., change its case), call @code{rl_modifying()}
887once, just before you modify the text. You must supply the indices of
888the text range that you are going to modify.
889
890@deftypefun int rl_modifying (int start, int end)
891Tell Readline to save the text between @var{start} and @var{end} as a
892single undo unit. It is assumed that you will subsequently modify
893that text.
894@end deftypefun
895
896@node Redisplay
897@subsection Redisplay
898
899@deftypefun void rl_redisplay (void)
900Change what's displayed on the screen to reflect the current contents
901of @code{rl_line_buffer}.
902@end deftypefun
903
904@deftypefun int rl_forced_update_display (void)
905Force the line to be updated and redisplayed, whether or not
906Readline thinks the screen display is correct.
907@end deftypefun
908
909@deftypefun int rl_on_new_line (void)
910Tell the update functions that we have moved onto a new (empty) line,
5836a818 911usually after ouputting a newline.
b585a9fa
EZ
912@end deftypefun
913
914@deftypefun int rl_on_new_line_with_prompt (void)
915Tell the update functions that we have moved onto a new line, with
916@var{rl_prompt} already displayed.
917This could be used by applications that want to output the prompt string
918themselves, but still need Readline to know the prompt string length for
919redisplay.
920It should be used after setting @var{rl_already_prompted}.
921@end deftypefun
922
923@deftypefun int rl_reset_line_state (void)
924Reset the display state to a clean state and redisplay the current line
925starting on a new line.
926@end deftypefun
927
928@deftypefun int rl_crlf (void)
929Move the cursor to the start of the next screen line.
930@end deftypefun
931
932@deftypefun int rl_show_char (int c)
933Display character @var{c} on @code{rl_outstream}.
934If Readline has not been set to display meta characters directly, this
935will convert meta characters to a meta-prefixed key sequence.
936This is intended for use by applications which wish to do their own
937redisplay.
938@end deftypefun
939
940@deftypefun int rl_message (const char *, @dots{})
941The arguments are a format string as would be supplied to @code{printf},
942possibly containing conversion specifications such as @samp{%d}, and
943any additional arguments necessary to satisfy the conversion specifications.
944The resulting string is displayed in the @dfn{echo area}. The echo area
945is also used to display numeric arguments and search strings.
946You should call @code{rl_save_prompt} to save the prompt information
947before calling this function.
948@end deftypefun
949
950@deftypefun int rl_clear_message (void)
951Clear the message in the echo area. If the prompt was saved with a call to
952@code{rl_save_prompt} before the last call to @code{rl_message},
953call @code{rl_restore_prompt} before calling this function.
954@end deftypefun
955
956@deftypefun void rl_save_prompt (void)
957Save the local Readline prompt display state in preparation for
958displaying a new message in the message area with @code{rl_message()}.
959@end deftypefun
960
961@deftypefun void rl_restore_prompt (void)
962Restore the local Readline prompt display state saved by the most
963recent call to @code{rl_save_prompt}.
964if @code{rl_save_prompt} was called to save the prompt before a call
965to @code{rl_message}, this function should be called before the
966corresponding call to @code{rl_clear_message}.
967@end deftypefun
968
969@deftypefun int rl_expand_prompt (char *prompt)
970Expand any special character sequences in @var{prompt} and set up the
971local Readline prompt redisplay variables.
972This function is called by @code{readline()}. It may also be called to
973expand the primary prompt if the @code{rl_on_new_line_with_prompt()}
974function or @code{rl_already_prompted} variable is used.
975It returns the number of visible characters on the last line of the
976(possibly multi-line) prompt.
977Applications may indicate that the prompt contains characters that take
978up no physical screen space when displayed by bracketing a sequence of
979such characters with the special markers @code{RL_PROMPT_START_IGNORE}
5836a818 980and @code{RL_PROMPT_END_IGNORE} (declared in @file{readline.h}. This may
b585a9fa
EZ
981be used to embed terminal-specific escape sequences in prompts.
982@end deftypefun
983
984@deftypefun int rl_set_prompt (const char *prompt)
985Make Readline use @var{prompt} for subsequent redisplay. This calls
986@code{rl_expand_prompt()} to expand the prompt and sets @code{rl_prompt}
987to the result.
988@end deftypefun
989
990@node Modifying Text
991@subsection Modifying Text
992
993@deftypefun int rl_insert_text (const char *text)
994Insert @var{text} into the line at the current cursor position.
995Returns the number of characters inserted.
996@end deftypefun
997
998@deftypefun int rl_delete_text (int start, int end)
999Delete the text between @var{start} and @var{end} in the current line.
1000Returns the number of characters deleted.
1001@end deftypefun
1002
1003@deftypefun {char *} rl_copy_text (int start, int end)
1004Return a copy of the text between @var{start} and @var{end} in
1005the current line.
1006@end deftypefun
1007
1008@deftypefun int rl_kill_text (int start, int end)
1009Copy the text between @var{start} and @var{end} in the current line
1010to the kill ring, appending or prepending to the last kill if the
1011last command was a kill command. The text is deleted.
1012If @var{start} is less than @var{end},
1013the text is appended, otherwise prepended. If the last command was
1014not a kill, a new kill ring slot is used.
1015@end deftypefun
1016
1017@deftypefun int rl_push_macro_input (char *macro)
1018Cause @var{macro} to be inserted into the line, as if it had been invoked
1019by a key bound to a macro. Not especially useful; use
1020@code{rl_insert_text()} instead.
1021@end deftypefun
1022
1023@node Character Input
1024@subsection Character Input
1025
1026@deftypefun int rl_read_key (void)
1027Return the next character available from Readline's current input stream.
1028This handles input inserted into
1029the input stream via @var{rl_pending_input} (@pxref{Readline Variables})
1030and @code{rl_stuff_char()}, macros, and characters read from the keyboard.
1031While waiting for input, this function will call any function assigned to
1032the @code{rl_event_hook} variable.
1033@end deftypefun
1034
1035@deftypefun int rl_getc (FILE *stream)
1036Return the next character available from @var{stream}, which is assumed to
1037be the keyboard.
1038@end deftypefun
1039
1040@deftypefun int rl_stuff_char (int c)
1041Insert @var{c} into the Readline input stream. It will be "read"
1042before Readline attempts to read characters from the terminal with
1043@code{rl_read_key()}. Up to 512 characters may be pushed back.
1044@code{rl_stuff_char} returns 1 if the character was successfully inserted;
10450 otherwise.
1046@end deftypefun
1047
1048@deftypefun int rl_execute_next (int c)
1049Make @var{c} be the next command to be executed when @code{rl_read_key()}
1050is called. This sets @var{rl_pending_input}.
1051@end deftypefun
1052
1053@deftypefun int rl_clear_pending_input (void)
1054Unset @var{rl_pending_input}, effectively negating the effect of any
1055previous call to @code{rl_execute_next()}. This works only if the
1056pending input has not already been read with @code{rl_read_key()}.
1057@end deftypefun
1058
1059@deftypefun int rl_set_keyboard_input_timeout (int u)
1060While waiting for keyboard input in @code{rl_read_key()}, Readline will
1061wait for @var{u} microseconds for input before calling any function
cc88a640
JK
1062assigned to @code{rl_event_hook}. @var{u} must be greater than or equal
1063to zero (a zero-length timeout is equivalent to a poll).
1064The default waiting period is one-tenth of a second.
1065Returns the old timeout value.
b585a9fa
EZ
1066@end deftypefun
1067
1068@node Terminal Management
1069@subsection Terminal Management
1070
1071@deftypefun void rl_prep_terminal (int meta_flag)
1072Modify the terminal settings for Readline's use, so @code{readline()}
1073can read a single character at a time from the keyboard.
1074The @var{meta_flag} argument should be non-zero if Readline should
1075read eight-bit input.
1076@end deftypefun
1077
1078@deftypefun void rl_deprep_terminal (void)
1079Undo the effects of @code{rl_prep_terminal()}, leaving the terminal in
1080the state in which it was before the most recent call to
1081@code{rl_prep_terminal()}.
1082@end deftypefun
1083
1084@deftypefun void rl_tty_set_default_bindings (Keymap kmap)
1085Read the operating system's terminal editing characters (as would be
1086displayed by @code{stty}) to their Readline equivalents.
1087The bindings are performed in @var{kmap}.
1088@end deftypefun
1089
1090@deftypefun void rl_tty_unset_default_bindings (Keymap kmap)
1091Reset the bindings manipulated by @code{rl_tty_set_default_bindings} so
1092that the terminal editing characters are bound to @code{rl_insert}.
1093The bindings are performed in @var{kmap}.
1094@end deftypefun
1095
1096@deftypefun int rl_reset_terminal (const char *terminal_name)
1097Reinitialize Readline's idea of the terminal settings using
1098@var{terminal_name} as the terminal type (e.g., @code{vt100}).
1099If @var{terminal_name} is @code{NULL}, the value of the @code{TERM}
1100environment variable is used.
1101@end deftypefun
1102
1103@node Utility Functions
1104@subsection Utility Functions
1105
cc88a640
JK
1106@deftypefun int rl_save_state (struct readline_state *sp)
1107Save a snapshot of Readline's internal state to @var{sp}.
1108The contents of the @var{readline_state} structure are documented
1109in @file{readline.h}.
1110The caller is responsible for allocating the structure.
1111@end deftypefun
1112
1113@deftypefun int rl_restore_state (struct readline_state *sp)
1114Restore Readline's internal state to that stored in @var{sp}, which must
1115have been saved by a call to @code{rl_save_state}.
1116The contents of the @var{readline_state} structure are documented
1117in @file{readline.h}.
1118The caller is responsible for freeing the structure.
1119@end deftypefun
1120
1121@deftypefun void rl_free (void *mem)
1122Deallocate the memory pointed to by @var{mem}. @var{mem} must have been
1123allocated by @code{malloc}.
1124@end deftypefun
1125
b585a9fa
EZ
1126@deftypefun void rl_replace_line (const char *text, int clear_undo)
1127Replace the contents of @code{rl_line_buffer} with @var{text}.
1128The point and mark are preserved, if possible.
1129If @var{clear_undo} is non-zero, the undo list associated with the
1130current line is cleared.
1131@end deftypefun
1132
cc88a640 1133@deftypefun void rl_extend_line_buffer (int len)
b585a9fa
EZ
1134Ensure that @code{rl_line_buffer} has enough space to hold @var{len}
1135characters, possibly reallocating it if necessary.
1136@end deftypefun
1137
1138@deftypefun int rl_initialize (void)
1139Initialize or re-initialize Readline's internal state.
1140It's not strictly necessary to call this; @code{readline()} calls it before
1141reading any input.
1142@end deftypefun
1143
1144@deftypefun int rl_ding (void)
1145Ring the terminal bell, obeying the setting of @code{bell-style}.
1146@end deftypefun
1147
1148@deftypefun int rl_alphabetic (int c)
1149Return 1 if @var{c} is an alphabetic character.
1150@end deftypefun
1151
1152@deftypefun void rl_display_match_list (char **matches, int len, int max)
1153A convenience function for displaying a list of strings in
1154columnar format on Readline's output stream. @code{matches} is the list
1155of strings, in argv format, such as a list of completion matches.
1156@code{len} is the number of strings in @code{matches}, and @code{max}
1157is the length of the longest string in @code{matches}. This function uses
1158the setting of @code{print-completions-horizontally} to select how the
1159matches are displayed (@pxref{Readline Init File Syntax}).
cc88a640
JK
1160When displaying completions, this function sets the number of columns used
1161for display to the value of @code{completion-display-width}, the value of
1162the environment variable @env{COLUMNS}, or the screen width, in that order.
b585a9fa
EZ
1163@end deftypefun
1164
1165The following are implemented as macros, defined in @code{chardefs.h}.
1166Applications should refrain from using them.
1167
1168@deftypefun int _rl_uppercase_p (int c)
1169Return 1 if @var{c} is an uppercase alphabetic character.
1170@end deftypefun
1171
1172@deftypefun int _rl_lowercase_p (int c)
1173Return 1 if @var{c} is a lowercase alphabetic character.
1174@end deftypefun
1175
1176@deftypefun int _rl_digit_p (int c)
1177Return 1 if @var{c} is a numeric character.
1178@end deftypefun
1179
1180@deftypefun int _rl_to_upper (int c)
1181If @var{c} is a lowercase alphabetic character, return the corresponding
1182uppercase character.
1183@end deftypefun
1184
1185@deftypefun int _rl_to_lower (int c)
1186If @var{c} is an uppercase alphabetic character, return the corresponding
1187lowercase character.
1188@end deftypefun
1189
1190@deftypefun int _rl_digit_value (int c)
1191If @var{c} is a number, return the value it represents.
1192@end deftypefun
1193
1194@node Miscellaneous Functions
1195@subsection Miscellaneous Functions
1196
1197@deftypefun int rl_macro_bind (const char *keyseq, const char *macro, Keymap map)
1198Bind the key sequence @var{keyseq} to invoke the macro @var{macro}.
1199The binding is performed in @var{map}. When @var{keyseq} is invoked, the
1200@var{macro} will be inserted into the line. This function is deprecated;
1201use @code{rl_generic_bind()} instead.
1202@end deftypefun
1203
1204@deftypefun void rl_macro_dumper (int readable)
1205Print the key sequences bound to macros and their values, using
1206the current keymap, to @code{rl_outstream}.
1207If @var{readable} is non-zero, the list is formatted in such a way
1208that it can be made part of an @code{inputrc} file and re-read.
1209@end deftypefun
1210
1211@deftypefun int rl_variable_bind (const char *variable, const char *value)
1212Make the Readline variable @var{variable} have @var{value}.
1213This behaves as if the readline command
1214@samp{set @var{variable} @var{value}} had been executed in an @code{inputrc}
1215file (@pxref{Readline Init File Syntax}).
1216@end deftypefun
1217
1218@deftypefun {char *} rl_variable_value (const char *variable)
1219Return a string representing the value of the Readline variable @var{variable}.
1220For boolean variables, this string is either @samp{on} or @samp{off}.
1221@end deftypefun
1222
1223@deftypefun void rl_variable_dumper (int readable)
1224Print the readline variable names and their current values
1225to @code{rl_outstream}.
1226If @var{readable} is non-zero, the list is formatted in such a way
1227that it can be made part of an @code{inputrc} file and re-read.
1228@end deftypefun
1229
1230@deftypefun int rl_set_paren_blink_timeout (int u)
1231Set the time interval (in microseconds) that Readline waits when showing
1232a balancing character when @code{blink-matching-paren} has been enabled.
1233@end deftypefun
1234
1235@deftypefun {char *} rl_get_termcap (const char *cap)
1236Retrieve the string value of the termcap capability @var{cap}.
1237Readline fetches the termcap entry for the current terminal name and
1238uses those capabilities to move around the screen line and perform other
1239terminal-specific operations, like erasing a line. Readline does not
1240use all of a terminal's capabilities, and this function will return
1241values for only those capabilities Readline uses.
1242@end deftypefun
1243
1244@node Alternate Interface
1245@subsection Alternate Interface
1246
1247An alternate interface is available to plain @code{readline()}. Some
1248applications need to interleave keyboard I/O with file, device, or
1249window system I/O, typically by using a main loop to @code{select()}
5836a818 1250on various file descriptors. To accomodate this need, readline can
b585a9fa
EZ
1251also be invoked as a `callback' function from an event loop. There
1252are functions available to make this easy.
1253
1254@deftypefun void rl_callback_handler_install (const char *prompt, rl_vcpfunc_t *lhandler)
1255Set up the terminal for readline I/O and display the initial
1256expanded value of @var{prompt}. Save the value of @var{lhandler} to
5836a818
PP
1257use as a function to call when a complete line of input has been entered.
1258The function takes the text of the line as an argument.
b585a9fa
EZ
1259@end deftypefun
1260
1261@deftypefun void rl_callback_read_char (void)
1262Whenever an application determines that keyboard input is available, it
1263should call @code{rl_callback_read_char()}, which will read the next
1264character from the current input source.
1265If that character completes the line, @code{rl_callback_read_char} will
5836a818
PP
1266invoke the @var{lhandler} function saved by @code{rl_callback_handler_install}
1267to process the line.
b585a9fa
EZ
1268Before calling the @var{lhandler} function, the terminal settings are
1269reset to the values they had before calling
1270@code{rl_callback_handler_install}.
1271If the @var{lhandler} function returns,
1272the terminal settings are modified for Readline's use again.
5836a818 1273@code{EOF} is indicated by calling @var{lhandler} with a
b585a9fa
EZ
1274@code{NULL} line.
1275@end deftypefun
1276
1277@deftypefun void rl_callback_handler_remove (void)
1278Restore the terminal to its initial state and remove the line handler.
5836a818 1279This may be called from within a callback as well as independently.
b585a9fa
EZ
1280If the @var{lhandler} installed by @code{rl_callback_handler_install}
1281does not exit the program, either this function or the function referred
1282to by the value of @code{rl_deprep_term_function} should be called before
1283the program exits to reset the terminal settings.
1284@end deftypefun
1285
1286@node A Readline Example
1287@subsection A Readline Example
1288
1289Here is a function which changes lowercase characters to their uppercase
1290equivalents, and uppercase characters to lowercase. If
1291this function was bound to @samp{M-c}, then typing @samp{M-c} would
1292change the case of the character under point. Typing @samp{M-1 0 M-c}
1293would change the case of the following 10 characters, leaving the cursor on
1294the last character changed.
1295
1296@example
1297/* Invert the case of the COUNT following characters. */
1298int
1299invert_case_line (count, key)
1300 int count, key;
1301@{
1302 register int start, end, i;
1303
1304 start = rl_point;
1305
1306 if (rl_point >= rl_end)
1307 return (0);
1308
1309 if (count < 0)
1310 @{
1311 direction = -1;
1312 count = -count;
1313 @}
1314 else
1315 direction = 1;
1316
1317 /* Find the end of the range to modify. */
1318 end = start + (count * direction);
1319
1320 /* Force it to be within range. */
1321 if (end > rl_end)
1322 end = rl_end;
1323 else if (end < 0)
1324 end = 0;
1325
1326 if (start == end)
1327 return (0);
1328
1329 if (start > end)
1330 @{
1331 int temp = start;
1332 start = end;
1333 end = temp;
1334 @}
1335
1336 /* Tell readline that we are modifying the line,
1337 so it will save the undo information. */
1338 rl_modifying (start, end);
1339
1340 for (i = start; i != end; i++)
1341 @{
1342 if (_rl_uppercase_p (rl_line_buffer[i]))
1343 rl_line_buffer[i] = _rl_to_lower (rl_line_buffer[i]);
1344 else if (_rl_lowercase_p (rl_line_buffer[i]))
1345 rl_line_buffer[i] = _rl_to_upper (rl_line_buffer[i]);
1346 @}
1347 /* Move point to on top of the last character changed. */
1348 rl_point = (direction == 1) ? end - 1 : start;
1349 return (0);
1350@}
1351@end example
1352
1353@node Readline Signal Handling
1354@section Readline Signal Handling
1355
1356Signals are asynchronous events sent to a process by the Unix kernel,
1357sometimes on behalf of another process. They are intended to indicate
1358exceptional events, like a user pressing the interrupt key on his terminal,
1359or a network connection being broken. There is a class of signals that can
1360be sent to the process currently reading input from the keyboard. Since
1361Readline changes the terminal attributes when it is called, it needs to
1362perform special processing when such a signal is received in order to
1363restore the terminal to a sane state, or provide application writers with
1364functions to do so manually.
1365
1366Readline contains an internal signal handler that is installed for a
1367number of signals (@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM},
1368@code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}).
1369When one of these signals is received, the signal handler
1370will reset the terminal attributes to those that were in effect before
1371@code{readline()} was called, reset the signal handling to what it was
1372before @code{readline()} was called, and resend the signal to the calling
1373application.
1374If and when the calling application's signal handler returns, Readline
1375will reinitialize the terminal and continue to accept input.
1376When a @code{SIGINT} is received, the Readline signal handler performs
1377some additional work, which will cause any partially-entered line to be
1378aborted (see the description of @code{rl_free_line_state()} below).
1379
1380There is an additional Readline signal handler, for @code{SIGWINCH}, which
1381the kernel sends to a process whenever the terminal's size changes (for
1382example, if a user resizes an @code{xterm}). The Readline @code{SIGWINCH}
1383handler updates Readline's internal screen size information, and then calls
1384any @code{SIGWINCH} signal handler the calling application has installed.
1385Readline calls the application's @code{SIGWINCH} signal handler without
1386resetting the terminal to its original state. If the application's signal
1387handler does more than update its idea of the terminal size and return (for
1388example, a @code{longjmp} back to a main processing loop), it @emph{must}
1389call @code{rl_cleanup_after_signal()} (described below), to restore the
5836a818 1390terminal state.
b585a9fa
EZ
1391
1392Readline provides two variables that allow application writers to
1393control whether or not it will catch certain signals and act on them
1394when they are received. It is important that applications change the
1395values of these variables only when calling @code{readline()}, not in
1396a signal handler, so Readline's internal signal state is not corrupted.
1397
1398@deftypevar int rl_catch_signals
1399If this variable is non-zero, Readline will install signal handlers for
5836a818 1400@code{SIGINT}, @code{SIGQUIT}, @code{SIGTERM}, @code{SIGALRM},
b585a9fa
EZ
1401@code{SIGTSTP}, @code{SIGTTIN}, and @code{SIGTTOU}.
1402
1403The default value of @code{rl_catch_signals} is 1.
1404@end deftypevar
1405
1406@deftypevar int rl_catch_sigwinch
5836a818
PP
1407If this variable is non-zero, Readline will install a signal handler for
1408@code{SIGWINCH}.
b585a9fa
EZ
1409
1410The default value of @code{rl_catch_sigwinch} is 1.
1411@end deftypevar
1412
1413If an application does not wish to have Readline catch any signals, or
1414to handle signals other than those Readline catches (@code{SIGHUP},
1415for example),
1416Readline provides convenience functions to do the necessary terminal
1417and internal state cleanup upon receipt of a signal.
1418
1419@deftypefun void rl_cleanup_after_signal (void)
1420This function will reset the state of the terminal to what it was before
1421@code{readline()} was called, and remove the Readline signal handlers for
1422all signals, depending on the values of @code{rl_catch_signals} and
1423@code{rl_catch_sigwinch}.
1424@end deftypefun
1425
1426@deftypefun void rl_free_line_state (void)
1427This will free any partial state associated with the current input line
1428(undo information, any partial history entry, any partially-entered
1429keyboard macro, and any partially-entered numeric argument). This
1430should be called before @code{rl_cleanup_after_signal()}. The
1431Readline signal handler for @code{SIGINT} calls this to abort the
1432current input line.
1433@end deftypefun
1434
1435@deftypefun void rl_reset_after_signal (void)
1436This will reinitialize the terminal and reinstall any Readline signal
1437handlers, depending on the values of @code{rl_catch_signals} and
1438@code{rl_catch_sigwinch}.
1439@end deftypefun
1440
1441If an application does not wish Readline to catch @code{SIGWINCH}, it may
1442call @code{rl_resize_terminal()} or @code{rl_set_screen_size()} to force
1443Readline to update its idea of the terminal size when a @code{SIGWINCH}
1444is received.
1445
cc88a640
JK
1446@deftypefun void rl_echo_signal_char (int sig)
1447If an application wishes to install its own signal handlers, but still
1448have readline display characters that generate signals, calling this
1449function with @var{sig} set to @code{SIGINT}, @code{SIGQUIT}, or
1450@code{SIGTSTP} will display the character generating that signal.
1451@end deftypefun
1452
b585a9fa
EZ
1453@deftypefun void rl_resize_terminal (void)
1454Update Readline's internal screen size by reading values from the kernel.
1455@end deftypefun
1456
1457@deftypefun void rl_set_screen_size (int rows, int cols)
1458Set Readline's idea of the terminal size to @var{rows} rows and
1459@var{cols} columns. If either @var{rows} or @var{columns} is less than
1460or equal to 0, Readline's idea of that terminal dimension is unchanged.
1461@end deftypefun
1462
1463If an application does not want to install a @code{SIGWINCH} handler, but
1464is still interested in the screen dimensions, Readline's idea of the screen
1465size may be queried.
1466
1467@deftypefun void rl_get_screen_size (int *rows, int *cols)
1468Return Readline's idea of the terminal's size in the
1469variables pointed to by the arguments.
1470@end deftypefun
1471
1472@deftypefun void rl_reset_screen_size (void)
1473Cause Readline to reobtain the screen size and recalculate its dimensions.
1474@end deftypefun
1475
1476The following functions install and remove Readline's signal handlers.
1477
1478@deftypefun int rl_set_signals (void)
1479Install Readline's signal handler for @code{SIGINT}, @code{SIGQUIT},
5836a818 1480@code{SIGTERM}, @code{SIGALRM}, @code{SIGTSTP}, @code{SIGTTIN},
b585a9fa
EZ
1481@code{SIGTTOU}, and @code{SIGWINCH}, depending on the values of
1482@code{rl_catch_signals} and @code{rl_catch_sigwinch}.
1483@end deftypefun
1484
1485@deftypefun int rl_clear_signals (void)
1486Remove all of the Readline signal handlers installed by
1487@code{rl_set_signals()}.
1488@end deftypefun
1489
1490@node Custom Completers
1491@section Custom Completers
1492@cindex application-specific completion functions
1493
1494Typically, a program that reads commands from the user has a way of
1495disambiguating commands and data. If your program is one of these, then
1496it can provide completion for commands, data, or both.
1497The following sections describe how your program and Readline
1498cooperate to provide this service.
1499
1500@menu
1501* How Completing Works:: The logic used to do completion.
1502* Completion Functions:: Functions provided by Readline.
1503* Completion Variables:: Variables which control completion.
1504* A Short Completion Example:: An example of writing completer subroutines.
1505@end menu
1506
1507@node How Completing Works
1508@subsection How Completing Works
1509
1510In order to complete some text, the full list of possible completions
1511must be available. That is, it is not possible to accurately
1512expand a partial word without knowing all of the possible words
1513which make sense in that context. The Readline library provides
1514the user interface to completion, and two of the most common
1515completion functions: filename and username. For completing other types
1516of text, you must write your own completion function. This section
1517describes exactly what such functions must do, and provides an example.
1518
1519There are three major functions used to perform completion:
1520
1521@enumerate
1522@item
1523The user-interface function @code{rl_complete()}. This function is
1524called with the same arguments as other bindable Readline functions:
1525@var{count} and @var{invoking_key}.
1526It isolates the word to be completed and calls
1527@code{rl_completion_matches()} to generate a list of possible completions.
1528It then either lists the possible completions, inserts the possible
1529completions, or actually performs the
1530completion, depending on which behavior is desired.
1531
1532@item
1533The internal function @code{rl_completion_matches()} uses an
1534application-supplied @dfn{generator} function to generate the list of
1535possible matches, and then returns the array of these matches.
1536The caller should place the address of its generator function in
1537@code{rl_completion_entry_function}.
1538
1539@item
1540The generator function is called repeatedly from
1541@code{rl_completion_matches()}, returning a string each time. The
1542arguments to the generator function are @var{text} and @var{state}.
1543@var{text} is the partial word to be completed. @var{state} is zero the
1544first time the function is called, allowing the generator to perform
1545any necessary initialization, and a positive non-zero integer for
1546each subsequent call. The generator function returns
1547@code{(char *)NULL} to inform @code{rl_completion_matches()} that there are
1548no more possibilities left. Usually the generator function computes the
1549list of possible completions when @var{state} is zero, and returns them
1550one at a time on subsequent calls. Each string the generator function
1551returns as a match must be allocated with @code{malloc()}; Readline
1552frees the strings when it has finished with them.
1553Such a generator function is referred to as an
1554@dfn{application-specific completion function}.
1555
1556@end enumerate
1557
1558@deftypefun int rl_complete (int ignore, int invoking_key)
1559Complete the word at or before point. You have supplied the function
1560that does the initial simple matching selection algorithm (see
1561@code{rl_completion_matches()}). The default is to do filename completion.
1562@end deftypefun
1563
1564@deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1565This is a pointer to the generator function for
1566@code{rl_completion_matches()}.
1567If the value of @code{rl_completion_entry_function} is
1568@code{NULL} then the default filename generator
1569function, @code{rl_filename_completion_function()}, is used.
1570An @dfn{application-specific completion function} is a function whose
1571address is assigned to @code{rl_completion_entry_function} and whose
1572return values are used to generate possible completions.
1573@end deftypevar
1574
1575@node Completion Functions
1576@subsection Completion Functions
1577
1578Here is the complete list of callable completion functions present in
1579Readline.
1580
1581@deftypefun int rl_complete_internal (int what_to_do)
1582Complete the word at or before point. @var{what_to_do} says what to do
1583with the completion. A value of @samp{?} means list the possible
1584completions. @samp{TAB} means do standard completion. @samp{*} means
1585insert all of the possible completions. @samp{!} means to display
1586all of the possible completions, if there is more than one, as well as
1587performing partial completion. @samp{@@} is similar to @samp{!}, but
1588possible completions are not listed if the possible completions share
1589a common prefix.
1590@end deftypefun
1591
1592@deftypefun int rl_complete (int ignore, int invoking_key)
1593Complete the word at or before point. You have supplied the function
1594that does the initial simple matching selection algorithm (see
1595@code{rl_completion_matches()} and @code{rl_completion_entry_function}).
1596The default is to do filename
1597completion. This calls @code{rl_complete_internal()} with an
1598argument depending on @var{invoking_key}.
1599@end deftypefun
1600
1601@deftypefun int rl_possible_completions (int count, int invoking_key)
1602List the possible completions. See description of @code{rl_complete
1603()}. This calls @code{rl_complete_internal()} with an argument of
1604@samp{?}.
1605@end deftypefun
1606
1607@deftypefun int rl_insert_completions (int count, int invoking_key)
1608Insert the list of possible completions into the line, deleting the
1609partially-completed word. See description of @code{rl_complete()}.
1610This calls @code{rl_complete_internal()} with an argument of @samp{*}.
1611@end deftypefun
1612
1613@deftypefun int rl_completion_mode (rl_command_func_t *cfunc)
5836a818 1614Returns the apppriate value to pass to @code{rl_complete_internal()}
b585a9fa
EZ
1615depending on whether @var{cfunc} was called twice in succession and
1616the values of the @code{show-all-if-ambiguous} and
1617@code{show-all-if-unmodified} variables.
1618Application-specific completion functions may use this function to present
1619the same interface as @code{rl_complete()}.
1620@end deftypefun
1621
1622@deftypefun {char **} rl_completion_matches (const char *text, rl_compentry_func_t *entry_func)
1623Returns an array of strings which is a list of completions for
1624@var{text}. If there are no completions, returns @code{NULL}.
1625The first entry in the returned array is the substitution for @var{text}.
1626The remaining entries are the possible completions. The array is
1627terminated with a @code{NULL} pointer.
1628
1629@var{entry_func} is a function of two args, and returns a
1630@code{char *}. The first argument is @var{text}. The second is a
1631state argument; it is zero on the first call, and non-zero on subsequent
1632calls. @var{entry_func} returns a @code{NULL} pointer to the caller
1633when there are no more matches.
1634@end deftypefun
1635
1636@deftypefun {char *} rl_filename_completion_function (const char *text, int state)
1637A generator function for filename completion in the general case.
1638@var{text} is a partial filename.
1639The Bash source is a useful reference for writing application-specific
1640completion functions (the Bash completion functions call this and other
1641Readline functions).
1642@end deftypefun
1643
1644@deftypefun {char *} rl_username_completion_function (const char *text, int state)
1645A completion generator for usernames. @var{text} contains a partial
1646username preceded by a random character (usually @samp{~}). As with all
1647completion generators, @var{state} is zero on the first call and non-zero
1648for subsequent calls.
1649@end deftypefun
1650
1651@node Completion Variables
1652@subsection Completion Variables
1653
1654@deftypevar {rl_compentry_func_t *} rl_completion_entry_function
1655A pointer to the generator function for @code{rl_completion_matches()}.
1656@code{NULL} means to use @code{rl_filename_completion_function()},
1657the default filename completer.
1658@end deftypevar
1659
1660@deftypevar {rl_completion_func_t *} rl_attempted_completion_function
1661A pointer to an alternative function to create matches.
1662The function is called with @var{text}, @var{start}, and @var{end}.
1663@var{start} and @var{end} are indices in @code{rl_line_buffer} defining
1664the boundaries of @var{text}, which is a character string.
1665If this function exists and returns @code{NULL}, or if this variable is
1666set to @code{NULL}, then @code{rl_complete()} will call the value of
1667@code{rl_completion_entry_function} to generate matches, otherwise the
1668array of strings returned will be used.
1669If this function sets the @code{rl_attempted_completion_over}
1670variable to a non-zero value, Readline will not perform its default
1671completion even if this function returns no matches.
1672@end deftypevar
1673
1674@deftypevar {rl_quote_func_t *} rl_filename_quoting_function
1675A pointer to a function that will quote a filename in an
1676application-specific fashion. This is called if filename completion is being
1677attempted and one of the characters in @code{rl_filename_quote_characters}
1678appears in a completed filename. The function is called with
1679@var{text}, @var{match_type}, and @var{quote_pointer}. The @var{text}
1680is the filename to be quoted. The @var{match_type} is either
1681@code{SINGLE_MATCH}, if there is only one completion match, or
1682@code{MULT_MATCH}. Some functions use this to decide whether or not to
1683insert a closing quote character. The @var{quote_pointer} is a pointer
1684to any opening quote character the user typed. Some functions choose
1685to reset this character.
1686@end deftypevar
1687
1688@deftypevar {rl_dequote_func_t *} rl_filename_dequoting_function
1689A pointer to a function that will remove application-specific quoting
1690characters from a filename before completion is attempted, so those
1691characters do not interfere with matching the text against names in
1692the filesystem. It is called with @var{text}, the text of the word
1693to be dequoted, and @var{quote_char}, which is the quoting character
1694that delimits the filename (usually @samp{'} or @samp{"}). If
1695@var{quote_char} is zero, the filename was not in an embedded string.
1696@end deftypevar
1697
1698@deftypevar {rl_linebuf_func_t *} rl_char_is_quoted_p
1699A pointer to a function to call that determines whether or not a specific
1700character in the line buffer is quoted, according to whatever quoting
1701mechanism the program calling Readline uses. The function is called with
1702two arguments: @var{text}, the text of the line, and @var{index}, the
1703index of the character in the line. It is used to decide whether a
1704character found in @code{rl_completer_word_break_characters} should be
1705used to break words for the completer.
1706@end deftypevar
1707
1708@deftypevar {rl_compignore_func_t *} rl_ignore_some_completions_function
1709This function, if defined, is called by the completer when real filename
1710completion is done, after all the matching names have been generated.
1711It is passed a @code{NULL} terminated array of matches.
1712The first element (@code{matches[0]}) is the
1713maximal substring common to all matches. This function can
1714re-arrange the list of matches as required, but each element deleted
1715from the array must be freed.
1716@end deftypevar
1717
1718@deftypevar {rl_icppfunc_t *} rl_directory_completion_hook
1719This function, if defined, is allowed to modify the directory portion
cc88a640
JK
1720of filenames Readline completes.
1721It could be used to expand symbolic links or shell variables in pathnames.
1722It is called with the address of a string (the current directory name) as an
1723argument, and may modify that string.
b585a9fa
EZ
1724If the string is replaced with a new string, the old value should be freed.
1725Any modified directory name should have a trailing slash.
cc88a640 1726The modified value will be used as part of the completion, replacing
b585a9fa 1727the directory portion of the pathname the user typed.
cc88a640
JK
1728At the least, even if no other expansion is performed, this function should
1729remove any quote characters from the directory name, because its result will
1730be passed directly to @code{opendir()}.
1731The directory completion hook returns an integer that should be non-zero if
1732the function modifies its directory argument.
1733The function should not modify the directory argument if it returns 0.
1734@end deftypevar
1735
5836a818
PP
1736@ignore
1737@deftypevar extern rl_icppfunc_t *rl_directory_rewrite_hook;
cc88a640
JK
1738If non-zero, this is the address of a function to call when completing
1739a directory name. This function takes the address of the directory name
1740to be modified as an argument. Unlike @code{rl_directory_completion_hook},
1741it only modifies the directory name used in @code{opendir}, not what is
1742displayed when the possible completions are printed or inserted. It is
1743called before rl_directory_completion_hook.
1744
5836a818 1745I'm not happy with how this works yet, so it's undocumented.
cc88a640 1746@end deftypevar
5836a818 1747@end ignore
cc88a640
JK
1748
1749@deftypevar {rl_dequote_func_t *} rl_filename_rewrite_hook
1750If non-zero, this is the address of a function called when reading
1751directory entries from the filesystem for completion and comparing
1752them to the partial word to be completed. The function should
5836a818 1753perform any necesary application or system-specific conversion on
cc88a640
JK
1754the filename, such as converting between character sets or converting
1755from a filesystem format to a character input format.
1756The function takes two arguments: @var{fname}, the filename to be converted,
1757and @var{fnlen}, its length in bytes.
1758It must either return its first argument (if no conversion takes place)
1759or the converted filename in newly-allocated memory. The converted
1760form is used to compare against the word to be completed, and, if it
1761matches, is added to the list of matches. Readline will free the
1762allocated string.
b585a9fa
EZ
1763@end deftypevar
1764
1765@deftypevar {rl_compdisp_func_t *} rl_completion_display_matches_hook
1766If non-zero, then this is the address of a function to call when
1767completing a word would normally display the list of possible matches.
1768This function is called in lieu of Readline displaying the list.
1769It takes three arguments:
1770(@code{char **}@var{matches}, @code{int} @var{num_matches}, @code{int} @var{max_length})
1771where @var{matches} is the array of matching strings,
1772@var{num_matches} is the number of strings in that array, and
1773@var{max_length} is the length of the longest string in that array.
1774Readline provides a convenience function, @code{rl_display_match_list},
5836a818
PP
1775that takes care of doing the display to Readline's output stream. That
1776function may be called from this hook.
b585a9fa
EZ
1777@end deftypevar
1778
1779@deftypevar {const char *} rl_basic_word_break_characters
1780The basic list of characters that signal a break between words for the
1781completer routine. The default value of this variable is the characters
1782which break words for completion in Bash:
1783@code{" \t\n\"\\'`@@$><=;|&@{("}.
1784@end deftypevar
1785
1786@deftypevar {const char *} rl_basic_quote_characters
1787A list of quote characters which can cause a word break.
1788@end deftypevar
1789
1790@deftypevar {const char *} rl_completer_word_break_characters
1791The list of characters that signal a break between words for
1792@code{rl_complete_internal()}. The default list is the value of
1793@code{rl_basic_word_break_characters}.
1794@end deftypevar
1795
1796@deftypevar {rl_cpvfunc_t *} rl_completion_word_break_hook
1797If non-zero, this is the address of a function to call when Readline is
1798deciding where to separate words for word completion. It should return
1799a character string like @code{rl_completer_word_break_characters} to be
1800used to perform the current completion. The function may choose to set
1801@code{rl_completer_word_break_characters} itself. If the function
1802returns @code{NULL}, @code{rl_completer_word_break_characters} is used.
1803@end deftypevar
1804
1805@deftypevar {const char *} rl_completer_quote_characters
1806A list of characters which can be used to quote a substring of the line.
1807Completion occurs on the entire substring, and within the substring
1808@code{rl_completer_word_break_characters} are treated as any other character,
1809unless they also appear within this list.
1810@end deftypevar
1811
1812@deftypevar {const char *} rl_filename_quote_characters
1813A list of characters that cause a filename to be quoted by the completer
1814when they appear in a completed filename. The default is the null string.
1815@end deftypevar
1816
1817@deftypevar {const char *} rl_special_prefixes
1818The list of characters that are word break characters, but should be
1819left in @var{text} when it is passed to the completion function.
1820Programs can use this to help determine what kind of completing to do.
1821For instance, Bash sets this variable to "$@@" so that it can complete
1822shell variables and hostnames.
1823@end deftypevar
1824
1825@deftypevar int rl_completion_query_items
1826Up to this many items will be displayed in response to a
1827possible-completions call. After that, readline asks the user if she is sure
1828she wants to see them all. The default value is 100. A negative value
1829indicates that Readline should never ask the user.
1830@end deftypevar
1831
1832@deftypevar {int} rl_completion_append_character
1833When a single completion alternative matches at the end of the command
1834line, this character is appended to the inserted completion text. The
1835default is a space character (@samp{ }). Setting this to the null
1836character (@samp{\0}) prevents anything being appended automatically.
1837This can be changed in application-specific completion functions to
1838provide the ``most sensible word separator character'' according to
1839an application-specific command line syntax specification.
1840@end deftypevar
1841
1842@deftypevar int rl_completion_suppress_append
1843If non-zero, @var{rl_completion_append_character} is not appended to
1844matches at the end of the command line, as described above.
1845It is set to 0 before any application-specific completion function
1846is called, and may only be changed within such a function.
1847@end deftypevar
1848
1849@deftypevar int rl_completion_quote_character
1850When Readline is completing quoted text, as delimited by one of the
1851characters in @var{rl_completer_quote_characters}, it sets this variable
1852to the quoting character found.
1853This is set before any application-specific completion function is called.
1854@end deftypevar
1855
1856@deftypevar int rl_completion_suppress_quote
1857If non-zero, Readline does not append a matching quote character when
1858performing completion on a quoted string.
1859It is set to 0 before any application-specific completion function
1860is called, and may only be changed within such a function.
1861@end deftypevar
1862
1863@deftypevar int rl_completion_found_quote
1864When Readline is completing quoted text, it sets this variable
1865to a non-zero value if the word being completed contains or is delimited
1866by any quoting characters, including backslashes.
1867This is set before any application-specific completion function is called.
1868@end deftypevar
1869
1870@deftypevar int rl_completion_mark_symlink_dirs
1871If non-zero, a slash will be appended to completed filenames that are
1872symbolic links to directory names, subject to the value of the
1873user-settable @var{mark-directories} variable.
1874This variable exists so that application-specific completion functions
1875can override the user's global preference (set via the
1876@var{mark-symlinked-directories} Readline variable) if appropriate.
1877This variable is set to the user's preference before any
1878application-specific completion function is called, so unless that
1879function modifies the value, the user's preferences are honored.
1880@end deftypevar
1881
1882@deftypevar int rl_ignore_completion_duplicates
1883If non-zero, then duplicates in the matches are removed.
1884The default is 1.
1885@end deftypevar
1886
1887@deftypevar int rl_filename_completion_desired
1888Non-zero means that the results of the matches are to be treated as
1889filenames. This is @emph{always} zero when completion is attempted,
1890and can only be changed
1891within an application-specific completion function. If it is set to a
1892non-zero value by such a function, directory names have a slash appended
1893and Readline attempts to quote completed filenames if they contain any
1894characters in @code{rl_filename_quote_characters} and
1895@code{rl_filename_quoting_desired} is set to a non-zero value.
1896@end deftypevar
1897
1898@deftypevar int rl_filename_quoting_desired
1899Non-zero means that the results of the matches are to be quoted using
1900double quotes (or an application-specific quoting mechanism) if the
1901completed filename contains any characters in
1902@code{rl_filename_quote_chars}. This is @emph{always} non-zero
1903when completion is attempted, and can only be changed within an
1904application-specific completion function.
1905The quoting is effected via a call to the function pointed to
1906by @code{rl_filename_quoting_function}.
1907@end deftypevar
1908
1909@deftypevar int rl_attempted_completion_over
1910If an application-specific completion function assigned to
1911@code{rl_attempted_completion_function} sets this variable to a non-zero
1912value, Readline will not perform its default filename completion even
1913if the application's completion function returns no matches.
1914It should be set only by an application's completion function.
1915@end deftypevar
1916
cc88a640
JK
1917@deftypevar int rl_sort_completion_matches
1918If an application sets this variable to 0, Readline will not sort the
1919list of completions (which implies that it cannot remove any duplicate
1920completions). The default value is 1, which means that Readline will
1921sort the completions and, depending on the value of
1922@code{rl_ignore_completion_duplicates}, will attempt to remove duplicate
1923matches.
1924@end deftypevar
1925
b585a9fa
EZ
1926@deftypevar int rl_completion_type
1927Set to a character describing the type of completion Readline is currently
1928attempting; see the description of @code{rl_complete_internal()}
1929(@pxref{Completion Functions}) for the list of characters.
1930This is set to the appropriate value before any application-specific
1931completion function is called, allowing such functions to present
1932the same interface as @code{rl_complete()}.
1933@end deftypevar
1934
cc88a640
JK
1935@deftypevar int rl_completion_invoking_key
1936Set to the final character in the key sequence that invoked one of the
1937completion functions that call @code{rl_complete_internal()}. This is
1938set to the appropriate value before any application-specific completion
1939function is called.
1940@end deftypevar
1941
b585a9fa
EZ
1942@deftypevar int rl_inhibit_completion
1943If this variable is non-zero, completion is inhibited. The completion
1944character will be inserted as any other bound to @code{self-insert}.
1945@end deftypevar
1946
1947@node A Short Completion Example
1948@subsection A Short Completion Example
1949
1950Here is a small application demonstrating the use of the GNU Readline
1951library. It is called @code{fileman}, and the source code resides in
1952@file{examples/fileman.c}. This sample application provides
1953completion of command names, line editing features, and access to the
1954history list.
1955
1956@page
1957@smallexample
1958/* fileman.c -- A tiny application which demonstrates how to use the
1959 GNU Readline library. This application interactively allows users
1960 to manipulate files and their modes. */
1961
cc88a640
JK
1962#ifdef HAVE_CONFIG_H
1963# include <config.h>
1964#endif
1965
b585a9fa 1966#include <sys/types.h>
cc88a640
JK
1967#ifdef HAVE_SYS_FILE_H
1968# include <sys/file.h>
1969#endif
b585a9fa 1970#include <sys/stat.h>
cc88a640
JK
1971
1972#ifdef HAVE_UNISTD_H
1973# include <unistd.h>
1974#endif
1975
1976#include <fcntl.h>
1977#include <stdio.h>
1978#include <errno.h>
1979
1980#if defined (HAVE_STRING_H)
1981# include <string.h>
1982#else /* !HAVE_STRING_H */
1983# include <strings.h>
1984#endif /* !HAVE_STRING_H */
1985
1986#ifdef HAVE_STDLIB_H
1987# include <stdlib.h>
1988#endif
1989
1990#include <time.h>
b585a9fa
EZ
1991
1992#include <readline/readline.h>
1993#include <readline/history.h>
1994
cc88a640 1995extern char *xmalloc PARAMS((size_t));
b585a9fa
EZ
1996
1997/* The names of functions that actually do the manipulation. */
cc88a640
JK
1998int com_list PARAMS((char *));
1999int com_view PARAMS((char *));
2000int com_rename PARAMS((char *));
2001int com_stat PARAMS((char *));
2002int com_pwd PARAMS((char *));
2003int com_delete PARAMS((char *));
2004int com_help PARAMS((char *));
2005int com_cd PARAMS((char *));
2006int com_quit PARAMS((char *));
b585a9fa
EZ
2007
2008/* A structure which contains information on the commands this program
2009 can understand. */
2010
2011typedef struct @{
2012 char *name; /* User printable name of the function. */
2013 rl_icpfunc_t *func; /* Function to call to do the job. */
2014 char *doc; /* Documentation for this function. */
2015@} COMMAND;
2016
2017COMMAND commands[] = @{
2018 @{ "cd", com_cd, "Change to directory DIR" @},
2019 @{ "delete", com_delete, "Delete FILE" @},
2020 @{ "help", com_help, "Display this text" @},
2021 @{ "?", com_help, "Synonym for `help'" @},
2022 @{ "list", com_list, "List files in DIR" @},
2023 @{ "ls", com_list, "Synonym for `list'" @},
2024 @{ "pwd", com_pwd, "Print the current working directory" @},
2025 @{ "quit", com_quit, "Quit using Fileman" @},
2026 @{ "rename", com_rename, "Rename FILE to NEWNAME" @},
2027 @{ "stat", com_stat, "Print out statistics on FILE" @},
2028 @{ "view", com_view, "View the contents of FILE" @},
2029 @{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL @}
2030@};
2031
2032/* Forward declarations. */
2033char *stripwhite ();
2034COMMAND *find_command ();
2035
2036/* The name of this program, as taken from argv[0]. */
2037char *progname;
2038
cc88a640 2039/* When non-zero, this global means the user is done using this program. */
b585a9fa
EZ
2040int done;
2041
2042char *
2043dupstr (s)
cc88a640 2044 char *s;
b585a9fa
EZ
2045@{
2046 char *r;
2047
2048 r = xmalloc (strlen (s) + 1);
2049 strcpy (r, s);
2050 return (r);
2051@}
2052
2053main (argc, argv)
2054 int argc;
2055 char **argv;
2056@{
2057 char *line, *s;
2058
2059 progname = argv[0];
2060
2061 initialize_readline (); /* Bind our completer. */
2062
2063 /* Loop reading and executing lines until the user quits. */
2064 for ( ; done == 0; )
2065 @{
2066 line = readline ("FileMan: ");
2067
2068 if (!line)
2069 break;
2070
2071 /* Remove leading and trailing whitespace from the line.
2072 Then, if there is anything left, add it to the history list
2073 and execute it. */
2074 s = stripwhite (line);
2075
2076 if (*s)
2077 @{
2078 add_history (s);
2079 execute_line (s);
2080 @}
2081
2082 free (line);
2083 @}
2084 exit (0);
2085@}
2086
2087/* Execute a command line. */
2088int
2089execute_line (line)
2090 char *line;
2091@{
2092 register int i;
2093 COMMAND *command;
2094 char *word;
2095
2096 /* Isolate the command word. */
2097 i = 0;
2098 while (line[i] && whitespace (line[i]))
2099 i++;
2100 word = line + i;
2101
2102 while (line[i] && !whitespace (line[i]))
2103 i++;
2104
2105 if (line[i])
2106 line[i++] = '\0';
2107
2108 command = find_command (word);
2109
2110 if (!command)
2111 @{
2112 fprintf (stderr, "%s: No such command for FileMan.\n", word);
2113 return (-1);
2114 @}
2115
2116 /* Get argument to command, if any. */
2117 while (whitespace (line[i]))
2118 i++;
2119
2120 word = line + i;
2121
2122 /* Call the function. */
2123 return ((*(command->func)) (word));
2124@}
2125
2126/* Look up NAME as the name of a command, and return a pointer to that
2127 command. Return a NULL pointer if NAME isn't a command name. */
2128COMMAND *
2129find_command (name)
2130 char *name;
2131@{
2132 register int i;
2133
2134 for (i = 0; commands[i].name; i++)
2135 if (strcmp (name, commands[i].name) == 0)
2136 return (&commands[i]);
2137
2138 return ((COMMAND *)NULL);
2139@}
2140
2141/* Strip whitespace from the start and end of STRING. Return a pointer
2142 into STRING. */
2143char *
2144stripwhite (string)
2145 char *string;
2146@{
2147 register char *s, *t;
2148
2149 for (s = string; whitespace (*s); s++)
2150 ;
2151
2152 if (*s == 0)
2153 return (s);
2154
2155 t = s + strlen (s) - 1;
2156 while (t > s && whitespace (*t))
2157 t--;
2158 *++t = '\0';
2159
2160 return s;
2161@}
2162
2163/* **************************************************************** */
2164/* */
2165/* Interface to Readline Completion */
2166/* */
2167/* **************************************************************** */
2168
cc88a640
JK
2169char *command_generator PARAMS((const char *, int));
2170char **fileman_completion PARAMS((const char *, int, int));
b585a9fa 2171
cc88a640
JK
2172/* Tell the GNU Readline library how to complete. We want to try to complete
2173 on command names if this is the first word in the line, or on filenames
2174 if not. */
b585a9fa
EZ
2175initialize_readline ()
2176@{
2177 /* Allow conditional parsing of the ~/.inputrc file. */
2178 rl_readline_name = "FileMan";
2179
2180 /* Tell the completer that we want a crack first. */
2181 rl_attempted_completion_function = fileman_completion;
2182@}
2183
cc88a640
JK
2184/* Attempt to complete on the contents of TEXT. START and END bound the
2185 region of rl_line_buffer that contains the word to complete. TEXT is
2186 the word to complete. We can use the entire contents of rl_line_buffer
2187 in case we want to do some simple parsing. Return the array of matches,
2188 or NULL if there aren't any. */
b585a9fa
EZ
2189char **
2190fileman_completion (text, start, end)
2191 const char *text;
2192 int start, end;
2193@{
2194 char **matches;
2195
2196 matches = (char **)NULL;
2197
2198 /* If this word is at the start of the line, then it is a command
2199 to complete. Otherwise it is the name of a file in the current
2200 directory. */
2201 if (start == 0)
2202 matches = rl_completion_matches (text, command_generator);
2203
2204 return (matches);
2205@}
2206
cc88a640
JK
2207/* Generator function for command completion. STATE lets us know whether
2208 to start from scratch; without any state (i.e. STATE == 0), then we
2209 start at the top of the list. */
b585a9fa
EZ
2210char *
2211command_generator (text, state)
2212 const char *text;
2213 int state;
2214@{
2215 static int list_index, len;
2216 char *name;
2217
cc88a640
JK
2218 /* If this is a new word to complete, initialize now. This includes
2219 saving the length of TEXT for efficiency, and initializing the index
2220 variable to 0. */
b585a9fa
EZ
2221 if (!state)
2222 @{
2223 list_index = 0;
2224 len = strlen (text);
2225 @}
2226
cc88a640 2227 /* Return the next name which partially matches from the command list. */
b585a9fa
EZ
2228 while (name = commands[list_index].name)
2229 @{
2230 list_index++;
2231
2232 if (strncmp (name, text, len) == 0)
2233 return (dupstr(name));
2234 @}
2235
2236 /* If no names matched, then return NULL. */
2237 return ((char *)NULL);
2238@}
2239
2240/* **************************************************************** */
2241/* */
2242/* FileMan Commands */
2243/* */
2244/* **************************************************************** */
2245
2246/* String to pass to system (). This is for the LIST, VIEW and RENAME
2247 commands. */
2248static char syscom[1024];
2249
2250/* List the file(s) named in arg. */
2251com_list (arg)
2252 char *arg;
2253@{
2254 if (!arg)
2255 arg = "";
2256
2257 sprintf (syscom, "ls -FClg %s", arg);
2258 return (system (syscom));
2259@}
2260
2261com_view (arg)
2262 char *arg;
2263@{
2264 if (!valid_argument ("view", arg))
2265 return 1;
2266
cc88a640
JK
2267#if defined (__MSDOS__)
2268 /* more.com doesn't grok slashes in pathnames */
2269 sprintf (syscom, "less %s", arg);
2270#else
b585a9fa 2271 sprintf (syscom, "more %s", arg);
cc88a640 2272#endif
b585a9fa
EZ
2273 return (system (syscom));
2274@}
2275
2276com_rename (arg)
2277 char *arg;
2278@{
2279 too_dangerous ("rename");
2280 return (1);
2281@}
2282
2283com_stat (arg)
2284 char *arg;
2285@{
2286 struct stat finfo;
2287
2288 if (!valid_argument ("stat", arg))
2289 return (1);
2290
2291 if (stat (arg, &finfo) == -1)
2292 @{
2293 perror (arg);
2294 return (1);
2295 @}
2296
2297 printf ("Statistics for `%s':\n", arg);
2298
cc88a640
JK
2299 printf ("%s has %d link%s, and is %d byte%s in length.\n",
2300 arg,
b585a9fa
EZ
2301 finfo.st_nlink,
2302 (finfo.st_nlink == 1) ? "" : "s",
2303 finfo.st_size,
2304 (finfo.st_size == 1) ? "" : "s");
2305 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
2306 printf (" Last access at: %s", ctime (&finfo.st_atime));
2307 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
2308 return (0);
2309@}
2310
2311com_delete (arg)
2312 char *arg;
2313@{
2314 too_dangerous ("delete");
2315 return (1);
2316@}
2317
2318/* Print out help for ARG, or for all of the commands if ARG is
2319 not present. */
2320com_help (arg)
2321 char *arg;
2322@{
2323 register int i;
2324 int printed = 0;
2325
2326 for (i = 0; commands[i].name; i++)
2327 @{
2328 if (!*arg || (strcmp (arg, commands[i].name) == 0))
2329 @{
2330 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
2331 printed++;
2332 @}
2333 @}
2334
2335 if (!printed)
2336 @{
2337 printf ("No commands match `%s'. Possibilties are:\n", arg);
2338
2339 for (i = 0; commands[i].name; i++)
2340 @{
2341 /* Print in six columns. */
2342 if (printed == 6)
2343 @{
2344 printed = 0;
2345 printf ("\n");
2346 @}
2347
2348 printf ("%s\t", commands[i].name);
2349 printed++;
2350 @}
2351
2352 if (printed)
2353 printf ("\n");
2354 @}
2355 return (0);
2356@}
2357
2358/* Change to the directory ARG. */
2359com_cd (arg)
2360 char *arg;
2361@{
2362 if (chdir (arg) == -1)
2363 @{
2364 perror (arg);
2365 return 1;
2366 @}
2367
2368 com_pwd ("");
2369 return (0);
2370@}
2371
2372/* Print out the current working directory. */
2373com_pwd (ignore)
2374 char *ignore;
2375@{
2376 char dir[1024], *s;
2377
2378 s = getcwd (dir, sizeof(dir) - 1);
2379 if (s == 0)
2380 @{
2381 printf ("Error getting pwd: %s\n", dir);
2382 return 1;
2383 @}
2384
2385 printf ("Current directory is %s\n", dir);
2386 return 0;
2387@}
2388
cc88a640 2389/* The user wishes to quit using this program. Just set DONE non-zero. */
b585a9fa
EZ
2390com_quit (arg)
2391 char *arg;
2392@{
2393 done = 1;
2394 return (0);
2395@}
2396
2397/* Function which tells you that you can't do this. */
2398too_dangerous (caller)
2399 char *caller;
2400@{
2401 fprintf (stderr,
cc88a640 2402 "%s: Too dangerous for me to distribute. Write it yourself.\n",
b585a9fa 2403 caller);
b585a9fa
EZ
2404@}
2405
cc88a640
JK
2406/* Return non-zero if ARG is a valid argument for CALLER, else print
2407 an error message and return zero. */
b585a9fa
EZ
2408int
2409valid_argument (caller, arg)
2410 char *caller, *arg;
2411@{
2412 if (!arg || !*arg)
2413 @{
2414 fprintf (stderr, "%s: Argument required.\n", caller);
2415 return (0);
2416 @}
2417
2418 return (1);
2419@}
2420@end smallexample
This page took 0.63509 seconds and 4 git commands to generate.