update.
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
27 (require 'poe)
28
29 (defvar default-load-path load-path
30   "*Base of `load-path'.
31 It is used as default value of target path to search file or
32 subdirectory under load-path.")
33
34 ;;;###autoload
35 (defun add-path (path &rest options)
36   "Add PATH to `load-path' if it exists under `default-load-path'
37 directories and it does not exist in `load-path'.
38
39 You can use following PATH styles:
40         load-path relative: \"PATH/\"
41                         (it is searched from `default-load-path')
42         home directory relative: \"~/PATH/\" \"~USER/PATH/\"
43         absolute path: \"/HOO/BAR/BAZ/\"
44
45 You can specify following OPTIONS:
46         'all-paths      search from `load-path'
47                         instead of `default-load-path'
48         'append         add PATH to the last of `load-path'"
49   (let ((rest (if (memq 'all-paths options)
50                   load-path
51                 default-load-path))
52         p)
53     (if (and (catch 'tag
54                (while rest
55                  (setq p (expand-file-name path (car rest)))
56                  (if (file-directory-p p)
57                      (throw 'tag p))
58                  (setq rest (cdr rest))))
59              (not (or (member p load-path)
60                       (if (string-match "/$" p)
61                           (member (substring p 0 (1- (length p))) load-path)
62                         (member (file-name-as-directory p) load-path)))))
63         (setq load-path
64               (if (memq 'append options)
65                   (append load-path (list p))
66                 (cons p load-path))))))
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   (let (files)
141     (catch 'tag
142       (while suffixes
143         (let ((suf (car suffixes)))
144           (if (and (not (string= suf ""))
145                    (string-match (concat (regexp-quote suf) "$") file))
146               (progn
147                 (setq files (list file))
148                 (throw 'tag nil)
149                 )
150             (setq files (cons (concat file suf) files))
151             )
152           (setq suffixes (cdr suffixes))
153           )))
154     (setq files (nreverse files))
155     (catch 'tag
156       (while paths
157         (let ((path (car paths))
158               (files files)
159               )
160           (while files
161             (setq file (expand-file-name (car files) path))
162             (if (file-executable-p file)
163                 (throw 'tag file)
164               )
165             (setq files (cdr files))
166             )
167           (setq paths (cdr paths))
168           )))))
169
170 ;;;###autoload
171 (defun module-installed-p (module &optional paths)
172   "Return t if module is provided or exists in PATHS.
173 If PATHS is omitted, `load-path' is used."
174   (or (featurep module)
175       (let ((file (symbol-name module)))
176         (or paths
177             (setq paths load-path)
178             )
179         (catch 'tag
180           (while paths
181             (let ((stem (expand-file-name file (car paths)))
182                   (sufs '(".elc" ".el"))
183                   )
184               (while sufs
185                 (let ((file (concat stem (car sufs))))
186                   (if (file-exists-p file)
187                       (throw 'tag file)
188                     ))
189                 (setq sufs (cdr sufs))
190                 ))
191             (setq paths (cdr paths))
192             )))))
193
194
195 ;;; @ end
196 ;;;
197
198 (require 'product)
199 (product-provide (provide 'path-util) (require 'apel-ver))
200
201 ;;; path-util.el ends here