Skip to main content

25.9.6 File Name Completion

This section describes low-level subroutines for completing a file name. For higher level functions, see Reading File Names.

function file-name-all-completions partial-filename directory​

This function returns a list of all possible completions for a file whose name starts with partial-filename in directory directory. The order of the completions is the order of the files in the directory, which is unpredictable and conveys no useful information.

The argument partial-filename must be a file name containing no directory part and no slash (or backslash on some systems). The current buffer’s default directory is prepended to directory, if directory is not absolute.

In the following example, suppose that ~rms/lewis is the current default directory, and has five files whose names begin with β€˜f’: foo, file~, file.c, file.c.~1~, and file.c.~2~.

(file-name-all-completions "f" "")
β‡’ ("foo" "file~" "file.c.~2~"
"file.c.~1~" "file.c")
(file-name-all-completions "fo" "")
β‡’ ("foo")

function file-name-completion filename directory \&optional predicate​

This function completes the file name filename in directory directory. It returns the longest prefix common to all file names in directory directory that start with filename. If predicate is non-nil then it ignores possible completions that don’t satisfy predicate, after calling that function with one argument, the expanded absolute file name.

If only one match exists and filename matches it exactly, the function returns t. The function returns nil if directory directory contains no name starting with filename.

In the following example, suppose that the current default directory has five files whose names begin with β€˜f’: foo, file~, file.c, file.c.~1~, and file.c.~2~.

(file-name-completion "fi" "")
β‡’ "file"
(file-name-completion "file.c.~1" "")
β‡’ "file.c.~1~"
(file-name-completion "file.c.~1~" "")
β‡’ t
(file-name-completion "file.c.~3" "")
β‡’ nil

user option completion-ignored-extensions​

file-name-completion usually ignores file names that end in any string in this list. It does not ignore them when all the possible completions end in one of these suffixes. This variable has no effect on file-name-all-completions.

A typical value might look like this:

completion-ignored-extensions
β‡’ (".o" ".elc" "~" ".dvi")

If an element of completion-ignored-extensions ends in a slash β€˜/’, it signals a directory. The elements which do not end in a slash will never match a directory; thus, the above value will not filter out a directory named foo.elc.