updated my/list-package-and-versions

This commit is contained in:
Jason Tian 2024-06-28 12:20:20 +08:00
parent 95f25de948
commit a293015346

View file

@ -360,19 +360,25 @@ DIRECTORY is the directory where the org files are located."
(defun my/list-packages-and-versions (&optional filepath) (defun my/list-packages-and-versions (&optional filepath)
"List installed packages and their versions. "List installed packages and their versions.
If FILEPATH is provided, export the output to the specified file. If FILEPATH is provided, export the output to the specified file.
If the file is an org file, insert a title with the Emacs version at the beginning.
Otherwise, output to the message buffer." Otherwise, output to the message buffer."
(interactive) (interactive)
(package-initialize) (package-initialize)
(let ((pkgs (mapcar 'car package-alist)) (let ((pkgs (mapcar 'car package-alist)))
(output-buffer (or filepath "*Packages and Versions*")))
(with-temp-buffer (with-temp-buffer
;; Insert title if the output is an org file
(when (and filepath (string-suffix-p ".org" filepath))
(insert (format "#+TITLE: My Packages and Versions on Emacs %s\n" emacs-version))
(insert (format "#+DATE: %sn\n" (format-time-string "%Y-%m-%d %H:%M:%S"))))
;; Insert package and version information
(dolist (pkg pkgs) (dolist (pkg pkgs)
(insert (format "%s - %s\n" (insert (format "%s - %s\n"
pkg (package-desc-version (cadr (assq pkg package-alist)))))) pkg (package-desc-version (cadr (assq pkg package-alist))))))
;; Output to message buffer or write to file
(if (or (called-interactively-p 'interactive) (not filepath)) (if (or (called-interactively-p 'interactive) (not filepath))
(message "%s" (buffer-string)) (message "%s" (buffer-string))
(when filepath (when filepath
(set-buffer-file-coding-system 'utf-8-unix) (set-buffer-file-coding-system 'utf-8-unix)
(write-file filepath)))))) (write-file filepath))))))