📄 rltech.texinfo
字号:
Print the readline function names and the key sequences currentlybound to them to @code{rl_outstream}. If @var{readable} is non-zero,the list is formatted in such a way that it can be made part of an@code{inputrc} file and re-read.@end deftypefun@deftypefun void rl_list_funmap_names ()Print the names of all bindable Readline functions to @code{rl_outstream}.@end deftypefun@node Allowing Undoing@subsection Allowing UndoingSupporting the undo command is a painless thing, and makes yourfunctions much more useful. It is certainly easy to trysomething if you know you can undo it. I could use an undo function forthe stock market.If your function simply inserts text once, or deletes text once, anduses @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, thenundoing is already done for you automatically.If you do multiple insertions or multiple deletions, or any combinationof these operations, you should group them together into one operation.This is done with @code{rl_begin_undo_group ()} and@code{rl_end_undo_group ()}.The types of events that can be undone are:@exampleenum undo_code @{ UNDO_DELETE, UNDO_INSERT, UNDO_BEGIN, UNDO_END @}; @end exampleNotice that @code{UNDO_DELETE} means to insert some text, and@code{UNDO_INSERT} means to delete some text. That is, the undo codetells undo what to undo, not how to undo it. @code{UNDO_BEGIN} and@code{UNDO_END} are tags added by @code{rl_begin_undo_group ()} and@code{rl_end_undo_group ()}.@deftypefun int rl_begin_undo_group ()Begins saving undo information in a group construct. The undoinformation usually comes from calls to @code{rl_insert_text ()} and@code{rl_delete_text ()}, but could be the result of calls to@code{rl_add_undo ()}.@end deftypefun@deftypefun int rl_end_undo_group ()Closes the current undo group started with @code{rl_begin_undo_group()}. There should be one call to @code{rl_end_undo_group ()}for each call to @code{rl_begin_undo_group ()}.@end deftypefun@deftypefun void rl_add_undo (enum undo_code what, int start, int end, char *text)Remember how to undo an event (according to @var{what}). The affectedtext runs from @var{start} to @var{end}, and encompasses @var{text}.@end deftypefun@deftypefun void free_undo_list ()Free the existing undo list.@end deftypefun@deftypefun int rl_do_undo ()Undo the first thing on the undo list. Returns @code{0} if there wasnothing to undo, non-zero if something was undone.@end deftypefunFinally, if you neither insert nor delete text, but directly modify theexisting text (e.g., change its case), call @code{rl_modifying ()}once, just before you modify the text. You must supply the indices ofthe text range that you are going to modify.@deftypefun int rl_modifying (int start, int end)Tell Readline to save the text between @var{start} and @var{end} as asingle undo unit. It is assumed that you will subsequently modifythat text.@end deftypefun@node Redisplay@subsection Redisplay@deftypefun int rl_redisplay ()Change what's displayed on the screen to reflect the current contentsof @code{rl_line_buffer}.@end deftypefun@deftypefun int rl_forced_update_display ()Force the line to be updated and redisplayed, whether or notReadline thinks the screen display is correct.@end deftypefun@deftypefun int rl_on_new_line ()Tell the update routines that we have moved onto a new (empty) line,usually after ouputting a newline.@end deftypefun@deftypefun int rl_reset_line_state ()Reset the display state to a clean state and redisplay the current linestarting on a new line.@end deftypefun@deftypefun int rl_message (va_alist)The arguments are a string as would be supplied to @code{printf}. Theresulting string is displayed in the @dfn{echo area}. The echo areais also used to display numeric arguments and search strings.@end deftypefun@deftypefun int rl_clear_message ()Clear the message in the echo area.@end deftypefun@node Modifying Text@subsection Modifying Text@deftypefun int rl_insert_text (char *text)Insert @var{text} into the line at the current cursor position.@end deftypefun@deftypefun int rl_delete_text (int start, int end)Delete the text between @var{start} and @var{end} in the current line.@end deftypefun@deftypefun {char *} rl_copy_text (int start, int end)Return a copy of the text between @var{start} and @var{end} inthe current line.@end deftypefun@deftypefun int rl_kill_text (int start, int end)Copy the text between @var{start} and @var{end} in the current lineto the kill ring, appending or prepending to the last kill if thelast command was a kill command. The text is deleted.If @var{start} is less than @var{end},the text is appended, otherwise prepended. If the last command wasnot a kill, a new kill ring slot is used.@end deftypefun@node Utility Functions@subsection Utility Functions@deftypefun int rl_read_key ()Return the next character available. This handles input inserted intothe input stream via @var{pending input} (@pxref{Readline Variables})and @code{rl_stuff_char ()}, macros, and characters read from the keyboard.@end deftypefun@deftypefun int rl_stuff_char (int c)Insert @var{c} into the Readline input stream. It will be "read"before Readline attempts to read characters from the terminal with@code{rl_read_key ()}.@end deftypefun@deftypefun int rl_initialize ()Initialize or re-initialize Readline's internal state.@end deftypefun@deftypefun int rl_reset_terminal (char *terminal_name)Reinitialize Readline's idea of the terminal settings using@var{terminal_name} as the terminal type (e.g., @code{vt100}).@end deftypefun@deftypefun int alphabetic (int c)Return 1 if @var{c} is an alphabetic character.@end deftypefun@deftypefun int numeric (int c)Return 1 if @var{c} is a numeric character.@end deftypefun@deftypefun int ding ()Ring the terminal bell, obeying the setting of @code{bell-style}.@end deftypefunThe following are implemented as macros, defined in @code{chartypes.h}.@deftypefun int uppercase_p (int c)Return 1 if @var{c} is an uppercase alphabetic character.@end deftypefun@deftypefun int lowercase_p (int c)Return 1 if @var{c} is a lowercase alphabetic character.@end deftypefun@deftypefun int digit_p (int c)Return 1 if @var{c} is a numeric character.@end deftypefun@deftypefun int to_upper (int c)If @var{c} is a lowercase alphabetic character, return the correspondinguppercase character.@end deftypefun@deftypefun int to_lower (int c)If @var{c} is an uppercase alphabetic character, return the correspondinglowercase character.@end deftypefun@deftypefun int digit_value (int c)If @var{c} is a number, return the value it represents.@end deftypefun@subsection An ExampleHere is a function which changes lowercase characters to their uppercaseequivalents, and uppercase characters to lowercase. Ifthis function was bound to @samp{M-c}, then typing @samp{M-c} wouldchange the case of the character under point. Typing @samp{M-1 0 M-c}would change the case of the following 10 characters, leaving the cursor onthe last character changed.@example/* Invert the case of the COUNT following characters. */intinvert_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);@}@end example@node Custom Completers@section Custom CompletersTypically, a program that reads commands from the user has a way ofdisambiguating commands and data. If your program is one of these, thenit can provide completion for commands, data, or both.The following sections describe how your program and Readlinecooperate to provide this service.@menu* How Completing Works:: The logic used to do completion.* Completion Functions:: Functions provided by Readline.* Completion Variables:: Variables which control completion.* A Short Completion Example:: An example of writing completer subroutines.@end menu@node How Completing Works@subsection How Completing WorksIn order to complete some text, the full list of possible completionsmust be available. That is, it is not possible to accuratelyexpand a partial word without knowing all of the possible wordswhich make sense in that context. The Readline library providesthe user interface to completion, and two of the most commoncompletion functions: filename and username. For completing other typesof text, you must write your own completion function. This sectiondescribes exactly what such functions must do, and provides an example.There are three major functions used to perform completion:@enumerate@itemThe user-interface function @code{rl_complete ()}. This function iscalled with the same arguments as other Readlinefunctions intended for interactive use: @var{count} and@var{invoking_key}. It isolates the word to be completed and calls@code{completion_matches ()} to generate a list of possible completions.It then either lists the possible completions, inserts the possiblecompletions, or actually performs thecompletion, depending on which behavior is desired.@itemThe internal function @code{completion_matches ()} uses your@dfn{generator} function to generate the list of possible matches, andthen returns the array of these matches. You should place the addressof your generator function in @code{rl_completion_entry_function}.@itemThe generator function is called repeatedly from@code{completion_matches ()}, returning a string each time. Thearguments to the generator function are @var{text} and @var{state}.@var{text} is the partial word to be completed. @var{state} is zero thefirst time the function is called, allowing the generator to performany necessary initialization, and a positive non-zero integer foreach subsequent call. When the generator function returns@code{(char *)NULL} this signals @code{completion_matches ()} that there areno more possibilities left. Usually the generator function computes thelist of possible completions when @var{state} is zero, and returns themone at a time on subsequent calls. Each string the generator functionreturns as a match must be allocated with @code{malloc()}; Readlinefrees the strings when it has finished with them.@end enumerate@deftypefun int rl_complete (int ignore, int invoking_key)Complete the word at or before point. You have supplied the functionthat does the initial simple matching selection algorithm (see@code{completion_matches ()}). The default is to do filename completion.@end deftypefun@deftypevar {Function *} rl_completion_entry_functionThis is a pointer to the generator function for @code{completion_matches()}. If the value of @code{rl_completion_entry_function} is@code{(Function *)NULL} then the default filename generator function,@code{filename_entry_function ()}, is used.@end deftypevar@node Completion Functions@subsection Completion FunctionsHere is the complete list of callable completion functions present inReadline.@deftypefun int rl_complete_internal (int what_to_do)Complete the word at or before point. @var{what_to_do} says what to dowith the completion. A value of @samp{?} means list the possiblecompletions. @samp{TAB} means do standard completion. @samp{*} meansinsert all of the possible completions. @samp{!} means to displayall of the possible completions, if there is more than one, as well asperforming partial completion.@end deftypefun@deftypefun int rl_complete (int ignore, int invoking_key)Complete the word at or before point. You have supplied the functionthat does the initial simple matching selection algorithm (see@code{completion_matches ()} and @code{rl_completion_entry_function}).The default is to do filenamecompletion. This calls @code{rl_complete_internal ()} with anargument depending on @var{invoking_key}.@end deftypefun@deftypefun int rl_possible_completions (int count, int invoking_key))List the possible completions. See description of @code{rl_complete()}. This calls @code{rl_complete_internal ()} with an argument of@samp{?}.@end deftypefun@deftypefun int rl_insert_completions (int count, int invoking_key))Insert the list of possible completions into the line, deleting thepartially-completed word. See description of @code{rl_complete ()}.This calls @code{rl_complete_internal ()} with an argument of @samp{*}.@end deftypefun@deftypefun {char **} completion_matches (char *text, CPFunction *entry_func)Returns an array of @code{(char *)} which is a list of completions for@var{text}. If there are no completions, returns @code{(char **)NULL}.The first entry in the returned array is the substitution for @var{text}.The remaining entries are the possible completions. The array isterminated with a @code{NULL} pointer.@var{entry_func} is a function of two args, and returns a@code{(char *)}. The first argument is @var{text}. The second is astate argument; it is zero on the first call, and non-zero on subsequentcalls. @var{entry_func} returns a @code{NULL} pointer to the callerwhen there are no more matches.@end deftypefun@deftypefun {char *} filename_completion_function (char *text, int state)A generator function for filename completion in the general case. Notethat completion in Bash is a little different because of allthe pathnames that must be followed when looking up completions for acommand. The Bash source is a useful reference for writing customcompletion functions.@end deftypefun@deftypefun {char *} username_completion_function (char *text, int state)A completion generator for usernames. @var{text} contains a partialusername preceded by a random character (usually @samp{~}). As with allcompletion generators, @var{state} is zero on the first call and non-zerofor subsequent calls.@end deftypefun@node Completion Variables@subsection Completion Variables@deftypevar {Function *} rl_completion_entry_functionA pointer to the generator function for @code{completion_matches ()}.@code{NULL} means to use @code{filename_entry_function ()}, the defaultfilename completer.@end deftypevar@deftypevar {CPPFunction *} rl_attempted_completion_functionA pointer to an alternative function to create matches.The function is called with @var{text}, @var{start}, and @var{end}.@var{start} and @var{end} are indices in @code{rl_line_buffer} sayingwhat the boundaries of @var{text} are. If this function exists andreturns @code{NULL}, or if this variable is set to @code{NULL}, then@code{rl_complete ()} will call the value of@code{rl_completion_entry_function} to generate matches, otherwise thearray of strings returned will be used.@end deftypevar@deftypevar int rl_completion_query_itemsUp to this many items will be displayed in response to apossible-completions call. After that, we ask the user if she is sureshe wants to see them all. The default value is 100.@end deftypevar@deftypevar {char *} rl_basic_word_break_charactersThe basic list of characters that signal a break between words for thecompleter routine. The default value of this variable is the characterswhich break words for completion in Bash, i.e.,@code{" \t\n\"\\'`@@$><=;|&@{("}.@end deftypevar@deftypevar {char *} rl_completer_word_break_charactersThe list of characters that signal a break between words for@code{rl_complete_internal ()}. The default list is the value of@code{rl_basic_word_break_characters}.@end deftypevar@deftypevar {char *} rl_completer_quote_charactersList of characters which can be used to quote a substring of the line.Completion occurs on the entire substring, and within the substring@code{rl_completer_word_break_characters} are treated as any other character,unless they also appear within this list.@end deftypevar@deftypevar {char *} rl_special_prefixesThe list of characters that are word break characters, but should beleft in @var{text} when it is passed to the completion function.Programs can use this to help determine what kind of completing to do.For instance, Bash sets this variable to "$@@" so that it can completeshell variables and hostnames.@end deftypevar@deftypevar int rl_ignore_completion_duplicatesIf non-zero, then disallow duplicates in the matches. Default is 1.@end deftypevar@deftypevar int rl_filename_completion_desiredNon-zero means that the results of the matches are to be treated asfilenames. This is @emph{always} zero on entry, and can only be changedwithin a completion entry generator function. If it is set to a non-zero
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -