Import of readline 4.3.
[external/binutils.git] / readline / doc / history.3
1 .\"
2 .\" MAN PAGE COMMENTS to
3 .\"
4 .\"     Chet Ramey
5 .\"     Information Network Services
6 .\"     Case Western Reserve University
7 .\"     chet@ins.CWRU.Edu
8 .\"
9 .\"     Last Change: Thu Jan 31 16:08:07 EST 2002
10 .\"
11 .TH HISTORY 3 "2002 January 31" "GNU History 4.3"
12 .\"
13 .\" File Name macro.  This used to be `.PN', for Path Name,
14 .\" but Sun doesn't seem to like that very much.
15 .\"
16 .de FN
17 \fI\|\\$1\|\fP
18 ..
19 .ds lp \fR\|(\fP
20 .ds rp \fR\|)\fP
21 .\" FnN return-value fun-name N arguments
22 .de Fn1
23 \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3\fP\\*(rp
24 .br
25 ..
26 .de Fn2
27 .if t \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3,\|\\$4\fP\\*(rp
28 .if n \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3, \\$4\fP\\*(rp
29 .br
30 ..
31 .de Fn3
32 .if t \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3,\|\\$4,\|\\$5\fP\|\\*(rp
33 .if n \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3, \\$4, \\$5\fP\\*(rp
34 .br
35 ..
36 .de Vb
37 \fI\\$1\fP \fB\\$2\fP
38 .br
39 ..
40 .SH NAME
41 history \- GNU History Library
42 .SH COPYRIGHT
43 .if t The GNU History Library is Copyright \(co 1989-2002 by the Free Software Foundation, Inc.
44 .if n The GNU History Library is Copyright (C) 1989-2002 by the Free Software Foundation, Inc.
45 .SH DESCRIPTION
46 Many programs read input from the user a line at a time.  The GNU
47 History library is able to keep track of those lines, associate arbitrary
48 data with each line, and utilize information from previous lines in
49 composing new ones. 
50 .PP
51 .SH "HISTORY EXPANSION"
52 .PP
53 The history library supports a history expansion feature that
54 is identical to the history expansion in
55 .BR bash.
56 This section describes what syntax features are available.
57 .PP
58 History expansions introduce words from the history list into
59 the input stream, making it easy to repeat commands, insert the
60 arguments to a previous command into the current input line, or
61 fix errors in previous commands quickly.
62 .PP
63 History expansion is usually performed immediately after a complete line
64 is read.
65 It takes place in two parts.
66 The first is to determine which line from the history list
67 to use during substitution.
68 The second is to select portions of that line for inclusion into
69 the current one.
70 The line selected from the history is the \fIevent\fP,
71 and the portions of that line that are acted upon are \fIwords\fP.
72 Various \fImodifiers\fP are available to manipulate the selected words.
73 The line is broken into words in the same fashion as \fBbash\fP
74 does when reading input,
75 so that several words that would otherwise be separated 
76 are considered one word when surrounded by quotes (see the
77 description of \fBhistory_tokenize()\fP below).
78 History expansions are introduced by the appearance of the
79 history expansion character, which is \^\fB!\fP\^ by default.
80 Only backslash (\^\fB\e\fP\^) and single quotes can quote
81 the history expansion character.
82 .SS Event Designators
83 .PP
84 An event designator is a reference to a command line entry in the
85 history list.
86 .PP
87 .PD 0
88 .TP
89 .B !
90 Start a history substitution, except when followed by a
91 .BR blank ,
92 newline, = or (.
93 .TP
94 .B !\fIn\fR
95 Refer to command line
96 .IR n .
97 .TP
98 .B !\-\fIn\fR
99 Refer to the current command line minus
100 .IR n .
101 .TP
102 .B !!
103 Refer to the previous command.  This is a synonym for `!\-1'.
104 .TP
105 .B !\fIstring\fR
106 Refer to the most recent command starting with 
107 .IR string .
108 .TP
109 .B !?\fIstring\fR\fB[?]\fR
110 Refer to the most recent command containing
111 .IR string .
112 The trailing \fB?\fP may be omitted if
113 .I string
114 is followed immediately by a newline.
115 .TP
116 .B \d\s+2^\s-2\u\fIstring1\fP\d\s+2^\s-2\u\fIstring2\fP\d\s+2^\s-2\u
117 Quick substitution.  Repeat the last command, replacing
118 .I string1
119 with
120 .IR string2 .
121 Equivalent to
122 ``!!:s/\fIstring1\fP/\fIstring2\fP/''
123 (see \fBModifiers\fP below).
124 .TP
125 .B !#
126 The entire command line typed so far.
127 .PD
128 .SS Word Designators
129 .PP
130 Word designators are used to select desired words from the event.
131
132 .B :
133 separates the event specification from the word designator.
134 It may be omitted if the word designator begins with a
135 .BR ^ ,
136 .BR $ ,
137 .BR * ,
138 .BR \- ,
139 or
140 .BR % .
141 Words are numbered from the beginning of the line,
142 with the first word being denoted by 0 (zero).
143 Words are inserted into the current line separated by single spaces.
144 .PP
145 .PD 0
146 .TP
147 .B 0 (zero)
148 The zeroth word.  For the shell, this is the command
149 word.
150 .TP
151 .I n
152 The \fIn\fRth word.
153 .TP
154 .B ^
155 The first argument.  That is, word 1.
156 .TP
157 .B $
158 The last argument.
159 .TP
160 .B %
161 The word matched by the most recent `?\fIstring\fR?' search.
162 .TP
163 .I x\fB\-\fPy
164 A range of words; `\-\fIy\fR' abbreviates `0\-\fIy\fR'.
165 .TP
166 .B *
167 All of the words but the zeroth.  This is a synonym
168 for `\fI1\-$\fP'.  It is not an error to use
169 .B *
170 if there is just one
171 word in the event; the empty string is returned in that case.
172 .TP
173 .B x*
174 Abbreviates \fIx\-$\fP.
175 .TP
176 .B x\-
177 Abbreviates \fIx\-$\fP like \fBx*\fP, but omits the last word.
178 .PD
179 .PP
180 If a word designator is supplied without an event specification, the
181 previous command is used as the event.
182 .SS Modifiers
183 .PP
184 After the optional word designator, there may appear a sequence of
185 one or more of the following modifiers, each preceded by a `:'.
186 .PP
187 .PD 0
188 .PP
189 .TP
190 .B h
191 Remove a trailing file name component, leaving only the head.
192 .TP
193 .B t
194 Remove all leading file name components, leaving the tail.
195 .TP
196 .B r
197 Remove a trailing suffix of the form \fI.xxx\fP, leaving the
198 basename.
199 .TP
200 .B e
201 Remove all but the trailing suffix.
202 .TP
203 .B p
204 Print the new command but do not execute it.
205 .TP
206 .B q
207 Quote the substituted words, escaping further substitutions.
208 .TP
209 .B x
210 Quote the substituted words as with
211 .BR q ,
212 but break into words at
213 .B blanks
214 and newlines.
215 .TP
216 .B s/\fIold\fP/\fInew\fP/
217 Substitute
218 .I new
219 for the first occurrence of
220 .I old
221 in the event line.  Any delimiter can be used in place of /.  The
222 final delimiter is optional if it is the last character of the
223 event line.  The delimiter may be quoted in
224 .I old
225 and
226 .I new
227 with a single backslash.  If & appears in
228 .IR new ,
229 it is replaced by
230 .IR old .
231 A single backslash will quote the &.  If
232 .I old
233 is null, it is set to the last
234 .I old
235 substituted, or, if no previous history substitutions took place,
236 the last
237 .I string
238 in a
239 .B !?\fIstring\fR\fB[?]\fR
240 search.
241 .TP
242 .B &
243 Repeat the previous substitution.
244 .TP
245 .B g
246 Cause changes to be applied over the entire event line.  This is
247 used in conjunction with `\fB:s\fP' (e.g., `\fB:gs/\fIold\fP/\fInew\fP/\fR')
248 or `\fB:&\fP'.  If used with
249 `\fB:s\fP', any delimiter can be used
250 in place of /, and the final delimiter is optional
251 if it is the last character of the event line.
252 .PD
253 .SH "PROGRAMMING WITH HISTORY FUNCTIONS"
254 This section describes how to use the History library in other programs.
255 .SS Introduction to History
256 .PP
257 The programmer using the History library has available functions
258 for remembering lines on a history list, associating arbitrary data
259 with a line, removing lines from the list, searching through the list
260 for a line containing an arbitrary text string, and referencing any line
261 in the list directly.  In addition, a history \fIexpansion\fP function
262 is available which provides for a consistent user interface across
263 different programs.
264 .PP
265 The user using programs written with the History library has the
266 benefit of a consistent user interface with a set of well-known
267 commands for manipulating the text of previous lines and using that text
268 in new commands.  The basic history manipulation commands are
269 identical to
270 the history substitution provided by \fBbash\fP.
271 .PP
272 If the programmer desires, he can use the Readline library, which
273 includes some history manipulation by default, and has the added
274 advantage of command line editing.
275 .PP
276 Before declaring any functions using any functionality the History
277 library provides in other code, an application writer should include
278 the file
279 .FN <readline/history.h>
280 in any file that uses the
281 History library's features.  It supplies extern declarations for all
282 of the library's public functions and variables, and declares all of
283 the public data structures.
284
285 .SS History Storage
286 .PP
287 The history list is an array of history entries.  A history entry is
288 declared as follows:
289 .PP
290 .Vb "typedef void *" histdata_t;
291 .PP
292 .nf
293 typedef struct _hist_entry {
294   char *line;
295   histdata_t data;
296 } HIST_ENTRY;
297 .fi
298 .PP
299 The history list itself might therefore be declared as
300 .PP
301 .Vb "HIST_ENTRY **" the_history_list;
302 .PP
303 The state of the History library is encapsulated into a single structure:
304 .PP
305 .nf
306 /*
307  * A structure used to pass around the current state of the history.
308  */
309 typedef struct _hist_state {
310   HIST_ENTRY **entries; /* Pointer to the entries themselves. */
311   int offset;           /* The location pointer within this array. */
312   int length;           /* Number of elements within this array. */
313   int size;             /* Number of slots allocated to this array. */
314   int flags;
315 } HISTORY_STATE;
316 .fi
317 .PP
318 If the flags member includes \fBHS_STIFLED\fP, the history has been
319 stifled.
320 .SH "History Functions"
321 .PP
322 This section describes the calling sequence for the various functions
323 exported by the GNU History library.
324 .SS Initializing History and State Management
325 This section describes functions used to initialize and manage
326 the state of the History library when you want to use the history
327 functions in your program.
328
329 .Fn1 void using_history void
330 Begin a session in which the history functions might be used.  This
331 initializes the interactive variables.
332
333 .Fn1 "HISTORY_STATE *" history_get_history_state void
334 Return a structure describing the current state of the input history.
335
336 .Fn1 void history_set_history_state "HISTORY_STATE *state"
337 Set the state of the history list according to \fIstate\fP.
338
339 .SS History List Management
340
341 These functions manage individual entries on the history list, or set
342 parameters managing the list itself.
343
344 .Fn1 void add_history "const char *string"
345 Place \fIstring\fP at the end of the history list.  The associated data
346 field (if any) is set to \fBNULL\fP.
347
348 .Fn1 "HIST_ENTRY *" remove_history "int which"
349 Remove history entry at offset \fIwhich\fP from the history.  The
350 removed element is returned so you can free the line, data,
351 and containing structure.
352
353 .Fn3 "HIST_ENTRY *" replace_history_entry "int which" "const char *line" "histdata_t data"
354 Make the history entry at offset \fIwhich\fP have \fIline\fP and \fIdata\fP.
355 This returns the old entry so you can dispose of the data.  In the case
356 of an invalid \fIwhich\fP, a \fBNULL\fP pointer is returned.
357
358 .Fn1 void clear_history "void"
359 Clear the history list by deleting all the entries.
360
361 .Fn1 void stifle_history "int max"
362 Stifle the history list, remembering only the last \fImax\fP entries.
363
364 .Fn1 int unstifle_history "void"
365 Stop stifling the history.  This returns the previously-set
366 maximum number of history entries (as set by \fBstifle_history()\fP).
367 history was stifled.  The value is positive if the history was
368 stifled, negative if it wasn't.
369
370 .Fn1 int history_is_stifled "void"
371 Returns non-zero if the history is stifled, zero if it is not.
372
373 .SS Information About the History List
374
375 These functions return information about the entire history list or
376 individual list entries.
377
378 .Fn1 "HIST_ENTRY **" history_list "void"
379 Return a \fBNULL\fP terminated array of \fIHIST_ENTRY *\fP which is the
380 current input history.  Element 0 of this list is the beginning of time.
381 If there is no history, return \fBNULL\fP.
382
383 .Fn1 int where_history "void"
384 Returns the offset of the current history element.
385
386 .Fn1 "HIST_ENTRY *" current_history "void"
387 Return the history entry at the current position, as determined by
388 \fBwhere_history()\fP.  If there is no entry there, return a \fBNULL\fP
389 pointer.
390
391 .Fn1 "HIST_ENTRY *" history_get "int offset"
392 Return the history entry at position \fIoffset\fP, starting from
393 \fBhistory_base\fP.
394 If there is no entry there, or if \fIoffset\fP
395 is greater than the history length, return a \fBNULL\fP pointer.
396
397 .Fn1 int history_total_bytes "void"
398 Return the number of bytes that the primary history entries are using.
399 This function returns the sum of the lengths of all the lines in the
400 history.
401
402 .SS Moving Around the History List
403
404 These functions allow the current index into the history list to be
405 set or changed.
406
407 .Fn1 int history_set_pos "int pos"
408 Set the current history offset to \fIpos\fP, an absolute index
409 into the list.
410 Returns 1 on success, 0 if \fIpos\fP is less than zero or greater
411 than the number of history entries.
412
413 .Fn1 "HIST_ENTRY *" previous_history "void"
414 Back up the current history offset to the previous history entry, and
415 return a pointer to that entry.  If there is no previous entry, return
416 a \fBNULL\fP pointer.
417
418 .Fn1 "HIST_ENTRY *" next_history "void"
419 Move the current history offset forward to the next history entry, and
420 return the a pointer to that entry.  If there is no next entry, return
421 a \fBNULL\fP pointer.
422
423 .SS Searching the History List
424
425 These functions allow searching of the history list for entries containing
426 a specific string.  Searching may be performed both forward and backward
427 from the current history position.  The search may be \fIanchored\fP,
428 meaning that the string must match at the beginning of the history entry.
429
430 .Fn2 int history_search "const char *string" "int direction"
431 Search the history for \fIstring\fP, starting at the current history offset.
432 If \fIdirection\fP is less than 0, then the search is through
433 previous entries, otherwise through subsequent entries.
434 If \fIstring\fP is found, then
435 the current history index is set to that history entry, and the value
436 returned is the offset in the line of the entry where
437 \fIstring\fP was found.  Otherwise, nothing is changed, and a -1 is
438 returned.
439
440 .Fn2 int history_search_prefix "const char *string" "int direction"
441 Search the history for \fIstring\fP, starting at the current history
442 offset.  The search is anchored: matching lines must begin with
443 \fIstring\fP.  If \fIdirection\fP is less than 0, then the search is
444 through previous entries, otherwise through subsequent entries.
445 If \fIstring\fP is found, then the
446 current history index is set to that entry, and the return value is 0. 
447 Otherwise, nothing is changed, and a -1 is returned. 
448
449 .Fn3 int history_search_pos "const char *string" "int direction" "int pos"
450 Search for \fIstring\fP in the history list, starting at \fIpos\fP, an
451 absolute index into the list.  If \fIdirection\fP is negative, the search
452 proceeds backward from \fIpos\fP, otherwise forward.  Returns the absolute
453 index of the history element where \fIstring\fP was found, or -1 otherwise.
454
455 .SS Managing the History File
456 The History library can read the history from and write it to a file.
457 This section documents the functions for managing a history file.
458
459 .Fn1 int read_history "const char *filename"
460 Add the contents of \fIfilename\fP to the history list, a line at a time.
461 If \fIfilename\fP is \fBNULL\fP, then read from \fI~/.history\fP.
462 Returns 0 if successful, or \fBerrno\fP if not.
463
464 .Fn3 int read_history_range "const char *filename" "int from" "int to"
465 Read a range of lines from \fIfilename\fP, adding them to the history list.
466 Start reading at line \fIfrom\fP and end at \fIto\fP.
467 If \fIfrom\fP is zero, start at the beginning.  If \fIto\fP is less than
468 \fIfrom\fP, then read until the end of the file.  If \fIfilename\fP is
469 \fBNULL\fP, then read from \fI~/.history\fP.  Returns 0 if successful,
470 or \fBerrno\fP if not.
471
472 .Fn1 int write_history "const char *filename"
473 Write the current history to \fIfilename\fP, overwriting \fIfilename\fP
474 if necessary.
475 If \fIfilename\fP is \fBNULL\fP, then write the history list to \fI~/.history\fP.
476 Returns 0 on success, or \fBerrno\fP on a read or write error.
477
478
479 .Fn2 int append_history "int nelements" "const char *filename"
480 Append the last \fInelements\fP of the history list to \fIfilename\fP.
481 If \fIfilename\fP is \fBNULL\fP, then append to \fI~/.history\fP.
482 Returns 0 on success, or \fBerrno\fP on a read or write error.
483
484 .Fn2 int history_truncate_file "const char *filename" "int nlines"
485 Truncate the history file \fIfilename\fP, leaving only the last
486 \fInlines\fP lines.
487 If \fIfilename\fP is \fBNULL\fP, then \fI~/.history\fP is truncated.
488 Returns 0 on success, or \fBerrno\fP on failure.
489
490 .SS History Expansion
491
492 These functions implement history expansion.
493
494 .Fn2 int history_expand "char *string" "char **output"
495 Expand \fIstring\fP, placing the result into \fIoutput\fP, a pointer
496 to a string.  Returns:
497 .RS
498 .PD 0
499 .TP
500 0
501 If no expansions took place (or, if the only change in
502 the text was the removal of escape characters preceding the history expansion
503 character);
504 .TP
505 1
506 if expansions did take place;
507 .TP
508 -1
509 if there was an error in expansion;
510 .TP
511 2
512 if the returned line should be displayed, but not executed,
513 as with the \fB:p\fP modifier.
514 .PD
515 .RE
516 If an error ocurred in expansion, then \fIoutput\fP contains a descriptive
517 error message.
518
519 .Fn3 "char *" get_history_event "const char *string" "int *cindex" "int qchar"
520 Returns the text of the history event beginning at \fIstring\fP +
521 \fI*cindex\fP.  \fI*cindex\fP is modified to point to after the event
522 specifier.  At function entry, \fIcindex\fP points to the index into
523 \fIstring\fP where the history event specification begins.  \fIqchar\fP
524 is a character that is allowed to end the event specification in addition
525 to the ``normal'' terminating characters.
526
527 .Fn1 "char **" history_tokenize "const char *string"
528 Return an array of tokens parsed out of \fIstring\fP, much as the
529 shell might.
530 The tokens are split on the characters in the
531 \fBhistory_word_delimiters\fP variable,
532 and shell quoting conventions are obeyed.
533
534 .Fn3 "char *" history_arg_extract "int first" "int last" "const char *string"
535 Extract a string segment consisting of the \fIfirst\fP through \fIlast\fP
536 arguments present in \fIstring\fP.  Arguments are split using
537 \fBhistory_tokenize()\fP.
538
539 .SS History Variables
540
541 This section describes the externally-visible variables exported by
542 the GNU History Library.
543
544 .Vb int history_base
545 The logical offset of the first entry in the history list.
546
547 .Vb int history_length
548 The number of entries currently stored in the history list.
549
550 .Vb int history_max_entries
551 The maximum number of history entries.  This must be changed using
552 \fBstifle_history()\fP.
553
554 .Vb char history_expansion_char
555 The character that introduces a history event.  The default is \fB!\fP.
556 Setting this to 0 inhibits history expansion.
557
558 .Vb char history_subst_char
559 The character that invokes word substitution if found at the start of
560 a line.  The default is \fB^\fP.
561
562 .Vb char history_comment_char
563 During tokenization, if this character is seen as the first character
564 of a word, then it and all subsequent characters up to a newline are
565 ignored, suppressing history expansion for the remainder of the line.
566 This is disabled by default.
567
568 .Vb "char *" history_word_delimiters
569 The characters that separate tokens for \fBhistory_tokenize()\fP.
570 The default value is \fB"\ \et\en()<>;&|"\fP.
571
572 .Vb "char *" history_no_expand_chars
573 The list of characters which inhibit history expansion if found immediately
574 following \fBhistory_expansion_char\fP.  The default is space, tab, newline,
575 \fB\er\fP, and \fB=\fP.
576
577 .Vb "char *" history_search_delimiter_chars
578 The list of additional characters which can delimit a history search
579 string, in addition to space, tab, \fI:\fP and \fI?\fP in the case of
580 a substring search.  The default is empty.
581
582 .Vb int history_quotes_inhibit_expansion
583 If non-zero, single-quoted words are not scanned for the history expansion
584 character.  The default value is 0.
585
586 .Vb "rl_linebuf_func_t *" history_inhibit_expansion_function
587 This should be set to the address of a function that takes two arguments:
588 a \fBchar *\fP (\fIstring\fP)
589 and an \fBint\fP index into that string (\fIi\fP).
590 It should return a non-zero value if the history expansion starting at
591 \fIstring[i]\fP should not be performed; zero if the expansion should
592 be done.
593 It is intended for use by applications like \fBbash\fP that use the history
594 expansion character for additional purposes.
595 By default, this variable is set to \fBNULL\fP.
596 .SH FILES
597 .PD 0 
598 .TP
599 .FN ~/.history
600 Default filename for reading and writing saved history
601 .PD
602 .SH "SEE ALSO"
603 .PD 0
604 .TP
605 \fIThe Gnu Readline Library\fP, Brian Fox and Chet Ramey
606 .TP
607 \fIThe Gnu History Library\fP, Brian Fox and Chet Ramey
608 .TP
609 \fIbash\fP(1)
610 .TP
611 \fIreadline\fP(3)
612 .PD
613 .SH AUTHORS
614 Brian Fox, Free Software Foundation
615 .br
616 bfox@gnu.org
617 .PP
618 Chet Ramey, Case Western Reserve University
619 .br
620 chet@ins.CWRU.Edu
621 .SH BUG REPORTS
622 If you find a bug in the
623 .B history
624 library, you should report it.  But first, you should
625 make sure that it really is a bug, and that it appears in the latest
626 version of the
627 .B history
628 library that you have.
629 .PP
630 Once you have determined that a bug actually exists, mail a
631 bug report to \fIbug\-readline\fP@\fIgnu.org\fP.
632 If you have a fix, you are welcome to mail that
633 as well!  Suggestions and `philosophical' bug reports may be mailed
634 to \fPbug-readline\fP@\fIgnu.org\fP or posted to the Usenet
635 newsgroup
636 .BR gnu.bash.bug .
637 .PP
638 Comments and bug reports concerning
639 this manual page should be directed to
640 .IR chet@ins.CWRU.Edu .