APEL 3.4.
[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:
7 ;;      $Id: file-detect.el,v 3.4 1997/06/28 07:18:31 morioka Exp $
8 ;; Keywords: install, module
9
10 ;; This file is part of APEL (A Portable Emacs Library).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (defvar default-load-path load-path)
30
31 ;;;###autoload
32 (defun add-path (path &rest options)
33   "Add PATH to `load-path' if it exists under `default-load-path'
34 directories and it does not exist in `load-path'.
35
36 You can use following PATH styles:
37         load-path relative: \"PATH/\"
38                         (it is searched from `defaul-load-path')
39         home directory relative: \"~/PATH/\" \"~USER/PATH/\"
40         absolute path: \"/HOO/BAR/BAZ/\"
41
42 You can specify following OPTIONS:
43         'all-paths      search from `load-path'
44                         instead of `default-load-path'
45         'append         add PATH to the last of `load-path'"
46   (let ((rest (if (memq 'all-paths options)
47                   load-path
48                 default-load-path))
49         p)
50     (if (and (catch 'tag
51                (while rest
52                  (setq p (expand-file-name path (car rest)))
53                  (if (file-directory-p p)
54                      (throw 'tag p)
55                    )
56                  (setq rest (cdr rest))
57                  ))
58              (not (member p load-path))
59              )
60         (setq load-path
61               (if (memq 'append options)
62                   (append load-path (list p))
63                 (cons p load-path)
64                 ))
65       )))
66
67 ;;;###autoload
68 (defun add-latest-path (pattern &optional all-paths)
69   "Add latest path matched by PATTERN to `load-path'
70 if it exists under `default-load-path' directories
71 and it does not exist in `load-path'.
72
73 If optional argument ALL-PATHS is specified, it is searched from all
74 of load-path instead of default-load-path. [file-detect.el]"
75   (let ((path (get-latest-path pattern all-paths)))
76     (if path
77         (add-to-list 'load-path path)
78       )))
79
80 ;;;###autoload
81 (defun get-latest-path (pattern &optional all-paths)
82   "Return latest directory in default-load-path
83 which is matched to regexp PATTERN.
84 If optional argument ALL-PATHS is specified,
85 it is searched from all of load-path instead of default-load-path."
86   (catch 'tag
87     (let ((paths (if all-paths
88                     load-path
89                   default-load-path))
90           dir)
91       (while (setq dir (car paths))
92         (if (and (file-exists-p dir)
93                  (file-directory-p dir)
94                  )
95             (let ((files (sort (directory-files dir t pattern t)
96                                (function file-newer-than-file-p)))
97                   file)
98               (while (setq file (car files))
99                 (if (file-directory-p file)
100                     (throw 'tag file)
101                   )
102                 (setq files (cdr files))
103                 )))
104         (setq paths (cdr paths))
105         ))))
106
107 ;;;###autoload
108 (defun file-installed-p (file &optional paths)
109   "Return absolute-path of FILE if FILE exists in PATHS.
110 If PATHS is omitted, `load-path' is used."
111   (if (null paths)
112       (setq paths load-path)
113     )
114   (catch 'tag
115     (let (path)
116       (while paths
117         (setq path (expand-file-name file (car paths)))
118         (if (file-exists-p path)
119             (throw 'tag path)
120           )
121         (setq paths (cdr paths))
122         ))))
123
124 ;;;###autoload
125 (defvar exec-suffix-list '("")
126   "*List of suffixes for executable.")
127
128 ;;;###autoload
129 (defun exec-installed-p (file &optional paths suffixes)
130   "Return absolute-path of FILE if FILE exists in PATHS.
131 If PATHS is omitted, `exec-path' is used.
132 If suffixes is omitted, `exec-suffix-list' is used."
133   (or paths
134       (setq paths exec-path)
135       )
136   (or suffixes
137       (setq suffixes exec-suffix-list)
138       )
139   (catch 'tag
140     (while paths
141       (let ((stem (expand-file-name file (car paths)))
142             (sufs suffixes)
143             )
144         (while sufs
145           (let ((file (concat stem (car sufs))))
146             (if (file-exists-p file)
147                 (throw 'tag file)
148               ))
149           (setq sufs (cdr sufs))
150           ))
151       (setq paths (cdr paths))
152       )))
153
154 ;;;###autoload
155 (defun module-installed-p (module &optional paths)
156   "Return t if module is provided or exists in PATHS.
157 If PATHS is omitted, `load-path' is used."
158   (or (featurep module)
159       (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
160       ))
161
162
163 ;;; @ end
164 ;;;
165
166 (provide 'file-detect)
167
168 ;;; file-detect.el ends here