(directory-files): Emulate as Emacs 19 or later to accept the optional fourth
[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.2 1999-08-26 12:32:32 yamaoka 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 (condition-case nil
83     (directory-files "." nil nil t)
84   (file-error nil);; unreadable directory.
85   (wrong-number-of-arguments
86    (or (fboundp 'si:directory-files)
87        (fset 'si:directory-files (symbol-function 'directory-files)))
88    ;; This function is also defined in poe-18, but it is needed here
89    ;; for compiling other packages under old Emacsen.
90    (defun directory-files (directory &optional full match nosort)
91      "Return a list of names of files in DIRECTORY.
92 There are three optional arguments:
93 If FULL is non-nil, return absolute file names.  Otherwise return names
94  that are relative to the specified directory.
95 If MATCH is non-nil, mention only file names that match the regexp MATCH.
96 If NOSORT is dummy for compatibility.
97 \[poe-18.el; EMACS 19 emulating function]"
98      (si:directory-files directory full match))
99    ))
100
101 ;;;###autoload
102 (defun get-latest-path (pattern &optional all-paths)
103   "Return latest directory in default-load-path
104 which is matched to regexp PATTERN.
105 If optional argument ALL-PATHS is specified,
106 it is searched from all of load-path instead of default-load-path."
107   (catch 'tag
108     (let ((paths (if all-paths
109                     load-path
110                   default-load-path))
111           dir)
112       (while (setq dir (car paths))
113         (if (and (file-exists-p dir)
114                  (file-directory-p dir)
115                  )
116             (let ((files (sort (directory-files dir t pattern t)
117                                (function file-newer-than-file-p)))
118                   file)
119               (while (setq file (car files))
120                 (if (file-directory-p file)
121                     (throw 'tag file)
122                   )
123                 (setq files (cdr files))
124                 )))
125         (setq paths (cdr paths))
126         ))))
127
128 ;;;###autoload
129 (defun file-installed-p (file &optional paths)
130   "Return absolute-path of FILE if FILE exists in PATHS.
131 If PATHS is omitted, `load-path' is used."
132   (if (null paths)
133       (setq paths load-path)
134     )
135   (catch 'tag
136     (let (path)
137       (while paths
138         (setq path (expand-file-name file (car paths)))
139         (if (file-exists-p path)
140             (throw 'tag path)
141           )
142         (setq paths (cdr paths))
143         ))))
144
145 ;;;###autoload
146 (defvar exec-suffix-list '("")
147   "*List of suffixes for executable.")
148
149 ;;;###autoload
150 (defun exec-installed-p (file &optional paths suffixes)
151   "Return absolute-path of FILE if FILE exists in PATHS.
152 If PATHS is omitted, `exec-path' is used.
153 If suffixes is omitted, `exec-suffix-list' is used."
154   (or paths
155       (setq paths exec-path)
156       )
157   (or suffixes
158       (setq suffixes exec-suffix-list)
159       )
160   (catch 'tag
161     (while paths
162       (let ((stem (expand-file-name file (car paths)))
163             (sufs suffixes)
164             )
165         (while sufs
166           (let ((file (concat stem (car sufs))))
167             (if (file-exists-p file)
168                 (throw 'tag file)
169               ))
170           (setq sufs (cdr sufs))
171           ))
172       (setq paths (cdr paths))
173       )))
174
175 ;;;###autoload
176 (defun module-installed-p (module &optional paths)
177   "Return t if module is provided or exists in PATHS.
178 If PATHS is omitted, `load-path' is used."
179   (or (featurep module)
180       (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
181       ))
182
183
184 ;;; @ end
185 ;;;
186
187 (provide 'path-util)
188
189 ;;; path-util.el ends here