Contents in release-21-2 at 1999-06-30-19.
[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@altair.xemacs.org>
6 ;; Maintainer: Steven L Baur <steve@altair.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.)  This rules out CL-style
42 ;;   macros like `when', for instance.
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 loaded 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 (defvar package-locations
88   (list
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
104 show up in.
105 The third component is a thunk which, if it returns NIL, causes
106 the directory to be ignored.")
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     (remassq name packages-package-list)
127     (setq packages-package-list
128           (cons (cons name info) packages-package-list))))
129
130 (defun package-require (name version)
131   (let ((pkg (assq name packages-package-list)))
132     (cond ((null pkg)
133            (error "Package %s has not been loaded into this XEmacsen"
134                   name))
135           ((< (package-get-key name :version) version)
136            (error "Need version %g of package %s, got version %g"
137                   version name (cdr pkg)))
138           (t t))))
139
140 (defun package-delete-name (name)
141   (let (pkg)
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
145     ;; one.
146     (while (setq pkg (assq name packages-package-list))
147       (setq packages-package-list (delete pkg (copy-alist
148                                                packages-package-list)))
149       )
150     ))
151
152 ;;; Build time stuff
153
154 (defvar autoload-file-name "auto-autoloads.el"
155   "Filename that autoloads are expected to be found in.")
156
157 (defvar packages-hardcoded-lisp
158   '(
159     ;; Nothing at this time
160     )
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.")
164
165 (defvar packages-useful-lisp
166   '("bytecomp"
167     "byte-optimize"
168     "shadow"
169     "cl-macs")
170   "Lisp packages that need early byte compilation.")
171
172 (defvar packages-unbytecompiled-lisp
173   '("paths.el"
174     "dumped-lisp.el"
175     "dumped-pkg-lisp.el"
176     "version.el"
177     "very-early-lisp.el")
178   "Lisp packages that should not be byte compiled.")
179
180
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.
184
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.
191
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: ")
195                      nil nil
196                      t))
197   (let ((result
198          (locate-file
199           library
200           (or path load-path)
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.
205                  (if nosuffix
206                      '("" ".gz" ".Z")
207                    '(".elc" ".elc.gz" "elc.Z" ".el" ".el.gz" ".el.Z" "" ".gz" ".Z")))
208                 (t
209                  ;; No compression.
210                  (if nosuffix
211                      ""
212                    '(".elc" ".el" "")))))))
213     (and interactive-call
214          (if result
215              (message "Library is file %s" result)
216            (message "No library %s in search path" library)))
217     result))
218
219 (defun packages-add-suffix (str)
220   (if (null (string-match "\\.el\\'" str))
221       (concat str ".elc")
222     str))
223
224 (defun packages-list-autoloads-path ()
225   "List autoloads from precomputed load-path."
226   (let ((path load-path)
227         autoloads)
228     (while path
229       (if (file-exists-p (concat (car path)
230                                  autoload-file-name))
231           (setq autoloads (cons (concat (car path)
232                                         autoload-file-name)
233                                 autoloads)))
234       (setq path (cdr path)))
235     autoloads))
236
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)
242                                 t ".*"))
243         file autolist)
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)
251                                        autoload-file-name)
252                                autolist)))
253       (setq files (cdr files)))
254     autolist))
255
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."
259   (require 'loadhist)
260   (let ((me (concat invocation-directory invocation-name))
261         (path load-path)
262         result dir)
263     (while path
264       (setq dir (file-truename (car path)))
265       (let ((autoload-file (file-name-sans-extension (concat
266                                                       dir
267                                                       autoload-file-name))))
268         ;; Check for:
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
280                                                        ".elc")
281                                                me)
282                        (setq autoload-file (concat autoload-file ".elc"))))
283           (push autoload-file result)))
284       (setq path (cdr path)))
285     result))
286
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)))
293     (while autoload-list
294       (let* ((autoload-file (car autoload-list))
295              (feature (car-safe (file-provides autoload-file))))
296         (when feature
297           ;; (message "(unload-feature %S)" feature)
298           (unload-feature feature))
299         (condition-case nil
300             (load autoload-file)
301           (t nil)))
302       (setq autoload-list (cdr autoload-list)))))
303
304 ;; Data-directory is really a list now.  Provide something to search it for
305 ;; directories.
306
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'."
310   (unless dir-list
311     (setq dir-list data-directory-list))
312   (let (found found-dir found-dir-list)
313     (while dir-list
314       (setq found (file-name-as-directory (concat (car dir-list) name))
315             found-dir (file-directory-p found))
316       (and found-dir
317            (setq found-dir-list (cons found found-dir-list)))
318       (setq dir-list (cdr dir-list)))
319     (nreverse found-dir-list)))
320
321 ;; Data-directory is really a list now.  Provide something to search it for
322 ;; a directory.
323
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'."
327   (unless dir-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))
333       (or found-dir
334           (setq found nil))
335       (setq dir-list (cdr dir-list)))
336     found))
337
338 ;; Data-directory is really a list now.  Provide something to search it for
339 ;; files.
340
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)))
346
347 ;; Path setup
348
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)))
355     (paths-uniq-append
356      (and version-directory (list version-directory))
357      (and site-directory (list site-directory)))))
358
359 (defvar packages-special-base-regexp "^\\(etc\\|info\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
360   "Special subdirectories of packages.")
361
362 (defvar packages-no-package-hierarchy-regexp
363   (concat "\\(" paths-version-control-filename-regexp "\\)"
364           "\\|"
365           "\\(" packages-special-base-regexp "\\)")
366   "Directories which can't be the roots of package hierarchies.")
367
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))
373
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 '())
378         (rest path))
379     (while (and rest (null (string-equal "" (car rest))))
380       (setq reverse-tail (cons (car rest) reverse-tail))
381       (setq rest (cdr rest)))
382     (if (null rest)
383         (cons path nil)
384       (cons (nreverse reverse-tail) (cdr rest)))))
385
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))
395          (late (car stuff))
396          (last (cdr stuff)))
397     (list (packages-find-packages-in-directories early)
398           (packages-find-packages-in-directories late)
399           (packages-find-packages-in-directories last))))
400
401 (defun packages-deconstruct (list consumer)
402   "Deconstruct LIST and feed it to CONSUMER."
403   (apply consumer list))
404
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))))
412
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."
419   (or default
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)
426                         (funcall thunk))
427                    (setq packages
428                          (nconc packages
429                                 (packages-find-packages-by-name roots name))))))
430           (setq package-locations (cdr package-locations)))
431         packages)))
432
433 (defun packages-find-packages (roots)
434   "Find the packages."
435   (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
436     (if envvar-value
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)))))))
449
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."
454   (let ((directories
455          (apply
456           #'append
457           (mapcar #'(lambda (package)
458                       (mapcar #'(lambda (suffix)
459                                   (file-name-as-directory (concat package suffix)))
460                               suffixes))
461                   packages))))
462     (paths-directories-which-exist directories)))
463
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
469                                        '("lisp"))
470    packages-load-path-depth))
471
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))
478                                             "lib-src")))
479
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")))
484
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
490                                        '("etc"))
491    packages-data-path-depth))
492
493 ;; Loading package initialization files
494
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
501                   (load file-name t t)
502                 (error
503                  (warn (format "Autoload error in: %s:\n\t%s"
504                                file-name
505                                (with-output-to-string
506                                  (display-error error nil))))))))
507         package-load-path))
508
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)))
513
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)
520                   (let (package-lisp
521                         ;; 20.4 packages could set this
522                         preloaded-file-list)
523                     (load file-name)
524                     ;; dumped-lisp.el could have set this ...
525                     (if package-lisp
526                         (mapc #'(lambda (base)
527                                   (funcall handle base))
528                               package-lisp))))))
529         package-load-path))
530
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))
535
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"
539   (let ((*files* '()))
540     (packages-handle-package-dumped-lisps
541      #'(lambda (file)
542          (setq *files* (cons file *files*)))
543      package-load-path)
544     (reverse *files*)))
545
546 (provide 'packages)
547
548 ;;; packages.el ends here