The following completion functions have nothing in themselves to do with minibuffers. We describe them here to keep them near the higher-level completion features that do use the minibuffer.
This function returns the longest common substring of all possible completions of string in collection. The value of collection must be a list of strings or symbols, an alist, an obarray, a hash table, or a completion function (see Programmed Completion).
Completion compares string against each of the permissible completions specified by collection. If no permissible completions match,
try-completionreturnsnil. If there is just one matching completion, and the match is exact, it returnst. Otherwise, it returns the longest initial sequence common to all possible matching completions.If collection is an alist (see Association Lists), the permissible completions are the elements of the alist that are either strings, symbols, or conses whose car is a string or symbol. Symbols are converted to strings using
symbol-name. Other elements of the alist are ignored. (Remember that in Emacs Lisp, the elements of alists do not have to be conses.) In particular, a list of strings or symbols is allowed, even though we usually do not think of such lists as alists.If collection is an obarray (see Creating Symbols), the names of all symbols in the obarray form the set of permissible completions. The global variable
obarrayholds an obarray containing the names of all interned Lisp symbols.Note that the only valid way to make a new obarray is to create it empty and then add symbols to it one by one using
intern. Also, you cannot intern a given symbol in more than one obarray.If collection is a hash table, then the keys that are strings are the possible completions. Other keys are ignored.
You can also use a symbol that is a function as collection. Then the function is solely responsible for performing completion;
try-completionreturns whatever this function returns. The function is called with three arguments: string, predicate andnil(the reason for the third argument is so that the same function can be used inall-completionsand do the appropriate thing in either case). See Programmed Completion.If the argument predicate is non-
nil, then it must be a function of one argument, unless collection is a hash table, in which case it should be a function of two arguments. It is used to test each possible match, and the match is accepted only if predicate returns non-nil. The argument given to predicate is either a string or a cons cell (the car of which is a string) from the alist, or a symbol (not a symbol name) from the obarray. If collection is a hash table, predicate is called with two arguments, the string key and the associated value.In addition, to be acceptable, a completion must also match all the regular expressions in
completion-regexp-list. (Unless collection is a function, in which case that function has to handlecompletion-regexp-listitself.)In the first of the following examples, the string ‘foo’ is matched by three of the alist cars. All of the matches begin with the characters ‘fooba’, so that is the result. In the second example, there is only one possible match, and it is exact, so the value is
t.(try-completion "foo" '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))) ⇒ "fooba" (try-completion "foo" '(("barfoo" 2) ("foo" 3))) ⇒ tIn the following example, numerous symbols begin with the characters ‘forw’, and all of them begin with the word ‘forward’. In most of the symbols, this is followed with a ‘-’, but not in all, so no more than ‘forward’ can be completed.
(try-completion "forw" obarray) ⇒ "forward"Finally, in the following example, only two of the three possible matches pass the predicate
test(the string ‘foobaz’ is too short). Both of those begin with the string ‘foobar’.(defun test (s) (> (length (car s)) 6)) ⇒ test (try-completion "foo" '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)) 'test) ⇒ "foobar"
This function returns a list of all possible completions of string. The arguments to this function (aside from nospace) are the same as those of
try-completion. Also, this function usescompletion-regexp-listin the same way thattry-completiondoes.The optional argument nospace is obsolete. If it is non-
nil, completions that start with a space are ignored unless string starts with a space.If collection is a function, it is called with three arguments: string, predicate and
t; thenall-completionsreturns whatever the function returns. See Programmed Completion.Here is an example, using the function
testshown in the example fortry-completion:(defun test (s) (> (length (car s)) 6)) ⇒ test (all-completions "foo" '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4)) 'test) ⇒ ("foobar1" "foobar2")
This function returns non-
nilif string is a valid completion possibility specified by collection and predicate. The arguments are the same as intry-completion. For instance, if collection is a list of strings, this is true if string appears in the list and predicate is satisfied.This function uses
completion-regexp-listin the same way thattry-completiondoes.If predicate is non-
niland if collection contains several strings that are equal to each other, as determined bycompare-stringsaccording tocompletion-ignore-case, then predicate should accept either all or none of them. Otherwise, the return value oftest-completionis essentially unpredictable.If collection is a function, it is called with three arguments, the values string, predicate and
lambda; whatever it returns,test-completionreturns in turn.
This function returns the boundaries of the field on which collection will operate, assuming that string holds the text before point and suffix holds the text after point.
Normally completion operates on the whole string, so for all normal collections, this will always return
(0 . (lengthsuffix)). But more complex completion such as completion on files is done one field at a time. For example, completion of"/usr/sh"will include"/usr/share/"but not"/usr/share/doc"even if"/usr/share/doc"exists. Alsoall-completionson"/usr/sh"will not include"/usr/share/"but only"share/". So if string is"/usr/sh"and suffix is"e/doc",completion-boundarieswill return(5 . 1)which tells us that the collection will only return completion information that pertains to the area after"/usr/"and before"/doc".
If you store a completion alist in a variable, you should mark the
variable as “risky” with a non-nil
risky-local-variable property. See File Local Variables.
If the value of this variable is non-
nil, Emacs does not consider case significant in completion. Note, however, that this variable is overridden byread-file-name-completion-ignore-casewithinread-file-name(see Reading File Names), and byread-buffer-completion-ignore-casewithinread-buffer(see High-Level Completion).
This is a list of regular expressions. The completion functions only consider a completion acceptable if it matches all regular expressions in this list, with
case-fold-search(see Searching and Case) bound to the value ofcompletion-ignore-case.
This macro provides a way to initialize the variable var as a collection for completion in a lazy way, not computing its actual contents until they are first needed. You use this macro to produce a value that you store in var. The actual computation of the proper value is done the first time you do completion using var. It is done by calling fun with no arguments. The value fun returns becomes the permanent value of var.
Here is an example of use:
(defvar foo (lazy-completion-table foo make-my-alist))
The function completion-in-region provides a convenient way to
perform completion on an arbitrary stretch of text in an Emacs buffer:
This function completes the text in the current buffer between the positions start and end, using collection. The argument collection has the same meaning as in
try-completion(see Basic Completion).This function inserts the completion text directly into the current buffer. Unlike
completing-read(see Minibuffer Completion), it does not activate the minibuffer.For this function to work, point must be somewhere between start and end.