tm 7.79.
[elisp/apel.git] / file-detect.el
1 ;;; file-detect.el --- Emacs Lisp file detection utility
2
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version:
7 ;;      $Id: file-detect.el,v 1.6 1996/08/21 11:43:09 morioka Exp $
8 ;; Keywords: install, module
9
10 ;; This file is part of tl (Tiny 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 This program; see the file COPYING.  If not, write to
24 ;; the 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 (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
46 \[file-detect.el]"
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 (defun get-latest-path (pat &optional all-paths)
69   "Return latest directory in default-load-path
70 which is matched to regexp PAT.
71 If optional argument ALL-PATHS is specified,
72 it is searched from all of load-path instead of default-load-path.
73 \[file-detect.el]"
74   (catch 'tag
75     (let ((paths (if all-paths
76                     load-path
77                   default-load-path))
78           dir)
79       (while (setq dir (car paths))
80         (let ((files (sort (directory-files dir t pat t)
81                            (function file-newer-than-file-p)))
82               file)
83           (while (setq file (car files))
84             (if (file-directory-p file)
85                 (throw 'tag file)
86               )
87             (setq files (cdr files))
88             ))
89         (setq paths (cdr paths))
90         ))))
91
92 (defun file-installed-p (file &optional paths)
93   "Return t if FILE exists in PATHS.
94 If PATHS is omitted, `load-path' is used. [file-detect.el]"
95   (if (null paths)
96       (setq paths load-path)
97     )
98   (catch 'tag
99     (let (path)
100       (while paths
101         (setq path (expand-file-name file (car paths)))
102         (if (file-exists-p path)
103             (throw 'tag path)
104           )
105         (setq paths (cdr paths))
106         ))))
107
108 (defun module-installed-p (module &optional paths)
109   "Return t if module is provided or exists in PATHS.
110 If PATHS is omitted, `load-path' is used. [file-detect.el]"
111   (or (featurep module)
112       (let ((name (symbol-name module)))
113         (if (null paths)
114             (setq paths load-path)
115           )
116         (catch 'tag
117           (while paths
118             (let ((file (expand-file-name name (car paths))))
119               (let ((elc-file (concat file ".elc")))
120                 (if (file-exists-p elc-file)
121                     (throw 'tag elc-file)
122                   ))
123               (let ((el-file (concat file ".el")))
124                 (if (file-exists-p el-file)
125                     (throw 'tag el-file)
126                   ))
127               (if (file-exists-p file)
128                   (throw 'tag file)
129                 )
130               )
131             (setq paths (cdr paths))
132             )))))
133
134
135 ;;; @ end
136 ;;;
137
138 (provide 'file-detect)
139
140 ;;; file-detect.el ends here