xref: /openbsd-src/gnu/lib/libreadline/history.h (revision 9704b281e65e1189747652d0ba55eee892cff5f7)
11acd27e7Smillert /* History.h -- the names of functions that you can call in history. */
21acd27e7Smillert /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
31acd27e7Smillert 
41acd27e7Smillert    This file contains the GNU History Library (the Library), a set of
51acd27e7Smillert    routines for managing the text of previously typed lines.
61acd27e7Smillert 
71acd27e7Smillert    The Library is free software; you can redistribute it and/or modify
81acd27e7Smillert    it under the terms of the GNU General Public License as published by
91acd27e7Smillert    the Free Software Foundation; either version 2, or (at your option)
101acd27e7Smillert    any later version.
111acd27e7Smillert 
121acd27e7Smillert    The Library is distributed in the hope that it will be useful, but
131acd27e7Smillert    WITHOUT ANY WARRANTY; without even the implied warranty of
141acd27e7Smillert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
151acd27e7Smillert    General Public License for more details.
161acd27e7Smillert 
171acd27e7Smillert    The GNU General Public License is often shipped with GNU software, and
181acd27e7Smillert    is generally kept in a file called COPYING or LICENSE.  If you do not
191acd27e7Smillert    have a copy of the license, write to the Free Software Foundation,
201acd27e7Smillert    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
211acd27e7Smillert 
221acd27e7Smillert #ifndef _HISTORY_H_
231acd27e7Smillert #define _HISTORY_H_
241acd27e7Smillert 
251acd27e7Smillert #ifdef __cplusplus
261acd27e7Smillert extern "C" {
271acd27e7Smillert #endif
281acd27e7Smillert 
291acd27e7Smillert #if defined READLINE_LIBRARY
301acd27e7Smillert #  include "rlstdc.h"
31*15b117eaSkettenis #  include "rltypedefs.h"
321acd27e7Smillert #else
331acd27e7Smillert #  include <readline/rlstdc.h>
34*15b117eaSkettenis #  include <readline/rltypedefs.h>
351acd27e7Smillert #endif
361acd27e7Smillert 
371acd27e7Smillert #ifdef __STDC__
381acd27e7Smillert typedef void *histdata_t;
391acd27e7Smillert #else
401acd27e7Smillert typedef char *histdata_t;
411acd27e7Smillert #endif
421acd27e7Smillert 
431acd27e7Smillert /* The structure used to store a history entry. */
441acd27e7Smillert typedef struct _hist_entry {
451acd27e7Smillert   char *line;
461acd27e7Smillert   histdata_t data;
471acd27e7Smillert } HIST_ENTRY;
481acd27e7Smillert 
491acd27e7Smillert /* A structure used to pass the current state of the history stuff around. */
501acd27e7Smillert typedef struct _hist_state {
511acd27e7Smillert   HIST_ENTRY **entries;		/* Pointer to the entries themselves. */
521acd27e7Smillert   int offset;			/* The location pointer within this array. */
531acd27e7Smillert   int length;			/* Number of elements within this array. */
541acd27e7Smillert   int size;			/* Number of slots allocated to this array. */
551acd27e7Smillert   int flags;
561acd27e7Smillert } HISTORY_STATE;
571acd27e7Smillert 
581acd27e7Smillert /* Flag values for the `flags' member of HISTORY_STATE. */
591acd27e7Smillert #define HS_STIFLED	0x01
601acd27e7Smillert 
611acd27e7Smillert /* Initialization and state management. */
621acd27e7Smillert 
631acd27e7Smillert /* Begin a session in which the history functions might be used.  This
641acd27e7Smillert    just initializes the interactive variables. */
65*15b117eaSkettenis extern void using_history PARAMS((void));
661acd27e7Smillert 
671acd27e7Smillert /* Return the current HISTORY_STATE of the history. */
68*15b117eaSkettenis extern HISTORY_STATE *history_get_history_state PARAMS((void));
691acd27e7Smillert 
701acd27e7Smillert /* Set the state of the current history array to STATE. */
71*15b117eaSkettenis extern void history_set_history_state PARAMS((HISTORY_STATE *));
721acd27e7Smillert 
731acd27e7Smillert /* Manage the history list. */
741acd27e7Smillert 
751acd27e7Smillert /* Place STRING at the end of the history list.
761acd27e7Smillert    The associated data field (if any) is set to NULL. */
77*15b117eaSkettenis extern void add_history PARAMS((const char *));
781acd27e7Smillert 
791acd27e7Smillert /* A reasonably useless function, only here for completeness.  WHICH
801acd27e7Smillert    is the magic number that tells us which element to delete.  The
811acd27e7Smillert    elements are numbered from 0. */
82*15b117eaSkettenis extern HIST_ENTRY *remove_history PARAMS((int));
831acd27e7Smillert 
841acd27e7Smillert /* Make the history entry at WHICH have LINE and DATA.  This returns
851acd27e7Smillert    the old entry so you can dispose of the data.  In the case of an
861acd27e7Smillert    invalid WHICH, a NULL pointer is returned. */
87*15b117eaSkettenis extern HIST_ENTRY *replace_history_entry PARAMS((int, const char *, histdata_t));
881acd27e7Smillert 
891acd27e7Smillert /* Clear the history list and start over. */
90*15b117eaSkettenis extern void clear_history PARAMS((void));
911acd27e7Smillert 
921acd27e7Smillert /* Stifle the history list, remembering only MAX number of entries. */
93*15b117eaSkettenis extern void stifle_history PARAMS((int));
941acd27e7Smillert 
951acd27e7Smillert /* Stop stifling the history.  This returns the previous amount the
961acd27e7Smillert    history was stifled by.  The value is positive if the history was
971acd27e7Smillert    stifled, negative if it wasn't. */
98*15b117eaSkettenis extern int unstifle_history PARAMS((void));
991acd27e7Smillert 
1001acd27e7Smillert /* Return 1 if the history is stifled, 0 if it is not. */
101*15b117eaSkettenis extern int history_is_stifled PARAMS((void));
1021acd27e7Smillert 
1031acd27e7Smillert /* Information about the history list. */
1041acd27e7Smillert 
1051acd27e7Smillert /* Return a NULL terminated array of HIST_ENTRY which is the current input
1061acd27e7Smillert    history.  Element 0 of this list is the beginning of time.  If there
1071acd27e7Smillert    is no history, return NULL. */
108*15b117eaSkettenis extern HIST_ENTRY **history_list PARAMS((void));
1091acd27e7Smillert 
1101acd27e7Smillert /* Returns the number which says what history element we are now
1111acd27e7Smillert    looking at.  */
112*15b117eaSkettenis extern int where_history PARAMS((void));
1131acd27e7Smillert 
1141acd27e7Smillert /* Return the history entry at the current position, as determined by
1151acd27e7Smillert    history_offset.  If there is no entry there, return a NULL pointer. */
116*15b117eaSkettenis extern HIST_ENTRY *current_history PARAMS((void));
1171acd27e7Smillert 
1181acd27e7Smillert /* Return the history entry which is logically at OFFSET in the history
1191acd27e7Smillert    array.  OFFSET is relative to history_base. */
120*15b117eaSkettenis extern HIST_ENTRY *history_get PARAMS((int));
1211acd27e7Smillert 
1221acd27e7Smillert /* Return the number of bytes that the primary history entries are using.
1231acd27e7Smillert    This just adds up the lengths of the_history->lines. */
124*15b117eaSkettenis extern int history_total_bytes PARAMS((void));
1251acd27e7Smillert 
1261acd27e7Smillert /* Moving around the history list. */
1271acd27e7Smillert 
1281acd27e7Smillert /* Set the position in the history list to POS. */
129*15b117eaSkettenis extern int history_set_pos PARAMS((int));
1301acd27e7Smillert 
1311acd27e7Smillert /* Back up history_offset to the previous history entry, and return
1321acd27e7Smillert    a pointer to that entry.  If there is no previous entry, return
1331acd27e7Smillert    a NULL pointer. */
134*15b117eaSkettenis extern HIST_ENTRY *previous_history PARAMS((void));
1351acd27e7Smillert 
1361acd27e7Smillert /* Move history_offset forward to the next item in the input_history,
1371acd27e7Smillert    and return the a pointer to that entry.  If there is no next entry,
1381acd27e7Smillert    return a NULL pointer. */
139*15b117eaSkettenis extern HIST_ENTRY *next_history PARAMS((void));
1401acd27e7Smillert 
1411acd27e7Smillert /* Searching the history list. */
1421acd27e7Smillert 
1431acd27e7Smillert /* Search the history for STRING, starting at history_offset.
1441acd27e7Smillert    If DIRECTION < 0, then the search is through previous entries,
1451acd27e7Smillert    else through subsequent.  If the string is found, then
1461acd27e7Smillert    current_history () is the history entry, and the value of this function
1471acd27e7Smillert    is the offset in the line of that history entry that the string was
1481acd27e7Smillert    found in.  Otherwise, nothing is changed, and a -1 is returned. */
149*15b117eaSkettenis extern int history_search PARAMS((const char *, int));
1501acd27e7Smillert 
1511acd27e7Smillert /* Search the history for STRING, starting at history_offset.
1521acd27e7Smillert    The search is anchored: matching lines must begin with string.
1531acd27e7Smillert    DIRECTION is as in history_search(). */
154*15b117eaSkettenis extern int history_search_prefix PARAMS((const char *, int));
1551acd27e7Smillert 
1561acd27e7Smillert /* Search for STRING in the history list, starting at POS, an
1571acd27e7Smillert    absolute index into the list.  DIR, if negative, says to search
1581acd27e7Smillert    backwards from POS, else forwards.
1591acd27e7Smillert    Returns the absolute index of the history element where STRING
1601acd27e7Smillert    was found, or -1 otherwise. */
161*15b117eaSkettenis extern int history_search_pos PARAMS((const char *, int, int));
1621acd27e7Smillert 
1631acd27e7Smillert /* Managing the history file. */
1641acd27e7Smillert 
1651acd27e7Smillert /* Add the contents of FILENAME to the history list, a line at a time.
1661acd27e7Smillert    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
1671acd27e7Smillert    successful, or errno if not. */
168*15b117eaSkettenis extern int read_history PARAMS((const char *));
1691acd27e7Smillert 
1701acd27e7Smillert /* Read a range of lines from FILENAME, adding them to the history list.
1711acd27e7Smillert    Start reading at the FROM'th line and end at the TO'th.  If FROM
1721acd27e7Smillert    is zero, start at the beginning.  If TO is less than FROM, read
1731acd27e7Smillert    until the end of the file.  If FILENAME is NULL, then read from
1741acd27e7Smillert    ~/.history.  Returns 0 if successful, or errno if not. */
175*15b117eaSkettenis extern int read_history_range PARAMS((const char *, int, int));
1761acd27e7Smillert 
1771acd27e7Smillert /* Write the current history to FILENAME.  If FILENAME is NULL,
1781acd27e7Smillert    then write the history list to ~/.history.  Values returned
1791acd27e7Smillert    are as in read_history ().  */
180*15b117eaSkettenis extern int write_history PARAMS((const char *));
1811acd27e7Smillert 
1821acd27e7Smillert /* Append NELEMENT entries to FILENAME.  The entries appended are from
1831acd27e7Smillert    the end of the list minus NELEMENTs up to the end of the list. */
184*15b117eaSkettenis extern int append_history PARAMS((int, const char *));
1851acd27e7Smillert 
1861acd27e7Smillert /* Truncate the history file, leaving only the last NLINES lines. */
187*15b117eaSkettenis extern int history_truncate_file PARAMS((const char *, int));
1881acd27e7Smillert 
1891acd27e7Smillert /* History expansion. */
1901acd27e7Smillert 
1911acd27e7Smillert /* Expand the string STRING, placing the result into OUTPUT, a pointer
1921acd27e7Smillert    to a string.  Returns:
1931acd27e7Smillert 
1941acd27e7Smillert    0) If no expansions took place (or, if the only change in
1951acd27e7Smillert       the text was the de-slashifying of the history expansion
1961acd27e7Smillert       character)
1971acd27e7Smillert    1) If expansions did take place
1981acd27e7Smillert   -1) If there was an error in expansion.
1991acd27e7Smillert    2) If the returned line should just be printed.
2001acd27e7Smillert 
2011acd27e7Smillert   If an error ocurred in expansion, then OUTPUT contains a descriptive
2021acd27e7Smillert   error message. */
203*15b117eaSkettenis extern int history_expand PARAMS((char *, char **));
2041acd27e7Smillert 
2051acd27e7Smillert /* Extract a string segment consisting of the FIRST through LAST
2061acd27e7Smillert    arguments present in STRING.  Arguments are broken up as in
2071acd27e7Smillert    the shell. */
208*15b117eaSkettenis extern char *history_arg_extract PARAMS((int, int, const char *));
2091acd27e7Smillert 
2101acd27e7Smillert /* Return the text of the history event beginning at the current
2111acd27e7Smillert    offset into STRING.  Pass STRING with *INDEX equal to the
2121acd27e7Smillert    history_expansion_char that begins this specification.
2131acd27e7Smillert    DELIMITING_QUOTE is a character that is allowed to end the string
2141acd27e7Smillert    specification for what to search for in addition to the normal
2151acd27e7Smillert    characters `:', ` ', `\t', `\n', and sometimes `?'. */
216*15b117eaSkettenis extern char *get_history_event PARAMS((const char *, int *, int));
2171acd27e7Smillert 
2181acd27e7Smillert /* Return an array of tokens, much as the shell might.  The tokens are
2191acd27e7Smillert    parsed out of STRING. */
220*15b117eaSkettenis extern char **history_tokenize PARAMS((const char *));
2211acd27e7Smillert 
2221acd27e7Smillert /* Exported history variables. */
2231acd27e7Smillert extern int history_base;
2241acd27e7Smillert extern int history_length;
225*15b117eaSkettenis extern int history_max_entries;
2261acd27e7Smillert extern char history_expansion_char;
2271acd27e7Smillert extern char history_subst_char;
228*15b117eaSkettenis extern char *history_word_delimiters;
2291acd27e7Smillert extern char history_comment_char;
2301acd27e7Smillert extern char *history_no_expand_chars;
2311acd27e7Smillert extern char *history_search_delimiter_chars;
2321acd27e7Smillert extern int history_quotes_inhibit_expansion;
2331acd27e7Smillert 
234*15b117eaSkettenis /* Backwards compatibility */
235*15b117eaSkettenis extern int max_input_history;
236*15b117eaSkettenis 
2371acd27e7Smillert /* If set, this function is called to decide whether or not a particular
2381acd27e7Smillert    history expansion should be treated as a special case for the calling
2391acd27e7Smillert    application and not expanded. */
240*15b117eaSkettenis extern rl_linebuf_func_t *history_inhibit_expansion_function;
2411acd27e7Smillert 
2421acd27e7Smillert #ifdef __cplusplus
2431acd27e7Smillert }
2441acd27e7Smillert #endif
2451acd27e7Smillert 
2461acd27e7Smillert #endif /* !_HISTORY_H_ */
247