1 ;;; packages.el --- Low level support for XEmacs packages
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 2002, 2003 Ben Wing.
6 ;; Author: Steven L Baur <steve@xemacs.org>
7 ;; Maintainer: Steven L Baur <steve@xemacs.org>
8 ;; Keywords: internal, lisp, dumped
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: Not in FSF
31 ;; This file is dumped with XEmacs.
33 ;; This file provides low level facilities for XEmacs startup --
34 ;; particularly regarding the package setup. This code has to run in
35 ;; what we call "bare temacs" -- i.e. XEmacs without the usual Lisp
36 ;; environment. Pay special attention:
38 ;; - not to use the `lambda' macro. Use #'(lambda ...) instead.
39 ;; (this goes for any package loaded before `subr.el'.)
41 ;; - not to use macros, because they are not yet available (and this
42 ;; file must be loadable uncompiled.) Built in macros, such as
43 ;; `when' and `unless' are fine, of course.
45 ;; - not to use `defcustom'. If you must add user-customizable
46 ;; variables here, use `defvar', and add the variable to
49 ;; Because of all this, make sure that the stuff you put here really
52 ;; This file requires find-paths.el.
56 ;;; Package versioning
58 (defvar packages-package-list nil
59 "Database of installed packages and version numbers")
61 (defvar packages-hierarchy-depth 1
62 "Depth of package hierarchies.")
64 (defvar packages-load-path-depth 1
65 "Depth of load-path search in package hierarchies.")
67 (defvar packages-data-path-depth 1
68 "Depth of data-path search in package hierarchies.")
70 (defvar early-packages nil
71 "Packages early in the load path.")
73 (defvar early-package-load-path nil
74 "Load path for packages early in the load path.")
76 (defvar late-packages nil
77 "Packages late in the load path.")
79 (defvar late-package-load-path nil
80 "Load path for packages late in the load path.")
82 (defvar last-packages nil
83 "Packages last in the load path.")
85 (defvar last-package-load-path nil
86 "Load path for packages last in the load path.")
88 (defun packages-compute-package-locations (user-init-directory)
89 "Compute locations of the various package directories.
90 This is a list each of whose elements describes one directory.
91 A directory description is a three-element list.
92 The first element is either an absolute path or a subdirectory
93 in the XEmacs hierarchy.
94 The second component is one of the symbols EARLY, LATE, LAST,
95 depending on the load-path segment the hierarchy is supposed to
97 The third component is a thunk which, if it returns NIL, causes
98 the directory to be ignored."
100 (list (paths-construct-path (list user-init-directory "site-packages"))
101 'early #'(lambda () t))
102 (list (paths-construct-path (list user-init-directory "infodock-packages"))
103 'early #'(lambda () (featurep 'infodock)))
104 (list (paths-construct-path (list user-init-directory "mule-packages"))
105 'early #'(lambda () (featurep 'mule)))
106 (list (paths-construct-path (list user-init-directory "xemacs-packages"))
107 'early #'(lambda () t))
108 (list "site-packages" 'late #'(lambda () t))
109 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
110 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
111 (list "xemacs-packages" 'late #'(lambda () t))))
113 (defun package-get-key-1 (info key)
114 "Locate keyword `key' in list."
119 (t (package-get-key-1 (cddr info) key))))
121 (defun package-get-key (name key)
122 "Get info `key' from package `name'."
123 (let ((info (assq name packages-package-list)))
125 (package-get-key-1 (cdr info) key))))
127 (defun package-provide (name &rest attributes)
128 (let ((info (if (and attributes (floatp (car attributes)))
129 (list :version (car attributes))
131 (setq packages-package-list
132 (cons (cons name info) (remassq name packages-package-list)))))
134 (defun package-require (name version)
135 (let ((pkg (assq name packages-package-list)))
137 (error 'invalid-state
138 (format "Package %s has not been loaded into this XEmacsen"
140 ((< (package-get-key name :version) version)
141 (error 'search-failed
142 (format "Need version %g of package %s, got version %g"
143 version name (package-get-key name :version))))
146 (defun package-delete-name (name)
148 ;; Delete ALL versions of package.
149 ;; This is pretty memory-intensive, as we use copy-alist when deleting
150 ;; package entries, to prevent side-effects in functions that call this
152 (while (setq pkg (assq name packages-package-list))
153 (setq packages-package-list (delete pkg (copy-alist
154 packages-package-list))))))
158 (defvar autoload-file-name "auto-autoloads.el"
159 "Filename that autoloads are expected to be found in.")
161 ;; Moved from help.el.
162 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
163 ;; primitive, which should make it lightning-fast.
165 (defun locate-library (library &optional nosuffix path interactive-call)
166 "Show the precise file name of Emacs library LIBRARY.
167 This command searches the directories in `load-path' like `M-x load-library'
168 to find the file that `M-x load-library RET LIBRARY RET' would load.
169 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
170 to the specified name LIBRARY.
172 If the optional third arg PATH is specified, that list of directories
173 is used instead of `load-path'."
174 (interactive (list (read-library-name "Locate library: ")
181 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
182 (and (boundp 'find-file-hooks)
183 (member 'crypt-find-file-hook find-file-hooks)))
184 ;; Compression involved.
186 '("" ".gz" ".Z" ".bz2")
187 '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
188 ".el" ".el.gz" ".el.Z" ".el.bz2"
189 "" ".gz" ".Z" ".bz2")))
194 '(".elc" ".el" "")))))))
195 (and interactive-call
197 (message "Library is file %s" result)
198 (message "No library %s in search path" library)))
201 (defun packages-add-suffix (str)
202 (if (null (string-match "\\.el\\'" str))
206 (defun packages-list-autoloads-path ()
207 "List autoloads from precomputed load-path."
208 (let ((path load-path)
211 (if (file-exists-p (concat (car path)
213 (setq autoloads (cons (concat (car path)
216 (setq path (cdr path)))
219 (defun packages-list-autoloads (source-directory)
220 "List autoload files in (what will be) the normal lisp search path.
221 This function is used during build to find where the global symbol files so
222 they can be perused for their useful information."
223 (let ((files (directory-files (file-name-as-directory source-directory)
226 ;; (print (prin1-to-string source-directory))
227 ;; (print (prin1-to-string files))
228 (while (setq file (car-safe files))
229 (if (and (file-directory-p file)
230 (file-exists-p (concat (file-name-as-directory file)
231 autoload-file-name)))
232 (setq autolist (cons (concat (file-name-as-directory file)
235 (setq files (cdr files)))
238 ;; The following function cannot be called from a bare temacs
239 (defun packages-new-autoloads ()
240 "Return autoloads files that have been added or modified since XEmacs dump."
242 (let ((me (concat invocation-directory invocation-name))
246 (setq dir (file-truename (car path)))
247 (let ((autoload-file (file-name-sans-extension (concat
249 autoload-file-name))))
251 ;; 1. An auto-autoload file that hasn't provided a feature (because
252 ;; it has been installed since XEmacs was dumped).
253 ;; 2. auto-autoload.el being newer than the executable
254 ;; 3. auto-autoload.elc being newer than the executable (the .el
255 ;; could be missing or compressed)
256 (when (or (and (null (file-provides autoload-file))
257 (or (file-exists-p (concat autoload-file ".elc"))
258 (file-exists-p (concat autoload-file ".el"))))
259 (and (file-newer-than-file-p (concat autoload-file ".el") me)
260 (setq autoload-file (concat autoload-file ".el")))
261 (and (file-newer-than-file-p (concat autoload-file
264 (setq autoload-file (concat autoload-file ".elc"))))
265 (push autoload-file result)))
266 (setq path (cdr path)))
269 ;; The following function cannot be called from a bare temacs
270 (defun packages-reload-autoloads ()
271 "Reload new or updated auto-autoloads files.
272 This is an extremely dangerous function to call after the user-init-files
273 is run. Don't call it or you'll be sorry."
274 (let ((autoload-list (packages-new-autoloads)))
276 (let* ((autoload-file (car autoload-list))
277 (feature (car-safe (file-provides autoload-file))))
279 ;; (message "(unload-feature %S)" feature)
280 (unload-feature feature))
284 (setq autoload-list (cdr autoload-list)))))
286 ;; Data-directory is really a list now. Provide something to search it for
289 (defun locate-data-directory-list (name &optional dir-list)
290 "Locate the matching list of directories in a search path DIR-LIST.
291 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
293 (setq dir-list data-directory-list))
294 (let (found found-dir found-dir-list)
296 (setq found (file-name-as-directory (concat (car dir-list) name))
297 found-dir (file-directory-p found))
299 (setq found-dir-list (cons found found-dir-list)))
300 (setq dir-list (cdr dir-list)))
301 (nreverse found-dir-list)))
303 ;; Data-directory is really a list now. Provide something to search it for
306 (defun locate-data-directory (name &optional dir-list)
307 "Locate a directory in a search path DIR-LIST (a list of directories).
308 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
310 (setq dir-list data-directory-list))
311 (let (found found-dir)
312 (while (and (null found-dir) dir-list)
313 (setq found (file-name-as-directory (concat (car dir-list) name))
314 found-dir (file-directory-p found))
317 (setq dir-list (cdr dir-list)))
320 ;; Data-directory is really a list now. Provide something to search it for
323 (defun locate-data-file (name &optional dir-list)
324 "Locate a file in a search path DIR-LIST (a list of directories).
325 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
326 This function is basically a wrapper over `locate-file'."
327 (locate-file name (or dir-list data-directory-list)))
331 (defun packages-find-package-directories (roots base)
332 "Find a set of package directories."
333 ;; make sure paths-find-version-directory and paths-find-site-directory
334 ;; don't both pick up version-independent directories ...
335 (let ((version-directory (paths-find-version-directory roots base nil nil t))
336 (site-directory (paths-find-site-directory roots base)))
338 (and version-directory (list version-directory))
339 (and site-directory (list site-directory)))))
341 (defvar packages-special-base-regexp "^\\(etc\\|info\\|man\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
342 "Special subdirectories of packages.")
344 (defvar packages-no-package-hierarchy-regexp
345 (concat "\\(" paths-version-control-filename-regexp "\\)"
347 "\\(" packages-special-base-regexp "\\)")
348 "Directories which can't be the roots of package hierarchies.")
350 (defun packages-find-packages-in-directories (directories)
351 "Find all packages underneath directories in DIRECTORIES."
352 (paths-find-recursive-path directories
353 packages-hierarchy-depth
354 packages-no-package-hierarchy-regexp))
356 (defun packages-split-path (path)
357 "Split PATH at \"\", return pair with two components.
358 The second component is shared with PATH."
359 (let ((reverse-tail '())
361 (while (and rest (null (string-equal "" (car rest))))
362 (setq reverse-tail (cons (car rest) reverse-tail))
363 (setq rest (cdr rest)))
366 (cons (nreverse reverse-tail) (cdr rest)))))
368 (defun packages-split-package-path (package-path)
369 "Split up PACKAGE-PATH into early, late and last components.
370 The separation is by \"\" components.
371 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
372 ;; When in doubt, it's late
373 (let* ((stuff (packages-split-path package-path))
374 (early (and (cdr stuff) (car stuff)))
375 (late+last (or (cdr stuff) (car stuff)))
376 (stuff (packages-split-path late+last))
379 (list (packages-find-packages-in-directories early)
380 (packages-find-packages-in-directories late)
381 (packages-find-packages-in-directories last))))
383 (defun packages-deconstruct (list consumer)
384 "Deconstruct LIST and feed it to CONSUMER."
385 (apply consumer list))
387 (defun packages-find-packages-by-name (roots name)
388 "Find a package hierarchy by its name."
389 (packages-find-packages-in-directories
390 (if (and (file-name-absolute-p name)
391 (file-name-directory (expand-file-name name)))
392 (list (file-name-as-directory (expand-file-name name)))
393 (packages-find-package-directories roots name))))
395 (defun packages-find-packages-at-time
396 (roots package-locations time &optional default)
397 "Find packages at given time.
398 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
399 TIME is either 'EARLY, 'LATE, or 'LAST.
400 DEFAULT is a default list of packages."
402 (let ((packages '()))
403 (while package-locations
404 (packages-deconstruct
405 (car package-locations)
406 #'(lambda (name a-time thunk)
407 (if (and (eq time a-time)
411 (packages-find-packages-by-name roots name))))))
412 (setq package-locations (cdr package-locations)))
415 (defun packages-find-packages (roots package-locations)
417 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
419 (packages-split-package-path (paths-decode-directory-path envvar-value))
420 (packages-deconstruct
421 (packages-split-package-path configure-package-path)
422 #'(lambda (configure-early-packages
423 configure-late-packages
424 configure-last-packages)
425 (list (packages-find-packages-at-time roots package-locations 'early
426 configure-early-packages)
427 (packages-find-packages-at-time roots package-locations 'late
428 configure-late-packages)
429 (packages-find-packages-at-time roots package-locations 'last
430 configure-last-packages)))))))
432 (defun packages-find-package-library-path (packages suffixes)
433 "Construct a path into a component of the packages hierarchy.
434 PACKAGES is a list of package directories.
435 SUFFIXES is a list of names of package subdirectories to look for."
439 (mapcar #'(lambda (package)
440 (mapcar #'(lambda (suffix)
441 (file-name-as-directory (concat package suffix)))
444 (paths-directories-which-exist directories)))
446 (defun packages-find-package-load-path (packages)
447 "Construct the load-path component for packages.
448 PACKAGES is a list of package directories."
449 (paths-find-recursive-load-path
450 (packages-find-package-library-path packages
452 packages-load-path-depth))
454 (defun packages-find-package-exec-path (packages)
455 "Construct the exec-path component for packages.
456 PACKAGES is a list of package directories."
457 (packages-find-package-library-path packages
458 (list (paths-construct-path
459 (list "bin" system-configuration))
462 (defun packages-find-package-info-path (packages)
463 "Construct the info-path component for packages.
464 PACKAGES is a list of package directories."
465 (packages-find-package-library-path packages '("info")))
467 (defun packages-find-package-data-path (packages)
468 "Construct the data-path component for packages.
469 PACKAGES is a list of package directories."
470 (paths-find-recursive-load-path
471 (packages-find-package-library-path packages
473 packages-data-path-depth))
475 ;; Loading package initialization files
477 (defun packages-load-package-lisps (package-load-path base)
478 "Load all Lisp files of a certain name along a load path.
479 BASE is the base name of the files."
480 (mapcar #'(lambda (dir)
481 (let ((file-name (expand-file-name base dir)))
482 (condition-case error
485 (warn (format "Autoload error in: %s:\n\t%s"
487 (with-output-to-string
488 (display-error error nil))))))))
491 (defun packages-load-package-auto-autoloads (package-load-path)
492 "Load auto-autoload files along a load path."
493 (packages-load-package-lisps package-load-path
494 (file-name-sans-extension autoload-file-name)))
496 (defun packages-handle-package-dumped-lisps (handle package-load-path)
497 "Load dumped-lisp.el files along a load path.
498 Call HANDLE on each file off definitions of PACKAGE-LISP there."
499 (mapcar #'(lambda (dir)
500 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
501 (if (file-exists-p file-name)
503 ;; 20.4 packages could set this
506 ;; dumped-lisp.el could have set this ...
508 (mapcar #'(lambda (base)
509 (funcall handle base))
513 (defun packages-load-package-dumped-lisps (package-load-path)
514 "Load dumped-lisp.el files along a load path.
515 Also load files off PACKAGE-LISP definitions there."
516 (packages-handle-package-dumped-lisps #'load package-load-path))
518 (defun packages-collect-package-dumped-lisps (package-load-path)
519 "Load dumped-lisp.el files along a load path.
520 Return list of files off PACKAGE-LISP definitions there."
522 (packages-handle-package-dumped-lisps
524 (setq *files* (cons file *files*)))
530 ;;; packages.el ends here