1 ;;; packages.el --- Low level support for XEmacs packages
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Author: Steven L Baur <steve@xemacs.org>
6 ;; Maintainer: Steven L Baur <steve@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.) Built in macros, such as
42 ;; `when' and `unless' are fine, of course.
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 installed 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 (defun packages-compute-package-locations (user-init-directory)
88 "Compute locations of the various package directories.
89 This is a list each of whose elements describes one directory.
90 A directory description is a three-element list.
91 The first element is either an absolute path or a subdirectory
92 in the XEmacs hierarchy.
93 The second component is one of the symbols EARLY, LATE, LAST,
94 depending on the load-path segment the hierarchy is supposed to
96 The third component is a thunk which, if it returns NIL, causes
97 the directory to be ignored."
99 (list (paths-construct-path (list user-init-directory "mule-packages"))
100 'early #'(lambda () (featurep 'mule)))
101 (list (paths-construct-path (list user-init-directory "xemacs-packages"))
102 'early #'(lambda () t))
103 (list "site-packages" 'late #'(lambda () t))
104 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
105 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
106 (list "xemacs-packages" 'late #'(lambda () t))))
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 (setq packages-package-list
127 (cons (cons name info) (remassq name packages-package-list)))))
129 (defun package-require (name version)
130 (let ((pkg (assq name packages-package-list)))
132 (error "Package %s has not been loaded into this XEmacsen"
134 ((< (package-get-key name :version) version)
135 (error "Need version %g of package %s, got version %g"
136 version name (cdr pkg)))
139 (defun package-delete-name (name)
141 ;; Delete ALL versions of package.
142 ;; This is pretty memory-intensive, as we use copy-alist when deleting
143 ;; package entries, to prevent side-effects in functions that call this
145 (while (setq pkg (assq name packages-package-list))
146 (setq packages-package-list (delete pkg (copy-alist
147 packages-package-list)))
153 (defvar autoload-file-name "auto-autoloads.el"
154 "Filename that autoloads are expected to be found in.")
156 (defvar packages-hardcoded-lisp
158 ;; Nothing at this time
160 "Lisp packages that are always dumped with XEmacs.
161 This includes every package that is loaded directly by a package listed
162 in dumped-lisp.el and is not itself listed.")
164 (defvar packages-useful-lisp
169 "Lisp packages that need early byte compilation.")
171 (defvar packages-unbytecompiled-lisp
176 "very-early-lisp.el")
177 "Lisp packages that should not be byte compiled.")
180 ;; Copied from help.el, could possibly move it to here permanently.
181 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
182 ;; primitive, which should make it lightning-fast.
184 (defun locate-library (library &optional nosuffix path interactive-call)
185 "Show the precise file name of Emacs library LIBRARY.
186 This command searches the directories in `load-path' like `M-x load-library'
187 to find the file that `M-x load-library RET LIBRARY RET' would load.
188 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
189 to the specified name LIBRARY.
191 If the optional third arg PATH is specified, that list of directories
192 is used instead of `load-path'."
193 (interactive (list (read-string "Locate library: ")
200 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
201 (and (boundp 'find-file-hooks)
202 (member 'crypt-find-file-hook find-file-hooks)))
203 ;; Compression involved.
205 '("" ".gz" ".Z" ".bz2")
206 '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
207 ".el" ".el.gz" ".el.Z" ".el.bz2"
208 "" ".gz" ".Z" ".bz2")))
213 '(".elc" ".el" "")))))))
214 (and interactive-call
216 (message "Library is file %s" result)
217 (message "No library %s in search path" library)))
220 (defun packages-add-suffix (str)
221 (if (null (string-match "\\.el\\'" str))
225 (defun packages-list-autoloads-path ()
226 "List autoloads from precomputed load-path."
227 (let ((path load-path)
230 (if (file-exists-p (concat (car path)
232 (setq autoloads (cons (concat (car path)
235 (setq path (cdr path)))
238 (defun packages-list-autoloads (source-directory)
239 "List autoload files in (what will be) the normal lisp search path.
240 This function is used during build to find where the global symbol files so
241 they can be perused for their useful information."
242 (let ((files (directory-files (file-name-as-directory source-directory)
245 ;; (print (prin1-to-string source-directory))
246 ;; (print (prin1-to-string files))
247 (while (setq file (car-safe files))
248 (if (and (file-directory-p file)
249 (file-exists-p (concat (file-name-as-directory file)
250 autoload-file-name)))
251 (setq autolist (cons (concat (file-name-as-directory file)
254 (setq files (cdr files)))
257 ;; The following function cannot be called from a bare temacs
258 (defun packages-new-autoloads ()
259 "Return autoloads files that have been added or modified since XEmacs dump."
261 (let ((me (concat invocation-directory invocation-name))
265 (setq dir (file-truename (car path)))
266 (let ((autoload-file (file-name-sans-extension (concat
268 autoload-file-name))))
270 ;; 1. An auto-autoload file that hasn't provided a feature (because
271 ;; it has been installed since XEmacs was dumped).
272 ;; 2. auto-autoload.el being newer than the executable
273 ;; 3. auto-autoload.elc being newer than the executable (the .el
274 ;; could be missing or compressed)
275 (when (or (and (null (file-provides autoload-file))
276 (or (file-exists-p (concat autoload-file ".elc"))
277 (file-exists-p (concat autoload-file ".el"))))
278 (and (file-newer-than-file-p (concat autoload-file ".el") me)
279 (setq autoload-file (concat autoload-file ".el")))
280 (and (file-newer-than-file-p (concat autoload-file
283 (setq autoload-file (concat autoload-file ".elc"))))
284 (push autoload-file result)))
285 (setq path (cdr path)))
288 ;; The following function cannot be called from a bare temacs
289 (defun packages-reload-autoloads ()
290 "Reload new or updated auto-autoloads files.
291 This is an extremely dangerous function to call after the user-init-files
292 is run. Don't call it or you'll be sorry."
293 (let ((autoload-list (packages-new-autoloads)))
295 (let* ((autoload-file (car autoload-list))
296 (feature (car-safe (file-provides autoload-file))))
298 ;; (message "(unload-feature %S)" feature)
299 (unload-feature feature))
303 (setq autoload-list (cdr autoload-list)))))
305 ;; Data-directory is really a list now. Provide something to search it for
308 (defun locate-data-directory-list (name &optional dir-list)
309 "Locate the matching list of directories in a search path DIR-LIST.
310 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
312 (setq dir-list data-directory-list))
313 (let (found found-dir found-dir-list)
315 (setq found (file-name-as-directory (concat (car dir-list) name))
316 found-dir (file-directory-p found))
318 (setq found-dir-list (cons found found-dir-list)))
319 (setq dir-list (cdr dir-list)))
320 (nreverse found-dir-list)))
322 ;; Data-directory is really a list now. Provide something to search it for
325 (defun locate-data-directory (name &optional dir-list)
326 "Locate a directory in a search path DIR-LIST (a list of directories).
327 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
329 (setq dir-list data-directory-list))
330 (let (found found-dir)
331 (while (and (null found-dir) dir-list)
332 (setq found (file-name-as-directory (concat (car dir-list) name))
333 found-dir (file-directory-p found))
336 (setq dir-list (cdr dir-list)))
339 ;; Data-directory is really a list now. Provide something to search it for
342 (defun locate-data-file (name &optional dir-list)
343 "Locate a file in a search path DIR-LIST (a list of directories).
344 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
345 This function is basically a wrapper over `locate-file'."
346 (locate-file name (or dir-list data-directory-list)))
350 (defun packages-find-package-directories (roots base)
351 "Find a set of package directories."
352 ;; make sure paths-find-version-directory and paths-find-site-directory
353 ;; don't both pick up version-independent directories ...
354 (let ((version-directory (paths-find-version-directory roots base nil nil t))
355 (site-directory (paths-find-site-directory roots base nil nil t)))
357 (and version-directory (list version-directory))
358 (and site-directory (list site-directory)))))
360 (defvar packages-special-base-regexp "^\\(etc\\|info\\|man\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
361 "Special subdirectories of packages.")
363 (defvar packages-no-package-hierarchy-regexp
364 (concat "\\(" paths-version-control-filename-regexp "\\)"
366 "\\(" packages-special-base-regexp "\\)")
367 "Directories which can't be the roots of package hierarchies.")
369 (defun packages-find-packages-in-directories (directories)
370 "Find all packages underneath directories in DIRECTORIES."
371 (paths-find-recursive-path directories
372 packages-hierarchy-depth
373 packages-no-package-hierarchy-regexp))
375 (defun packages-split-path (path)
376 "Split PATH at \"\", return pair with two components.
377 The second component is shared with PATH."
378 (let ((reverse-tail '())
380 (while (and rest (null (string-equal "" (car rest))))
381 (setq reverse-tail (cons (car rest) reverse-tail))
382 (setq rest (cdr rest)))
385 (cons (nreverse reverse-tail) (cdr rest)))))
387 (defun packages-split-package-path (package-path)
388 "Split up PACKAGE-PATH into early, late and last components.
389 The separation is by \"\" components.
390 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
391 ;; When in doubt, it's late
392 (let* ((stuff (packages-split-path package-path))
393 (early (and (cdr stuff) (car stuff)))
394 (late+last (or (cdr stuff) (car stuff)))
395 (stuff (packages-split-path late+last))
398 (list (packages-find-packages-in-directories early)
399 (packages-find-packages-in-directories late)
400 (packages-find-packages-in-directories last))))
402 (defun packages-deconstruct (list consumer)
403 "Deconstruct LIST and feed it to CONSUMER."
404 (apply consumer list))
406 (defun packages-find-packages-by-name (roots name)
407 "Find a package hierarchy by its name."
408 (packages-find-packages-in-directories
409 (if (and (file-name-absolute-p name)
410 (file-name-directory (expand-file-name name)))
411 (list (file-name-as-directory (expand-file-name name)))
412 (packages-find-package-directories roots name))))
414 (defun packages-find-packages-at-time
415 (roots package-locations time &optional default)
416 "Find packages at given time.
417 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
418 TIME is either 'EARLY, 'LATE, or 'LAST.
419 DEFAULT is a default list of packages."
421 (let ((packages '()))
422 (while package-locations
423 (packages-deconstruct
424 (car package-locations)
425 #'(lambda (name a-time thunk)
426 (if (and (eq time a-time)
430 (packages-find-packages-by-name roots name))))))
431 (setq package-locations (cdr package-locations)))
434 (defun packages-find-packages (roots package-locations)
436 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
438 (packages-split-package-path (paths-decode-directory-path envvar-value))
439 (packages-deconstruct
440 (packages-split-package-path configure-package-path)
441 #'(lambda (configure-early-packages
442 configure-late-packages
443 configure-last-packages)
444 (list (packages-find-packages-at-time roots package-locations 'early
445 configure-early-packages)
446 (packages-find-packages-at-time roots package-locations 'late
447 configure-late-packages)
448 (packages-find-packages-at-time roots package-locations 'last
449 configure-last-packages)))))))
451 (defun packages-find-package-library-path (packages suffixes)
452 "Construct a path into a component of the packages hierarchy.
453 PACKAGES is a list of package directories.
454 SUFFIXES is a list of names of package subdirectories to look for."
458 (mapcar #'(lambda (package)
459 (mapcar #'(lambda (suffix)
460 (file-name-as-directory (concat package suffix)))
463 (paths-directories-which-exist directories)))
465 (defun packages-find-package-load-path (packages)
466 "Construct the load-path component for packages.
467 PACKAGES is a list of package directories."
468 (paths-find-recursive-load-path
469 (packages-find-package-library-path packages
471 packages-load-path-depth))
473 (defun packages-find-package-exec-path (packages)
474 "Construct the exec-path component for packages.
475 PACKAGES is a list of package directories."
476 (packages-find-package-library-path packages
477 (list (paths-construct-path
478 (list "bin" system-configuration))
481 (defun packages-find-package-info-path (packages)
482 "Construct the info-path component for packages.
483 PACKAGES is a list of package directories."
484 (packages-find-package-library-path packages '("info")))
486 (defun packages-find-package-data-path (packages)
487 "Construct the data-path component for packages.
488 PACKAGES is a list of package directories."
489 (paths-find-recursive-load-path
490 (packages-find-package-library-path packages
492 packages-data-path-depth))
494 ;; Loading package initialization files
496 (defun packages-load-package-lisps (package-load-path base)
497 "Load all Lisp files of a certain name along a load path.
498 BASE is the base name of the files."
499 (mapcar #'(lambda (dir)
500 (let ((file-name (expand-file-name base dir)))
501 (condition-case error
504 (warn (format "Autoload error in: %s:\n\t%s"
506 (with-output-to-string
507 (display-error error nil))))))))
510 (defun packages-load-package-auto-autoloads (package-load-path)
511 "Load auto-autoload files along a load path."
512 (packages-load-package-lisps package-load-path
513 (file-name-sans-extension autoload-file-name)))
515 (defun packages-handle-package-dumped-lisps (handle package-load-path)
516 "Load dumped-lisp.el files along a load path.
517 Call HANDLE on each file off definitions of PACKAGE-LISP there."
518 (mapcar #'(lambda (dir)
519 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
520 (if (file-exists-p file-name)
522 ;; 20.4 packages could set this
525 ;; dumped-lisp.el could have set this ...
527 (mapcar #'(lambda (base)
528 (funcall handle base))
532 (defun packages-load-package-dumped-lisps (package-load-path)
533 "Load dumped-lisp.el files along a load path.
534 Also load files off PACKAGE-LISP definitions there."
535 (packages-handle-package-dumped-lisps #'load package-load-path))
537 (defun packages-collect-package-dumped-lisps (package-load-path)
538 "Load dumped-lisp.el files along a load path.
539 Return list of files off PACKAGE-LISP definitions there."
541 (packages-handle-package-dumped-lisps
543 (setq *files* (cons file *files*)))
549 ;;; packages.el ends here