1 ;;; packages.el --- Low level support for XEmacs packages
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Author: Steven L Baur <steve@altair.xemacs.org>
6 ;; Maintainer: Steven L Baur <steve@altair.xemacs.org>
7 ;; Keywords: internal, lisp, dumped
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 ;;; Synched up with: Not in FSF
30 ;; This file is dumped with XEmacs.
32 ;; This file provides low level facilities for XEmacs startup --
33 ;; particularly regarding the package setup. This code has to run in
34 ;; what we call "bare temacs" -- i.e. XEmacs without the usual Lisp
35 ;; environment. Pay special attention:
37 ;; - not to use the `lambda' macro. Use #'(lambda ...) instead.
38 ;; (this goes for any package loaded before `subr.el'.)
40 ;; - not to use macros, because they are not yet available (and this
41 ;; file must be loadable uncompiled.) This rules out CL-style
42 ;; macros like `when', for instance.
44 ;; - not to use `defcustom'. If you must add user-customizable
45 ;; variables here, use `defvar', and add the variable to
48 ;; Because of all this, make sure that the stuff you put here really
51 ;; This file requires find-paths.el.
55 ;;; Package versioning
57 (defvar packages-package-list nil
58 "database of loaded packages and version numbers")
60 (defvar packages-hierarchy-depth 1
61 "Depth of package hierarchies.")
63 (defvar packages-load-path-depth 1
64 "Depth of load-path search in package hierarchies.")
66 (defvar early-packages nil
67 "Packages early in the load path.")
69 (defvar early-package-load-path nil
70 "Load path for packages early in the load path.")
72 (defvar late-packages nil
73 "Packages late in the load path.")
75 (defvar late-package-load-path nil
76 "Load path for packages late in the load path.")
78 (defvar last-packages nil
79 "Packages last in the load path.")
81 (defvar last-package-load-path nil
82 "Load path for packages last in the load path.")
84 (defvar package-locations
86 (list (paths-construct-path '("~" ".xemacs"))
87 'early #'(lambda () t))
88 (list "site-packages" 'late #'(lambda () t))
89 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
90 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
91 (list "xemacs-packages" 'late #'(lambda () t))
92 (list "packages" 'late #'(lambda () t)))
93 "Locations of the various package directories.
94 This is a list each of whose elements describes one directory.
95 A directory description is a three-element list.
96 The first element is either an absolute path or a subdirectory
97 in the XEmacs hierarchy.
98 The second component is one of the symbols EARLY, LATE, LAST,
99 depending on the load-path segment the hierarchy is supposed to
101 The third component is a thunk which, if it returns NIL, causes
102 the directory to be ignored.")
104 (defun package-get-key-1 (info key)
105 "Locate keyword `key' in list."
110 (t (package-get-key-1 (cddr info) key))))
112 (defun package-get-key (name key)
113 "Get info `key' from package `name'."
114 (let ((info (assq name packages-package-list)))
116 (package-get-key-1 (cdr info) key))))
118 (defun package-provide (name &rest attributes)
119 (let ((info (if (and attributes (floatp (car attributes)))
120 (list :version (car attributes))
122 (remassq name packages-package-list)
123 (setq packages-package-list
124 (cons (cons name info) packages-package-list))))
126 (defun package-require (name version)
127 (let ((pkg (assq name packages-package-list)))
129 (error "Package %s has not been loaded into this XEmacsen"
131 ((< (package-get-key name :version) version)
132 (error "Need version %g of package %s, got version %g"
133 version name (cdr pkg)))
138 (defvar autoload-file-name "auto-autoloads.el"
139 "Filename that autoloads are expected to be found in.")
141 (defvar packages-hardcoded-lisp
143 ;; Nothing at this time
145 "Lisp packages that are always dumped with XEmacs.
146 This includes every package that is loaded directly by a package listed
147 in dumped-lisp.el and is not itself listed.")
149 (defvar packages-useful-lisp
154 "Lisp packages that need early byte compilation.")
156 (defvar packages-unbytecompiled-lisp
163 "Lisp packages that should not be byte compiled.")
166 ;; Copied from help.el, could possibly move it to here permanently.
167 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
168 ;; primitive, which should make it lightning-fast.
170 (defun locate-library (library &optional nosuffix path interactive-call)
171 "Show the precise file name of Emacs library LIBRARY.
172 This command searches the directories in `load-path' like `M-x load-library'
173 to find the file that `M-x load-library RET LIBRARY RET' would load.
174 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
175 to the specified name LIBRARY.
177 If the optional third arg PATH is specified, that list of directories
178 is used instead of `load-path'."
179 (interactive (list (read-string "Locate library: ")
186 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
187 (and (boundp 'find-file-hooks)
188 (member 'crypt-find-file-hook find-file-hooks)))
189 ;; Compression involved.
192 ".elc:.elc.gz:elc.Z:.el:.el.gz:.el.Z::.gz:.Z"))
199 (and interactive-call
201 (message "Library is file %s" result)
202 (message "No library %s in search path" library)))
205 (defun packages-add-suffix (str)
206 (if (null (string-match "\\.el\\'" str))
210 (defun packages-list-autoloads-path ()
211 "List autoloads from precomputed load-path."
212 (let ((path load-path)
215 (if (file-exists-p (concat (car path)
217 (setq autoloads (cons (concat (car path)
220 (setq path (cdr path)))
223 (defun packages-list-autoloads ()
224 "List autoload files in (what will be) the normal lisp search path.
225 This function is used during build to find where the global symbol files so
226 they can be perused for their useful information."
227 ;; Source directory may not be initialized yet.
228 ;; (print (prin1-to-string load-path))
229 (if (null source-directory)
230 (setq source-directory (car load-path)))
231 (let ((files (directory-files (file-name-as-directory source-directory)
234 ;; (print (prin1-to-string source-directory))
235 ;; (print (prin1-to-string files))
236 (while (setq file (car-safe files))
237 (if (and (file-directory-p file)
238 (file-exists-p (concat (file-name-as-directory file)
239 autoload-file-name)))
240 (setq autolist (cons (concat (file-name-as-directory file)
243 (setq files (cdr files)))
246 ;; The following function cannot be called from a bare temacs
247 (defun packages-new-autoloads ()
248 "Return autoloads files that have been added or modified since XEmacs dump."
250 (let ((me (concat invocation-directory invocation-name))
254 (setq dir (file-truename (car path)))
255 (let ((autoload-file (file-name-sans-extension (concat
257 autoload-file-name))))
259 ;; 1. An auto-autoload file that hasn't provided a feature (because
260 ;; it has been installed since XEmacs was dumped).
261 ;; 2. auto-autoload.el being newer than the executable
262 ;; 3. auto-autoload.elc being newer than the executable (the .el
263 ;; could be missing or compressed)
264 (when (or (and (null (file-provides autoload-file))
265 (or (file-exists-p (concat autoload-file ".elc"))
266 (file-exists-p (concat autoload-file ".el"))))
267 (and (file-newer-than-file-p (concat autoload-file ".el") me)
268 (setq autoload-file (concat autoload-file ".el")))
269 (and (file-newer-than-file-p (concat autoload-file
272 (setq autoload-file (concat autoload-file ".elc"))))
273 (push autoload-file result)))
274 (setq path (cdr path)))
277 ;; The following function cannot be called from a bare temacs
278 (defun packages-reload-autoloads ()
279 "Reload new or updated auto-autoloads files.
280 This is an extremely dangerous function to call after the user-init-files
281 is run. Don't call it or you'll be sorry."
282 (let ((autoload-list (packages-new-autoloads)))
284 (let* ((autoload-file (car autoload-list))
285 (feature (car-safe (file-provides autoload-file))))
287 ;; (message "(unload-feature %S)" feature)
288 (unload-feature feature))
292 (setq autoload-list (cdr autoload-list)))))
294 ;; Data-directory is really a list now. Provide something to search it for
297 (defun locate-data-directory (name &optional dir-list)
298 "Locate a directory in a search path DIR-LIST (a list of directories).
299 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
301 (setq dir-list data-directory-list))
302 (let (found found-dir)
303 (while (and (null found-dir) dir-list)
304 (setq found (file-name-as-directory (concat (car dir-list) name))
305 found-dir (file-directory-p found))
308 (setq dir-list (cdr dir-list)))
311 ;; Data-directory is really a list now. Provide something to search it for
314 (defun locate-data-file (name &optional dir-list)
315 "Locate a file in a search path DIR-LIST (a list of directories).
316 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
317 This function is basically a wrapper over `locate-file'."
319 (setq dir-list data-directory-list))
320 (locate-file name dir-list))
324 (defun packages-find-package-directories (roots base)
325 "Find a set of package directories."
326 ;; make sure paths-find-version-directory and paths-find-site-directory
327 ;; don't both pick up version-independent directories ...
328 (let ((version-directory (paths-find-version-directory roots base nil nil t))
329 (site-directory (paths-find-site-directory roots base)))
331 (and version-directory (list version-directory))
332 (and site-directory (list site-directory)))))
334 (defvar packages-special-base-regexp "^\\(etc\\|info\\|lisp\\|lib-src\\|bin\\)$"
335 "Special subdirectories of packages.")
337 (defvar packages-no-package-hierarchy-regexp
338 (concat "\\(" paths-version-control-filename-regexp "\\)"
340 "\\(" packages-special-base-regexp "\\)")
341 "Directories which can't be the roots of package hierarchies.")
343 (defun packages-find-packages-in-directories (directories)
344 "Find all packages underneath directories in DIRECTORIES."
345 (paths-find-recursive-path directories
346 packages-hierarchy-depth
347 packages-no-package-hierarchy-regexp))
349 (defun packages-split-path (path)
350 "Split PATH at \"\", return pair with two components.
351 The second component is shared with PATH."
352 (let ((reverse-tail '())
354 (while (and rest (null (string-equal "" (car rest))))
355 (setq reverse-tail (cons (car rest) reverse-tail))
356 (setq rest (cdr rest)))
359 (cons (nreverse reverse-tail) (cdr rest)))))
361 (defun packages-split-package-path (package-path)
362 "Split up PACKAGE-PATH into early, late and last components.
363 The separation is by \"\" components.
364 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
365 ;; When in doubt, it's late
366 (let* ((stuff (packages-split-path package-path))
367 (early (and (cdr stuff) (car stuff)))
368 (late+last (or (cdr stuff) (car stuff)))
369 (stuff (packages-split-path late+last))
372 (list (packages-find-packages-in-directories early)
373 (packages-find-packages-in-directories late)
374 (packages-find-packages-in-directories last))))
376 (defun packages-deconstruct (list consumer)
377 "Deconstruct LIST and feed it to CONSUMER."
378 (apply consumer list))
380 (defun packages-find-packages-by-name (roots name)
381 "Find a package hierarchy by its name."
382 (packages-find-packages-in-directories
383 (if (and (file-name-absolute-p name)
384 (file-name-directory (expand-file-name name)))
385 (list (file-name-as-directory (expand-file-name name)))
386 (packages-find-package-directories roots name))))
388 (defun packages-find-packages-at-time
389 (roots package-locations time &optional default)
390 "Find packages at given time.
391 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
392 TIME is either 'EARLY, 'LATE, or 'LAST.
393 DEFAULT is a default list of packages."
395 (let ((packages '()))
396 (while package-locations
397 (packages-deconstruct
398 (car package-locations)
399 #'(lambda (name a-time thunk)
400 (if (and (eq time a-time)
404 (packages-find-packages-by-name roots name))))))
405 (setq package-locations (cdr package-locations)))
408 (defun packages-find-packages (roots)
410 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
412 (packages-split-package-path (paths-decode-directory-path envvar-value))
413 (packages-deconstruct
414 (packages-split-package-path configure-package-path)
415 #'(lambda (configure-early-packages
416 configure-late-packages
417 configure-last-packages)
418 (list (packages-find-packages-at-time roots package-locations 'early
419 configure-early-packages)
420 (packages-find-packages-at-time roots package-locations 'late
421 configure-late-packages)
422 (packages-find-packages-at-time roots package-locations 'last
423 configure-last-packages)))))))
425 (defun packages-find-package-library-path (packages suffixes)
426 "Construct a path into a component of the packages hierarchy.
427 PACKAGES is a list of package directories.
428 SUFFIXES is a list of names of package subdirectories to look for."
432 (mapcar #'(lambda (package)
433 (mapcar #'(lambda (suffix)
434 (file-name-as-directory (concat package suffix)))
437 (paths-directories-which-exist directories)))
439 (defun packages-find-package-load-path (packages)
440 "Construct the load-path component for packages.
441 PACKAGES is a list of package directories."
442 (paths-find-recursive-load-path
443 (packages-find-package-library-path packages
445 packages-load-path-depth))
447 (defun packages-find-package-exec-path (packages)
448 (packages-find-package-library-path packages
449 (list (paths-construct-path
450 (list "bin" system-configuration))
453 (defun packages-find-package-info-path (packages)
454 (packages-find-package-library-path packages '("info")))
456 (defun packages-find-package-data-path (packages)
457 (packages-find-package-library-path packages '("etc")))
459 ;; Loading package initialization files
461 (defun packages-load-package-lisps (package-load-path base)
462 "Load all Lisp files of a certain name along a load path.
463 BASE is the base name of the files."
464 (mapc #'(lambda (dir)
465 (let ((file-name (expand-file-name base dir)))
466 (condition-case error
469 (warn (format "Autoload error in: %s:\n\t%s"
471 (with-output-to-string
472 (display-error error nil))))))))
475 (defun packages-load-package-auto-autoloads (package-load-path)
476 "Load auto-autoload files along a load path."
477 (packages-load-package-lisps package-load-path
478 (file-name-sans-extension autoload-file-name)))
480 (defun packages-handle-package-dumped-lisps (handle package-load-path)
481 "Load dumped-lisp.el files along a load path.
482 Call HANDLE on each file off definitions of PACKAGE-LISP there."
483 (mapc #'(lambda (dir)
484 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
485 (if (file-exists-p file-name)
487 ;; 20.4 packages could set this
490 ;; dumped-lisp.el could have set this ...
492 (mapc #'(lambda (base)
493 (funcall handle base))
497 (defun packages-load-package-dumped-lisps (package-load-path)
498 "Load dumped-lisp.el files along a load path.
499 Also load files off PACKAGE-LISP definitions there"
500 (packages-handle-package-dumped-lisps #'load package-load-path))
502 (defun packages-collect-package-dumped-lisps (package-load-path)
503 "Load dumped-lisp.el files along a load path.
504 Return list of files off PACKAGE-LISP definitions there"
506 (packages-handle-package-dumped-lisps
508 (setq *files* (cons file *files*)))
514 ;;; packages.el ends here