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