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