XEmacs 21.2-b2
[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"))
90                              'early #'(lambda () t))
91    (list "site-packages"     'late  #'(lambda () t))
92    (list "infodock-packages" 'late  #'(lambda () (featurep 'infodock)))
93    (list "mule-packages"     'late  #'(lambda () (featurep 'mule)))
94    (list "xemacs-packages"   'late  #'(lambda () t))
95    (list "packages"          'late  #'(lambda () t)))
96   "Locations of the various package directories.
97 This is a list each of whose elements describes one directory.
98 A directory description is a three-element list.
99 The first element is either an absolute path or a subdirectory
100 in the XEmacs hierarchy.
101 The second component is one of the symbols EARLY, LATE, LAST,
102 depending on the load-path segment the hierarchy is supposed to
103 show up in.
104 The third component is a thunk which, if it returns NIL, causes
105 the directory to be ignored.")
106
107 (defun package-get-key-1 (info key)
108   "Locate keyword `key' in list."
109   (cond ((null info)
110          nil)
111         ((eq (car info) key)
112          (nth 1 info))
113         (t (package-get-key-1 (cddr info) key))))
114
115 (defun package-get-key (name key)
116   "Get info `key' from package `name'."
117   (let ((info (assq name packages-package-list)))
118     (when info
119       (package-get-key-1 (cdr info) key))))
120
121 (defun package-provide (name &rest attributes)
122   (let ((info (if (and attributes (floatp (car attributes)))
123                   (list :version (car attributes))
124                 attributes)))
125     (remassq name packages-package-list)
126     (setq packages-package-list
127           (cons (cons name info) 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     "Installation.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           4)))
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   (unless dir-list
347     (setq dir-list data-directory-list))
348   (locate-file name dir-list))
349
350 ;; Path setup
351
352 (defun packages-find-package-directories (roots base)
353   "Find a set of package directories."
354   ;; make sure paths-find-version-directory and paths-find-site-directory
355   ;; don't both pick up version-independent directories ...
356   (let ((version-directory (paths-find-version-directory roots base nil nil t))
357         (site-directory (paths-find-site-directory roots base)))
358     (paths-uniq-append
359      (and version-directory (list version-directory))
360      (and site-directory (list site-directory)))))
361
362 (defvar packages-special-base-regexp "^\\(etc\\|info\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
363   "Special subdirectories of packages.")
364
365 (defvar packages-no-package-hierarchy-regexp
366   (concat "\\(" paths-version-control-filename-regexp "\\)"
367           "\\|"
368           "\\(" packages-special-base-regexp "\\)")
369   "Directories which can't be the roots of package hierarchies.")
370
371 (defun packages-find-packages-in-directories (directories)
372   "Find all packages underneath directories in DIRECTORIES."
373   (paths-find-recursive-path directories
374                              packages-hierarchy-depth
375                              packages-no-package-hierarchy-regexp))
376
377 (defun packages-split-path (path)
378   "Split PATH at \"\", return pair with two components.
379 The second component is shared with PATH."
380   (let ((reverse-tail '())
381         (rest path))
382     (while (and rest (null (string-equal "" (car rest))))
383       (setq reverse-tail (cons (car rest) reverse-tail))
384       (setq rest (cdr rest)))
385     (if (null rest)
386         (cons path nil)
387       (cons (nreverse reverse-tail) (cdr rest)))))
388
389 (defun packages-split-package-path (package-path)
390   "Split up PACKAGE-PATH into early, late and last components.
391 The separation is by \"\" components.
392 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
393   ;; When in doubt, it's late
394   (let* ((stuff (packages-split-path package-path))
395          (early (and (cdr stuff) (car stuff)))
396          (late+last (or (cdr stuff) (car stuff)))
397          (stuff (packages-split-path late+last))
398          (late (car stuff))
399          (last (cdr stuff)))
400     (list (packages-find-packages-in-directories early)
401           (packages-find-packages-in-directories late)
402           (packages-find-packages-in-directories last))))
403
404 (defun packages-deconstruct (list consumer)
405   "Deconstruct LIST and feed it to CONSUMER."
406   (apply consumer list))
407
408 (defun packages-find-packages-by-name (roots name)
409   "Find a package hierarchy by its name."
410   (packages-find-packages-in-directories
411    (if (and (file-name-absolute-p name)
412             (file-name-directory (expand-file-name name)))
413        (list (file-name-as-directory (expand-file-name name)))
414     (packages-find-package-directories roots name))))
415
416 (defun packages-find-packages-at-time
417   (roots package-locations time &optional default)
418   "Find packages at given time.
419 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
420 TIME is either 'EARLY, 'LATE, or 'LAST.
421 DEFAULT is a default list of packages."
422   (or default
423       (let ((packages '()))
424         (while package-locations
425           (packages-deconstruct 
426            (car package-locations)
427            #'(lambda (name a-time thunk)
428                (if (and (eq time a-time)
429                         (funcall thunk))
430                    (setq packages
431                          (nconc packages
432                                 (packages-find-packages-by-name roots name))))))
433           (setq package-locations (cdr package-locations)))
434         packages)))
435
436 (defun packages-find-packages (roots)
437   "Find the packages."
438   (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
439     (if envvar-value
440         (packages-split-package-path (paths-decode-directory-path envvar-value))
441       (packages-deconstruct
442        (packages-split-package-path configure-package-path)
443        #'(lambda (configure-early-packages
444                   configure-late-packages
445                   configure-last-packages)
446            (list (packages-find-packages-at-time roots package-locations 'early
447                                                  configure-early-packages)
448                  (packages-find-packages-at-time roots package-locations 'late
449                                                  configure-late-packages)
450                  (packages-find-packages-at-time roots package-locations 'last
451                                                  configure-last-packages)))))))
452
453 (defun packages-find-package-library-path (packages suffixes)
454   "Construct a path into a component of the packages hierarchy.
455 PACKAGES is a list of package directories.
456 SUFFIXES is a list of names of package subdirectories to look for."
457   (let ((directories
458          (apply
459           #'append
460           (mapcar #'(lambda (package)
461                       (mapcar #'(lambda (suffix)
462                                   (file-name-as-directory (concat package suffix)))
463                               suffixes))
464                   packages))))
465     (paths-directories-which-exist directories)))
466
467 (defun packages-find-package-load-path (packages)
468   "Construct the load-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
472                                        '("lisp"))
473    packages-load-path-depth))
474
475 (defun packages-find-package-exec-path (packages)
476   "Construct the exec-path component for packages.
477 PACKAGES is a list of package directories."
478   (packages-find-package-library-path packages
479                                       (list (paths-construct-path
480                                              (list "bin" system-configuration))
481                                             "lib-src")))
482
483 (defun packages-find-package-info-path (packages)
484   "Construct the info-path component for packages.
485 PACKAGES is a list of package directories."
486   (packages-find-package-library-path packages '("info")))
487
488 (defun packages-find-package-data-path (packages)
489   "Construct the data-path component for packages.
490 PACKAGES is a list of package directories."
491   (paths-find-recursive-load-path
492    (packages-find-package-library-path packages
493                                        '("etc"))
494    packages-data-path-depth))
495
496 ;; Loading package initialization files
497
498 (defun packages-load-package-lisps (package-load-path base)
499   "Load all Lisp files of a certain name along a load path.
500 BASE is the base name of the files."
501   (mapc #'(lambda (dir)
502             (let ((file-name (expand-file-name base dir)))
503               (condition-case error
504                   (load file-name t t)
505                 (error
506                  (warn (format "Autoload error in: %s:\n\t%s"
507                                file-name
508                                (with-output-to-string
509                                  (display-error error nil))))))))
510         package-load-path))
511
512 (defun packages-load-package-auto-autoloads (package-load-path)
513   "Load auto-autoload files along a load path."
514   (packages-load-package-lisps package-load-path
515                                (file-name-sans-extension autoload-file-name)))
516
517 (defun packages-handle-package-dumped-lisps (handle package-load-path)
518   "Load dumped-lisp.el files along a load path.
519 Call HANDLE on each file off definitions of PACKAGE-LISP there."
520   (mapc #'(lambda (dir)
521             (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
522               (if (file-exists-p file-name)
523                   (let (package-lisp
524                         ;; 20.4 packages could set this
525                         preloaded-file-list)
526                     (load file-name)
527                     ;; dumped-lisp.el could have set this ...
528                     (if package-lisp
529                         (mapc #'(lambda (base)
530                                   (funcall handle base))
531                               package-lisp))))))
532         package-load-path))
533
534 (defun packages-load-package-dumped-lisps (package-load-path)
535   "Load dumped-lisp.el files along a load path.
536 Also load files off PACKAGE-LISP definitions there"
537   (packages-handle-package-dumped-lisps #'load package-load-path))
538
539 (defun packages-collect-package-dumped-lisps (package-load-path)
540   "Load dumped-lisp.el files along a load path.
541 Return list of files off PACKAGE-LISP definitions there"
542   (let ((*files* '()))
543     (packages-handle-package-dumped-lisps
544      #'(lambda (file)
545          (setq *files* (cons file *files*)))
546      package-load-path)
547     (reverse *files*)))
548
549 (provide 'packages)
550
551 ;;; packages.el ends here