summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2018-10-18 12:17:52 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2018-10-18 12:17:52 -0400
commitf35916ce510968cf32a34cc32ebc21dd9be30443 (patch)
treea869d7494b02ffd129c761a93c3e64aab7fd5170 /lisp/emacs-lisp
parent46106eec16ddb2294e06f9e482b9183777b90014 (diff)
downloademacs-f35916ce510968cf32a34cc32ebc21dd9be30443.tar.gz
emacs-f35916ce510968cf32a34cc32ebc21dd9be30443.tar.bz2
emacs-f35916ce510968cf32a34cc32ebc21dd9be30443.zip
* lisp/emacs-lisp/package.el (package-get-version): New macro
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/package.el35
1 files changed, 33 insertions, 2 deletions
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 "<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