1 ;;; path-util.el --- Emacs Lisp file detection utility
3 ;; Copyright (C) 1996,1997,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: file detection, install, module
8 ;; This file is part of APEL (A Portable Emacs Library).
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.
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.
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.
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.")
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'.
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/\"
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)
55 (setq p (expand-file-name path (car rest)))
56 (if (file-directory-p 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)))))
64 (if (memq 'append options)
65 (append load-path (list p))
66 (cons p load-path))))))
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'.
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)))
78 (add-to-list 'load-path path)
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."
88 (let ((paths (if all-paths
92 (while (setq dir (car paths))
93 (if (and (file-exists-p dir)
94 (file-directory-p dir)
96 (let ((files (sort (directory-files dir t pattern t)
97 (function file-newer-than-file-p)))
99 (while (setq file (car files))
100 (if (file-directory-p file)
103 (setq files (cdr files))
105 (setq paths (cdr paths))
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."
113 (setq paths load-path)
118 (setq path (expand-file-name file (car paths)))
119 (if (file-exists-p path)
122 (setq paths (cdr paths))
126 (defvar exec-suffix-list '("")
127 "*List of suffixes for executable.")
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."
135 (setq paths exec-path)
138 (setq suffixes exec-suffix-list)
143 (let ((suf (car suffixes)))
144 (if (and (not (string= suf ""))
145 (string-match (concat (regexp-quote suf) "$") file))
147 (setq files (list file))
150 (setq files (cons (concat file suf) files))
152 (setq suffixes (cdr suffixes))
154 (setq files (nreverse files))
157 (let ((path (car paths))
161 (setq file (expand-file-name (car files) path))
162 (if (file-executable-p file)
165 (setq files (cdr files))
167 (setq paths (cdr paths))
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)))
177 (setq paths load-path)
181 (let ((stem (expand-file-name file (car paths)))
182 (sufs '(".elc" ".el"))
185 (let ((file (concat stem (car sufs))))
186 (if (file-exists-p file)
189 (setq sufs (cdr sufs))
191 (setq paths (cdr paths))
199 (product-provide (provide 'path-util) (require 'apel-ver))
201 ;;; path-util.el ends here