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 (null (string-match exclude-regexp (car raw-entries)))
74 (cons (expand-file-name (car raw-entries) directory)
76 (setq raw-entries (cdr raw-entries)))
79 (paths-find-recursive-path (reverse reverse-dirs)
80 (if (numberp max-depth)
84 (setq path (nconc path
87 (setq directories (cdr directories)))
90 (defun paths-file-readable-directory-p (filename)
91 "Check if filename is a readable directory."
92 (and (file-directory-p filename)
93 (file-readable-p filename)))
95 (defun paths-find-recursive-load-path (directories &optional max-depth)
96 "Construct a recursive load path underneath DIRECTORIES."
97 (paths-find-recursive-path directories
98 max-depth paths-no-lisp-directory-regexp))
100 (defun paths-emacs-root-p (directory)
101 "Check if DIRECTORY is a plausible installation root for XEmacs."
104 (paths-file-readable-directory-p (paths-construct-path (list directory
106 emacs-program-name)))
107 ;; in-place or windows-nt
109 (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
110 (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
112 (defun paths-root-in-place-p (root)
113 "Check if ROOT is an in-place installation root for XEmacs."
114 (paths-file-readable-directory-p (paths-construct-path (list root "lisp"))))
116 (defun paths-chase-symlink (file-name)
117 "Chase a symlink until the bitter end."
118 (let ((maybe-symlink (file-symlink-p file-name)))
120 (let* ((directory (file-name-directory file-name))
121 (destination (expand-file-name maybe-symlink directory)))
122 (paths-chase-symlink destination))
125 (defun paths-find-emacs-root
126 (invocation-directory invocation-name)
127 "Find the run-time root of XEmacs."
128 (let* ((executable-file-name (paths-chase-symlink
129 (concat invocation-directory
131 (executable-directory (file-name-directory executable-file-name))
132 (maybe-root-1 (file-name-as-directory
133 (paths-construct-path '("..") executable-directory)))
134 (maybe-root-2 (file-name-as-directory
135 (paths-construct-path '(".." "..") executable-directory))))
136 (or (and (paths-emacs-root-p maybe-root-1)
138 (and (paths-emacs-root-p maybe-root-2)
141 (defun paths-construct-path (components &optional expand-directory)
142 "Convert list of path components COMPONENTS into a path.
143 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
144 to EXPAND-FILE-NAME."
145 (let* ((reverse-components (reverse components))
146 (last-component (car reverse-components))
147 (first-components (reverse (cdr reverse-components)))
150 (append (mapcar #'file-name-as-directory first-components)
151 (list last-component)))))
153 (expand-file-name path expand-directory)
156 (defun paths-construct-emacs-directory (root suffix base)
157 "Construct a directory name within the XEmacs hierarchy."
158 (file-name-as-directory
161 (file-name-as-directory root)
165 (defun paths-find-emacs-directory (roots suffix base
166 &optional envvar default keep-suffix
168 "Find a directory in the XEmacs hierarchy.
169 ROOTS must be a list of installation roots.
170 SUFFIX is the subdirectory from there.
171 BASE is the base to look for.
172 ENVVAR is the name of the environment variable that might also
173 specify the directory.
174 DEFAULT is the preferred value.
175 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
177 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
178 an in-place root-hierarchy."
179 (let ((preferred-value (or (and envvar (getenv envvar))
181 (if (and preferred-value
182 (paths-file-readable-directory-p preferred-value))
183 (file-name-as-directory preferred-value)
186 (let ((root (car roots)))
188 (let ((path (paths-construct-emacs-directory root suffix base)))
189 (if (paths-file-readable-directory-p path)
190 (throw 'gotcha path)))
192 (if (null keep-suffix)
193 (let ((path (paths-construct-emacs-directory root "" base)))
194 (if (paths-file-readable-directory-p path)
195 (throw 'gotcha path))))
196 (if (and in-place-external
197 (paths-root-in-place-p root))
198 (let ((path (paths-construct-emacs-directory
199 (paths-construct-path '("..") root)
201 (if (paths-file-readable-directory-p path)
202 (throw 'gotcha path)))))
203 (setq roots (cdr roots)))
206 (defun paths-find-site-directory (roots base &optional envvar default in-place-external)
207 "Find a site-specific directory in the XEmacs hierarchy.
208 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
209 an in-place root-hierarchy."
210 (paths-find-emacs-directory roots
211 (file-name-as-directory
212 (paths-construct-path (list
214 emacs-program-name)))
220 (defun paths-find-version-directory (roots base
221 &optional envvar default enforce-version)
222 "Find a version-specific directory in the XEmacs hierarchy.
223 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
224 (paths-find-emacs-directory roots
225 (file-name-as-directory
226 (paths-construct-path
228 (construct-emacs-version-name))))
233 (defun paths-find-architecture-directory (roots base &optional envvar default)
234 "Find an architecture-specific directory in the XEmacs hierarchy."
236 ;; from more to less specific
237 (paths-find-version-directory roots
238 (concat base system-configuration)
240 (paths-find-version-directory roots
243 (paths-find-version-directory roots
247 (defun construct-emacs-version-name ()
248 "Construct the raw XEmacs version number."
249 (concat emacs-program-name "-" emacs-program-version))
251 (defun paths-directories-which-exist (directories)
252 "Return the directories among DIRECTORIES."
253 (let ((reverse-directories '()))
255 (if (paths-file-readable-directory-p (car directories))
256 (setq reverse-directories
257 (cons (car directories)
258 reverse-directories)))
259 (setq directories (cdr directories)))
260 (reverse reverse-directories)))
262 (defun paths-uniq-append (list-1 list-2)
263 "Append LIST-1 and LIST-2, omitting duplicates."
264 (let ((reverse-survivors '()))
266 (if (null (member (car list-2) list-1))
267 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
268 (setq list-2 (cdr list-2)))
270 (reverse reverse-survivors))))
272 (defun paths-filter (predicate list)
273 "Delete all matches of PREDICATE from LIST."
274 (let ((reverse-result '()))
276 (if (funcall predicate (car list))
277 (setq reverse-result (cons (car list) reverse-result)))
278 (setq list (cdr list)))
279 (nreverse reverse-result)))
281 (defun paths-decode-directory-path (string &optional drop-empties)
282 "Split STRING at path separators into a directory list.
283 Non-\"\" components are converted into directory form.
284 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
285 Otherwise, they are left alone."
286 (let* ((components (split-path string))
288 (mapcar #'(lambda (component)
289 (if (string-equal "" component)
291 (file-name-as-directory component)))
294 (paths-filter #'(lambda (component)
295 (null (string-equal "" component)))
299 (defun paths-find-emacs-roots (invocation-directory
301 "Find all plausible installation roots for XEmacs."
302 (let* ((potential-invocation-root
303 (paths-find-emacs-root invocation-directory invocation-name))
305 (and potential-invocation-root
306 (list potential-invocation-root)))
307 (potential-installation-roots
309 (and configure-exec-prefix-directory
310 (list (file-name-as-directory
311 configure-exec-prefix-directory)))
312 (and configure-prefix-directory
313 (list (file-name-as-directory
314 configure-prefix-directory)))))
316 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
317 (paths-uniq-append invocation-roots
318 installation-roots)))
320 ;;; find-paths.el ends here