1 ;;; packages.el --- Low level support for SXEmacs 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 SXEmacs.
12 ;; SXEmacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; SXEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: Not in FSF
29 ;; This file is dumped with SXEmacs.
31 ;; This file provides low level facilities for XEmacs startup --
32 ;; particularly regarding the package setup. This code has to run in
33 ;; what we call "bare temacs" -- i.e. XEmacs without the usual Lisp
34 ;; environment. Pay special attention:
36 ;; - not to use the `lambda' macro. Use #'(lambda ...) instead.
37 ;; (this goes for any package loaded before `subr.el'.)
39 ;; - not to use macros, because they are not yet available (and this
40 ;; file must be loadable uncompiled.) Built in macros, such as
41 ;; `when' and `unless' are fine, of course.
43 ;; - not to use `defcustom'. If you must add user-customizable
44 ;; variables here, use `defvar', and add the variable to
47 ;; Because of all this, make sure that the stuff you put here really
50 ;; This file requires find-paths.el.
54 ;;; Package versioning
56 (defvar packages-package-list nil
57 "Database of installed packages and version numbers")
59 (defvar packages-hierarchy-depth 1
60 "Depth of package hierarchies.")
62 (defvar packages-load-path-depth 1
63 "Depth of load-path search in package hierarchies.")
65 (defvar packages-data-path-depth 1
66 "Depth of data-path search in package hierarchies.")
68 (defvar early-packages nil
69 "Packages early in the load path.")
71 (defvar early-package-load-path nil
72 "Load path for packages early in the load path.")
74 (defvar late-packages nil
75 "Packages late in the load path.")
77 (defvar late-package-load-path nil
78 "Load path for packages late in the load path.")
80 (defvar last-packages nil
81 "Packages last in the load path.")
83 (defvar last-package-load-path nil
84 "Load path for packages last in the load path.")
86 (defun packages-compute-package-locations (user-init-directory)
87 "Compute locations of the various package directories.
88 This is a list each of whose elements describes one directory.
89 A directory description is a three-element list.
90 The first element is either an absolute path or a subdirectory
91 in the XEmacs hierarchy.
92 The second component is one of the symbols EARLY, LATE, LAST,
93 depending on the load-path segment the hierarchy is supposed to
95 The third component is a thunk which, if it returns NIL, causes
96 the directory to be ignored."
98 (list (paths-construct-path (list user-init-directory "site-packages"))
99 'early #'(lambda () t))
100 (list (paths-construct-path (list user-init-directory "sxemacs-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 "sxemacs-packages" 'late #'(lambda () t))
110 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
111 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
112 (list "xemacs-packages" 'late #'(lambda () t))))
114 (defun package-get-key-1 (info key)
115 "Locate keyword `key' in list."
120 (t (package-get-key-1 (cddr info) key))))
122 (defun package-get-key (name key)
123 "Get info `key' from package `name'."
124 (let ((info (assq name packages-package-list)))
126 (package-get-key-1 (cdr info) key))))
128 (defun package-provide (name &rest attributes)
129 (let ((info (if (and attributes (floatp (car attributes)))
130 (list :version (car attributes))
132 (setq packages-package-list
133 (cons (cons name info) (remassq name packages-package-list)))))
135 (defun package-suppress (package file form)
136 "Set up a package-suppress condition FORM for FILE in PACKAGE.
137 When SXEmacs searches for a file in the load path, it will ignore FILE
138 if FORM evaluates to non-nil."
139 (setq load-suppress-alist
140 (acons (expand-file-name file load-file-name) form
141 load-suppress-alist)))
143 (defun package-require (name version)
144 (let ((pkg (assq name packages-package-list)))
146 (error 'invalid-state
147 (format "Package %s has not been loaded into this XEmacsen"
149 ((< (package-get-key name :version) version)
150 (error 'search-failed
151 (format "Need version %g of package %s, got version %g"
152 version name (package-get-key name :version))))
155 (defun package-delete-name (name)
157 ;; Delete ALL versions of package.
158 ;; This is pretty memory-intensive, as we use copy-alist when deleting
159 ;; package entries, to prevent side-effects in functions that call this
161 (while (setq pkg (assq name packages-package-list))
162 (setq packages-package-list (delete pkg (copy-alist
163 packages-package-list))))))
167 (defvar autoload-file-name "auto-autoloads.el"
168 "Filename that autoloads are expected to be found in.")
170 ;; Moved from help.el.
171 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
172 ;; primitive, which should make it lightning-fast.
174 (defun locate-library (library &optional nosuffix path interactive-call)
175 "Show the precise file name of Emacs library LIBRARY.
176 This command searches the directories in `load-path' like `M-x load-library'
177 to find the file that `M-x load-library RET LIBRARY RET' would load.
178 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
179 to the specified name LIBRARY.
181 If the optional third arg PATH is specified, that list of directories
182 is used instead of `load-path'."
183 (interactive (list (read-library-name "Locate library: ")
190 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
191 (and (boundp 'find-file-hooks)
192 (member 'crypt-find-file-hook find-file-hooks)))
193 ;; Compression involved.
195 '("" ".gz" ".Z" ".bz2")
196 '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
197 ".el" ".el.gz" ".el.Z" ".el.bz2"
198 "" ".gz" ".Z" ".bz2")))
203 '(".elc" ".el" "")))))))
204 (and interactive-call
206 (message "Library is file %s" result)
207 (message "No library %s in search path" library)))
210 (defun packages-add-suffix (str)
211 (if (null (string-match #r"\.el\'" str))
215 (defun packages-list-autoloads-path ()
216 "List autoloads from precomputed load-path."
217 (let ((path load-path)
220 (if (file-exists-p (concat (car path)
222 (setq autoloads (cons (concat (car path)
225 (setq path (cdr path)))
228 (defun packages-list-autoloads (source-directory)
229 "List autoload files in (what will be) the normal lisp search path.
230 This function is used during build to find where the global symbol files so
231 they can be perused for their useful information."
232 (let ((files (directory-files (file-name-as-directory source-directory)
235 ;; (print (prin1-to-string source-directory))
236 ;; (print (prin1-to-string files))
237 (while (setq file (car-safe files))
238 (if (and (file-directory-p file)
239 (file-exists-p (concat (file-name-as-directory file)
240 autoload-file-name)))
241 (setq autolist (cons (concat (file-name-as-directory file)
244 (setq files (cdr files)))
247 ;; The following function cannot be called from a bare temacs
248 (defun packages-new-autoloads ()
249 "Return autoloads files that have been added or modified since XEmacs dump."
251 (let ((me (concat invocation-directory invocation-name))
255 (setq dir (file-truename (car path)))
256 (let ((autoload-file (file-name-sans-extension (concat
258 autoload-file-name))))
260 ;; 1. An auto-autoload file that hasn't provided a feature (because
261 ;; it has been installed since XEmacs was dumped).
262 ;; 2. auto-autoload.el being newer than the executable
263 ;; 3. auto-autoload.elc being newer than the executable (the .el
264 ;; could be missing or compressed)
265 (when (or (and (null (file-provides autoload-file))
266 (or (file-exists-p (concat autoload-file ".elc"))
267 (file-exists-p (concat autoload-file ".el"))))
268 (and (file-newer-than-file-p (concat autoload-file ".el") me)
269 (setq autoload-file (concat autoload-file ".el")))
270 (and (file-newer-than-file-p (concat autoload-file
273 (setq autoload-file (concat autoload-file ".elc"))))
274 (push autoload-file result)))
275 (setq path (cdr path)))
278 ;; The following function cannot be called from a bare temacs
279 (defun packages-reload-autoloads ()
280 "Reload new or updated auto-autoloads files.
281 This is an extremely dangerous function to call after the user-init-files
282 is run. Don't call it or you'll be sorry."
283 (let ((autoload-list (packages-new-autoloads)))
285 (let* ((autoload-file (car autoload-list))
286 (feature (car-safe (file-provides autoload-file))))
288 ;; (message "(unload-feature %S)" feature)
289 (unload-feature feature))
293 (setq autoload-list (cdr autoload-list)))))
295 ;; Data-directory is really a list now. Provide something to search it for
298 (defun locate-data-directory-list (name &optional dir-list)
299 "Locate the matching list of directories in a search path DIR-LIST.
300 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
302 (setq dir-list data-directory-list))
303 (let (found found-dir found-dir-list)
305 (setq found (file-name-as-directory (concat (car dir-list) name))
306 found-dir (file-directory-p found))
308 (setq found-dir-list (cons found found-dir-list)))
309 (setq dir-list (cdr dir-list)))
310 (nreverse found-dir-list)))
312 ;; Data-directory is really a list now. Provide something to search it for
315 (defun locate-data-directory (name &optional dir-list)
316 "Locate a directory in a search path DIR-LIST (a list of directories).
317 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
319 (setq dir-list data-directory-list))
320 (let (found found-dir)
321 (while (and (null found-dir) dir-list)
322 (setq found (file-name-as-directory (concat (car dir-list) name))
323 found-dir (file-directory-p found))
326 (setq dir-list (cdr dir-list)))
329 ;; Data-directory is really a list now. Provide something to search it for
332 (defun locate-data-file (name &optional dir-list)
333 "Locate a file in a search path DIR-LIST (a list of directories).
334 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
335 This function is basically a wrapper over `locate-file'."
336 (locate-file name (or dir-list data-directory-list)))
340 (defun packages-find-package-directories (roots base)
341 "Find a set of package directories."
342 ;; make sure paths-find-version-directory and paths-find-site-directory
343 ;; don't both pick up version-independent directories ...
344 (let ((version-directory (paths-find-version-archindep-directory
345 roots base nil nil t))
346 (site-directory (paths-find-site-archindep-directory roots base)))
348 (and version-directory (list version-directory))
349 (and site-directory (list site-directory)))))
351 (defvar packages-special-base-regexp #r"^\(etc\|info\|man\|lisp\|lib-src\|bin\|pkginfo\)$"
352 "Special subdirectories of packages.")
354 (defvar packages-no-package-hierarchy-regexp
355 (concat "\\(" paths-version-control-filename-regexp "\\)"
357 "\\(" packages-special-base-regexp "\\)")
358 "Directories which can't be the roots of package hierarchies.")
360 (defun packages-find-packages-in-directories (directories)
361 "Find all packages underneath directories in DIRECTORIES."
362 (paths-find-recursive-path directories
363 packages-hierarchy-depth
364 packages-no-package-hierarchy-regexp))
366 (defun packages-split-path (path)
367 "Split PATH at \"\", return pair with two components.
368 The second component is shared with PATH."
369 (let ((reverse-tail '())
371 (while (and rest (null (string-equal "" (car rest))))
372 (setq reverse-tail (cons (car rest) reverse-tail))
373 (setq rest (cdr rest)))
376 (cons (nreverse reverse-tail) (cdr rest)))))
378 (defun packages-split-package-path (package-path)
379 "Split up PACKAGE-PATH into early, late and last components.
380 The separation is by \"\" components.
381 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
382 ;; When in doubt, it's late
383 (let* ((stuff (packages-split-path package-path))
384 (early (and (cdr stuff) (car stuff)))
385 (late+last (or (cdr stuff) (car stuff)))
386 (stuff (packages-split-path late+last))
389 (list (packages-find-packages-in-directories early)
390 (packages-find-packages-in-directories late)
391 (packages-find-packages-in-directories last))))
393 (defun packages-deconstruct (list consumer)
394 "Deconstruct LIST and feed it to CONSUMER."
395 (apply consumer list))
397 (defun packages-find-packages-by-name (roots name)
398 "Find a package hierarchy by its name."
399 (packages-find-packages-in-directories
400 (if (and (file-name-absolute-p name)
401 (file-name-directory (expand-file-name name)))
402 (list (file-name-as-directory (expand-file-name name)))
403 (packages-find-package-directories roots name))))
405 (defun packages-find-packages-at-time
406 (roots package-locations time &optional default)
407 "Find packages at given time.
408 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
409 TIME is either 'EARLY, 'LATE, or 'LAST.
410 DEFAULT is a default list of packages."
412 (let ((packages '()))
413 (while package-locations
414 (packages-deconstruct
415 (car package-locations)
416 #'(lambda (name a-time thunk)
417 (if (and (eq time a-time)
421 (packages-find-packages-by-name roots name))))))
422 (setq package-locations (cdr package-locations)))
425 (defun packages-find-packages (roots package-locations)
427 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
429 (packages-split-package-path (paths-decode-directory-path envvar-value))
430 (packages-deconstruct
431 (packages-split-package-path configure-package-path)
432 #'(lambda (configure-early-packages
433 configure-late-packages
434 configure-last-packages)
435 (list (packages-find-packages-at-time roots package-locations 'early
436 configure-early-packages)
437 (packages-find-packages-at-time roots package-locations 'late
438 configure-late-packages)
439 (packages-find-packages-at-time roots package-locations 'last
440 configure-last-packages)))))))
442 (defun packages-find-package-library-path (packages suffixes)
443 "Construct a path into a component of the packages hierarchy.
444 PACKAGES is a list of package directories.
445 SUFFIXES is a list of names of package subdirectories to look for."
449 (mapcar #'(lambda (package)
450 (mapcar #'(lambda (suffix)
451 (file-name-as-directory (concat package suffix)))
454 (paths-directories-which-exist directories)))
456 (defun packages-find-package-load-path (packages)
457 "Construct the load-path component for packages.
458 PACKAGES is a list of package directories."
459 (paths-find-recursive-load-path
460 (packages-find-package-library-path packages
462 packages-load-path-depth))
464 (defun packages-find-package-exec-path (packages)
465 "Construct the exec-path component for packages.
466 PACKAGES is a list of package directories."
467 (packages-find-package-library-path packages
468 (list (paths-construct-path
469 (list "bin" system-configuration))
472 (defun packages-find-package-info-path (packages)
473 "Construct the info-path component for packages.
474 PACKAGES is a list of package directories."
475 (packages-find-package-library-path packages '("info")))
477 (defun packages-find-package-data-path (packages)
478 "Construct the data-path component for packages.
479 PACKAGES is a list of package directories."
480 (paths-find-recursive-load-path
481 (packages-find-package-library-path packages
483 packages-data-path-depth))
485 ;; Loading package initialization files
487 (defun packages-load-package-lisps (package-load-path base)
488 "Load all Lisp files of a certain name along a load path.
489 BASE is the base name of the files."
490 (mapcar #'(lambda (dir)
491 (let ((file-name (expand-file-name base dir)))
492 (condition-case error
495 (warn (format "Autoload error in: %s:\n\t%s"
497 (with-output-to-string
498 (display-error error nil))))))))
501 (defun packages-load-package-auto-autoloads (package-load-path)
502 "Load auto-autoload files along a load path."
503 (packages-load-package-lisps package-load-path
504 (file-name-sans-extension autoload-file-name)))
506 (defun packages-handle-package-dumped-lisps (handle package-load-path)
507 "Load dumped-lisp.el files along a load path.
508 Call HANDLE on each file off definitions of PACKAGE-LISP there."
509 (mapcar #'(lambda (dir)
510 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
511 (if (file-exists-p file-name)
513 ;; 20.4 packages could set this
516 ;; dumped-lisp.el could have set this ...
518 (mapcar #'(lambda (base)
519 (funcall handle base))
523 (defun packages-load-package-dumped-lisps (package-load-path)
524 "Load dumped-lisp.el files along a load path.
525 Also load files off PACKAGE-LISP definitions there."
526 (packages-handle-package-dumped-lisps #'load package-load-path))
528 (defun packages-collect-package-dumped-lisps (package-load-path)
529 "Load dumped-lisp.el files along a load path.
530 Return list of files off PACKAGE-LISP definitions there."
532 (packages-handle-package-dumped-lisps
534 (setq *files* (cons file *files*)))
540 ;;; packages.el ends here