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.2 1999-08-26 12:32:32 yamaoka 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 (directory-files "." nil nil t)
84 (file-error nil);; unreadable directory.
85 (wrong-number-of-arguments
86 (or (fboundp 'si:directory-files)
87 (fset 'si:directory-files (symbol-function 'directory-files)))
88 ;; This function is also defined in poe-18, but it is needed here
89 ;; for compiling other packages under old Emacsen.
90 (defun directory-files (directory &optional full match nosort)
91 "Return a list of names of files in DIRECTORY.
92 There are three optional arguments:
93 If FULL is non-nil, return absolute file names. Otherwise return names
94 that are relative to the specified directory.
95 If MATCH is non-nil, mention only file names that match the regexp MATCH.
96 If NOSORT is dummy for compatibility.
97 \[poe-18.el; EMACS 19 emulating function]"
98 (si:directory-files directory full match))
102 (defun get-latest-path (pattern &optional all-paths)
103 "Return latest directory in default-load-path
104 which is matched to regexp PATTERN.
105 If optional argument ALL-PATHS is specified,
106 it is searched from all of load-path instead of default-load-path."
108 (let ((paths (if all-paths
112 (while (setq dir (car paths))
113 (if (and (file-exists-p dir)
114 (file-directory-p dir)
116 (let ((files (sort (directory-files dir t pattern t)
117 (function file-newer-than-file-p)))
119 (while (setq file (car files))
120 (if (file-directory-p file)
123 (setq files (cdr files))
125 (setq paths (cdr paths))
129 (defun file-installed-p (file &optional paths)
130 "Return absolute-path of FILE if FILE exists in PATHS.
131 If PATHS is omitted, `load-path' is used."
133 (setq paths load-path)
138 (setq path (expand-file-name file (car paths)))
139 (if (file-exists-p path)
142 (setq paths (cdr paths))
146 (defvar exec-suffix-list '("")
147 "*List of suffixes for executable.")
150 (defun exec-installed-p (file &optional paths suffixes)
151 "Return absolute-path of FILE if FILE exists in PATHS.
152 If PATHS is omitted, `exec-path' is used.
153 If suffixes is omitted, `exec-suffix-list' is used."
155 (setq paths exec-path)
158 (setq suffixes exec-suffix-list)
162 (let ((stem (expand-file-name file (car paths)))
166 (let ((file (concat stem (car sufs))))
167 (if (file-exists-p file)
170 (setq sufs (cdr sufs))
172 (setq paths (cdr paths))
176 (defun module-installed-p (module &optional paths)
177 "Return t if module is provided or exists in PATHS.
178 If PATHS is omitted, `load-path' is used."
179 (or (featurep module)
180 (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
189 ;;; path-util.el ends here