home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / sources / 915 next >
Encoding:
Text File  |  1993-01-04  |  3.1 KB  |  98 lines

  1. Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!gdt!aber!fronta.aber.ac.uk!pcg
  2. From: pcg@aber.ac.uk (Piercarlo Grandi)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: pathfind.el: Path finding functions
  5. Message-ID: <PCG.93Jan4030314@decb.aber.ac.uk>
  6. Date: 4 Jan 93 03:03:14 GMT
  7. Sender: news@aber.ac.uk (USENET news service)
  8. Reply-To: pcg@aber.ac.uk (Piercarlo Grandi)
  9. Organization: Prifysgol Cymru, Aberystwyth
  10. Lines: 85
  11. Nntp-Posting-Host: decb.aber.ac.uk
  12.  
  13.  
  14. Here is a simple set of path finding functions, with which I have
  15. added path finding to the Info reader. Version 19 will have a similar
  16. facility, but in the meantime here is mine.
  17.  
  18. Please note the unusual indentation style, and that I have provided
  19. "executable" comments for exemplification and testing. This is a
  20. smalltalky sort of thing, and should be more common.
  21.  
  22. ;;; Piercarlo Grandi, Aberystwyth, 1992
  23.  
  24. ;; This function will return a pathname if it exists with any of the
  25. ;; given suffixes. It does not check for existence of the pathname
  26. ;; without any suffix.
  27.  
  28. ; EXAMPLES:
  29. ;  (path-suffixed-p "/usr/include/types" '(".h"))
  30. ;  (path-suffixed-p "/usr/include/tar" ".h")
  31.  
  32. (defun path-suffixed-p (pathname suffixes)
  33.   (if (null suffixes) nil
  34.     (if (atom suffixes) (setq suffixes (list suffixes)))
  35.     (let ((suffixed (concat pathname (car suffixes))))
  36.       (if (file-exists-p suffixed) suffixed
  37.     (path-suffixed-p pathname (cdr suffixes))
  38.       )
  39.     )
  40.   )
  41. )
  42.  
  43. ;; This function will return a complete pathname composed of the prefix
  44. ;; followed by the name, followed _optionally_ by one of the suffixes,
  45. ;; if such a pathname exist. Prefix and suffix lists are scanned strictly
  46. ;; left to right.
  47.  
  48. ; EXAMPLES:
  49. ;  (path-exists-p "/usr/include" "tar.h" ".h")
  50. ;  (path-exists-p "/usr/include" "tar" ".h")
  51. ;  (path-exists-p "/usr/include" "types" ".h")
  52.  
  53. (defun path-exists-p (prefix name &optional suffixes)
  54.   (let ((pathname (expand-file-name name prefix)))
  55.     (if (file-exists-p pathname) pathname
  56.       (if (null suffixes) nil (path-suffixed-p pathname suffixes))
  57.     )
  58.   )
  59. )
  60.  
  61. ;; This function will return a complete pathname built by concatenating
  62. ;; one of the suffixes with the name with one of the suffixes,
  63. ;; if it exists and the given test applied to it is satisfied.
  64.  
  65. ; EXAMPLES:
  66. ;  (path-find-file '("/usr/include" "/usr/include/sys") "tar.h" ".h" nil)
  67. ;  (path-find-file '("/usr/include" "/usr/include/sys") "types" ".h" 'file-exists-p)
  68.  
  69. ;  (path-find-file load-path "debug" ".elc" nil)
  70. ;  (path-find-file load-path "calc" '(".elc" ".el"))
  71.  
  72. (defun path-find-file (prefixes name &optional suffixes test)
  73.   (if (null prefixes) nil
  74.     (if (atom prefixes) (setq prefixes (list prefixes)))
  75.     (let
  76.       (
  77.     (pathname
  78.       (path-suffixed-p
  79.         (expand-file-name name (car prefixes))
  80.         (or suffixes '(""))
  81.       )
  82.     )
  83.       )
  84.       (if (and pathname (or (null test) (apply test (list pathname))))
  85.     pathname
  86.     (path-find-file (cdr prefixes) name suffixes test)
  87.       )
  88.     )
  89.   )
  90. )
  91.  
  92. (provide 'pathfind)
  93.  
  94. --
  95. Piercarlo Grandi, Dept of CS, PC/UW@Aberystwyth <pcg@aber.ac.uk>
  96.   E l'italiano cantava, cantava. E le sue disperate invocazioni giunsero
  97.   alle orecchie del suo divino protettore, il dio della barzelletta
  98.