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