From: Elena Zannoni
+
+
+
+@dircategory Libraries
+@direntry
+* History: (history). The GNU history library API
+
+
+This document describes the GNU History library, a programming tool that
+provides a consistent user interface for recalling lines of previously
+typed input.
+
+
+Published by the Free Software Foundation
+Permission is granted to make and distribute verbatim copies of
+this manual provided the copyright notice and this permission notice
+are preserved on all copies.
+
+
+Permission is granted to copy and distribute modified versions of this
+manual under the conditions for verbatim copying, provided that the entire
+resulting derived work is distributed under the terms of a permission
+notice identical to this one.
+
+
+Permission is granted to copy and distribute translations of this manual
+into another language, under the above conditions for modified versions,
+except that this permission notice may be stated in a translation approved
+by the Foundation.
+
+
+Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+
+
+This chapter describes how to use the GNU History Library interactively,
+from a user's standpoint. It should be considered a user's guide. For
+information on using the GNU History Library in your own programs,
+see section Programming with GNU History.
+
+
+The History library provides a history expansion feature that is similar
+to the history expansion provided by
+History expansions introduce words from the history list into
+the input stream, making it easy to repeat commands, insert the
+arguments to a previous command into the current input line, or
+fix errors in previous commands quickly.
+
+
+History expansion takes place in two parts. The first is to determine
+which line from the history list should be used during substitution.
+The second is to select portions of that line for inclusion into the
+current one. The line selected from the history is called the
+event, and the portions of that line that are acted upon are
+called words. Various modifiers are available to manipulate
+the selected words. The line is broken into words in the same fashion
+that Bash does, so that several words
+surrounded by quotes are considered one word.
+History expansions are introduced by the appearance of the
+history expansion character, which is `!' by default.
+
+
+An event designator is a reference to a command line entry in the
+history list.
+
+
+
+Word designators are used to select desired words from the event.
+A `:' separates the event specification from the word designator. It
+may be omitted if the word designator begins with a `^', `$',
+`*', `-', or `%'. Words are numbered from the beginning
+of the line, with the first word being denoted by 0 (zero). Words are
+inserted into the current line separated by single spaces.
+
+
+If a word designator is supplied without an event specification, the
+previous command is used as the event.
+
+
+After the optional word designator, you can add a sequence of one or more
+of the following modifiers, each preceded by a `:'.
+
+
+This chapter describes how to interface programs that you write
+with the GNU History Library.
+It should be considered a technical guide.
+For information on the interactive use of GNU History, see section Using History Interactively.
+
+
+Many programs read input from the user a line at a time. The GNU History
+library is able to keep track of those lines, associate arbitrary data with
+each line, and utilize information from previous lines in composing new
+ones.
+
+
+The programmer using the History library has available functions
+for remembering lines on a history list, associating arbitrary data
+with a line, removing lines from the list, searching through the list
+for a line containing an arbitrary text string, and referencing any line
+in the list directly. In addition, a history expansion function
+is available which provides for a consistent user interface across
+different programs.
+
+
+The user using programs written with the History library has the
+benefit of a consistent user interface with a set of well-known
+commands for manipulating the text of previous lines and using that text
+in new commands. The basic history manipulation commands are similar to
+the history substitution provided by
+If the programmer desires, he can use the Readline library, which
+includes some history manipulation by default, and has the added
+advantage of command line editing.
+
+
+The history list is an array of history entries. A history entry is
+declared as follows:
+
+
+The history list itself might therefore be declared as
+
+
+The state of the History library is encapsulated into a single structure:
+
+
+If the flags member includes
+This section describes the calling sequence for the various functions
+present in GNU History.
+
+
+This section describes functions used to initialize and manage
+the state of the History library when you want to use the history
+functions in your program.
+
+
+GNU History Library
+Edition 2.1, for
+History Library
Version 2.1.March 1996
+Brian Fox, Free Software Foundation
+Chet Ramey, Case Western Reserve University
+
+675 Massachusetts Avenue,
+Cambridge, MA 02139 USA
+
+Using History Interactively
+
+History Expansion
+
+csh
. This section
+describes the syntax used to manipulate the history information.
+
+Event Designators
+
+
+
+
+
+
+
+!
+!n
+!-n
+!!
+!string
+!?string[?]
+^string1^string2^
+!!:s/string1/string2/
.
+
+!#
+Word Designators
+
+
+
+
+
+0 (zero)
+0
th word. For many applications, this is the command word.
+
+n
+^
+$
+%
+x-y
+*
+0
th. This is a synonym for `1-$'.
+It is not an error to use `*' if there is just one word in the event;
+the empty string is returned in that case.
+
+x*
+x-
+Modifiers
+
+
+
+
+
+
+
+h
+t
+r
+e
+p
+s/old/new/
+&
+g
+gs/old/new/
,
+or with `&'.
+
+Programming with GNU History
+
+Introduction to History
+
+csh
.
+
+History Storage
+
+
+typedef struct _hist_entry {
+ char *line;
+ char *data;
+} HIST_ENTRY;
+
+
+
+HIST_ENTRY **the_history_list;
+
+
+
+/* A structure used to pass the current state of the history stuff around. */
+typedef struct _hist_state {
+ HIST_ENTRY **entries; /* Pointer to the entries themselves. */
+ int offset; /* The location pointer within this array. */
+ int length; /* Number of elements within this array. */
+ int size; /* Number of slots allocated to this array. */
+ int flags;
+} HISTORY_STATE;
+
+
+HS_STIFLED
, the history has been
+stifled.
+
+History Functions
+
+Initializing History and State Management
+
+
+
+
+These functions manage individual entries on the history list, or set +parameters managing the list itself. + +
++
NULL
.
++
+
NULL
pointer is returned.
++
+
+
+
+These functions return information about the entire history list or +individual list entries. + +
++
NULL
terminated array of HIST_ENTRY
which is the
+current input history. Element 0 of this list is the beginning of time.
+If there is no history, return NULL
.
++
+
where_history ()
. If there is no entry there, return a NULL
+pointer.
++
history_base
. If there is no entry there, or if offset
+is greater than the history length, return a NULL
pointer.
++
+These functions allow the current index into the history list to be +set or changed. + +
++
+
NULL
pointer.
++
NULL
pointer.
++These functions allow searching of the history list for entries containing +a specific string. Searching may be performed both forward and backward +from the current history position. The search may be anchored, +meaning that the string must match at the beginning of the history entry. + + +
++
+
+
+The History library can read the history from and write it to a file. +This section documents the functions for managing a history file. + +
++
NULL
, then read from
+`~/.history'. Returns 0 if successful, or errno if not.
++
NULL
, then read from `~/.history'. Returns 0 if successful,
+or errno
if not.
++
NULL
, then write the history list to `~/.history'. Values
+returned are as in read_history ()
.
++
+
+These functions implement csh
-like history expansion.
+
+
+
0
+1
+-1
+2
+:p
modifier (see section Modifiers).
++If an error ocurred in expansion, then output contains a descriptive +error message. +
+
+
+
()<>;&|$
, and shell quoting conventions are
+obeyed.
++This section describes the externally visible variables exported by +the GNU History Library. + +
++
+
+
stifle_history ()
.
++
+
+
+
+
+
+
char *
(string) and an integer index into that string (i).
+It should return a non-zero value if the history expansion starting at
+string[i] should not be performed; zero if the expansion should
+be done.
+It is intended for use by applications like Bash that use the history
+expansion character for additional purposes.
+By default, this variable is set to NULL.
++The following program demonstrates simple use of the GNU History Library. + +
+ ++main () +{ + char line[1024], *t; + int len, done = 0; + + line[0] = 0; + + using_history (); + while (!done) + { + printf ("history$ "); + fflush (stdout); + t = fgets (line, sizeof (line) - 1, stdin); + if (t && *t) + { + len = strlen (t); + if (t[len - 1] == '\n') + t[len - 1] = '\0'; + } + + if (!t) + strcpy (line, "quit"); + + if (line[0]) + { + char *expansion; + int result; + + result = history_expand (line, &expansion); + if (result) + fprintf (stderr, "%s\n", expansion); + + if (result < 0 || result == 2) + { + free (expansion); + continue; + } + + add_history (expansion); + strncpy (line, expansion, sizeof (line) - 1); + free (expansion); + } + + if (strcmp (line, "quit") == 0) + done = 1; + else if (strcmp (line, "save") == 0) + write_history ("history_file"); + else if (strcmp (line, "read") == 0) + read_history ("history_file"); + else if (strcmp (line, "list") == 0) + { + register HIST_ENTRY **the_list; + register int i; + + the_list = history_list (); + if (the_list) + for (i = 0; the_list[i]; i++) + printf ("%d: %s\n", i + history_base, the_list[i]->line); + } + else if (strncmp (line, "delete", 6) == 0) + { + int which; + if ((sscanf (line + 6, "%d", &which)) == 1) + { + HIST_ENTRY *entry = remove_history (which); + if (!entry) + fprintf (stderr, "No such entry %d\n", which); + else + { + free (entry->line); + free (entry); + } + } + else + { + fprintf (stderr, "non-numeric arg given to `delete'\n"); + } + } + } +} ++ + + +
+
+
+This document was generated on 2 April 1998 using the +texi2html +translator version 1.51.
+ + diff --git a/readline/doc/history.info b/readline/doc/history.info index df7651d274f..f3c59a17786 100644 --- a/readline/doc/history.info +++ b/readline/doc/history.info @@ -1,15 +1,21 @@ -Info file history.info, produced by Makeinfo, -*- Text -*- from input -file hist.texinfo. +This is Info file history.info, produced by Makeinfo version 1.67 from +the input file /usr/homes/chet/src/bash/readline-2.2/doc/hist.texinfo. + +INFO-DIR-SECTION Libraries +START-INFO-DIR-ENTRY +* History: (history). The GNU history library API +END-INFO-DIR-ENTRY This document describes the GNU History library, a programming tool that provides a consistent user interface for recalling lines of previously typed input. - Copyright (C) 1988, 1991 Free Software Foundation, Inc. + Copyright (C) 1988, 1991, 1993, 1995, 1996 Free Software Foundation, +Inc. - Permission is granted to make and distribute verbatim copies of -this manual provided the copyright notice and this permission notice -pare preserved on all copies. + Permission is granted to make and distribute verbatim copies of this +manual provided the copyright notice and this permission notice pare +preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that @@ -22,7 +28,7 @@ versions, except that this permission notice may be stated in a translation approved by the Foundation. -File: history.info, Node: Top, Next: Using History Interactively, Prev: (DIR), Up: (DIR) +File: history.info, Node: Top, Next: Using History Interactively, Up: (dir) GNU History Library ******************* @@ -47,8 +53,8 @@ Using History Interactively This chapter describes how to use the GNU History Library interactively, from a user's standpoint. It should be considered a -user's guide. For information on using the GNU History Library in -your own programs, *note Programming with GNU History::.. +user's guide. For information on using the GNU History Library in your +own programs, *note Programming with GNU History::.. * Menu: @@ -57,27 +63,34 @@ your own programs, *note Programming with GNU History::.. File: history.info, Node: History Interaction, Up: Using History Interactively -History Interaction -=================== +History Expansion +================= The History library provides a history expansion feature that is -similar to the history expansion in Csh. The following text describes -the sytax that you use to manipulate the history information. +similar to the history expansion provided by `csh'. This section +describes the syntax used to manipulate the history information. + + History expansions introduce words from the history list into the +input stream, making it easy to repeat commands, insert the arguments +to a previous command into the current input line, or fix errors in +previous commands quickly. History expansion takes place in two parts. The first is to -determine which line from the previous history should be used during +determine which line from the history list should be used during substitution. The second is to select portions of that line for -inclusion into the current one. The line selected from the previous -history is called the "event", and the portions of that line that are -acted upon are called "words". The line is broken into words in the -same fashion that the Bash shell does, so that several English (or -Unix) words surrounded by quotes are considered as one word. +inclusion into the current one. The line selected from the history is +called the "event", and the portions of that line that are acted upon +are called "words". Various "modifiers" are available to manipulate +the selected words. The line is broken into words in the same fashion +that Bash does, so that several words surrounded by quotes are +considered one word. History expansions are introduced by the +appearance of the history expansion character, which is `!' by default. * Menu: * Event Designators:: How to specify which history line to use. * Word Designators:: Specifying which words are of interest. -* Modifiers:: Modifying the results of susbstitution. +* Modifiers:: Modifying the results of substitution. File: history.info, Node: Event Designators, Next: Word Designators, Up: History Interaction @@ -89,23 +102,32 @@ Event Designators history list. `!' - Start a history subsititution, except when followed by a space, - tab, or the end of the line... = or (. + Start a history substitution, except when followed by a space, tab, + the end of the line, `=' or `('. + +`!N' + Refer to command line N. + +`!-N' + Refer to the command N lines back. `!!' Refer to the previous command. This is a synonym for `!-1'. -`!n' - Refer to command line N. +`!STRING' + Refer to the most recent command starting with STRING. -`!-n' - Refer to the command line N lines back. +`!?STRING[?]' + Refer to the most recent command containing STRING. The trailing + `?' may be omitted if the STRING is followed immediately by a + newline. -`!string' - Refer to the most recent command starting with STRING. +`^STRING1^STRING2^' + Quick Substitution. Repeat the last command, replacing STRING1 + with STRING2. Equivalent to `!!:s/STRING1/STRING2/'. -`!?string'[`?'] - Refer to the most recent command containing STRING. +`!#' + The entire command line typed so far. File: history.info, Node: Word Designators, Next: Modifiers, Prev: Event Designators, Up: History Interaction @@ -113,34 +135,44 @@ File: history.info, Node: Word Designators, Next: Modifiers, Prev: Event Desi Word Designators ---------------- - A : separates the event specification from the word designator. It -can be omitted if the word designator begins with a ^, $, * or %. -Words are numbered from the beginning of the line, with the first word -being denoted by a 0 (zero). + Word designators are used to select desired words from the event. A +`:' separates the event specification from the word designator. It may +be omitted if the word designator begins with a `^', `$', `*', `-', or +`%'. Words are numbered from the beginning of the line, with the first +word being denoted by 0 (zero). Words are inserted into the current +line separated by single spaces. `0 (zero)' - The zero'th word. For many applications, this is the command - word. + The `0'th word. For many applications, this is the command word. -`n' - The N'th word. +`N' + The Nth word. `^' - The first argument. that is, word 1. + The first argument; that is, word 1. `$' The last argument. `%' - The word matched by the most recent `?string?' search. + The word matched by the most recent `?STRING?' search. -`x-y' - A range of words; `-Y' Abbreviates `0-Y'. +`X-Y' + A range of words; `-Y' abbreviates `0-Y'. `*' - All of the words, excepting the zero'th. This is a synonym for - `1-$'. It is not an error to use * if there is just one word in - the event. The empty string is returned in that case. + All of the words, except the `0'th. This is a synonym for `1-$'. + It is not an error to use `*' if there is just one word in the + event; the empty string is returned in that case. + +`X*' + Abbreviates `X-$' + +`X-' + Abbreviates `X-$' like `X*', but omits the last word. + + If a word designator is supplied without an event specification, the +previous command is used as the event. File: history.info, Node: Modifiers, Prev: Word Designators, Up: History Interaction @@ -148,38 +180,48 @@ File: history.info, Node: Modifiers, Prev: Word Designators, Up: History Inte Modifiers --------- - After the optional word designator, you can add a sequence of one -or more of the following modifiers, each preceded by a :. - -`#' - The entire command line typed so far. This means the current - command, not the previous command, so it really isn't a word - designator, and doesn't belong in this section. + After the optional word designator, you can add a sequence of one or +more of the following modifiers, each preceded by a `:'. `h' Remove a trailing pathname component, leaving only the head. +`t' + Remove all leading pathname components, leaving the tail. + `r' - Remove a trailing suffix of the form `.'SUFFIX, leaving the + Remove a trailing suffix of the form `.SUFFIX', leaving the basename. `e' - Remove all but the suffix. - -`t' - Remove all leading pathname components, leaving the tail. + Remove all but the trailing suffix. `p' Print the new command but do not execute it. +`s/OLD/NEW/' + Substitute NEW for the first occurrence of OLD in the event line. + Any delimiter may be used in place of `/'. The delimiter may be + quoted in OLD and NEW with a single backslash. If `&' appears in + NEW, it is replaced by OLD. A single backslash will quote the + `&'. The final delimiter is optional if it is the last character + on the input line. + +`&' + Repeat the previous substitution. + +`g' + Cause changes to be applied over the entire event line. Used in + conjunction with `s', as in `gs/OLD/NEW/', or with `&'. + File: history.info, Node: Programming with GNU History, Next: Concept Index, Prev: Using History Interactively, Up: Top Programming with GNU History **************************** - This chapter describes how to interface the GNU History Library with -programs that you write. It should be considered a technical guide. + This chapter describes how to interface programs that you write with +the GNU History Library. It should be considered a technical guide. For information on the interactive use of GNU History, *note Using History Interactively::.. @@ -198,27 +240,27 @@ Introduction to History ======================= Many programs read input from the user a line at a time. The GNU -history library is able to keep track of those lines, associate +History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous -lines in making up new ones. - - The programmer using the History library has available to him -functions for remembering lines on a history stack, associating -arbitrary data with a line, removing lines from the stack, searching -through the stack for a line containing an arbitrary text string, and -referencing any line on the stack directly. In addition, a history -"expansion" function is available which provides for a consistent user -interface across many different programs. - - The end-user using programs written with the History library has the -benifit of a consistent user interface, with a set of well-known -commands for manipulating the text of previous lines and using that -text in new commands. The basic history manipulation commands are -similar to the history substitution used by `Csh'. +lines in composing new ones. + + The programmer using the History library has available functions for +remembering lines on a history list, associating arbitrary data with a +line, removing lines from the list, searching through the list for a +line containing an arbitrary text string, and referencing any line in +the list directly. In addition, a history "expansion" function is +available which provides for a consistent user interface across +different programs. + + The user using programs written with the History library has the +benefit of a consistent user interface with a set of well-known +commands for manipulating the text of previous lines and using that text +in new commands. The basic history manipulation commands are similar to +the history substitution provided by `csh'. If the programmer desires, he can use the Readline library, which includes some history manipulation by default, and has the added -advantage of Emacs style command line editing. +advantage of command line editing. File: history.info, Node: History Storage, Next: History Functions, Prev: Introduction to History, Up: Programming with GNU History @@ -226,139 +268,290 @@ File: history.info, Node: History Storage, Next: History Functions, Prev: Int History Storage =============== + The history list is an array of history entries. A history entry is +declared as follows: + typedef struct _hist_entry { char *line; char *data; } HIST_ENTRY; + The history list itself might therefore be declared as + + HIST_ENTRY **the_history_list; + + The state of the History library is encapsulated into a single +structure: + + /* A structure used to pass the current state of the history stuff around. */ + typedef struct _hist_state { + HIST_ENTRY **entries; /* Pointer to the entries themselves. */ + int offset; /* The location pointer within this array. */ + int length; /* Number of elements within this array. */ + int size; /* Number of slots allocated to this array. */ + int flags; + } HISTORY_STATE; + + If the flags member includes `HS_STIFLED', the history has been +stifled. + File: history.info, Node: History Functions, Next: History Variables, Prev: History Storage, Up: Programming with GNU History History Functions ================= - This section describes the calling sequence for the various -functions present in GNU History. + This section describes the calling sequence for the various functions +present in GNU History. + +* Menu: + +* Initializing History and State Management:: Functions to call when you + want to use history in a + program. +* History List Management:: Functions used to manage the list + of history entries. +* Information About the History List:: Functions returning information about + the history list. +* Moving Around the History List:: Functions used to change the position + in the history list. +* Searching the History List:: Functions to search the history list + for entries containing a string. +* Managing the History File:: Functions that read and write a file + containing the history list. +* History Expansion:: Functions to perform csh-like history + expansion. + + +File: history.info, Node: Initializing History and State Management, Next: History List Management, Up: History Functions - * Function: void using_history () - Begin a session in which the history functions might be used. - This just initializes the interactive variables. +Initializing History and State Management +----------------------------------------- + + This section describes functions used to initialize and manage the +state of the History library when you want to use the history functions +in your program. + + - Function: void using_history () + Begin a session in which the history functions might be used. This + initializes the interactive variables. + + - Function: HISTORY_STATE * history_get_history_state () + Return a structure describing the current state of the input + history. + + - Function: void history_set_history_state (HISTORY_STATE *state) + Set the state of the history list according to STATE. + + +File: history.info, Node: History List Management, Next: Information About the History List, Prev: Initializing History and State Management, Up: History Functions - * Function: void add_history (CHAR *STRING) +History List Management +----------------------- + + These functions manage individual entries on the history list, or set +parameters managing the list itself. + + - Function: void add_history (char *string) Place STRING at the end of the history list. The associated data field (if any) is set to `NULL'. - * Function: int where_history () - Returns the number which says what history element we are now - looking at. + - Function: HIST_ENTRY * remove_history (int which) + Remove history entry at offset WHICH from the history. The + removed element is returned so you can free the line, data, and + containing structure. - * Function: int history_set_pos (INT POS) - Set the position in the history list to POS. + - Function: HIST_ENTRY * replace_history_entry (int which, char *line, + char *data) + Make the history entry at offset WHICH have LINE and DATA. This + returns the old entry so you can dispose of the data. In the case + of an invalid WHICH, a `NULL' pointer is returned. - * Function: int history_search_pos (CHAR *STRING, INT DIRECTION, INT - POS) - Search for STRING in the history list, starting at POS, an - absolute index into the list. DIRECTION, if negative, says to - search backwards from POS, else forwards. Returns the absolute - index of the history element where STRING was found, or -1 - otherwise. - - * Function: HIST_ENTRY *remove_history (); - Remove history element WHICH from the history. The removed - element is returned to you so you can free the line, data, and - containing structure. + - Function: void clear_history () + Clear the history list by deleting all the entries. - * Function: void stifle_history (INT MAX) - Stifle the history list, remembering only MAX number of entries. + - Function: void stifle_history (int max) + Stifle the history list, remembering only the last MAX entries. - * Function: int unstifle_history (); + - Function: int unstifle_history () Stop stifling the history. This returns the previous amount the - history was stifled by. The value is positive if the history was + history was stifled. The value is positive if the history was stifled, negative if it wasn't. - * Function: int read_history (CHAR *FILENAME) + - Function: int history_is_stifled () + Returns non-zero if the history is stifled, zero if it is not. + + +File: history.info, Node: Information About the History List, Next: Moving Around the History List, Prev: History List Management, Up: History Functions + +Information About the History List +---------------------------------- + + These functions return information about the entire history list or +individual list entries. + + - Function: HIST_ENTRY ** history_list () + Return a `NULL' terminated array of `HIST_ENTRY' which is the + current input history. Element 0 of this list is the beginning of + time. If there is no history, return `NULL'. + + - Function: int where_history () + Returns the offset of the current history element. + + - Function: HIST_ENTRY * current_history () + Return the history entry at the current position, as determined by + `where_history ()'. If there is no entry there, return a `NULL' + pointer. + + - Function: HIST_ENTRY * history_get (int offset) + Return the history entry at position OFFSET, starting from + `history_base'. If there is no entry there, or if OFFSET is + greater than the history length, return a `NULL' pointer. + + - Function: int history_total_bytes () + Return the number of bytes that the primary history entries are + using. This function returns the sum of the lengths of all the + lines in the history. + + +File: history.info, Node: Moving Around the History List, Next: Searching the History List, Prev: Information About the History List, Up: History Functions + +Moving Around the History List +------------------------------ + + These functions allow the current index into the history list to be +set or changed. + + - Function: int history_set_pos (int pos) + Set the position in the history list to POS, an absolute index + into the list. + + - Function: HIST_ENTRY * previous_history () + Back up the current history offset to the previous history entry, + and return a pointer to that entry. If there is no previous + entry, return a `NULL' pointer. + + - Function: HIST_ENTRY * next_history () + Move the current history offset forward to the next history entry, + and return the a pointer to that entry. If there is no next + entry, return a `NULL' pointer. + + +File: history.info, Node: Searching the History List, Next: Managing the History File, Prev: Moving Around the History List, Up: History Functions + +Searching the History List +-------------------------- + + These functions allow searching of the history list for entries +containing a specific string. Searching may be performed both forward +and backward from the current history position. The search may be +"anchored", meaning that the string must match at the beginning of the +history entry. + + - Function: int history_search (char *string, int direction) + Search the history for STRING, starting at the current history + offset. If DIRECTION < 0, then the search is through previous + entries, else through subsequent. If STRING is found, then the + current history index is set to that history entry, and the value + returned is the offset in the line of the entry where STRING was + found. Otherwise, nothing is changed, and a -1 is returned. + + - Function: int history_search_prefix (char *string, int direction) + Search the history for STRING, starting at the current history + offset. The search is anchored: matching lines must begin with + STRING. If DIRECTION < 0, then the search is through previous + entries, else through subsequent. If STRING is found, then the + current history index is set to that entry, and the return value + is 0. Otherwise, nothing is changed, and a -1 is returned. + + - Function: int history_search_pos (char *string, int direction, int + pos) + Search for STRING in the history list, starting at POS, an + absolute index into the list. If DIRECTION is negative, the search + proceeds backward from POS, otherwise forward. Returns the + absolute index of the history element where STRING was found, or + -1 otherwise. + + +File: history.info, Node: Managing the History File, Next: History Expansion, Prev: Searching the History List, Up: History Functions + +Managing the History File +------------------------- + + The History library can read the history from and write it to a file. +This section documents the functions for managing a history file. + + - Function: int read_history (char *filename) Add the contents of FILENAME to the history list, a line at a - time. If FILENAME is `NULL', then read from `~/.history'. + time. If FILENAME is `NULL', then read from `~/.history'. Returns 0 if successful, or errno if not. - * Function: int read_history_range (CHAR *FILENAME, INT FROM, INT TO) + - Function: int read_history_range (char *filename, int from, int to) Read a range of lines from FILENAME, adding them to the history - list. Start reading at the FROM'th line and end at the TO'th. If - FROM is zero, start at the beginning. If TO is less than FROM, - then read until the end of the file. If FILENAME is `NULL', then - read from `~/.history'. Returns 0 if successful, or `errno' if - not. - - * Function: int write_history (CHAR *FILENAME) - Append the current history to FILENAME. If FILENAME is `NULL', - then append the history list to `~/.history'. Values returned - are as in `read_history ()'. - - * Function: int append_history (INT NELEMENTS, CHAR *FILENAME) - Append NELEMENT entries to FILENAME. The entries appended are - from the end of the list minus NELEMENTS up to the end of the - list. - - * Function: HIST_ENTRY *replace_history_entry () - Make the history entry at WHICH have LINE and DATA. This returns - the old entry so you can dispose of the data. In the case of an - invalid WHICH, a `NULL' pointer is returned. - - * Function: HIST_ENTRY *current_history () - Return the history entry at the current position, as determined by - `history_offset'. If there is no entry there, return a `NULL' - pointer. + list. Start reading at line FROM and end at TO. If FROM is zero, + start at the beginning. If TO is less than FROM, then read until + the end of the file. If FILENAME is `NULL', then read from + `~/.history'. Returns 0 if successful, or `errno' if not. - * Function: HIST_ENTRY *previous_history () - Back up HISTORY_OFFSET to the previous history entry, and return a - pointer to that entry. If there is no previous entry, return a - `NULL' pointer. + - Function: int write_history (char *filename) + Write the current history to FILENAME, overwriting FILENAME if + necessary. If FILENAME is `NULL', then write the history list to + `~/.history'. Values returned are as in `read_history ()'. - * Function: HIST_ENTRY *next_history () - Move `history_offset' forward to the next history entry, and - return the a pointer to that entry. If there is no next entry, - return a `NULL' pointer. + - Function: int append_history (int nelements, char *filename) + Append the last NELEMENTS of the history list to FILENAME. - * Function: HIST_ENTRY **history_list () - Return a `NULL' terminated array of `HIST_ENTRY' which is the - current input history. Element 0 of this list is the beginning - of time. If there is no history, return `NULL'. - - * Function: int history_search (CHAR *STRING, INT DIRECTION) - Search the history for STRING, starting at `history_offset'. If - DIRECTION < 0, then the search is through previous entries, else - through subsequent. If STRING is found, then `current_history - ()' is the history entry, and the value of this function is the - offset in the line of that history entry that the STRING was - found in. Otherwise, nothing is changed, and a -1 is returned. - - * Function: int history_expand (CHAR *STRING, CHAR **OUTPUT) - Expand STRING, placing the result into OUTPUT, a pointer to a - string. Returns: + - Function: int history_truncate_file (char *filename, int nlines) + Truncate the history file FILENAME, leaving only the last NLINES + lines. + +File: history.info, Node: History Expansion, Prev: Managing the History File, Up: History Functions + +History Expansion +----------------- + + These functions implement `csh'-like history expansion. + + - Function: int history_expand (char *string, char **output) + Expand STRING, placing the result into OUTPUT, a pointer to a + string (*note History Interaction::.). Returns: `0' If no expansions took place (or, if the only change in the text was the de-slashifying of the history expansion - character), + character); `1' - if expansions did take place, or + if expansions did take place; `-1' - if there was an error in expansion. + if there was an error in expansion; + + `2' + if the returned line should only be displayed, but not + executed, as with the `:p' modifier (*note Modifiers::.). - If an error ocurred in expansion, then OUTPUT contains a + If an error ocurred in expansion, then OUTPUT contains a descriptive error message. - * Function: char *history_arg_extract (INT FIRST, INT LAST, CHAR - *STRING) + - Function: char * history_arg_extract (int first, int last, char + *string) Extract a string segment consisting of the FIRST through LAST - arguments present in STRING. Arguments are broken up as in the - GNU Bash shell. - - * Function: int history_total_bytes (); - Return the number of bytes that the primary history entries are - using. This just adds up the lengths of `the_history->lines'. + arguments present in STRING. Arguments are broken up as in Bash. + + - Function: char * get_history_event (char *string, int *cindex, int + qchar) + Returns the text of the history event beginning at STRING + + *CINDEX. *CINDEX is modified to point to after the event + specifier. At function entry, CINDEX points to the index into + STRING where the history event specification begins. QCHAR is a + character that is allowed to end the event specification in + addition to the "normal" terminating characters. + + - Function: char ** history_tokenize (char *string) + Return an array of tokens parsed out of STRING, much as the shell + might. The tokens are split on white space and on the characters + `()<>;&|$', and shell quoting conventions are obeyed. File: history.info, Node: History Variables, Next: History Programming Example, Prev: History Functions, Up: Programming with GNU History @@ -366,12 +559,54 @@ File: history.info, Node: History Variables, Next: History Programming Example History Variables ================= - This section describes the variables in GNU History that are -externally visible. + This section describes the externally visible variables exported by +the GNU History Library. + + - Variable: int history_base + The logical offset of the first entry in the history list. + + - Variable: int history_length + The number of entries currently stored in the history list. + + - Variable: int max_input_history + The maximum number of history entries. This must be changed using + `stifle_history ()'. + + - Variable: char history_expansion_char + The character that starts a history event. The default is `!'. - * Variable: int history_base - For convenience only. You set this when interpreting history - commands. It is the logical offset of the first history element. + - Variable: char history_subst_char + The character that invokes word substitution if found at the start + of a line. The default is `^'. + + - Variable: char history_comment_char + During tokenization, if this character is seen as the first + character of a word, then it and all subsequent characters up to a + newline are ignored, suppressing history expansion for the + remainder of the line. This is disabled by default. + + - Variable: char * history_no_expand_chars + The list of characters which inhibit history expansion if found + immediately following HISTORY_EXPANSION_CHAR. The default is + whitespace and `='. + + - Variable: char * history_search_delimiter_chars + The list of additional characters which can delimit a history + search string, in addition to whitespace, `:' and `?' in the case + of a substring search. The default is empty. + + - Variable: int history_quotes_inhibit_expansion + If non-zero, single-quoted words are not scanned for the history + expansion character. The default value is 0. + + - Variable: Function * history_inhibit_expansion_function + This should be set to the address of a function that takes two + arguments: a `char *' (STRING) and an integer index into that + string (I). It should return a non-zero value if the history + expansion starting at STRING[I] should not be performed; zero if + the expansion should be done. It is intended for use by + applications like Bash that use the history expansion character + for additional purposes. By default, this variable is set to NULL. File: history.info, Node: History Programming Example, Prev: History Variables, Up: Programming with GNU History @@ -379,20 +614,28 @@ File: history.info, Node: History Programming Example, Prev: History Variables History Programming Example =========================== - The following snippet of code demonstrates simple use of the GNU -History Library. + The following program demonstrates simple use of the GNU History +Library. main () { char line[1024], *t; - int done = 0; + int len, done = 0; line[0] = 0; + using_history (); while (!done) { - fprintf (stdout, "history%% "); - t = gets (line); + printf ("history$ "); + fflush (stdout); + t = fgets (line, sizeof (line) - 1, stdin); + if (t && *t) + { + len = strlen (t); + if (t[len - 1] == '\n') + t[len - 1] = '\0'; + } if (!t) strcpy (line, "quit"); @@ -402,37 +645,41 @@ History Library. char *expansion; int result; - using_history (); - result = history_expand (line, &expansion); - strcpy (line, expansion); - free (expansion); if (result) - fprintf (stderr, "%s\n", line); + fprintf (stderr, "%s\n", expansion); - if (result < 0) - continue; + if (result < 0 || result == 2) + { + free (expansion); + continue; + } - add_history (line); + add_history (expansion); + strncpy (line, expansion, sizeof (line) - 1); + free (expansion); } - if (strcmp (line, "quit") == 0) done = 1; - if (strcmp (line, "save") == 0) write_history (0); - if (strcmp (line, "read") == 0) read_history (0); - if (strcmp (line, "list") == 0) + if (strcmp (line, "quit") == 0) + done = 1; + else if (strcmp (line, "save") == 0) + write_history ("history_file"); + else if (strcmp (line, "read") == 0) + read_history ("history_file"); + else if (strcmp (line, "list") == 0) { - register HIST_ENTRY **the_list = history_list (); + register HIST_ENTRY **the_list; register int i; + the_list = history_list (); if (the_list) for (i = 0; the_list[i]; i++) - fprintf (stdout, "%d: %s\n", - i + history_base, the_list[i]->line); + printf ("%d: %s\n", i + history_base, the_list[i]->line); } - if (strncmp (line, "delete", strlen ("delete")) == 0) + else if (strncmp (line, "delete", 6) == 0) { int which; - if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1) + if ((sscanf (line + 6, "%d", &which)) == 1) { HIST_ENTRY *entry = remove_history (which); if (!entry) @@ -459,8 +706,11 @@ Concept Index * Menu: -* event designators: Event Designators. -* expansion: History Interaction. +* anchored search: Searching the History List. +* event designators: Event Designators. +* history events: Event Designators. +* history expansion: History Interaction. +* History Searching: Searching the History List. File: history.info, Node: Function and Variable Index, Prev: Concept Index, Up: Top @@ -470,45 +720,70 @@ Function and Variable Index * Menu: -* HIST_ENTRY **history_list: History Functions. -* HIST_ENTRY *current_history: History Functions. -* HIST_ENTRY *next_history: History Functions. -* HIST_ENTRY *previous_history: History Functions. -* HIST_ENTRY *remove_history: History Functions. -* HIST_ENTRY *replace_history_entry: History Functions. -* char *history_arg_extract: History Functions. -* int append_history: History Functions. -* int history_base: History Variables. -* int history_expand: History Functions. -* int history_search: History Functions. -* int history_search_pos: History Functions. -* int history_set_pos: History Functions. -* int history_total_bytes: History Functions. -* int read_history: History Functions. -* int read_history_range: History Functions. -* int unstifle_history: History Functions. -* int where_history: History Functions. -* int write_history: History Functions. -* void add_history: History Functions. -* void stifle_history: History Functions. -* void using_history: History Functions. +* add_history: History List Management. +* append_history: Managing the History File. +* clear_history: History List Management. +* current_history: Information About the History List. +* get_history_event: History Expansion. +* history_arg_extract: History Expansion. +* history_base: History Variables. +* history_comment_char: History Variables. +* history_expand: History Expansion. +* history_expansion_char: History Variables. +* history_get: Information About the History List. +* history_get_history_state: Initializing History and State Management. +* history_inhibit_expansion_function: History Variables. +* history_is_stifled: History List Management. +* history_length: History Variables. +* history_list: Information About the History List. +* history_no_expand_chars: History Variables. +* history_quotes_inhibit_expansion: History Variables. +* history_search: Searching the History List. +* history_search_delimiter_chars: History Variables. +* history_search_pos: Searching the History List. +* history_search_prefix: Searching the History List. +* history_set_history_state: Initializing History and State Management. +* history_set_pos: Moving Around the History List. +* history_subst_char: History Variables. +* history_tokenize: History Expansion. +* history_total_bytes: Information About the History List. +* history_truncate_file: Managing the History File. +* max_input_history: History Variables. +* next_history: Moving Around the History List. +* previous_history: Moving Around the History List. +* read_history: Managing the History File. +* read_history_range: Managing the History File. +* remove_history: History List Management. +* replace_history_entry: History List Management. +* stifle_history: History List Management. +* unstifle_history: History List Management. +* using_history: Initializing History and State Management. +* where_history: Information About the History List. +* write_history: Managing the History File. Tag Table: -Node: Top973 -Node: Using History Interactively1567 -Node: History Interaction2075 -Node: Event Designators3127 -Node: Word Designators3770 -Node: Modifiers4676 -Node: Programming with GNU History5425 -Node: Introduction to History6152 -Node: History Storage7502 -Node: History Functions7766 -Node: History Variables13063 -Node: History Programming Example13499 -Node: Concept Index15744 -Node: Function and Variable Index16030 +Node: Top1167 +Node: Using History Interactively1747 +Node: History Interaction2255 +Node: Event Designators3674 +Node: Word Designators4601 +Node: Modifiers5850 +Node: Programming with GNU History6988 +Node: Introduction to History7714 +Node: History Storage9035 +Node: History Functions10128 +Node: Initializing History and State Management11099 +Node: History List Management11891 +Node: Information About the History List13412 +Node: Moving Around the History List14718 +Node: Searching the History List15603 +Node: Managing the History File17435 +Node: History Expansion18941 +Node: History Variables20785 +Node: History Programming Example23103 +Node: Concept Index25707 +Node: Function and Variable Index26193 End Tag Table diff --git a/readline/doc/history.ps b/readline/doc/history.ps new file mode 100644 index 00000000000..b9a28e87fb6 --- /dev/null +++ b/readline/doc/history.ps @@ -0,0 +1,2071 @@ +%!PS-Adobe-2.0 +%%Creator: dvipsk 5.58f Copyright 1986, 1994 Radical Eye Software +%%Title: history.dvi +%%Pages: 18 +%%PageOrder: Ascend +%%BoundingBox: 0 0 596 842 +%%DocumentPaperSizes: A4 +%%EndComments +%DVIPSCommandLine: dvips -D 300 -o history.ps history.dvi +%DVIPSParameters: dpi=300, comments removed +%DVIPSSource: TeX output 1998.04.02:1444 +%%BeginProcSet: tex.pro +/TeXDict 250 dict def TeXDict begin /N{def}def /B{bind def}N /S{exch}N +/X{S N}B /TR{translate}N /isls false N /vsize 11 72 mul N /hsize 8.5 72 +mul N /landplus90{false}def /@rigin{isls{[0 landplus90{1 -1}{-1 1} +ifelse 0 0 0]concat}if 72 Resolution div 72 VResolution div neg scale +isls{landplus90{VResolution 72 div vsize mul 0 exch}{Resolution -72 div +hsize mul 0}ifelse TR}if Resolution VResolution vsize -72 div 1 add mul +TR[matrix currentmatrix{dup dup round sub abs 0.00001 lt{round}if} +forall round exch round exch]setmatrix}N /@landscape{/isls true N}B +/@manualfeed{statusdict /manualfeed true put}B /@copies{/#copies X}B +/FMat[1 0 0 -1 0 0]N /FBB[0 0 0 0]N /nn 0 N /IE 0 N /ctr 0 N /df-tail{ +/nn 8 dict N nn begin /FontType 3 N /FontMatrix fntrx N /FontBBox FBB N +string /base X array /BitMaps X /BuildChar{CharBuilder}N /Encoding IE N +end dup{/foo setfont}2 array copy cvx N load 0 nn put /ctr 0 N[}B /df{ +/sf 1 N /fntrx FMat N df-tail}B /dfs{div /sf X /fntrx[sf 0 0 sf neg 0 0] +N df-tail}B /E{pop nn dup definefont setfont}B /ch-width{ch-data dup +length 5 sub get}B /ch-height{ch-data dup length 4 sub get}B /ch-xoff{ +128 ch-data dup length 3 sub get sub}B /ch-yoff{ch-data dup length 2 sub +get 127 sub}B /ch-dx{ch-data dup length 1 sub get}B /ch-image{ch-data +dup type /stringtype ne{ctr get /ctr ctr 1 add N}if}B /id 0 N /rw 0 N +/rc 0 N /gp 0 N /cp 0 N /G 0 N /sf 0 N /CharBuilder{save 3 1 roll S dup +/base get 2 index get S /BitMaps get S get /ch-data X pop /ctr 0 N ch-dx +0 ch-xoff ch-yoff ch-height sub ch-xoff ch-width add ch-yoff +setcachedevice ch-width ch-height true[1 0 0 -1 -.1 ch-xoff sub ch-yoff +.1 sub]{ch-image}imagemask restore}B /D{/cc X dup type /stringtype ne{]} +if nn /base get cc ctr put nn /BitMaps get S ctr S sf 1 ne{dup dup +length 1 sub dup 2 index S get sf div put}if put /ctr ctr 1 add N}B /I{ +cc 1 add D}B /bop{userdict /bop-hook known{bop-hook}if /SI save N @rigin +0 0 moveto /V matrix currentmatrix dup 1 get dup mul exch 0 get dup mul +add .99 lt{/QV}{/RV}ifelse load def pop pop}N /eop{SI restore userdict +/eop-hook known{eop-hook}if showpage}N /@start{userdict /start-hook +known{start-hook}if pop /VResolution X /Resolution X 1000 div /DVImag X +/IE 256 array N 0 1 255{IE S 1 string dup 0 3 index put cvn put}for +65781.76 div /vsize X 65781.76 div /hsize X}N /p{show}N /RMat[1 0 0 -1 0 +0]N /BDot 260 string N /rulex 0 N /ruley 0 N /v{/ruley X /rulex X V}B /V +{}B /RV statusdict begin /product where{pop product dup length 7 ge{0 7 +getinterval dup(Display)eq exch 0 4 getinterval(NeXT)eq or}{pop false} +ifelse}{false}ifelse end{{gsave TR -.1 .1 TR 1 1 scale rulex ruley false +RMat{BDot}imagemask grestore}}{{gsave TR -.1 .1 TR rulex ruley scale 1 1 +false RMat{BDot}imagemask grestore}}ifelse B /QV{gsave newpath transform +round exch round exch itransform moveto rulex 0 rlineto 0 ruley neg +rlineto rulex neg 0 rlineto fill grestore}B /a{moveto}B /delta 0 N /tail +{dup /delta X 0 rmoveto}B /M{S p delta add tail}B /b{S p tail}B /c{-4 M} +B /d{-3 M}B /e{-2 M}B /f{-1 M}B /g{0 M}B /h{1 M}B /i{2 M}B /j{3 M}B /k{ +4 M}B /w{0 rmoveto}B /l{p -4 w}B /m{p -3 w}B /n{p -2 w}B /o{p -1 w}B /q{ +p 1 w}B /r{p 2 w}B /s{p 3 w}B /t{p 4 w}B /x{0 S rmoveto}B /y{3 2 roll p +a}B /bos{/SS save N}B /eos{SS restore}B end +%%EndProcSet +TeXDict begin 39158280 55380996 1000 300 300 (history.dvi) +@start /Fa 1 47 df<70F8F8F0E005057B840E>46 D E /Fb 1 +47 df<0E003F007F807F80FF80FF80FF007E003C000909798815>46 +D E /Fc 11 121 df<00800100020004000C000800180030003000300060006000600060 +00E000E000E000E000E000E000E000E000E000E000600060006000600030003000300018 +0008000C00040002000100008009267D9B0F>40 D<8000400020001000180008000C0006 +000600060003000300030003000380038003800380038003800380038003800380030003 +00030003000600060006000C0008001800100020004000800009267E9B0F>IHistory Library
Version 2.1.+
+
+This document was generated on 2 April 1998 using the +texi2html +translator version 1.51.
+ + diff --git a/readline/doc/hstech.texinfo b/readline/doc/hstech.texinfo index c3fe3f6a11f..54100908407 100644 --- a/readline/doc/hstech.texinfo +++ b/readline/doc/hstech.texinfo @@ -1,8 +1,8 @@ @ignore This file documents the user interface to the GNU History library. -Copyright (C) 1988, 1991 Free Software Foundation, Inc. -Authored by Brian Fox. +Copyright (C) 1988, 1991, 1994, 1996 Free Software Foundation, Inc. +Authored by Brian Fox and Chet Ramey. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on @@ -26,8 +26,9 @@ into another language, under the above conditions for modified versions. @node Programming with GNU History @chapter Programming with GNU History -This chapter describes how to interface the GNU History Library with -programs that you write. It should be considered a technical guide. +This chapter describes how to interface programs that you write +with the GNU History Library. +It should be considered a technical guide. For information on the interactive use of GNU History, @pxref{Using History Interactively}. @@ -42,32 +43,35 @@ History Interactively}. @node Introduction to History @section Introduction to History -Many programs read input from the user a line at a time. The GNU history +Many programs read input from the user a line at a time. The GNU History library is able to keep track of those lines, associate arbitrary data with -each line, and utilize information from previous lines in making up new +each line, and utilize information from previous lines in composing new ones. -The programmer using the History library has available to him functions -for remembering lines on a history stack, associating arbitrary data -with a line, removing lines from the stack, searching through the stack +The programmer using the History library has available functions +for remembering lines on a history list, associating arbitrary data +with a line, removing lines from the list, searching through the list for a line containing an arbitrary text string, and referencing any line -on the stack directly. In addition, a history @dfn{expansion} function -is available which provides for a consistent user interface across many +in the list directly. In addition, a history @dfn{expansion} function +is available which provides for a consistent user interface across different programs. -The end-user using programs written with the History library has the -benifit of a consistent user interface, with a set of well-known +The user using programs written with the History library has the +benefit of a consistent user interface with a set of well-known commands for manipulating the text of previous lines and using that text in new commands. The basic history manipulation commands are similar to -the history substitution used by @code{Csh}. +the history substitution provided by @code{csh}. If the programmer desires, he can use the Readline library, which includes some history manipulation by default, and has the added -advantage of Emacs style command line editing. +advantage of command line editing. @node History Storage @section History Storage +The history list is an array of history entries. A history entry is +declared as follows: + @example typedef struct _hist_entry @{ char *line; @@ -75,179 +79,378 @@ typedef struct _hist_entry @{ @} HIST_ENTRY; @end example +The history list itself might therefore be declared as + +@example +HIST_ENTRY **the_history_list; +@end example + +The state of the History library is encapsulated into a single structure: + +@example +/* A structure used to pass the current state of the history stuff around. */ +typedef struct _hist_state @{ + HIST_ENTRY **entries; /* Pointer to the entries themselves. */ + int offset; /* The location pointer within this array. */ + int length; /* Number of elements within this array. */ + int size; /* Number of slots allocated to this array. */ + int flags; +@} HISTORY_STATE; +@end example + +If the flags member includes @code{HS_STIFLED}, the history has been +stifled. + @node History Functions @section History Functions This section describes the calling sequence for the various functions present in GNU History. -@defun {void using_history} () +@menu +* Initializing History and State Management:: Functions to call when you + want to use history in a + program. +* History List Management:: Functions used to manage the list + of history entries. +* Information About the History List:: Functions returning information about + the history list. +* Moving Around the History List:: Functions used to change the position + in the history list. +* Searching the History List:: Functions to search the history list + for entries containing a string. +* Managing the History File:: Functions that read and write a file + containing the history list. +* History Expansion:: Functions to perform csh-like history + expansion. +@end menu + +@node Initializing History and State Management +@subsection Initializing History and State Management + +This section describes functions used to initialize and manage +the state of the History library when you want to use the history +functions in your program. + +@deftypefun void using_history () Begin a session in which the history functions might be used. This -just initializes the interactive variables. -@end defun +initializes the interactive variables. +@end deftypefun + +@deftypefun {HISTORY_STATE *} history_get_history_state () +Return a structure describing the current state of the input history. +@end deftypefun + +@deftypefun void history_set_history_state (HISTORY_STATE *state) +Set the state of the history list according to @var{state}. +@end deftypefun + +@node History List Management +@subsection History List Management -@defun {void add_history} (char *string) +These functions manage individual entries on the history list, or set +parameters managing the list itself. + +@deftypefun void add_history (char *string) Place @var{string} at the end of the history list. The associated data field (if any) is set to @code{NULL}. -@end defun - -@defun {int where_history} () -Returns the number which says what history element we are now looking -at. -@end defun - -@defun {int history_set_pos} (int pos) -Set the position in the history list to @var{pos}. -@end defun - -@defun {int history_search_pos} (char *string, int direction, int pos) -Search for @var{string} in the history list, starting at @var{pos}, an -absolute index into the list. @var{direction}, if negative, says to search -backwards from @var{pos}, else forwards. Returns the absolute index of -the history element where @var{string} was found, or -1 otherwise. -@end defun - -@defun {HIST_ENTRY *remove_history} (); -Remove history element @var{which} from the history. The removed -element is returned to you so you can free the line, data, +@end deftypefun + +@deftypefun {HIST_ENTRY *} remove_history (int which) +Remove history entry at offset @var{which} from the history. The +removed element is returned so you can free the line, data, and containing structure. -@end defun +@end deftypefun -@defun {void stifle_history} (int max) -Stifle the history list, remembering only @var{max} number of entries. -@end defun +@deftypefun {HIST_ENTRY *} replace_history_entry (int which, char *line, char *data) +Make the history entry at offset @var{which} have @var{line} and @var{data}. +This returns the old entry so you can dispose of the data. In the case +of an invalid @var{which}, a @code{NULL} pointer is returned. +@end deftypefun -@defun {int unstifle_history} (); +@deftypefun void clear_history () +Clear the history list by deleting all the entries. +@end deftypefun + +@deftypefun void stifle_history (int max) +Stifle the history list, remembering only the last @var{max} entries. +@end deftypefun + +@deftypefun int unstifle_history () Stop stifling the history. This returns the previous amount the -history was stifled by. The value is positive if the history was +history was stifled. The value is positive if the history was stifled, negative if it wasn't. -@end defun +@end deftypefun + +@deftypefun int history_is_stifled () +Returns non-zero if the history is stifled, zero if it is not. +@end deftypefun + +@node Information About the History List +@subsection Information About the History List -@defun {int read_history} (char *filename) +These functions return information about the entire history list or +individual list entries. + +@deftypefun {HIST_ENTRY **} history_list () +Return a @code{NULL} terminated array of @code{HIST_ENTRY} which is the +current input history. Element 0 of this list is the beginning of time. +If there is no history, return @code{NULL}. +@end deftypefun + +@deftypefun int where_history () +Returns the offset of the current history element. +@end deftypefun + +@deftypefun {HIST_ENTRY *} current_history () +Return the history entry at the current position, as determined by +@code{where_history ()}. If there is no entry there, return a @code{NULL} +pointer. +@end deftypefun + +@deftypefun {HIST_ENTRY *} history_get (int offset) +Return the history entry at position @var{offset}, starting from +@code{history_base}. If there is no entry there, or if @var{offset} +is greater than the history length, return a @code{NULL} pointer. +@end deftypefun + +@deftypefun int history_total_bytes () +Return the number of bytes that the primary history entries are using. +This function returns the sum of the lengths of all the lines in the +history. +@end deftypefun + +@node Moving Around the History List +@subsection Moving Around the History List + +These functions allow the current index into the history list to be +set or changed. + +@deftypefun int history_set_pos (int pos) +Set the position in the history list to @var{pos}, an absolute index +into the list. +@end deftypefun + +@deftypefun {HIST_ENTRY *} previous_history () +Back up the current history offset to the previous history entry, and +return a pointer to that entry. If there is no previous entry, return +a @code{NULL} pointer. +@end deftypefun + +@deftypefun {HIST_ENTRY *} next_history () +Move the current history offset forward to the next history entry, and +return the a pointer to that entry. If there is no next entry, return +a @code{NULL} pointer. +@end deftypefun + +@node Searching the History List +@subsection Searching the History List +@cindex History Searching + +These functions allow searching of the history list for entries containing +a specific string. Searching may be performed both forward and backward +from the current history position. The search may be @dfn{anchored}, +meaning that the string must match at the beginning of the history entry. +@cindex anchored search + +@deftypefun int history_search (char *string, int direction) +Search the history for @var{string}, starting at the current history +offset. If @var{direction} < 0, then the search is through previous entries, +else through subsequent. If @var{string} is found, then +the current history index is set to that history entry, and the value +returned is the offset in the line of the entry where +@var{string} was found. Otherwise, nothing is changed, and a -1 is +returned. +@end deftypefun + +@deftypefun int history_search_prefix (char *string, int direction) +Search the history for @var{string}, starting at the current history +offset. The search is anchored: matching lines must begin with +@var{string}. If @var{direction} < 0, then the search is through previous +entries, else through subsequent. If @var{string} is found, then the +current history index is set to that entry, and the return value is 0. +Otherwise, nothing is changed, and a -1 is returned. +@end deftypefun + +@deftypefun int history_search_pos (char *string, int direction, int pos) +Search for @var{string} in the history list, starting at @var{pos}, an +absolute index into the list. If @var{direction} is negative, the search +proceeds backward from @var{pos}, otherwise forward. Returns the absolute +index of the history element where @var{string} was found, or -1 otherwise. +@end deftypefun + +@node Managing the History File +@subsection Managing the History File + +The History library can read the history from and write it to a file. +This section documents the functions for managing a history file. + +@deftypefun int read_history (char *filename) Add the contents of @var{filename} to the history list, a line at a time. If @var{filename} is @code{NULL}, then read from @file{~/.history}. Returns 0 if successful, or errno if not. -@end defun +@end deftypefun -@defun {int read_history_range} (char *filename, int from, int to) +@deftypefun int read_history_range (char *filename, int from, int to) Read a range of lines from @var{filename}, adding them to the history list. -Start reading at the @var{from}'th line and end at the @var{to}'th. If +Start reading at line @var{from} and end at @var{to}. If @var{from} is zero, start at the beginning. If @var{to} is less than @var{from}, then read until the end of the file. If @var{filename} is @code{NULL}, then read from @file{~/.history}. Returns 0 if successful, or @code{errno} if not. -@end defun +@end deftypefun -@defun {int write_history} (char *filename) -Append the current history to @var{filename}. If @var{filename} is -@code{NULL}, then append the history list to @file{~/.history}. Values +@deftypefun int write_history (char *filename) +Write the current history to @var{filename}, overwriting @var{filename} +if necessary. If @var{filename} is +@code{NULL}, then write the history list to @file{~/.history}. Values returned are as in @code{read_history ()}. -@end defun +@end deftypefun -@defun {int append_history} (int nelements, char *filename) -Append @var{nelement} entries to @var{filename}. The entries appended -are from the end of the list minus @var{nelements} up to the end of the -list. -@end defun +@deftypefun int append_history (int nelements, char *filename) +Append the last @var{nelements} of the history list to @var{filename}. +@end deftypefun -@defun {HIST_ENTRY *replace_history_entry} () -Make the history entry at @var{which} have @var{line} and @var{data}. -This returns the old entry so you can dispose of the data. In the case -of an invalid @var{which}, a @code{NULL} pointer is returned. -@end defun - -@defun {HIST_ENTRY *current_history} () -Return the history entry at the current position, as determined by -@code{history_offset}. If there is no entry there, return a @code{NULL} -pointer. -@end defun - -@defun {HIST_ENTRY *previous_history} () -Back up @var{history_offset} to the previous history entry, and return a -pointer to that entry. If there is no previous entry, return a -@code{NULL} pointer. -@end defun +@deftypefun int history_truncate_file (char *filename, int nlines) +Truncate the history file @var{filename}, leaving only the last +@var{nlines} lines. +@end deftypefun -@defun {HIST_ENTRY *next_history} () -Move @code{history_offset} forward to the next history entry, and return -the a pointer to that entry. If there is no next entry, return a -@code{NULL} pointer. -@end defun - -@defun {HIST_ENTRY **history_list} () -Return a @code{NULL} terminated array of @code{HIST_ENTRY} which is the -current input history. Element 0 of this list is the beginning of time. -If there is no history, return @code{NULL}. -@end defun +@node History Expansion +@subsection History Expansion -@defun {int history_search} (char *string, int direction) -Search the history for @var{string}, starting at @code{history_offset}. -If @var{direction} < 0, then the search is through previous entries, -else through subsequent. If @var{string} is found, then -@code{current_history ()} is the history entry, and the value of this -function is the offset in the line of that history entry that the -@var{string} was found in. Otherwise, nothing is changed, and a -1 is -returned. -@end defun +These functions implement @code{csh}-like history expansion. -@defun {int history_expand} (char *string, char **output) +@deftypefun int history_expand (char *string, char **output) Expand @var{string}, placing the result into @var{output}, a pointer -to a string. Returns: +to a string (@pxref{History Interaction}). Returns: @table @code @item 0 If no expansions took place (or, if the only change in the text was the de-slashifying of the history expansion -character), +character); @item 1 -if expansions did take place, or +if expansions did take place; @item -1 -if there was an error in expansion. +if there was an error in expansion; +@item 2 +if the returned line should only be displayed, but not executed, +as with the @code{:p} modifier (@pxref{Modifiers}). @end table If an error ocurred in expansion, then @var{output} contains a descriptive error message. -@end defun +@end deftypefun -@defun {char *history_arg_extract} (int first, int last, char *string) +@deftypefun {char *} history_arg_extract (int first, int last, char *string) Extract a string segment consisting of the @var{first} through @var{last} -arguments present in @var{string}. Arguments are broken up as in -the GNU Bash shell. -@end defun - -@defun {int history_total_bytes} (); -Return the number of bytes that the primary history entries are using. -This just adds up the lengths of @code{the_history->lines}. -@end defun +arguments present in @var{string}. Arguments are broken up as in Bash. +@end deftypefun + +@deftypefun {char *} get_history_event (char *string, int *cindex, int qchar) +Returns the text of the history event beginning at @var{string} + +@var{*cindex}. @var{*cindex} is modified to point to after the event +specifier. At function entry, @var{cindex} points to the index into +@var{string} where the history event specification begins. @var{qchar} +is a character that is allowed to end the event specification in addition +to the ``normal'' terminating characters. +@end deftypefun + +@deftypefun {char **} history_tokenize (char *string) +Return an array of tokens parsed out of @var{string}, much as the +shell might. The tokens are split on white space and on the +characters @code{()<>;&|$}, and shell quoting conventions are +obeyed. +@end deftypefun @node History Variables @section History Variables -This section describes the variables in GNU History that are externally -visible. - -@defvar {int history_base} -For convenience only. You set this when interpreting history commands. -It is the logical offset of the first history element. -@end defvar +This section describes the externally visible variables exported by +the GNU History Library. + +@deftypevar int history_base +The logical offset of the first entry in the history list. +@end deftypevar + +@deftypevar int history_length +The number of entries currently stored in the history list. +@end deftypevar + +@deftypevar int max_input_history +The maximum number of history entries. This must be changed using +@code{stifle_history ()}. +@end deftypevar + +@deftypevar char history_expansion_char +The character that starts a history event. The default is @samp{!}. +@end deftypevar + +@deftypevar char history_subst_char +The character that invokes word substitution if found at the start of +a line. The default is @samp{^}. +@end deftypevar + +@deftypevar char history_comment_char +During tokenization, if this character is seen as the first character +of a word, then it and all subsequent characters up to a newline are +ignored, suppressing history expansion for the remainder of the line. +This is disabled by default. +@end deftypevar + +@deftypevar {char *} history_no_expand_chars +The list of characters which inhibit history expansion if found immediately +following @var{history_expansion_char}. The default is whitespace and +@samp{=}. +@end deftypevar + +@deftypevar {char *} history_search_delimiter_chars +The list of additional characters which can delimit a history search +string, in addition to whitespace, @samp{:} and @samp{?} in the case of +a substring search. The default is empty. +@end deftypevar + +@deftypevar int history_quotes_inhibit_expansion +If non-zero, single-quoted words are not scanned for the history expansion +character. The default value is 0. +@end deftypevar + +@deftypevar {Function *} history_inhibit_expansion_function +This should be set to the address of a function that takes two arguments: +a @code{char *} (@var{string}) and an integer index into that string (@var{i}). +It should return a non-zero value if the history expansion starting at +@var{string[i]} should not be performed; zero if the expansion should +be done. +It is intended for use by applications like Bash that use the history +expansion character for additional purposes. +By default, this variable is set to NULL. +@end deftypevar @node History Programming Example @section History Programming Example -The following snippet of code demonstrates simple use of the GNU History -Library. +The following program demonstrates simple use of the GNU History Library. @smallexample main () @{ char line[1024], *t; - int done = 0; + int len, done = 0; line[0] = 0; + using_history (); while (!done) @{ - fprintf (stdout, "history%% "); - t = gets (line); + printf ("history$ "); + fflush (stdout); + t = fgets (line, sizeof (line) - 1, stdin); + if (t && *t) + @{ + len = strlen (t); + if (t[len - 1] == '\n') + t[len - 1] = '\0'; + @} if (!t) strcpy (line, "quit"); @@ -257,37 +460,41 @@ main () char *expansion; int result; - using_history (); - result = history_expand (line, &expansion); - strcpy (line, expansion); - free (expansion); if (result) - fprintf (stderr, "%s\n", line); + fprintf (stderr, "%s\n", expansion); - if (result < 0) - continue; + if (result < 0 || result == 2) + @{ + free (expansion); + continue; + @} - add_history (line); + add_history (expansion); + strncpy (line, expansion, sizeof (line) - 1); + free (expansion); @} - if (strcmp (line, "quit") == 0) done = 1; - if (strcmp (line, "save") == 0) write_history (0); - if (strcmp (line, "read") == 0) read_history (0); - if (strcmp (line, "list") == 0) + if (strcmp (line, "quit") == 0) + done = 1; + else if (strcmp (line, "save") == 0) + write_history ("history_file"); + else if (strcmp (line, "read") == 0) + read_history ("history_file"); + else if (strcmp (line, "list") == 0) @{ - register HIST_ENTRY **the_list = history_list (); + register HIST_ENTRY **the_list; register int i; + the_list = history_list (); if (the_list) for (i = 0; the_list[i]; i++) - fprintf (stdout, "%d: %s\n", - i + history_base, the_list[i]->line); + printf ("%d: %s\n", i + history_base, the_list[i]->line); @} - if (strncmp (line, "delete", strlen ("delete")) == 0) + else if (strncmp (line, "delete", 6) == 0) @{ int which; - if ((sscanf (line + strlen ("delete"), "%d", &which)) == 1) + if ((sscanf (line + 6, "%d", &which)) == 1) @{ HIST_ENTRY *entry = remove_history (which); if (!entry) @@ -306,6 +513,3 @@ main () @} @} @end smallexample - - - diff --git a/readline/doc/hsuser.texinfo b/readline/doc/hsuser.texinfo index cda0a688c74..76cb63b1eee 100644 --- a/readline/doc/hsuser.texinfo +++ b/readline/doc/hsuser.texinfo @@ -1,8 +1,8 @@ @ignore This file documents the user interface to the GNU History library. -Copyright (C) 1988, 1991 Free Software Foundation, Inc. -Authored by Brian Fox. +Copyright (C) 1988, 1991, 1996 Free Software Foundation, Inc. +Authored by Brian Fox and Chet Ramey. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on @@ -26,36 +26,236 @@ into another language, under the above conditions for modified versions. @node Using History Interactively @chapter Using History Interactively +@ifset BashFeatures +This chapter describes how to use the GNU History Library interactively, +from a user's standpoint. It should be considered a user's guide. For +information on using the GNU History Library in other programs, +see the GNU Readline Library Manual. +@end ifset +@ifclear BashFeatures This chapter describes how to use the GNU History Library interactively, from a user's standpoint. It should be considered a user's guide. For information on using the GNU History Library in your own programs, @pxref{Programming with GNU History}. +@end ifclear +@ifset BashFeatures +@menu +* Bash History Facilities:: How Bash lets you manipulate your command + history. +* Bash History Builtins:: The Bash builtin commands that manipulate + the command history. +* History Interaction:: What it feels like using History as a user. +@end menu +@end ifset +@ifclear BashFeatures @menu * History Interaction:: What it feels like using History as a user. @end menu +@end ifclear + +@ifset BashFeatures +@node Bash History Facilities +@section Bash History Facilities +@cindex command history +@cindex history list + +When the @samp{-o history} option to the @code{set} builtin +is enabled (@pxref{The Set Builtin}), +the shell provides access to the @var{command history}, +the list of commands previously typed. The text of the last +@code{HISTSIZE} +commands (default 500) is saved in a history list. The shell +stores each command in the history list prior to parameter and +variable expansion +but after history expansion is performed, subject to the +values of the shell variables +@code{HISTIGNORE} and @code{HISTCONTROL}. +When the shell starts up, the history is initialized from the +file named by the @code{HISTFILE} variable (default @file{~/.bash_history}). +@code{HISTFILE} is truncated, if necessary, to contain no more than +the number of lines specified by the value of the @code{HISTFILESIZE} +variable. When an interactive shell exits, the last +@code{HISTSIZE} lines are copied from the history list to @code{HISTFILE}. +If the @code{histappend} shell option is set (@pxref{Bash Builtins}), +the lines are appended to the history file, +otherwise the history file is overwritten. +If @code{HISTFILE} +is unset, or if the history file is unwritable, the history is +not saved. After saving the history, the history file is truncated +to contain no more than @code{$HISTFILESIZE} +lines. If @code{HISTFILESIZE} is not set, no truncation is performed. + +The builtin command @code{fc} may be used to list or edit and re-execute +a portion of the history list. +The @code{history} builtin can be used to display or modify the history +list and manipulate the history file. +When using the command-line editing, search commands +are available in each editing mode that provide access to the +history list. + +The shell allows control over which commands are saved on the history +list. The @code{HISTCONTROL} and @code{HISTIGNORE} +variables may be set to cause the shell to save only a subset of the +commands entered. +The @code{cmdhist} +shell option, if enabled, causes the shell to attempt to save each +line of a multi-line command in the same history entry, adding +semicolons where necessary to preserve syntactic correctness. +The @code{lithist} +shell option causes the shell to save the command with embedded newlines +instead of semicolons. +@xref{Bash Builtins}, for a description of @code{shopt}. + +@node Bash History Builtins +@section Bash History Builtins +@cindex history builtins + +Bash provides two builtin commands that allow you to manipulate the +history list and history file. + +@table @code + +@item fc +@btindex fc +@example +@code{fc [-e @var{ename}] [-nlr] [@var{first}] [@var{last}]} +@code{fc -s [@var{pat}=@var{rep}] [@var{command}]} +@end example + +Fix Command. In the first form, a range of commands from @var{first} to +@var{last} is selected from the history list. Both @var{first} and +@var{last} may be specified as a string (to locate the most recent +command beginning with that string) or as a number (an index into the +history list, where a negative number is used as an offset from the +current command number). If @var{last} is not specified it is set to +@var{first}. If @var{first} is not specified it is set to the previous +command for editing and @minus{}16 for listing. If the @samp{-l} flag is +given, the commands are listed on standard output. The @samp{-n} flag +suppresses the command numbers when listing. The @samp{-r} flag +reverses the order of the listing. Otherwise, the editor given by +@var{ename} is invoked on a file containing those commands. If +@var{ename} is not given, the value of the following variable expansion +is used: @code{$@{FCEDIT:-$@{EDITOR:-vi@}@}}. This says to use the +value of the @code{FCEDIT} variable if set, or the value of the +@code{EDITOR} variable if that is set, or @code{vi} if neither is set. +When editing is complete, the edited commands are echoed and executed. + +In the second form, @var{command} is re-executed after each instance +of @var{pat} in the selected command is replaced by @var{rep}. + +A useful alias to use with the @code{fc} command is @code{r='fc -s'}, so +that typing @samp{r cc} runs the last command beginning with @code{cc} +and typing @samp{r} re-executes the last command (@pxref{Aliases}). + +@item history +@btindex history +@example +history [-c] [@var{n}] +history [-anrw] [@var{filename}] +history -ps @var{arg} +@end example + +Display the history list with line numbers. Lines prefixed with +with a @samp{*} have been modified. An argument of @var{n} says +to list only the last @var{n} lines. Options, if supplied, have +the following meanings: + +@table @code +@item -w +Write out the current history to the history file. + +@item -r +Read the current history file and append its contents to +the history list. + +@item -a +Append the new +history lines (history lines entered since the beginning of the +current Bash session) to the history file. + +@item -n +Append the history lines not already read from the history file +to the current history list. These are lines appended to the history +file since the beginning of the current Bash session. + +@item -c +Clear the history list. This may be combined +with the other options to replace the history list completely. + +@item -s +The @var{arg}s are added to the end of +the history list as a single entry. + +@item -p +Perform history substitution on the @var{arg}s and display the result +on the standard output, without storing the results in the history list. +@end table + +When the @samp{-w}, @samp{-r}, @samp{-a}, or @samp{-n} option is +used, if @var{filename} +is given, then it is used as the history file. If not, then +the value of the @code{HISTFILE} variable is used. + +@end table +@end ifset @node History Interaction -@section History Interaction -@cindex expansion +@section History Expansion +@cindex history expansion The History library provides a history expansion feature that is similar -to the history expansion in Csh. The following text describes the sytax -that you use to manipulate the history information. +to the history expansion provided by @code{csh}. This section +describes the syntax used to manipulate the history information. + +History expansions introduce words from the history list into +the input stream, making it easy to repeat commands, insert the +arguments to a previous command into the current input line, or +fix errors in previous commands quickly. History expansion takes place in two parts. The first is to determine -which line from the previous history should be used during substitution. +which line from the history list should be used during substitution. The second is to select portions of that line for inclusion into the -current one. The line selected from the previous history is called the +current one. The line selected from the history is called the @dfn{event}, and the portions of that line that are acted upon are -called @dfn{words}. The line is broken into words in the same fashion -that the Bash shell does, so that several English (or Unix) words -surrounded by quotes are considered as one word. +called @dfn{words}. Various @dfn{modifiers} are available to manipulate +the selected words. The line is broken into words in the same fashion +that Bash does, so that several words +surrounded by quotes are considered one word. +History expansions are introduced by the appearance of the +history expansion character, which is @samp{!} by default. +@ifset BashFeatures +Only @samp{\} and @samp{'} may be used to escape the history expansion +character. +@end ifset + +@ifset BashFeatures +Several shell options settable with the @code{shopt} +builtin (@pxref{Bash Builtins}) may be used to tailor +the behavior of history expansion. If the +@code{histverify} shell option is enabled, and Readline +is being used, history substitutions are not immediately passed to +the shell parser. +Instead, the expanded line is reloaded into the Readline +editing buffer for further modification. +If Readline is being used, and the @code{histreedit} +shell option is enabled, a failed history expansion will be +reloaded into the Readline editing buffer for correction. +The @samp{-p} option to the @code{history} builtin command +may be used to see what a history expansion will do before using it. +The @samp{-s} option to the @code{history} builtin may be used to +add commands to the end of the history list without actually executing +them, so that they are available for subsequent recall. +This is most useful in conjunction with Readline. + +The shell allows control of the various characters used by the +history expansion mechanism with the @code{histchars} variable. +@end ifset @menu * Event Designators:: How to specify which history line to use. * Word Designators:: Specifying which words are of interest. -* Modifiers:: Modifying the results of susbstitution. +* Modifiers:: Modifying the results of substitution. @end menu @node Event Designators @@ -64,90 +264,135 @@ surrounded by quotes are considered as one word. An event designator is a reference to a command line entry in the history list. +@cindex history events @table @asis @item @code{!} -Start a history subsititution, except when followed by a space, tab, or -the end of the line... @key{=} or @key{(}. +Start a history substitution, except when followed by a space, tab, +the end of the line, @samp{=} or @samp{(}. -@item @code{!!} -Refer to the previous command. This is a synonym for @code{!-1}. - -@item @code{!n} +@item @code{!@var{n}} Refer to command line @var{n}. -@item @code{!-n} -Refer to the command line @var{n} lines back. +@item @code{!-@var{n}} +Refer to the command @var{n} lines back. -@item @code{!string} +@item @code{!!} +Refer to the previous command. This is a synonym for @samp{!-1}. + +@item @code{!@var{string}} Refer to the most recent command starting with @var{string}. -@item @code{!?string}[@code{?}] -Refer to the most recent command containing @var{string}. +@item @code{!?@var{string}[?]} +Refer to the most recent command containing @var{string}. The trailing +@samp{?} may be omitted if the @var{string} is followed immediately by +a newline. + +@item @code{^@var{string1}^@var{string2}^} +Quick Substitution. Repeat the last command, replacing @var{string1} +with @var{string2}. Equivalent to +@code{!!:s/@var{string1}/@var{string2}/}. + +@item @code{!#} +The entire command line typed so far. @end table @node Word Designators @subsection Word Designators -A @key{:} separates the event specification from the word designator. It -can be omitted if the word designator begins with a @key{^}, @key{$}, -@key{*} or @key{%}. Words are numbered from the beginning of the line, -with the first word being denoted by a 0 (zero). +Word designators are used to select desired words from the event. +A @samp{:} separates the event specification from the word designator. It +may be omitted if the word designator begins with a @samp{^}, @samp{$}, +@samp{*}, @samp{-}, or @samp{%}. Words are numbered from the beginning +of the line, with the first word being denoted by 0 (zero). Words are +inserted into the current line separated by single spaces. @table @code @item 0 (zero) -The zero'th word. For many applications, this is the command word. +The @code{0}th word. For many applications, this is the command word. -@item n -The @var{n}'th word. +@item @var{n} +The @var{n}th word. @item ^ -The first argument. that is, word 1. +The first argument; that is, word 1. @item $ The last argument. @item % -The word matched by the most recent @code{?string?} search. +The word matched by the most recent @samp{?@var{string}?} search. -@item x-y -A range of words; @code{-@var{y}} Abbreviates @code{0-@var{y}}. +@item @var{x}-@var{y} +A range of words; @samp{-@var{y}} abbreviates @samp{0-@var{y}}. @item * -All of the words, excepting the zero'th. This is a synonym for @code{1-$}. -It is not an error to use @key{*} if there is just one word in the event. -The empty string is returned in that case. +All of the words, except the @code{0}th. This is a synonym for @samp{1-$}. +It is not an error to use @samp{*} if there is just one word in the event; +the empty string is returned in that case. + +@item @var{x}* +Abbreviates @samp{@var{x}-$} + +@item @var{x}- +Abbreviates @samp{@var{x}-$} like @samp{@var{x}*}, but omits the last word. @end table +If a word designator is supplied without an event specification, the +previous command is used as the event. + @node Modifiers @subsection Modifiers After the optional word designator, you can add a sequence of one or more -of the following modifiers, each preceded by a @key{:}. +of the following modifiers, each preceded by a @samp{:}. @table @code -@item # -The entire command line typed so far. This means the current command, -not the previous command, so it really isn't a word designator, and doesn't -belong in this section. - @item h Remove a trailing pathname component, leaving only the head. +@item t +Remove all leading pathname components, leaving the tail. + @item r -Remove a trailing suffix of the form @samp{.}@var{suffix}, leaving the basename. +Remove a trailing suffix of the form @samp{.@var{suffix}}, leaving +the basename. @item e -Remove all but the suffix. - -@item t -Remove all leading pathname components, leaving the tail. +Remove all but the trailing suffix. @item p Print the new command but do not execute it. + +@ifset BashFeatures +@item q +Quote the substituted words, escaping further substitutions. + +@item x +Quote the substituted words as with @samp{q}, +but break into words at spaces, tabs, and newlines. +@end ifset + +@item s/@var{old}/@var{new}/ +Substitute @var{new} for the first occurrence of @var{old} in the +event line. Any delimiter may be used in place of @samp{/}. +The delimiter may be quoted in @var{old} and @var{new} +with a single backslash. If @samp{&} appears in @var{new}, +it is replaced by @var{old}. A single backslash will quote +the @samp{&}. The final delimiter is optional if it is the last +character on the input line. + +@item & +Repeat the previous substitution. + +@item g +Cause changes to be applied over the entire event line. Used in +conjunction with @samp{s}, as in @code{gs/@var{old}/@var{new}/}, +or with @samp{&}. + @end table diff --git a/readline/doc/readline.0 b/readline/doc/readline.0 new file mode 100644 index 00000000000..c925d529cb5 --- /dev/null +++ b/readline/doc/readline.0 @@ -0,0 +1,1122 @@ + + + +READLINE(3) READLINE(3) + + +NNAAMMEE + readline - get a line from a user with editing + +SSYYNNOOPPSSIISS + ##iinncclluuddee <Readline Library
Version 2.1.+
+ +
+@dircategory Libraries +@direntry +* Readline: (readline). The GNU readline library API + +
+ ++This document describes the GNU Readline Library, a utility which aids +in the consistency of user interface across discrete programs that need +to provide a command line interface. + +
+
+Published by the Free Software Foundation
+675 Massachusetts Avenue,
+Cambridge, MA 02139 USA
+
+
+Permission is granted to make and distribute verbatim copies of +this manual provided the copyright notice and this permission notice +are preserved on all copies. + +
++Permission is granted to copy and distribute modified versions of this +manual under the conditions for verbatim copying, provided that the entire +resulting derived work is distributed under the terms of a permission +notice identical to this one. + +
++Permission is granted to copy and distribute translations of this manual +into another language, under the above conditions for modified versions, +except that this permission notice may be stated in a translation approved +by the Foundation. + +
++Copyright (C) 1989, 1991 Free Software Foundation, Inc. + +
+ + + ++This chapter describes the basic features of the GNU +command line editing interface. + +
+ + + ++The following paragraphs describe the notation used to represent +keystrokes. + +
++The text C-k is read as `Control-K' and describes the character +produced when the k key is pressed while the Control key +is depressed. + +
++The text M-k is read as `Meta-K' and describes the character +produced when the meta key (if you have one) is depressed, and the k +key is pressed. If you do not have a meta key, the identical keystroke +can be generated by typing ESC first, and then typing k. +Either process is known as metafying the k key. + +
++The text M-C-k is read as `Meta-Control-k' and describes the +character produced by metafying C-k. + +
++In addition, several keys have their own names. Specifically, +DEL, ESC, LFD, SPC, RET, and TAB all +stand for themselves when seen in this text, or in an init file +(@xref{Readline Init File}). + +
+ + ++Often during an interactive session you type in a long line of text, +only to notice that the first word on the line is misspelled. The +Readline library gives you a set of commands for manipulating the text +as you type it in, allowing you to just fix your typo, and not forcing +you to retype the majority of the line. Using these editing commands, +you move the cursor to the place that needs correction, and delete or +insert the text of the corrections. Then, when you are satisfied with +the line, you simply press RETURN. You do not have to be at the +end of the line to press RETURN; the entire line is accepted +regardless of the location of the cursor within the line. + +
+ + + ++There are only a few basic constructs allowed in the +Readline init file. Blank lines are ignored. +Lines beginning with a `#' are comments. +Lines beginning with a `$' indicate conditional +constructs (see section Conditional Init Constructs). Other lines +denote variable settings and key bindings. + +
+set
command within the init file. Here is how to
+change from the default Emacs-like key binding to use
+vi
line editing commands:
+
+
++set editing-mode vi ++ +A great deal of run-time behavior is changeable with the following +variables. + +
bell-style
+comment-begin
+insert-comment
command is executed. The default value
+is "#"
.
+
+completion-ignore-case
+completion-query-items
+100
.
+
+convert-meta
+disable-completion
+self-insert
. The default is `off'.
+
+editing-mode
+editing-mode
variable controls which default set of
+key bindings is used. By default, Readline starts up in Emacs editing
+mode, where the keystrokes are most similar to Emacs. This variable can be
+set to either `emacs' or `vi'.
+
+enable-keypad
+expand-tilde
+horizontal-scroll-mode
+keymap
+keymap
names are
+emacs
,
+emacs-standard
,
+emacs-meta
,
+emacs-ctlx
,
+vi
,
+vi-command
, and
+vi-insert
.
+vi
is equivalent to vi-command
; emacs
is
+equivalent to emacs-standard
. The default value is emacs
.
+The value of the editing-mode
variable also affects the
+default keymap.
+
+mark-directories
+mark-modified-lines
+input-meta
+meta-flag
is a
+synonym for this variable.
+
+output-meta
+print-completions-horizontally
+show-all-if-ambiguous
+visible-stats
++Control-u: universal-argument +Meta-Rubout: backward-kill-word +Control-o: "> output" ++ +In the above example, C-u is bound to the function +
universal-argument
, and C-o is bound to run the macro
+expressed on the right hand side (that is, to insert the text
+`> output' into the line).
+
++"\C-u": universal-argument +"\C-x\C-r": re-read-init-file +"\e[11~": "Function Key 1" ++ +In the above example, C-u is bound to the function +
universal-argument
(just as it was in the first example),
+`C-x C-r' is bound to the function re-read-init-file
,
+and `ESC [ 1 1 ~' is bound to insert
+the text `Function Key 1'.
+
+\C-
+\M-
+\e
+\\
+\"
+\'
+\a
+\b
+\d
+\f
+\n
+\r
+\t
+\v
+\nnn
+\xnnn
++"\C-x\\": "\\" ++ +
+Readline implements a facility similar in spirit to the conditional +compilation features of the C preprocessor which allows key +bindings and variable settings to be performed as the result +of tests. There are four parser directives used. + +
+$if
+$if
construct allows bindings to be made based on the
+editing mode, the terminal being used, or the application using
+Readline. The text of the test extends to the end of the line;
+no characters are required to isolate it.
+
+mode
+mode=
form of the $if
directive is used to test
+whether Readline is in emacs
or vi
mode.
+This may be used in conjunction
+with the `set keymap' command, for instance, to set bindings in
+the emacs-standard
and emacs-ctlx
keymaps only if
+Readline is starting out in emacs
mode.
+
+term
+term=
form may be used to include terminal-specific
+key bindings, perhaps to bind the key sequences output by the
+terminal's function keys. The word on the right side of the
+`=' is tested against both the full name of the terminal and
+the portion of the terminal name before the first `-'. This
+allows sun
to match both sun
and sun-cmd
,
+for instance.
+
+application
++$if Bash +# Quote the current or previous word +"\C-xq": "\eb\"\ef\"" +$endif ++ +
$endif
+$if
command.
+
+$else
+$if
directive are executed if
+the test fails.
+
+$include
++$include /etc/inputrc ++ +
+Here is an example of an inputrc file. This illustrates key +binding, variable assignment, and conditional syntax. + +
+ ++# This file controls the behaviour of line input editing for +# programs that use the Gnu Readline library. Existing programs +# include FTP, Bash, and Gdb. +# +# You can re-read the inputrc file with C-x C-r. +# Lines beginning with '#' are comments. +# +# First, include any systemwide bindings and variable assignments from +# /etc/Inputrc +$include /etc/Inputrc + +# +# Set various bindings for emacs mode. + +set editing-mode emacs + +$if mode=emacs + +Meta-Control-h: backward-kill-word Text after the function name is ignored + +# +# Arrow keys in keypad mode +# +#"\M-OD": backward-char +#"\M-OC": forward-char +#"\M-OA": previous-history +#"\M-OB": next-history +# +# Arrow keys in ANSI mode +# +"\M-[D": backward-char +"\M-[C": forward-char +"\M-[A": previous-history +"\M-[B": next-history +# +# Arrow keys in 8 bit keypad mode +# +#"\M-\C-OD": backward-char +#"\M-\C-OC": forward-char +#"\M-\C-OA": previous-history +#"\M-\C-OB": next-history +# +# Arrow keys in 8 bit ANSI mode +# +#"\M-\C-[D": backward-char +#"\M-\C-[C": forward-char +#"\M-\C-[A": previous-history +#"\M-\C-[B": next-history + +C-q: quoted-insert + +$endif + +# An old-style binding. This happens to be the default. +TAB: complete + +# Macros that are convenient for shell interaction +$if Bash +# edit the path +"\C-xp": "PATH=${PATH}\e\C-e\C-a\ef\C-f" +# prepare to type a quoted word -- insert open and close double quotes +# and move to just after the open quote +"\C-x\"": "\"\"\C-b" +# insert a backslash (testing backslash escapes in sequences and macros) +"\C-x\\": "\\" +# Quote the current or previous word +"\C-xq": "\eb\"\ef\"" +# Add a binding to refresh the line, which is unbound +"\C-xr": redraw-current-line +# Edit variable on current line. +"\M-\C-v": "\C-a\C-k$\C-y\M-\C-e\C-a\C-y=" +$endif + +# use a visible bell if one is available +set bell-style visible + +# don't strip characters to 7 bits when reading +set input-meta on + +# allow iso-latin1 characters to be inserted rather than converted to +# prefix-meta sequences +set convert-meta off + +# display characters with the eighth bit set directly rather than +# as meta-prefixed characters +set output-meta on + +# if there are more than 150 possible completions for a word, ask the +# user if he wants to see all of them +set completion-query-items 150 + +# For FTP +$if Ftp +"\C-xg": "get \M-?" +"\C-xt": "put \M-?" +"\M-.": yank-last-arg +$endif ++ + + +
+This section describes Readline commands that may be bound to key +sequences. + +
+ + +beginning-of-line (C-a)
+end-of-line (C-e)
+forward-char (C-f)
+backward-char (C-b)
+forward-word (M-f)
+backward-word (M-b)
+clear-screen (C-l)
+redraw-current-line ()
+accept-line (Newline, Return)
+previous-history (C-p)
+next-history (C-n)
+beginning-of-history (M-<)
+end-of-history (M->)
+reverse-search-history (C-r)
+forward-search-history (C-s)
+non-incremental-reverse-search-history (M-p)
+non-incremental-forward-search-history (M-n)
+history-search-forward ()
+history-search-backward ()
+yank-nth-arg (M-C-y)
+yank-last-arg (M-., M-_)
+yank-nth-arg
.
+Successive calls to yank-last-arg
move back through the history
+list, inserting the last argument of each line in turn.
+
+delete-char (C-d)
+delete-char
, then
+return EOF
.
+
+backward-delete-char (Rubout)
+quoted-insert (C-q, C-v)
+tab-insert (M-TAB)
+self-insert (a, b, A, 1, !, ...)
+transpose-chars (C-t)
+transpose-words (M-t)
+upcase-word (M-u)
+downcase-word (M-l)
+capitalize-word (M-c)
+kill-line (C-k)
+backward-kill-line (C-x Rubout)
+unix-line-discard (C-u)
+kill-whole-line ()
+kill-word (M-d)
+forward-word
.
+
+backward-kill-word (M-DEL)
+backward-word
.
+
+unix-word-rubout (C-w)
+delete-horizontal-space ()
+kill-region ()
+copy-region-as-kill ()
+copy-backward-word ()
+backward-word
.
+By default, this command is unbound.
+
+copy-forward-word ()
+forward-word
.
+By default, this command is unbound.
+
+yank (C-y)
+yank-pop (M-y)
+digit-argument (M-0, M-1, ... M--)
+universal-argument ()
+universal-argument
+again ends the numeric argument, but is otherwise ignored.
+As a special case, if this command is immediately followed by a
+character that is neither a digit or minus sign, the argument count
+for the next command is multiplied by four.
+The argument count is initially one, so executing this function the
+first time makes the argument count four, a second time makes the
+argument count sixteen, and so on.
+By default, this is not bound to a key.
+complete (TAB)
+possible-completions (M-?)
+insert-completions (M-*)
+possible-completions
.
+
+menu-complete ()
+complete
, but replaces the word to be completed
+with a single match from the list of possible completions.
+Repeated execution of menu-complete
steps through the list
+of possible completions, inserting each match in turn.
+At the end of the list of completions, the bell is rung and the
+original text is restored.
+An argument of n moves n positions forward in the list
+of matches; a negative argument may be used to move backward
+through the list.
+This command is intended to be bound to TAB
, but is unbound
+by default.
+
+start-kbd-macro (C-x ()
+end-kbd-macro (C-x ))
+call-last-kbd-macro (C-x e)
+re-read-init-file (C-x C-r)
+abort (C-g)
+bell-style
).
+
+do-uppercase-version (M-a, M-b, M-x, ...)
+prefix-meta (ESC)
+undo (C-_, C-x C-u)
+revert-line (M-r)
+undo
+command enough times to get back to the beginning.
+
+tilde-expand (M-~)
+set-mark (C-@)
+exchange-point-and-mark (C-x C-x)
+character-search (C-])
+character-search-backward (M-C-])
+insert-comment (M-#)
+comment-begin
+variable is inserted at the beginning of the current line,
+and the line is accepted as if a newline had been typed.
+
+dump-functions ()
+dump-variables ()
+dump-macros ()
+
+While the Readline library does not have a full set of vi
+editing functions, it does contain enough to allow simple editing
+of the line. The Readline vi
mode behaves as specified in
+the POSIX 1003.2 standard.
+
+
+In order to switch interactively between emacs
and vi
+editing modes, use the command M-C-j (toggle-editing-mode).
+The Readline default is emacs
mode.
+
+
+When you enter a line in vi
mode, you are already placed in
+`insertion' mode, as if you had typed an `i'. Pressing ESC
+switches you into `command' mode, where you can edit the text of the
+line with the standard vi
movement keys, move to previous
+history lines with `k' and subsequent lines with `j', and
+so forth.
+
+
+This chapter describes the interface between the GNU Readline Library and +other programs. If you are a programmer, and you wish to include the +features found in GNU Readline +such as completion, line editing, and interactive history manipulation +in your own programs, this section is for you. + +
+ + + +
+Many programs provide a command line interface, such as mail
,
+ftp
, and sh
. For such programs, the default behaviour of
+Readline is sufficient. This section describes how to use Readline in
+the simplest way possible, perhaps to replace calls in your code to
+gets()
or fgets ()
.
+
+
+
+
+The function readline ()
prints a prompt and then reads and returns
+a single line of text from the user. The line readline
+returns is allocated with malloc ()
; you should free ()
+the line when you are done with it. The declaration for readline
+in ANSI C is
+
+
+char *readline (char *prompt);
+
+
++So, one might say + +
+char *line = readline ("Enter a line: ");
+
+
++in order to read a line of text from the user. +The line returned has the final newline removed, so only the +text remains. + +
+
+If readline
encounters an EOF
while reading the line, and the
+line is empty at that point, then (char *)NULL
is returned.
+Otherwise, the line is ended just as if a newline had been typed.
+
+
+If you want the user to be able to get at the line later, (with
+C-p for example), you must call add_history ()
to save the
+line away in a history list of such lines.
+
+
+add_history (line)
;
+
+
++For full details on the GNU History Library, see the associated manual. + +
+
+It is preferable to avoid saving empty lines on the history list, since
+users rarely have a burning need to reuse a blank line. Here is
+a function which usefully replaces the standard gets ()
library
+function, and has the advantage of no static buffer to overflow:
+
+
+/* A static variable for holding the line. */ +static char *line_read = (char *)NULL; + +/* Read a string, and return a pointer to it. Returns NULL on EOF. */ +char * +rl_gets () +{ + /* If the buffer has already been allocated, return the memory + to the free pool. */ + if (line_read) + { + free (line_read); + line_read = (char *)NULL; + } + + /* Get a line from the user. */ + line_read = readline (""); + + /* If the line has any text in it, save it on the history. */ + if (line_read && *line_read) + add_history (line_read); + + return (line_read); +} ++ +
+This function gives the user the default behaviour of TAB
+completion: completion on file names. If you do not want Readline to
+complete on filenames, you can change the binding of the TAB key
+with rl_bind_key ()
.
+
+
+int rl_bind_key (int key, int (*function)());
+
+
+
+rl_bind_key ()
takes two arguments: key is the character that
+you want to bind, and function is the address of the function to
+call when key is pressed. Binding TAB to rl_insert ()
+makes TAB insert itself.
+rl_bind_key ()
returns non-zero if key is not a valid
+ASCII character code (between 0 and 255).
+
+
+Thus, to disable the default TAB behavior, the following suffices: + +
+rl_bind_key ('\t', rl_insert);
+
+
+
+This code should be executed once at the start of your program; you
+might write a function called initialize_readline ()
which
+performs this and other desired initializations, such as installing
+custom completers (see section Custom Completers).
+
+
+Readline provides many functions for manipulating the text of +the line, but it isn't possible to anticipate the needs of all +programs. This section describes the various functions and variables +defined within the Readline library which allow a user program to add +customized functionality to Readline. + +
+ + + +
+For readabilty, we declare a new type of object, called
+Function. A Function
is a C function which
+returns an int
. The type declaration for Function
is:
+
+
+typedef int Function ();
+
+
+The reason for declaring this new type is to make it easier to write +code describing pointers to C functions. Let us say we had a variable +called func which was a pointer to a function. Instead of the +classic C declaration + +
+
+int (*)()func;
+
+
+we may write + +
+
+Function *func;
+
+
+Similarly, there are + +
+ ++typedef void VFunction (); +typedef char *CPFunction (); and +typedef char **CPPFunction (); ++ +
+for functions returning no value, pointer to char
, and
+pointer to pointer to char
, respectively.
+
+
+In order to write new functions for Readline, you need to know the +calling conventions for keyboard-invoked functions, and the names of the +variables that describe the current state of the line read so far. + +
+
+The calling sequence for a command foo
looks like
+
+
+foo (int count, int key)
+
+
++where count is the numeric argument (or 1 if defaulted) and +key is the key that invoked this function. + +
++It is completely up to the function as to what should be done with the +numeric argument. Some functions use it as a repeat count, some +as a flag, and others to choose alternate behavior (refreshing the current +line as opposed to refreshing the screen, for example). Some choose to +ignore it. In general, if a +function uses the numeric argument as a repeat count, it should be able +to do something useful with both negative and positive arguments. +At the very least, it should be aware that it can be passed a +negative argument. + +
+ + ++These variables are available to function writers. + +
++
+
rl_line_buffer
+(the point).
++
rl_line_buffer
. When
+rl_point
is at the end of the line, rl_point
and
+rl_end
are equal.
++
+
+
+
readline ()
, and should not be assigned to directly.
++
+
+
+
+
+
readline
prints the first prompt.
++
+
readline
will call indirectly through this pointer
+to get a character from the input stream. By default, it is set to
+rl_getc
, the default readline
character input function
+(see section Utility Functions).
++
readline
will call indirectly through this pointer
+to update the display with the current contents of the editing buffer.
+By default, it is set to rl_redisplay
, the default readline
+redisplay function (see section Redisplay).
++
+
+The user can dynamically change the bindings of keys while using +Readline. This is done by representing the function with a descriptive +name. The user is able to type the descriptive name when referring to +the function. Thus, in an init file, one might find + +
+ ++Meta-Rubout: backward-kill-word ++ +
+This binds the keystroke Meta-Rubout to the function
+descriptively named backward-kill-word
. You, as the
+programmer, should bind the functions you write to descriptive names as
+well. Readline provides a function for doing that:
+
+
+
rl_bind_key ()
.
++Using this function alone is sufficient for most applications. It is +the recommended way to add a few functions to the default functions that +Readline has built in. If you need to do something other +than adding a function to Readline, you may need to use the +underlying functions described below. + +
+ + ++Key bindings take place on a keymap. The keymap is the +association between the keys that the user types and the functions that +get run. You can make your own keymaps, copy existing keymaps, and tell +Readline which keymap to use. + +
++
malloc ()
; you should free ()
it when you are done.
++
+
+
+Readline has several internal keymaps. These functions allow you to +change which keymap is active. + +
++
+
+
set keymap
inputrc line (@xref{Readline Init File}).
++
set keymap
inputrc line (@xref{Readline Init File}).
+
+You associate keys with functions through the keymap. Readline has
+several internal keymaps: emacs_standard_keymap
,
+emacs_meta_keymap
, emacs_ctlx_keymap
,
+vi_movement_keymap
, and vi_insertion_keymap
.
+emacs_standard_keymap
is the default, and the examples in
+this manual assume that.
+
+
+These functions manage key bindings. + +
++
+
+
+
+
+
+
ISFUNC
), a macro
+(ISMACR
), or a keymap (ISKMAP
). This makes new keymaps as
+necessary. The initial keymap in which to do bindings is map.
++
inputrc
file and
+perform any key bindings and variable assignments found
+(@xref{Readline Init File}).
++
+These functions allow you to find out what keys invoke named functions +and the functions invoked by a particular key sequence. + +
++
+
ISFUNC
,
+ISKMAP
, or ISMACR
).
++
+
+
rl_outstream
. If readable is non-zero,
+the list is formatted in such a way that it can be made part of an
+inputrc
file and re-read.
++
rl_outstream
.
++Supporting the undo command is a painless thing, and makes your +functions much more useful. It is certainly easy to try +something if you know you can undo it. I could use an undo function for +the stock market. + +
+
+If your function simply inserts text once, or deletes text once, and
+uses rl_insert_text ()
or rl_delete_text ()
to do it, then
+undoing is already done for you automatically.
+
+
+If you do multiple insertions or multiple deletions, or any combination
+of these operations, you should group them together into one operation.
+This is done with rl_begin_undo_group ()
and
+rl_end_undo_group ()
.
+
+
+The types of events that can be undone are: + +
+ ++enum undo_code { UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END }; ++ +
+Notice that UNDO_DELETE
means to insert some text, and
+UNDO_INSERT
means to delete some text. That is, the undo code
+tells undo what to undo, not how to undo it. UNDO_BEGIN
and
+UNDO_END
are tags added by rl_begin_undo_group ()
and
+rl_end_undo_group ()
.
+
+
+
rl_insert_text ()
and
+rl_delete_text ()
, but could be the result of calls to
+rl_add_undo ()
.
++
rl_begin_undo_group
+()
. There should be one call to rl_end_undo_group ()
+for each call to rl_begin_undo_group ()
.
++
+
+
0
if there was
+nothing to undo, non-zero if something was undone.
+
+Finally, if you neither insert nor delete text, but directly modify the
+existing text (e.g., change its case), call rl_modifying ()
+once, just before you modify the text. You must supply the indices of
+the text range that you are going to modify.
+
+
+
+
rl_line_buffer
.
++
+
+
+
printf
. The
+resulting string is displayed in the echo area. The echo area
+is also used to display numeric arguments and search strings.
++
+
+
+
+
+
rl_stuff_char ()
, macros, and characters read from the keyboard.
++
+
rl_read_key ()
.
++
rl_line_buffer
has enough space to hold len
+characters, possibly reallocating it if necessary.
++
+
vt100
).
++
+
+
bell-style
.
+
+The following are implemented as macros, defined in chartypes.h
.
+
+
+
+
+
+
+
+
+An alternate interface is available to plain readline()
. Some
+applications need to interleave keyboard I/O with file, device, or
+window system I/O, typically by using a main loop to select()
+on various file descriptors. To accomodate this need, readline can
+also be invoked as a `callback' function from an event loop. There
+are functions available to make this easy.
+
+
+
+
rl_callback_read_char()
, which will read the next
+character from the current input source. If that character completes the
+line, rl_callback_read_char
will invoke the lhandler
+function saved by rl_callback_handler_install
to process the
+line. EOF
is indicated by calling lhandler with a
+NULL
line.
++
+Here is a function which changes lowercase characters to their uppercase +equivalents, and uppercase characters to lowercase. If +this function was bound to `M-c', then typing `M-c' would +change the case of the character under point. Typing `M-1 0 M-c' +would change the case of the following 10 characters, leaving the cursor on +the last character changed. + +
+ ++/* Invert the case of the COUNT following characters. */ +int +invert_case_line (count, key) + int count, key; +{ + register int start, end, i; + + start = rl_point; + + if (rl_point >= rl_end) + return (0); + + if (count < 0) + { + direction = -1; + count = -count; + } + else + direction = 1; + + /* Find the end of the range to modify. */ + end = start + (count * direction); + + /* Force it to be within range. */ + if (end > rl_end) + end = rl_end; + else if (end < 0) + end = 0; + + if (start == end) + return (0); + + if (start > end) + { + int temp = start; + start = end; + end = temp; + } + + /* Tell readline that we are modifying the line, so it will save + the undo information. */ + rl_modifying (start, end); + + for (i = start; i != end; i++) + { + if (uppercase_p (rl_line_buffer[i])) + rl_line_buffer[i] = to_lower (rl_line_buffer[i]); + else if (lowercase_p (rl_line_buffer[i])) + rl_line_buffer[i] = to_upper (rl_line_buffer[i]); + } + /* Move point to on top of the last character changed. */ + rl_point = (direction == 1) ? end - 1 : start; + return (0); +} ++ + + +
+Typically, a program that reads commands from the user has a way of +disambiguating commands and data. If your program is one of these, then +it can provide completion for commands, data, or both. +The following sections describe how your program and Readline +cooperate to provide this service. + +
+ + + ++In order to complete some text, the full list of possible completions +must be available. That is, it is not possible to accurately +expand a partial word without knowing all of the possible words +which make sense in that context. The Readline library provides +the user interface to completion, and two of the most common +completion functions: filename and username. For completing other types +of text, you must write your own completion function. This section +describes exactly what such functions must do, and provides an example. + +
++There are three major functions used to perform completion: + +
+ +rl_complete ()
. This function is
+called with the same arguments as other Readline
+functions intended for interactive use: count and
+invoking_key. It isolates the word to be completed and calls
+completion_matches ()
to generate a list of possible completions.
+It then either lists the possible completions, inserts the possible
+completions, or actually performs the
+completion, depending on which behavior is desired.
+
+completion_matches ()
uses your
+generator function to generate the list of possible matches, and
+then returns the array of these matches. You should place the address
+of your generator function in rl_completion_entry_function
.
+
+completion_matches ()
, returning a string each time. The
+arguments to the generator function are text and state.
+text is the partial word to be completed. state is zero the
+first time the function is called, allowing the generator to perform
+any necessary initialization, and a positive non-zero integer for
+each subsequent call. When the generator function returns
+(char *)NULL
this signals completion_matches ()
that there are
+no more possibilities left. Usually the generator function computes the
+list of possible completions when state is zero, and returns them
+one at a time on subsequent calls. Each string the generator function
+returns as a match must be allocated with malloc()
; Readline
+frees the strings when it has finished with them.
+
++
completion_matches ()
). The default is to do filename completion.
++
completion_matches
+()
. If the value of rl_completion_entry_function
is
+(Function *)NULL
then the default filename generator function,
+filename_completion_function ()
, is used.
++Here is the complete list of callable completion functions present in +Readline. + +
++
+
completion_matches ()
and rl_completion_entry_function
).
+The default is to do filename
+completion. This calls rl_complete_internal ()
with an
+argument depending on invoking_key.
++
rl_complete
+()
. This calls rl_complete_internal ()
with an argument of
+`?'.
++
rl_complete ()
.
+This calls rl_complete_internal ()
with an argument of `*'.
++
(char *)
which is a list of completions for
+text. If there are no completions, returns (char **)NULL
.
+The first entry in the returned array is the substitution for text.
+The remaining entries are the possible completions. The array is
+terminated with a NULL
pointer.
+
+
+
+entry_func is a function of two args, and returns a
+(char *)
. The first argument is text. The second is a
+state argument; it is zero on the first call, and non-zero on subsequent
+calls. entry_func returns a NULL
pointer to the caller
+when there are no more matches.
+
+
+
+
completion_matches ()
.
+NULL
means to use filename_entry_function ()
, the default
+filename completer.
++
rl_line_buffer
saying
+what the boundaries of text are. If this function exists and
+returns NULL
, or if this variable is set to NULL
, then
+rl_complete ()
will call the value of
+rl_completion_entry_function
to generate matches, otherwise the
+array of strings returned will be used.
++
rl_filename_quote_characters
+appears in a completed filename. The function is called with
+text, match_type, and quote_pointer. The text
+is the filename to be quoted. The match_type is either
+SINGLE_MATCH
, if there is only one completion match, or
+MULT_MATCH
. Some functions use this to decide whether or not to
+insert a closing quote character. The quote_pointer is a pointer
+to any opening quote character the user typed. Some functions choose
+to reset this character.
++
+
rl_completer_word_break_characters
should be
+used to break words for the completer.
++
+
" \t\n\"\\'`@$><=;|&{("
.
++
+
rl_complete_internal ()
. The default list is the value of
+rl_basic_word_break_characters
.
++
rl_completer_word_break_characters
are treated as any other character,
+unless they also appear within this list.
++
+
+
+
+
+
rl_filename_quote_chars
. This is always non-zero
+on entry, and can only be changed within a completion entry generator
+function. The quoting is effected via a call to the function pointed to
+by rl_filename_quoting_function
.
++
self-insert
.
++
NULL
terminated array of matches.
+The first element (matches[0]
) is the
+maximal substring common to all matches. This function can
+re-arrange the list of matches as required, but each element deleted
+from the array must be freed.
++
+Here is a small application demonstrating the use of the GNU Readline
+library. It is called fileman
, and the source code resides in
+`examples/fileman.c'. This sample application provides
+completion of command names, line editing features, and access to the
+history list.
+
+
+/* fileman.c -- A tiny application which demonstrates how to use the + GNU Readline library. This application interactively allows users + to manipulate files and their modes. */ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/file.h> +#include <sys/stat.h> +#include <sys/errno.h> + +#include <readline/readline.h> +#include <readline/history.h> + +extern char *getwd (); +extern char *xmalloc (); + +/* The names of functions that actually do the manipulation. */ +int com_list (), com_view (), com_rename (), com_stat (), com_pwd (); +int com_delete (), com_help (), com_cd (), com_quit (); + +/* A structure which contains information on the commands this program + can understand. */ + +typedef struct { + char *name; /* User printable name of the function. */ + Function *func; /* Function to call to do the job. */ + char *doc; /* Documentation for this function. */ +} COMMAND; + +COMMAND commands[] = { + { "cd", com_cd, "Change to directory DIR" }, + { "delete", com_delete, "Delete FILE" }, + { "help", com_help, "Display this text" }, + { "?", com_help, "Synonym for `help'" }, + { "list", com_list, "List files in DIR" }, + { "ls", com_list, "Synonym for `list'" }, + { "pwd", com_pwd, "Print the current working directory" }, + { "quit", com_quit, "Quit using Fileman" }, + { "rename", com_rename, "Rename FILE to NEWNAME" }, + { "stat", com_stat, "Print out statistics on FILE" }, + { "view", com_view, "View the contents of FILE" }, + { (char *)NULL, (Function *)NULL, (char *)NULL } +}; + +/* Forward declarations. */ +char *stripwhite (); +COMMAND *find_command (); + +/* The name of this program, as taken from argv[0]. */ +char *progname; + +/* When non-zero, this global means the user is done using this program. */ +int done; + +char * +dupstr (s) + int s; +{ + char *r; + + r = xmalloc (strlen (s) + 1); + strcpy (r, s); + return (r); +} + +main (argc, argv) + int argc; + char **argv; +{ + char *line, *s; + + progname = argv[0]; + + initialize_readline (); /* Bind our completer. */ + + /* Loop reading and executing lines until the user quits. */ + for ( ; done == 0; ) + { + line = readline ("FileMan: "); + + if (!line) + break; + + /* Remove leading and trailing whitespace from the line. + Then, if there is anything left, add it to the history list + and execute it. */ + s = stripwhite (line); + + if (*s) + { + add_history (s); + execute_line (s); + } + + free (line); + } + exit (0); +} + +/* Execute a command line. */ +int +execute_line (line) + char *line; +{ + register int i; + COMMAND *command; + char *word; + + /* Isolate the command word. */ + i = 0; + while (line[i] && whitespace (line[i])) + i++; + word = line + i; + + while (line[i] && !whitespace (line[i])) + i++; + + if (line[i]) + line[i++] = '\0'; + + command = find_command (word); + + if (!command) + { + fprintf (stderr, "%s: No such command for FileMan.\n", word); + return (-1); + } + + /* Get argument to command, if any. */ + while (whitespace (line[i])) + i++; + + word = line + i; + + /* Call the function. */ + return ((*(command->func)) (word)); +} + +/* Look up NAME as the name of a command, and return a pointer to that + command. Return a NULL pointer if NAME isn't a command name. */ +COMMAND * +find_command (name) + char *name; +{ + register int i; + + for (i = 0; commands[i].name; i++) + if (strcmp (name, commands[i].name) == 0) + return (&commands[i]); + + return ((COMMAND *)NULL); +} + +/* Strip whitespace from the start and end of STRING. Return a pointer + into STRING. */ +char * +stripwhite (string) + char *string; +{ + register char *s, *t; + + for (s = string; whitespace (*s); s++) + ; + + if (*s == 0) + return (s); + + t = s + strlen (s) - 1; + while (t > s && whitespace (*t)) + t--; + *++t = '\0'; + + return s; +} + +/* **************************************************************** */ +/* */ +/* Interface to Readline Completion */ +/* */ +/* **************************************************************** */ + +char *command_generator (); +char **fileman_completion (); + +/* Tell the GNU Readline library how to complete. We want to try to complete + on command names if this is the first word in the line, or on filenames + if not. */ +initialize_readline () +{ + /* Allow conditional parsing of the ~/.inputrc file. */ + rl_readline_name = "FileMan"; + + /* Tell the completer that we want a crack first. */ + rl_attempted_completion_function = (CPPFunction *)fileman_completion; +} + +/* Attempt to complete on the contents of TEXT. START and END bound the + region of rl_line_buffer that contains the word to complete. TEXT is + the word to complete. We can use the entire contents of rl_line_buffer + in case we want to do some simple parsing. Return the array of matches, + or NULL if there aren't any. */ +char ** +fileman_completion (text, start, end) + char *text; + int start, end; +{ + char **matches; + + matches = (char **)NULL; + + /* If this word is at the start of the line, then it is a command + to complete. Otherwise it is the name of a file in the current + directory. */ + if (start == 0) + matches = completion_matches (text, command_generator); + + return (matches); +} + +/* Generator function for command completion. STATE lets us know whether + to start from scratch; without any state (i.e. STATE == 0), then we + start at the top of the list. */ +char * +command_generator (text, state) + char *text; + int state; +{ + static int list_index, len; + char *name; + + /* If this is a new word to complete, initialize now. This includes + saving the length of TEXT for efficiency, and initializing the index + variable to 0. */ + if (!state) + { + list_index = 0; + len = strlen (text); + } + + /* Return the next name which partially matches from the command list. */ + while (name = commands[list_index].name) + { + list_index++; + + if (strncmp (name, text, len) == 0) + return (dupstr(name)); + } + + /* If no names matched, then return NULL. */ + return ((char *)NULL); +} + +/* **************************************************************** */ +/* */ +/* FileMan Commands */ +/* */ +/* **************************************************************** */ + +/* String to pass to system (). This is for the LIST, VIEW and RENAME + commands. */ +static char syscom[1024]; + +/* List the file(s) named in arg. */ +com_list (arg) + char *arg; +{ + if (!arg) + arg = ""; + + sprintf (syscom, "ls -FClg %s", arg); + return (system (syscom)); +} + +com_view (arg) + char *arg; +{ + if (!valid_argument ("view", arg)) + return 1; + + sprintf (syscom, "more %s", arg); + return (system (syscom)); +} + +com_rename (arg) + char *arg; +{ + too_dangerous ("rename"); + return (1); +} + +com_stat (arg) + char *arg; +{ + struct stat finfo; + + if (!valid_argument ("stat", arg)) + return (1); + + if (stat (arg, &finfo) == -1) + { + perror (arg); + return (1); + } + + printf ("Statistics for `%s':\n", arg); + + printf ("%s has %d link%s, and is %d byte%s in length.\n", arg, + finfo.st_nlink, + (finfo.st_nlink == 1) ? "" : "s", + finfo.st_size, + (finfo.st_size == 1) ? "" : "s"); + printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime)); + printf (" Last access at: %s", ctime (&finfo.st_atime)); + printf (" Last modified at: %s", ctime (&finfo.st_mtime)); + return (0); +} + +com_delete (arg) + char *arg; +{ + too_dangerous ("delete"); + return (1); +} + +/* Print out help for ARG, or for all of the commands if ARG is + not present. */ +com_help (arg) + char *arg; +{ + register int i; + int printed = 0; + + for (i = 0; commands[i].name; i++) + { + if (!*arg || (strcmp (arg, commands[i].name) == 0)) + { + printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc); + printed++; + } + } + + if (!printed) + { + printf ("No commands match `%s'. Possibilties are:\n", arg); + + for (i = 0; commands[i].name; i++) + { + /* Print in six columns. */ + if (printed == 6) + { + printed = 0; + printf ("\n"); + } + + printf ("%s\t", commands[i].name); + printed++; + } + + if (printed) + printf ("\n"); + } + return (0); +} + +/* Change to the directory ARG. */ +com_cd (arg) + char *arg; +{ + if (chdir (arg) == -1) + { + perror (arg); + return 1; + } + + com_pwd (""); + return (0); +} + +/* Print out the current working directory. */ +com_pwd (ignore) + char *ignore; +{ + char dir[1024], *s; + + s = getwd (dir); + if (s == 0) + { + printf ("Error getting pwd: %s\n", dir); + return 1; + } + + printf ("Current directory is %s\n", dir); + return 0; +} + +/* The user wishes to quit using this program. Just set DONE non-zero. */ +com_quit (arg) + char *arg; +{ + done = 1; + return (0); +} + +/* Function which tells you that you can't do this. */ +too_dangerous (caller) + char *caller; +{ + fprintf (stderr, + "%s: Too dangerous for me to distribute. Write it yourself.\n", + caller); +} + +/* Return non-zero if ARG is a valid argument for CALLER, else print + an error message and return zero. */ +int +valid_argument (caller, arg) + char *caller, *arg; +{ + if (!arg || !*arg) + { + fprintf (stderr, "%s: Argument required.\n", caller); + return (0); + } + + return (1); +} ++ + + +
+
+
+This document was generated on 2 April 1998 using the +texi2html +translator version 1.51.
+ + diff --git a/readline/doc/readline.info b/readline/doc/readline.info index a93489f3d6d..251ca9e56b5 100644 --- a/readline/doc/readline.info +++ b/readline/doc/readline.info @@ -1,15 +1,20 @@ -Info file readline.info, produced by Makeinfo, -*- Text -*- from input -file rlman.texinfo. +This is Info file readline.info, produced by Makeinfo version 1.67 from +the input file /usr/homes/chet/src/bash/readline-2.2/doc/rlman.texinfo. + +INFO-DIR-SECTION Libraries +START-INFO-DIR-ENTRY +* Readline: (readline). The GNU readline library API +END-INFO-DIR-ENTRY This document describes the GNU Readline Library, a utility which -aids in the consistency of user interface across discrete programs -that need to provide a command line interface. +aids in the consistency of user interface across discrete programs that +need to provide a command line interface. Copyright (C) 1988, 1991 Free Software Foundation, Inc. - Permission is granted to make and distribute verbatim copies of -this manual provided the copyright notice and this permission notice -pare preserved on all copies. + Permission is granted to make and distribute verbatim copies of this +manual provided the copyright notice and this permission notice pare +preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that @@ -22,14 +27,14 @@ versions, except that this permission notice may be stated in a translation approved by the Foundation. -File: readline.info, Node: Top, Next: Command Line Editing, Prev: (DIR), Up: (DIR) +File: readline.info, Node: Top, Next: Command Line Editing, Up: (dir) GNU Readline Library ******************** This document describes the GNU Readline Library, a utility which -aids in the consistency of user interface across discrete programs -that need to provide a command line interface. +aids in the consistency of user interface across discrete programs that +need to provide a command line interface. * Menu: @@ -45,13 +50,18 @@ File: readline.info, Node: Command Line Editing, Next: Programming with GNU Re Command Line Editing ******************** - This text describes GNU's command line editing interface. + This chapter describes the basic features of the GNU command line +editing interface. * Menu: * Introduction and Notation:: Notation used in this text. * Readline Interaction:: The minimum set of commands for editing a line. * Readline Init File:: Customizing Readline from a user's view. +* Bindable Readline Commands:: A description of most of the Readline commands + available for binding +* Readline vi Mode:: A short description of how to make Readline + behave like the vi editor. File: readline.info, Node: Introduction and Notation, Next: Readline Interaction, Up: Command Line Editing @@ -59,24 +69,25 @@ File: readline.info, Node: Introduction and Notation, Next: Readline Interacti Introduction to Line Editing ============================ - The following paragraphs describe the notation we use to represent + The following paragraphs describe the notation used to represent keystrokes. - The text C-k is read as `Control-K' and describes the character -produced when the Control key is depressed and the k key is struck. + The text