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.
27 (defvar default-load-path load-path
28 "*Base of `load-path'.
29 It is used as default value of target path to search file or
30 subdirectory under load-path.")
33 (defun add-path (path &rest options)
34 "Add PATH to `load-path' if it exists under `default-load-path'
35 directories and it does not exist in `load-path'.
37 You can use following PATH styles:
38 load-path relative: \"PATH/\"
39 (it is searched from `defaul-load-path')
40 home directory relative: \"~/PATH/\" \"~USER/PATH/\"
41 absolute path: \"/HOO/BAR/BAZ/\"
43 You can specify following OPTIONS:
44 'all-paths search from `load-path'
45 instead of `default-load-path'
46 'append add PATH to the last of `load-path'"
47 (let ((rest (if (memq 'all-paths options)
53 (setq p (expand-file-name path (car rest)))
54 (if (file-directory-p p)
57 (setq rest (cdr rest))
59 (not (member p load-path))
62 (if (memq 'append options)
63 (append load-path (list p))
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)
142 (let ((stem (expand-file-name file (car paths)))
146 (let ((file (concat stem (car sufs))))
147 (if (file-exists-p file)
150 (setq sufs (cdr sufs))
152 (setq paths (cdr paths))
156 (defun module-installed-p (module &optional paths)
157 "Return t if module is provided or exists in PATHS.
158 If PATHS is omitted, `load-path' is used."
159 (or (featurep module)
160 (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
169 ;;; path-util.el ends here