XEmacs 21.4.4 "Artificial Intelligence".
[chise/xemacs-chise.git.1] / lisp / find-paths.el
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 \f
37 ;;; Code:
38
39 (defvar paths-version-control-filename-regexp
40   "^\\(RCS\\|CVS\\|SCCS\\)$"
41   "File bases associated with version control.")
42
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)
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
59 from the search."
60   (let ((path '()))
61     (while directories
62       (let ((directory (file-name-as-directory
63                         (expand-file-name
64                          (car directories)))))
65         (if (paths-file-readable-directory-p directory)
66             (let ((raw-entries
67                    (if (equal 0 max-depth)
68                        '()
69                      (directory-files directory nil "^[^.-]")))
70                   (reverse-dirs '()))
71               (while raw-entries
72                 (if (not (and exclude-regexp
73                               (string-match exclude-regexp (car raw-entries))))
74                     (setq reverse-dirs
75                           (cons (expand-file-name (car raw-entries) directory)
76                                 reverse-dirs)))
77                 (setq raw-entries (cdr raw-entries)))
78
79               (let ((sub-path
80                      (paths-find-recursive-path (reverse reverse-dirs)
81                                                 (if (numberp max-depth)
82                                                     (- max-depth 1)
83                                                   max-depth)
84                                                 exclude-regexp)))
85                 (setq path (nconc path
86                                   (list directory)
87                                   sub-path))))))
88       (setq directories (cdr directories)))
89     path))
90
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)))
95
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))
100
101 (defun paths-emacs-root-p (directory)
102   "Check if DIRECTORY is a plausible installation root for XEmacs."
103   (or
104    ;; installed
105    (paths-file-readable-directory-p (paths-construct-path (list directory
106                                                                 "lib"
107                                                                 emacs-program-name)))
108    ;; in-place or windows-nt
109    (and
110     (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
111     (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
112
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"))))
116
117 (defun paths-chase-symlink (file-name)
118   "Chase a symlink until the bitter end."
119       (let ((maybe-symlink (file-symlink-p file-name)))
120         (if maybe-symlink
121             (let* ((directory (file-name-directory file-name))
122                    (destination (expand-file-name maybe-symlink directory)))
123               (paths-chase-symlink destination))
124           file-name)))
125
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
131                                         invocation-name)))
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)
138              maybe-root-1)
139         (and (paths-emacs-root-p maybe-root-2)
140              maybe-root-2))))
141
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)))
149          (path
150           (apply #'concat
151                  (append (mapcar #'file-name-as-directory first-components)
152                          (list last-component)))))
153     (if expand-directory
154         (expand-file-name path expand-directory)
155       path)))
156
157 (defun paths-construct-emacs-directory (root suffix base)
158   "Construct a directory name within the XEmacs hierarchy."
159   (file-name-as-directory
160    (expand-file-name
161     (concat
162      (file-name-as-directory root)
163      suffix
164      base))))
165
166 (defun paths-find-emacs-directory (roots suffix base
167                                    &optional envvar default keep-suffix
168                                              in-place-external)
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
177 the directory.
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))
181                              default)))
182     (if (and preferred-value
183              (paths-file-readable-directory-p preferred-value))
184         (file-name-as-directory preferred-value)
185       (catch 'gotcha
186         (while roots
187           (let ((root (car roots)))
188             ;; installed
189             (let ((path (paths-construct-emacs-directory root suffix base)))
190               (if (paths-file-readable-directory-p path)
191                   (throw 'gotcha path)))
192             ;; in-place
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)
201                              "" base)))
202                   (if (paths-file-readable-directory-p path)
203                       (throw 'gotcha path)))))
204           (setq roots (cdr roots)))
205         nil))))
206
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
214                                                       "lib"
215                                                       emacs-program-name)))
216                               base
217                               envvar default
218                               nil
219                               in-place-external))
220
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
228                                 (list "lib"
229                                       (construct-emacs-version-name))))
230                               base
231                               envvar default
232                               enforce-version))
233
234 (defun paths-find-architecture-directory (roots base &optional envvar default)
235   "Find an architecture-specific directory in the XEmacs hierarchy."
236   (or
237    ;; from more to less specific
238    (paths-find-version-directory roots
239                                  (concat base system-configuration)
240                                  envvar default)
241    (paths-find-version-directory roots
242                                  base
243                                  envvar)
244    (paths-find-version-directory roots
245                                  system-configuration
246                                  envvar)))
247
248 (defun construct-emacs-version-name ()
249   "Construct the raw XEmacs version number."
250   (concat emacs-program-name "-" emacs-program-version))
251
252 (defun paths-directories-which-exist (directories)
253   "Return the directories among DIRECTORIES."
254   (let ((reverse-directories '()))
255     (while directories
256       (if (paths-file-readable-directory-p (car directories))
257           (setq reverse-directories
258                 (cons (car directories)
259                       reverse-directories)))
260       (setq directories (cdr directories)))
261     (reverse reverse-directories)))
262
263 (defun paths-uniq-append (list-1 list-2)
264   "Append LIST-1 and LIST-2, omitting duplicates."
265   (let ((reverse-survivors '()))
266     (while list-2
267       (if (null (member (car list-2) list-1))
268           (setq reverse-survivors (cons (car list-2) reverse-survivors)))
269       (setq list-2 (cdr list-2)))
270     (append list-1
271             (reverse reverse-survivors))))
272
273 (defun paths-filter (predicate list)
274   "Delete all matches of PREDICATE from LIST."
275   (let ((reverse-result '()))
276     (while list
277       (if (funcall predicate (car list))
278           (setq reverse-result (cons (car list) reverse-result)))
279       (setq list (cdr list)))
280     (nreverse reverse-result)))
281
282 (defun paths-decode-directory-path (string &optional drop-empties)
283   "Split STRING at path separators into a directory list.
284 Non-\"\" components are converted into directory form.
285 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
286 Otherwise, they are left alone."
287   (let* ((components (split-path string))
288          (directories
289           (mapcar #'(lambda (component)
290                       (if (string-equal "" component)
291                           component
292                         (file-name-as-directory component)))
293                   components)))
294     (if drop-empties
295         (paths-filter #'(lambda (component)
296                           (null (string-equal "" component)))
297                       directories)
298       directories)))
299
300 (defun paths-find-emacs-roots (invocation-directory
301                                invocation-name)
302   "Find all plausible installation roots for XEmacs."
303   (let* ((potential-invocation-root
304           (paths-find-emacs-root invocation-directory invocation-name))
305          (invocation-roots
306           (and potential-invocation-root
307                (list potential-invocation-root)))
308          (potential-installation-roots
309           (paths-uniq-append
310            (and configure-exec-prefix-directory
311                 (list (file-name-as-directory
312                        configure-exec-prefix-directory)))
313            (and configure-prefix-directory
314                 (list (file-name-as-directory
315                        configure-prefix-directory)))))
316          (installation-roots
317           (paths-filter #'paths-emacs-root-p potential-installation-roots)))
318     (paths-uniq-append invocation-roots
319                        installation-roots)))
320
321 ;;; find-paths.el ends here