(directory-files): Don't redefine.
[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 (defvar default-load-path load-path
28   "*Base of `load-path'.
29 It is used as default value of target path to search file or
30 subdirectory under load-path.")
31
32 ;;;###autoload
33 (defun add-path (path &rest options)
34   "Add PATH to `load-path' if it exists under `default-load-path'
35 directories and it does not exist in `load-path'.
36
37 You can use following PATH styles:
38         load-path relative: \"PATH/\"
39                         (it is searched from `defaul-load-path')
40         home directory relative: \"~/PATH/\" \"~USER/PATH/\"
41         absolute path: \"/HOO/BAR/BAZ/\"
42
43 You can specify following OPTIONS:
44         'all-paths      search from `load-path'
45                         instead of `default-load-path'
46         'append         add PATH to the last of `load-path'"
47   (let ((rest (if (memq 'all-paths options)
48                   load-path
49                 default-load-path))
50         p)
51     (if (and (catch 'tag
52                (while rest
53                  (setq p (expand-file-name path (car rest)))
54                  (if (file-directory-p p)
55                      (throw 'tag p)
56                    )
57                  (setq rest (cdr rest))
58                  ))
59              (not (member p load-path))
60              )
61         (setq load-path
62               (if (memq 'append options)
63                   (append load-path (list p))
64                 (cons p load-path)
65                 ))
66       )))
67
68 ;;;###autoload
69 (defun add-latest-path (pattern &optional all-paths)
70   "Add latest path matched by PATTERN to `load-path'
71 if it exists under `default-load-path' directories
72 and it does not exist in `load-path'.
73
74 If optional argument ALL-PATHS is specified, it is searched from all
75 of load-path instead of default-load-path."
76   (let ((path (get-latest-path pattern all-paths)))
77     (if path
78         (add-to-list 'load-path path)
79       )))
80
81 ;;;###autoload
82 (defun get-latest-path (pattern &optional all-paths)
83   "Return latest directory in default-load-path
84 which is matched to regexp PATTERN.
85 If optional argument ALL-PATHS is specified,
86 it is searched from all of load-path instead of default-load-path."
87   (catch 'tag
88     (let ((paths (if all-paths
89                     load-path
90                   default-load-path))
91           dir)
92       (while (setq dir (car paths))
93         (if (and (file-exists-p dir)
94                  (file-directory-p dir)
95                  )
96             (let ((files (sort (directory-files dir t pattern t)
97                                (function file-newer-than-file-p)))
98                   file)
99               (while (setq file (car files))
100                 (if (file-directory-p file)
101                     (throw 'tag file)
102                   )
103                 (setq files (cdr files))
104                 )))
105         (setq paths (cdr paths))
106         ))))
107
108 ;;;###autoload
109 (defun file-installed-p (file &optional paths)
110   "Return absolute-path of FILE if FILE exists in PATHS.
111 If PATHS is omitted, `load-path' is used."
112   (if (null paths)
113       (setq paths load-path)
114     )
115   (catch 'tag
116     (let (path)
117       (while paths
118         (setq path (expand-file-name file (car paths)))
119         (if (file-exists-p path)
120             (throw 'tag path)
121           )
122         (setq paths (cdr paths))
123         ))))
124
125 ;;;###autoload
126 (defvar exec-suffix-list '("")
127   "*List of suffixes for executable.")
128
129 ;;;###autoload
130 (defun exec-installed-p (file &optional paths suffixes)
131   "Return absolute-path of FILE if FILE exists in PATHS.
132 If PATHS is omitted, `exec-path' is used.
133 If suffixes is omitted, `exec-suffix-list' is used."
134   (or paths
135       (setq paths exec-path)
136       )
137   (or suffixes
138       (setq suffixes exec-suffix-list)
139       )
140   (catch 'tag
141     (while paths
142       (let ((stem (expand-file-name file (car paths)))
143             (sufs suffixes)
144             )
145         (while sufs
146           (let ((file (concat stem (car sufs))))
147             (if (file-exists-p file)
148                 (throw 'tag file)
149               ))
150           (setq sufs (cdr sufs))
151           ))
152       (setq paths (cdr paths))
153       )))
154
155 ;;;###autoload
156 (defun module-installed-p (module &optional paths)
157   "Return t if module is provided or exists in PATHS.
158 If PATHS is omitted, `load-path' is used."
159   (or (featurep module)
160       (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
161       ))
162
163
164 ;;; @ end
165 ;;;
166
167 (provide 'path-util)
168
169 ;;; path-util.el ends here