1 ;;; path-util.el --- Emacs Lisp file detection utility
3 ;; Copyright (C) 1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Id: path-util.el,v 1.1 1997-11-06 15:47:23 morioka Exp $
7 ;; Keywords: file detection, install, module
9 ;; This file is part of APEL (A Portable Emacs Library).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (defvar default-load-path load-path
29 "*Base of `load-path'.
30 It is used as default value of target path to search file or
31 subdirectory under load-path.")
34 (defun add-path (path &rest options)
35 "Add PATH to `load-path' if it exists under `default-load-path'
36 directories and it does not exist in `load-path'.
38 You can use following PATH styles:
39 load-path relative: \"PATH/\"
40 (it is searched from `defaul-load-path')
41 home directory relative: \"~/PATH/\" \"~USER/PATH/\"
42 absolute path: \"/HOO/BAR/BAZ/\"
44 You can specify following OPTIONS:
45 'all-paths search from `load-path'
46 instead of `default-load-path'
47 'append add PATH to the last of `load-path'"
48 (let ((rest (if (memq 'all-paths options)
54 (setq p (expand-file-name path (car rest)))
55 (if (file-directory-p p)
58 (setq rest (cdr rest))
60 (not (member p load-path))
63 (if (memq 'append options)
64 (append load-path (list p))
70 (defun add-latest-path (pattern &optional all-paths)
71 "Add latest path matched by PATTERN to `load-path'
72 if it exists under `default-load-path' directories
73 and it does not exist in `load-path'.
75 If optional argument ALL-PATHS is specified, it is searched from all
76 of load-path instead of default-load-path."
77 (let ((path (get-latest-path pattern all-paths)))
79 (add-to-list 'load-path path)
83 (defun get-latest-path (pattern &optional all-paths)
84 "Return latest directory in default-load-path
85 which is matched to regexp PATTERN.
86 If optional argument ALL-PATHS is specified,
87 it is searched from all of load-path instead of default-load-path."
89 (let ((paths (if all-paths
93 (while (setq dir (car paths))
94 (if (and (file-exists-p dir)
95 (file-directory-p dir)
97 (let ((files (sort (directory-files dir t pattern t)
98 (function file-newer-than-file-p)))
100 (while (setq file (car files))
101 (if (file-directory-p file)
104 (setq files (cdr files))
106 (setq paths (cdr paths))
110 (defun file-installed-p (file &optional paths)
111 "Return absolute-path of FILE if FILE exists in PATHS.
112 If PATHS is omitted, `load-path' is used."
114 (setq paths load-path)
119 (setq path (expand-file-name file (car paths)))
120 (if (file-exists-p path)
123 (setq paths (cdr paths))
127 (defvar exec-suffix-list '("")
128 "*List of suffixes for executable.")
131 (defun exec-installed-p (file &optional paths suffixes)
132 "Return absolute-path of FILE if FILE exists in PATHS.
133 If PATHS is omitted, `exec-path' is used.
134 If suffixes is omitted, `exec-suffix-list' is used."
136 (setq paths exec-path)
139 (setq suffixes exec-suffix-list)
143 (let ((stem (expand-file-name file (car paths)))
147 (let ((file (concat stem (car sufs))))
148 (if (file-exists-p file)
151 (setq sufs (cdr sufs))
153 (setq paths (cdr paths))
157 (defun module-installed-p (module &optional paths)
158 "Return t if module is provided or exists in PATHS.
159 If PATHS is omitted, `load-path' is used."
160 (or (featurep module)
161 (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
170 ;;; path-util.el ends here