428
+ − 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
776
+ − 34 ;; This file contains basic library functionality for manipulating paths
+ − 35 ;; and path lists and finding paths in the XEmacs hierarchy.
+ − 36
428
+ − 37
+ − 38 ;;; Code:
+ − 39
+ − 40 (defvar paths-version-control-filename-regexp
+ − 41 "^\\(RCS\\|CVS\\|SCCS\\)$"
+ − 42 "File bases associated with version control.")
+ − 43
+ − 44 (defvar paths-lisp-filename-regexp
+ − 45 "^\\(.*\\.elc?\\)$"
+ − 46 "File bases that contain Lisp file.")
+ − 47
+ − 48 (defvar paths-no-lisp-directory-regexp
+ − 49 (concat "\\(" paths-version-control-filename-regexp "\\)"
+ − 50 "\\|"
+ − 51 "\\(" paths-lisp-filename-regexp "\\)")
+ − 52 "File bases that may not be directories containing Lisp code.")
+ − 53
+ − 54 (defun paths-find-recursive-path (directories &optional max-depth exclude-regexp)
+ − 55 "Return a list of the directory hierarchy underneath DIRECTORIES.
+ − 56 The returned list is sorted by pre-order and lexicographically.
+ − 57 MAX-DEPTH limits the depth of the search to MAX-DEPTH level,
+ − 58 if it is a number. If MAX-DEPTH is NIL, the search depth is unlimited.
+ − 59 EXCLUDE-REGEXP is a regexp that matches directory names to exclude
+ − 60 from the search."
+ − 61 (let ((path '()))
+ − 62 (while directories
+ − 63 (let ((directory (file-name-as-directory
+ − 64 (expand-file-name
+ − 65 (car directories)))))
+ − 66 (if (paths-file-readable-directory-p directory)
+ − 67 (let ((raw-entries
+ − 68 (if (equal 0 max-depth)
+ − 69 '()
+ − 70 (directory-files directory nil "^[^.-]")))
+ − 71 (reverse-dirs '()))
+ − 72 (while raw-entries
531
+ − 73 (if (not (and exclude-regexp
+ − 74 (string-match exclude-regexp (car raw-entries))))
428
+ − 75 (setq reverse-dirs
+ − 76 (cons (expand-file-name (car raw-entries) directory)
+ − 77 reverse-dirs)))
+ − 78 (setq raw-entries (cdr raw-entries)))
+ − 79
+ − 80 (let ((sub-path
+ − 81 (paths-find-recursive-path (reverse reverse-dirs)
+ − 82 (if (numberp max-depth)
+ − 83 (- max-depth 1)
+ − 84 max-depth)
+ − 85 exclude-regexp)))
+ − 86 (setq path (nconc path
+ − 87 (list directory)
+ − 88 sub-path))))))
+ − 89 (setq directories (cdr directories)))
+ − 90 path))
+ − 91
+ − 92 (defun paths-file-readable-directory-p (filename)
+ − 93 "Check if filename is a readable directory."
+ − 94 (and (file-directory-p filename)
+ − 95 (file-readable-p filename)))
+ − 96
+ − 97 (defun paths-find-recursive-load-path (directories &optional max-depth)
+ − 98 "Construct a recursive load path underneath DIRECTORIES."
+ − 99 (paths-find-recursive-path directories
+ − 100 max-depth paths-no-lisp-directory-regexp))
+ − 101
+ − 102 (defun paths-emacs-root-p (directory)
+ − 103 "Check if DIRECTORY is a plausible installation root for XEmacs."
+ − 104 (or
+ − 105 ;; installed
+ − 106 (paths-file-readable-directory-p (paths-construct-path (list directory
+ − 107 "lib"
689
+ − 108 (construct-emacs-version-name))))
428
+ − 109 ;; in-place or windows-nt
+ − 110 (and
+ − 111 (paths-file-readable-directory-p (paths-construct-path (list directory "lisp")))
+ − 112 (paths-file-readable-directory-p (paths-construct-path (list directory "etc"))))))
+ − 113
442
+ − 114 (defun paths-root-in-place-p (root)
+ − 115 "Check if ROOT is an in-place installation root for XEmacs."
+ − 116 (paths-file-readable-directory-p (paths-construct-path (list root "lisp"))))
+ − 117
428
+ − 118 (defun paths-chase-symlink (file-name)
+ − 119 "Chase a symlink until the bitter end."
+ − 120 (let ((maybe-symlink (file-symlink-p file-name)))
+ − 121 (if maybe-symlink
+ − 122 (let* ((directory (file-name-directory file-name))
+ − 123 (destination (expand-file-name maybe-symlink directory)))
+ − 124 (paths-chase-symlink destination))
+ − 125 file-name)))
+ − 126
+ − 127 (defun paths-find-emacs-root
+ − 128 (invocation-directory invocation-name)
+ − 129 "Find the run-time root of XEmacs."
+ − 130 (let* ((executable-file-name (paths-chase-symlink
+ − 131 (concat invocation-directory
+ − 132 invocation-name)))
+ − 133 (executable-directory (file-name-directory executable-file-name))
+ − 134 (maybe-root-1 (file-name-as-directory
+ − 135 (paths-construct-path '("..") executable-directory)))
+ − 136 (maybe-root-2 (file-name-as-directory
+ − 137 (paths-construct-path '(".." "..") executable-directory))))
+ − 138 (or (and (paths-emacs-root-p maybe-root-1)
+ − 139 maybe-root-1)
+ − 140 (and (paths-emacs-root-p maybe-root-2)
+ − 141 maybe-root-2))))
+ − 142
+ − 143 (defun paths-construct-path (components &optional expand-directory)
+ − 144 "Convert list of path components COMPONENTS into a path.
+ − 145 If EXPAND-DIRECTORY is non-NIL, use it as a directory to feed
+ − 146 to EXPAND-FILE-NAME."
+ − 147 (let* ((reverse-components (reverse components))
+ − 148 (last-component (car reverse-components))
+ − 149 (first-components (reverse (cdr reverse-components)))
+ − 150 (path
+ − 151 (apply #'concat
+ − 152 (append (mapcar #'file-name-as-directory first-components)
+ − 153 (list last-component)))))
+ − 154 (if expand-directory
+ − 155 (expand-file-name path expand-directory)
+ − 156 path)))
+ − 157
+ − 158 (defun paths-construct-emacs-directory (root suffix base)
+ − 159 "Construct a directory name within the XEmacs hierarchy."
+ − 160 (file-name-as-directory
+ − 161 (expand-file-name
+ − 162 (concat
+ − 163 (file-name-as-directory root)
+ − 164 suffix
+ − 165 base))))
+ − 166
+ − 167 (defun paths-find-emacs-directory (roots suffix base
442
+ − 168 &optional envvar default keep-suffix
+ − 169 in-place-external)
428
+ − 170 "Find a directory in the XEmacs hierarchy.
+ − 171 ROOTS must be a list of installation roots.
+ − 172 SUFFIX is the subdirectory from there.
+ − 173 BASE is the base to look for.
+ − 174 ENVVAR is the name of the environment variable that might also
+ − 175 specify the directory.
+ − 176 DEFAULT is the preferred value.
+ − 177 If KEEP-SUFFIX is non-nil, the suffix must be respected in searching
442
+ − 178 the directory.
+ − 179 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
+ − 180 an in-place root-hierarchy."
428
+ − 181 (let ((preferred-value (or (and envvar (getenv envvar))
+ − 182 default)))
+ − 183 (if (and preferred-value
+ − 184 (paths-file-readable-directory-p preferred-value))
+ − 185 (file-name-as-directory preferred-value)
+ − 186 (catch 'gotcha
+ − 187 (while roots
442
+ − 188 (let ((root (car roots)))
+ − 189 ;; installed
+ − 190 (let ((path (paths-construct-emacs-directory root suffix base)))
+ − 191 (if (paths-file-readable-directory-p path)
+ − 192 (throw 'gotcha path)))
+ − 193 ;; in-place
+ − 194 (if (null keep-suffix)
+ − 195 (let ((path (paths-construct-emacs-directory root "" base)))
+ − 196 (if (paths-file-readable-directory-p path)
+ − 197 (throw 'gotcha path))))
+ − 198 (if (and in-place-external
+ − 199 (paths-root-in-place-p root))
+ − 200 (let ((path (paths-construct-emacs-directory
+ − 201 (paths-construct-path '("..") root)
+ − 202 "" base)))
+ − 203 (if (paths-file-readable-directory-p path)
+ − 204 (throw 'gotcha path)))))
428
+ − 205 (setq roots (cdr roots)))
+ − 206 nil))))
+ − 207
442
+ − 208 (defun paths-find-site-directory (roots base &optional envvar default in-place-external)
+ − 209 "Find a site-specific directory in the XEmacs hierarchy.
+ − 210 If IN-PLACE-EXTERNAL is non-nil, the directory might be found outside
+ − 211 an in-place root-hierarchy."
428
+ − 212 (paths-find-emacs-directory roots
+ − 213 (file-name-as-directory
+ − 214 (paths-construct-path (list
+ − 215 "lib"
+ − 216 emacs-program-name)))
+ − 217 base
442
+ − 218 envvar default
+ − 219 nil
+ − 220 in-place-external))
428
+ − 221
+ − 222 (defun paths-find-version-directory (roots base
+ − 223 &optional envvar default enforce-version)
+ − 224 "Find a version-specific directory in the XEmacs hierarchy.
+ − 225 If ENFORCE-VERSION is non-nil, the directory must contain the XEmacs version."
+ − 226 (paths-find-emacs-directory roots
+ − 227 (file-name-as-directory
+ − 228 (paths-construct-path
+ − 229 (list "lib"
+ − 230 (construct-emacs-version-name))))
+ − 231 base
+ − 232 envvar default
+ − 233 enforce-version))
+ − 234
+ − 235 (defun paths-find-architecture-directory (roots base &optional envvar default)
+ − 236 "Find an architecture-specific directory in the XEmacs hierarchy."
+ − 237 (or
+ − 238 ;; from more to less specific
+ − 239 (paths-find-version-directory roots
865
+ − 240 (paths-construct-path
+ − 241 (list system-configuration base))
428
+ − 242 envvar default)
+ − 243 (paths-find-version-directory roots
+ − 244 base
+ − 245 envvar)
+ − 246 (paths-find-version-directory roots
+ − 247 system-configuration
+ − 248 envvar)))
+ − 249
+ − 250 (defun construct-emacs-version-name ()
+ − 251 "Construct the raw XEmacs version number."
+ − 252 (concat emacs-program-name "-" emacs-program-version))
+ − 253
+ − 254 (defun paths-directories-which-exist (directories)
+ − 255 "Return the directories among DIRECTORIES."
+ − 256 (let ((reverse-directories '()))
+ − 257 (while directories
+ − 258 (if (paths-file-readable-directory-p (car directories))
+ − 259 (setq reverse-directories
+ − 260 (cons (car directories)
+ − 261 reverse-directories)))
+ − 262 (setq directories (cdr directories)))
+ − 263 (reverse reverse-directories)))
+ − 264
+ − 265 (defun paths-uniq-append (list-1 list-2)
+ − 266 "Append LIST-1 and LIST-2, omitting duplicates."
+ − 267 (let ((reverse-survivors '()))
+ − 268 (while list-2
+ − 269 (if (null (member (car list-2) list-1))
+ − 270 (setq reverse-survivors (cons (car list-2) reverse-survivors)))
+ − 271 (setq list-2 (cdr list-2)))
+ − 272 (append list-1
+ − 273 (reverse reverse-survivors))))
+ − 274
+ − 275 (defun paths-filter (predicate list)
+ − 276 "Delete all matches of PREDICATE from LIST."
+ − 277 (let ((reverse-result '()))
+ − 278 (while list
+ − 279 (if (funcall predicate (car list))
+ − 280 (setq reverse-result (cons (car list) reverse-result)))
+ − 281 (setq list (cdr list)))
+ − 282 (nreverse reverse-result)))
+ − 283
+ − 284 (defun paths-decode-directory-path (string &optional drop-empties)
+ − 285 "Split STRING at path separators into a directory list.
442
+ − 286 Non-\"\" components are converted into directory form.
428
+ − 287 If DROP-EMPTIES is non-NIL, \"\" components are dropped from the output.
+ − 288 Otherwise, they are left alone."
+ − 289 (let* ((components (split-path string))
+ − 290 (directories
+ − 291 (mapcar #'(lambda (component)
+ − 292 (if (string-equal "" component)
+ − 293 component
+ − 294 (file-name-as-directory component)))
+ − 295 components)))
+ − 296 (if drop-empties
+ − 297 (paths-filter #'(lambda (component)
+ − 298 (null (string-equal "" component)))
+ − 299 directories)
+ − 300 directories)))
+ − 301
+ − 302 (defun paths-find-emacs-roots (invocation-directory
+ − 303 invocation-name)
+ − 304 "Find all plausible installation roots for XEmacs."
+ − 305 (let* ((potential-invocation-root
+ − 306 (paths-find-emacs-root invocation-directory invocation-name))
+ − 307 (invocation-roots
+ − 308 (and potential-invocation-root
+ − 309 (list potential-invocation-root)))
+ − 310 (potential-installation-roots
+ − 311 (paths-uniq-append
+ − 312 (and configure-exec-prefix-directory
+ − 313 (list (file-name-as-directory
+ − 314 configure-exec-prefix-directory)))
+ − 315 (and configure-prefix-directory
+ − 316 (list (file-name-as-directory
+ − 317 configure-prefix-directory)))))
+ − 318 (installation-roots
+ − 319 (paths-filter #'paths-emacs-root-p potential-installation-roots)))
+ − 320 (paths-uniq-append invocation-roots
+ − 321 installation-roots)))
+ − 322
+ − 323 ;;; find-paths.el ends here