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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 `defaul-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)
59 (setq rest (cdr rest))
61 (not (member p load-path))
64 (if (memq 'append options)
65 (append load-path (list p))
71 (defun add-latest-path (pattern &optional all-paths)
72 "Add latest path matched by PATTERN to `load-path'
73 if it exists under `default-load-path' directories
74 and it does not exist in `load-path'.
76 If optional argument ALL-PATHS is specified, it is searched from all
77 of load-path instead of default-load-path."
78 (let ((path (get-latest-path pattern all-paths)))
80 (add-to-list 'load-path path)
84 (defun get-latest-path (pattern &optional all-paths)
85 "Return latest directory in default-load-path
86 which is matched to regexp PATTERN.
87 If optional argument ALL-PATHS is specified,
88 it is searched from all of load-path instead of default-load-path."
90 (let ((paths (if all-paths
94 (while (setq dir (car paths))
95 (if (and (file-exists-p dir)
96 (file-directory-p dir)
98 (let ((files (sort (directory-files dir t pattern t)
99 (function file-newer-than-file-p)))
101 (while (setq file (car files))
102 (if (file-directory-p file)
105 (setq files (cdr files))
107 (setq paths (cdr paths))
111 (defun file-installed-p (file &optional paths)
112 "Return absolute-path of FILE if FILE exists in PATHS.
113 If PATHS is omitted, `load-path' is used."
115 (setq paths load-path)
120 (setq path (expand-file-name file (car paths)))
121 (if (file-exists-p path)
124 (setq paths (cdr paths))
128 (defvar exec-suffix-list '("")
129 "*List of suffixes for executable.")
132 (defun exec-installed-p (file &optional paths suffixes)
133 "Return absolute-path of FILE if FILE exists in PATHS.
134 If PATHS is omitted, `exec-path' is used.
135 If suffixes is omitted, `exec-suffix-list' is used."
137 (setq paths exec-path)
140 (setq suffixes exec-suffix-list)
144 (let ((stem (expand-file-name file (car paths)))
148 (let ((file (concat stem (car sufs))))
149 (if (file-exists-p file)
152 (setq sufs (cdr sufs))
154 (setq paths (cdr paths))
158 (defun module-installed-p (module &optional paths)
159 "Return t if module is provided or exists in PATHS.
160 If PATHS is omitted, `load-path' is used."
161 (or (featurep module)
162 (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
171 ;;; path-util.el ends here