Function: file-expand-wildcards

file-expand-wildcards is a byte-compiled function defined in files.el.gz.

Signature

(file-expand-wildcards PATTERN &optional FULL)

Documentation

Expand (a.k.a. "glob") file-name wildcard pattern PATTERN.

This returns a list of file names that match PATTERN. The returned list of file names is sorted in the string< order.

If PATTERN is written as an absolute file name, the expansions in the returned list are also absolute.

If PATTERN is written as a relative file name, it is interpreted relative to the current default-directory. The file names returned are normally also relative to the current default directory. However, if FULL is non-nil, they are absolute.

Other relevant functions are documented in the file group.

Probably introduced at or before Emacs version 20.4.

Shortdoc

;; file
(file-expand-wildcards "/tmp/*.png")
    e.g. => ("/tmp/foo.png" "/tmp/zot.png")

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun file-expand-wildcards (pattern &optional full)
  "Expand (a.k.a. \"glob\") file-name wildcard pattern PATTERN.
This returns a list of file names that match PATTERN.
The returned list of file names is sorted in the `string<' order.

If PATTERN is written as an absolute file name,
the expansions in the returned list are also absolute.

If PATTERN is written as a relative file name, it is interpreted
relative to the current `default-directory'.
The file names returned are normally also relative to the current
default directory.  However, if FULL is non-nil, they are absolute."
  (save-match-data
    (let* ((nondir (file-name-nondirectory pattern))
	   (dirpart (file-name-directory pattern))
	   ;; A list of all dirs that DIRPART specifies.
	   ;; This can be more than one dir
	   ;; if DIRPART contains wildcards.
	   (dirs (if (and dirpart
			  (string-match "[[*?]" (file-local-name dirpart)))
		     (mapcar 'file-name-as-directory
			     (file-expand-wildcards (directory-file-name dirpart)))
		   (list dirpart)))
	   contents)
      (dolist (dir dirs)
	(when (or (null dir)	; Possible if DIRPART is not wild.
		  (file-accessible-directory-p dir))
	  (let ((this-dir-contents
		 ;; Filter out "." and ".."
		 (delq nil
		       (mapcar #'(lambda (name)
				   (unless (string-match "\\`\\.\\.?\\'"
							 (file-name-nondirectory name))
				     name))
			       (directory-files (or dir ".") full
						(wildcard-to-regexp nondir))))))
	    (setq contents
		  (nconc
		   (if (and dir (not full))
		       (mapcar #'(lambda (name) (concat dir name))
			       this-dir-contents)
		     this-dir-contents)
		   contents)))))
      contents)))