📄 hstech.texi
字号:
@deftypefun int history_search (const char *string, int direction)Search the history for @var{string}, starting at the current history offset.If @var{direction} is less than 0, then the search is throughprevious entries, otherwise through subsequent entries.If @var{string} is found, thenthe current history index is set to that history entry, and the valuereturned is the offset in the line of the entry where@var{string} was found. Otherwise, nothing is changed, and a -1 isreturned.@end deftypefun@deftypefun int history_search_prefix (const char *string, int direction)Search the history for @var{string}, starting at the current historyoffset. The search is anchored: matching lines must begin with@var{string}. If @var{direction} is less than 0, then the search isthrough previous entries, otherwise through subsequent entries.If @var{string} is found, then thecurrent 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 (const char *string, int direction, int pos)Search for @var{string} in the history list, starting at @var{pos}, anabsolute index into the list. If @var{direction} is negative, the searchproceeds backward from @var{pos}, otherwise forward. Returns the absoluteindex of the history element where @var{string} was found, or -1 otherwise.@end deftypefun@node Managing the History File@subsection Managing the History FileThe 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 (const 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 @code{errno} if not.@end deftypefun@deftypefun int read_history_range (const char *filename, int from, int to)Read a range of lines from @var{filename}, adding them to the history list.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 deftypefun@deftypefun int write_history (const 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}.Returns 0 on success, or @code{errno} on a read or write error.@end deftypefun@deftypefun int append_history (int nelements, const char *filename)Append the last @var{nelements} of the history list to @var{filename}.If @var{filename} is @code{NULL}, then append to @file{~/.history}.Returns 0 on success, or @code{errno} on a read or write error.@end deftypefun@deftypefun int history_truncate_file (const char *filename, int nlines)Truncate the history file @var{filename}, leaving only the last@var{nlines} lines.If @var{filename} is @code{NULL}, then @file{~/.history} is truncated.Returns 0 on success, or @code{errno} on failure.@end deftypefun@node History Expansion@subsection History ExpansionThese functions implement history expansion.@deftypefun int history_expand (char *string, char **output)Expand @var{string}, placing the result into @var{output}, a pointerto a string (@pxref{History Interaction}). Returns:@table @code@item 0If no expansions took place (or, if the only change inthe text was the removal of escape characters preceding the history expansioncharacter);@item 1if expansions did take place;@item -1if there was an error in expansion;@item 2if the returned line should be displayed, but not executed,as with the @code{:p} modifier (@pxref{Modifiers}).@end tableIf an error ocurred in expansion, then @var{output} contains a descriptiveerror message.@end deftypefun@deftypefun {char *} get_history_event (const 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 eventspecifier. 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 additionto the ``normal'' terminating characters.@end deftypefun@deftypefun {char **} history_tokenize (const char *string)Return an array of tokens parsed out of @var{string}, much as theshell might. The tokens are split on the characters in the@var{history_word_delimiters} variable,and shell quoting conventions are obeyed.@end deftypefun@deftypefun {char *} history_arg_extract (int first, int last, const char *string)Extract a string segment consisting of the @var{first} through @var{last}arguments present in @var{string}. Arguments are split using@code{history_tokenize}.@end deftypefun@node History Variables@section History VariablesThis section describes the externally-visible variables exported bythe @sc{gnu} History Library.@deftypevar int history_baseThe logical offset of the first entry in the history list.@end deftypevar@deftypevar int history_lengthThe number of entries currently stored in the history list.@end deftypevar@deftypevar int history_max_entriesThe maximum number of history entries. This must be changed using@code{stifle_history()}.@end deftypevar@deftypevar int history_write_timestampsIf non-zero, timestamps are written to the history file, so they can bepreserved between sessions. The default value is 0, meaning thattimestamps are not saved.@end deftypevar@deftypevar char history_expansion_charThe character that introduces a history event. The default is @samp{!}.Setting this to 0 inhibits history expansion.@end deftypevar@deftypevar char history_subst_charThe character that invokes word substitution if found at the start ofa line. The default is @samp{^}.@end deftypevar@deftypevar char history_comment_charDuring tokenization, if this character is seen as the first characterof a word, then it and all subsequent characters up to a newline areignored, suppressing history expansion for the remainder of the line.This is disabled by default.@end deftypevar@deftypevar {char *} history_word_delimitersThe characters that separate tokens for @code{history_tokenize()}.The default value is @code{" \t\n()<>;&|"}.@end deftypevar@deftypevar {char *} history_search_delimiter_charsThe list of additional characters which can delimit a history searchstring, in addition to space, TAB, @samp{:} and @samp{?} in the case ofa substring search. The default is empty.@end deftypevar@deftypevar {char *} history_no_expand_charsThe list of characters which inhibit history expansion if found immediatelyfollowing @var{history_expansion_char}. The default is space, tab, newline,carriage return, and @samp{=}.@end deftypevar@deftypevar int history_quotes_inhibit_expansionIf non-zero, single-quoted words are not scanned for the history expansioncharacter. The default value is 0.@end deftypevar@deftypevar {rl_linebuf_func_t *} history_inhibit_expansion_functionThis should be set to the address of a function that takes two arguments:a @code{char *} (@var{string})and an @code{int} 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 shouldbe done.It is intended for use by applications like Bash that use the historyexpansion character for additional purposes.By default, this variable is set to @code{NULL}.@end deftypevar@node History Programming Example@section History Programming ExampleThe following program demonstrates simple use of the @sc{gnu} History Library.@smallexample#include <stdio.h>#include <readline/history.h>main (argc, argv) int argc; char **argv;@{ 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"); @} @} @}@}@end smallexample
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -