1 This is Info file history.info, produced by Makeinfo version 1.67 from
2 the input file /usr/homes/chet/src/bash/readline-2.2/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, 1991, 1993, 1995, 1996 Free Software Foundation,
16 Permission is granted to make and distribute verbatim copies of this
17 manual provided the copyright notice and this permission notice pare
18 preserved on all copies.
20 Permission is granted to copy and distribute modified versions of
21 this manual under the conditions for verbatim copying, provided that
22 the entire resulting derived work is distributed under the terms of a
23 permission notice identical to this one.
25 Permission is granted to copy and distribute translations of this
26 manual into another language, under the above conditions for modified
27 versions, except that this permission notice may be stated in a
28 translation approved by the Foundation.
31 File: history.info, Node: Top, Next: Using History Interactively, Up: (dir)
36 This document describes the GNU History library, a programming tool
37 that provides a consistent user interface for recalling lines of
38 previously typed input.
42 * Using History Interactively:: GNU History User's Manual.
43 * Programming with GNU History:: GNU History Programmer's Manual.
44 * Concept Index:: Index of concepts described in this manual.
45 * Function and Variable Index:: Index of externally visible functions
49 File: history.info, Node: Using History Interactively, Next: Programming with GNU History, Prev: Top, Up: Top
51 Using History Interactively
52 ***************************
54 This chapter describes how to use the GNU History Library
55 interactively, from a user's standpoint. It should be considered a
56 user's guide. For information on using the GNU History Library in your
57 own programs, *note Programming with GNU History::..
61 * History Interaction:: What it feels like using History as a user.
64 File: history.info, Node: History Interaction, Up: Using History Interactively
69 The History library provides a history expansion feature that is
70 similar to the history expansion provided by `csh'. This section
71 describes the syntax used to manipulate the history information.
73 History expansions introduce words from the history list into the
74 input stream, making it easy to repeat commands, insert the arguments
75 to a previous command into the current input line, or fix errors in
76 previous commands quickly.
78 History expansion takes place in two parts. The first is to
79 determine which line from the history list should be used during
80 substitution. The second is to select portions of that line for
81 inclusion into the current one. The line selected from the history is
82 called the "event", and the portions of that line that are acted upon
83 are called "words". Various "modifiers" are available to manipulate
84 the selected words. The line is broken into words in the same fashion
85 that Bash does, so that several words surrounded by quotes are
86 considered one word. History expansions are introduced by the
87 appearance of the history expansion character, which is `!' by default.
91 * Event Designators:: How to specify which history line to use.
92 * Word Designators:: Specifying which words are of interest.
93 * Modifiers:: Modifying the results of substitution.
96 File: history.info, Node: Event Designators, Next: Word Designators, Up: History Interaction
101 An event designator is a reference to a command line entry in the
105 Start a history substitution, except when followed by a space, tab,
106 the end of the line, `=' or `('.
109 Refer to command line N.
112 Refer to the command N lines back.
115 Refer to the previous command. This is a synonym for `!-1'.
118 Refer to the most recent command starting with STRING.
121 Refer to the most recent command containing STRING. The trailing
122 `?' may be omitted if the STRING is followed immediately by a
126 Quick Substitution. Repeat the last command, replacing STRING1
127 with STRING2. Equivalent to `!!:s/STRING1/STRING2/'.
130 The entire command line typed so far.
133 File: history.info, Node: Word Designators, Next: Modifiers, Prev: Event Designators, Up: History Interaction
138 Word designators are used to select desired words from the event. A
139 `:' separates the event specification from the word designator. It may
140 be omitted if the word designator begins with a `^', `$', `*', `-', or
141 `%'. Words are numbered from the beginning of the line, with the first
142 word being denoted by 0 (zero). Words are inserted into the current
143 line separated by single spaces.
146 The `0'th word. For many applications, this is the command word.
152 The first argument; that is, word 1.
158 The word matched by the most recent `?STRING?' search.
161 A range of words; `-Y' abbreviates `0-Y'.
164 All of the words, except the `0'th. This is a synonym for `1-$'.
165 It is not an error to use `*' if there is just one word in the
166 event; the empty string is returned in that case.
172 Abbreviates `X-$' like `X*', but omits the last word.
174 If a word designator is supplied without an event specification, the
175 previous command is used as the event.
178 File: history.info, Node: Modifiers, Prev: Word Designators, Up: History Interaction
183 After the optional word designator, you can add a sequence of one or
184 more of the following modifiers, each preceded by a `:'.
187 Remove a trailing pathname component, leaving only the head.
190 Remove all leading pathname components, leaving the tail.
193 Remove a trailing suffix of the form `.SUFFIX', leaving the
197 Remove all but the trailing suffix.
200 Print the new command but do not execute it.
203 Substitute NEW for the first occurrence of OLD in the event line.
204 Any delimiter may be used in place of `/'. The delimiter may be
205 quoted in OLD and NEW with a single backslash. If `&' appears in
206 NEW, it is replaced by OLD. A single backslash will quote the
207 `&'. The final delimiter is optional if it is the last character
211 Repeat the previous substitution.
214 Cause changes to be applied over the entire event line. Used in
215 conjunction with `s', as in `gs/OLD/NEW/', or with `&'.
218 File: history.info, Node: Programming with GNU History, Next: Concept Index, Prev: Using History Interactively, Up: Top
220 Programming with GNU History
221 ****************************
223 This chapter describes how to interface programs that you write with
224 the GNU History Library. It should be considered a technical guide.
225 For information on the interactive use of GNU History, *note Using
226 History Interactively::..
230 * Introduction to History:: What is the GNU History library for?
231 * History Storage:: How information is stored.
232 * History Functions:: Functions that you can use.
233 * History Variables:: Variables that control behaviour.
234 * History Programming Example:: Example of using the GNU History Library.
237 File: history.info, Node: Introduction to History, Next: History Storage, Up: Programming with GNU History
239 Introduction to History
240 =======================
242 Many programs read input from the user a line at a time. The GNU
243 History library is able to keep track of those lines, associate
244 arbitrary data with each line, and utilize information from previous
245 lines in composing new ones.
247 The programmer using the History library has available functions for
248 remembering lines on a history list, associating arbitrary data with a
249 line, removing lines from the list, searching through the list for a
250 line containing an arbitrary text string, and referencing any line in
251 the list directly. In addition, a history "expansion" function is
252 available which provides for a consistent user interface across
255 The user using programs written with the History library has the
256 benefit of a consistent user interface with a set of well-known
257 commands for manipulating the text of previous lines and using that text
258 in new commands. The basic history manipulation commands are similar to
259 the history substitution provided by `csh'.
261 If the programmer desires, he can use the Readline library, which
262 includes some history manipulation by default, and has the added
263 advantage of command line editing.
266 File: history.info, Node: History Storage, Next: History Functions, Prev: Introduction to History, Up: Programming with GNU History
271 The history list is an array of history entries. A history entry is
274 typedef struct _hist_entry {
279 The history list itself might therefore be declared as
281 HIST_ENTRY **the_history_list;
283 The state of the History library is encapsulated into a single
286 /* A structure used to pass the current state of the history stuff around. */
287 typedef struct _hist_state {
288 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
289 int offset; /* The location pointer within this array. */
290 int length; /* Number of elements within this array. */
291 int size; /* Number of slots allocated to this array. */
295 If the flags member includes `HS_STIFLED', the history has been
299 File: history.info, Node: History Functions, Next: History Variables, Prev: History Storage, Up: Programming with GNU History
304 This section describes the calling sequence for the various functions
305 present in GNU History.
309 * Initializing History and State Management:: Functions to call when you
310 want to use history in a
312 * History List Management:: Functions used to manage the list
314 * Information About the History List:: Functions returning information about
316 * Moving Around the History List:: Functions used to change the position
318 * Searching the History List:: Functions to search the history list
319 for entries containing a string.
320 * Managing the History File:: Functions that read and write a file
321 containing the history list.
322 * History Expansion:: Functions to perform csh-like history
326 File: history.info, Node: Initializing History and State Management, Next: History List Management, Up: History Functions
328 Initializing History and State Management
329 -----------------------------------------
331 This section describes functions used to initialize and manage the
332 state of the History library when you want to use the history functions
335 - Function: void using_history ()
336 Begin a session in which the history functions might be used. This
337 initializes the interactive variables.
339 - Function: HISTORY_STATE * history_get_history_state ()
340 Return a structure describing the current state of the input
343 - Function: void history_set_history_state (HISTORY_STATE *state)
344 Set the state of the history list according to STATE.
347 File: history.info, Node: History List Management, Next: Information About the History List, Prev: Initializing History and State Management, Up: History Functions
349 History List Management
350 -----------------------
352 These functions manage individual entries on the history list, or set
353 parameters managing the list itself.
355 - Function: void add_history (char *string)
356 Place STRING at the end of the history list. The associated data
357 field (if any) is set to `NULL'.
359 - Function: HIST_ENTRY * remove_history (int which)
360 Remove history entry at offset WHICH from the history. The
361 removed element is returned so you can free the line, data, and
362 containing structure.
364 - Function: HIST_ENTRY * replace_history_entry (int which, char *line,
366 Make the history entry at offset WHICH have LINE and DATA. This
367 returns the old entry so you can dispose of the data. In the case
368 of an invalid WHICH, a `NULL' pointer is returned.
370 - Function: void clear_history ()
371 Clear the history list by deleting all the entries.
373 - Function: void stifle_history (int max)
374 Stifle the history list, remembering only the last MAX entries.
376 - Function: int unstifle_history ()
377 Stop stifling the history. This returns the previous amount the
378 history was stifled. The value is positive if the history was
379 stifled, negative if it wasn't.
381 - Function: int history_is_stifled ()
382 Returns non-zero if the history is stifled, zero if it is not.
385 File: history.info, Node: Information About the History List, Next: Moving Around the History List, Prev: History List Management, Up: History Functions
387 Information About the History List
388 ----------------------------------
390 These functions return information about the entire history list or
391 individual list entries.
393 - Function: HIST_ENTRY ** history_list ()
394 Return a `NULL' terminated array of `HIST_ENTRY' which is the
395 current input history. Element 0 of this list is the beginning of
396 time. If there is no history, return `NULL'.
398 - Function: int where_history ()
399 Returns the offset of the current history element.
401 - Function: HIST_ENTRY * current_history ()
402 Return the history entry at the current position, as determined by
403 `where_history ()'. If there is no entry there, return a `NULL'
406 - Function: HIST_ENTRY * history_get (int offset)
407 Return the history entry at position OFFSET, starting from
408 `history_base'. If there is no entry there, or if OFFSET is
409 greater than the history length, return a `NULL' pointer.
411 - Function: int history_total_bytes ()
412 Return the number of bytes that the primary history entries are
413 using. This function returns the sum of the lengths of all the
414 lines in the history.
417 File: history.info, Node: Moving Around the History List, Next: Searching the History List, Prev: Information About the History List, Up: History Functions
419 Moving Around the History List
420 ------------------------------
422 These functions allow the current index into the history list to be
425 - Function: int history_set_pos (int pos)
426 Set the position in the history list to POS, an absolute index
429 - Function: HIST_ENTRY * previous_history ()
430 Back up the current history offset to the previous history entry,
431 and return a pointer to that entry. If there is no previous
432 entry, return a `NULL' pointer.
434 - Function: HIST_ENTRY * next_history ()
435 Move the current history offset forward to the next history entry,
436 and return the a pointer to that entry. If there is no next
437 entry, return a `NULL' pointer.
440 File: history.info, Node: Searching the History List, Next: Managing the History File, Prev: Moving Around the History List, Up: History Functions
442 Searching the History List
443 --------------------------
445 These functions allow searching of the history list for entries
446 containing a specific string. Searching may be performed both forward
447 and backward from the current history position. The search may be
448 "anchored", meaning that the string must match at the beginning of the
451 - Function: int history_search (char *string, int direction)
452 Search the history for STRING, starting at the current history
453 offset. If DIRECTION < 0, then the search is through previous
454 entries, else through subsequent. If STRING is found, then the
455 current history index is set to that history entry, and the value
456 returned is the offset in the line of the entry where STRING was
457 found. Otherwise, nothing is changed, and a -1 is returned.
459 - Function: int history_search_prefix (char *string, int direction)
460 Search the history for STRING, starting at the current history
461 offset. The search is anchored: matching lines must begin with
462 STRING. If DIRECTION < 0, then the search is through previous
463 entries, else through subsequent. If STRING is found, then the
464 current history index is set to that entry, and the return value
465 is 0. Otherwise, nothing is changed, and a -1 is returned.
467 - Function: int history_search_pos (char *string, int direction, int
469 Search for STRING in the history list, starting at POS, an
470 absolute index into the list. If DIRECTION is negative, the search
471 proceeds backward from POS, otherwise forward. Returns the
472 absolute index of the history element where STRING was found, or
476 File: history.info, Node: Managing the History File, Next: History Expansion, Prev: Searching the History List, Up: History Functions
478 Managing the History File
479 -------------------------
481 The History library can read the history from and write it to a file.
482 This section documents the functions for managing a history file.
484 - Function: int read_history (char *filename)
485 Add the contents of FILENAME to the history list, a line at a
486 time. If FILENAME is `NULL', then read from `~/.history'.
487 Returns 0 if successful, or errno if not.
489 - Function: int read_history_range (char *filename, int from, int to)
490 Read a range of lines from FILENAME, adding them to the history
491 list. Start reading at line FROM and end at TO. If FROM is zero,
492 start at the beginning. If TO is less than FROM, then read until
493 the end of the file. If FILENAME is `NULL', then read from
494 `~/.history'. Returns 0 if successful, or `errno' if not.
496 - Function: int write_history (char *filename)
497 Write the current history to FILENAME, overwriting FILENAME if
498 necessary. If FILENAME is `NULL', then write the history list to
499 `~/.history'. Values returned are as in `read_history ()'.
501 - Function: int append_history (int nelements, char *filename)
502 Append the last NELEMENTS of the history list to FILENAME.
504 - Function: int history_truncate_file (char *filename, int nlines)
505 Truncate the history file FILENAME, leaving only the last NLINES
509 File: history.info, Node: History Expansion, Prev: Managing the History File, Up: History Functions
514 These functions implement `csh'-like history expansion.
516 - Function: int history_expand (char *string, char **output)
517 Expand STRING, placing the result into OUTPUT, a pointer to a
518 string (*note History Interaction::.). Returns:
520 If no expansions took place (or, if the only change in the
521 text was the de-slashifying of the history expansion
525 if expansions did take place;
528 if there was an error in expansion;
531 if the returned line should only be displayed, but not
532 executed, as with the `:p' modifier (*note Modifiers::.).
534 If an error ocurred in expansion, then OUTPUT contains a
535 descriptive error message.
537 - Function: char * history_arg_extract (int first, int last, char
539 Extract a string segment consisting of the FIRST through LAST
540 arguments present in STRING. Arguments are broken up as in Bash.
542 - Function: char * get_history_event (char *string, int *cindex, int
544 Returns the text of the history event beginning at STRING +
545 *CINDEX. *CINDEX is modified to point to after the event
546 specifier. At function entry, CINDEX points to the index into
547 STRING where the history event specification begins. QCHAR is a
548 character that is allowed to end the event specification in
549 addition to the "normal" terminating characters.
551 - Function: char ** history_tokenize (char *string)
552 Return an array of tokens parsed out of STRING, much as the shell
553 might. The tokens are split on white space and on the characters
554 `()<>;&|$', and shell quoting conventions are obeyed.
557 File: history.info, Node: History Variables, Next: History Programming Example, Prev: History Functions, Up: Programming with GNU History
562 This section describes the externally visible variables exported by
563 the GNU History Library.
565 - Variable: int history_base
566 The logical offset of the first entry in the history list.
568 - Variable: int history_length
569 The number of entries currently stored in the history list.
571 - Variable: int max_input_history
572 The maximum number of history entries. This must be changed using
575 - Variable: char history_expansion_char
576 The character that starts a history event. The default is `!'.
578 - Variable: char history_subst_char
579 The character that invokes word substitution if found at the start
580 of a line. The default is `^'.
582 - Variable: char history_comment_char
583 During tokenization, if this character is seen as the first
584 character of a word, then it and all subsequent characters up to a
585 newline are ignored, suppressing history expansion for the
586 remainder of the line. This is disabled by default.
588 - Variable: char * history_no_expand_chars
589 The list of characters which inhibit history expansion if found
590 immediately following HISTORY_EXPANSION_CHAR. The default is
593 - Variable: char * history_search_delimiter_chars
594 The list of additional characters which can delimit a history
595 search string, in addition to whitespace, `:' and `?' in the case
596 of a substring search. The default is empty.
598 - Variable: int history_quotes_inhibit_expansion
599 If non-zero, single-quoted words are not scanned for the history
600 expansion character. The default value is 0.
602 - Variable: Function * history_inhibit_expansion_function
603 This should be set to the address of a function that takes two
604 arguments: a `char *' (STRING) and an integer index into that
605 string (I). It should return a non-zero value if the history
606 expansion starting at STRING[I] should not be performed; zero if
607 the expansion should be done. It is intended for use by
608 applications like Bash that use the history expansion character
609 for additional purposes. By default, this variable is set to NULL.
612 File: history.info, Node: History Programming Example, Prev: History Variables, Up: Programming with GNU History
614 History Programming Example
615 ===========================
617 The following program demonstrates simple use of the GNU History
630 printf ("history$ ");
632 t = fgets (line, sizeof (line) - 1, stdin);
636 if (t[len - 1] == '\n')
641 strcpy (line, "quit");
648 result = history_expand (line, &expansion);
650 fprintf (stderr, "%s\n", expansion);
652 if (result < 0 || result == 2)
658 add_history (expansion);
659 strncpy (line, expansion, sizeof (line) - 1);
663 if (strcmp (line, "quit") == 0)
665 else if (strcmp (line, "save") == 0)
666 write_history ("history_file");
667 else if (strcmp (line, "read") == 0)
668 read_history ("history_file");
669 else if (strcmp (line, "list") == 0)
671 register HIST_ENTRY **the_list;
674 the_list = history_list ();
676 for (i = 0; the_list[i]; i++)
677 printf ("%d: %s\n", i + history_base, the_list[i]->line);
679 else if (strncmp (line, "delete", 6) == 0)
682 if ((sscanf (line + 6, "%d", &which)) == 1)
684 HIST_ENTRY *entry = remove_history (which);
686 fprintf (stderr, "No such entry %d\n", which);
695 fprintf (stderr, "non-numeric arg given to `delete'\n");
702 File: history.info, Node: Concept Index, Next: Function and Variable Index, Prev: Programming with GNU History, Up: Top
709 * anchored search: Searching the History List.
710 * event designators: Event Designators.
711 * history events: Event Designators.
712 * history expansion: History Interaction.
713 * History Searching: Searching the History List.
716 File: history.info, Node: Function and Variable Index, Prev: Concept Index, Up: Top
718 Function and Variable Index
719 ***************************
723 * add_history: History List Management.
724 * append_history: Managing the History File.
725 * clear_history: History List Management.
726 * current_history: Information About the History List.
727 * get_history_event: History Expansion.
728 * history_arg_extract: History Expansion.
729 * history_base: History Variables.
730 * history_comment_char: History Variables.
731 * history_expand: History Expansion.
732 * history_expansion_char: History Variables.
733 * history_get: Information About the History List.
734 * history_get_history_state: Initializing History and State Management.
735 * history_inhibit_expansion_function: History Variables.
736 * history_is_stifled: History List Management.
737 * history_length: History Variables.
738 * history_list: Information About the History List.
739 * history_no_expand_chars: History Variables.
740 * history_quotes_inhibit_expansion: History Variables.
741 * history_search: Searching the History List.
742 * history_search_delimiter_chars: History Variables.
743 * history_search_pos: Searching the History List.
744 * history_search_prefix: Searching the History List.
745 * history_set_history_state: Initializing History and State Management.
746 * history_set_pos: Moving Around the History List.
747 * history_subst_char: History Variables.
748 * history_tokenize: History Expansion.
749 * history_total_bytes: Information About the History List.
750 * history_truncate_file: Managing the History File.
751 * max_input_history: History Variables.
752 * next_history: Moving Around the History List.
753 * previous_history: Moving Around the History List.
754 * read_history: Managing the History File.
755 * read_history_range: Managing the History File.
756 * remove_history: History List Management.
757 * replace_history_entry: History List Management.
758 * stifle_history: History List Management.
759 * unstifle_history: History List Management.
760 * using_history: Initializing History and State Management.
761 * where_history: Information About the History List.
762 * write_history: Managing the History File.
768 Node: Using History Interactively
\7f1747
769 Node: History Interaction
\7f2255
770 Node: Event Designators
\7f3674
771 Node: Word Designators
\7f4601
772 Node: Modifiers
\7f5850
773 Node: Programming with GNU History
\7f6988
774 Node: Introduction to History
\7f7714
775 Node: History Storage
\7f9035
776 Node: History Functions
\7f10128
777 Node: Initializing History and State Management
\7f11099
778 Node: History List Management
\7f11891
779 Node: Information About the History List
\7f13412
780 Node: Moving Around the History List
\7f14718
781 Node: Searching the History List
\7f15603
782 Node: Managing the History File
\7f17435
783 Node: History Expansion
\7f18941
784 Node: History Variables
\7f20785
785 Node: History Programming Example
\7f23103
786 Node: Concept Index
\7f25707
787 Node: Function and Variable Index
\7f26193