From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Hrvoje Niksic Newsgroups: gmane.emacs.devel Subject: locate-file in Emacs Date: Wed, 17 Apr 2002 07:05:26 +0200 Sender: emacs-devel-admin@gnu.org Message-ID: NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1019019973 9270 127.0.0.1 (17 Apr 2002 05:06:13 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Wed, 17 Apr 2002 05:06:13 +0000 (UTC) Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.33 #1 (Debian)) id 16xhe1-0002PP-00 for ; Wed, 17 Apr 2002 07:06:13 +0200 Original-Received: from fencepost.gnu.org ([199.232.76.164]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 16xhwY-0008Du-00 for ; Wed, 17 Apr 2002 07:25:23 +0200 Original-Received: from localhost ([127.0.0.1] helo=fencepost.gnu.org) by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian)) id 16xhe2-0002bK-00; Wed, 17 Apr 2002 01:06:15 -0400 Original-Received: from dragon.arsdigita.de ([212.84.246.66] helo=florida.arsdigita.de) by fencepost.gnu.org with esmtp (Exim 3.34 #1 (Debian)) id 16xhdH-0002ZO-00 for ; Wed, 17 Apr 2002 01:05:28 -0400 Original-Received: from hniksic by florida.arsdigita.de with local (Exim 3.35 #1 (Debian)) id 16xhdG-0001WY-00 for ; Wed, 17 Apr 2002 07:05:26 +0200 Original-To: emacs-devel@gnu.org X-Attribution: Hrvoje X-Face: &{dT~)Pu6V<0y?>3p$;@vh\`C7xB~A0T-J%Og)J,@-1%q6Q+, gs<-9M#&`I8cJp2b1{vPE|~+JE+gx;a7%BG{}nY^ehK1"q#rG O,Rn1A_Cy%t]V=Brv7h List-Post: List-Subscribe: , List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.devel:2683 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:2683 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)))