* config/mh-ncr3000 (INSTALL): Don't use /usr/ucb/install,
[deliverable/binutils-gdb.git] / readline / readline.texi
CommitLineData
681fa9a8
RP
1\input texinfo @c -*-texinfo-*-
2@comment %**start of header (This is for running Texinfo on a region.)
3@setfilename readline.info
4@settitle Line Editing Commands
5@comment %**end of header (This is for running Texinfo on a region.)
6@synindex fn vr
7
14ccee32
RP
8@ifinfo
9@format
10START-INFO-DIR-ENTRY
681fa9a8 11* Readline: (readline). The GNU Readline Library.
14ccee32
RP
12END-INFO-DIR-ENTRY
13@end format
14@end ifinfo
681fa9a8
RP
15
16@iftex
17@comment finalout
18@end iftex
19
20@ifinfo
21This document describes the GNU Readline Library, a utility for aiding
22in the consitency of user interface across discrete programs that need
23to provide a command line interface.
24
25Copyright (C) 1988 Free Software Foundation, Inc.
26
27Permission is granted to make and distribute verbatim copies of
28this manual provided the copyright notice and this permission notice
29pare preserved on all copies.
30
31@ignore
32Permission is granted to process this file through TeX and print the
33results, provided the printed document carries copying permission
34notice identical to this one except for the removal of this paragraph
35(this paragraph not being relevant to the printed manual).
36
37@end ignore
38Permission is granted to copy and distribute modified versions of this
39manual under the conditions for verbatim copying, provided that the entire
40resulting derived work is distributed under the terms of a permission
41notice identical to this one.
42
43Permission is granted to copy and distribute translations of this manual
44into another language, under the above conditions for modified versions,
45except that this permission notice may be stated in a translation approved
46by the Foundation.
47@end ifinfo
48
49@setchapternewpage odd
50@titlepage
51@sp 11
52@center @titlefont{GNU Readline Library}
53@sp 2
54@center by Brian Fox
55@sp 2
56@center Version 1.0
57@sp 2
58@center February 1989
59
60@comment Include the Distribution inside the titlepage environment so
61@c that headings are turned off.
62
63@page
64@vskip 0pt plus 1filll
65Copyright @copyright{} 1989 Free Software Foundation, Inc.
66
67@sp 2
68This document describes the GNU Readline Library, a utility for aiding
69in the consistency of user interface across discrete programs that need
70to provide a command line interface.
71@sp 2
72
73Published by the Free Software Foundation @*
74675 Massachusetts Avenue, @*
75Cambridge, MA 02139 USA
76
77Permission is granted to make and distribute verbatim copies of
78this manual provided the copyright notice and this permission notice
79are preserved on all copies.
80
81Permission is granted to copy and distribute modified versions of this
82manual under the conditions for verbatim copying, provided that the entire
83resulting derived work is distributed under the terms of a permission
84notice identical to this one.
85
86Permission is granted to copy and distribute translations of this manual
87into another language, under the above conditions for modified versions,
88except that this permission notice may be stated in a translation approved
89by the Foundation.
90
91@end titlepage
92
9dd0e793 93@node Top, , ,(DIR)
681fa9a8
RP
94@chapter GNU Readline Library
95
96@ifinfo
97This document describes the GNU Readline Library, a utility for aiding
98in the consistency of user interface across discrete programs that need
99to provide a command line interface.
100@end ifinfo
101
102@menu
9dd0e793
RP
103* Command Line Editing:: GNU Readline User's Manual
104* Readline Technical:: GNU Readline Programmer's Manual
681fa9a8 105@end menu
3558ead5 106@include inc-read.texi
681fa9a8
RP
107@node Readline Technical, , Top, Top
108@chapter Readline Programmer's Manual
109
110This manual describes the interface between the GNU Readline Library and
111user programs. If you are a programmer, and you wish to include the
112features found in GNU Readline in your own programs, such as completion,
113line editing, and interactive history manipulation, this documentation
114is for you.
115
116@menu
117* Default Behaviour:: Using the default behaviour of Readline.
118* Custom Functions:: Adding your own functions to Readline.
119* Custom Completers:: Supplanting or supplementing Readline's
120 completion functions.
121* Variable Index:: Index of externally tweakable variables.
122@end menu
123
124@node Default Behaviour, Custom Functions, Readline Technical, Readline Technical
125@section Default Behaviour
126
127Many programs provide a command line interface, such as @code{mail},
128@code{ftp}, and @code{sh}. For such programs, the default behaviour of
129Readline is sufficient. This section describes how to use Readline in
130the simplest way possible, perhaps to replace calls in your code to
131@code{gets ()}.
132
133@findex readline ()
134@cindex readline, function
135The function @code{readline} prints a prompt and then reads and returns
136a single line of text from the user. The line which @code{readline ()}
137returns is allocated with @code{malloc ()}; you should @code{free ()}
138the line when you are done with it. The declaration in ANSI C is
139
140@example
141@code{char *readline (char *@var{prompt});}
142@end example
143or, preferably,
144@example
145@code{#include <readline/readline.h>}
146@end example
147
148So, one might say
149@example
150@code{char *line = readline ("Enter a line: ");}
151@end example
152in order to read a line of text from the user.
153
154The line which is returned has the final newline removed, so only the
155text of the line remains.
156
157If readline encounters an EOF while reading the line, and the line is
158empty at that point, then @code{(char *)NULL} is returned. Otherwise,
159the line is ended just as if a newline was typed.
160
161If you want the user to be able to get at the line later, (with
162@key{C-p} for example), you must call @code{add_history ()} to save the
163line away in a @dfn{history} list of such lines.
164
165@example
166@code{add_history (line)};
167@end example
168
169If you use @code{add_history ()}, you should also
170@code{#include <readline/history.h>}
171For full details on the GNU History Library, see the associated manual.
172
173It is polite to avoid saving empty lines on the history list, since
174no one has a burning need to reuse a blank line. Here is a function
175which usefully replaces the standard @code{gets ()} library function:
176
177@example
178#include <readline/readline.h>
179#include <readline/history.h>
180
181/* A static variable for holding the line. */
182static char *my_gets_line = (char *)NULL;
183
184/* Read a string, and return a pointer to it. Returns NULL on EOF. */
185char *
186my_gets ()
187@{
188 /* If the buffer has already been allocated, return the memory
189 to the free pool. */
190 if (my_gets_line != (char *)NULL)
191 free (my_gets_line);
192
193 /* Get a line from the user. */
194 my_gets_line = readline ("");
195
196 /* If the line has any text in it, save it on the history. */
197 if (my_get_line && *my_gets_line)
198 add_history (my_gets_line);
199
200 return (my_gets_line);
201@}
202@end example
203
204The above code gives the user the default behaviour of @key{TAB}
205completion: completion on file names. If you do not want readline to
206complete on filenames, you can change the binding of the @key{TAB} key
207with @code{rl_bind_key ()}.
208
209@findex rl_bind_key ()
210
211@example
212@code{int rl_bind_key (int @var{key}, (int (*)())@var{function});}
213@end example
214
215@code{rl_bind_key ()} takes 2 arguments; @var{key} is the character that
216you want to bind, and @var{function} is the address of the function to
217run when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert ()}
218makes @key{TAB} just insert itself.
219
220@code{rl_bind_key ()} returns non-zero if @var{key} is not a valid
221ASCII character code (between 0 and 255).
222
223@example
224@code{rl_bind_key ('\t', rl_insert);}
225@end example
226
227@node Custom Functions, Custom Completers, Default Behaviour, Readline Technical
228@section Custom Functions
229
230Readline provides a great many functions for manipulating the text of
231the line. But it isn't possible to anticipate the needs of all
232programs. This section describes the various functions and variables
233defined in within the Readline library which allow a user program to add
234customized functionality to Readline.
235
236@menu
237* The Function Type:: C declarations to make code readable.
238* Function Naming:: How to give a function you write a name.
239* Keymaps:: Making keymaps.
240* Binding Keys:: Changing Keymaps.
241* Function Writing:: Variables and calling conventions.
242* Allowing Undoing:: How to make your functions undoable.
243@end menu
244
245@node The Function Type, Function Naming, Custom Functions, Custom Functions
246For the sake of readabilty, we declare a new type of object, called
247@dfn{Function}. `Function' is a C language function which returns an
248@code{int}. The type declaration for `Function' is:
249
250@code{typedef int Function ();}
251
252The reason for declaring this new type is to make it easier to discuss
253pointers to C functions. Let us say we had a variable called @var{func}
254which was a pointer to a function. Instead of the classic C declaration
255
256@code{int (*)()func;}
257
258we have
259
260@code{Function *func;}
261
262@node Function Naming, Keymaps, The Function Type, Custom Functions
263@subsection Naming a Function
264
265The user can dynamically change the bindings of keys while using
266Readline. This is done by representing the function with a descriptive
267name. The user is able to type the descriptive name when referring to
268the function. Thus, in an init file, one might find
269
270@example
271Meta-Rubout: backward-kill-word
272@end example
273
274This binds @key{Meta-Rubout} to the function @emph{descriptively} named
275@code{backward-kill-word}. You, as a programmer, should bind the
276functions you write to descriptive names as well. Here is how to do
277that.
278
279@defun rl_add_defun (char *name, Function *function, int key)
280Add @var{name} to the list of named functions. Make @var{function} be
281the function that gets called. If @var{key} is not -1, then bind it to
282@var{function} using @code{rl_bind_key ()}.
283@end defun
284
285Using this function alone is sufficient for most applications. It is
286the recommended way to add a few functions to the default functions that
287Readline has built in already. If you need to do more or different
288things than adding a function to Readline, you may need to use the
289underlying functions described below.
290
291@node Keymaps, Binding Keys, Function Naming, Custom Functions
292@subsection Selecting a Keymap
293
294Key bindings take place on a @dfn{keymap}. The keymap is the
295association between the keys that the user types and the functions that
296get run. You can make your own keymaps, copy existing keymaps, and tell
297Readline which keymap to use.
298
299@defun rl_make_bare_keymap ()
300Returns a new, empty keymap. The space for the keymap is allocated with
301@code{malloc ()}; you should @code{free ()} it when you are done.
302@end defun
303
304@defun rl_copy_keymap (Keymap map)
305Return a new keymap which is a copy of @var{map}.
306@end defun
307
308@defun rl_make_keymap ()
309Return a new keymap with the printing characters bound to rl_insert,
310the lowercase Meta characters bound to run their equivalents, and
311the Meta digits bound to produce numeric arguments.
312@end defun
313
314@node Binding Keys, Function Writing, Keymaps, Custom Functions
315@subsection Binding Keys
316
317You associate keys with functions through the keymap. Here are
318the functions for doing that.
319
320@defun rl_bind_key (int key, Function *function)
321Binds @var{key} to @var{function} in the currently selected keymap.
322Returns non-zero in the case of an invalid @var{key}.
323@end defun
324
325@defun rl_bind_key_in_map (int key, Function *function, Keymap map)
326Bind @var{key} to @var{function} in @var{map}. Returns non-zero in the case
327of an invalid @var{key}.
328@end defun
329
330@defun rl_unbind_key (int key)
331Make @var{key} do nothing in the currently selected keymap.
332Returns non-zero in case of error.
333@end defun
334
335@defun rl_unbind_key_in_map (int key, Keymap map)
336Make @var{key} be bound to the null function in @var{map}.
337Returns non-zero in case of error.
338@end defun
339
340@node Function Writing, Allowing Undoing, Binding Keys, Custom Functions
341@subsection Writing a New Function
342
343In order to write new functions for Readline, you need to know the
344calling conventions for keyboard invoked functions, and the names of the
345variables that describe the current state of the line gathered so far.
346
347@defvar char *rl_line_buffer
348This is the line gathered so far. You are welcome to modify the
349contents of this, but see Undoing, below.
350@end defvar
351
352@defvar int rl_point
353The offset of the current cursor position in @var{rl_line_buffer}.
354@end defvar
355
356@defvar int rl_end
357The number of characters present in @code{rl_line_buffer}. When
358@code{rl_point} is at the end of the line, then @code{rl_point} and
359@code{rl_end} are equal.
360@end defvar
361
362The calling sequence for a command @code{foo} looks like
363
364@example
365@code{foo (count, key)}
366@end example
367
368where @var{count} is the numeric argument (or 1 if defaulted) and
369@var{key} is the key that invoked this function.
370
371It is completely up to the function as to what should be done with the
372numeric argument; some functions use it as a repeat count, other
373functions as a flag, and some choose to ignore it. In general, if a
374function uses the numeric argument as a repeat count, it should be able
375to do something useful with a negative argument as well as a positive
376argument. At the very least, it should be aware that it can be passed a
377negative argument.
378
379@node Allowing Undoing, , Function Writing, Custom Functions
380@subsection Allowing Undoing
381
382Supporting the undo command is a painless thing to do, and makes your
383function much more useful to the end user. It is certainly easy to try
384something if you know you can undo it. I could use an undo function for
385the stock market.
386
387If your function simply inserts text once, or deletes text once, and it
388calls @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, then
389undoing is already done for you automatically, and you can safely skip
390this section.
391
392If you do multiple insertions or multiple deletions, or any combination
393of these operations, you will want to group them together into one
394operation. This can be done with @code{rl_begin_undo_group ()} and
395@code{rl_end_undo_group ()}.
396
397@defun rl_begin_undo_group ()
398Begins saving undo information in a group construct. The undo
399information usually comes from calls to @code{rl_insert_text ()} and
400@code{rl_delete_text ()}, but they could be direct calls to
401@code{rl_add_undo ()}.
402@end defun
403
404@defun rl_end_undo_group ()
405Closes the current undo group started with @code{rl_begin_undo_group
406()}. There should be exactly one call to @code{rl_end_undo_group ()}
407for every call to @code{rl_begin_undo_group ()}.
408@end defun
409
410Finally, if you neither insert nor delete text, but directly modify the
411existing text (e.g. change its case), you call @code{rl_modifying ()}
412once, just before you modify the text. You must supply the indices of
413the text range that you are going to modify.
414
415@defun rl_modifying (int start, int end)
416Tell Readline to save the text between @var{start} and @var{end} as a
417single undo unit. It is assumed that subsequent to this call you will
418modify that range of text in some way.
419@end defun
420
421@subsection An Example
422
423Let us say that we are actually going to put an example here.
424
425@node Custom Completers, Variable Index, Custom Functions, Readline Technical
426
427Typically, a program that reads commands from the user has a way of
428disambiguating between commands and data. If your program is one of
429these, then it can provide completion for either commands, or data, or
430both commands and data. The following sections describe how your
431program and Readline cooperate to provide this service to end users.
432
433@menu
434@end menu
435
436@node Variable Index, , Custom Completers, Readline Technical
437@appendix Variable Index
438@printindex vr
439@contents
440
441@bye
442
This page took 0.051741 seconds and 4 git commands to generate.