1 ;;; find-paths.el --- setup various XEmacs paths
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
7 ;; Author: Mike Sperber <sperber@informatik.uni-tuebingen.de>
8 ;; Maintainer: XEmacs Development Team
9 ;; Keywords: internal, dumped
11 ;; This file is part of XEmacs.
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)
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.
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.
28 ;;; Synched up with: Not in FSF.
32 ;; This file is dumped with XEmacs.
34 ;; This file contains the library functionality to find paths into the
39 (defvar paths-version-control-filename-regexp
40 "^\\(RCS\\|CVS\\|SCCS\\)$"
41 "File bases associated with version control.")
43 (defvar paths-lisp-filename-regexp
45 "File bases that contain Lisp file.")
47 (defvar paths-no-lisp-directory-regexp
48 (concat "\\(" paths-version-control-filename-regexp "\\)"
50 "\\(" paths-lisp-filename-regexp "\\)")
51 "File bases that may not be directories containing Lisp code.")
53 (defun paths-find-recursive-path (directories &optional max-depth exclude-regexp)
54 "Return a list of the directory hierarchy underneath DIRECTORIES.
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.
58 EXCLUDE-REGEXP is a regexp that matches directory names to exclude
62 (let ((directory (file-name-as-directory
65 (if (paths-file-readable-directory-p directory)
67 (if (equal 0 max-depth)
69 (directory-files directory nil "^[^.-]")))
72 (if (not (and exclude-regexp
73 (string-match exclude-regexp (car raw-entries))))
75 (cons (expand-file-name (car raw-entries) directory)
77 (setq raw-entries (cdr raw-entries)))
80 (paths-find-recursive-path (reverse reverse-dirs)
81 (if (numberp max-depth)
85 (setq path (nconc path
88 (setq directories (cdr directories)))
91 (defun paths-file-readable-directory-p (filename)
92 "Check if filename is a readable directory."
93 (and (file-directory-p filename)
94 (file-readable-p filename)))
96 (defun paths-find-recursive-load-path (directories &optional max-depth)
97 "Construct a recursive load path underneath DIRECTORIES."
98 (paths-find-recursive-path directories
99 max-depth paths-no-lisp-directory-regexp))
101 (defun paths-emacs-root-p (directory)
102 "Check if DIRECTORY is a plausible installation root for XEmacs."
105 (paths-file-readable-directory-p (paths-construct-path (list directory
107 emacs-program-name)))
108 ;; in-place or windows-nt
110 (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
111 (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
113 (defun paths-root-in-place-p (root)
114 "Check if ROOT is an in-place installation root for XEmacs."
115 (paths-file-readable-directory-p (paths-construct-path (list root "lisp"))))
117 (defun paths-chase-symlink (file-name)
118 "Chase a symlink until the bitter end."
119 (let ((maybe-symlink (file-symlink-p file-name)))
121 (let* ((directory (file-name-directory file-name))
122 (destination (expand-file-name maybe-symlink directory)))
123 (paths-chase-symlink destination))
126 (defun paths-find-emacs-root
127 (invocation-directory invocation-name)
128 "Find the run-time root of XEmacs."
129 (let* ((executable-file-name (paths-chase-symlink
130 (concat invocation-directory
132 (executable-directory (file-name-directory executable-file-name))
133 (maybe-root-1 (file-name-as-directory
134 (paths-construct-path '("..") executable-directory)))
135 (maybe-root-2 (file-name-as-directory
136 (paths-construct-path '(".." "..") executable-directory))))
137 (or (and (paths-emacs-root-p maybe-root-1)
139 (and (paths-emacs-root-p maybe-root-2)
142 (defun paths-construct-path (components &optional expand-directory)
143 "Convert list of path components COMPONENTS into a path.
144 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
145 to EXPAND-FILE-NAME."
146 (let* ((reverse-components (reverse components))
147 (last-component (car reverse-components))
148 (first-components (reverse (cdr reverse-components)))
151 (append (mapcar #'file-name-as-directory first-components)
152 (list last-component)))))
154 (expand-file-name path expand-directory)
157 (defun paths-construct-emacs-directory (root suffix base)
158 "Construct a directory name within the XEmacs hierarchy."
159 (file-name-as-directory
162 (file-name-as-directory root)
166 (defun paths-find-emacs-directory (roots suffix base
167 &optional envvar default keep-suffix
169 "Find a directory in the XEmacs hierarchy.
170 ROOTS must be a list of installation roots.
171 SUFFIX is the subdirectory from there.
172 BASE is the base to look for.
173 ENVVAR is the name of the environment variable that might also
174 specify the directory.
175 DEFAULT is the preferred value.
176 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
178 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
179 an in-place root-hierarchy."
180 (let ((preferred-value (or (and envvar (getenv envvar))
182 (if (and preferred-value
183 (paths-file-readable-directory-p preferred-value))
184 (file-name-as-directory preferred-value)
187 (let ((root (car roots)))
189 (let ((path (paths-construct-emacs-directory root suffix base)))
190 (if (paths-file-readable-directory-p path)
191 (throw 'gotcha path)))
193 (if (null keep-suffix)
194 (let ((path (paths-construct-emacs-directory root "" base)))
195 (if (paths-file-readable-directory-p path)
196 (throw 'gotcha path))))
197 (if (and in-place-external
198 (paths-root-in-place-p root))
199 (let ((path (paths-construct-emacs-directory
200 (paths-construct-path '("..") root)
202 (if (paths-file-readable-directory-p path)
203 (throw 'gotcha path)))))
204 (setq roots (cdr roots)))
207 (defun paths-find-site-directory (roots base &optional envvar default in-place-external)
208 "Find a site-specific directory in the XEmacs hierarchy.
209 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
210 an in-place root-hierarchy."
211 (paths-find-emacs-directory roots
212 (file-name-as-directory
213 (paths-construct-path (list
215 emacs-program-name)))
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
229 (construct-emacs-version-name))))
234 (defun paths-find-architecture-directory (roots base &optional envvar default)
235 "Find an architecture-specific directory in the XEmacs hierarchy."
237 ;; from more to less specific
238 (paths-find-version-directory roots
239 (paths-construct-path
240 (list system-configuration base))
242 (paths-find-version-directory roots
245 (paths-find-version-directory roots
249 (defun construct-emacs-version-name ()
250 "Construct the raw XEmacs version number."
251 (concat emacs-program-name "-" emacs-program-version))
253 (defun paths-directories-which-exist (directories)
254 "Return the directories among DIRECTORIES."
255 (let ((reverse-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)))
264 (defun paths-uniq-append (list-1 list-2)
265 "Append LIST-1 and LIST-2, omitting duplicates."
266 (let ((reverse-survivors '()))
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)))
272 (reverse reverse-survivors))))
274 (defun paths-filter (predicate list)
275 "Delete all matches of PREDICATE from LIST."
276 (let ((reverse-result '()))
278 (if (funcall predicate (car list))
279 (setq reverse-result (cons (car list) reverse-result)))
280 (setq list (cdr list)))
281 (nreverse reverse-result)))
283 (defun paths-decode-directory-path (string &optional drop-empties)
284 "Split STRING at path separators into a directory list.
285 Non-\"\" components are converted into directory form.
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))
290 (mapcar #'(lambda (component)
291 (if (string-equal "" component)
293 (file-name-as-directory component)))
296 (paths-filter #'(lambda (component)
297 (null (string-equal "" component)))
301 (defun paths-find-emacs-roots (invocation-directory
303 "Find all plausible installation roots for XEmacs."
304 (let* ((potential-invocation-root
305 (paths-find-emacs-root invocation-directory invocation-name))
307 (and potential-invocation-root
308 (list potential-invocation-root)))
309 (potential-installation-roots
311 (and configure-exec-prefix-directory
312 (list (file-name-as-directory
313 configure-exec-prefix-directory)))
314 (and configure-prefix-directory
315 (list (file-name-as-directory
316 configure-prefix-directory)))))
318 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
319 (paths-uniq-append invocation-roots
320 installation-roots)))
322 ;;; find-paths.el ends here