Function: ff-get-file-name

ff-get-file-name is a byte-compiled function defined in find-file.el.gz.

Signature

(ff-get-file-name SEARCH-DIRS FNAME-STUB &optional SUFFIX-LIST)

Documentation

Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.

If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise search for FNAME-STUB with each of the given suffixes. Return the name of the first file found.

Source Code

;; Defined in /usr/src/emacs/lisp/find-file.el.gz
(defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
  "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
search for FNAME-STUB with each of the given suffixes.  Return the
name of the first file found."
  (let (dirs         ;; working copy of dirs to search
	dir          ;; the current dir considered
	file         ;; filename being looked for
	rest         ;; pathname after first /*
	this-suffix  ;; the suffix we are currently considering
	suffixes     ;; working copy of suffix-list
	filename     ;; built filename
	blist        ;; list of live buffers
	buf          ;; current buffer in blist
	found)       ;; whether we have found anything

    (setq suffixes suffix-list)

    ;; suffixes is nil => fname-stub is the file we are looking for
    ;; otherwise fname-stub is a stub, and we append a suffix
    (if suffixes
        (setq this-suffix (car suffixes))
      (setq this-suffix "")
      (setq suffixes (list "")))

    ;; find whether the file is in a buffer first
    (while (and suffixes (not found))
      (setq filename (concat fname-stub this-suffix))

      (if (not ff-quiet-mode)
          (message "Finding buffer %s..." filename))

      (if (bufferp (get-file-buffer filename))
          (setq found (ff-buffer-file-name (get-file-buffer filename))))

      (setq blist (buffer-list))
      (setq buf (buffer-name (car blist)))
      (while (and blist (not found))

        (if (string-match-p (concat filename "<[0-9]+>") buf)
            (setq found (ff-buffer-file-name (car blist))))

        (setq blist (cdr blist))
        (setq buf (buffer-name (car blist))))

      (setq suffixes (cdr suffixes))
      (setq this-suffix (car suffixes)))

    ;; now look for the real file
    (setq dirs search-dirs)
    (setq dir  (car dirs))
    (while (and (not found) dirs)

      (setq suffixes suffix-list)

      ;; if dir does not contain '/*', look for the file
      (if (and dir (not (string-match "\\([^*]*\\)/\\*\\(/.*\\)*" dir)))
          (progn

            ;; suffixes is nil => fname-stub is the file we are looking for
            ;; otherwise fname-stub is a stub, and we append a suffix
            (if suffixes
                (setq this-suffix (car suffixes))
              (setq this-suffix "")
              (setq suffixes (list "")))

            (while (and suffixes (not found))

              (setq filename (concat fname-stub this-suffix))
              (setq file (expand-file-name filename dir))

              (if (not ff-quiet-mode)
                  (message "Finding %s..." file))

              (if (file-exists-p file)
                  (setq found file))

              (setq suffixes (cdr suffixes))
              (setq this-suffix (car suffixes))))

        ;; otherwise dir matches the '/*', so search each dir separately
        (progn
          (setq rest (if (match-beginning 2)
                         (match-string 2 dir)
                       ""))
          (setq dir  (match-string 1 dir))

          (let ((dirlist (ff-all-dirs-under dir '("..")))
                this-dir compl-dirs)

            (setq this-dir (car dirlist))
            (while dirlist
              (setq compl-dirs
                    (append
                     compl-dirs
                     (list (concat this-dir rest))
                     ))
              (setq dirlist  (cdr dirlist))
              (setq this-dir (car dirlist)))

            (if compl-dirs
                (setq found (ff-get-file-name compl-dirs
                                              fname-stub
                                              suffix-list))))))
      (setq dirs (cdr dirs))
      (setq dir (car dirs)))

    (if found
        (message "%s found" found))

    found))