From f35916ce510968cf32a34cc32ebc21dd9be30443 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Thu, 18 Oct 2018 12:17:52 -0400 Subject: [PATCH] * lisp/emacs-lisp/package.el (package-get-version): New macro --- etc/NEWS | 3 +++ lisp/emacs-lisp/package.el | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index b46dcae9c23..2ebe5a16a22 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -414,6 +414,9 @@ This enables more efficient backends. See the docstring of ** Package +*** New macro `package-get-version` lets packages query their own version +Example use in auctex.el: (defconst auctex-version (package-get-version)) + *** New 'package-quickstart' feature. When 'package-quickstart' is non-nil, package.el precomputes a big autoloads file so that activation of packages can be done much faster, which can speed up diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el index 2ddab653630..06e9956da4a 100644 --- a/lisp/emacs-lisp/package.el +++ b/lisp/emacs-lisp/package.el @@ -689,8 +689,9 @@ PKG-DESC is a `package-desc' object." Load the autoloads file, and ensure `load-path' is setup. If RELOAD is non-nil, also load all files in the package that correspond to previously loaded files." - (let* ((loaded-files-list (when reload - (package--list-loaded-files (package-desc-dir pkg-desc))))) + (let* ((loaded-files-list + (when reload + (package--list-loaded-files (package-desc-dir pkg-desc))))) ;; Add to load path, add autoloads, and activate the package. (package--activate-autoloads-and-load-path pkg-desc) ;; Call `load' on all files in `package-desc-dir' already present in @@ -3450,6 +3451,36 @@ The list is displayed in a buffer named `*Packages*'." (interactive) (list-packages t)) +;;;###autoload +(defmacro package-get-version () + "Return the version number of the package in which this is used. +Assumes it is used from an Elisp file placed inside the top-level directory +of an installed ELPA package. +The return value is a string (or nil in case we can't find it)." + ;; Hack alert! + (let ((file + (or (if (boundp 'byte-compile-current-file) byte-compile-current-file) + load-file-name + buffer-file-name))) + (cond + ((null file) nil) + ;; Packages are normally installed into directories named "-", + ;; so get the version number from there. + ((string-match "/[^/]+-\\([0-9]\\(?:[0-9.]\\|pre\\|beta\\|alpha\\|snapshot\\)+\\)/[^/]+\\'" file) + (match-string 1 file)) + ;; For packages run straight from the an elpa.git clone, there's no + ;; "-" in the directory name, so we have to fetch the version + ;; the hard way. + (t + (let* ((pkgdir (file-name-directory file)) + (pkgname (file-name-nondirectory (directory-file-name pkgdir))) + (mainfile (expand-file-name (concat pkgname ".el") pkgdir))) + (when (file-readable-p mainfile) + (with-temp-buffer + (insert-file-contents mainfile) + (or (lm-header "package-version") + (lm-header "version"))))))))) + ;;;; Quickstart: precompute activation actions for faster start up. ;; Activating packages via `package-initialize' is costly: for N installed -- 2.39.5