428
|
1 ;;; find-paths.el --- setup various XEmacs paths
|
|
2
|
|
3 ;; Copyright (C) 1985-1986, 1990, 1992-1997 Free Software Foundation, Inc.
|
|
4 ;; Copyright (c) 1993, 1994 Sun Microsystems, Inc.
|
|
5 ;; Copyright (C) 1995 Board of Trustees, University of Illinois
|
|
6
|
|
7 ;; Author: Mike Sperber <sperber@informatik.uni-tuebingen.de>
|
|
8 ;; Maintainer: XEmacs Development Team
|
|
9 ;; Keywords: internal, dumped
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: Not in FSF.
|
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; This file is dumped with XEmacs.
|
|
33
|
776
|
34 ;; This file contains basic library functionality for manipulating paths
|
|
35 ;; and path lists and finding paths in the XEmacs hierarchy.
|
|
36
|
428
|
37
|
|
38 ;;; Code:
|
|
39
|
|
40 (defvar paths-version-control-filename-regexp
|
|
41 "^\\(RCS\\|CVS\\|SCCS\\)$"
|
|
42 "File bases associated with version control.")
|
|
43
|
|
44 (defvar paths-lisp-filename-regexp
|
|
45 "^\\(.*\\.elc?\\)$"
|
|
46 "File bases that contain Lisp file.")
|
|
47
|
|
48 (defvar paths-no-lisp-directory-regexp
|
|
49 (concat "\\(" paths-version-control-filename-regexp "\\)"
|
|
50 "\\|"
|
|
51 "\\(" paths-lisp-filename-regexp "\\)")
|
|
52 "File bases that may not be directories containing Lisp code.")
|
|
53
|
|
54 (defun paths-find-recursive-path (directories &optional max-depth exclude-regexp)
|
|
55 "Return a list of the directory hierarchy underneath DIRECTORIES.
|
|
56 The returned list is sorted by pre-order and lexicographically.
|
|
57 MAX-DEPTH limits the depth of the search to MAX-DEPTH level,
|
|
58 if it is a number. If MAX-DEPTH is NIL, the search depth is unlimited.
|
|
59 EXCLUDE-REGEXP is a regexp that matches directory names to exclude
|
|
60 from the search."
|
|
61 (let ((path '()))
|
|
62 (while directories
|
|
63 (let ((directory (file-name-as-directory
|
|
64 (expand-file-name
|
|
65 (car directories)))))
|
|
66 (if (paths-file-readable-directory-p directory)
|
|
67 (let ((raw-entries
|
|
68 (if (equal 0 max-depth)
|
|
69 '()
|
|
70 (directory-files directory nil "^[^.-]")))
|
|
71 (reverse-dirs '()))
|
|
72 (while raw-entries
|
531
|
73 (if (not (and exclude-regexp
|
|
74 (string-match exclude-regexp (car raw-entries))))
|
428
|
75 (setq reverse-dirs
|
|
76 (cons (expand-file-name (car raw-entries) directory)
|
|
77 reverse-dirs)))
|
|
78 (setq raw-entries (cdr raw-entries)))
|
|
79
|
|
80 (let ((sub-path
|
|
81 (paths-find-recursive-path (reverse reverse-dirs)
|
|
82 (if (numberp max-depth)
|
|
83 (- max-depth 1)
|
|
84 max-depth)
|
|
85 exclude-regexp)))
|
|
86 (setq path (nconc path
|
|
87 (list directory)
|
|
88 sub-path))))))
|
|
89 (setq directories (cdr directories)))
|
|
90 path))
|
|
91
|
|
92 (defun paths-file-readable-directory-p (filename)
|
|
93 "Check if filename is a readable directory."
|
|
94 (and (file-directory-p filename)
|
|
95 (file-readable-p filename)))
|
|
96
|
|
97 (defun paths-find-recursive-load-path (directories &optional max-depth)
|
|
98 "Construct a recursive load path underneath DIRECTORIES."
|
|
99 (paths-find-recursive-path directories
|
|
100 max-depth paths-no-lisp-directory-regexp))
|
|
101
|
|
102 (defun paths-emacs-root-p (directory)
|
1227
|
103 "Check if DIRECTORY is a plausible installation root."
|
428
|
104 (or
|
|
105 ;; installed
|
|
106 (paths-file-readable-directory-p (paths-construct-path (list directory
|
|
107 "lib"
|
689
|
108 (construct-emacs-version-name))))
|
428
|
109 ;; in-place or windows-nt
|
|
110 (and
|
|
111 (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
|
|
112 (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
|
|
113
|
1227
|
114 (defun paths-emacs-data-root-p (directory)
|
|
115 "Check if DIRECTORY is a plausible data installation root.
|
|
116 A data installation root is one containing data files that may be shared
|
|
117 among multiple different versions of XEmacs, the packages in particular."
|
|
118 (or
|
|
119 ;; installed
|
|
120 (paths-file-readable-directory-p (paths-construct-path (list directory
|
|
121 "lib"
|
|
122 emacs-program-name)))
|
|
123 (paths-file-readable-directory-p (paths-construct-path (list directory
|
|
124 "lib"
|
|
125 (construct-emacs-version-name))))
|
|
126 ;; in-place or windows-nt
|
|
127 (and
|
|
128 (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
|
|
129 (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
|
|
130
|
428
|
131 (defun paths-chase-symlink (file-name)
|
|
132 "Chase a symlink until the bitter end."
|
|
133 (let ((maybe-symlink (file-symlink-p file-name)))
|
|
134 (if maybe-symlink
|
|
135 (let* ((directory (file-name-directory file-name))
|
|
136 (destination (expand-file-name maybe-symlink directory)))
|
|
137 (paths-chase-symlink destination))
|
|
138 file-name)))
|
|
139
|
|
140 (defun paths-find-emacs-root
|
|
141 (invocation-directory invocation-name)
|
|
142 "Find the run-time root of XEmacs."
|
|
143 (let* ((executable-file-name (paths-chase-symlink
|
|
144 (concat invocation-directory
|
|
145 invocation-name)))
|
|
146 (executable-directory (file-name-directory executable-file-name))
|
|
147 (maybe-root-1 (file-name-as-directory
|
|
148 (paths-construct-path '("..") executable-directory)))
|
|
149 (maybe-root-2 (file-name-as-directory
|
|
150 (paths-construct-path '(".." "..") executable-directory))))
|
|
151 (or (and (paths-emacs-root-p maybe-root-1)
|
|
152 maybe-root-1)
|
|
153 (and (paths-emacs-root-p maybe-root-2)
|
|
154 maybe-root-2))))
|
|
155
|
|
156 (defun paths-construct-path (components &optional expand-directory)
|
|
157 "Convert list of path components COMPONENTS into a path.
|
|
158 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
|
|
159 to EXPAND-FILE-NAME."
|
|
160 (let* ((reverse-components (reverse components))
|
|
161 (last-component (car reverse-components))
|
|
162 (first-components (reverse (cdr reverse-components)))
|
|
163 (path
|
|
164 (apply #'concat
|
|
165 (append (mapcar #'file-name-as-directory first-components)
|
|
166 (list last-component)))))
|
|
167 (if expand-directory
|
|
168 (expand-file-name path expand-directory)
|
|
169 path)))
|
|
170
|
|
171 (defun paths-construct-emacs-directory (root suffix base)
|
|
172 "Construct a directory name within the XEmacs hierarchy."
|
|
173 (file-name-as-directory
|
|
174 (expand-file-name
|
|
175 (concat
|
|
176 (file-name-as-directory root)
|
|
177 suffix
|
|
178 base))))
|
|
179
|
|
180 (defun paths-find-emacs-directory (roots suffix base
|
1218
|
181 &optional envvar default keep-suffix)
|
428
|
182 "Find a directory in the XEmacs hierarchy.
|
|
183 ROOTS must be a list of installation roots.
|
|
184 SUFFIX is the subdirectory from there.
|
|
185 BASE is the base to look for.
|
|
186 ENVVAR is the name of the environment variable that might also
|
|
187 specify the directory.
|
|
188 DEFAULT is the preferred value.
|
|
189 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
|
1218
|
190 the directory."
|
428
|
191 (let ((preferred-value (or (and envvar (getenv envvar))
|
|
192 default)))
|
|
193 (if (and preferred-value
|
|
194 (paths-file-readable-directory-p preferred-value))
|
|
195 (file-name-as-directory preferred-value)
|
|
196 (catch 'gotcha
|
|
197 (while roots
|
1218
|
198 (let* ((root (car roots))
|
|
199 ;; installed
|
|
200 (path (paths-construct-emacs-directory root suffix base)))
|
|
201 (if (paths-file-readable-directory-p path)
|
|
202 (throw 'gotcha path)
|
|
203 ;; in-place
|
|
204 (if (null keep-suffix)
|
|
205 (let ((path (paths-construct-emacs-directory root "" base)))
|
|
206 (if (paths-file-readable-directory-p path)
|
|
207 (throw 'gotcha path))))))
|
428
|
208 (setq roots (cdr roots)))
|
|
209 nil))))
|
|
210
|
1218
|
211 (defun paths-find-site-directory (roots base &optional envvar default)
|
|
212 "Find a site-specific directory in the XEmacs hierarchy."
|
428
|
213 (paths-find-emacs-directory roots
|
|
214 (file-name-as-directory
|
|
215 (paths-construct-path (list
|
|
216 "lib"
|
|
217 emacs-program-name)))
|
|
218 base
|
1218
|
219 envvar default))
|
428
|
220
|
|
221 (defun paths-find-version-directory (roots base
|
|
222 &optional envvar default enforce-version)
|
|
223 "Find a version-specific directory in the XEmacs hierarchy.
|
|
224 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
|
|
225 (paths-find-emacs-directory roots
|
|
226 (file-name-as-directory
|
|
227 (paths-construct-path
|
|
228 (list "lib"
|
|
229 (construct-emacs-version-name))))
|
|
230 base
|
|
231 envvar default
|
|
232 enforce-version))
|
|
233
|
|
234 (defun paths-find-architecture-directory (roots base &optional envvar default)
|
|
235 "Find an architecture-specific directory in the XEmacs hierarchy."
|
|
236 (or
|
|
237 ;; from more to less specific
|
|
238 (paths-find-version-directory roots
|
865
|
239 (paths-construct-path
|
|
240 (list system-configuration base))
|
428
|
241 envvar default)
|
|
242 (paths-find-version-directory roots
|
|
243 base
|
|
244 envvar)
|
|
245 (paths-find-version-directory roots
|
|
246 system-configuration
|
|
247 envvar)))
|
|
248
|
|
249 (defun construct-emacs-version-name ()
|
|
250 "Construct the raw XEmacs version number."
|
|
251 (concat emacs-program-name "-" emacs-program-version))
|
|
252
|
|
253 (defun paths-directories-which-exist (directories)
|
|
254 "Return the directories among DIRECTORIES."
|
|
255 (let ((reverse-directories '()))
|
|
256 (while directories
|
|
257 (if (paths-file-readable-directory-p (car directories))
|
|
258 (setq reverse-directories
|
|
259 (cons (car directories)
|
|
260 reverse-directories)))
|
|
261 (setq directories (cdr directories)))
|
|
262 (reverse reverse-directories)))
|
|
263
|
|
264 (defun paths-uniq-append (list-1 list-2)
|
|
265 "Append LIST-1 and LIST-2, omitting duplicates."
|
|
266 (let ((reverse-survivors '()))
|
|
267 (while list-2
|
|
268 (if (null (member (car list-2) list-1))
|
|
269 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
|
|
270 (setq list-2 (cdr list-2)))
|
|
271 (append list-1
|
|
272 (reverse reverse-survivors))))
|
|
273
|
|
274 (defun paths-filter (predicate list)
|
|
275 "Delete all matches of PREDICATE from LIST."
|
|
276 (let ((reverse-result '()))
|
|
277 (while list
|
|
278 (if (funcall predicate (car list))
|
|
279 (setq reverse-result (cons (car list) reverse-result)))
|
|
280 (setq list (cdr list)))
|
|
281 (nreverse reverse-result)))
|
|
282
|
|
283 (defun paths-decode-directory-path (string &optional drop-empties)
|
|
284 "Split STRING at path separators into a directory list.
|
442
|
285 Non-\"\" components are converted into directory form.
|
428
|
286 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
|
|
287 Otherwise, they are left alone."
|
|
288 (let* ((components (split-path string))
|
|
289 (directories
|
|
290 (mapcar #'(lambda (component)
|
|
291 (if (string-equal "" component)
|
|
292 component
|
|
293 (file-name-as-directory component)))
|
|
294 components)))
|
|
295 (if drop-empties
|
|
296 (paths-filter #'(lambda (component)
|
|
297 (null (string-equal "" component)))
|
|
298 directories)
|
|
299 directories)))
|
|
300
|
|
301 (defun paths-find-emacs-roots (invocation-directory
|
1227
|
302 invocation-name
|
|
303 root-p)
|
|
304 "Find all plausible installation roots for XEmacs.
|
|
305 INVOCATION-DIRECTORY is the directory from which XEmacs was started.
|
|
306 INVOCATION-NAME is the name of the XEmacs executable that was originally
|
|
307 started.
|
|
308 ROOT-P is a function that tests whether a root is plausible."
|
428
|
309 (let* ((potential-invocation-root
|
|
310 (paths-find-emacs-root invocation-directory invocation-name))
|
|
311 (invocation-roots
|
|
312 (and potential-invocation-root
|
|
313 (list potential-invocation-root)))
|
|
314 (potential-installation-roots
|
|
315 (paths-uniq-append
|
|
316 (and configure-exec-prefix-directory
|
|
317 (list (file-name-as-directory
|
|
318 configure-exec-prefix-directory)))
|
|
319 (and configure-prefix-directory
|
|
320 (list (file-name-as-directory
|
|
321 configure-prefix-directory)))))
|
|
322 (installation-roots
|
1227
|
323 (paths-filter root-p potential-installation-roots)))
|
428
|
324 (paths-uniq-append invocation-roots
|
|
325 installation-roots)))
|
|
326
|
|
327 ;;; find-paths.el ends here
|