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.
28 (directory-files "." nil nil t)
29 (file-error nil);; unreadable directory.
30 (wrong-number-of-arguments
31 (or (fboundp 'si:directory-files)
32 (fset 'si:directory-files (symbol-function 'directory-files)))
33 ;; This function is also defined in poe-18, but it is needed here
34 ;; for compiling other packages under old Emacsen.
35 (defun directory-files (directory &optional full match nosort)
36 "Return a list of names of files in DIRECTORY.
37 There are three optional arguments:
38 If FULL is non-nil, return absolute file names. Otherwise return names
39 that are relative to the specified directory.
40 If MATCH is non-nil, mention only file names that match the regexp MATCH.
41 If NOSORT is dummy for compatibility.
42 \[poe-18.el; EMACS 19 emulating function]"
43 (si:directory-files directory full match))
46 (defvar default-load-path load-path
47 "*Base of `load-path'.
48 It is used as default value of target path to search file or
49 subdirectory under load-path.")
52 (defun add-path (path &rest options)
53 "Add PATH to `load-path' if it exists under `default-load-path'
54 directories and it does not exist in `load-path'.
56 You can use following PATH styles:
57 load-path relative: \"PATH/\"
58 (it is searched from `defaul-load-path')
59 home directory relative: \"~/PATH/\" \"~USER/PATH/\"
60 absolute path: \"/HOO/BAR/BAZ/\"
62 You can specify following OPTIONS:
63 'all-paths search from `load-path'
64 instead of `default-load-path'
65 'append add PATH to the last of `load-path'"
66 (let ((rest (if (memq 'all-paths options)
72 (setq p (expand-file-name path (car rest)))
73 (if (file-directory-p p)
76 (setq rest (cdr rest))
78 (not (member p load-path))
81 (if (memq 'append options)
82 (append load-path (list p))
88 (defun add-latest-path (pattern &optional all-paths)
89 "Add latest path matched by PATTERN to `load-path'
90 if it exists under `default-load-path' directories
91 and it does not exist in `load-path'.
93 If optional argument ALL-PATHS is specified, it is searched from all
94 of load-path instead of default-load-path."
95 (let ((path (get-latest-path pattern all-paths)))
97 (add-to-list 'load-path path)
101 (defun get-latest-path (pattern &optional all-paths)
102 "Return latest directory in default-load-path
103 which is matched to regexp PATTERN.
104 If optional argument ALL-PATHS is specified,
105 it is searched from all of load-path instead of default-load-path."
107 (let ((paths (if all-paths
111 (while (setq dir (car paths))
112 (if (and (file-exists-p dir)
113 (file-directory-p dir)
115 (let ((files (sort (directory-files dir t pattern t)
116 (function file-newer-than-file-p)))
118 (while (setq file (car files))
119 (if (file-directory-p file)
122 (setq files (cdr files))
124 (setq paths (cdr paths))
128 (defun file-installed-p (file &optional paths)
129 "Return absolute-path of FILE if FILE exists in PATHS.
130 If PATHS is omitted, `load-path' is used."
132 (setq paths load-path)
137 (setq path (expand-file-name file (car paths)))
138 (if (file-exists-p path)
141 (setq paths (cdr paths))
145 (defvar exec-suffix-list '("")
146 "*List of suffixes for executable.")
149 (defun exec-installed-p (file &optional paths suffixes)
150 "Return absolute-path of FILE if FILE exists in PATHS.
151 If PATHS is omitted, `exec-path' is used.
152 If suffixes is omitted, `exec-suffix-list' is used."
154 (setq paths exec-path)
157 (setq suffixes exec-suffix-list)
161 (let ((stem (expand-file-name file (car paths)))
165 (let ((file (concat stem (car sufs))))
166 (if (file-exists-p file)
169 (setq sufs (cdr sufs))
171 (setq paths (cdr paths))
175 (defun module-installed-p (module &optional paths)
176 "Return t if module is provided or exists in PATHS.
177 If PATHS is omitted, `load-path' is used."
178 (or (featurep module)
179 (exec-installed-p (symbol-name module) load-path '(".elc" ".el"))
188 ;;; path-util.el ends here