📄 cpl_complete_word.3
字号:
.fi.spThe \f3cpl_complete_word()\f1 is normally called behind the scenes by\f3gl_get_line(3)\f1, but can also be called separately if youseparately allocate a \f3WordCompletion\f1 object. It performs wordcompletion, as described at the beginning of this section. Its firstargument is a resource object previously returned by\f3new_CompleteWord()\f1. The \f3line\f1 argument is the input linestring, containing the word to be completed. The \f3word_end\f1argument contains the index of the character in the input line, thatjust follows the last character of the word to be completed. Whencalled by \f3gl_get_line()\f1, this is the character over which theuser pressed \f3TAB\f1. The \f3match_fn\f3 argument is the functionpointer of the callback function which will lookup possiblecompletions of the word, as described above, and the \f3data\f1argument provides a way for the application to pass arbitrary data tothe callback function..spIf no errors occur, the \f3cpl_complete_word()\f1 function returns apointer to a \f3CplMatches\f1 container, as defined below. Thiscontainer is allocated as part of the \f3cpl\f1 object that was passedto \f3cpl_complete_word()\f1, and will thus change on each call whichuses the same \f3cpl\f1 argument..sp.nf typedef struct { char *completion; /* A matching completion */ /* string */ char *suffix; /* The part of the */ /* completion string which */ /* would have to be */ /* appended to complete the */ /* original word. */ const char *type_suffix; /* A suffix to be added when */ /* listing completions, to */ /* indicate the type of the */ /* completion. */ } CplMatch; typedef struct { char *suffix; /* The common initial part */ /* of all of the completion */ /* suffixes. */ const char *cont_suffix; /* Optional continuation */ /* string to be appended to */ /* the sole completion when */ /* nmatch==1. */ CplMatch *matches; /* The array of possible */ /* completion strings, */ /* sorted into lexical */ /* order. */ int nmatch; /* The number of elements in */ /* the above matches[] */ /* array. */ } CplMatches;.fi.spIf an error occurs during completion, \f3cpl_complete_word()\f1returns NULL. A description of the error can be acquired by callingthe \f3cpl_last_error()\f3 function..sp.nf const char *cpl_last_error(WordCompletion *cpl);.fi.spThe \f3cpl_last_error()\f3 function returns a terse description of theerror which occurred on the last call to \f3cpl_complete_word()\f1 or\f3cpl_add_completion()\f1..sp.nf int cpl_list_completions(CplMatches *result, FILE *fp, int terminal_width);.fi.spWhen the \f3cpl_complete_word()\f1 function returns multiple possiblecompletions, the \f3cpl_list_completions()\f1 function can be calledupon to list them, suitably arranged across the available width of theterminal. It arranges for the displayed columns of completions to allhave the same width, set by the longest completion. It also appendsthe \f3type_suffix\f1 strings that were recorded with each completion,thus indicating their types to the user..SH THE BUILT-IN FILENAME-COMPLETION CALLBACKBy default the \f3gl_get_line(3)\f1 function, passes the followingcompletion callback function to \f3cpl_complete_word()\f1. Thisfunction can also be used separately, either by sending it to\f3cpl_complete_word()\f1, or by calling it directly from yourown completion callback function..sp.nf CPL_MATCH_FN(cpl_file_completions);.fi.spCertain aspects of the behavior of this callback can be changed viaits \f3data\f1 argument. If you are happy with its default behavioryou can pass \f3NULL\f1 in this argument. Otherwise it should be apointer to a \f3CplFileConf\f1 object, previously allocated by calling\f3new_CplFileConf()\f1..sp.nf CplFileConf *new_CplFileConf(void);.fi.sp\f3CplFileConf\f1 objects encapsulate the configuration parameters of\f3cpl_file_completions()\f1. These parameters, which start out withdefault values, can be changed by calling the accessor functionsdescribed below..spBy default, the \f3cpl_file_completions()\f3 callback functionsearches backwards for the start of the filename being completed,looking for the first un-escaped space or the start of the inputline. If you wish to specify a different location, call\f3cfc_file_start()\f1 with the index at which the filename starts inthe input line. Passing start_index=-1 re-enables the defaultbehavior..sp.nf void cfc_file_start(CplFileConf *cfc, int start_index);.fi.spBy default, when \f3cpl_file_completions()\f1 looks at a filename inthe input line, each lone backslash in the input line is interpretedas being a special character which removes any special significance ofthe character which follows it, such as a space which should be takenas part of the filename rather than delimiting the start of thefilename. These backslashes are thus ignored while looking forcompletions, and subsequently added before spaces, tabs and literalbackslashes in the list of completions. To have unescaped backslashestreated as normal characters, call \f3cfc_literal_escapes()\f1 with anon-zero value in its \f3literal\f1 argument..sp.nf void cfc_literal_escapes(CplFileConf *cfc, int literal);.fi.spBy default, \f3cpl_file_completions()\f1 reports all files who's namesstart with the prefix that is being completed. If you only want aselected subset of these files to be reported in the list ofcompletions, you can arrange this by providing a callback functionwhich takes the full pathname of a file, and returns \f30\f1 if thefile should be ignored, or \f31\f1 if the file should be included inthe list of completions. To register such a function for use by\f3cpl_file_completions()\f1, call \f3cfc_set_check_fn()\f1, and passit a pointer to the function, together with a pointer to any data thatyou would like passed to this callback whenever it is called. Yourcallback can make its decisions based on any property of the file,such as the filename itself, whether the file is readable, writable orexecutable, or even based on what the file contains..sp.nf #define CPL_CHECK_FN(fn) int (fn)(void *data, \\ const char *pathname) typedef CPL_CHECK_FN(CplCheckFn); void cfc_set_check_fn(CplFileConf *cfc, CplCheckFn *chk_fn, void *chk_data);.fi.spThe \f3cpl_check_exe()\f1 function is a provided callback of the abovetype, for use with \f3cpl_file_completions()\f1. It returns non-zeroif the filename that it is given represents a normal file that theuser has execute permission to. You could use this to have\f3cpl_file_completions()\f1 only list completions of executablefiles..spWhen you have finished with a \f3CplFileConf\f1 variable, you can passit to the \f3del_CplFileConf()\f1 destructor function to reclaim itsmemory..sp.nf CplFileConf *del_CplFileConf(CplFileConf *cfc);.fi.sp.SH THREAD SAFETYIn multi-threaded programs, you should use the \f3libtecla_r.a\f1version of the library. This uses POSIX reentrant functions whereavailable (hence the \f3_r\f1 suffix), and disables features that relyon non-reentrant system functions. In the case of this module, theonly disabled feature is username completion in \f3~username/\f1expressions, in \f3cpl_file_completions()\f1.Using the \f3libtecla_r.a\f1 version of the library, it is safe to usethe facilities of this module in multiple threads, provided that eachthread uses a separately allocated \f3WordCompletion\f1 object. Inother words, if two threads want to do word completion, they shouldeach call \f3new_WordCompletion()\f1 to allocate their own completionobjects..SH FILES.nflibtecla.a - The tecla librarylibtecla.h - The tecla header file..fi.SH SEE ALSOlibtecla(3), gl_get_line(3), ef_expand_file(3), pca_lookup_file(3) .SH AUTHORMartin Shepherd (mcs@astro.caltech.edu)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -