XEmacs 21.2.42 "Poseidon".
[chise/xemacs-chise.git.1] / lisp / packages.el
1 ;;; packages.el --- Low level support for XEmacs packages
2
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4
5 ;; Author: Steven L Baur <steve@xemacs.org>
6 ;; Maintainer: Steven L Baur <steve@xemacs.org>
7 ;; Keywords: internal, lisp, dumped
8
9 ;; This file is part of XEmacs.
10
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)
14 ;; any later version.
15
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.
20
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
24 ;; 02111-1307, USA.
25
26 ;;; Synched up with: Not in FSF
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs.
31
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:
36
37 ;; - not to use the `lambda' macro.  Use #'(lambda ...) instead.
38 ;;   (this goes for any package loaded before `subr.el'.)
39 ;;
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.
43 ;;
44 ;; - not to use `defcustom'.  If you must add user-customizable
45 ;;   variables here, use `defvar', and add the variable to
46 ;;   `cus-start.el'.
47
48 ;; Because of all this, make sure that the stuff you put here really
49 ;; belongs here.
50
51 ;; This file requires find-paths.el.
52 \f
53 ;;; Code:
54
55 ;;; Package versioning
56
57 (defvar packages-package-list nil
58   "Database of installed packages and version numbers")
59
60 (defvar packages-hierarchy-depth 1
61   "Depth of package hierarchies.")
62
63 (defvar packages-load-path-depth 1
64   "Depth of load-path search in package hierarchies.")
65
66 (defvar packages-data-path-depth 1
67   "Depth of data-path search in package hierarchies.")
68
69 (defvar early-packages nil
70   "Packages early in the load path.")
71
72 (defvar early-package-load-path nil
73   "Load path for packages early in the load path.")
74
75 (defvar late-packages nil
76   "Packages late in the load path.")
77
78 (defvar late-package-load-path nil
79   "Load path for packages late in the load path.")
80
81 (defvar last-packages nil
82   "Packages last in the load path.")
83
84 (defvar last-package-load-path nil
85   "Load path for packages last in the load path.")
86
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
95 show up in.
96 The third component is a thunk which, if it returns NIL, causes
97 the directory to be ignored."
98   (list
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))))
107
108 (defun package-get-key-1 (info key)
109   "Locate keyword `key' in list."
110   (cond ((null info)
111          nil)
112         ((eq (car info) key)
113          (nth 1 info))
114         (t (package-get-key-1 (cddr info) key))))
115
116 (defun package-get-key (name key)
117   "Get info `key' from package `name'."
118   (let ((info (assq name packages-package-list)))
119     (when info
120       (package-get-key-1 (cdr info) key))))
121
122 (defun package-provide (name &rest attributes)
123   (let ((info (if (and attributes (floatp (car attributes)))
124                   (list :version (car attributes))
125                 attributes)))
126     (setq packages-package-list
127           (cons (cons name info) (remassq name packages-package-list)))))
128
129 (defun package-require (name version)
130   (let ((pkg (assq name packages-package-list)))
131     (cond ((null pkg)
132            (error "Package %s has not been loaded into this XEmacsen"
133                   name))
134           ((< (package-get-key name :version) version)
135            (error "Need version %g of package %s, got version %g"
136                   version name (cdr pkg)))
137           (t t))))
138
139 (defun package-delete-name (name)
140   (let (pkg)
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
144     ;; one.
145     (while (setq pkg (assq name packages-package-list))
146       (setq packages-package-list (delete pkg (copy-alist
147                                                packages-package-list)))
148       )
149     ))
150
151 ;;; Build time stuff
152
153 (defvar autoload-file-name "auto-autoloads.el"
154   "Filename that autoloads are expected to be found in.")
155
156 (defvar packages-hardcoded-lisp
157   '(
158     ;; Nothing at this time
159     )
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.")
163
164 (defvar packages-useful-lisp
165   '("bytecomp"
166     "byte-optimize"
167     "shadow"
168     "cl-macs")
169   "Lisp packages that need early byte compilation.")
170
171 (defvar packages-unbytecompiled-lisp
172   '("paths.el"
173     "dumped-lisp.el"
174     "dumped-pkg-lisp.el"
175     "version.el"
176     "very-early-lisp.el")
177   "Lisp packages that should not be byte compiled.")
178
179
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.
183
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.
190
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: ")
194                      nil nil
195                      t))
196   (let ((result
197          (locate-file
198           library
199           (or path load-path)
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.
204                  (if nosuffix
205                      '("" ".gz" ".Z" ".bz2")
206                    '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
207                      ".el" ".el.gz" ".el.Z" ".el.bz2"
208                      "" ".gz" ".Z" ".bz2")))
209                 (t
210                  ;; No compression.
211                  (if nosuffix
212                      ""
213                    '(".elc" ".el" "")))))))
214     (and interactive-call
215          (if result
216              (message "Library is file %s" result)
217            (message "No library %s in search path" library)))
218     result))
219
220 (defun packages-add-suffix (str)
221   (if (null (string-match "\\.el\\'" str))
222       (concat str ".elc")
223     str))
224
225 (defun packages-list-autoloads-path ()
226   "List autoloads from precomputed load-path."
227   (let ((path load-path)
228         autoloads)
229     (while path
230       (if (file-exists-p (concat (car path)
231                                  autoload-file-name))
232           (setq autoloads (cons (concat (car path)
233                                         autoload-file-name)
234                                 autoloads)))
235       (setq path (cdr path)))
236     autoloads))
237
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)
243                                 t ".*"))
244         file autolist)
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)
252                                        autoload-file-name)
253                                autolist)))
254       (setq files (cdr files)))
255     autolist))
256
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."
260   (require 'loadhist)
261   (let ((me (concat invocation-directory invocation-name))
262         (path load-path)
263         result dir)
264     (while path
265       (setq dir (file-truename (car path)))
266       (let ((autoload-file (file-name-sans-extension (concat
267                                                       dir
268                                                       autoload-file-name))))
269         ;; Check for:
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
281                                                        ".elc")
282                                                me)
283                        (setq autoload-file (concat autoload-file ".elc"))))
284           (push autoload-file result)))
285       (setq path (cdr path)))
286     result))
287
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)))
294     (while autoload-list
295       (let* ((autoload-file (car autoload-list))
296              (feature (car-safe (file-provides autoload-file))))
297         (when feature
298           ;; (message "(unload-feature %S)" feature)
299           (unload-feature feature))
300         (condition-case nil
301             (load autoload-file)
302           (t nil)))
303       (setq autoload-list (cdr autoload-list)))))
304
305 ;; Data-directory is really a list now.  Provide something to search it for
306 ;; directories.
307
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'."
311   (unless dir-list
312     (setq dir-list data-directory-list))
313   (let (found found-dir found-dir-list)
314     (while dir-list
315       (setq found (file-name-as-directory (concat (car dir-list) name))
316             found-dir (file-directory-p found))
317       (and found-dir
318            (setq found-dir-list (cons found found-dir-list)))
319       (setq dir-list (cdr dir-list)))
320     (nreverse found-dir-list)))
321
322 ;; Data-directory is really a list now.  Provide something to search it for
323 ;; a directory.
324
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'."
328   (unless dir-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))
334       (or found-dir
335           (setq found nil))
336       (setq dir-list (cdr dir-list)))
337     found))
338
339 ;; Data-directory is really a list now.  Provide something to search it for
340 ;; files.
341
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)))
347
348 ;; Path setup
349
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)))
356     (paths-uniq-append
357      (and version-directory (list version-directory))
358      (and site-directory (list site-directory)))))
359
360 (defvar packages-special-base-regexp "^\\(etc\\|info\\|man\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
361   "Special subdirectories of packages.")
362
363 (defvar packages-no-package-hierarchy-regexp
364   (concat "\\(" paths-version-control-filename-regexp "\\)"
365           "\\|"
366           "\\(" packages-special-base-regexp "\\)")
367   "Directories which can't be the roots of package hierarchies.")
368
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))
374
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 '())
379         (rest path))
380     (while (and rest (null (string-equal "" (car rest))))
381       (setq reverse-tail (cons (car rest) reverse-tail))
382       (setq rest (cdr rest)))
383     (if (null rest)
384         (cons path nil)
385       (cons (nreverse reverse-tail) (cdr rest)))))
386
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))
396          (late (car stuff))
397          (last (cdr stuff)))
398     (list (packages-find-packages-in-directories early)
399           (packages-find-packages-in-directories late)
400           (packages-find-packages-in-directories last))))
401
402 (defun packages-deconstruct (list consumer)
403   "Deconstruct LIST and feed it to CONSUMER."
404   (apply consumer list))
405
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))))
413
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."
420   (or default
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)
427                         (funcall thunk))
428                    (setq packages
429                          (nconc packages
430                                 (packages-find-packages-by-name roots name))))))
431           (setq package-locations (cdr package-locations)))
432         packages)))
433
434 (defun packages-find-packages (roots package-locations)
435   "Find the packages."
436   (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
437     (if envvar-value
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)))))))
450
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."
455   (let ((directories
456          (apply
457           #'nconc
458           (mapcar #'(lambda (package)
459                       (mapcar #'(lambda (suffix)
460                                   (file-name-as-directory (concat package suffix)))
461                               suffixes))
462                   packages))))
463     (paths-directories-which-exist directories)))
464
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
470                                        '("lisp"))
471    packages-load-path-depth))
472
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))
479                                             "lib-src")))
480
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")))
485
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
491                                        '("etc"))
492    packages-data-path-depth))
493
494 ;; Loading package initialization files
495
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
502                   (load file-name t t)
503                 (error
504                  (warn (format "Autoload error in: %s:\n\t%s"
505                                file-name
506                                (with-output-to-string
507                                  (display-error error nil))))))))
508         package-load-path))
509
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)))
514
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)
521                   (let (package-lisp
522                         ;; 20.4 packages could set this
523                         preloaded-file-list)
524                     (load file-name)
525                     ;; dumped-lisp.el could have set this ...
526                     (if package-lisp
527                         (mapcar #'(lambda (base)
528                                   (funcall handle base))
529                               package-lisp))))))
530         package-load-path))
531
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))
536
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."
540   (let ((*files* '()))
541     (packages-handle-package-dumped-lisps
542      #'(lambda (file)
543          (setq *files* (cons file *files*)))
544      package-load-path)
545     (reverse *files*)))
546
547 (provide 'packages)
548
549 ;;; packages.el ends here