]>
Commit | Line | Data |
---|---|---|
be9485d5 SG |
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 | |
7 | This document describes the GNU Readline Library, a utility for aiding | |
8 | in the consitency of user interface across discrete programs that need | |
9 | to provide a command line interface. | |
10 | ||
11 | Copyright (C) 1988 Free Software Foundation, Inc. | |
12 | ||
13 | Permission is granted to make and distribute verbatim copies of | |
14 | this manual provided the copyright notice and this permission notice | |
15 | pare preserved on all copies. | |
16 | ||
17 | @ignore | |
18 | Permission is granted to process this file through TeX and print the | |
19 | results, provided the printed document carries copying permission | |
20 | notice 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 | ||
24 | Permission is granted to copy and distribute modified versions of this | |
25 | manual under the conditions for verbatim copying, provided that the entire | |
26 | resulting derived work is distributed under the terms of a permission | |
27 | notice identical to this one. | |
28 | ||
29 | Permission is granted to copy and distribute translations of this manual | |
30 | into another language, under the above conditions for modified versions, | |
31 | except that this permission notice may be stated in a translation approved | |
32 | by the Foundation. | |
33 | @end ifinfo | |
34 | ||
35 | @node Programming with GNU Readline | |
36 | @chapter Programming with GNU Readline | |
37 | ||
38 | This manual describes the interface between the GNU Readline Library and | |
39 | user programs. If you are a programmer, and you wish to include the | |
40 | features found in GNU Readline in your own programs, such as completion, | |
41 | line editing, and interactive history manipulation, this documentation | |
42 | is for you. | |
43 | ||
44 | @menu | |
45 | * Default Behaviour:: Using the default behaviour of Readline. | |
46 | * Custom Functions:: Adding your own functions to Readline. | |
47 | * Custom Completers:: Supplanting or supplementing Readline's | |
48 | completion functions. | |
49 | @end menu | |
50 | ||
51 | @node Default Behaviour | |
52 | @section Default Behaviour | |
53 | ||
54 | Many programs provide a command line interface, such as @code{mail}, | |
55 | @code{ftp}, and @code{sh}. For such programs, the default behaviour of | |
56 | Readline is sufficient. This section describes how to use Readline in | |
57 | the simplest way possible, perhaps to replace calls in your code to | |
58 | @code{gets ()}. | |
59 | ||
60 | @findex readline () | |
61 | @cindex readline, function | |
62 | The function @code{readline} prints a prompt and then reads and returns | |
63 | a single line of text from the user. The line which @code{readline ()} | |
64 | returns is allocated with @code{malloc ()}; you should @code{free ()} | |
65 | the line when you are done with it. The declaration for @code{readline} | |
66 | in ANSI C is | |
67 | ||
68 | @example | |
69 | @code{char *readline (char *@var{prompt});} | |
70 | @end example | |
71 | ||
72 | So, one might say | |
73 | @example | |
74 | @code{char *line = readline ("Enter a line: ");} | |
75 | @end example | |
76 | in order to read a line of text from the user. | |
77 | ||
78 | The line which is returned has the final newline removed, so only the | |
79 | text of the line remains. | |
80 | ||
81 | If readline encounters an @code{EOF} while reading the line, and the | |
82 | line is empty at that point, then @code{(char *)NULL} is returned. | |
83 | Otherwise, the line is ended just as if a newline was typed. | |
84 | ||
85 | If you want the user to be able to get at the line later, (with | |
86 | @key{C-p} for example), you must call @code{add_history ()} to save the | |
87 | line away in a @dfn{history} list of such lines. | |
88 | ||
89 | @example | |
90 | @code{add_history (line)}; | |
91 | @end example | |
92 | ||
93 | For full details on the GNU History Library, see the associated manual. | |
94 | ||
95 | It is polite to avoid saving empty lines on the history list, since it | |
96 | is rare than someone has a burning need to reuse a blank line. Here is | |
97 | a function which usefully replaces the standard @code{gets ()} library | |
98 | function: | |
99 | ||
100 | @example | |
101 | /* A static variable for holding the line. */ | |
102 | static char *line_read = (char *)NULL; | |
103 | ||
104 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ | |
105 | char * | |
106 | do_gets () | |
107 | @{ | |
108 | /* If the buffer has already been allocated, return the memory | |
109 | to the free pool. */ | |
110 | if (line_read != (char *)NULL) | |
111 | @{ | |
112 | free (line_read); | |
113 | line_read = (char *)NULL; | |
114 | @} | |
115 | ||
116 | /* Get a line from the user. */ | |
117 | line_read = readline (""); | |
118 | ||
119 | /* If the line has any text in it, save it on the history. */ | |
120 | if (line_read && *line_read) | |
121 | add_history (line_read); | |
122 | ||
123 | return (line_read); | |
124 | @} | |
125 | @end example | |
126 | ||
127 | The above code gives the user the default behaviour of @key{TAB} | |
128 | completion: completion on file names. If you do not want readline to | |
129 | complete on filenames, you can change the binding of the @key{TAB} key | |
130 | with @code{rl_bind_key ()}. | |
131 | ||
132 | @findex rl_bind_key () | |
133 | @example | |
134 | @code{int rl_bind_key (int @var{key}, int (*@var{function})());} | |
135 | @end example | |
136 | ||
137 | @code{rl_bind_key ()} takes 2 arguments; @var{key} is the character that | |
138 | you want to bind, and @var{function} is the address of the function to | |
139 | run when @var{key} is pressed. Binding @key{TAB} to @code{rl_insert ()} | |
140 | makes @key{TAB} just insert itself. | |
141 | ||
142 | @code{rl_bind_key ()} returns non-zero if @var{key} is not a valid | |
143 | ASCII character code (between 0 and 255). | |
144 | ||
145 | @example | |
146 | @code{rl_bind_key ('\t', rl_insert);} | |
147 | @end example | |
148 | ||
149 | This code should be executed once at the start of your program; you | |
150 | might write a function called @code{initialize_readline ()} which | |
151 | performs this and other desired initializations, such as installing | |
152 | custom completers, etc. | |
153 | ||
154 | @node Custom Functions | |
155 | @section Custom Functions | |
156 | ||
157 | Readline provides a great many functions for manipulating the text of | |
158 | the line. But it isn't possible to anticipate the needs of all | |
159 | programs. This section describes the various functions and variables | |
160 | defined in within the Readline library which allow a user program to add | |
161 | customized functionality to Readline. | |
162 | ||
163 | @menu | |
164 | * The Function Type:: C declarations to make code readable. | |
165 | * Function Naming:: How to give a function you write a name. | |
166 | * Keymaps:: Making keymaps. | |
167 | * Binding Keys:: Changing Keymaps. | |
168 | * Function Writing:: Variables and calling conventions. | |
169 | * Allowing Undoing:: How to make your functions undoable. | |
170 | @end menu | |
171 | ||
172 | @node The Function Type | |
173 | @subsection The Function Type | |
174 | ||
175 | For the sake of readabilty, we declare a new type of object, called | |
176 | @dfn{Function}. A @code{Function} is a C language function which | |
177 | returns an @code{int}. The type declaration for @code{Function} is: | |
178 | ||
179 | @noindent | |
180 | @code{typedef int Function ();} | |
181 | ||
182 | The reason for declaring this new type is to make it easier to write | |
183 | code describing pointers to C functions. Let us say we had a variable | |
184 | called @var{func} which was a pointer to a function. Instead of the | |
185 | classic C declaration | |
186 | ||
187 | @code{int (*)()func;} | |
188 | ||
189 | we have | |
190 | ||
191 | @code{Function *func;} | |
192 | ||
193 | @node Function Naming | |
194 | @subsection Naming a Function | |
195 | ||
196 | The user can dynamically change the bindings of keys while using | |
197 | Readline. This is done by representing the function with a descriptive | |
198 | name. The user is able to type the descriptive name when referring to | |
199 | the function. Thus, in an init file, one might find | |
200 | ||
201 | @example | |
202 | Meta-Rubout: backward-kill-word | |
203 | @end example | |
204 | ||
205 | This binds the keystroke @key{Meta-Rubout} to the function | |
206 | @emph{descriptively} named @code{backward-kill-word}. You, as the | |
207 | programmer, should bind the functions you write to descriptive names as | |
208 | well. Readline provides a function for doing that: | |
209 | ||
210 | @defun rl_add_defun (char *name, Function *function, int key) | |
211 | Add @var{name} to the list of named functions. Make @var{function} be | |
212 | the function that gets called. If @var{key} is not -1, then bind it to | |
213 | @var{function} using @code{rl_bind_key ()}. | |
214 | @end defun | |
215 | ||
216 | Using this function alone is sufficient for most applications. It is | |
217 | the recommended way to add a few functions to the default functions that | |
218 | Readline has built in already. If you need to do more or different | |
219 | things than adding a function to Readline, you may need to use the | |
220 | underlying functions described below. | |
221 | ||
222 | @node Keymaps | |
223 | @subsection Selecting a Keymap | |
224 | ||
225 | Key bindings take place on a @dfn{keymap}. The keymap is the | |
226 | association between the keys that the user types and the functions that | |
227 | get run. You can make your own keymaps, copy existing keymaps, and tell | |
228 | Readline which keymap to use. | |
229 | ||
230 | @defun {Keymap rl_make_bare_keymap} () | |
231 | Returns a new, empty keymap. The space for the keymap is allocated with | |
232 | @code{malloc ()}; you should @code{free ()} it when you are done. | |
233 | @end defun | |
234 | ||
235 | @defun {Keymap rl_copy_keymap} (Keymap map) | |
236 | Return a new keymap which is a copy of @var{map}. | |
237 | @end defun | |
238 | ||
239 | @defun {Keymap rl_make_keymap} () | |
240 | Return a new keymap with the printing characters bound to rl_insert, | |
241 | the lowercase Meta characters bound to run their equivalents, and | |
242 | the Meta digits bound to produce numeric arguments. | |
243 | @end defun | |
244 | ||
245 | @node Binding Keys | |
246 | @subsection Binding Keys | |
247 | ||
248 | You associate keys with functions through the keymap. Here are | |
249 | functions for doing that. | |
250 | ||
251 | @defun {int rl_bind_key} (int key, Function *function) | |
252 | Binds @var{key} to @var{function} in the currently selected keymap. | |
253 | Returns non-zero in the case of an invalid @var{key}. | |
254 | @end defun | |
255 | ||
256 | @defun {int rl_bind_key_in_map} (int key, Function *function, Keymap map) | |
257 | Bind @var{key} to @var{function} in @var{map}. Returns non-zero in the case | |
258 | of an invalid @var{key}. | |
259 | @end defun | |
260 | ||
261 | @defun {int rl_unbind_key} (int key) | |
262 | Make @var{key} do nothing in the currently selected keymap. | |
263 | Returns non-zero in case of error. | |
264 | @end defun | |
265 | ||
266 | @defun {int rl_unbind_key_in_map} (int key, Keymap map) | |
267 | Make @var{key} be bound to the null function in @var{map}. | |
268 | Returns non-zero in case of error. | |
269 | @end defun | |
270 | ||
271 | @defun rl_generic_bind (int type, char *keyseq, char *data, Keymap map) | |
272 | Bind the key sequence represented by the string @var{keyseq} to the arbitrary | |
273 | pointer @var{data}. @var{type} says what kind of data is pointed to by | |
274 | @var{data}; right now this can be a function (@code{ISFUNC}), a macro | |
275 | (@code{ISMACR}), or a keymap (@code{ISKMAP}). This makes new keymaps as | |
276 | necessary. The initial place to do bindings is in @var{map}. | |
277 | @end defun | |
278 | ||
279 | @node Function Writing | |
280 | @subsection Writing a New Function | |
281 | ||
282 | In order to write new functions for Readline, you need to know the | |
283 | calling conventions for keyboard invoked functions, and the names of the | |
284 | variables that describe the current state of the line gathered so far. | |
285 | ||
286 | @defvar {char *rl_line_buffer} | |
287 | This is the line gathered so far. You are welcome to modify the | |
288 | contents of this, but see Undoing, below. | |
289 | @end defvar | |
290 | ||
291 | @defvar {int rl_point} | |
292 | The offset of the current cursor position in @var{rl_line_buffer}. | |
293 | @end defvar | |
294 | ||
295 | @defvar {int rl_end} | |
296 | The number of characters present in @code{rl_line_buffer}. When | |
297 | @code{rl_point} is at the end of the line, then @code{rl_point} and | |
298 | @code{rl_end} are equal. | |
299 | @end defvar | |
300 | ||
301 | The calling sequence for a command @code{foo} looks like | |
302 | ||
303 | @example | |
304 | @code{foo (int count, int key)} | |
305 | @end example | |
306 | ||
307 | where @var{count} is the numeric argument (or 1 if defaulted) and | |
308 | @var{key} is the key that invoked this function. | |
309 | ||
310 | It is completely up to the function as to what should be done with the | |
311 | numeric argument; some functions use it as a repeat count, other | |
312 | functions as a flag, and some choose to ignore it. In general, if a | |
313 | function uses the numeric argument as a repeat count, it should be able | |
314 | to do something useful with a negative argument as well as a positive | |
315 | argument. At the very least, it should be aware that it can be passed a | |
316 | negative argument. | |
317 | ||
318 | @node Allowing Undoing | |
319 | @subsection Allowing Undoing | |
320 | ||
321 | Supporting the undo command is a painless thing to do, and makes your | |
322 | functions much more useful to the end user. It is certainly easy to try | |
323 | something if you know you can undo it. I could use an undo function for | |
324 | the stock market. | |
325 | ||
326 | If your function simply inserts text once, or deletes text once, and it | |
327 | calls @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, then | |
328 | undoing is already done for you automatically, and you can safely skip | |
329 | this section. | |
330 | ||
331 | If you do multiple insertions or multiple deletions, or any combination | |
332 | of these operations, you should group them together into one operation. | |
333 | This can be done with @code{rl_begin_undo_group ()} and | |
334 | @code{rl_end_undo_group ()}. | |
335 | ||
336 | @defun rl_begin_undo_group () | |
337 | Begins saving undo information in a group construct. The undo | |
338 | information usually comes from calls to @code{rl_insert_text ()} and | |
339 | @code{rl_delete_text ()}, but they could be direct calls to | |
340 | @code{rl_add_undo ()}. | |
341 | @end defun | |
342 | ||
343 | @defun rl_end_undo_group () | |
344 | Closes the current undo group started with @code{rl_begin_undo_group | |
345 | ()}. There should be exactly one call to @code{rl_end_undo_group ()} | |
346 | for every call to @code{rl_begin_undo_group ()}. | |
347 | @end defun | |
348 | ||
349 | Finally, if you neither insert nor delete text, but directly modify the | |
350 | existing text (e.g. change its case), you call @code{rl_modifying ()} | |
351 | once, just before you modify the text. You must supply the indices of | |
352 | the text range that you are going to modify. | |
353 | ||
354 | @defun rl_modifying (int start, int end) | |
355 | Tell Readline to save the text between @var{start} and @var{end} as a | |
356 | single undo unit. It is assumed that subsequent to this call you will | |
357 | modify that range of text in some way. | |
358 | @end defun | |
359 | ||
360 | @subsection An Example | |
361 | ||
362 | Here is a function which changes lowercase characters to the uppercase | |
363 | equivalents, and uppercase characters to the lowercase equivalents. If | |
364 | this function was bound to @samp{M-c}, then typing @samp{M-c} would | |
365 | change the case of the character under point. Typing @samp{10 M-c} | |
366 | would change the case of the following 10 characters, leaving the cursor on | |
367 | the last character changed. | |
368 | ||
369 | @example | |
370 | /* Invert the case of the COUNT following characters. */ | |
371 | invert_case_line (count, key) | |
372 | int count, key; | |
373 | @{ | |
374 | register int start, end; | |
375 | ||
376 | start = rl_point; | |
377 | ||
378 | if (count < 0) | |
379 | @{ | |
380 | direction = -1; | |
381 | count = -count; | |
382 | @} | |
383 | else | |
384 | direction = 1; | |
385 | ||
386 | /* Find the end of the range to modify. */ | |
387 | end = start + (count * direction); | |
388 | ||
389 | /* Force it to be within range. */ | |
390 | if (end > rl_end) | |
391 | end = rl_end; | |
392 | else if (end < 0) | |
393 | end = -1; | |
394 | ||
395 | if (start > end) | |
396 | @{ | |
397 | int temp = start; | |
398 | start = end; | |
399 | end = temp; | |
400 | @} | |
401 | ||
402 | if (start == end) | |
403 | return; | |
404 | ||
405 | /* Tell readline that we are modifying the line, so save the undo | |
406 | information. */ | |
407 | rl_modifying (start, end); | |
408 | ||
409 | for (; start != end; start += direction) | |
410 | @{ | |
411 | if (uppercase_p (rl_line_buffer[start])) | |
412 | rl_line_buffer[start] = to_lower (rl_line_buffer[start]); | |
413 | else if (lowercase_p (rl_line_buffer[start])) | |
414 | rl_line_buffer[start] = to_upper (rl_line_buffer[start]); | |
415 | @} | |
416 | /* Move point to on top of the last character changed. */ | |
417 | rl_point = end - direction; | |
418 | @} | |
419 | @end example | |
420 | ||
421 | @node Custom Completers | |
422 | @section Custom Completers | |
423 | ||
424 | Typically, a program that reads commands from the user has a way of | |
425 | disambiguating commands and data. If your program is one of these, then | |
426 | it can provide completion for either commands, or data, or both commands | |
427 | and data. The following sections describe how your program and Readline | |
428 | cooperate to provide this service to end users. | |
429 | ||
430 | @menu | |
431 | * How Completing Works:: The logic used to do completion. | |
432 | * Completion Functions:: Functions provided by Readline. | |
433 | * Completion Variables:: Variables which control completion. | |
434 | * A Short Completion Example:: An example of writing completer subroutines. | |
435 | @end menu | |
436 | ||
437 | @node How Completing Works | |
438 | @subsection How Completing Works | |
439 | ||
440 | In order to complete some text, the full list of possible completions | |
441 | must be available. That is to say, it is not possible to accurately | |
442 | expand a partial word without knowing what all of the possible words | |
443 | that make sense in that context are. The GNU Readline library provides | |
444 | the user interface to completion, and additionally, two of the most common | |
445 | completion functions; filename and username. For completing other types | |
446 | of text, you must write your own completion function. This section | |
447 | describes exactly what those functions must do, and provides an example | |
448 | function. | |
449 | ||
450 | There are three major functions used to perform completion: | |
451 | ||
452 | @enumerate | |
453 | @item | |
454 | The user-interface function @code{rl_complete ()}. This function is | |
455 | called interactively with the same calling conventions as other | |
456 | functions in readline intended for interactive use; i.e. @var{count}, | |
457 | and @var{invoking-key}. It isolates the word to be completed and calls | |
458 | @code{completion_matches ()} to generate a list of possible completions. | |
459 | It then either lists the possible completions or actually performs the | |
460 | completion, depending on which behaviour is desired. | |
461 | ||
462 | @item | |
463 | The internal function @code{completion_matches ()} uses your | |
464 | @dfn{generator} function to generate the list of possible matches, and | |
465 | then returns the array of these matches. You should place the address | |
466 | of your generator function in @code{rl_completion_entry_function}. | |
467 | ||
468 | @item | |
469 | The generator function is called repeatedly from | |
470 | @code{completion_matches ()}, returning a string each time. The | |
471 | arguments to the generator function are @var{text} and @var{state}. | |
472 | @var{text} is the partial word to be completed. @var{state} is zero the | |
473 | first time the function is called, and a positive non-zero integer for | |
474 | each subsequent call. When the generator function returns @code{(char | |
475 | *)NULL} this signals @code{completion_matches ()} that there are no more | |
476 | possibilities left. | |
477 | ||
478 | @end enumerate | |
479 | ||
480 | @defun rl_complete (int ignore, int invoking_key) | |
481 | Complete the word at or before point. You have supplied the function | |
482 | that does the initial simple matching selection algorithm (see | |
483 | @code{completion_matches ()}). The default is to do filename completion. | |
484 | @end defun | |
485 | ||
486 | Note that @code{rl_complete ()} has the identical calling conventions as | |
487 | any other key-invokable function; this is because by default it is bound | |
488 | to the @samp{TAB} key. | |
489 | ||
490 | @defvar {Function *rl_completion_entry_function} | |
491 | This is a pointer to the generator function for @code{completion_matches | |
492 | ()}. If the value of @code{rl_completion_entry_function} is | |
493 | @code{(Function *)NULL} then the default filename generator function is | |
494 | used, namely @code{filename_entry_function ()}. | |
495 | @end defvar | |
496 | ||
497 | @node Completion Functions | |
498 | @subsection Completion Functions | |
499 | ||
500 | Here is the complete list of callable completion functions present in | |
501 | Readline. | |
502 | ||
503 | @defun rl_complete_internal (int what_to_do) | |
504 | Complete the word at or before point. @var{what_to_do} says what to do | |
505 | with the completion. A value of @samp{?} means list the possible | |
506 | completions. @samp{TAB} means do standard completion. @samp{*} means | |
507 | insert all of the possible completions. | |
508 | @end defun | |
509 | ||
510 | @defun rl_complete (int ignore, int invoking_key) | |
511 | Complete the word at or before point. You have supplied the function | |
512 | that does the initial simple matching selection algorithm (see | |
513 | @code{completion_matches ()}). The default is to do filename | |
514 | completion. This just calls @code{rl_complete_internal ()} with an | |
515 | argument of @samp{TAB}. | |
516 | @end defun | |
517 | ||
518 | @defun rl_possible_completions () | |
519 | List the possible completions. See description of @code{rl_complete | |
520 | ()}. This just calls @code{rl_complete_internal ()} with an argument of | |
521 | @samp{?}. | |
522 | @end defun | |
523 | ||
524 | @defun {char **completion_matches} (char *text, char *(*entry_function) ()) | |
525 | Returns an array of @code{(char *)} which is a list of completions for | |
526 | @var{text}. If there are no completions, returns @code{(char **)NULL}. | |
527 | The first entry in the returned array is the substitution for @var{text}. | |
528 | The remaining entries are the possible completions. The array is | |
529 | terminated with a @code{NULL} pointer. | |
530 | ||
531 | @var{entry_function} is a function of two args, and returns a | |
532 | @code{(char *)}. The first argument is @var{text}. The second is a | |
533 | state argument; it is zero on the first call, and non-zero on subsequent | |
534 | calls. It returns a @code{NULL} pointer to the caller when there are | |
535 | no more matches. | |
536 | @end defun | |
537 | ||
538 | @defun {char *filename_completion_function} (char *text, int state) | |
539 | A generator function for filename completion in the general case. Note | |
540 | that completion in the Bash shell is a little different because of all | |
541 | the pathnames that must be followed when looking up the completion for a | |
542 | command. | |
543 | @end defun | |
544 | ||
545 | @defun {char *username_completion_function} (char *text, int state) | |
546 | A completion generator for usernames. @var{text} contains a partial | |
547 | username preceded by a random character (usually @samp{~}). | |
548 | @end defun | |
549 | ||
550 | @node Completion Variables | |
551 | @subsection Completion Variables | |
552 | ||
553 | @defvar {Function *rl_completion_entry_function} | |
554 | A pointer to the generator function for @code{completion_matches ()}. | |
555 | @code{NULL} means to use @code{filename_entry_function ()}, the default | |
556 | filename completer. | |
557 | @end defvar | |
558 | ||
559 | @defvar {Function *rl_attempted_completion_function} | |
560 | A pointer to an alternative function to create matches. | |
561 | The function is called with @var{text}, @var{start}, and @var{end}. | |
562 | @var{start} and @var{end} are indices in @code{rl_line_buffer} saying | |
563 | what the boundaries of @var{text} are. If this function exists and | |
564 | returns @code{NULL} then @code{rl_complete ()} will call the value of | |
565 | @code{rl_completion_entry_function} to generate matches, otherwise the | |
566 | array of strings returned will be used. | |
567 | @end defvar | |
568 | ||
569 | @defvar {int rl_completion_query_items} | |
570 | Up to this many items will be displayed in response to a | |
571 | possible-completions call. After that, we ask the user if she is sure | |
572 | she wants to see them all. The default value is 100. | |
573 | @end defvar | |
574 | ||
575 | @defvar {char *rl_basic_word_break_characters} | |
576 | The basic list of characters that signal a break between words for the | |
577 | completer routine. The contents of this variable is what breaks words | |
578 | in the Bash shell, i.e. " \t\n\"\\'`@@$><=;|&@{(". | |
579 | @end defvar | |
580 | ||
581 | @defvar {char *rl_completer_word_break_characters} | |
582 | The list of characters that signal a break between words for | |
583 | @code{rl_complete_internal ()}. The default list is the contents of | |
584 | @code{rl_basic_word_break_characters}. | |
585 | @end defvar | |
586 | ||
587 | @defvar {char *rl_special_prefixes} | |
588 | The list of characters that are word break characters, but should be | |
589 | left in @var{text} when it is passed to the completion function. | |
590 | Programs can use this to help determine what kind of completing to do. | |
591 | @end defvar | |
592 | ||
593 | @defvar {int rl_ignore_completion_duplicates} | |
594 | If non-zero, then disallow duplicates in the matches. Default is 1. | |
595 | @end defvar | |
596 | ||
597 | @defvar {int rl_filename_completion_desired} | |
598 | Non-zero means that the results of the matches are to be treated as | |
599 | filenames. This is @emph{always} zero on entry, and can only be changed | |
600 | within a completion entry generator function. | |
601 | @end defvar | |
602 | ||
603 | @defvar {Function *rl_ignore_some_completions_function} | |
604 | This function, if defined, is called by the completer when real filename | |
605 | completion is done, after all the matching names have been generated. | |
606 | It is passed a @code{NULL} terminated array of @code{(char *)} known as | |
607 | @var{matches} in the code. The 1st element (@code{matches[0]}) is the | |
608 | maximal substring that is common to all matches. This function can | |
609 | re-arrange the list of matches as required, but each deleted element of | |
610 | the array must be @code{free()}'d. | |
611 | @end defvar | |
612 | ||
613 | @node A Short Completion Example | |
614 | @subsection A Short Completion Example | |
615 | ||
616 | Here is a small application demonstrating the use of the GNU Readline | |
617 | library. It is called @code{fileman}, and the source code resides in | |
618 | @file{readline/examples/fileman.c}. This sample application provides | |
619 | completion of command names, line editing features, and access to the | |
620 | history list. | |
621 | ||
622 | @page | |
623 | @smallexample | |
624 | /* fileman.c -- A tiny application which demonstrates how to use the | |
625 | GNU Readline library. This application interactively allows users | |
626 | to manipulate files and their modes. */ | |
627 | ||
628 | #include <stdio.h> | |
629 | #include <readline/readline.h> | |
630 | #include <readline/history.h> | |
631 | #include <sys/types.h> | |
632 | #include <sys/file.h> | |
633 | #include <sys/stat.h> | |
634 | #include <sys/errno.h> | |
635 | ||
636 | /* The names of functions that actually do the manipulation. */ | |
637 | int com_list (), com_view (), com_rename (), com_stat (), com_pwd (); | |
638 | int com_delete (), com_help (), com_cd (), com_quit (); | |
639 | ||
640 | /* A structure which contains information on the commands this program | |
641 | can understand. */ | |
642 | ||
643 | typedef struct @{ | |
644 | char *name; /* User printable name of the function. */ | |
645 | Function *func; /* Function to call to do the job. */ | |
646 | char *doc; /* Documentation for this function. */ | |
647 | @} COMMAND; | |
648 | ||
649 | COMMAND commands[] = @{ | |
650 | @{ "cd", com_cd, "Change to directory DIR" @}, | |
651 | @{ "delete", com_delete, "Delete FILE" @}, | |
652 | @{ "help", com_help, "Display this text" @}, | |
653 | @{ "?", com_help, "Synonym for `help'" @}, | |
654 | @{ "list", com_list, "List files in DIR" @}, | |
655 | @{ "ls", com_list, "Synonym for `list'" @}, | |
656 | @{ "pwd", com_pwd, "Print the current working directory" @}, | |
657 | @{ "quit", com_quit, "Quit using Fileman" @}, | |
658 | @{ "rename", com_rename, "Rename FILE to NEWNAME" @}, | |
659 | @{ "stat", com_stat, "Print out statistics on FILE" @}, | |
660 | @{ "view", com_view, "View the contents of FILE" @}, | |
661 | @{ (char *)NULL, (Function *)NULL, (char *)NULL @} | |
662 | @}; | |
663 | ||
664 | /* The name of this program, as taken from argv[0]. */ | |
665 | char *progname; | |
666 | ||
667 | /* When non-zero, this global means the user is done using this program. */ | |
668 | int done = 0; | |
669 | @page | |
670 | main (argc, argv) | |
671 | int argc; | |
672 | char **argv; | |
673 | @{ | |
674 | progname = argv[0]; | |
675 | ||
676 | initialize_readline (); /* Bind our completer. */ | |
677 | ||
678 | /* Loop reading and executing lines until the user quits. */ | |
679 | while (!done) | |
680 | @{ | |
681 | char *line; | |
682 | ||
683 | line = readline ("FileMan: "); | |
684 | ||
685 | if (!line) | |
686 | @{ | |
687 | done = 1; /* Encountered EOF at top level. */ | |
688 | @} | |
689 | else | |
690 | @{ | |
691 | /* Remove leading and trailing whitespace from the line. | |
692 | Then, if there is anything left, add it to the history list | |
693 | and execute it. */ | |
694 | stripwhite (line); | |
695 | ||
696 | if (*line) | |
697 | @{ | |
698 | add_history (line); | |
699 | execute_line (line); | |
700 | @} | |
701 | @} | |
702 | ||
703 | if (line) | |
704 | free (line); | |
705 | @} | |
706 | exit (0); | |
707 | @} | |
708 | ||
709 | /* Execute a command line. */ | |
710 | execute_line (line) | |
711 | char *line; | |
712 | @{ | |
713 | register int i; | |
714 | COMMAND *find_command (), *command; | |
715 | char *word; | |
716 | ||
717 | /* Isolate the command word. */ | |
718 | i = 0; | |
719 | while (line[i] && !whitespace (line[i])) | |
720 | i++; | |
721 | ||
722 | word = line; | |
723 | ||
724 | if (line[i]) | |
725 | line[i++] = '\0'; | |
726 | ||
727 | command = find_command (word); | |
728 | ||
729 | if (!command) | |
730 | @{ | |
731 | fprintf (stderr, "%s: No such command for FileMan.\n", word); | |
732 | return; | |
733 | @} | |
734 | ||
735 | /* Get argument to command, if any. */ | |
736 | while (whitespace (line[i])) | |
737 | i++; | |
738 | ||
739 | word = line + i; | |
740 | ||
741 | /* Call the function. */ | |
742 | (*(command->func)) (word); | |
743 | @} | |
744 | ||
745 | /* Look up NAME as the name of a command, and return a pointer to that | |
746 | command. Return a NULL pointer if NAME isn't a command name. */ | |
747 | COMMAND * | |
748 | find_command (name) | |
749 | char *name; | |
750 | @{ | |
751 | register int i; | |
752 | ||
753 | for (i = 0; commands[i].name; i++) | |
754 | if (strcmp (name, commands[i].name) == 0) | |
755 | return (&commands[i]); | |
756 | ||
757 | return ((COMMAND *)NULL); | |
758 | @} | |
759 | ||
760 | /* Strip whitespace from the start and end of STRING. */ | |
761 | stripwhite (string) | |
762 | char *string; | |
763 | @{ | |
764 | register int i = 0; | |
765 | ||
766 | while (whitespace (string[i])) | |
767 | i++; | |
768 | ||
769 | if (i) | |
770 | strcpy (string, string + i); | |
771 | ||
772 | i = strlen (string) - 1; | |
773 | ||
774 | while (i > 0 && whitespace (string[i])) | |
775 | i--; | |
776 | ||
777 | string[++i] = '\0'; | |
778 | @} | |
779 | @page | |
780 | /* **************************************************************** */ | |
781 | /* */ | |
782 | /* Interface to Readline Completion */ | |
783 | /* */ | |
784 | /* **************************************************************** */ | |
785 | ||
786 | /* Tell the GNU Readline library how to complete. We want to try to complete | |
787 | on command names if this is the first word in the line, or on filenames | |
788 | if not. */ | |
789 | initialize_readline () | |
790 | @{ | |
791 | char **fileman_completion (); | |
792 | ||
793 | /* Allow conditional parsing of the ~/.inputrc file. */ | |
794 | rl_readline_name = "FileMan"; | |
795 | ||
796 | /* Tell the completer that we want a crack first. */ | |
797 | rl_attempted_completion_function = (Function *)fileman_completion; | |
798 | @} | |
799 | ||
800 | /* Attempt to complete on the contents of TEXT. START and END show the | |
801 | region of TEXT that contains the word to complete. We can use the | |
802 | entire line in case we want to do some simple parsing. Return the | |
803 | array of matches, or NULL if there aren't any. */ | |
804 | char ** | |
805 | fileman_completion (text, start, end) | |
806 | char *text; | |
807 | int start, end; | |
808 | @{ | |
809 | char **matches; | |
810 | char *command_generator (); | |
811 | ||
812 | matches = (char **)NULL; | |
813 | ||
814 | /* If this word is at the start of the line, then it is a command | |
815 | to complete. Otherwise it is the name of a file in the current | |
816 | directory. */ | |
817 | if (start == 0) | |
818 | matches = completion_matches (text, command_generator); | |
819 | ||
820 | return (matches); | |
821 | @} | |
822 | ||
823 | /* Generator function for command completion. STATE lets us know whether | |
824 | to start from scratch; without any state (i.e. STATE == 0), then we | |
825 | start at the top of the list. */ | |
826 | char * | |
827 | command_generator (text, state) | |
828 | char *text; | |
829 | int state; | |
830 | @{ | |
831 | static int list_index, len; | |
832 | char *name; | |
833 | ||
834 | /* If this is a new word to complete, initialize now. This includes | |
835 | saving the length of TEXT for efficiency, and initializing the index | |
836 | variable to 0. */ | |
837 | if (!state) | |
838 | @{ | |
839 | list_index = 0; | |
840 | len = strlen (text); | |
841 | @} | |
842 | ||
843 | /* Return the next name which partially matches from the command list. */ | |
844 | while (name = commands[list_index].name) | |
845 | @{ | |
846 | list_index++; | |
847 | ||
848 | if (strncmp (name, text, len) == 0) | |
849 | return (name); | |
850 | @} | |
851 | ||
852 | /* If no names matched, then return NULL. */ | |
853 | return ((char *)NULL); | |
854 | @} | |
855 | @page | |
856 | /* **************************************************************** */ | |
857 | /* */ | |
858 | /* FileMan Commands */ | |
859 | /* */ | |
860 | /* **************************************************************** */ | |
861 | ||
862 | /* String to pass to system (). This is for the LIST, VIEW and RENAME | |
863 | commands. */ | |
864 | static char syscom[1024]; | |
865 | ||
866 | /* List the file(s) named in arg. */ | |
867 | com_list (arg) | |
868 | char *arg; | |
869 | @{ | |
870 | if (!arg) | |
871 | arg = "*"; | |
872 | ||
873 | sprintf (syscom, "ls -FClg %s", arg); | |
874 | system (syscom); | |
875 | @} | |
876 | ||
877 | com_view (arg) | |
878 | char *arg; | |
879 | @{ | |
880 | if (!valid_argument ("view", arg)) | |
881 | return; | |
882 | ||
883 | sprintf (syscom, "cat %s | more", arg); | |
884 | system (syscom); | |
885 | @} | |
886 | ||
887 | com_rename (arg) | |
888 | char *arg; | |
889 | @{ | |
890 | too_dangerous ("rename"); | |
891 | @} | |
892 | ||
893 | com_stat (arg) | |
894 | char *arg; | |
895 | @{ | |
896 | struct stat finfo; | |
897 | ||
898 | if (!valid_argument ("stat", arg)) | |
899 | return; | |
900 | ||
901 | if (stat (arg, &finfo) == -1) | |
902 | @{ | |
903 | perror (arg); | |
904 | return; | |
905 | @} | |
906 | ||
907 | printf ("Statistics for `%s':\n", arg); | |
908 | ||
909 | printf ("%s has %d link%s, and is %d bytes in length.\n", arg, | |
910 | finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s", finfo.st_size); | |
911 | printf (" Created on: %s", ctime (&finfo.st_ctime)); | |
912 | printf (" Last access at: %s", ctime (&finfo.st_atime)); | |
913 | printf ("Last modified at: %s", ctime (&finfo.st_mtime)); | |
914 | @} | |
915 | ||
916 | com_delete (arg) | |
917 | char *arg; | |
918 | @{ | |
919 | too_dangerous ("delete"); | |
920 | @} | |
921 | ||
922 | /* Print out help for ARG, or for all of the commands if ARG is | |
923 | not present. */ | |
924 | com_help (arg) | |
925 | char *arg; | |
926 | @{ | |
927 | register int i; | |
928 | int printed = 0; | |
929 | ||
930 | for (i = 0; commands[i].name; i++) | |
931 | @{ | |
932 | if (!*arg || (strcmp (arg, commands[i].name) == 0)) | |
933 | @{ | |
934 | printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc); | |
935 | printed++; | |
936 | @} | |
937 | @} | |
938 | ||
939 | if (!printed) | |
940 | @{ | |
941 | printf ("No commands match `%s'. Possibilties are:\n", arg); | |
942 | ||
943 | for (i = 0; commands[i].name; i++) | |
944 | @{ | |
945 | /* Print in six columns. */ | |
946 | if (printed == 6) | |
947 | @{ | |
948 | printed = 0; | |
949 | printf ("\n"); | |
950 | @} | |
951 | ||
952 | printf ("%s\t", commands[i].name); | |
953 | printed++; | |
954 | @} | |
955 | ||
956 | if (printed) | |
957 | printf ("\n"); | |
958 | @} | |
959 | @} | |
960 | ||
961 | /* Change to the directory ARG. */ | |
962 | com_cd (arg) | |
963 | char *arg; | |
964 | @{ | |
965 | if (chdir (arg) == -1) | |
966 | perror (arg); | |
967 | ||
968 | com_pwd (""); | |
969 | @} | |
970 | ||
971 | /* Print out the current working directory. */ | |
972 | com_pwd (ignore) | |
973 | char *ignore; | |
974 | @{ | |
975 | char dir[1024]; | |
976 | ||
977 | (void) getwd (dir); | |
978 | ||
979 | printf ("Current directory is %s\n", dir); | |
980 | @} | |
981 | ||
982 | /* The user wishes to quit using this program. Just set DONE non-zero. */ | |
983 | com_quit (arg) | |
984 | char *arg; | |
985 | @{ | |
986 | done = 1; | |
987 | @} | |
988 | ||
989 | /* Function which tells you that you can't do this. */ | |
990 | too_dangerous (caller) | |
991 | char *caller; | |
992 | @{ | |
993 | fprintf (stderr, | |
994 | "%s: Too dangerous for me to distribute. Write it yourself.\n", | |
995 | caller); | |
996 | @} | |
997 | ||
998 | /* Return non-zero if ARG is a valid argument for CALLER, else print | |
999 | an error message and return zero. */ | |
1000 | int | |
1001 | valid_argument (caller, arg) | |
1002 | char *caller, *arg; | |
1003 | @{ | |
1004 | if (!arg || !*arg) | |
1005 | @{ | |
1006 | fprintf (stderr, "%s: Argument required.\n", caller); | |
1007 | return (0); | |
1008 | @} | |
1009 | ||
1010 | return (1); | |
1011 | @} | |
1012 | @end smallexample |