1 This is Info file history.info, produced by Makeinfo version 1.67 from
2 the input file /usr/homes/chet/src/bash/readline-src/doc/hist.texinfo.
4 INFO-DIR-SECTION Libraries
6 * History: (history). The GNU history library API
9 This document describes the GNU History library, a programming tool
10 that provides a consistent user interface for recalling lines of
11 previously typed input.
13 Copyright (C) 1988-1999 Free Software Foundation, Inc.
15 Permission is granted to make and distribute verbatim copies of this
16 manual provided the copyright notice and this permission notice pare
17 preserved on all copies.
19 Permission is granted to copy and distribute modified versions of
20 this manual under the conditions for verbatim copying, provided that
21 the entire resulting derived work is distributed under the terms of a
22 permission notice identical to this one.
24 Permission is granted to copy and distribute translations of this
25 manual into another language, under the above conditions for modified
26 versions, except that this permission notice may be stated in a
27 translation approved by the Free Software Foundation.
30 File: history.info, Node: Top, Next: Using History Interactively, Up: (dir)
35 This document describes the GNU History library, a programming tool
36 that provides a consistent user interface for recalling lines of
37 previously typed input.
41 * Using History Interactively:: GNU History User's Manual.
42 * Programming with GNU History:: GNU History Programmer's Manual.
43 * Concept Index:: Index of concepts described in this manual.
44 * Function and Variable Index:: Index of externally visible functions
48 File: history.info, Node: Using History Interactively, Next: Programming with GNU History, Prev: Top, Up: Top
50 Using History Interactively
51 ***************************
53 This chapter describes how to use the GNU History Library
54 interactively, from a user's standpoint. It should be considered a
55 user's guide. For information on using the GNU History Library in your
56 own programs, *note Programming with GNU History::..
60 * History Interaction:: What it feels like using History as a user.
63 File: history.info, Node: History Interaction, Up: Using History Interactively
68 The History library provides a history expansion feature that is
69 similar to the history expansion provided by `csh'. This section
70 describes the syntax used to manipulate the history information.
72 History expansions introduce words from the history list into the
73 input stream, making it easy to repeat commands, insert the arguments
74 to a previous command into the current input line, or fix errors in
75 previous commands quickly.
77 History expansion takes place in two parts. The first is to
78 determine which line from the history list should be used during
79 substitution. The second is to select portions of that line for
80 inclusion into the current one. The line selected from the history is
81 called the "event", and the portions of that line that are acted upon
82 are called "words". Various "modifiers" are available to manipulate
83 the selected words. The line is broken into words in the same fashion
84 that Bash does, so that several words surrounded by quotes are
85 considered one word. History expansions are introduced by the
86 appearance of the history expansion character, which is `!' by default.
90 * Event Designators:: How to specify which history line to use.
91 * Word Designators:: Specifying which words are of interest.
92 * Modifiers:: Modifying the results of substitution.
95 File: history.info, Node: Event Designators, Next: Word Designators, Up: History Interaction
100 An event designator is a reference to a command line entry in the
104 Start a history substitution, except when followed by a space, tab,
105 the end of the line, `=' or `('.
108 Refer to command line N.
111 Refer to the command N lines back.
114 Refer to the previous command. This is a synonym for `!-1'.
117 Refer to the most recent command starting with STRING.
120 Refer to the most recent command containing STRING. The trailing
121 `?' may be omitted if the STRING is followed immediately by a
125 Quick Substitution. Repeat the last command, replacing STRING1
126 with STRING2. Equivalent to `!!:s/STRING1/STRING2/'.
129 The entire command line typed so far.
132 File: history.info, Node: Word Designators, Next: Modifiers, Prev: Event Designators, Up: History Interaction
137 Word designators are used to select desired words from the event. A
138 `:' separates the event specification from the word designator. It may
139 be omitted if the word designator begins with a `^', `$', `*', `-', or
140 `%'. Words are numbered from the beginning of the line, with the first
141 word being denoted by 0 (zero). Words are inserted into the current
142 line separated by single spaces.
145 The `0'th word. For many applications, this is the command word.
151 The first argument; that is, word 1.
157 The word matched by the most recent `?STRING?' search.
160 A range of words; `-Y' abbreviates `0-Y'.
163 All of the words, except the `0'th. This is a synonym for `1-$'.
164 It is not an error to use `*' if there is just one word in the
165 event; the empty string is returned in that case.
171 Abbreviates `X-$' like `X*', but omits the last word.
173 If a word designator is supplied without an event specification, the
174 previous command is used as the event.
177 File: history.info, Node: Modifiers, Prev: Word Designators, Up: History Interaction
182 After the optional word designator, you can add a sequence of one or
183 more of the following modifiers, each preceded by a `:'.
186 Remove a trailing pathname component, leaving only the head.
189 Remove all leading pathname components, leaving the tail.
192 Remove a trailing suffix of the form `.SUFFIX', leaving the
196 Remove all but the trailing suffix.
199 Print the new command but do not execute it.
202 Substitute NEW for the first occurrence of OLD in the event line.
203 Any delimiter may be used in place of `/'. The delimiter may be
204 quoted in OLD and NEW with a single backslash. If `&' appears in
205 NEW, it is replaced by OLD. A single backslash will quote the
206 `&'. The final delimiter is optional if it is the last character
210 Repeat the previous substitution.
213 Cause changes to be applied over the entire event line. Used in
214 conjunction with `s', as in `gs/OLD/NEW/', or with `&'.
217 File: history.info, Node: Programming with GNU History, Next: Concept Index, Prev: Using History Interactively, Up: Top
219 Programming with GNU History
220 ****************************
222 This chapter describes how to interface programs that you write with
223 the GNU History Library. It should be considered a technical guide.
224 For information on the interactive use of GNU History, *note Using
225 History Interactively::..
229 * Introduction to History:: What is the GNU History library for?
230 * History Storage:: How information is stored.
231 * History Functions:: Functions that you can use.
232 * History Variables:: Variables that control behaviour.
233 * History Programming Example:: Example of using the GNU History Library.
236 File: history.info, Node: Introduction to History, Next: History Storage, Up: Programming with GNU History
238 Introduction to History
239 =======================
241 Many programs read input from the user a line at a time. The GNU
242 History library is able to keep track of those lines, associate
243 arbitrary data with each line, and utilize information from previous
244 lines in composing new ones.
246 The programmer using the History library has available functions for
247 remembering lines on a history list, associating arbitrary data with a
248 line, removing lines from the list, searching through the list for a
249 line containing an arbitrary text string, and referencing any line in
250 the list directly. In addition, a history "expansion" function is
251 available which provides for a consistent user interface across
254 The user using programs written with the History library has the
255 benefit of a consistent user interface with a set of well-known
256 commands for manipulating the text of previous lines and using that text
257 in new commands. The basic history manipulation commands are similar to
258 the history substitution provided by `csh'.
260 If the programmer desires, he can use the Readline library, which
261 includes some history manipulation by default, and has the added
262 advantage of command line editing.
265 File: history.info, Node: History Storage, Next: History Functions, Prev: Introduction to History, Up: Programming with GNU History
270 The history list is an array of history entries. A history entry is
273 typedef struct _hist_entry {
278 The history list itself might therefore be declared as
280 HIST_ENTRY **the_history_list;
282 The state of the History library is encapsulated into a single
285 /* A structure used to pass the current state of the history stuff around. */
286 typedef struct _hist_state {
287 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
288 int offset; /* The location pointer within this array. */
289 int length; /* Number of elements within this array. */
290 int size; /* Number of slots allocated to this array. */
294 If the flags member includes `HS_STIFLED', the history has been
298 File: history.info, Node: History Functions, Next: History Variables, Prev: History Storage, Up: Programming with GNU History
303 This section describes the calling sequence for the various functions
304 present in GNU History.
308 * Initializing History and State Management:: Functions to call when you
309 want to use history in a
311 * History List Management:: Functions used to manage the list
313 * Information About the History List:: Functions returning information about
315 * Moving Around the History List:: Functions used to change the position
317 * Searching the History List:: Functions to search the history list
318 for entries containing a string.
319 * Managing the History File:: Functions that read and write a file
320 containing the history list.
321 * History Expansion:: Functions to perform csh-like history
325 File: history.info, Node: Initializing History and State Management, Next: History List Management, Up: History Functions
327 Initializing History and State Management
328 -----------------------------------------
330 This section describes functions used to initialize and manage the
331 state of the History library when you want to use the history functions
334 - Function: void using_history ()
335 Begin a session in which the history functions might be used. This
336 initializes the interactive variables.
338 - Function: HISTORY_STATE * history_get_history_state ()
339 Return a structure describing the current state of the input
342 - Function: void history_set_history_state (HISTORY_STATE *state)
343 Set the state of the history list according to STATE.
346 File: history.info, Node: History List Management, Next: Information About the History List, Prev: Initializing History and State Management, Up: History Functions
348 History List Management
349 -----------------------
351 These functions manage individual entries on the history list, or set
352 parameters managing the list itself.
354 - Function: void add_history (char *string)
355 Place STRING at the end of the history list. The associated data
356 field (if any) is set to `NULL'.
358 - Function: HIST_ENTRY * remove_history (int which)
359 Remove history entry at offset WHICH from the history. The
360 removed element is returned so you can free the line, data, and
361 containing structure.
363 - Function: HIST_ENTRY * replace_history_entry (int which, char *line,
365 Make the history entry at offset WHICH have LINE and DATA. This
366 returns the old entry so you can dispose of the data. In the case
367 of an invalid WHICH, a `NULL' pointer is returned.
369 - Function: void clear_history ()
370 Clear the history list by deleting all the entries.
372 - Function: void stifle_history (int max)
373 Stifle the history list, remembering only the last MAX entries.
375 - Function: int unstifle_history ()
376 Stop stifling the history. This returns the previous amount the
377 history was stifled. The value is positive if the history was
378 stifled, negative if it wasn't.
380 - Function: int history_is_stifled ()
381 Returns non-zero if the history is stifled, zero if it is not.
384 File: history.info, Node: Information About the History List, Next: Moving Around the History List, Prev: History List Management, Up: History Functions
386 Information About the History List
387 ----------------------------------
389 These functions return information about the entire history list or
390 individual list entries.
392 - Function: HIST_ENTRY ** history_list ()
393 Return a `NULL' terminated array of `HIST_ENTRY' which is the
394 current input history. Element 0 of this list is the beginning of
395 time. If there is no history, return `NULL'.
397 - Function: int where_history ()
398 Returns the offset of the current history element.
400 - Function: HIST_ENTRY * current_history ()
401 Return the history entry at the current position, as determined by
402 `where_history ()'. If there is no entry there, return a `NULL'
405 - Function: HIST_ENTRY * history_get (int offset)
406 Return the history entry at position OFFSET, starting from
407 `history_base'. If there is no entry there, or if OFFSET is
408 greater than the history length, return a `NULL' pointer.
410 - Function: int history_total_bytes ()
411 Return the number of bytes that the primary history entries are
412 using. This function returns the sum of the lengths of all the
413 lines in the history.
416 File: history.info, Node: Moving Around the History List, Next: Searching the History List, Prev: Information About the History List, Up: History Functions
418 Moving Around the History List
419 ------------------------------
421 These functions allow the current index into the history list to be
424 - Function: int history_set_pos (int pos)
425 Set the position in the history list to POS, an absolute index
428 - Function: HIST_ENTRY * previous_history ()
429 Back up the current history offset to the previous history entry,
430 and return a pointer to that entry. If there is no previous
431 entry, return a `NULL' pointer.
433 - Function: HIST_ENTRY * next_history ()
434 Move the current history offset forward to the next history entry,
435 and return the a pointer to that entry. If there is no next
436 entry, return a `NULL' pointer.
439 File: history.info, Node: Searching the History List, Next: Managing the History File, Prev: Moving Around the History List, Up: History Functions
441 Searching the History List
442 --------------------------
444 These functions allow searching of the history list for entries
445 containing a specific string. Searching may be performed both forward
446 and backward from the current history position. The search may be
447 "anchored", meaning that the string must match at the beginning of the
450 - Function: int history_search (char *string, int direction)
451 Search the history for STRING, starting at the current history
452 offset. If DIRECTION < 0, then the search is through previous
453 entries, else through subsequent. If STRING is found, then the
454 current history index is set to that history entry, and the value
455 returned is the offset in the line of the entry where STRING was
456 found. Otherwise, nothing is changed, and a -1 is returned.
458 - Function: int history_search_prefix (char *string, int direction)
459 Search the history for STRING, starting at the current history
460 offset. The search is anchored: matching lines must begin with
461 STRING. If DIRECTION < 0, then the search is through previous
462 entries, else through subsequent. If STRING is found, then the
463 current history index is set to that entry, and the return value
464 is 0. Otherwise, nothing is changed, and a -1 is returned.
466 - Function: int history_search_pos (char *string, int direction, int
468 Search for STRING in the history list, starting at POS, an
469 absolute index into the list. If DIRECTION is negative, the search
470 proceeds backward from POS, otherwise forward. Returns the
471 absolute index of the history element where STRING was found, or
475 File: history.info, Node: Managing the History File, Next: History Expansion, Prev: Searching the History List, Up: History Functions
477 Managing the History File
478 -------------------------
480 The History library can read the history from and write it to a file.
481 This section documents the functions for managing a history file.
483 - Function: int read_history (char *filename)
484 Add the contents of FILENAME to the history list, a line at a
485 time. If FILENAME is `NULL', then read from `~/.history'.
486 Returns 0 if successful, or errno if not.
488 - Function: int read_history_range (char *filename, int from, int to)
489 Read a range of lines from FILENAME, adding them to the history
490 list. Start reading at line FROM and end at TO. If FROM is zero,
491 start at the beginning. If TO is less than FROM, then read until
492 the end of the file. If FILENAME is `NULL', then read from
493 `~/.history'. Returns 0 if successful, or `errno' if not.
495 - Function: int write_history (char *filename)
496 Write the current history to FILENAME, overwriting FILENAME if
497 necessary. If FILENAME is `NULL', then write the history list to
498 `~/.history'. Values returned are as in `read_history ()'.
500 - Function: int append_history (int nelements, char *filename)
501 Append the last NELEMENTS of the history list to FILENAME.
503 - Function: int history_truncate_file (char *filename, int nlines)
504 Truncate the history file FILENAME, leaving only the last NLINES
508 File: history.info, Node: History Expansion, Prev: Managing the History File, Up: History Functions
513 These functions implement `csh'-like history expansion.
515 - Function: int history_expand (char *string, char **output)
516 Expand STRING, placing the result into OUTPUT, a pointer to a
517 string (*note History Interaction::.). Returns:
519 If no expansions took place (or, if the only change in the
520 text was the de-slashifying of the history expansion
524 if expansions did take place;
527 if there was an error in expansion;
530 if the returned line should only be displayed, but not
531 executed, as with the `:p' modifier (*note Modifiers::.).
533 If an error ocurred in expansion, then OUTPUT contains a
534 descriptive error message.
536 - Function: char * history_arg_extract (int first, int last, char
538 Extract a string segment consisting of the FIRST through LAST
539 arguments present in STRING. Arguments are broken up as in Bash.
541 - Function: char * get_history_event (char *string, int *cindex, int
543 Returns the text of the history event beginning at STRING +
544 *CINDEX. *CINDEX is modified to point to after the event
545 specifier. At function entry, CINDEX points to the index into
546 STRING where the history event specification begins. QCHAR is a
547 character that is allowed to end the event specification in
548 addition to the "normal" terminating characters.
550 - Function: char ** history_tokenize (char *string)
551 Return an array of tokens parsed out of STRING, much as the shell
552 might. The tokens are split on white space and on the characters
553 `()<>;&|$', and shell quoting conventions are obeyed.
556 File: history.info, Node: History Variables, Next: History Programming Example, Prev: History Functions, Up: Programming with GNU History
561 This section describes the externally visible variables exported by
562 the GNU History Library.
564 - Variable: int history_base
565 The logical offset of the first entry in the history list.
567 - Variable: int history_length
568 The number of entries currently stored in the history list.
570 - Variable: int max_input_history
571 The maximum number of history entries. This must be changed using
574 - Variable: char history_expansion_char
575 The character that starts a history event. The default is `!'.
577 - Variable: char history_subst_char
578 The character that invokes word substitution if found at the start
579 of a line. The default is `^'.
581 - Variable: char history_comment_char
582 During tokenization, if this character is seen as the first
583 character of a word, then it and all subsequent characters up to a
584 newline are ignored, suppressing history expansion for the
585 remainder of the line. This is disabled by default.
587 - Variable: char * history_no_expand_chars
588 The list of characters which inhibit history expansion if found
589 immediately following HISTORY_EXPANSION_CHAR. The default is
592 - Variable: char * history_search_delimiter_chars
593 The list of additional characters which can delimit a history
594 search string, in addition to whitespace, `:' and `?' in the case
595 of a substring search. The default is empty.
597 - Variable: int history_quotes_inhibit_expansion
598 If non-zero, single-quoted words are not scanned for the history
599 expansion character. The default value is 0.
601 - Variable: Function * history_inhibit_expansion_function
602 This should be set to the address of a function that takes two
603 arguments: a `char *' (STRING) and an integer index into that
604 string (I). It should return a non-zero value if the history
605 expansion starting at STRING[I] should not be performed; zero if
606 the expansion should be done. It is intended for use by
607 applications like Bash that use the history expansion character
608 for additional purposes. By default, this variable is set to NULL.
611 File: history.info, Node: History Programming Example, Prev: History Variables, Up: Programming with GNU History
613 History Programming Example
614 ===========================
616 The following program demonstrates simple use of the GNU History
629 printf ("history$ ");
631 t = fgets (line, sizeof (line) - 1, stdin);
635 if (t[len - 1] == '\n')
640 strcpy (line, "quit");
647 result = history_expand (line, &expansion);
649 fprintf (stderr, "%s\n", expansion);
651 if (result < 0 || result == 2)
657 add_history (expansion);
658 strncpy (line, expansion, sizeof (line) - 1);
662 if (strcmp (line, "quit") == 0)
664 else if (strcmp (line, "save") == 0)
665 write_history ("history_file");
666 else if (strcmp (line, "read") == 0)
667 read_history ("history_file");
668 else if (strcmp (line, "list") == 0)
670 register HIST_ENTRY **the_list;
673 the_list = history_list ();
675 for (i = 0; the_list[i]; i++)
676 printf ("%d: %s\n", i + history_base, the_list[i]->line);
678 else if (strncmp (line, "delete", 6) == 0)
681 if ((sscanf (line + 6, "%d", &which)) == 1)
683 HIST_ENTRY *entry = remove_history (which);
685 fprintf (stderr, "No such entry %d\n", which);
694 fprintf (stderr, "non-numeric arg given to `delete'\n");
701 File: history.info, Node: Concept Index, Next: Function and Variable Index, Prev: Programming with GNU History, Up: Top
708 * anchored search: Searching the History List.
709 * event designators: Event Designators.
710 * history events: Event Designators.
711 * history expansion: History Interaction.
712 * History Searching: Searching the History List.
715 File: history.info, Node: Function and Variable Index, Prev: Concept Index, Up: Top
717 Function and Variable Index
718 ***************************
722 * add_history: History List Management.
723 * append_history: Managing the History File.
724 * clear_history: History List Management.
725 * current_history: Information About the History List.
726 * get_history_event: History Expansion.
727 * history_arg_extract: History Expansion.
728 * history_base: History Variables.
729 * history_comment_char: History Variables.
730 * history_expand: History Expansion.
731 * history_expansion_char: History Variables.
732 * history_get: Information About the History List.
733 * history_get_history_state: Initializing History and State Management.
734 * history_inhibit_expansion_function: History Variables.
735 * history_is_stifled: History List Management.
736 * history_length: History Variables.
737 * history_list: Information About the History List.
738 * history_no_expand_chars: History Variables.
739 * history_quotes_inhibit_expansion: History Variables.
740 * history_search: Searching the History List.
741 * history_search_delimiter_chars: History Variables.
742 * history_search_pos: Searching the History List.
743 * history_search_prefix: Searching the History List.
744 * history_set_history_state: Initializing History and State Management.
745 * history_set_pos: Moving Around the History List.
746 * history_subst_char: History Variables.
747 * history_tokenize: History Expansion.
748 * history_total_bytes: Information About the History List.
749 * history_truncate_file: Managing the History File.
750 * max_input_history: History Variables.
751 * next_history: Moving Around the History List.
752 * previous_history: Moving Around the History List.
753 * read_history: Managing the History File.
754 * read_history_range: Managing the History File.
755 * remove_history: History List Management.
756 * replace_history_entry: History List Management.
757 * stifle_history: History List Management.
758 * unstifle_history: History List Management.
759 * using_history: Initializing History and State Management.
760 * where_history: Information About the History List.
761 * write_history: Managing the History File.
767 Node: Using History Interactively
\7f1742
768 Node: History Interaction
\7f2250
769 Node: Event Designators
\7f3669
770 Node: Word Designators
\7f4596
771 Node: Modifiers
\7f5845
772 Node: Programming with GNU History
\7f6983
773 Node: Introduction to History
\7f7709
774 Node: History Storage
\7f9030
775 Node: History Functions
\7f10123
776 Node: Initializing History and State Management
\7f11094
777 Node: History List Management
\7f11886
778 Node: Information About the History List
\7f13407
779 Node: Moving Around the History List
\7f14713
780 Node: Searching the History List
\7f15598
781 Node: Managing the History File
\7f17430
782 Node: History Expansion
\7f18936
783 Node: History Variables
\7f20780
784 Node: History Programming Example
\7f23098
785 Node: Concept Index
\7f25702
786 Node: Function and Variable Index
\7f26188