(g2-UU+5B73): Add `=decomposition@hanyo-denshi'.
[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 ;; Copyright (C) 2002, 2003 Ben Wing.
5
6 ;; Author: Steven L Baur <steve@xemacs.org>
7 ;; Maintainer: Steven L Baur <steve@xemacs.org>
8 ;; Keywords: internal, lisp, dumped
9
10 ;; This file is part of XEmacs.
11
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)
15 ;; any later version.
16
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.
21
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
25 ;; 02111-1307, USA.
26
27 ;;; Synched up with: Not in FSF
28
29 ;;; Commentary:
30
31 ;; This file is dumped with XEmacs.
32
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:
37
38 ;; - not to use the `lambda' macro.  Use #'(lambda ...) instead.
39 ;;   (this goes for any package loaded before `subr.el'.)
40 ;;
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.
44 ;;
45 ;; - not to use `defcustom'.  If you must add user-customizable
46 ;;   variables here, use `defvar', and add the variable to
47 ;;   `cus-start.el'.
48
49 ;; Because of all this, make sure that the stuff you put here really
50 ;; belongs here.
51
52 ;; This file requires find-paths.el.
53 \f
54 ;;; Code:
55
56 ;;; Package versioning
57
58 (defvar packages-package-list nil
59   "Database of installed packages and version numbers")
60
61 (defvar packages-hierarchy-depth 1
62   "Depth of package hierarchies.")
63
64 (defvar packages-load-path-depth 1
65   "Depth of load-path search in package hierarchies.")
66
67 (defvar packages-data-path-depth 1
68   "Depth of data-path search in package hierarchies.")
69
70 (defvar early-packages nil
71   "Packages early in the load path.")
72
73 (defvar early-package-load-path nil
74   "Load path for packages early in the load path.")
75
76 (defvar late-packages nil
77   "Packages late in the load path.")
78
79 (defvar late-package-load-path nil
80   "Load path for packages late in the load path.")
81
82 (defvar last-packages nil
83   "Packages last in the load path.")
84
85 (defvar last-package-load-path nil
86   "Load path for packages last in the load path.")
87
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
96 show up in.
97 The third component is a thunk which, if it returns NIL, causes
98 the directory to be ignored."
99   (list
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))))
112
113 (defun package-get-key-1 (info key)
114   "Locate keyword `key' in list."
115   (cond ((null info)
116          nil)
117         ((eq (car info) key)
118          (nth 1 info))
119         (t (package-get-key-1 (cddr info) key))))
120
121 (defun package-get-key (name key)
122   "Get info `key' from package `name'."
123   (let ((info (assq name packages-package-list)))
124     (when info
125       (package-get-key-1 (cdr info) key))))
126
127 (defun package-provide (name &rest attributes)
128   (let ((info (if (and attributes (floatp (car attributes)))
129                   (list :version (car attributes))
130                 attributes)))
131     (setq packages-package-list
132           (cons (cons name info) (remassq name packages-package-list)))))
133
134 (defun package-require (name version)
135   (let ((pkg (assq name packages-package-list)))
136     (cond ((null pkg)
137            (error 'invalid-state
138                   (format "Package %s has not been loaded into this XEmacsen"
139                           name)))
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))))
144           (t t))))
145
146 (defun package-delete-name (name)
147   (let (pkg)
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
151     ;; one.
152     (while (setq pkg (assq name packages-package-list))
153       (setq packages-package-list (delete pkg (copy-alist
154                                                packages-package-list))))))
155
156 ;;; Build time stuff
157
158 (defvar autoload-file-name "auto-autoloads.el"
159   "Filename that autoloads are expected to be found in.")
160
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.
164
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.
171
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: ")
175                      nil nil
176                      t))
177   (let ((result
178          (locate-file
179           library
180           (or path load-path)
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.
185                  (if nosuffix
186                      '("" ".gz" ".Z" ".bz2")
187                    '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
188                      ".el" ".el.gz" ".el.Z" ".el.bz2"
189                      "" ".gz" ".Z" ".bz2")))
190                 (t
191                  ;; No compression.
192                  (if nosuffix
193                      ""
194                    '(".elc" ".el" "")))))))
195     (and interactive-call
196          (if result
197              (message "Library is file %s" result)
198            (message "No library %s in search path" library)))
199     result))
200
201 (defun packages-add-suffix (str)
202   (if (null (string-match "\\.el\\'" str))
203       (concat str ".elc")
204     str))
205
206 (defun packages-list-autoloads-path ()
207   "List autoloads from precomputed load-path."
208   (let ((path load-path)
209         autoloads)
210     (while path
211       (if (file-exists-p (concat (car path)
212                                  autoload-file-name))
213           (setq autoloads (cons (concat (car path)
214                                         autoload-file-name)
215                                 autoloads)))
216       (setq path (cdr path)))
217     autoloads))
218
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)
224                                 t ".*"))
225         file autolist)
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)
233                                        autoload-file-name)
234                                autolist)))
235       (setq files (cdr files)))
236     autolist))
237
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."
241   (require 'loadhist)
242   (let ((me (concat invocation-directory invocation-name))
243         (path load-path)
244         result dir)
245     (while path
246       (setq dir (file-truename (car path)))
247       (let ((autoload-file (file-name-sans-extension (concat
248                                                       dir
249                                                       autoload-file-name))))
250         ;; Check for:
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
262                                                        ".elc")
263                                                me)
264                        (setq autoload-file (concat autoload-file ".elc"))))
265           (push autoload-file result)))
266       (setq path (cdr path)))
267     result))
268
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)))
275     (while autoload-list
276       (let* ((autoload-file (car autoload-list))
277              (feature (car-safe (file-provides autoload-file))))
278         (when feature
279           ;; (message "(unload-feature %S)" feature)
280           (unload-feature feature))
281         (condition-case nil
282             (load autoload-file)
283           (t nil)))
284       (setq autoload-list (cdr autoload-list)))))
285
286 ;; Data-directory is really a list now.  Provide something to search it for
287 ;; directories.
288
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'."
292   (unless dir-list
293     (setq dir-list data-directory-list))
294   (let (found found-dir found-dir-list)
295     (while dir-list
296       (setq found (file-name-as-directory (concat (car dir-list) name))
297             found-dir (file-directory-p found))
298       (and found-dir
299            (setq found-dir-list (cons found found-dir-list)))
300       (setq dir-list (cdr dir-list)))
301     (nreverse found-dir-list)))
302
303 ;; Data-directory is really a list now.  Provide something to search it for
304 ;; a directory.
305
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'."
309   (unless dir-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))
315       (or found-dir
316           (setq found nil))
317       (setq dir-list (cdr dir-list)))
318     found))
319
320 ;; Data-directory is really a list now.  Provide something to search it for
321 ;; files.
322
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)))
328
329 ;; Path setup
330
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)))
337     (paths-uniq-append
338      (and version-directory (list version-directory))
339      (and site-directory (list site-directory)))))
340
341 (defvar packages-special-base-regexp "^\\(etc\\|info\\|man\\|lisp\\|lib-src\\|bin\\|pkginfo\\)$"
342   "Special subdirectories of packages.")
343
344 (defvar packages-no-package-hierarchy-regexp
345   (concat "\\(" paths-version-control-filename-regexp "\\)"
346           "\\|"
347           "\\(" packages-special-base-regexp "\\)")
348   "Directories which can't be the roots of package hierarchies.")
349
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))
355
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 '())
360         (rest path))
361     (while (and rest (null (string-equal "" (car rest))))
362       (setq reverse-tail (cons (car rest) reverse-tail))
363       (setq rest (cdr rest)))
364     (if (null rest)
365         (cons path nil)
366       (cons (nreverse reverse-tail) (cdr rest)))))
367
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))
377          (late (car stuff))
378          (last (cdr stuff)))
379     (list (packages-find-packages-in-directories early)
380           (packages-find-packages-in-directories late)
381           (packages-find-packages-in-directories last))))
382
383 (defun packages-deconstruct (list consumer)
384   "Deconstruct LIST and feed it to CONSUMER."
385   (apply consumer list))
386
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))))
394
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."
401   (or default
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)
408                         (funcall thunk))
409                    (setq packages
410                          (nconc packages
411                                 (packages-find-packages-by-name roots name))))))
412           (setq package-locations (cdr package-locations)))
413         packages)))
414
415 (defun packages-find-packages (roots package-locations)
416   "Find the packages."
417   (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
418     (if envvar-value
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)))))))
431
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."
436   (let ((directories
437          (apply
438           #'nconc
439           (mapcar #'(lambda (package)
440                       (mapcar #'(lambda (suffix)
441                                   (file-name-as-directory (concat package suffix)))
442                               suffixes))
443                   packages))))
444     (paths-directories-which-exist directories)))
445
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
451                                        '("lisp"))
452    packages-load-path-depth))
453
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))
460                                             "lib-src")))
461
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")))
466
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
472                                        '("etc"))
473    packages-data-path-depth))
474
475 ;; Loading package initialization files
476
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
483                   (load file-name t t)
484                 (error
485                  (warn (format "Autoload error in: %s:\n\t%s"
486                                file-name
487                                (with-output-to-string
488                                  (display-error error nil))))))))
489         package-load-path))
490
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)))
495
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)
502                   (let (package-lisp
503                         ;; 20.4 packages could set this
504                         preloaded-file-list)
505                     (load file-name)
506                     ;; dumped-lisp.el could have set this ...
507                     (if package-lisp
508                         (mapcar #'(lambda (base)
509                                   (funcall handle base))
510                               package-lisp))))))
511         package-load-path))
512
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))
517
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."
521   (let ((*files* '()))
522     (packages-handle-package-dumped-lisps
523      #'(lambda (file)
524          (setq *files* (cons file *files*)))
525      package-load-path)
526     (reverse *files*)))
527
528 (provide 'packages)
529
530 ;;; packages.el ends here