📄 gl_get_line.3
字号:
An example of a key binding line in the configuration file isthe following..nf bind M-[2~ insert-mode.fiOn many keyboards, the above key sequence is generated when onepresses the \f3insert\f1 key, so with this keybinding, one can togglebetween the emacs-mode insert and overwrite modes by hitting onekey. One could also do it by typing out the above sequence ofcharacters one by one. As explained above, the \f3M-\f1 part of thissequence can be typed either by pressing the escape key before thefollowing key, or by pressing the Meta key at the same time as thefollowing key. Thus if you had set the above key binding, and theinsert key on your keyboard didn't generate the above key sequence,you could still type it in either of the following 2 ways..nf 1. Hit the escape key momentarily, then press '[', then '2', then finally '~'. 2. Press the meta key at the same time as pressing the '[' key, then press '2', then '~'..fiIf you set a keybinding for a key-sequence that is already bound to a function,the new binding overrides the old one. If in the new binding you omit the nameof the new function to bind to the key-sequence, the original binding becomesundefined..spStarting with versions of libtecla later than 1.3.3 it is now possibleto bind keysequences that begin with a printable character. Previouslykey-sequences were required to start with a control or meta character..spNote that the special keywords "up", "down", "left" and "right" referto the arrow keys, and are thus not treated as keysequences. So, forexample, to rebind the up and down arrow keys to use the historysearch mechanism instead of the simple history recall method, youcould place the following in your configuration file:.nf bind up history-search-backwards bind down history-search-backwards.fi.spTo unbind an existing binding, you can do this with the bind commandby omitting to name any action to rebind the key sequence to. Forexample, by not specifying an action function, the following commandunbinds the default beginning-of-line action from the ^A key sequence:.nf bind ^A.fi.SH ALTERNATE CONFIGURATION SOURCESAs mentioned above, by default users have the option of configuringthe behavior of \f3gl_get_line()\f1 via a configuration file called\f3\&.teclarc\f1 in their home directories. The fact that allapplications share this same configuration file is both an advantageand a disadvantage. In most cases it is an advantage, since itencourages uniformity, and frees the user from having to configureeach application separately. In some applications, however, thissingle means of configuration is a problem. This is particularly trueof embedded software, where there's no filesystem to read aconfiguration file from, and also in applications where a radicallydifferent choice of keybindings is needed to emulate a legacy keyboardinterface. To cater for such cases, the following function allows theapplication to control where configuration information is read from..sp.nf int gl_configure_getline(GetLine *gl, const char *app_string, const char *app_file, const char *user_file);.fi.spIt allows the configuration commands that would normally be read froma user's \f3~/.teclarc\f1 file, to be read from any or none of, astring, an application specific configuration file, and/or auser-specific configuration file. If this function is called beforethe first call to \f3gl_get_line()\f1, the default behavior ofreading \f3~/.teclarc\f1 on the first call to \f3gl_get_line()\f1 isdisabled, so all configuration must be achieved using theconfiguration sources specified with this function.If \f3app_string != NULL\f1, then it is interpreted as a stringcontaining one or more configuration commands, separated from eachother in the string by embedded newline characters. If \f3app_file !=NULL\f1 then it is interpreted as the full pathname of anapplication-specific configuration file. If \f3user_file != NULL\f1then it is interpreted as the full pathname of a user-specificconfiguration file, such as \f3~/.teclarc\f1. For example, in thefollowing call, gl_configure_getline(gl, "edit-mode vi \\n nobeep", "/usr/share/myapp/teclarc", "~/.teclarc");the \f3app_string\f1 argument causes the calling application to startin vi edit-mode, instead of the default emacs mode, and turns off theuse of the terminal bell by the library. It then attempts to readsystem-wide configuration commands from an optional file called\f3/usr/share/myapp/teclarc\f1, then finally reads user-specificconfiguration commands from an optional \f3\&.teclarc\f1 file in theuser's home directory. Note that the arguments are listed in ascendingorder of priority, with the contents of \f3app_string\f1 beingpotentially overriden by commands in \f3app_file\f1, and commands in\f3app_file\f1 potentially being overriden by commands in\f3user_file\f1..spYou can call this function as many times as needed, the results beingcumulative, but note that copies of any filenames specified via the\f3app_file\f1 and \f3user_file\f1 arguments are recorded internallyfor subsequent use by the \f3read-init-files\f1 key-binding function,so if you plan to call this function multiple times, be sure that thelast call specifies the filenames that you want re-read when the userrequests that the configuration files be re-read..SH FILENAME AND TILDE COMPLETIONWith the default key bindings, pressing the TAB key (aka. ^I) resultsin \f3gl_get_line()\f1 attempting to complete the incomplete filenamethat precedes the cursor. \f3gl_get_line()\f1 searches backwards fromthe cursor, looking for the start of the filename, stopping when ithits either a space or the start of the line. If more than one filehas the specified prefix, \f3gl_get_line()\f1 completes the filenameup to the point at which the ambiguous matches start to differ, thenlists the possible matches..spIn addition to literally written filenames, \f3gl_get_line()\f1 cancomplete files that start with \f3~/\f1 and \f3~user/\f1 expressionsand that contain \f3$envvar\f1 expressions. In particular, if you hitTAB within an incomplete \f3~user\f1, expression, \f3gl_get_line()\f1will attempt to complete the username, listing any ambiguous matches..spThe completion binding is implemented using the\f3cpl_word_completions()\f1 function, which is also availableseparately to users of this library. See the\f3cpl_word_completions(3)\f1 man page for more details..SH CUSTOMIZED WORD COMPLETIONIf in your application, you would like to have TAB completion completeother things in addition to or instead of filenames, you can arrangethis by registering an alternate completion callback function, via acall to the \f3gl_customize_completion()\f1 function..sp.nf int gl_customize_completion(GetLine *gl, void *data, CplMatchFn *match_fn);.fi.spThe \f3data\f1 argument provides a way for your application to passarbitrary, application-specific information to the callbackfunction. This is passed to the callback every time that it iscalled. It might for example, point to the symbol table from whichpossible completions are to be sought. The \f3match_fn\f1 argumentspecifies the callback function to be called. The \f3CplMatchFn\f1function type is defined in \f3libtecla.h\f1, as is a\f3CPL_MATCH_FN()\f1 macro that you can use to declare and prototypecallback functions. The declaration and responsibilities of callbackfunctions are described in depth in the \f1cpl_complete_word(3)\f1 manpage..spIn brief, the callback function is responsible for looking backwardsin the input line, back from the point at which the user pressed TAB,to find the start of the word being completed. It then must lookuppossible completions of this word, and record them one by one in the\f3WordCompletion\f1 object that is passed to it as an argument, bycalling the \f3cpl_add_completion()\f1 function. If the callbackfunction wishes to provide filename completion in addition to its ownspecific completions, it has the option of itself calling the builtinfile-name completion callback. This also, is documented in the\f3cpl_complete_word(3)\f1 man page..spNote that if you would like \f3gl_get_line()\f1 to return the currentinput line when a successful completion is been made, you can arrangethis when you call \f3cpl_add_completion()\f1, by making the lastcharacter of the continuation suffix a newline character. If you dothis, the input line will be updated to display the completion,together with any contiuation suffix up to the newline character, then\f3gl_get_line()\f1 will return this input line..SH FILENAME EXPANSIONWith the default key bindings, pressing \f3^X*\f1 causes\f3gl_get_line()\f1 to expand the filename that precedes the cursor,replacing \f3~/\f1 and \f3~user/\f1 expressions with the correspondinghome directories, and replacing \f3$envvar\f1 expressions with thevalue of the specified environment variable, then if there are anywildcards, replacing the so far expanded filename with aspace-separated list of the files which match the wild cards..spThe expansion binding is implemented using the \f3ef_expand_file()\f1 function.See the \f3ef_expand_file(3)\f1 man page for more details..SH RECALLING PREVIOUSLY TYPED LINESEvery time that a new line is entered by the user, it is appended to alist of historical input lines maintained within the GetLine resourceobject. You can traverse up and down this list using the up and downarrow keys. Alternatively, you can do the same with the \f3^P\f1, and\f3^N\f1 keys, and in vi command mode you can alternatively use the kand j characters. Thus pressing up-arrow once, replaces the currentinput line with the previously entered line. Pressing up-arrow again,replaces this with the line that was entered before it, etc.. Havinggone back one or more lines into the history list, one can return tonewer lines by pressing down-arrow one or more times. If you do thissufficient times, you will return to the original line that you wereentering when you first hit up-arrow..spNote that in vi mode, all of the history recall functions switch thelibrary into command mode..spIn emacs mode the \f3M-p\f1 and \f3M-n\f1 keys work just like the\f3^P\f1 and \f3^N\f1 keys, except that they skip all but thosehistorical lines which share the prefix that precedes the cursor. Invi command mode the upper case \f3K\f1 and \f3J\f1 characters do thesame thing, except that the string that they search for includes thecharacter under the cursor as well as what precedes it..spThus for example, suppose that you were in emacs mode, and you hadjust entered the following list of commands in the order shown:.nf ls ~/tecla/ cd ~/tecla ls -l getline.c emacs ~/tecla/getline.c.fiIf you next typed:.nf ls.fiand then hit \f3M-p\f1, then rather than returning the previouslytyped emacs line, which doesn't start with "ls", \f3gl_get_line()\f1would recall the "ls -l getline.c" line. Pressing \f3M-p\f1 againwould recall the "ls ~/tecla/" line..SH HISTORY FILESTo save the contents of the history buffer before quitting yourapplication, and subsequently restore them when you next start theapplication, the following functions are provided..sp.nf int gl_save_history(GetLine *gl, const char *filename, const char *comment, int max_lines); int gl_load_history(GetLine *gl, const char *filename, const char *comment);.fi.spThe \f3filename\f1 argument specifies the name to give the historyfile when saving, or the name of an existing history file, whenloading. This may contain home-directory and environment variableexpressions, such as "~/.myapp_history" or "$HOME/.myapp_history"..spAlong with each history line, extra information about it, such as whenit was entered by the user, and what its nesting level is, is recordedas a comment preceding the line in the history file. Writing this as acomment allows the history file to double as a command file, just incase you wish to replay a whole session using it. Since commentprefixes differ in different languages, the \f3comment\f1 argument isprovided for specifying the comment prefix. For example, if yourapplication were a unix shell, such as the bourne shell, you wouldspecify "#" here. Whatever you choose for the comment character, youmust specify the same prefix to \f3gl_load_history()\f1 that you usedwhen you called \f3gl_save_history()\f1 to write the history file..spThe \f3max_lines\f1 must be either -1 to specify that all lines in thehistory list be saved, or a positive number specifying a ceiling onhow many of the most recent lines should be saved..spBoth fuctions return non-zero on error, after writing an error messageto stderr. Note that \f3gl_load_history()\f1 does not consider thenon-existence of a file to be an error..SH MULTIPLE HISTORY LISTSIf your application uses a single \f3GetLine\f1 object for enteringmany different types of input lines, you may wish \f3gl_get_line()\f1to distinguish the different types of lines in the history list, andonly recall lines that match the current type of line. To support thisrequirement, \f3gl_get_line()\f1 marks lines being recorded in thehistory list with an integer identifier chosen by the application.Initially this identifier is set
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -