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-chase-symlink (file-name)
113 "Chase a symlink until the bitter end."
114 (let ((maybe-symlink (file-symlink-p file-name)))
116 (let* ((directory (file-name-directory file-name))
117 (destination (expand-file-name maybe-symlink directory)))
118 (paths-chase-symlink destination))
121 (defun paths-find-emacs-root
122 (invocation-directory invocation-name)
123 "Find the run-time root of XEmacs."
124 (let* ((executable-file-name (paths-chase-symlink
125 (concat invocation-directory
127 (executable-directory (file-name-directory executable-file-name))
128 (maybe-root-1 (file-name-as-directory
129 (paths-construct-path '("..") executable-directory)))
130 (maybe-root-2 (file-name-as-directory
131 (paths-construct-path '(".." "..") executable-directory))))
132 (or (and (paths-emacs-root-p maybe-root-1)
134 (and (paths-emacs-root-p maybe-root-2)
137 (defun paths-construct-path (components &optional expand-directory)
138 "Convert list of path components COMPONENTS into a path.
139 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
140 to EXPAND-FILE-NAME."
141 (let* ((reverse-components (reverse components))
142 (last-component (car reverse-components))
143 (first-components (reverse (cdr reverse-components)))
146 (append (mapcar #'file-name-as-directory first-components)
147 (list last-component)))))
149 (expand-file-name path expand-directory)
152 (defun paths-construct-emacs-directory (root suffix base)
153 "Construct a directory name within the XEmacs hierarchy."
154 (file-name-as-directory
157 (file-name-as-directory root)
161 (defun paths-find-emacs-directory (roots suffix base
162 &optional envvar default keep-suffix)
163 "Find a directory in the XEmacs hierarchy.
164 ROOTS must be a list of installation roots.
165 SUFFIX is the subdirectory from there.
166 BASE is the base to look for.
167 ENVVAR is the name of the environment variable that might also
168 specify the directory.
169 DEFAULT is the preferred value.
170 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
172 (let ((preferred-value (or (and envvar (getenv envvar))
174 (if (and preferred-value
175 (paths-file-readable-directory-p preferred-value))
176 (file-name-as-directory preferred-value)
179 (let* ((root (car roots))
181 (path (paths-construct-emacs-directory root suffix base)))
182 (if (paths-file-readable-directory-p path)
185 (if (null keep-suffix)
186 (let ((path (paths-construct-emacs-directory root "" base)))
187 (if (paths-file-readable-directory-p path)
188 (throw 'gotcha path))))))
189 (setq roots (cdr roots)))
192 (defun paths-find-site-directory (roots base &optional envvar default)
193 "Find a site-specific directory in the XEmacs hierarchy."
194 (paths-find-emacs-directory roots
195 (file-name-as-directory
196 (paths-construct-path (list
198 emacs-program-name)))
202 (defun paths-find-version-directory (roots base
203 &optional envvar default enforce-version)
204 "Find a version-specific directory in the XEmacs hierarchy.
205 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
206 (paths-find-emacs-directory roots
207 (file-name-as-directory
208 (paths-construct-path
210 (construct-emacs-version-name))))
215 (defun paths-find-architecture-directory (roots base &optional envvar default)
216 "Find an architecture-specific directory in the XEmacs hierarchy."
218 ;; from more to less specific
219 (paths-find-version-directory roots
220 (concat base system-configuration)
222 (paths-find-version-directory roots
225 (paths-find-version-directory roots
229 (defun construct-emacs-version-name ()
230 "Construct the raw XEmacs version number."
231 (concat emacs-program-name "-" emacs-program-version))
233 (defun paths-directories-which-exist (directories)
234 "Return the directories among DIRECTORIES."
235 (let ((reverse-directories '()))
237 (if (paths-file-readable-directory-p (car directories))
238 (setq reverse-directories
239 (cons (car directories)
240 reverse-directories)))
241 (setq directories (cdr directories)))
242 (reverse reverse-directories)))
244 (defun paths-uniq-append (list-1 list-2)
245 "Append LIST-1 and LIST-2, omitting duplicates."
246 (let ((reverse-survivors '()))
248 (if (null (member (car list-2) list-1))
249 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
250 (setq list-2 (cdr list-2)))
252 (reverse reverse-survivors))))
254 (defun paths-filter (predicate list)
255 "Delete all matches of PREDICATE from LIST."
256 (let ((reverse-result '()))
258 (if (funcall predicate (car list))
259 (setq reverse-result (cons (car list) reverse-result)))
260 (setq list (cdr list)))
261 (nreverse reverse-result)))
263 (defun paths-decode-directory-path (string &optional drop-empties)
264 "Split STRING at path separators into a directory list.
265 Non-\"\" comonents are converted into directory form.
266 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
267 Otherwise, they are left alone."
268 (let* ((components (split-path string))
270 (mapcar #'(lambda (component)
271 (if (string-equal "" component)
273 (file-name-as-directory component)))
276 (paths-filter #'(lambda (component)
277 (null (string-equal "" component)))
281 (defun paths-find-emacs-roots (invocation-directory
283 "Find all plausible installation roots for XEmacs."
284 (let* ((potential-invocation-root
285 (paths-find-emacs-root invocation-directory invocation-name))
287 (and potential-invocation-root
288 (list potential-invocation-root)))
289 (potential-installation-roots
291 (and configure-exec-prefix-directory
292 (list (file-name-as-directory
293 configure-exec-prefix-directory)))
294 (and configure-prefix-directory
295 (list (file-name-as-directory
296 configure-prefix-directory)))))
298 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
299 (paths-uniq-append invocation-roots
300 installation-roots)))
302 ;;; find-paths.el ends here