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 packages-data-path-depth 1
67 "Depth of data-path search in package hierarchies.")
69 (defvar early-packages nil
70 "Packages early in the load path.")
72 (defvar early-package-load-path nil
73 "Load path for packages early in the load path.")
75 (defvar late-packages nil
76 "Packages late in the load path.")
78 (defvar late-package-load-path nil
79 "Load path for packages late in the load path.")
81 (defvar last-packages nil
82 "Packages last in the load path.")
84 (defvar last-package-load-path nil
85 "Load path for packages last in the load path.")
87 (defvar package-locations
89 (list (paths-construct-path '("~" ".xemacs" "mule-packages"))
90 'early #'(lambda () (featurep 'mule)))
91 (list (paths-construct-path '("~" ".xemacs" "xemacs-packages"))
92 'early #'(lambda () t))
93 (list "site-packages" 'late #'(lambda () t))
94 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
95 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
96 (list "xemacs-packages" 'late #'(lambda () t)))
97 "Locations of the various package directories.
98 This is a list each of whose elements describes one directory.
99 A directory description is a three-element list.
100 The first element is either an absolute path or a subdirectory
101 in the XEmacs hierarchy.
102 The second component is one of the symbols EARLY, LATE, LAST,
103 depending on the load-path segment the hierarchy is supposed to
105 The third component is a thunk which, if it returns NIL, causes
106 the directory to be ignored.")
108 (defun package-get-key-1 (info key)
109 "Locate keyword `key' in list."
114 (t (package-get-key-1 (cddr info) key))))
116 (defun package-get-key (name key)
117 "Get info `key' from package `name'."
118 (let ((info (assq name packages-package-list)))
120 (package-get-key-1 (cdr info) key))))
122 (defun package-provide (name &rest attributes)
123 (let ((info (if (and attributes (floatp (car attributes)))
124 (list :version (car attributes))
126 (remassq name packages-package-list)
127 (setq packages-package-list
128 (cons (cons name info) packages-package-list))))
130 (defun package-require (name version)
131 (let ((pkg (assq name packages-package-list)))
133 (error "Package %s has not been loaded into this XEmacsen"
135 ((< (package-get-key name :version) version)
136 (error "Need version %g of package %s, got version %g"
137 version name (cdr pkg)))
140 (defun package-delete-name (name)
142 ;; Delete ALL versions of package.
143 ;; This is pretty memory-intensive, as we use copy-alist when deleting
144 ;; package entries, to prevent side-effects in functions that call this
146 (while (setq pkg (assq name packages-package-list))
147 (setq packages-package-list (delete pkg (copy-alist
148 packages-package-list)))
154 (defvar autoload-file-name "auto-autoloads.el"
155 "Filename that autoloads are expected to be found in.")
157 (defvar packages-hardcoded-lisp
159 ;; Nothing at this time
161 "Lisp packages that are always dumped with XEmacs.
162 This includes every package that is loaded directly by a package listed
163 in dumped-lisp.el and is not itself listed.")
165 (defvar packages-useful-lisp
170 "Lisp packages that need early byte compilation.")
172 (defvar packages-unbytecompiled-lisp
177 "very-early-lisp.el")
178 "Lisp packages that should not be byte compiled.")
181 ;; Copied from help.el, could possibly move it to here permanently.
182 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
183 ;; primitive, which should make it lightning-fast.
185 (defun locate-library (library &optional nosuffix path interactive-call)
186 "Show the precise file name of Emacs library LIBRARY.
187 This command searches the directories in `load-path' like `M-x load-library'
188 to find the file that `M-x load-library RET LIBRARY RET' would load.
189 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
190 to the specified name LIBRARY.
192 If the optional third arg PATH is specified, that list of directories
193 is used instead of `load-path'."
194 (interactive (list (read-string "Locate library: ")
201 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
202 (and (boundp 'find-file-hooks)
203 (member 'crypt-find-file-hook find-file-hooks)))
204 ;; Compression involved.
207 '(".elc" ".elc.gz" "elc.Z" ".el" ".el.gz" ".el.Z" "" ".gz" ".Z")))
212 '(".elc" ".el" "")))))))
213 (and interactive-call
215 (message "Library is file %s" result)
216 (message "No library %s in search path" library)))
219 (defun packages-add-suffix (str)
220 (if (null (string-match "\\.el\\'" str))
224 (defun packages-list-autoloads-path ()
225 "List autoloads from precomputed load-path."
226 (let ((path load-path)
229 (if (file-exists-p (concat (car path)
231 (setq autoloads (cons (concat (car path)
234 (setq path (cdr path)))
237 (defun packages-list-autoloads (source-directory)
238 "List autoload files in (what will be) the normal lisp search path.
239 This function is used during build to find where the global symbol files so
240 they can be perused for their useful information."
241 (let ((files (directory-files (file-name-as-directory source-directory)
244 ;; (print (prin1-to-string source-directory))
245 ;; (print (prin1-to-string files))
246 (while (setq file (car-safe files))
247 (if (and (file-directory-p file)
248 (file-exists-p (concat (file-name-as-directory file)
249 autoload-file-name)))
250 (setq autolist (cons (concat (file-name-as-directory file)
253 (setq files (cdr files)))
256 ;; The following function cannot be called from a bare temacs
257 (defun packages-new-autoloads ()
258 "Return autoloads files that have been added or modified since XEmacs dump."
260 (let ((me (concat invocation-directory invocation-name))
264 (setq dir (file-truename (car path)))
265 (let ((autoload-file (file-name-sans-extension (concat
267 autoload-file-name))))
269 ;; 1. An auto-autoload file that hasn't provided a feature (because
270 ;; it has been installed since XEmacs was dumped).
271 ;; 2. auto-autoload.el being newer than the executable
272 ;; 3. auto-autoload.elc being newer than the executable (the .el
273 ;; could be missing or compressed)
274 (when (or (and (null (file-provides autoload-file))
275 (or (file-exists-p (concat autoload-file ".elc"))
276 (file-exists-p (concat autoload-file ".el"))))
277 (and (file-newer-than-file-p (concat autoload-file ".el") me)
278 (setq autoload-file (concat autoload-file ".el")))
279 (and (file-newer-than-file-p (concat autoload-file
282 (setq autoload-file (concat autoload-file ".elc"))))
283 (push autoload-file result)))
284 (setq path (cdr path)))
287 ;; The following function cannot be called from a bare temacs
288 (defun packages-reload-autoloads ()
289 "Reload new or updated auto-autoloads files.
290 This is an extremely dangerous function to call after the user-init-files
291 is run. Don't call it or you'll be sorry."
292 (let ((autoload-list (packages-new-autoloads)))
294 (let* ((autoload-file (car autoload-list))
295 (feature (car-safe (file-provides autoload-file))))
297 ;; (message "(unload-feature %S)" feature)
298 (unload-feature feature))
302 (setq autoload-list (cdr autoload-list)))))
304 ;; Data-directory is really a list now. Provide something to search it for
307 (defun locate-data-directory-list (name &optional dir-list)
308 "Locate the matching list of directories in a search path DIR-LIST.
309 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
311 (setq dir-list data-directory-list))
312 (let (found found-dir found-dir-list)
314 (setq found (file-name-as-directory (concat (car dir-list) name))
315 found-dir (file-directory-p found))
317 (setq found-dir-list (cons found found-dir-list)))
318 (setq dir-list (cdr dir-list)))
319 (nreverse found-dir-list)))
321 ;; Data-directory is really a list now. Provide something to search it for
324 (defun locate-data-directory (name &optional dir-list)
325 "Locate a directory in a search path DIR-LIST (a list of directories).
326 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
328 (setq dir-list data-directory-list))
329 (let (found found-dir)
330 (while (and (null found-dir) dir-list)
331 (setq found (file-name-as-directory (concat (car dir-list) name))
332 found-dir (file-directory-p found))
335 (setq dir-list (cdr dir-list)))
338 ;; Data-directory is really a list now. Provide something to search it for
341 (defun locate-data-file (name &optional dir-list)
342 "Locate a file in a search path DIR-LIST (a list of directories).
343 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
344 This function is basically a wrapper over `locate-file'."
345 (locate-file name (or dir-list data-directory-list)))
349 (defun packages-find-package-directories (roots base)
350 "Find a set of package directories."
351 ;; make sure paths-find-version-directory and paths-find-site-directory
352 ;; don't both pick up version-independent directories ...
353 (let ((version-directory (paths-find-version-directory roots base nil nil t))
354 (site-directory (paths-find-site-directory roots base)))
356 (and version-directory (list version-directory))
357 (and site-directory (list site-directory)))))
359 (defvar packages-special-base-regexp "^\\(etc\\|info\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
360 "Special subdirectories of packages.")
362 (defvar packages-no-package-hierarchy-regexp
363 (concat "\\(" paths-version-control-filename-regexp "\\)"
365 "\\(" packages-special-base-regexp "\\)")
366 "Directories which can't be the roots of package hierarchies.")
368 (defun packages-find-packages-in-directories (directories)
369 "Find all packages underneath directories in DIRECTORIES."
370 (paths-find-recursive-path directories
371 packages-hierarchy-depth
372 packages-no-package-hierarchy-regexp))
374 (defun packages-split-path (path)
375 "Split PATH at \"\", return pair with two components.
376 The second component is shared with PATH."
377 (let ((reverse-tail '())
379 (while (and rest (null (string-equal "" (car rest))))
380 (setq reverse-tail (cons (car rest) reverse-tail))
381 (setq rest (cdr rest)))
384 (cons (nreverse reverse-tail) (cdr rest)))))
386 (defun packages-split-package-path (package-path)
387 "Split up PACKAGE-PATH into early, late and last components.
388 The separation is by \"\" components.
389 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
390 ;; When in doubt, it's late
391 (let* ((stuff (packages-split-path package-path))
392 (early (and (cdr stuff) (car stuff)))
393 (late+last (or (cdr stuff) (car stuff)))
394 (stuff (packages-split-path late+last))
397 (list (packages-find-packages-in-directories early)
398 (packages-find-packages-in-directories late)
399 (packages-find-packages-in-directories last))))
401 (defun packages-deconstruct (list consumer)
402 "Deconstruct LIST and feed it to CONSUMER."
403 (apply consumer list))
405 (defun packages-find-packages-by-name (roots name)
406 "Find a package hierarchy by its name."
407 (packages-find-packages-in-directories
408 (if (and (file-name-absolute-p name)
409 (file-name-directory (expand-file-name name)))
410 (list (file-name-as-directory (expand-file-name name)))
411 (packages-find-package-directories roots name))))
413 (defun packages-find-packages-at-time
414 (roots package-locations time &optional default)
415 "Find packages at given time.
416 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
417 TIME is either 'EARLY, 'LATE, or 'LAST.
418 DEFAULT is a default list of packages."
420 (let ((packages '()))
421 (while package-locations
422 (packages-deconstruct
423 (car package-locations)
424 #'(lambda (name a-time thunk)
425 (if (and (eq time a-time)
429 (packages-find-packages-by-name roots name))))))
430 (setq package-locations (cdr package-locations)))
433 (defun packages-find-packages (roots)
435 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
437 (packages-split-package-path (paths-decode-directory-path envvar-value))
438 (packages-deconstruct
439 (packages-split-package-path configure-package-path)
440 #'(lambda (configure-early-packages
441 configure-late-packages
442 configure-last-packages)
443 (list (packages-find-packages-at-time roots package-locations 'early
444 configure-early-packages)
445 (packages-find-packages-at-time roots package-locations 'late
446 configure-late-packages)
447 (packages-find-packages-at-time roots package-locations 'last
448 configure-last-packages)))))))
450 (defun packages-find-package-library-path (packages suffixes)
451 "Construct a path into a component of the packages hierarchy.
452 PACKAGES is a list of package directories.
453 SUFFIXES is a list of names of package subdirectories to look for."
457 (mapcar #'(lambda (package)
458 (mapcar #'(lambda (suffix)
459 (file-name-as-directory (concat package suffix)))
462 (paths-directories-which-exist directories)))
464 (defun packages-find-package-load-path (packages)
465 "Construct the load-path component for packages.
466 PACKAGES is a list of package directories."
467 (paths-find-recursive-load-path
468 (packages-find-package-library-path packages
470 packages-load-path-depth))
472 (defun packages-find-package-exec-path (packages)
473 "Construct the exec-path component for packages.
474 PACKAGES is a list of package directories."
475 (packages-find-package-library-path packages
476 (list (paths-construct-path
477 (list "bin" system-configuration))
480 (defun packages-find-package-info-path (packages)
481 "Construct the info-path component for packages.
482 PACKAGES is a list of package directories."
483 (packages-find-package-library-path packages '("info")))
485 (defun packages-find-package-data-path (packages)
486 "Construct the data-path component for packages.
487 PACKAGES is a list of package directories."
488 (paths-find-recursive-load-path
489 (packages-find-package-library-path packages
491 packages-data-path-depth))
493 ;; Loading package initialization files
495 (defun packages-load-package-lisps (package-load-path base)
496 "Load all Lisp files of a certain name along a load path.
497 BASE is the base name of the files."
498 (mapc #'(lambda (dir)
499 (let ((file-name (expand-file-name base dir)))
500 (condition-case error
503 (warn (format "Autoload error in: %s:\n\t%s"
505 (with-output-to-string
506 (display-error error nil))))))))
509 (defun packages-load-package-auto-autoloads (package-load-path)
510 "Load auto-autoload files along a load path."
511 (packages-load-package-lisps package-load-path
512 (file-name-sans-extension autoload-file-name)))
514 (defun packages-handle-package-dumped-lisps (handle package-load-path)
515 "Load dumped-lisp.el files along a load path.
516 Call HANDLE on each file off definitions of PACKAGE-LISP there."
517 (mapc #'(lambda (dir)
518 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
519 (if (file-exists-p file-name)
521 ;; 20.4 packages could set this
524 ;; dumped-lisp.el could have set this ...
526 (mapc #'(lambda (base)
527 (funcall handle base))
531 (defun packages-load-package-dumped-lisps (package-load-path)
532 "Load dumped-lisp.el files along a load path.
533 Also load files off PACKAGE-LISP definitions there"
534 (packages-handle-package-dumped-lisps #'load package-load-path))
536 (defun packages-collect-package-dumped-lisps (package-load-path)
537 "Load dumped-lisp.el files along a load path.
538 Return list of files off PACKAGE-LISP definitions there"
540 (packages-handle-package-dumped-lisps
542 (setq *files* (cons file *files*)))
548 ;;; packages.el ends here