]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/emacs-lisp/package.el (package-get-version): New macro
authorStefan Monnier <monnier@iro.umontreal.ca>
Thu, 18 Oct 2018 16:17:52 +0000 (12:17 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Thu, 18 Oct 2018 16:17:52 +0000 (12:17 -0400)
etc/NEWS
lisp/emacs-lisp/package.el

index b46dcae9c23dbb2d0b97fb2ee41b23045892f6d6..2ebe5a16a22d629c485acf3aaf25a8d9cb88e75d 100644 (file)
--- 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
index 2ddab6536308e0628a66577ca4e40aa7960c9b15..06e9956da4aaa18520dfc08053e451fe64c0b3a7 100644 (file)
@@ -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 "<pkg>-<vers>",
+     ;; 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
+     ;; "-<vers>" 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