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