Rename file-detect.el -> path-util.el (file name should be less than
[elisp/apel.git] / path-util.el
1 ;;; path-util.el --- Emacs Lisp file detection utility
2
3 ;; Copyright (C) 1996,1997 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: path-util.el,v 1.1 1997-11-06 15:47:23 morioka Exp $
7 ;; Keywords: file detection, install, module
8
9 ;; This file is part of APEL (A Portable Emacs Library).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (defvar default-load-path load-path
29   "*Base of `load-path'.
30 It is used as default value of target path to search file or
31 subdirectory under load-path.")
32
33 ;;;###autoload
34 (defun add-path (path &rest options)
35   "Add PATH to `load-path' if it exists under `default-load-path'
36 directories and it does not exist in `load-path'.
37
38 You can use following PATH styles:
39         load-path relative: \"PATH/\"
40                         (it is searched from `defaul-load-path')
41         home directory relative: \"~/PATH/\" \"~USER/PATH/\"
42         absolute path: \"/HOO/BAR/BAZ/\"
43
44 You can specify following OPTIONS:
45         'all-paths      search from `load-path'
46                         instead of `default-load-path'
47         'append         add PATH to the last of `load-path'"
48   (let ((rest (if (memq 'all-paths options)
49                   load-path
50                 default-load-path))
51         p)
52     (if (and (catch 'tag
53                (while rest
54                  (setq p (expand-file-name path (car rest)))
55                  (if (file-directory-p p)
56                      (throw 'tag p)
57                    )
58                  (setq rest (cdr rest))
59                  ))
60              (not (member p load-path))
61              )
62         (setq load-path
63               (if (memq 'append options)
64                   (append load-path (list p))
65                 (cons p load-path)
66                 ))
67       )))
68
69 ;;;###autoload
70 (defun add-latest-path (pattern &optional all-paths)
71   "Add latest path matched by PATTERN to `load-path'
72 if it exists under `default-load-path' directories
73 and it does not exist in `load-path'.
74
75 If optional argument ALL-PATHS is specified, it is searched from all
76 of load-path instead of default-load-path."
77   (let ((path (get-latest-path pattern all-paths)))
78     (if path
79         (add-to-list 'load-path path)
80       )))
81
82 ;;;###autoload
83 (defun get-latest-path (pattern &optional all-paths)
84   "Return latest directory in default-load-path
85 which is matched to regexp PATTERN.
86 If optional argument ALL-PATHS is specified,
87 it is searched from all of load-path instead of default-load-path."
88   (catch 'tag
89     (let ((paths (if all-paths
90                     load-path
91                   default-load-path))
92           dir)
93       (while (setq dir (car paths))
94         (if (and (file-exists-p dir)
95                  (file-directory-p dir)
96                  )
97             (let ((files (sort (directory-files dir t pattern t)
98                                (function file-newer-than-file-p)))
99                   file)
100               (while (setq file (car files))
101                 (if (file-directory-p file)
102                     (throw 'tag file)
103                   )
104                 (setq files (cdr files))
105                 )))
106         (setq paths (cdr paths))
107         ))))
108
109 ;;;###autoload
110 (defun file-installed-p (file &optional paths)
111   "Return absolute-path of FILE if FILE exists in PATHS.
112 If PATHS is omitted, `load-path' is used."
113   (if (null paths)
114       (setq paths load-path)
115     )
116   (catch 'tag
117     (let (path)
118       (while paths
119         (setq path (expand-file-name file (car paths)))
120         (if (file-exists-p path)
121             (throw 'tag path)
122           )
123         (setq paths (cdr paths))
124         ))))
125
126 ;;;###autoload
127 (defvar exec-suffix-list '("")
128   "*List of suffixes for executable.")
129
130 ;;;###autoload
131 (defun exec-installed-p (file &optional paths suffixes)
132   "Return absolute-path of FILE if FILE exists in PATHS.
133 If PATHS is omitted, `exec-path' is used.
134 If suffixes is omitted, `exec-suffix-list' is used."
135   (or paths
136       (setq paths exec-path)
137       )
138   (or suffixes
139       (setq suffixes exec-suffix-list)
140       )
141   (catch 'tag
142     (while paths
143       (let ((stem (expand-file-name file (car paths)))
144             (sufs suffixes)
145             )
146         (while sufs
147           (let ((file (concat stem (car sufs))))
148             (if (file-exists-p file)
149                 (throw 'tag file)
150               ))
151           (setq sufs (cdr sufs))
152           ))
153       (setq paths (cdr paths))
154       )))
155
156 ;;;###autoload
157 (defun module-installed-p (module &optional paths)
158   "Return t if module is provided or exists in PATHS.
159 If PATHS is omitted, `load-path' is used."
160   (or (featurep module)
161       (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
162       ))
163
164
165 ;;; @ end
166 ;;;
167
168 (provide 'path-util)
169
170 ;;; path-util.el ends here