267
|
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
|
|
34 ;; This file contains the library functionality to find paths into the
|
|
35 ;; XEmacs hierarchy.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
286
|
39 (defvar paths-version-control-filename-regexp
|
|
40 "^\\(RCS\\|CVS\\|SCCS\\)$"
|
267
|
41 "File bases associated with version control.")
|
|
42
|
286
|
43 (defvar paths-lisp-filename-regexp
|
|
44 "^\\(.*\\.elc?\\)$"
|
|
45 "File bases that contain Lisp file.")
|
|
46
|
|
47 (defvar paths-no-lisp-directory-regexp
|
|
48 (concat "\\(" paths-version-control-filename-regexp "\\)"
|
|
49 "\\|"
|
|
50 "\\(" paths-lisp-filename-regexp "\\)")
|
|
51 "File bases that may not be directories containing Lisp code.")
|
|
52
|
|
53 (defun paths-find-recursive-path (directories &optional max-depth exclude-regexp)
|
267
|
54 "Return a list of the directory hierarchy underneath DIRECTORIES.
|
276
|
55 The returned list is sorted by pre-order and lexicographically.
|
|
56 MAX-DEPTH limits the depth of the search to MAX-DEPTH level,
|
|
57 if it is a number. If MAX-DEPTH is NIL, the search depth is unlimited.
|
286
|
58 EXCLUDE-REGEXP is a regexp that matches directory names to exclude
|
|
59 from the search."
|
267
|
60 (let ((path '()))
|
|
61 (while directories
|
|
62 (let ((directory (file-name-as-directory
|
|
63 (expand-file-name
|
|
64 (car directories)))))
|
|
65 (if (file-directory-p directory)
|
286
|
66 (let ((raw-entries
|
276
|
67 (if (equal 0 max-depth)
|
|
68 '()
|
286
|
69 (directory-files directory nil "^[^.-]")))
|
267
|
70 (reverse-dirs '()))
|
|
71
|
286
|
72 (while raw-entries
|
|
73 (if (null (string-match exclude-regexp (car raw-entries)))
|
267
|
74 (setq reverse-dirs
|
286
|
75 (cons (expand-file-name (car raw-entries) directory)
|
267
|
76 reverse-dirs)))
|
286
|
77 (setq raw-entries (cdr raw-entries)))
|
267
|
78
|
|
79 (let ((sub-path
|
276
|
80 (paths-find-recursive-path (reverse reverse-dirs)
|
|
81 (if (numberp max-depth)
|
|
82 (- max-depth 1)
|
|
83 max-depth)
|
286
|
84 exclude-regexp)))
|
267
|
85 (setq path (nconc path
|
|
86 (list directory)
|
|
87 sub-path))))))
|
|
88 (setq directories (cdr directories)))
|
|
89 path))
|
|
90
|
276
|
91 (defun paths-find-recursive-load-path (directories &optional max-depth)
|
267
|
92 "Construct a recursive load path underneath DIRECTORIES."
|
276
|
93 (paths-find-recursive-path directories
|
286
|
94 max-depth paths-no-lisp-directory-regexp))
|
267
|
95
|
|
96 (defun paths-emacs-root-p (directory)
|
|
97 "Check if DIRECTORY is a plausible installation root for XEmacs."
|
|
98 (or
|
|
99 ;; installed
|
280
|
100 (file-directory-p (paths-construct-path (list directory
|
|
101 "lib"
|
|
102 emacs-program-name)))
|
267
|
103 ;; in-place
|
|
104 (and
|
276
|
105 (file-directory-p (paths-construct-path (list directory "lib-src")))
|
|
106 (file-directory-p (paths-construct-path (list directory "lisp")))
|
|
107 (file-directory-p (paths-construct-path (list directory "src"))))))
|
267
|
108
|
274
|
109 (defun paths-chase-symlink (file-name)
|
|
110 "Chase a symlink until the bitter end."
|
|
111 (let ((maybe-symlink (file-symlink-p file-name)))
|
|
112 (if maybe-symlink
|
|
113 (let* ((directory (file-name-directory file-name))
|
|
114 (destination (expand-file-name maybe-symlink directory)))
|
|
115 (paths-chase-symlink destination))
|
|
116 file-name)))
|
|
117
|
267
|
118 (defun paths-find-emacs-root
|
|
119 (invocation-directory invocation-name)
|
|
120 "Find the run-time root of XEmacs."
|
274
|
121 (let* ((executable-file-name (paths-chase-symlink
|
|
122 (concat invocation-directory
|
|
123 invocation-name)))
|
|
124 (executable-directory (file-name-directory executable-file-name))
|
|
125 (maybe-root-1 (file-name-as-directory
|
276
|
126 (paths-construct-path '("..") executable-directory)))
|
274
|
127 (maybe-root-2 (file-name-as-directory
|
276
|
128 (paths-construct-path '(".." "..") executable-directory))))
|
274
|
129 (or (and (paths-emacs-root-p maybe-root-1)
|
|
130 maybe-root-1)
|
|
131 (and (paths-emacs-root-p maybe-root-2)
|
|
132 maybe-root-2))))
|
267
|
133
|
276
|
134 (defun paths-construct-path (components &optional expand-directory)
|
|
135 "Convert list of path components COMPONENTS into a path.
|
|
136 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
|
|
137 to EXPAND-FILE-NAME."
|
|
138 (let* ((reverse-components (reverse components))
|
|
139 (last-component (car reverse-components))
|
|
140 (first-components (reverse (cdr reverse-components)))
|
|
141 (path
|
|
142 (apply #'concat
|
|
143 (append (mapcar #'file-name-as-directory first-components)
|
|
144 (list last-component)))))
|
|
145 (if expand-directory
|
|
146 (expand-file-name path expand-directory)
|
|
147 path)))
|
|
148
|
267
|
149 (defun paths-construct-emacs-directory (root suffix base)
|
|
150 "Construct a directory name within the XEmacs hierarchy."
|
|
151 (file-name-as-directory
|
|
152 (expand-file-name
|
|
153 (concat
|
|
154 (file-name-as-directory root)
|
|
155 suffix
|
|
156 base))))
|
|
157
|
280
|
158 (defun paths-find-emacs-directory (roots suffix base
|
|
159 &optional envvar default keep-suffix)
|
267
|
160 "Find a directory in the XEmacs hierarchy.
|
|
161 ROOTS must be a list of installation roots.
|
|
162 SUFFIX is the subdirectory from there.
|
|
163 BASE is the base to look for.
|
|
164 ENVVAR is the name of the environment variable that might also
|
|
165 specify the directory.
|
280
|
166 DEFAULT is the preferred value.
|
|
167 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
|
|
168 the directory."
|
276
|
169 (let ((preferred-value (or (and envvar (getenv envvar))
|
|
170 default)))
|
|
171 (if (and preferred-value
|
|
172 (file-directory-p preferred-value))
|
|
173 (file-name-as-directory preferred-value)
|
267
|
174 (catch 'gotcha
|
|
175 (while roots
|
|
176 (let* ((root (car roots))
|
280
|
177 ;; installed
|
267
|
178 (path (paths-construct-emacs-directory root suffix base)))
|
|
179 (if (file-directory-p path)
|
|
180 (throw 'gotcha path)
|
280
|
181 ;; in-place
|
|
182 (if (null keep-suffix)
|
|
183 (let ((path (paths-construct-emacs-directory root "" base)))
|
|
184 (if (file-directory-p path)
|
|
185 (throw 'gotcha path))))))
|
267
|
186 (setq roots (cdr roots)))
|
276
|
187 nil))))
|
267
|
188
|
|
189 (defun paths-find-site-directory (roots base &optional envvar default)
|
|
190 "Find a site-specific directory in the XEmacs hierarchy."
|
276
|
191 (paths-find-emacs-directory roots
|
|
192 (file-name-as-directory
|
280
|
193 (paths-construct-path (list
|
|
194 "lib"
|
|
195 emacs-program-name)))
|
276
|
196 base
|
|
197 envvar default))
|
267
|
198
|
280
|
199 (defun paths-find-version-directory (roots base
|
|
200 &optional envvar default enforce-version)
|
|
201 "Find a version-specific directory in the XEmacs hierarchy.
|
|
202 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
|
267
|
203 (paths-find-emacs-directory roots
|
276
|
204 (file-name-as-directory
|
|
205 (paths-construct-path
|
|
206 (list "lib"
|
280
|
207 (construct-emacs-version-name))))
|
267
|
208 base
|
280
|
209 envvar default
|
|
210 enforce-version))
|
267
|
211
|
|
212 (defun paths-find-architecture-directory (roots base &optional envvar default)
|
|
213 "Find an architecture-specific directory in the XEmacs hierarchy."
|
|
214 (or
|
|
215 ;; from more to less specific
|
|
216 (paths-find-version-directory roots
|
|
217 (concat base system-configuration)
|
269
|
218 envvar)
|
|
219 (paths-find-version-directory roots
|
|
220 base
|
|
221 envvar)
|
267
|
222 (paths-find-version-directory roots
|
|
223 system-configuration
|
|
224 envvar default)))
|
|
225
|
280
|
226 (defun construct-emacs-version-name ()
|
|
227 "Construct the raw XEmacs version number."
|
|
228 (concat emacs-program-name "-" emacs-program-version))
|
267
|
229
|
|
230 (defun paths-directories-which-exist (directories)
|
|
231 "Return the directories among DIRECTORIES."
|
|
232 (let ((reverse-directories '()))
|
|
233 (while directories
|
|
234 (if (file-directory-p (car directories))
|
|
235 (setq reverse-directories
|
|
236 (cons (car directories)
|
|
237 reverse-directories)))
|
|
238 (setq directories (cdr directories)))
|
|
239 (reverse reverse-directories)))
|
|
240
|
274
|
241 (defun paths-uniq-append (list-1 list-2)
|
|
242 "Append LIST-1 and LIST-2, omitting duplicates."
|
|
243 (let ((reverse-survivors '()))
|
|
244 (while list-2
|
|
245 (if (null (member (car list-2) list-1))
|
|
246 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
|
|
247 (setq list-2 (cdr list-2)))
|
|
248 (append list-1
|
|
249 (reverse reverse-survivors))))
|
|
250
|
278
|
251 (defun paths-filter (predicate list)
|
276
|
252 "Delete all matches of PREDICATE from LIST."
|
|
253 (let ((reverse-result '()))
|
|
254 (while list
|
278
|
255 (if (funcall predicate (car list))
|
276
|
256 (setq reverse-result (cons (car list) reverse-result)))
|
|
257 (setq list (cdr list)))
|
|
258 (nreverse reverse-result)))
|
267
|
259
|
276
|
260 (defun paths-decode-directory-path (string &optional drop-empties)
|
|
261 "Split STRING at path separators into a directory list.
|
|
262 Non-\"\" comonents are converted into directory form.
|
|
263 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
|
|
264 Otherwise, they are left alone."
|
284
|
265 (let* ((components (split-path string))
|
276
|
266 (directories
|
|
267 (mapcar #'(lambda (component)
|
|
268 (if (string-equal "" component)
|
|
269 component
|
|
270 (file-name-as-directory component)))
|
|
271 components)))
|
|
272 (if drop-empties
|
278
|
273 (paths-filter #'(lambda (component)
|
|
274 (null (string-equal "" component)))
|
276
|
275 directories)
|
|
276 directories)))
|
|
277
|
267
|
278 (defun paths-find-emacs-roots (invocation-directory
|
|
279 invocation-name)
|
|
280 "Find all plausible installation roots for XEmacs."
|
278
|
281 (let* ((potential-invocation-root
|
|
282 (paths-find-emacs-root invocation-directory invocation-name))
|
|
283 (invocation-roots
|
|
284 (and potential-invocation-root
|
|
285 (list potential-invocation-root)))
|
|
286 (potential-installation-roots
|
|
287 (paths-uniq-append
|
|
288 (and configure-exec-prefix-directory
|
|
289 (list (file-name-as-directory
|
|
290 configure-exec-prefix-directory)))
|
|
291 (and configure-prefix-directory
|
|
292 (list (file-name-as-directory
|
|
293 configure-prefix-directory)))))
|
|
294 (installation-roots
|
|
295 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
|
|
296 (paths-uniq-append invocation-roots
|
|
297 installation-roots)))
|
267
|
298
|
|
299 ;;; find-paths.el ends here
|