Search code examples
emacsfilenamesglobcompletion

using wildcards in expand-file-name in elisp?


Is it possible to use wildcards such as * in elisp to expand filenames?

For example, I saw that

(expand-file-name "~/.emacs.d/elpa/babel-repl-20160504.1501")

expands to /home/username/.emacs.d/elpa/babel-repl-20160504.1501.

Is there a function to also expand filenames containing wildcards so that we can do:

(expand-file-name "~/.emacs.d/elpa/babel-repl*")

(taking the first filename if multiple matches exist)?

I tried the above, but expand-file-name does not seem to recognize * (Emacs 24.5 from Ubuntu 16.04)

-- EDIT --

@Drew I tried (byte-recompile-directory (file-expand-wildcards "~/.emacs.d/elpa/babel-repl*/") 0) but got an error Wrong type argument: stringp, nil.

I am not very familiar with elisp and the stringp type (a list of strings, I guess). I tried to use a (car ) on the returned value of file-expand-wildcards in order to get the first matched filename. But Emacs still work start correctly.

Any pointers?


Solution

  • Try file-expand-wildcards:

    file-expand-wildcards is a compiled Lisp function in files.el.

    (file-expand-wildcards PATTERN &optional FULL)

    Expand wildcard pattern PATTERN.

    This returns a list of file names which match the pattern.

    If PATTERN is written as an absolute file name, the values are absolute also.

    If PATTERN is written as a relative file name, it is interpreted relative to the current default directory, default-directory.

    The file names returned are normally also relative to the current default directory. However, if FULL is non-nil, they are absolute.

    That gives you a list of expansions. Pick the first one or any one you want.