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