1 ;;; packages.el --- Low level support for SXEmacs packages
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 2002, 2003 Ben Wing.
5 ;; Copyright (C) 2004 - 2015 Steve Youngs
7 ;; Author: Steven L Baur <steve@xemacs.org>
8 ;; Maintainer: Steve Youngs <steve@sxemacs.org>
9 ;; Keywords: internal, lisp, dumped
11 ;; This file is part of SXEmacs.
13 ;; SXEmacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; SXEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Synched up with: Not in FSF
30 ;; This file is dumped with SXEmacs.
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:
37 ;; - not to use the `lambda' macro. Use #'(lambda ...) instead.
38 ;; (this goes for any package loaded before `subr.el'.)
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.
44 ;; - not to use `defcustom'. If you must add user-customizable
45 ;; variables here, use `defvar', and add the variable to
48 ;; Because of all this, make sure that the stuff you put here really
51 ;; This file requires find-paths.el.
55 ;;; Package versioning
57 (defvar packages-package-list nil
58 "Database of installed packages and version numbers")
60 (defvar packages-hierarchy-depth 1
61 "Depth of package hierarchies.")
63 (defvar packages-load-path-depth 1
64 "Depth of load-path search in package hierarchies.")
66 (defvar packages-data-path-depth 1
67 "Depth of data-path search in package hierarchies.")
69 (defvar early-packages nil
70 "Packages early in the load path.")
72 (defvar early-package-load-path nil
73 "Load path for packages early in the load path.")
75 (defvar late-packages nil
76 "Packages late in the load path.")
78 (defvar late-package-load-path nil
79 "Load path for packages late in the load path.")
81 (defvar last-packages nil
82 "Packages last in the load path.")
84 (defvar last-package-load-path nil
85 "Load path for packages last in the load path.")
87 (defun packages-find-user-topdir ()
88 "Return the top of the user's local package hierachy.
90 This should be the equivalent of `$XDG_DATA_HOME/sxemacs'. But will
91 fall back to the old legacy directory, `~/.sxemacs', if that is the
92 value of `user-init-directory'. It may also be in the location set
93 via the `-user-pkgs-directory' command line arg."
94 (let* ((legacy (getenv "SXE_USE_LEGACY"))
95 (xdg (getenv "XDG_DATA_HOME"))
97 (paths-construct-path `(,xdg "sxemacs")))
99 `(,(user-home-directory)
100 ".local" "share" "sxemacs"))))
102 ;; Set it, if not already.
103 (when (null user-packages-topdir)
105 (string= (expand-file-name "~/.sxemacs")
106 (expand-file-name user-init-directory)))
107 (setq user-packages-topdir user-init-directory)
108 (setq user-packages-topdir (file-name-as-directory xdgdir))))
110 (unless (paths-file-readable-directory-p user-packages-topdir)
111 (make-directory-path user-packages-topdir))
112 ;; Warn if a user has pkg dirs in 2 or more of ~/.sxemacs,
113 ;; XDG_CONFIG_HOME, XDG_DATA_HOME, but not for symlinked dirs.
114 (let* ((regexp #r"\(s?xemacs\|site\|mule\)-packages")
115 (cpkgd (or (and (getenv "XDG_CONFIG_HOME")
116 (paths-construct-path
117 `(,(getenv "XDG_CONFIG_HOME") "sxemacs")))
118 (paths-construct-path
119 `(,(user-home-directory) ".config" "sxemacs")))))
121 (when (and (not (file-symlink-p "~/.sxemacs"))
122 (file-directory-p "~/.sxemacs")
123 (directory-files "~/.sxemacs" nil regexp nil 'subdir))
124 (push "~/.sxemacs" warndirs))
126 (when (and (not (file-symlink-p xdgdir))
127 (file-directory-p xdgdir)
128 (directory-files xdgdir nil regexp nil 'subdir))
129 (push xdgdir warndirs))
131 (when (and (not (file-symlink-p cpkgd))
132 (file-directory-p cpkgd)
133 (directory-files cpkgd nil regexp nil 'subdir))
134 (push cpkgd warndirs))
135 (when (> (length warndirs) 1)
136 (lwarn 'multi-pkgd nil
137 "Multiple user package hierarchies detected!!
139 The following directories contain what appears to be package subdirs:
142 Currently in use: `%s'
144 This, in itself, is not necessarily a problem, but it may mean that
145 some of your packages won't be visible to this SXEmacs instance. You
146 should keep all of your local packages in a single location.
148 See `display-warning-suppressed-classes' to suppress this warning"
149 warndirs user-packages-topdir)))
151 user-packages-topdir))
153 (defun packages-compute-package-locations (user-packages-topdir)
154 "Compute locations of the various package directories.
156 Argument USER-PACKAGES-TOPDIR is the top of the user's local package
157 hierarchy. It would normally be the equivalent of
158 `$XDG_DATA_HOME/sxemacs'.
160 This is a list, each of whose elements describes one directory. A
161 directory description is a three-element list.
163 The first element is either an absolute path or a subdirectory
164 in the XEmacs hierarchy.
166 The second element is one of the symbols EARLY, LATE, LAST,
167 depending on the load-path segment the hierarchy is supposed to
170 The third element is a thunk which, if it returns NIL, causes the
171 directory to be ignored."
173 (list (paths-construct-path (list user-packages-topdir "site-packages"))
174 'early #'(lambda () t))
175 (list (paths-construct-path (list user-packages-topdir "sxemacs-packages"))
176 'early #'(lambda () t))
177 (list (paths-construct-path (list user-packages-topdir "infodock-packages"))
178 'early #'(lambda () (featurep 'infodock)))
179 (list (paths-construct-path (list user-packages-topdir "mule-packages"))
180 'early #'(lambda () (featurep 'mule)))
181 (list (paths-construct-path (list user-packages-topdir "xemacs-packages"))
182 'early #'(lambda () t))
183 (list "site-packages" 'late #'(lambda () t))
184 (list "sxemacs-packages" 'late #'(lambda () t))
185 (list "infodock-packages" 'late #'(lambda () (featurep 'infodock)))
186 (list "mule-packages" 'late #'(lambda () (featurep 'mule)))
187 (list "xemacs-packages" 'late #'(lambda () t))))
189 (defun package-get-key-1 (info key)
190 "Locate keyword `key' in list."
195 (t (package-get-key-1 (cddr info) key))))
197 (defun package-get-key (name key)
198 "Get info `key' from package `name'."
199 (let ((info (assq name packages-package-list)))
201 (package-get-key-1 (cdr info) key))))
203 (defun package-provide (name &rest attributes)
204 (let ((info (if (and attributes (floatp (car attributes)))
205 (list :version (car attributes))
207 (setq packages-package-list
208 (cons (cons name info) (remassq name packages-package-list)))))
210 (defun package-suppress (package file form)
211 "Set up a package-suppress condition FORM for FILE in PACKAGE.
212 When SXEmacs searches for a file in the load path, it will ignore FILE
213 if FORM evaluates to non-nil."
214 (setq load-suppress-alist
215 (acons (expand-file-name file (file-dirname load-file-name)) form
216 load-suppress-alist)))
218 (defun package-require (name version)
219 (let ((pkg (assq name packages-package-list)))
221 (error 'invalid-state
222 (format "Package %s has not been loaded into this XEmacsen"
224 ((< (package-get-key name :version) version)
225 (error 'search-failed
226 (format "Need version %g of package %s, got version %g"
227 version name (package-get-key name :version))))
230 (defun package-delete-name (name)
232 ;; Delete ALL versions of package.
233 ;; This is pretty memory-intensive, as we use copy-alist when deleting
234 ;; package entries, to prevent side-effects in functions that call this
236 (while (setq pkg (assq name packages-package-list))
237 (setq packages-package-list (delete pkg (copy-alist
238 packages-package-list))))))
242 (defvar autoload-file-name "auto-autoloads.el"
243 "Filename that autoloads are expected to be found in.")
245 ;; Moved from help.el.
246 ;; Unlike the FSF version, our `locate-library' uses the `locate-file'
247 ;; primitive, which should make it lightning-fast.
249 (defun locate-library (library &optional nosuffix path interactive-call)
250 "Show the precise file name of Emacs library LIBRARY.
251 This command searches the directories in `load-path' like `M-x load-library'
252 to find the file that `M-x load-library RET LIBRARY RET' would load.
253 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
254 to the specified name LIBRARY.
256 If the optional third arg PATH is specified, that list of directories
257 is used instead of `load-path'."
258 (interactive (list (read-library-name "Locate library: ")
265 (cond ((or (rassq 'jka-compr-handler file-name-handler-alist)
266 (and (boundp 'find-file-hooks)
267 (member 'crypt-find-file-hook find-file-hooks)))
268 ;; Compression involved.
270 '("" ".gz" ".Z" ".bz2")
271 '(".elc" ".elc.gz" "elc.Z" ".elc.bz2"
272 ".el" ".el.gz" ".el.Z" ".el.bz2"
273 "" ".gz" ".Z" ".bz2")))
278 '(".elc" ".el" "")))))))
279 (and interactive-call
281 (message "Library is file %s" result)
282 (message "No library %s in search path" library)))
285 (defun packages-add-suffix (str)
286 (if (null (string-match #r"\.el\'" str))
290 (defun packages-list-autoloads-path ()
291 "List autoloads from precomputed load-path."
292 (let ((path load-path)
295 (if (file-exists-p (concat (car path)
297 (setq autoloads (cons (concat (car path)
300 (setq path (cdr path)))
303 (defun packages-list-autoloads (source-directory)
304 "List autoload files in (what will be) the normal lisp search path.
305 This function is used during build to find where the global symbol files so
306 they can be perused for their useful information."
307 (let ((files (directory-files (file-name-as-directory source-directory)
310 ;; (print (prin1-to-string source-directory))
311 ;; (print (prin1-to-string files))
312 (while (setq file (car-safe files))
313 (if (and (file-directory-p file)
314 (file-exists-p (concat (file-name-as-directory file)
315 autoload-file-name)))
316 (setq autolist (cons (concat (file-name-as-directory file)
319 (setq files (cdr files)))
322 ;; The following function cannot be called from a bare temacs
323 (defun packages-new-autoloads ()
324 "Return autoloads files that have been added or modified since XEmacs dump."
326 (let ((me (concat invocation-directory invocation-name))
330 (setq dir (file-truename (car path)))
331 (let ((autoload-file (file-name-sans-extension (concat
333 autoload-file-name))))
335 ;; 1. An auto-autoload file that hasn't provided a feature (because
336 ;; it has been installed since XEmacs was dumped).
337 ;; 2. auto-autoload.el being newer than the executable
338 ;; 3. auto-autoload.elc being newer than the executable (the .el
339 ;; could be missing or compressed)
340 (when (or (and (null (file-provides autoload-file))
341 (or (file-exists-p (concat autoload-file ".elc"))
342 (file-exists-p (concat autoload-file ".el"))))
343 (and (file-newer-than-file-p (concat autoload-file ".el") me)
344 (setq autoload-file (concat autoload-file ".el")))
345 (and (file-newer-than-file-p (concat autoload-file
348 (setq autoload-file (concat autoload-file ".elc"))))
349 (push autoload-file result)))
350 (setq path (cdr path)))
353 ;; The following function cannot be called from a bare temacs
354 (defun packages-reload-autoloads ()
355 "Reload new or updated auto-autoloads files.
356 This is an extremely dangerous function to call after the user-init-files
357 is run. Don't call it or you'll be sorry."
358 (let ((autoload-list (packages-new-autoloads)))
360 (let* ((autoload-file (car autoload-list))
361 (feature (car-safe (file-provides autoload-file))))
363 ;; (message "(unload-feature %S)" feature)
364 (unload-feature feature))
368 (setq autoload-list (cdr autoload-list)))))
370 ;; Data-directory is really a list now. Provide something to search it for
373 (defun locate-data-directory-list (name &optional dir-list)
374 "Locate the matching list of directories in a search path DIR-LIST.
375 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
377 (setq dir-list data-directory-list))
378 (let (found found-dir found-dir-list)
380 (setq found (file-name-as-directory (concat (car dir-list) name))
381 found-dir (file-directory-p found))
383 (setq found-dir-list (cons found found-dir-list)))
384 (setq dir-list (cdr dir-list)))
385 (nreverse found-dir-list)))
387 ;; Data-directory is really a list now. Provide something to search it for
390 (defun locate-data-directory (name &optional dir-list)
391 "Locate a directory in a search path DIR-LIST (a list of directories).
392 If no DIR-LIST is supplied, it defaults to `data-directory-list'."
394 (setq dir-list data-directory-list))
395 (let (found found-dir)
396 (while (and (null found-dir) dir-list)
397 (setq found (file-name-as-directory (concat (car dir-list) name))
398 found-dir (file-directory-p found))
401 (setq dir-list (cdr dir-list)))
404 ;; Data-directory is really a list now. Provide something to search it for
407 (defun locate-data-file (name &optional dir-list)
408 "Locate a file in a search path DIR-LIST (a list of directories).
409 If no DIR-LIST is supplied, it defaults to `data-directory-list'.
410 This function is basically a wrapper over `locate-file'."
411 (locate-file name (or dir-list data-directory-list)))
415 (defun packages-find-package-directories (roots base)
416 "Find a set of package directories."
417 ;; make sure paths-find-version-directory and paths-find-site-directory
418 ;; don't both pick up version-independent directories ...
419 (let ((version-directory (paths-find-version-archindep-directory
420 roots base nil nil t))
421 (site-directory (paths-find-site-archindep-directory roots base)))
423 (and version-directory (list version-directory))
424 (and site-directory (list site-directory)))))
426 (defvar packages-special-base-regexp #r"^\(etc\|info\|man\|lisp\|lib-src\|bin\|pkginfo\)$"
427 "Special subdirectories of packages.")
429 (defvar packages-no-package-hierarchy-regexp
430 (concat "\\(" paths-version-control-filename-regexp "\\)"
432 "\\(" packages-special-base-regexp "\\)")
433 "Directories which can't be the roots of package hierarchies.")
435 (defun packages-find-packages-in-directories (directories)
436 "Find all packages underneath directories in DIRECTORIES."
437 (paths-find-recursive-path directories
438 packages-hierarchy-depth
439 packages-no-package-hierarchy-regexp))
441 (defun packages-split-path (path)
442 "Split PATH at \"\", return pair with two components.
443 The second component is shared with PATH."
444 (let ((reverse-tail '())
446 (while (and rest (null (string-equal "" (car rest))))
447 (setq reverse-tail (cons (car rest) reverse-tail))
448 (setq rest (cdr rest)))
451 (cons (nreverse reverse-tail) (cdr rest)))))
453 (defun packages-split-package-path (package-path)
454 "Split up PACKAGE-PATH into early, late and last components.
455 The separation is by \"\" components.
456 This returns (LIST EARLY-PACKAGES LATE-PACKAGES LAST-PACKAGES)."
457 ;; When in doubt, it's late
458 (let* ((stuff (packages-split-path package-path))
459 (early (and (cdr stuff) (car stuff)))
460 (late+last (or (cdr stuff) (car stuff)))
461 (stuff (packages-split-path late+last))
464 (list (packages-find-packages-in-directories early)
465 (packages-find-packages-in-directories late)
466 (packages-find-packages-in-directories last))))
468 (defun packages-deconstruct (list consumer)
469 "Deconstruct LIST and feed it to CONSUMER."
470 (apply consumer list))
472 (defun packages-find-packages-by-name (roots name)
473 "Find a package hierarchy by its name."
474 (packages-find-packages-in-directories
475 (if (and (file-name-absolute-p name)
476 (file-name-directory (expand-file-name name)))
477 (list (file-name-as-directory (expand-file-name name)))
478 (packages-find-package-directories roots name))))
480 (defun packages-find-packages-at-time
481 (roots package-locations time &optional default)
482 "Find packages at given time.
483 For the format of PACKAGE-LOCATIONS, see the global variable of the same name.
484 TIME is either 'EARLY, 'LATE, or 'LAST.
485 DEFAULT is a default list of packages."
487 (let ((packages '()))
488 (while package-locations
489 (packages-deconstruct
490 (car package-locations)
491 #'(lambda (name a-time thunk)
492 (if (and (eq time a-time)
496 (packages-find-packages-by-name roots name))))))
497 (setq package-locations (cdr package-locations)))
500 (defun packages-find-packages (roots package-locations)
502 (let ((envvar-value (getenv "EMACSPACKAGEPATH")))
504 (packages-split-package-path (paths-decode-directory-path envvar-value))
505 (packages-deconstruct
506 (packages-split-package-path configure-package-path)
507 #'(lambda (configure-early-packages
508 configure-late-packages
509 configure-last-packages)
510 (list (packages-find-packages-at-time roots package-locations 'early
511 configure-early-packages)
512 (packages-find-packages-at-time roots package-locations 'late
513 configure-late-packages)
514 (packages-find-packages-at-time roots package-locations 'last
515 configure-last-packages)))))))
517 (defun packages-find-package-library-path (packages suffixes)
518 "Construct a path into a component of the packages hierarchy.
519 PACKAGES is a list of package directories.
520 SUFFIXES is a list of names of package subdirectories to look for."
524 (mapcar #'(lambda (package)
525 (mapcar #'(lambda (suffix)
526 (file-name-as-directory (concat package suffix)))
529 (paths-directories-which-exist directories)))
531 (defun packages-find-package-load-path (packages)
532 "Construct the load-path component for packages.
533 PACKAGES is a list of package directories."
534 (paths-find-recursive-load-path
535 (packages-find-package-library-path packages
537 packages-load-path-depth))
539 (defun packages-find-package-exec-path (packages)
540 "Construct the exec-path component for packages.
541 PACKAGES is a list of package directories."
542 (packages-find-package-library-path packages
543 (list (paths-construct-path
544 (list "bin" system-configuration))
547 (defun packages-find-package-info-path (packages)
548 "Construct the info-path component for packages.
549 PACKAGES is a list of package directories."
550 (packages-find-package-library-path packages '("info")))
552 (defun packages-find-package-data-path (packages)
553 "Construct the data-path component for packages.
554 PACKAGES is a list of package directories."
555 (paths-find-recursive-load-path
556 (packages-find-package-library-path packages
558 packages-data-path-depth))
560 ;; Loading package initialization files
562 (defun packages-load-package-lisps (package-load-path base)
563 "Load all Lisp files of a certain name along a load path.
564 BASE is the base name of the files."
565 (mapcar #'(lambda (dir)
566 (let ((file-name (expand-file-name base dir)))
567 (condition-case error
570 (warn (format "Autoload error in: %s:\n\t%s"
572 (with-output-to-string
573 (display-error error nil))))))))
576 (defun packages-load-package-auto-autoloads (package-load-path)
577 "Load auto-autoload files along a load path."
578 (packages-load-package-lisps package-load-path
579 (file-name-sans-extension autoload-file-name)))
581 (defun packages-handle-package-dumped-lisps (handle package-load-path)
582 "Load dumped-lisp.el files along a load path.
583 Call HANDLE on each file off definitions of PACKAGE-LISP there."
584 (mapcar #'(lambda (dir)
585 (let ((file-name (expand-file-name "dumped-lisp.el" dir)))
586 (if (file-exists-p file-name)
588 ;; 20.4 packages could set this
591 ;; dumped-lisp.el could have set this ...
593 (mapcar #'(lambda (base)
594 (funcall handle base))
598 (defun packages-load-package-dumped-lisps (package-load-path)
599 "Load dumped-lisp.el files along a load path.
600 Also load files off PACKAGE-LISP definitions there."
601 (packages-handle-package-dumped-lisps #'load package-load-path))
603 (defun packages-collect-package-dumped-lisps (package-load-path)
604 "Load dumped-lisp.el files along a load path.
605 Return list of files off PACKAGE-LISP definitions there."
607 (packages-handle-package-dumped-lisps
609 (setq *files* (cons file *files*)))
615 ;;; packages.el ends here