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