aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Malabarba2015-03-28 20:36:14 +0000
committerArtur Malabarba2015-03-31 00:47:31 +0100
commita7270fb20feaedc5dc6c4e0936714bdb167062f7 (patch)
treee8227db377194ecc22475724aabe25013bf56ddd
parent05a5a94000b82c81dc86cb7e2f3b4010bb2a4f0b (diff)
downloademacs-a7270fb20feaedc5dc6c4e0936714bdb167062f7.tar.gz
emacs-a7270fb20feaedc5dc6c4e0936714bdb167062f7.zip
emacs-lisp/package.el: Reorganize package.el
Reorganize package.el and divide it with page-breaks and comments
-rw-r--r--lisp/ChangeLog5
-rw-r--r--lisp/emacs-lisp/package.el1496
2 files changed, 791 insertions, 710 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 649e8849732..da5893f495b 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
12015-03-30 Artur Malabarba <bruce.connor.am@gmail.com>
2
3 * emacs-lisp/package.el: Reorganize package.el and divide it with
4 page-breaks and comments.
5
12015-03-30 Alan Mackenzie <acm@muc.de> 62015-03-30 Alan Mackenzie <acm@muc.de>
2 7
3 Correct calculation of CC Mode's font-lock region. 8 Correct calculation of CC Mode's font-lock region.
diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 0275da39229..526c0b41a77 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -173,6 +173,8 @@
173 :group 'applications 173 :group 'applications
174 :version "24.1") 174 :version "24.1")
175 175
176
177;;; Customization options
176;;;###autoload 178;;;###autoload
177(defcustom package-enable-at-startup t 179(defcustom package-enable-at-startup t
178 "Whether to activate installed packages when Emacs starts. 180 "Whether to activate installed packages when Emacs starts.
@@ -204,12 +206,6 @@ If VERSION is nil, the package is not loaded (it is \"disabled\")."
204 :group 'package 206 :group 'package
205 :version "24.1") 207 :version "24.1")
206 208
207(defvar Info-directory-list)
208(declare-function info-initialize "info" ())
209(declare-function url-http-file-exists-p "url-http" (url))
210(declare-function lm-header "lisp-mnt" (header))
211(declare-function lm-commentary "lisp-mnt" (&optional file))
212
213(defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/")) 209(defcustom package-archives '(("gnu" . "http://elpa.gnu.org/packages/"))
214 "An alist of archives from which to fetch. 210 "An alist of archives from which to fetch.
215The default value points to the GNU Emacs package repository. 211The default value points to the GNU Emacs package repository.
@@ -270,17 +266,6 @@ the package will be unavailable."
270 :group 'package 266 :group 'package
271 :version "24.4") 267 :version "24.4")
272 268
273(defconst package-archive-version 1
274 "Version number of the package archive understood by this file.
275Lower version numbers than this will probably be understood as well.")
276
277;; We don't prime the cache since it tends to get out of date.
278(defvar package-archive-contents nil
279 "Cache of the contents of the Emacs Lisp Package Archive.
280This is an alist mapping package names (symbols) to
281non-empty lists of `package-desc' structures.")
282(put 'package-archive-contents 'risky-local-variable t)
283
284(defcustom package-user-dir (locate-user-emacs-file "elpa") 269(defcustom package-user-dir (locate-user-emacs-file "elpa")
285 "Directory containing the user's Emacs Lisp packages. 270 "Directory containing the user's Emacs Lisp packages.
286The directory name should be absolute. 271The directory name should be absolute.
@@ -348,6 +333,14 @@ a sane initial value."
348 :group 'package 333 :group 'package
349 :type '(repeat symbol)) 334 :type '(repeat symbol))
350 335
336
337;;; `package-desc' object definition
338;; This is the struct used internally to represent packages.
339;; Functions that deal with packages should generally take this object
340;; as an argument. In some situations (e.g. commands that query the
341;; user) it makes sense to take the package name as a symbol instead,
342;; but keep in mind there could be multiple `package-desc's with the
343;; same name.
351(defvar package--default-summary "No description available.") 344(defvar package--default-summary "No description available.")
352 345
353(cl-defstruct (package-desc 346(cl-defstruct (package-desc
@@ -419,7 +412,43 @@ Slots:
419 extras 412 extras
420 signed) 413 signed)
421 414
415(defun package--from-builtin (bi-desc)
416 (package-desc-create :name (pop bi-desc)
417 :version (package--bi-desc-version bi-desc)
418 :summary (package--bi-desc-summary bi-desc)
419 :dir 'builtin))
420
422;; Pseudo fields. 421;; Pseudo fields.
422(defun package-version-join (vlist)
423 "Return the version string corresponding to the list VLIST.
424This is, approximately, the inverse of `version-to-list'.
425\(Actually, it returns only one of the possible inverses, since
426`version-to-list' is a many-to-one operation.)"
427 (if (null vlist)
428 ""
429 (let ((str-list (list "." (int-to-string (car vlist)))))
430 (dolist (num (cdr vlist))
431 (cond
432 ((>= num 0)
433 (push (int-to-string num) str-list)
434 (push "." str-list))
435 ((< num -4)
436 (error "Invalid version list `%s'" vlist))
437 (t
438 ;; pre, or beta, or alpha
439 (cond ((equal "." (car str-list))
440 (pop str-list))
441 ((not (string-match "[0-9]+" (car str-list)))
442 (error "Invalid version list `%s'" vlist)))
443 (push (cond ((= num -1) "pre")
444 ((= num -2) "beta")
445 ((= num -3) "alpha")
446 ((= num -4) "snapshot"))
447 str-list))))
448 (if (equal "." (car str-list))
449 (pop str-list))
450 (apply 'concat (nreverse str-list)))))
451
423(defun package-desc-full-name (pkg-desc) 452(defun package-desc-full-name (pkg-desc)
424 (format "%s-%s" 453 (format "%s-%s"
425 (package-desc-name pkg-desc) 454 (package-desc-name pkg-desc)
@@ -446,6 +475,13 @@ Slots:
446 reqs 475 reqs
447 summary) 476 summary)
448 477
478
479;;; Installed packages
480;; The following variables store information about packages present in
481;; the system. The most important of these is `package-alist'. The
482;; command `package-initialize' is also closely related to this
483;; section, but it is left for a later section because it also affects
484;; other stuff.
449(defvar package--builtins nil 485(defvar package--builtins nil
450 "Alist of built-in packages. 486 "Alist of built-in packages.
451The actual value is initialized by loading the library 487The actual value is initialized by loading the library
@@ -467,53 +503,33 @@ called via `package-initialize'. To change which packages are
467loaded and/or activated, customize `package-load-list'.") 503loaded and/or activated, customize `package-load-list'.")
468(put 'package-alist 'risky-local-variable t) 504(put 'package-alist 'risky-local-variable t)
469 505
470(defvar package--compatibility-table nil
471 "Hash table connecting package names to their compatibility.
472Each key is a symbol, the name of a package.
473
474The value is either nil, representing an incompatible package, or
475a version list, representing the highest compatible version of
476that package which is available.
477
478A package is considered incompatible if it requires an Emacs
479version higher than the one being used. To check for package
480\(in)compatibility, don't read this table directly, use
481`package--incompatible-p' which also checks dependencies.")
482
483(defvar package-activated-list nil 506(defvar package-activated-list nil
484 ;; FIXME: This should implicitly include all builtin packages. 507 ;; FIXME: This should implicitly include all builtin packages.
485 "List of the names of currently activated packages.") 508 "List of the names of currently activated packages.")
486(put 'package-activated-list 'risky-local-variable t) 509(put 'package-activated-list 'risky-local-variable t)
487 510
488(defun package-version-join (vlist) 511;;;; Populating `package-alist'.
489 "Return the version string corresponding to the list VLIST. 512;; The following functions are called on each installed package by
490This is, approximately, the inverse of `version-to-list'. 513;; `package-load-all-descriptors', which ultimately populates the
491\(Actually, it returns only one of the possible inverses, since 514;; `package-alist' variable.
492`version-to-list' is a many-to-one operation.)" 515(defun package-process-define-package (exp)
493 (if (null vlist) 516 (when (eq (car-safe exp) 'define-package)
494 "" 517 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
495 (let ((str-list (list "." (int-to-string (car vlist))))) 518 (name (package-desc-name new-pkg-desc))
496 (dolist (num (cdr vlist)) 519 (version (package-desc-version new-pkg-desc))
497 (cond 520 (old-pkgs (assq name package-alist)))
498 ((>= num 0) 521 (if (null old-pkgs)
499 (push (int-to-string num) str-list) 522 ;; If there's no old package, just add this to `package-alist'.
500 (push "." str-list)) 523 (push (list name new-pkg-desc) package-alist)
501 ((< num -4) 524 ;; If there is, insert the new package at the right place in the list.
502 (error "Invalid version list `%s'" vlist)) 525 (while
503 (t 526 (if (and (cdr old-pkgs)
504 ;; pre, or beta, or alpha 527 (version-list-< version
505 (cond ((equal "." (car str-list)) 528 (package-desc-version (cadr old-pkgs))))
506 (pop str-list)) 529 (setq old-pkgs (cdr old-pkgs))
507 ((not (string-match "[0-9]+" (car str-list))) 530 (push new-pkg-desc (cdr old-pkgs))
508 (error "Invalid version list `%s'" vlist))) 531 nil)))
509 (push (cond ((= num -1) "pre") 532 new-pkg-desc)))
510 ((= num -2) "beta")
511 ((= num -3) "alpha")
512 ((= num -4) "snapshot"))
513 str-list))))
514 (if (equal "." (car str-list))
515 (pop str-list))
516 (apply 'concat (nreverse str-list)))))
517 533
518(defun package-load-descriptor (pkg-dir) 534(defun package-load-descriptor (pkg-dir)
519 "Load the description file in directory PKG-DIR." 535 "Load the description file in directory PKG-DIR."
@@ -524,8 +540,9 @@ This is, approximately, the inverse of `version-to-list'.
524 (with-temp-buffer 540 (with-temp-buffer
525 (insert-file-contents pkg-file) 541 (insert-file-contents pkg-file)
526 (goto-char (point-min)) 542 (goto-char (point-min))
527 (let ((pkg-desc (package-process-define-package 543 (let ((pkg-desc (or (package-process-define-package
528 (read (current-buffer)) pkg-file))) 544 (read (current-buffer)))
545 (error "Can't find define-package in %s" pkg-file))))
529 (setf (package-desc-dir pkg-desc) pkg-dir) 546 (setf (package-desc-dir pkg-desc) pkg-dir)
530 (if (file-exists-p signed-file) 547 (if (file-exists-p signed-file)
531 (setf (package-desc-signed pkg-desc) t)) 548 (setf (package-desc-signed pkg-desc) t))
@@ -547,6 +564,24 @@ updates `package-alist'."
547 (when (file-directory-p pkg-dir) 564 (when (file-directory-p pkg-dir)
548 (package-load-descriptor pkg-dir))))))) 565 (package-load-descriptor pkg-dir)))))))
549 566
567(defun define-package (_name-string _version-string
568 &optional _docstring _requirements
569 &rest _extra-properties)
570 "Define a new package.
571NAME-STRING is the name of the package, as a string.
572VERSION-STRING is the version of the package, as a string.
573DOCSTRING is a short description of the package, a string.
574REQUIREMENTS is a list of dependencies on other packages.
575 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
576 where OTHER-VERSION is a string.
577
578EXTRA-PROPERTIES is currently unused."
579 ;; FIXME: Placeholder! Should we keep it?
580 (error "Don't call me!"))
581
582
583;;; Package activation
584;; Section for functions used by `package-activate', which see.
550(defun package-disabled-p (pkg-name version) 585(defun package-disabled-p (pkg-name version)
551 "Return whether PKG-NAME at VERSION can be activated. 586 "Return whether PKG-NAME at VERSION can be activated.
552The decision is made according to `package-load-list'. 587The decision is made according to `package-load-list'.
@@ -562,6 +597,23 @@ Return the max version (as a string) if the package is held at a lower version."
562 force)) 597 force))
563 (t (error "Invalid element in `package-load-list'"))))) 598 (t (error "Invalid element in `package-load-list'")))))
564 599
600(defun package-built-in-p (package &optional min-version)
601 "Return true if PACKAGE is built-in to Emacs.
602Optional arg MIN-VERSION, if non-nil, should be a version list
603specifying the minimum acceptable version."
604 (if (package-desc-p package) ;; was built-in and then was converted
605 (eq 'builtin (package-desc-dir package))
606 (let ((bi (assq package package--builtin-versions)))
607 (cond
608 (bi (version-list-<= min-version (cdr bi)))
609 ((remove 0 min-version) nil)
610 (t
611 (require 'finder-inf nil t) ; For `package--builtins'.
612 (assq package package--builtins))))))
613
614(defvar Info-directory-list)
615(declare-function info-initialize "info" ())
616
565(defun package-activate-1 (pkg-desc &optional reload) 617(defun package-activate-1 (pkg-desc &optional reload)
566 "Activate package given by PKG-DESC, even if it was already active. 618 "Activate package given by PKG-DESC, even if it was already active.
567If RELOAD is non-nil, also `load' any files inside the package which 619If RELOAD is non-nil, also `load' any files inside the package which
@@ -606,6 +658,7 @@ correspond to previously loaded files (those returned by
606 t)) 658 t))
607 659
608(declare-function find-library-name "find-func" (library)) 660(declare-function find-library-name "find-func" (library))
661
609(defun package--list-loaded-files (dir) 662(defun package--list-loaded-files (dir)
610 "Recursively list all files in DIR which correspond to loaded features. 663 "Recursively list all files in DIR which correspond to loaded features.
611Returns the `file-name-sans-extension' of each file, relative to 664Returns the `file-name-sans-extension' of each file, relative to
@@ -640,33 +693,14 @@ DIR, sorted by most recently loaded last."
640 ;; Sort the files by ascending HISTORY-POSITION. 693 ;; Sort the files by ascending HISTORY-POSITION.
641 (lambda (x y) (< (cdr x) (cdr y)))))))) 694 (lambda (x y) (< (cdr x) (cdr y))))))))
642 695
643(defun package-built-in-p (package &optional min-version) 696;;;; `package-activate'
644 "Return true if PACKAGE is built-in to Emacs. 697;; This function activates a newer version of a package if an older
645Optional arg MIN-VERSION, if non-nil, should be a version list 698;; one was already activated. It also loads a features of this
646specifying the minimum acceptable version." 699;; package which were already loaded.
647 (if (package-desc-p package) ;; was built-in and then was converted
648 (eq 'builtin (package-desc-dir package))
649 (let ((bi (assq package package--builtin-versions)))
650 (cond
651 (bi (version-list-<= min-version (cdr bi)))
652 ((remove 0 min-version) nil)
653 (t
654 (require 'finder-inf nil t) ; For `package--builtins'.
655 (assq package package--builtins))))))
656
657(defun package--from-builtin (bi-desc)
658 (package-desc-create :name (pop bi-desc)
659 :version (package--bi-desc-version bi-desc)
660 :summary (package--bi-desc-summary bi-desc)
661 :dir 'builtin))
662
663;; This function goes ahead and activates a newer version of a package
664;; if an older one was already activated. This is not ideal; we'd at
665;; least need to check to see if the package has actually been loaded,
666;; and not merely activated.
667(defun package-activate (package &optional force) 700(defun package-activate (package &optional force)
668 "Activate package PACKAGE. 701 "Activate package PACKAGE.
669If FORCE is true, (re-)activate it if it's already activated." 702If FORCE is true, (re-)activate it if it's already activated.
703Newer versions are always activated, regardless of FORCE."
670 (let ((pkg-descs (cdr (assq package package-alist)))) 704 (let ((pkg-descs (cdr (assq package package-alist))))
671 ;; Check if PACKAGE is available in `package-alist'. 705 ;; Check if PACKAGE is available in `package-alist'.
672 (while 706 (while
@@ -698,76 +732,14 @@ Required package `%s-%s' is unavailable"
698 ;; If all goes well, activate the package itself. 732 ;; If all goes well, activate the package itself.
699 (package-activate-1 pkg-vec force))))))) 733 (package-activate-1 pkg-vec force)))))))
700 734
701(defun define-package (_name-string _version-string 735
702 &optional _docstring _requirements 736;;; Installation -- Local operations
703 &rest _extra-properties) 737;; This section contains a variety of features regarding installing a
704 "Define a new package. 738;; package to/from disk. This includes autoload generation,
705NAME-STRING is the name of the package, as a string. 739;; unpacking, compiling, as well as defining a package from the
706VERSION-STRING is the version of the package, as a string. 740;; current buffer.
707DOCSTRING is a short description of the package, a string.
708REQUIREMENTS is a list of dependencies on other packages.
709 Each requirement is of the form (OTHER-PACKAGE OTHER-VERSION),
710 where OTHER-VERSION is a string.
711
712EXTRA-PROPERTIES is currently unused."
713 ;; FIXME: Placeholder! Should we keep it?
714 (error "Don't call me!"))
715
716(defun package-process-define-package (exp origin)
717 (unless (eq (car-safe exp) 'define-package)
718 (error "Can't find define-package in %s" origin))
719 (let* ((new-pkg-desc (apply #'package-desc-from-define (cdr exp)))
720 (name (package-desc-name new-pkg-desc))
721 (version (package-desc-version new-pkg-desc))
722 (old-pkgs (assq name package-alist)))
723 (if (null old-pkgs)
724 ;; If there's no old package, just add this to `package-alist'.
725 (push (list name new-pkg-desc) package-alist)
726 ;; If there is, insert the new package at the right place in the list.
727 (while
728 (if (and (cdr old-pkgs)
729 (version-list-< version
730 (package-desc-version (cadr old-pkgs))))
731 (setq old-pkgs (cdr old-pkgs))
732 (push new-pkg-desc (cdr old-pkgs))
733 nil)))
734 new-pkg-desc))
735
736;; From Emacs 22, but changed so it adds to load-path.
737(defun package-autoload-ensure-default-file (file)
738 "Make sure that the autoload file FILE exists and if not create it."
739 (unless (file-exists-p file)
740 (write-region
741 (concat ";;; " (file-name-nondirectory file)
742 " --- automatically extracted autoloads\n"
743 ";;\n"
744 ";;; Code:\n"
745 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
746 " \n;; Local Variables:\n"
747 ";; version-control: never\n"
748 ";; no-byte-compile: t\n"
749 ";; no-update-autoloads: t\n"
750 ";; End:\n"
751 ";;; " (file-name-nondirectory file)
752 " ends here\n")
753 nil file nil 'silent))
754 file)
755
756(defvar generated-autoload-file)
757(defvar version-control)
758
759(defun package-generate-autoloads (name pkg-dir)
760 (let* ((auto-name (format "%s-autoloads.el" name))
761 ;;(ignore-name (concat name "-pkg.el"))
762 (generated-autoload-file (expand-file-name auto-name pkg-dir))
763 (backup-inhibited t)
764 (version-control 'never))
765 (package-autoload-ensure-default-file generated-autoload-file)
766 (update-directory-autoloads pkg-dir)
767 (let ((buf (find-buffer-visiting generated-autoload-file)))
768 (when buf (kill-buffer buf)))
769 auto-name))
770 741
742;;;; Unpacking
771(defvar tar-parse-info) 743(defvar tar-parse-info)
772(declare-function tar-untar-buffer "tar-mode" ()) 744(declare-function tar-untar-buffer "tar-mode" ())
773(declare-function tar-header-name "tar-mode" (tar-header) t) 745(declare-function tar-header-name "tar-mode" (tar-header) t)
@@ -792,34 +764,6 @@ untar into a directory named DIR; otherwise, signal an error."
792 (error "Package does not untar cleanly into directory %s/" dir))))) 764 (error "Package does not untar cleanly into directory %s/" dir)))))
793 (tar-untar-buffer)) 765 (tar-untar-buffer))
794 766
795(defun package-generate-description-file (pkg-desc pkg-file)
796 "Create the foo-pkg.el file for single-file packages."
797 (let* ((name (package-desc-name pkg-desc)))
798 (let ((print-level nil)
799 (print-quoted t)
800 (print-length nil))
801 (write-region
802 (concat
803 ";;; -*- no-byte-compile: t -*-\n"
804 (prin1-to-string
805 (nconc
806 (list 'define-package
807 (symbol-name name)
808 (package-version-join (package-desc-version pkg-desc))
809 (package-desc-summary pkg-desc)
810 (let ((requires (package-desc-reqs pkg-desc)))
811 (list 'quote
812 ;; Turn version lists into string form.
813 (mapcar
814 (lambda (elt)
815 (list (car elt)
816 (package-version-join (cadr elt))))
817 requires))))
818 (package--alist-to-plist-args
819 (package-desc-extras pkg-desc))))
820 "\n")
821 nil pkg-file nil 'silent))))
822
823(defun package--alist-to-plist-args (alist) 767(defun package--alist-to-plist-args (alist)
824 (mapcar 'macroexp-quote 768 (mapcar 'macroexp-quote
825 (apply #'nconc 769 (apply #'nconc
@@ -866,6 +810,70 @@ untar into a directory named DIR; otherwise, signal an error."
866 (package-activate name 'force) 810 (package-activate name 'force)
867 pkg-dir)) 811 pkg-dir))
868 812
813(defun package-generate-description-file (pkg-desc pkg-file)
814 "Create the foo-pkg.el file for single-file packages."
815 (let* ((name (package-desc-name pkg-desc)))
816 (let ((print-level nil)
817 (print-quoted t)
818 (print-length nil))
819 (write-region
820 (concat
821 ";;; -*- no-byte-compile: t -*-\n"
822 (prin1-to-string
823 (nconc
824 (list 'define-package
825 (symbol-name name)
826 (package-version-join (package-desc-version pkg-desc))
827 (package-desc-summary pkg-desc)
828 (let ((requires (package-desc-reqs pkg-desc)))
829 (list 'quote
830 ;; Turn version lists into string form.
831 (mapcar
832 (lambda (elt)
833 (list (car elt)
834 (package-version-join (cadr elt))))
835 requires))))
836 (package--alist-to-plist-args
837 (package-desc-extras pkg-desc))))
838 "\n")
839 nil pkg-file nil 'silent))))
840
841;;;; Autoload
842;; From Emacs 22, but changed so it adds to load-path.
843(defun package-autoload-ensure-default-file (file)
844 "Make sure that the autoload file FILE exists and if not create it."
845 (unless (file-exists-p file)
846 (write-region
847 (concat ";;; " (file-name-nondirectory file)
848 " --- automatically extracted autoloads\n"
849 ";;\n"
850 ";;; Code:\n"
851 "(add-to-list 'load-path (or (file-name-directory #$) (car load-path)))\n"
852 " \n;; Local Variables:\n"
853 ";; version-control: never\n"
854 ";; no-byte-compile: t\n"
855 ";; no-update-autoloads: t\n"
856 ";; End:\n"
857 ";;; " (file-name-nondirectory file)
858 " ends here\n")
859 nil file nil 'silent))
860 file)
861
862(defvar generated-autoload-file)
863(defvar version-control)
864
865(defun package-generate-autoloads (name pkg-dir)
866 (let* ((auto-name (format "%s-autoloads.el" name))
867 ;;(ignore-name (concat name "-pkg.el"))
868 (generated-autoload-file (expand-file-name auto-name pkg-dir))
869 (backup-inhibited t)
870 (version-control 'never))
871 (package-autoload-ensure-default-file generated-autoload-file)
872 (update-directory-autoloads pkg-dir)
873 (let ((buf (find-buffer-visiting generated-autoload-file)))
874 (when buf (kill-buffer buf)))
875 auto-name))
876
869(defun package--make-autoloads-and-stuff (pkg-desc pkg-dir) 877(defun package--make-autoloads-and-stuff (pkg-desc pkg-dir)
870 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR." 878 "Generate autoloads, description file, etc.. for PKG-DESC installed at PKG-DIR."
871 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir) 879 (package-generate-autoloads (package-desc-name pkg-desc) pkg-dir)
@@ -876,33 +884,153 @@ untar into a directory named DIR; otherwise, signal an error."
876 ;; FIXME: Create foo.info and dir file from foo.texi? 884 ;; FIXME: Create foo.info and dir file from foo.texi?
877 ) 885 )
878 886
887;;;; Compilation
879(defun package--compile (pkg-desc) 888(defun package--compile (pkg-desc)
880 "Byte-compile installed package PKG-DESC." 889 "Byte-compile installed package PKG-DESC."
881 (package-activate-1 pkg-desc) 890 (package-activate-1 pkg-desc)
882 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t)) 891 (byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
883 892
893;;;; Inferring package from current buffer
894(defun package-read-from-string (str)
895 "Read a Lisp expression from STR.
896Signal an error if the entire string was not used."
897 (let* ((read-data (read-from-string str))
898 (more-left
899 (condition-case nil
900 ;; The call to `ignore' suppresses a compiler warning.
901 (progn (ignore (read-from-string
902 (substring str (cdr read-data))))
903 t)
904 (end-of-file nil))))
905 (if more-left
906 (error "Can't read whole string")
907 (car read-data))))
908
909(defun package--prepare-dependencies (deps)
910 "Turn DEPS into an acceptable list of dependencies.
911
912Any parts missing a version string get a default version string
913of \"0\" (meaning any version) and an appropriate level of lists
914is wrapped around any parts requiring it."
915 (cond
916 ((not (listp deps))
917 (error "Invalid requirement specifier: %S" deps))
918 (t (mapcar (lambda (dep)
919 (cond
920 ((symbolp dep) `(,dep "0"))
921 ((stringp dep)
922 (error "Invalid requirement specifier: %S" dep))
923 ((and (listp dep) (null (cdr dep)))
924 (list (car dep) "0"))
925 (t dep)))
926 deps))))
927
928(declare-function lm-header "lisp-mnt" (header))
929(declare-function lm-homepage "lisp-mnt" ())
930
931(defun package-buffer-info ()
932 "Return a `package-desc' describing the package in the current buffer.
933
934If the buffer does not contain a conforming package, signal an
935error. If there is a package, narrow the buffer to the file's
936boundaries."
937 (goto-char (point-min))
938 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
939 (error "Package lacks a file header"))
940 (let ((file-name (match-string-no-properties 1))
941 (desc (match-string-no-properties 2))
942 (start (line-beginning-position)))
943 (unless (search-forward (concat ";;; " file-name ".el ends here"))
944 (error "Package lacks a terminating comment"))
945 ;; Try to include a trailing newline.
946 (forward-line)
947 (narrow-to-region start (point))
948 (require 'lisp-mnt)
949 ;; Use some headers we've invented to drive the process.
950 (let* ((requires-str (lm-header "package-requires"))
951 ;; Prefer Package-Version; if defined, the package author
952 ;; probably wants us to use it. Otherwise try Version.
953 (pkg-version
954 (or (package-strip-rcs-id (lm-header "package-version"))
955 (package-strip-rcs-id (lm-header "version"))))
956 (homepage (lm-homepage)))
957 (unless pkg-version
958 (error
959 "Package lacks a \"Version\" or \"Package-Version\" header"))
960 (package-desc-from-define
961 file-name pkg-version desc
962 (if requires-str
963 (package--prepare-dependencies
964 (package-read-from-string requires-str)))
965 :kind 'single
966 :url homepage))))
967
968(defun package--read-pkg-desc (kind)
969 "Read a `define-package' form in current buffer.
970Return the pkg-desc, with desc-kind set to KIND."
971 (goto-char (point-min))
972 (unwind-protect
973 (let* ((pkg-def-parsed (read (current-buffer)))
974 (pkg-desc
975 (when (eq (car pkg-def-parsed) 'define-package)
976 (apply #'package-desc-from-define
977 (append (cdr pkg-def-parsed))))))
978 (when pkg-desc
979 (setf (package-desc-kind pkg-desc) kind)
980 pkg-desc))))
981
982(declare-function tar-get-file-descriptor "tar-mode" (file))
983(declare-function tar--extract "tar-mode" (descriptor))
984
985(defun package-tar-file-info ()
986 "Find package information for a tar file.
987The return result is a `package-desc'."
988 (cl-assert (derived-mode-p 'tar-mode))
989 (let* ((dir-name (file-name-directory
990 (tar-header-name (car tar-parse-info))))
991 (desc-file (package--description-file dir-name))
992 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
993 (unless tar-desc
994 (error "No package descriptor file found"))
995 (with-current-buffer (tar--extract tar-desc)
996 (unwind-protect
997 (or (package--read-pkg-desc 'tar)
998 (error "Can't find define-package in %s"
999 (tar-header-name tar-desc)))
1000 (kill-buffer (current-buffer))))))
1001
1002(defun package-dir-info ()
1003 "Find package information for a directory.
1004The return result is a `package-desc'."
1005 (cl-assert (derived-mode-p 'dired-mode))
1006 (let* ((desc-file (package--description-file default-directory)))
1007 (if (file-readable-p desc-file)
1008 (with-temp-buffer
1009 (insert-file-contents desc-file)
1010 (package--read-pkg-desc 'dir))
1011 (let ((files (directory-files default-directory t "\\.el\\'" t))
1012 info)
1013 (while files
1014 (with-temp-buffer
1015 (insert-file-contents (pop files))
1016 ;; When we find the file with the data,
1017 (when (setq info (ignore-errors (package-buffer-info)))
1018 ;; stop looping,
1019 (setq files nil)
1020 ;; set the 'dir kind,
1021 (setf (package-desc-kind info) 'dir))))
1022 ;; and return the info.
1023 info))))
1024
1025
1026;;; Communicating with Archives
1027;; Set of low-level functions for communicating with archives and
1028;; signature checking.
884(defun package--write-file-no-coding (file-name) 1029(defun package--write-file-no-coding (file-name)
885 (let ((buffer-file-coding-system 'no-conversion)) 1030 (let ((buffer-file-coding-system 'no-conversion))
886 (write-region (point-min) (point-max) file-name nil 'silent))) 1031 (write-region (point-min) (point-max) file-name nil 'silent)))
887 1032
888(defmacro package--with-work-buffer (location file &rest body) 1033(declare-function url-http-file-exists-p "url-http" (url))
889 "Run BODY in a buffer containing the contents of FILE at LOCATION.
890LOCATION is the base location of a package archive, and should be
891one of the URLs (or file names) specified in `package-archives'.
892FILE is the name of a file relative to that base location.
893
894This macro retrieves FILE from LOCATION into a temporary buffer,
895and evaluates BODY while that buffer is current. This work
896buffer is killed afterwards. Return the last value in BODY."
897 (declare (indent 2) (debug t))
898 `(with-temp-buffer
899 (if (string-match-p "\\`https?:" ,location)
900 (url-insert-file-contents (concat ,location ,file))
901 (unless (file-name-absolute-p ,location)
902 (error "Archive location %s is not an absolute file name"
903 ,location))
904 (insert-file-contents (expand-file-name ,file ,location)))
905 ,@body))
906 1034
907(defun package--archive-file-exists-p (location file) 1035(defun package--archive-file-exists-p (location file)
908 (let ((http (string-match "\\`https?:" location))) 1036 (let ((http (string-match "\\`https?:" location)))
@@ -935,6 +1063,25 @@ buffer is killed afterwards. Return the last value in BODY."
935 (insert (format "Error while verifying signature %s:\n" sig-file))) 1063 (insert (format "Error while verifying signature %s:\n" sig-file)))
936 (insert "\nCommand output:\n" (epg-context-error-output context)))))) 1064 (insert "\nCommand output:\n" (epg-context-error-output context))))))
937 1065
1066(defmacro package--with-work-buffer (location file &rest body)
1067 "Run BODY in a buffer containing the contents of FILE at LOCATION.
1068LOCATION is the base location of a package archive, and should be
1069one of the URLs (or file names) specified in `package-archives'.
1070FILE is the name of a file relative to that base location.
1071
1072This macro retrieves FILE from LOCATION into a temporary buffer,
1073and evaluates BODY while that buffer is current. This work
1074buffer is killed afterwards. Return the last value in BODY."
1075 (declare (indent 2) (debug t))
1076 `(with-temp-buffer
1077 (if (string-match-p "\\`https?:" ,location)
1078 (url-insert-file-contents (concat ,location ,file))
1079 (unless (file-name-absolute-p ,location)
1080 (error "Archive location %s is not an absolute file name"
1081 ,location))
1082 (insert-file-contents (expand-file-name ,file ,location)))
1083 ,@body))
1084
938(defun package--check-signature (location file) 1085(defun package--check-signature (location file)
939 "Check signature of the current buffer. 1086 "Check signature of the current buffer.
940GnuPG keyring is located under \"gnupg\" in `package-user-dir'." 1087GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
@@ -967,65 +1114,256 @@ GnuPG keyring is located under \"gnupg\" in `package-user-dir'."
967 (error "Failed to verify signature %s" sig-file)) 1114 (error "Failed to verify signature %s" sig-file))
968 good-signatures))) 1115 good-signatures)))
969 1116
970(defun package-install-from-archive (pkg-desc) 1117
971 "Download and install a tar package." 1118;;; Packages on Archives
972 ;; This won't happen, unless the archive is doing something wrong. 1119;; The following variables store information about packages available
973 (when (eq (package-desc-kind pkg-desc) 'dir) 1120;; from archives. The most important of these is
974 (error "Can't install directory package from archive")) 1121;; `package-archive-contents' which is initially populated by the
975 (let* ((location (package-archive-base pkg-desc)) 1122;; function `package-read-all-archive-contents' from a cache on disk.
976 (file (concat (package-desc-full-name pkg-desc) 1123;; The `package-initialize' command is also closely related to this
977 (package-desc-suffix pkg-desc))) 1124;; section, but it has its own section.
978 (sig-file (concat file ".sig")) 1125(defconst package-archive-version 1
979 good-signatures pkg-descs) 1126 "Version number of the package archive understood by this file.
980 (package--with-work-buffer location file 1127Lower version numbers than this will probably be understood as well.")
1128
1129;; We don't prime the cache since it tends to get out of date.
1130(defvar package-archive-contents nil
1131 "Cache of the contents of the Emacs Lisp Package Archive.
1132This is an alist mapping package names (symbols) to
1133non-empty lists of `package-desc' structures.")
1134(put 'package-archive-contents 'risky-local-variable t)
1135
1136(defvar package--compatibility-table nil
1137 "Hash table connecting package names to their compatibility.
1138Each key is a symbol, the name of a package.
1139
1140The value is either nil, representing an incompatible package, or
1141a version list, representing the highest compatible version of
1142that package which is available.
1143
1144A package is considered incompatible if it requires an Emacs
1145version higher than the one being used. To check for package
1146\(in)compatibility, don't read this table directly, use
1147`package--incompatible-p' which also checks dependencies.")
1148
1149(defun package--build-compatibility-table ()
1150 "Build `package--compatibility-table' with `package--mapc'."
1151 ;; Build compat table.
1152 (setq package--compatibility-table (make-hash-table :test 'eq))
1153 (package--mapc #'package--add-to-compatibility-table))
1154
1155(defun package--add-to-compatibility-table (pkg)
1156 "If PKG is compatible (without dependencies), add to the compatibility table.
1157PKG is a package-desc object.
1158Only adds if its version is higher than what's already stored in
1159the table."
1160 (unless (package--incompatible-p pkg 'shallow)
1161 (let* ((name (package-desc-name pkg))
1162 (version (or (package-desc-version pkg) '(0)))
1163 (table-version (gethash name package--compatibility-table)))
1164 (when (or (not table-version)
1165 (version-list-< table-version version))
1166 (puthash name version package--compatibility-table)))))
1167
1168;; Package descriptor objects used inside the "archive-contents" file.
1169;; Changing this defstruct implies changing the format of the
1170;; "archive-contents" files.
1171(cl-defstruct (package--ac-desc
1172 (:constructor package-make-ac-desc (version reqs summary kind extras))
1173 (:copier nil)
1174 (:type vector))
1175 version reqs summary kind extras)
1176
1177(defun package--append-to-alist (pkg-desc alist)
1178 "Append an entry for PKG-DESC to the start of ALIST and return it.
1179This entry takes the form (`package-desc-name' PKG-DESC).
1180
1181If ALIST already has an entry with this name, destructively add
1182PKG-DESC to the cdr of this entry instead, sorted by version
1183number."
1184 (let* ((name (package-desc-name pkg-desc))
1185 (priority-version (package-desc-priority-version pkg-desc))
1186 (existing-packages (assq name alist)))
1187 (if (not existing-packages)
1188 (cons (list name pkg-desc)
1189 alist)
1190 (while (if (and (cdr existing-packages)
1191 (version-list-< priority-version
1192 (package-desc-priority-version
1193 (cadr existing-packages))))
1194 (setq existing-packages (cdr existing-packages))
1195 (push pkg-desc (cdr existing-packages))
1196 nil))
1197 alist)))
1198
1199(defun package--add-to-archive-contents (package archive)
1200 "Add the PACKAGE from the given ARCHIVE if necessary.
1201PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1202Also, add the originating archive to the `package-desc' structure."
1203 (let* ((name (car package))
1204 (version (package--ac-desc-version (cdr package)))
1205 (pkg-desc
1206 (package-desc-create
1207 :name name
1208 :version version
1209 :reqs (package--ac-desc-reqs (cdr package))
1210 :summary (package--ac-desc-summary (cdr package))
1211 :kind (package--ac-desc-kind (cdr package))
1212 :archive archive
1213 :extras (and (> (length (cdr package)) 4)
1214 ;; Older archive-contents files have only 4
1215 ;; elements here.
1216 (package--ac-desc-extras (cdr package)))))
1217 (pinned-to-archive (assoc name package-pinned-packages)))
1218 ;; Skip entirely if pinned to another archive.
1219 (when (not (and pinned-to-archive
1220 (not (equal (cdr pinned-to-archive) archive))))
1221 (setq package-archive-contents
1222 (package--append-to-alist pkg-desc package-archive-contents)))))
1223
1224(defun package--read-archive-file (file)
1225 "Re-read archive file FILE, if it exists.
1226Will return the data from the file, or nil if the file does not exist.
1227Will throw an error if the archive version is too new."
1228 (let ((filename (expand-file-name file package-user-dir)))
1229 (when (file-exists-p filename)
1230 (with-temp-buffer
1231 (insert-file-contents-literally filename)
1232 (let ((contents (read (current-buffer))))
1233 (if (> (car contents) package-archive-version)
1234 (error "Package archive version %d is higher than %d"
1235 (car contents) package-archive-version))
1236 (cdr contents))))))
1237
1238(defun package-read-archive-contents (archive)
1239 "Re-read archive contents for ARCHIVE.
1240If successful, set the variable `package-archive-contents'.
1241If the archive version is too new, signal an error."
1242 ;; Version 1 of 'archive-contents' is identical to our internal
1243 ;; representation.
1244 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1245 (contents (package--read-archive-file contents-file)))
1246 (when contents
1247 (dolist (package contents)
1248 (package--add-to-archive-contents package archive)))))
1249
1250(defun package-read-all-archive-contents ()
1251 "Re-read `archive-contents', if it exists.
1252If successful, set `package-archive-contents'."
1253 (setq package-archive-contents nil)
1254 (dolist (archive package-archives)
1255 (package-read-archive-contents (car archive))))
1256
1257;;;; Package Initialize
1258;; A bit of a milestone. This brings together some of the above
1259;; sections and populates all relevant lists of packages from contents
1260;; available on disk.
1261(defvar package--initialized nil)
1262
1263;;;###autoload
1264(defun package-initialize (&optional no-activate)
1265 "Load Emacs Lisp packages, and activate them.
1266The variable `package-load-list' controls which packages to load.
1267If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1268 (interactive)
1269 (setq package-alist nil)
1270 (package-load-all-descriptors)
1271 (package-read-all-archive-contents)
1272 (unless no-activate
1273 (dolist (elt package-alist)
1274 (package-activate (car elt))))
1275 (setq package--initialized t)
1276 ;; This uses `package--mapc' so it must be called after
1277 ;; `package--initialized' is t.
1278 (package--build-compatibility-table))
1279
1280
1281;;;; Populating `package-archive-contents' from archives
1282;; This subsection populates the variables listed above from the
1283;; actual archives, instead of from a local cache.
1284(defun package--download-one-archive (archive file)
1285 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1286ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1287similar to an entry in `package-alist'. Save the cached copy to
1288\"archives/NAME/archive-contents\" in `package-user-dir'."
1289 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1290 package-user-dir))
1291 (sig-file (concat file ".sig"))
1292 good-signatures)
1293 (package--with-work-buffer (cdr archive) file
1294 ;; Check signature of archive-contents, if desired.
981 (if (and package-check-signature 1295 (if (and package-check-signature
982 (not (member (package-desc-archive pkg-desc) 1296 (not (member archive package-unsigned-archives)))
983 package-unsigned-archives))) 1297 (if (package--archive-file-exists-p (cdr archive) sig-file)
984 (if (package--archive-file-exists-p location sig-file) 1298 (setq good-signatures (package--check-signature (cdr archive)
985 (setq good-signatures (package--check-signature location file)) 1299 file))
986 (unless (eq package-check-signature 'allow-unsigned) 1300 (unless (eq package-check-signature 'allow-unsigned)
987 (error "Unsigned package: `%s'" 1301 (error "Unsigned archive `%s'"
988 (package-desc-name pkg-desc))))) 1302 (car archive)))))
989 (package-unpack pkg-desc)) 1303 ;; Read the retrieved buffer to make sure it is valid (e.g. it
990 ;; Here the package has been installed successfully, mark it as 1304 ;; may fetch a URL redirect page).
991 ;; signed if appropriate. 1305 (when (listp (read (current-buffer)))
1306 (make-directory dir t)
1307 (write-region nil nil (expand-file-name file dir) nil 'silent)))
992 (when good-signatures 1308 (when good-signatures
993 ;; Write out good signatures into NAME-VERSION.signed file. 1309 ;; Write out good signatures into archive-contents.signed file.
994 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n") 1310 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
995 nil 1311 nil
996 (expand-file-name 1312 (expand-file-name (concat file ".signed") dir)
997 (concat (package-desc-full-name pkg-desc) 1313 nil 'silent))))
998 ".signed")
999 package-user-dir)
1000 nil 'silent)
1001 ;; Update the old pkg-desc which will be shown on the description buffer.
1002 (setf (package-desc-signed pkg-desc) t)
1003 ;; Update the new (activated) pkg-desc as well.
1004 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
1005 (if pkg-descs
1006 (setf (package-desc-signed (car pkg-descs)) t)))))
1007 1314
1008(defvar package--initialized nil) 1315(declare-function epg-check-configuration "epg-config"
1316 (config &optional minimum-version))
1317(declare-function epg-configuration "epg-config" ())
1318(declare-function epg-import-keys-from-file "epg" (context keys))
1009 1319
1010(defun package-installed-p (package &optional min-version) 1320;;;###autoload
1011 "Return true if PACKAGE, of MIN-VERSION or newer, is installed. 1321(defun package-import-keyring (&optional file)
1012If PACKAGE is a symbol, it is the package name and MIN-VERSION 1322 "Import keys from FILE."
1013should be a version list. 1323 (interactive "fFile: ")
1324 (setq file (expand-file-name file))
1325 (let ((context (epg-make-context 'OpenPGP))
1326 (homedir (expand-file-name "gnupg" package-user-dir)))
1327 (with-file-modes 448
1328 (make-directory homedir t))
1329 (setf (epg-context-home-directory context) homedir)
1330 (message "Importing %s..." (file-name-nondirectory file))
1331 (epg-import-keys-from-file context file)
1332 (message "Importing %s...done" (file-name-nondirectory file))))
1014 1333
1015If PACKAGE is a package-desc object, MIN-VERSION is ignored." 1334;;;###autoload
1016 (unless package--initialized (error "package.el is not yet initialized!")) 1335(defun package-refresh-contents ()
1017 (if (package-desc-p package) 1336 "Download descriptions of all configured ELPA packages.
1018 (let ((dir (package-desc-dir package))) 1337For each archive configured in the variable `package-archives',
1019 (and (stringp dir) 1338inform Emacs about the latest versions of all packages it offers,
1020 (file-exists-p dir))) 1339and make them available for download."
1021 (or 1340 (interactive)
1022 (let ((pkg-descs (cdr (assq package package-alist)))) 1341 ;; FIXME: Do it asynchronously.
1023 (and pkg-descs 1342 (unless (file-exists-p package-user-dir)
1024 (version-list-<= min-version 1343 (make-directory package-user-dir t))
1025 (package-desc-version (car pkg-descs))))) 1344 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1026 ;; Also check built-in packages. 1345 data-directory)))
1027 (package-built-in-p package min-version)))) 1346 (when (and package-check-signature (file-exists-p default-keyring))
1347 (condition-case-unless-debug error
1348 (progn
1349 (epg-check-configuration (epg-configuration))
1350 (package-import-keyring default-keyring))
1351 (error (message "Cannot import default keyring: %S" (cdr error))))))
1352 (dolist (archive package-archives)
1353 (condition-case-unless-debug nil
1354 (package--download-one-archive archive "archive-contents")
1355 (error (message "Failed to download `%s' archive."
1356 (car archive)))))
1357 (package-read-all-archive-contents)
1358 (package--build-compatibility-table)
1359 (message "Package refresh done"))
1028 1360
1361
1362;;; Dependency Management
1363;; Calculating the full transaction necessary for an installation,
1364;; keeping track of which packages were installed strictly as
1365;; dependencies, and determining which packages cannot be removed
1366;; because they are dependencies.
1029(defun package-compute-transaction (packages requirements &optional seen) 1367(defun package-compute-transaction (packages requirements &optional seen)
1030 "Return a list of packages to be installed, including PACKAGES. 1368 "Return a list of packages to be installed, including PACKAGES.
1031PACKAGES should be a list of `package-desc'. 1369PACKAGES should be a list of `package-desc'.
@@ -1109,109 +1447,20 @@ but version %s required"
1109 (cons found seen)))))))) 1447 (cons found seen))))))))
1110 packages) 1448 packages)
1111 1449
1112(defun package-read-from-string (str) 1450(defun package--find-non-dependencies ()
1113 "Read a Lisp expression from STR. 1451 "Return a list of installed packages which are not dependencies.
1114Signal an error if the entire string was not used." 1452Finds all packages in `package-alist' which are not dependencies
1115 (let* ((read-data (read-from-string str)) 1453of any other packages.
1116 (more-left 1454Used to populate `package-selected-packages'."
1117 (condition-case nil 1455 (let ((dep-list
1118 ;; The call to `ignore' suppresses a compiler warning. 1456 (delete-dups
1119 (progn (ignore (read-from-string 1457 (apply #'append
1120 (substring str (cdr read-data)))) 1458 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1121 t) 1459 package-alist)))))
1122 (end-of-file nil)))) 1460 (cl-loop for p in package-alist
1123 (if more-left 1461 for name = (car p)
1124 (error "Can't read whole string") 1462 unless (memq name dep-list)
1125 (car read-data)))) 1463 collect name)))
1126
1127(defun package--read-archive-file (file)
1128 "Re-read archive file FILE, if it exists.
1129Will return the data from the file, or nil if the file does not exist.
1130Will throw an error if the archive version is too new."
1131 (let ((filename (expand-file-name file package-user-dir)))
1132 (when (file-exists-p filename)
1133 (with-temp-buffer
1134 (insert-file-contents-literally filename)
1135 (let ((contents (read (current-buffer))))
1136 (if (> (car contents) package-archive-version)
1137 (error "Package archive version %d is higher than %d"
1138 (car contents) package-archive-version))
1139 (cdr contents))))))
1140
1141(defun package-read-all-archive-contents ()
1142 "Re-read `archive-contents', if it exists.
1143If successful, set `package-archive-contents'."
1144 (setq package-archive-contents nil)
1145 (dolist (archive package-archives)
1146 (package-read-archive-contents (car archive))))
1147
1148(defun package-read-archive-contents (archive)
1149 "Re-read archive contents for ARCHIVE.
1150If successful, set the variable `package-archive-contents'.
1151If the archive version is too new, signal an error."
1152 ;; Version 1 of 'archive-contents' is identical to our internal
1153 ;; representation.
1154 (let* ((contents-file (format "archives/%s/archive-contents" archive))
1155 (contents (package--read-archive-file contents-file)))
1156 (when contents
1157 (dolist (package contents)
1158 (package--add-to-archive-contents package archive)))))
1159
1160;; Package descriptor objects used inside the "archive-contents" file.
1161;; Changing this defstruct implies changing the format of the
1162;; "archive-contents" files.
1163(cl-defstruct (package--ac-desc
1164 (:constructor package-make-ac-desc (version reqs summary kind extras))
1165 (:copier nil)
1166 (:type vector))
1167 version reqs summary kind extras)
1168
1169(defun package--add-to-archive-contents (package archive)
1170 "Add the PACKAGE from the given ARCHIVE if necessary.
1171PACKAGE should have the form (NAME . PACKAGE--AC-DESC).
1172Also, add the originating archive to the `package-desc' structure."
1173 (let* ((name (car package))
1174 (version (package--ac-desc-version (cdr package)))
1175 (pkg-desc
1176 (package-desc-create
1177 :name name
1178 :version version
1179 :reqs (package--ac-desc-reqs (cdr package))
1180 :summary (package--ac-desc-summary (cdr package))
1181 :kind (package--ac-desc-kind (cdr package))
1182 :archive archive
1183 :extras (and (> (length (cdr package)) 4)
1184 ;; Older archive-contents files have only 4
1185 ;; elements here.
1186 (package--ac-desc-extras (cdr package)))))
1187 (pinned-to-archive (assoc name package-pinned-packages)))
1188 ;; Skip entirely if pinned to another archive.
1189 (when (not (and pinned-to-archive
1190 (not (equal (cdr pinned-to-archive) archive))))
1191 (setq package-archive-contents
1192 (package--append-to-alist pkg-desc package-archive-contents)))))
1193
1194(defun package--append-to-alist (pkg-desc alist)
1195 "Append an entry for PKG-DESC to the start of ALIST and return it.
1196This entry takes the form (`package-desc-name' PKG-DESC).
1197
1198If ALIST already has an entry with this name, destructively add
1199PKG-DESC to the cdr of this entry instead, sorted by version
1200number."
1201 (let* ((name (package-desc-name pkg-desc))
1202 (priority-version (package-desc-priority-version pkg-desc))
1203 (existing-packages (assq name alist)))
1204 (if (not existing-packages)
1205 (cons (list name pkg-desc)
1206 alist)
1207 (while (if (and (cdr existing-packages)
1208 (version-list-< priority-version
1209 (package-desc-priority-version
1210 (cadr existing-packages))))
1211 (setq existing-packages (cdr existing-packages))
1212 (push pkg-desc (cdr existing-packages))
1213 nil))
1214 alist)))
1215 1464
1216(defun package--user-selected-p (pkg) 1465(defun package--user-selected-p (pkg)
1217 "Return non-nil if PKG is a package was installed by the user. 1466 "Return non-nil if PKG is a package was installed by the user.
@@ -1224,6 +1473,153 @@ if it is still empty."
1224 (setq package-selected-packages (package--find-non-dependencies)))) 1473 (setq package-selected-packages (package--find-non-dependencies))))
1225 (memq pkg package-selected-packages)) 1474 (memq pkg package-selected-packages))
1226 1475
1476(defun package--get-deps (pkg &optional only)
1477 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1478 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1479 for name = (car p)
1480 when (assq name package-alist)
1481 collect name))
1482 (indirect-deps (unless (eq only 'direct)
1483 (delete-dups
1484 (cl-loop for p in direct-deps
1485 append (package--get-deps p))))))
1486 (cl-case only
1487 (direct direct-deps)
1488 (separate (list direct-deps indirect-deps))
1489 (indirect indirect-deps)
1490 (t (delete-dups (append direct-deps indirect-deps))))))
1491
1492(defun package--removable-packages ()
1493 "Return a list of names of packages no longer needed.
1494These are packages which are neither contained in
1495`package-selected-packages' nor a dependency of one that is."
1496 (let ((needed (cl-loop for p in package-selected-packages
1497 if (assq p package-alist)
1498 ;; `p' and its dependencies are needed.
1499 append (cons p (package--get-deps p)))))
1500 (cl-loop for p in (mapcar #'car package-alist)
1501 unless (memq p needed)
1502 collect p)))
1503
1504(defun package--used-elsewhere-p (pkg-desc &optional pkg-list)
1505 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST.
1506Return the first package found in PKG-LIST of which PKG is a
1507dependency.
1508
1509When not specified, PKG-LIST defaults to `package-alist'
1510with PKG-DESC entry removed."
1511 (unless (string= (package-desc-status pkg-desc) "obsolete")
1512 (let ((pkg (package-desc-name pkg-desc)))
1513 (cl-loop with alist = (or pkg-list
1514 (remove (assq pkg package-alist)
1515 package-alist))
1516 for p in alist thereis
1517 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr p))))
1518 (car p))))))
1519
1520(defun package--sort-deps-in-alist (package only)
1521 "Return a list of dependencies for PACKAGE sorted by dependency.
1522PACKAGE is included as the first element of the returned list.
1523ONLY is an alist associating package names to package objects.
1524Only these packages will be in the return value an their cdrs are
1525destructively set to nil in ONLY."
1526 (let ((out))
1527 (dolist (dep (package-desc-reqs package))
1528 (when-let ((cell (assq (car dep) only))
1529 (dep-package (cdr-safe cell)))
1530 (setcdr cell nil)
1531 (setq out (append (package--sort-deps-in-alist dep-package only)
1532 out))))
1533 (cons package out)))
1534
1535(defun package--sort-by-dependence (package-list)
1536 "Return PACKAGE-LIST sorted by dependence.
1537That is, any element of the returned list is guaranteed to not
1538directly depend on any elements that come before it.
1539
1540PACKAGE-LIST is a list of package-desc objects.
1541Indirect dependencies are guaranteed to be returned in order only
1542if all the in-between dependencies are also in PACKAGE-LIST."
1543 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
1544 out-list)
1545 (dolist (cell alist out-list)
1546 ;; `package--sort-deps-in-alist' destructively changes alist, so
1547 ;; some cells might already be empty. We check this here.
1548 (when-let ((pkg-desc (cdr cell)))
1549 (setcdr cell nil)
1550 (setq out-list
1551 (append (package--sort-deps-in-alist pkg-desc alist)
1552 out-list))))))
1553
1554
1555;;; Installation Functions
1556;; As opposed to the previous section (which listed some underlying
1557;; functions necessary for installation), this one contains the actual
1558;; functions that install packages. The package itself can be
1559;; installed in a variety of ways (archives, buffer, file), but
1560;; requirements (dependencies) are always satisfied by looking in
1561;; `package-archive-contents'.
1562(defun package-archive-base (desc)
1563 "Return the archive containing the package NAME."
1564 (cdr (assoc (package-desc-archive desc) package-archives)))
1565
1566(defun package-install-from-archive (pkg-desc)
1567 "Download and install a tar package."
1568 ;; This won't happen, unless the archive is doing something wrong.
1569 (when (eq (package-desc-kind pkg-desc) 'dir)
1570 (error "Can't install directory package from archive"))
1571 (let* ((location (package-archive-base pkg-desc))
1572 (file (concat (package-desc-full-name pkg-desc)
1573 (package-desc-suffix pkg-desc)))
1574 (sig-file (concat file ".sig"))
1575 good-signatures pkg-descs)
1576 (package--with-work-buffer location file
1577 (if (and package-check-signature
1578 (not (member (package-desc-archive pkg-desc)
1579 package-unsigned-archives)))
1580 (if (package--archive-file-exists-p location sig-file)
1581 (setq good-signatures (package--check-signature location file))
1582 (unless (eq package-check-signature 'allow-unsigned)
1583 (error "Unsigned package: `%s'"
1584 (package-desc-name pkg-desc)))))
1585 (package-unpack pkg-desc))
1586 ;; Here the package has been installed successfully, mark it as
1587 ;; signed if appropriate.
1588 (when good-signatures
1589 ;; Write out good signatures into NAME-VERSION.signed file.
1590 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1591 nil
1592 (expand-file-name
1593 (concat (package-desc-full-name pkg-desc)
1594 ".signed")
1595 package-user-dir)
1596 nil 'silent)
1597 ;; Update the old pkg-desc which will be shown on the description buffer.
1598 (setf (package-desc-signed pkg-desc) t)
1599 ;; Update the new (activated) pkg-desc as well.
1600 (setq pkg-descs (cdr (assq (package-desc-name pkg-desc) package-alist)))
1601 (if pkg-descs
1602 (setf (package-desc-signed (car pkg-descs)) t)))))
1603
1604(defun package-installed-p (package &optional min-version)
1605 "Return true if PACKAGE, of MIN-VERSION or newer, is installed.
1606If PACKAGE is a symbol, it is the package name and MIN-VERSION
1607should be a version list.
1608
1609If PACKAGE is a package-desc object, MIN-VERSION is ignored."
1610 (unless package--initialized (error "package.el is not yet initialized!"))
1611 (if (package-desc-p package)
1612 (let ((dir (package-desc-dir package)))
1613 (and (stringp dir)
1614 (file-exists-p dir)))
1615 (or
1616 (let ((pkg-descs (cdr (assq package package-alist))))
1617 (and pkg-descs
1618 (version-list-<= min-version
1619 (package-desc-version (car pkg-descs)))))
1620 ;; Also check built-in packages.
1621 (package-built-in-p package min-version))))
1622
1227(defun package-download-transaction (packages) 1623(defun package-download-transaction (packages)
1228 "Download and install all the packages in PACKAGES. 1624 "Download and install all the packages in PACKAGES.
1229PACKAGES should be a list of package-desc. 1625PACKAGES should be a list of package-desc.
@@ -1276,20 +1672,6 @@ to install it but still mark it as selected."
1276 (package-compute-transaction () 1672 (package-compute-transaction ()
1277 (list (list pkg)))))) 1673 (list (list pkg))))))
1278 1674
1279;;;###autoload
1280(defun package-reinstall (pkg)
1281 "Reinstall package PKG.
1282PKG should be either a symbol, the package name, or a package-desc
1283object."
1284 (interactive (list (intern (completing-read
1285 "Reinstall package: "
1286 (mapcar #'symbol-name
1287 (mapcar #'car package-alist))))))
1288 (package-delete
1289 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
1290 'force 'nosave)
1291 (package-install pkg 'dont-select))
1292
1293(defun package-strip-rcs-id (str) 1675(defun package-strip-rcs-id (str)
1294 "Strip RCS version ID from the version string STR. 1676 "Strip RCS version ID from the version string STR.
1295If the result looks like a dotted numeric version, return it. 1677If the result looks like a dotted numeric version, return it.
@@ -1304,120 +1686,6 @@ Otherwise return nil."
1304 1686
1305(declare-function lm-homepage "lisp-mnt" (&optional file)) 1687(declare-function lm-homepage "lisp-mnt" (&optional file))
1306 1688
1307(defun package--prepare-dependencies (deps)
1308 "Turn DEPS into an acceptable list of dependencies.
1309
1310Any parts missing a version string get a default version string
1311of \"0\" (meaning any version) and an appropriate level of lists
1312is wrapped around any parts requiring it."
1313 (cond
1314 ((not (listp deps))
1315 (error "Invalid requirement specifier: %S" deps))
1316 (t (mapcar (lambda (dep)
1317 (cond
1318 ((symbolp dep) `(,dep "0"))
1319 ((stringp dep)
1320 (error "Invalid requirement specifier: %S" dep))
1321 ((and (listp dep) (null (cdr dep)))
1322 (list (car dep) "0"))
1323 (t dep)))
1324 deps))))
1325
1326(defun package-buffer-info ()
1327 "Return a `package-desc' describing the package in the current buffer.
1328
1329If the buffer does not contain a conforming package, signal an
1330error. If there is a package, narrow the buffer to the file's
1331boundaries."
1332 (goto-char (point-min))
1333 (unless (re-search-forward "^;;; \\([^ ]*\\)\\.el ---[ \t]*\\(.*?\\)[ \t]*\\(-\\*-.*-\\*-[ \t]*\\)?$" nil t)
1334 (error "Package lacks a file header"))
1335 (let ((file-name (match-string-no-properties 1))
1336 (desc (match-string-no-properties 2))
1337 (start (line-beginning-position)))
1338 (unless (search-forward (concat ";;; " file-name ".el ends here"))
1339 (error "Package lacks a terminating comment"))
1340 ;; Try to include a trailing newline.
1341 (forward-line)
1342 (narrow-to-region start (point))
1343 (require 'lisp-mnt)
1344 ;; Use some headers we've invented to drive the process.
1345 (let* ((requires-str (lm-header "package-requires"))
1346 ;; Prefer Package-Version; if defined, the package author
1347 ;; probably wants us to use it. Otherwise try Version.
1348 (pkg-version
1349 (or (package-strip-rcs-id (lm-header "package-version"))
1350 (package-strip-rcs-id (lm-header "version"))))
1351 (homepage (lm-homepage)))
1352 (unless pkg-version
1353 (error
1354 "Package lacks a \"Version\" or \"Package-Version\" header"))
1355 (package-desc-from-define
1356 file-name pkg-version desc
1357 (if requires-str
1358 (package--prepare-dependencies
1359 (package-read-from-string requires-str)))
1360 :kind 'single
1361 :url homepage))))
1362
1363(declare-function tar-get-file-descriptor "tar-mode" (file))
1364(declare-function tar--extract "tar-mode" (descriptor))
1365
1366(defun package-tar-file-info ()
1367 "Find package information for a tar file.
1368The return result is a `package-desc'."
1369 (cl-assert (derived-mode-p 'tar-mode))
1370 (let* ((dir-name (file-name-directory
1371 (tar-header-name (car tar-parse-info))))
1372 (desc-file (package--description-file dir-name))
1373 (tar-desc (tar-get-file-descriptor (concat dir-name desc-file))))
1374 (unless tar-desc
1375 (error "No package descriptor file found"))
1376 (with-current-buffer (tar--extract tar-desc)
1377 (unwind-protect
1378 (or (package--read-pkg-desc 'tar)
1379 (error "Can't find define-package in %s"
1380 (tar-header-name tar-desc)))
1381 (kill-buffer (current-buffer))))))
1382
1383(defun package-dir-info ()
1384 "Find package information for a directory.
1385The return result is a `package-desc'."
1386 (cl-assert (derived-mode-p 'dired-mode))
1387 (let* ((desc-file (package--description-file default-directory)))
1388 (if (file-readable-p desc-file)
1389 (with-temp-buffer
1390 (insert-file-contents desc-file)
1391 (package--read-pkg-desc 'dir))
1392 (let ((files (directory-files default-directory t "\\.el\\'" t))
1393 info)
1394 (while files
1395 (with-temp-buffer
1396 (insert-file-contents (pop files))
1397 ;; When we find the file with the data,
1398 (when (setq info (ignore-errors (package-buffer-info)))
1399 ;; stop looping,
1400 (setq files nil)
1401 ;; set the 'dir kind,
1402 (setf (package-desc-kind info) 'dir))))
1403 ;; and return the info.
1404 info))))
1405
1406(defun package--read-pkg-desc (kind)
1407 "Read a `define-package' form in current buffer.
1408Return the pkg-desc, with desc-kind set to KIND."
1409 (goto-char (point-min))
1410 (unwind-protect
1411 (let* ((pkg-def-parsed (read (current-buffer)))
1412 (pkg-desc
1413 (when (eq (car pkg-def-parsed) 'define-package)
1414 (apply #'package-desc-from-define
1415 (append (cdr pkg-def-parsed))))))
1416 (when pkg-desc
1417 (setf (package-desc-kind pkg-desc) kind)
1418 pkg-desc))))
1419
1420
1421;;;###autoload 1689;;;###autoload
1422(defun package-install-from-buffer () 1690(defun package-install-from-buffer ()
1423 "Install a package from the current buffer. 1691 "Install a package from the current buffer.
@@ -1469,22 +1737,6 @@ The file can either be a tar file or an Emacs Lisp file."
1469 (when (string-match "\\.tar\\'" file) (tar-mode))) 1737 (when (string-match "\\.tar\\'" file) (tar-mode)))
1470 (package-install-from-buffer))) 1738 (package-install-from-buffer)))
1471 1739
1472(defun package--get-deps (pkg &optional only)
1473 (let* ((pkg-desc (cadr (assq pkg package-alist)))
1474 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
1475 for name = (car p)
1476 when (assq name package-alist)
1477 collect name))
1478 (indirect-deps (unless (eq only 'direct)
1479 (delete-dups
1480 (cl-loop for p in direct-deps
1481 append (package--get-deps p))))))
1482 (cl-case only
1483 (direct direct-deps)
1484 (separate (list direct-deps indirect-deps))
1485 (indirect indirect-deps)
1486 (t (delete-dups (append direct-deps indirect-deps))))))
1487
1488;;;###autoload 1740;;;###autoload
1489(defun package-install-user-selected-packages () 1741(defun package-install-user-selected-packages ()
1490 "Ensure packages in `package-selected-packages' are installed. 1742 "Ensure packages in `package-selected-packages' are installed.
@@ -1507,22 +1759,8 @@ If some packages are not installed propose to install them."
1507 (mapc #'package-install lst)) 1759 (mapc #'package-install lst))
1508 (message "All your packages are already installed"))))) 1760 (message "All your packages are already installed")))))
1509 1761
1510(defun package--used-elsewhere-p (pkg-desc &optional pkg-list) 1762
1511 "Non-nil if PKG-DESC is a dependency of a package in PKG-LIST. 1763;;; Package Deletion
1512Return the first package found in PKG-LIST of which PKG is a
1513dependency.
1514
1515When not specified, PKG-LIST defaults to `package-alist'
1516with PKG-DESC entry removed."
1517 (unless (string= (package-desc-status pkg-desc) "obsolete")
1518 (let ((pkg (package-desc-name pkg-desc)))
1519 (cl-loop with alist = (or pkg-list
1520 (remove (assq pkg package-alist)
1521 package-alist))
1522 for p in alist thereis
1523 (and (memq pkg (mapcar #'car (package-desc-reqs (cadr p))))
1524 (car p))))))
1525
1526(defun package--newest-p (pkg) 1764(defun package--newest-p (pkg)
1527 "Return t if PKG is the newest package with its name." 1765 "Return t if PKG is the newest package with its name."
1528 (equal (cadr (assq (package-desc-name pkg) package-alist)) 1766 (equal (cadr (assq (package-desc-name pkg) package-alist))
@@ -1577,17 +1815,19 @@ If NOSAVE is non-nil, the package is not removed from
1577 (setq package-alist (delq pkgs package-alist)))) 1815 (setq package-alist (delq pkgs package-alist))))
1578 (message "Package `%s' deleted." (package-desc-full-name pkg-desc)))))) 1816 (message "Package `%s' deleted." (package-desc-full-name pkg-desc))))))
1579 1817
1580(defun package--removable-packages () 1818;;;###autoload
1581 "Return a list of names of packages no longer needed. 1819(defun package-reinstall (pkg)
1582These are packages which are neither contained in 1820 "Reinstall package PKG.
1583`package-selected-packages' nor a dependency of one that is." 1821PKG should be either a symbol, the package name, or a package-desc
1584 (let ((needed (cl-loop for p in package-selected-packages 1822object."
1585 if (assq p package-alist) 1823 (interactive (list (intern (completing-read
1586 ;; `p' and its dependencies are needed. 1824 "Reinstall package: "
1587 append (cons p (package--get-deps p))))) 1825 (mapcar #'symbol-name
1588 (cl-loop for p in (mapcar #'car package-alist) 1826 (mapcar #'car package-alist))))))
1589 unless (memq p needed) 1827 (package-delete
1590 collect p))) 1828 (if (package-desc-p pkg) pkg (cadr (assq pkg package-alist)))
1829 'force 'nosave)
1830 (package-install pkg 'dont-select))
1591 1831
1592;;;###autoload 1832;;;###autoload
1593(defun package-autoremove () 1833(defun package-autoremove ()
@@ -1614,157 +1854,6 @@ will be deleted."
1614 removable)) 1854 removable))
1615 (message "Nothing to autoremove"))))) 1855 (message "Nothing to autoremove")))))
1616 1856
1617(defun package-archive-base (desc)
1618 "Return the archive containing the package NAME."
1619 (cdr (assoc (package-desc-archive desc) package-archives)))
1620
1621(defun package-archive-priority (archive)
1622 "Return the priority of ARCHIVE.
1623
1624The archive priorities are specified in
1625`package-archive-priorities'. If not given there, the priority
1626defaults to 0."
1627 (or (cdr (assoc archive package-archive-priorities))
1628 0))
1629
1630(defun package-desc-priority-version (pkg-desc)
1631 "Return the version PKG-DESC with the archive priority prepended.
1632
1633This allows for easy comparison of package versions from
1634different archives if archive priorities are meant to be taken in
1635consideration."
1636 (cons (package-archive-priority
1637 (package-desc-archive pkg-desc))
1638 (package-desc-version pkg-desc)))
1639
1640(defun package--download-one-archive (archive file)
1641 "Retrieve an archive file FILE from ARCHIVE, and cache it.
1642ARCHIVE should be a cons cell of the form (NAME . LOCATION),
1643similar to an entry in `package-alist'. Save the cached copy to
1644\"archives/NAME/archive-contents\" in `package-user-dir'."
1645 (let ((dir (expand-file-name (format "archives/%s" (car archive))
1646 package-user-dir))
1647 (sig-file (concat file ".sig"))
1648 good-signatures)
1649 (package--with-work-buffer (cdr archive) file
1650 ;; Check signature of archive-contents, if desired.
1651 (if (and package-check-signature
1652 (not (member archive package-unsigned-archives)))
1653 (if (package--archive-file-exists-p (cdr archive) sig-file)
1654 (setq good-signatures (package--check-signature (cdr archive)
1655 file))
1656 (unless (eq package-check-signature 'allow-unsigned)
1657 (error "Unsigned archive `%s'"
1658 (car archive)))))
1659 ;; Read the retrieved buffer to make sure it is valid (e.g. it
1660 ;; may fetch a URL redirect page).
1661 (when (listp (read (current-buffer)))
1662 (make-directory dir t)
1663 (write-region nil nil (expand-file-name file dir) nil 'silent)))
1664 (when good-signatures
1665 ;; Write out good signatures into archive-contents.signed file.
1666 (write-region (mapconcat #'epg-signature-to-string good-signatures "\n")
1667 nil
1668 (expand-file-name (concat file ".signed") dir)
1669 nil 'silent))))
1670
1671(declare-function epg-check-configuration "epg-config"
1672 (config &optional minimum-version))
1673(declare-function epg-configuration "epg-config" ())
1674(declare-function epg-import-keys-from-file "epg" (context keys))
1675
1676;;;###autoload
1677(defun package-import-keyring (&optional file)
1678 "Import keys from FILE."
1679 (interactive "fFile: ")
1680 (setq file (expand-file-name file))
1681 (let ((context (epg-make-context 'OpenPGP))
1682 (homedir (expand-file-name "gnupg" package-user-dir)))
1683 (with-file-modes 448
1684 (make-directory homedir t))
1685 (setf (epg-context-home-directory context) homedir)
1686 (message "Importing %s..." (file-name-nondirectory file))
1687 (epg-import-keys-from-file context file)
1688 (message "Importing %s...done" (file-name-nondirectory file))))
1689
1690(defun package--build-compatibility-table ()
1691 "Build `package--compatibility-table' with `package--mapc'."
1692 ;; Build compat table.
1693 (setq package--compatibility-table (make-hash-table :test 'eq))
1694 (package--mapc #'package--add-to-compatibility-table))
1695
1696;;;###autoload
1697(defun package-refresh-contents ()
1698 "Download descriptions of all configured ELPA packages.
1699For each archive configured in the variable `package-archives',
1700inform Emacs about the latest versions of all packages it offers,
1701and make them available for download."
1702 (interactive)
1703 ;; FIXME: Do it asynchronously.
1704 (unless (file-exists-p package-user-dir)
1705 (make-directory package-user-dir t))
1706 (let ((default-keyring (expand-file-name "package-keyring.gpg"
1707 data-directory)))
1708 (when (and package-check-signature (file-exists-p default-keyring))
1709 (condition-case-unless-debug error
1710 (progn
1711 (epg-check-configuration (epg-configuration))
1712 (package-import-keyring default-keyring))
1713 (error (message "Cannot import default keyring: %S" (cdr error))))))
1714 (dolist (archive package-archives)
1715 (condition-case-unless-debug nil
1716 (package--download-one-archive archive "archive-contents")
1717 (error (message "Failed to download `%s' archive."
1718 (car archive)))))
1719 (package-read-all-archive-contents)
1720 (package--build-compatibility-table)
1721 (message "Package refresh done"))
1722
1723(defun package--find-non-dependencies ()
1724 "Return a list of installed packages which are not dependencies.
1725Finds all packages in `package-alist' which are not dependencies
1726of any other packages.
1727Used to populate `package-selected-packages'."
1728 (let ((dep-list
1729 (delete-dups
1730 (apply #'append
1731 (mapcar (lambda (p) (mapcar #'car (package-desc-reqs (cadr p))))
1732 package-alist)))))
1733 (cl-loop for p in package-alist
1734 for name = (car p)
1735 unless (memq name dep-list)
1736 collect name)))
1737
1738;;;###autoload
1739(defun package-initialize (&optional no-activate)
1740 "Load Emacs Lisp packages, and activate them.
1741The variable `package-load-list' controls which packages to load.
1742If optional arg NO-ACTIVATE is non-nil, don't activate packages."
1743 (interactive)
1744 (setq package-alist nil)
1745 (package-load-all-descriptors)
1746 (package-read-all-archive-contents)
1747 (unless no-activate
1748 (dolist (elt package-alist)
1749 (package-activate (car elt))))
1750 (setq package--initialized t)
1751 ;; This uses `package--mapc' so it must be called after
1752 ;; `package--initialized' is t.
1753 (package--build-compatibility-table))
1754
1755(defun package--add-to-compatibility-table (pkg)
1756 "If PKG is compatible (without dependencies), add to the compatibility table.
1757PKG is a package-desc object.
1758Only adds if its version is higher than what's already stored in
1759the table."
1760 (unless (package--incompatible-p pkg 'shallow)
1761 (let* ((name (package-desc-name pkg))
1762 (version (or (package-desc-version pkg) '(0)))
1763 (table-version (gethash name package--compatibility-table)))
1764 (when (or (not table-version)
1765 (version-list-< table-version version))
1766 (puthash name version package--compatibility-table)))))
1767
1768 1857
1769;;;; Package description buffer. 1858;;;; Package description buffer.
1770 1859
@@ -1798,6 +1887,8 @@ the table."
1798 (with-current-buffer standard-output 1887 (with-current-buffer standard-output
1799 (describe-package-1 package))))) 1888 (describe-package-1 package)))))
1800 1889
1890(declare-function lm-commentary "lisp-mnt" (&optional file))
1891
1801(defun describe-package-1 (pkg) 1892(defun describe-package-1 (pkg)
1802 (require 'lisp-mnt) 1893 (require 'lisp-mnt)
1803 (let* ((desc (or 1894 (let* ((desc (or
@@ -2392,6 +2483,25 @@ If optional arg BUTTON is non-nil, describe its associated package."
2392 (aref (cadr entry) 2) 2483 (aref (cadr entry) 2)
2393 ""))) 2484 "")))
2394 2485
2486(defun package-archive-priority (archive)
2487 "Return the priority of ARCHIVE.
2488
2489The archive priorities are specified in
2490`package-archive-priorities'. If not given there, the priority
2491defaults to 0."
2492 (or (cdr (assoc archive package-archive-priorities))
2493 0))
2494
2495(defun package-desc-priority-version (pkg-desc)
2496 "Return the version PKG-DESC with the archive priority prepended.
2497
2498This allows for easy comparison of package versions from
2499different archives if archive priorities are meant to be taken in
2500consideration."
2501 (cons (package-archive-priority
2502 (package-desc-archive pkg-desc))
2503 (package-desc-version pkg-desc)))
2504
2395(defun package-menu--find-upgrades () 2505(defun package-menu--find-upgrades ()
2396 (let (installed available upgrades) 2506 (let (installed available upgrades)
2397 ;; Build list of installed/available packages in this buffer. 2507 ;; Build list of installed/available packages in this buffer.
@@ -2441,40 +2551,6 @@ call will upgrade the package."
2441 (length upgrades) 2551 (length upgrades)
2442 (if (= (length upgrades) 1) "" "s"))))) 2552 (if (= (length upgrades) 1) "" "s")))))
2443 2553
2444(defun package--sort-deps-in-alist (package only)
2445 "Return a list of dependencies for PACKAGE sorted by dependency.
2446PACKAGE is included as the first element of the returned list.
2447ONLY is an alist associating package names to package objects.
2448Only these packages will be in the return value an their cdrs are
2449destructively set to nil in ONLY."
2450 (let ((out))
2451 (dolist (dep (package-desc-reqs package))
2452 (when-let ((cell (assq (car dep) only))
2453 (dep-package (cdr-safe cell)))
2454 (setcdr cell nil)
2455 (setq out (append (package--sort-deps-in-alist dep-package only)
2456 out))))
2457 (cons package out)))
2458
2459(defun package--sort-by-dependence (package-list)
2460 "Return PACKAGE-LIST sorted by dependence.
2461That is, any element of the returned list is guaranteed to not
2462directly depend on any elements that come before it.
2463
2464PACKAGE-LIST is a list of package-desc objects.
2465Indirect dependencies are guaranteed to be returned in order only
2466if all the in-between dependencies are also in PACKAGE-LIST."
2467 (let ((alist (mapcar (lambda (p) (cons (package-desc-name p) p)) package-list))
2468 out-list)
2469 (dolist (cell alist out-list)
2470 ;; `package--sort-deps-in-alist' destructively changes alist, so
2471 ;; some cells might already be empty. We check this here.
2472 (when-let ((pkg-desc (cdr cell)))
2473 (setcdr cell nil)
2474 (setq out-list
2475 (append (package--sort-deps-in-alist pkg-desc alist)
2476 out-list))))))
2477
2478(defun package-menu-execute (&optional noquery) 2554(defun package-menu-execute (&optional noquery)
2479 "Perform marked Package Menu actions. 2555 "Perform marked Package Menu actions.
2480Packages marked for installation are downloaded and installed; 2556Packages marked for installation are downloaded and installed;