Article View: gnu.emacs.sources
Article #304pathfind.el: Path finding functions
From: Piercarlo Grandi
Date: Mon, 04 Jan 1993 03:03
Date: Mon, 04 Jan 1993 03:03
85 lines
3272 bytes
3272 bytes
Here is a simple set of path finding functions, with which I have
added path finding to the Info reader. Version 19 will have a similar
facility, but in the meantime here is mine.
Please note the unusual indentation style, and that I have provided
"executable" comments for exemplification and testing. This is a
smalltalky sort of thing, and should be more common.
;;; Piercarlo Grandi, Aberystwyth, 1992
;; This function will return a pathname if it exists with any of the
;; given suffixes. It does not check for existence of the pathname
;; without any suffix.
; EXAMPLES:
; (path-suffixed-p "/usr/include/types" '(".h"))
; (path-suffixed-p "/usr/include/tar" ".h")
(defun path-suffixed-p (pathname suffixes)
(if (null suffixes) nil
(if (atom suffixes) (setq suffixes (list suffixes)))
(let ((suffixed (concat pathname (car suffixes))))
(if (file-exists-p suffixed) suffixed
(path-suffixed-p pathname (cdr suffixes))
)
)
)
)
;; This function will return a complete pathname composed of the prefix
;; followed by the name, followed _optionally_ by one of the suffixes,
;; if such a pathname exist. Prefix and suffix lists are scanned strictly
;; left to right.
; EXAMPLES:
; (path-exists-p "/usr/include" "tar.h" ".h")
; (path-exists-p "/usr/include" "tar" ".h")
; (path-exists-p "/usr/include" "types" ".h")
(defun path-exists-p (prefix name &optional suffixes)
(let ((pathname (expand-file-name name prefix)))
(if (file-exists-p pathname) pathname
(if (null suffixes) nil (path-suffixed-p pathname suffixes))
)
)
)
;; This function will return a complete pathname built by concatenating
;; one of the suffixes with the name with one of the suffixes,
;; if it exists and the given test applied to it is satisfied.
; EXAMPLES:
; (path-find-file '("/usr/include" "/usr/include/sys") "tar.h" ".h" nil)
; (path-find-file '("/usr/include" "/usr/include/sys") "types" ".h" 'file-exists-p)
; (path-find-file load-path "debug" ".elc" nil)
; (path-find-file load-path "calc" '(".elc" ".el"))
(defun path-find-file (prefixes name &optional suffixes test)
(if (null prefixes) nil
(if (atom prefixes) (setq prefixes (list prefixes)))
(let
(
(pathname
(path-suffixed-p
(expand-file-name name (car prefixes))
(or suffixes '(""))
)
)
)
(if (and pathname (or (null test) (apply test (list pathname))))
pathname
(path-find-file (cdr prefixes) name suffixes test)
)
)
)
)
(provide 'pathfind)
--
Piercarlo Grandi, Dept of CS, PC/UW@Aberystwyth <pcg@aber.ac.uk>
E l'italiano cantava, cantava. E le sue disperate invocazioni giunsero
alle orecchie del suo divino protettore, il dio della barzelletta
Technical Details
Message-ID:
<PCG.93Jan4030314@decb.aber.ac.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!gdt!aber!fronta.aber.ac.uk!pcgShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!gdt!aber!fronta.aber.ac.uk!pcg From: pcg@aber.ac.uk (Piercarlo Grandi) Newsgroups: gnu.emacs.sources Subject: pathfind.el: Path finding functions Message-ID: <PCG.93Jan4030314@decb.aber.ac.uk> Date: 4 Jan 93 03:03:14 GMT Sender: news@aber.ac.uk (USENET news service) Reply-To: pcg@aber.ac.uk (Piercarlo Grandi) Organization: Prifysgol Cymru, Aberystwyth Lines: 85 Nntp-Posting-Host: decb.aber.ac.uk