⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rltech.texinfo

📁 早期freebsd实现
💻 TEXINFO
📖 第 1 页 / 共 3 页
字号:
@comment %**start of header (This is for running Texinfo on a region.)@setfilename rltech.info@comment %**end of header (This is for running Texinfo on a region.)@setchapternewpage odd@ifinfoThis document describes the GNU Readline Library, a utility for aidingin the consitency of user interface across discrete programs that needto provide a command line interface.Copyright (C) 1988 Free Software Foundation, Inc.Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticepare preserved on all copies.@ignorePermission is granted to process this file through TeX and print theresults, provided the printed document carries copying permissionnotice identical to this one except for the removal of this paragraph(this paragraph not being relevant to the printed manual).@end ignorePermission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided that the entireresulting derived work is distributed under the terms of a permissionnotice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions,except that this permission notice may be stated in a translation approvedby the Foundation.@end ifinfo@node Programming with GNU Readline@chapter Programming with GNU ReadlineThis manual describes the interface between the GNU Readline Library anduser programs.  If you are a programmer, and you wish to include thefeatures found in GNU Readline in your own programs, such as completion,line editing, and interactive history manipulation, this documentationis for you.@menu* Default Behaviour::	Using the default behaviour of Readline.* Custom Functions::	Adding your own functions to Readline.* Custom Completers::	Supplanting or supplementing Readline's			completion functions.@end menu@node Default Behaviour@section Default BehaviourMany programs provide a command line interface, such as @code{mail},@code{ftp}, and @code{sh}.  For such programs, the default behaviour ofReadline is sufficient.  This section describes how to use Readline inthe simplest way possible, perhaps to replace calls in your code to@code{gets ()}.@findex readline ()@cindex readline, functionThe function @code{readline} prints a prompt and then reads and returnsa single line of text from the user.  The line which @code{readline ()}returns is allocated with @code{malloc ()}; you should @code{free ()}the line when you are done with it.  The declaration for @code{readline}in ANSI C is@example@code{char *readline (char *@var{prompt});}@end exampleSo, one might say@example@code{char *line = readline ("Enter a line: ");}@end examplein order to read a line of text from the user.The line which is returned has the final newline removed, so only thetext of the line remains.If readline encounters an @code{EOF} while reading the line, and theline is empty at that point, then @code{(char *)NULL} is returned.Otherwise, the line is ended just as if a newline was typed.If you want the user to be able to get at the line later, (with@key{C-p} for example), you must call @code{add_history ()} to save theline away in a @dfn{history} list of such lines.@example@code{add_history (line)};@end exampleFor full details on the GNU History Library, see the associated manual.It is polite to avoid saving empty lines on the history list, since itis rare than someone has a burning need to reuse a blank line.  Here isa function which usefully replaces the standard @code{gets ()} libraryfunction:@example/* 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 *do_gets ()@{  /* If the buffer has already been allocated, return the memory     to the free pool. */  if (line_read != (char *)NULL)    @{      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);@}@end exampleThe above code gives the user the default behaviour of @key{TAB}completion: completion on file names.  If you do not want readline tocomplete on filenames, you can change the binding of the @key{TAB} keywith @code{rl_bind_key ()}.@findex rl_bind_key ()@example@code{int rl_bind_key (int @var{key}, int (*@var{function})());}@end example@code{rl_bind_key ()} takes 2 arguments; @var{key} is the character thatyou want to bind, and @var{function} is the address of the function torun when @var{key} is pressed.  Binding @key{TAB} to @code{rl_insert ()}makes @key{TAB} just insert itself.@code{rl_bind_key ()} returns non-zero if @var{key} is not a validASCII character code (between 0 and 255).@example@code{rl_bind_key ('\t', rl_insert);}@end exampleThis code should be executed once at the start of your program; youmight write a function called @code{initialize_readline ()} whichperforms this and other desired initializations, such as installingcustom completers, etc.@node Custom Functions@section Custom FunctionsReadline provides a great many functions for manipulating the text ofthe line.  But it isn't possible to anticipate the needs of allprograms.  This section describes the various functions and variablesdefined in within the Readline library which allow a user program to addcustomized functionality to Readline.@menu* The Function Type::	C declarations to make code readable.* Function Naming::	How to give a function you write a name.* Keymaps::		Making keymaps.* Binding Keys::	Changing Keymaps.* Function Writing::	Variables and calling conventions.* Allowing Undoing::	How to make your functions undoable.@end menu@node The Function Type@subsection The Function TypeFor the sake of readabilty, we declare a new type of object, called@dfn{Function}.  A @code{Function} is a C language function whichreturns an @code{int}.  The type declaration for @code{Function} is:@noindent@code{typedef int Function ();}The reason for declaring this new type is to make it easier to writecode describing pointers to C functions.  Let us say we had a variablecalled @var{func} which was a pointer to a function.  Instead of theclassic C declaration@code{int (*)()func;}we have@code{Function *func;}@node Function Naming@subsection Naming a FunctionThe user can dynamically change the bindings of keys while usingReadline.  This is done by representing the function with a descriptivename.  The user is able to type the descriptive name when referring tothe function.  Thus, in an init file, one might find@exampleMeta-Rubout:	backward-kill-word@end exampleThis binds the keystroke @key{Meta-Rubout} to the function@emph{descriptively} named @code{backward-kill-word}.  You, as theprogrammer, should bind the functions you write to descriptive names aswell.  Readline provides a function for doing that:@defun rl_add_defun (char *name, Function *function, int key)Add @var{name} to the list of named functions.  Make @var{function} bethe function that gets called.  If @var{key} is not -1, then bind it to@var{function} using @code{rl_bind_key ()}.@end defunUsing this function alone is sufficient for most applications.  It isthe recommended way to add a few functions to the default functions thatReadline has built in already.  If you need to do more or differentthings than adding a function to Readline, you may need to use theunderlying functions described below.@node Keymaps@subsection Selecting a KeymapKey bindings take place on a @dfn{keymap}.  The keymap is theassociation between the keys that the user types and the functions thatget run.  You can make your own keymaps, copy existing keymaps, and tellReadline which keymap to use.@defun {Keymap rl_make_bare_keymap} ()Returns a new, empty keymap.  The space for the keymap is allocated with@code{malloc ()}; you should @code{free ()} it when you are done.@end defun@defun {Keymap rl_copy_keymap} (Keymap map)Return a new keymap which is a copy of @var{map}.@end defun@defun {Keymap rl_make_keymap} ()Return a new keymap with the printing characters bound to rl_insert,the lowercase Meta characters bound to run their equivalents, andthe Meta digits bound to produce numeric arguments.@end defun@node Binding Keys@subsection Binding KeysYou associate keys with functions through the keymap.  Here arefunctions for doing that.@defun {int rl_bind_key} (int key, Function *function)Binds @var{key} to @var{function} in the currently selected keymap.Returns non-zero in the case of an invalid @var{key}.@end defun@defun {int rl_bind_key_in_map} (int key, Function *function, Keymap map)Bind @var{key} to @var{function} in @var{map}.  Returns non-zero in the caseof an invalid @var{key}.@end defun@defun {int rl_unbind_key} (int key)Make @var{key} do nothing in the currently selected keymap.Returns non-zero in case of error.@end defun@defun {int rl_unbind_key_in_map} (int key, Keymap map)Make @var{key} be bound to the null function in @var{map}.Returns non-zero in case of error.@end defun@defun rl_generic_bind (int type, char *keyseq, char *data, Keymap map)Bind the key sequence represented by the string @var{keyseq} to the arbitrarypointer @var{data}.  @var{type} says what kind of data is pointed to by@var{data}; right now this can be a function (@code{ISFUNC}), a macro(@code{ISMACR}), or a keymap (@code{ISKMAP}).  This makes new keymaps asnecessary.  The initial place to do bindings is in @var{map}.@end defun@node Function Writing@subsection Writing a New FunctionIn order to write new functions for Readline, you need to know thecalling conventions for keyboard invoked functions, and the names of thevariables that describe the current state of the line gathered so far.@defvar {char *rl_line_buffer}This is the line gathered so far.  You are welcome to modify thecontents of this, but see Undoing, below.@end defvar@defvar {int rl_point}The offset of the current cursor position in @var{rl_line_buffer}.@end defvar@defvar {int rl_end}The number of characters present in @code{rl_line_buffer}.  When@code{rl_point} is at the end of the line, then @code{rl_point} and@code{rl_end} are equal.@end defvarThe calling sequence for a command @code{foo} looks like@example@code{foo (int count, int key)}@end examplewhere @var{count} is the numeric argument (or 1 if defaulted) and@var{key} is the key that invoked this function.It is completely up to the function as to what should be done with thenumeric argument; some functions use it as a repeat count, otherfunctions as a flag, and some choose to ignore it.  In general, if afunction uses the numeric argument as a repeat count, it should be ableto do something useful with a negative argument as well as a positiveargument.  At the very least, it should be aware that it can be passed anegative argument.@node Allowing Undoing@subsection Allowing UndoingSupporting the undo command is a painless thing to do, and makes yourfunctions much more useful to the end user.  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, and itcalls @code{rl_insert_text ()} or @code{rl_delete_text ()} to do it, thenundoing is already done for you automatically, and you can safely skipthis section.If you do multiple insertions or multiple deletions, or any combinationof these operations, you should group them together into one operation.This can be done with @code{rl_begin_undo_group ()} and@code{rl_end_undo_group ()}.@defun rl_begin_undo_group ()Begins saving undo information in a group construct.  The undoinformation usually comes from calls to @code{rl_insert_text ()} and

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -