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 ;; searching for a package directory on Windows
115 (string-match "win32\\|cygwin" system-configuration)
116 (paths-file-readable-directory-p (paths-construct-path (list directory "xemacs-packages"))))))
118 (defun paths-root-in-place-p (root)
119 "Check if ROOT is an in-place installation root for XEmacs."
120 (paths-file-readable-directory-p (paths-construct-path (list root "lisp"))))
122 (defun paths-chase-symlink (file-name)
123 "Chase a symlink until the bitter end."
124 (let ((maybe-symlink (file-symlink-p file-name)))
126 (let* ((directory (file-name-directory file-name))
127 (destination (expand-file-name maybe-symlink directory)))
128 (paths-chase-symlink destination))
131 (defun paths-find-invocation-roots (invocation-directory invocation-name)
132 "Find the list of run-time roots of XEmacs.
133 INVOCATION-DIRECTORY is a directory containing the XEmacs executable.
134 INVOCATION-NAME is the name of the executable itself."
135 (let* ((executable-file-name (paths-chase-symlink
136 (concat invocation-directory
138 (executable-directory (file-name-directory executable-file-name))
139 (maybe-root-1 (file-name-as-directory
140 (paths-construct-path '("..") executable-directory)))
141 (maybe-root-2 (file-name-as-directory
142 (paths-construct-path '(".." "..") executable-directory))))
144 (paths-filter #'paths-emacs-root-p
145 (list maybe-root-1 maybe-root-2))))
147 (defun paths-construct-path (components &optional expand-directory)
148 "Convert list of path components COMPONENTS into a path.
149 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
150 to EXPAND-FILE-NAME."
151 (let* ((reverse-components (reverse components))
152 (last-component (car reverse-components))
153 (first-components (reverse (cdr reverse-components)))
156 (append (mapcar #'file-name-as-directory first-components)
157 (list last-component)))))
159 (expand-file-name path expand-directory)
162 (defun paths-construct-emacs-directory (root suffix base)
163 "Construct a directory name within the XEmacs hierarchy."
164 (file-name-as-directory
167 (file-name-as-directory root)
171 (defun paths-find-emacs-directory (roots suffix base
172 &optional envvar default keep-suffix
174 "Find a directory in the XEmacs hierarchy.
175 ROOTS must be a list of installation roots.
176 SUFFIX is the subdirectory from there.
177 BASE is the base to look for.
178 ENVVAR is the name of the environment variable that might also
179 specify the directory.
180 DEFAULT is the preferred value.
181 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
183 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
184 an in-place root-hierarchy."
185 (let ((preferred-value (or (and envvar (getenv envvar))
187 (if (and preferred-value
188 (paths-file-readable-directory-p preferred-value))
189 (file-name-as-directory preferred-value)
192 (let ((root (car roots)))
194 (let ((path (paths-construct-emacs-directory root suffix base)))
195 (if (paths-file-readable-directory-p path)
196 (throw 'gotcha path)))
198 (if (null keep-suffix)
199 (let ((path (paths-construct-emacs-directory root "" base)))
200 (if (paths-file-readable-directory-p path)
201 (throw 'gotcha path))))
202 (if (and in-place-external
203 (paths-root-in-place-p root))
204 (let ((path (paths-construct-emacs-directory
205 (paths-construct-path '("..") root)
207 (if (paths-file-readable-directory-p path)
208 (throw 'gotcha path)))))
209 (setq roots (cdr roots)))
212 (defun paths-find-site-directory (roots base &optional envvar default in-place-external)
213 "Find a site-specific directory in the XEmacs hierarchy.
214 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
215 an in-place root-hierarchy."
216 (paths-find-emacs-directory roots
217 (file-name-as-directory
218 (paths-construct-path (list
220 emacs-program-name)))
226 (defun paths-find-version-directory (roots base
227 &optional envvar default enforce-version)
228 "Find a version-specific directory in the XEmacs hierarchy.
229 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
230 (paths-find-emacs-directory roots
231 (file-name-as-directory
232 (paths-construct-path
234 (construct-emacs-version-name))))
239 (defun paths-find-architecture-directory (roots base &optional envvar default)
240 "Find an architecture-specific directory in the XEmacs hierarchy."
242 ;; from more to less specific
243 (paths-find-version-directory roots
244 (paths-construct-path
245 (list system-configuration base))
247 (paths-find-version-directory roots
250 (paths-find-version-directory roots
254 (defun construct-emacs-version-name ()
255 "Construct the raw XEmacs version number."
256 (concat emacs-program-name "-" emacs-program-version))
258 (defun paths-directories-which-exist (directories)
259 "Return the directories among DIRECTORIES."
260 (let ((reverse-directories '()))
262 (if (paths-file-readable-directory-p (car directories))
263 (setq reverse-directories
264 (cons (car directories)
265 reverse-directories)))
266 (setq directories (cdr directories)))
267 (reverse reverse-directories)))
269 (defun paths-uniq-append (list-1 list-2)
270 "Append LIST-1 and LIST-2, omitting duplicates."
271 (let ((reverse-survivors '()))
273 (if (null (member (car list-2) list-1))
274 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
275 (setq list-2 (cdr list-2)))
277 (reverse reverse-survivors))))
279 (defun paths-filter (predicate list)
280 "Delete all matches of PREDICATE from LIST."
281 (let ((reverse-result '()))
283 (if (funcall predicate (car list))
284 (setq reverse-result (cons (car list) reverse-result)))
285 (setq list (cdr list)))
286 (nreverse reverse-result)))
288 (defun paths-decode-directory-path (string &optional drop-empties)
289 "Split STRING at path separators into a directory list.
290 Non-\"\" components are converted into directory form.
291 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
292 Otherwise, they are left alone."
293 (let* ((components (split-path string))
295 (mapcar #'(lambda (component)
296 (if (string-equal "" component)
298 (file-name-as-directory component)))
301 (paths-filter #'(lambda (component)
302 (null (string-equal "" component)))
306 (defun paths-find-emacs-roots (invocation-directory
308 "Find all plausible installation roots for XEmacs."
309 (let* ((invocation-roots
310 (paths-find-invocation-roots invocation-directory invocation-name))
311 (potential-installation-roots
313 (and configure-exec-prefix-directory
314 (list (file-name-as-directory
315 configure-exec-prefix-directory)))
316 (and configure-prefix-directory
317 (list (file-name-as-directory
318 configure-prefix-directory)))))
320 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
321 (paths-uniq-append invocation-roots
322 installation-roots)))
324 ;;; find-paths.el ends here