all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* locate-file in Emacs
@ 2002-04-17  5:05 Hrvoje Niksic
  2002-04-17  5:47 ` Eli Zaretskii
  2002-04-17  9:28 ` Stefan Monnier
  0 siblings, 2 replies; 31+ messages in thread
From: Hrvoje Niksic @ 2002-04-17  5:05 UTC (permalink / raw)


Several years ago I talked to Richard Stallman about incorporating the
`locate-file' function in Emacs.  `locate-file' is a very useful
XEmacs function that searches for a file in a path.

For example, to find an elisp file in load-path, one would use this:

    (locate-file "simple" load-path '(".elc" ".el"))
      => "/usr/local/lib/xemacs-21.4.6/lisp/simple.elc"

Or, to locate a Unix executable:

    (locate-file "ls" (split-string (getenv "PATH") path-separator)
                 nil 'executable)
      => "/bin/ls"

To make the same code work under Windows:

    (locate-file "links" (split-string (getenv "PATH") path-separator)
                 '("" ".exe") 'executable)
      => "/usr/bin/links"

I find that there are many uses for this kind of lookup across a list
of directories, and I would like to be able to use this function in
portable elisp programs.  The XEmacs version is written in C with
(probably) unclear copyright status, but Richard agreed to include a
Lisp version of the function.  I came back to that yesterday and wrote
one.

The code follows below.  Please let me know what you think.


(defun locate-file (filename path-list &optional suffixes mode)
  "Search for FILENAME through PATH-LIST.

If SUFFIXES is non-nil, it should be a list of suffixes to append to
file name when searching.

If MODE is non-nil, it should be a symbol or a list of symbols representing
requirements.  Allowed symbols are `exists', `executable', `writable', and
`readable'.  If MODE is nil, it defaults to `readable'."
  (let (all-file-names all-mode-functions)
    ;; Create a list of strings of FILENAME+suffix for each of
    ;; SUFFIXES, so we don't have to do it (and cons a new string)
    ;; once for each directory.
    (setq all-file-names
	  (if suffixes
	      (mapcar (lambda (suffix)
			(concat filename suffix))
		      suffixes)
	    (list filename)))

    ;; Convert MODE into a list of tests all of which need to return t
    ;; for a file to pass.
    (if (null mode)
	(setq all-mode-functions '(file-readable-p))
      (when (symbolp mode)
	(setq mode (list mode)))
      (setq all-mode-functions
	    (mapcar
	     (lambda (m)
	       (cond ((eq m 'exists)
		      'file-exists-p)
		     ((eq m 'executable)
		      'file-executable-p)
		     ((eq m 'writable)
		      ;; file-writable-p returns t if the dir is
		      ;; writable and the file doesn't exist.
		      (lambda (f)
			(and (file-exists-p f)
			     (file-writable-p f))))
		     ((eq m 'readable)
		      'file-readable-p)
		     (t
		      (error "Invalid mode: %s" m))))
	     mode)))

    (catch 'found
      (dolist (directory path-list)
	(dolist (file all-file-names)
	  (let ((full-name (expand-file-name file directory))
		(mode-functions all-mode-functions))
	    (while (and mode-functions
			(funcall (car mode-functions) full-name))
	      (pop mode-functions))
	    (when (null mode-functions)
	      ;; All functions passed -- we found the one.
	      (throw 'found full-name)))))
      nil)))

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2002-05-07 20:06 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-04-17  5:05 locate-file in Emacs Hrvoje Niksic
2002-04-17  5:47 ` Eli Zaretskii
2002-04-17  8:53   ` Hrvoje Niksic
2002-04-17  9:28 ` Stefan Monnier
2002-04-17  9:47   ` Hrvoje Niksic
2002-04-17 10:01     ` Stefan Monnier
2002-04-18 13:16       ` Hrvoje Niksic
2002-04-18 13:54         ` Miles Bader
2002-04-18 14:09           ` Hrvoje Niksic
2002-04-18 14:25             ` Miles Bader
2002-04-18 14:08         ` Stefan Monnier
2002-04-18 15:11           ` Hrvoje Niksic
2002-04-18 15:27             ` Stefan Monnier
2002-04-18 16:20               ` Hrvoje Niksic
2002-04-18 18:59                 ` Stefan Monnier
2002-04-25 12:12                   ` Hrvoje Niksic
2002-04-25 22:52                     ` Stefan Monnier
2002-05-06 15:58                       ` Hrvoje Niksic
     [not found]                         ` <200205061655.g46Gt3K01382@rum.cs.yale.edu>
2002-05-06 19:53                           ` Hrvoje Niksic
     [not found]                             ` <200205062052.g46KqwO02482@rum.cs.yale.edu>
2002-05-06 20:59                               ` Hrvoje Niksic
2002-05-06 21:12                                 ` Stefan Monnier
2002-05-06 21:57                                   ` Hrvoje Niksic
2002-05-07 20:06                                     ` Richard Stallman
2002-04-18 16:39               ` Eli Zaretskii
2002-04-18 16:24                 ` Hrvoje Niksic
2002-04-19  5:25               ` Richard Stallman
2002-04-18 15:31             ` Miles Bader
2002-04-18 16:29               ` Hrvoje Niksic
2002-04-18 16:45                 ` Miles Bader
2002-04-18 16:49                   ` Hrvoje Niksic
2002-04-18 16:54                     ` Miles Bader

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.