Skip to content

Commit 38ca1ca

Browse files
committed
Add use-package-tags-load function
1 parent d16eb0c commit 38ca1ca

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

use-package-tags-test.el

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
(expect (use-package-tags-collect-tags "./tests/init.el" :sort t)
6767
:to-equal '(active bar foo inactive))))
6868

69+
(describe "use-package-tags-load"
70+
(xit "loads packages with a tag and add the tag to the profile")
71+
(xit "If an error occurs, don't add the tag to the profile"))
72+
73+
(describe "use-package-tags--unloaded-tags (private function)"
74+
(xit "returns tags that are not in the profile"))
75+
6976
(describe "use-package-tags--source-buffer-list (private function)"
7077
(describe "When t is given"
7178
(if (file-exists-p (expand-file-name "init.el" user-emacs-directory))

use-package-tags.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,49 @@ If SORT is non-nil, the result will be lexicographically sorted."
290290
(cl-remove-duplicates)
291291
(order)))))
292292

293+
(defun use-package-tags--unloaded-tags ()
294+
"Return a list of tags that are not in the current profile."
295+
(-difference (use-package-tags-collect-tags t :sort t)
296+
use-package-tags-current-profile))
297+
298+
;;;###autoload
299+
(defun use-package-tags-load (tag)
300+
"Load packages with a TAG.
301+
302+
This function evalates `use-package' forms with the selected tag
303+
in `use-package-tags-init-files'.
304+
305+
After successfully loading all matching packages, the tag will be
306+
added to `use-package-tags-current-profile', without saving the value
307+
to `custom-file'."
308+
(interactive (list (completing-read "Load packages with tag: "
309+
(use-package-tags--unloaded-tags)
310+
nil 'match)))
311+
(cl-labels
312+
((get-keyword (prop rest) (-some->> (member prop rest)
313+
(nth 1)))
314+
(has-tag-p (tag rest)
315+
(let ((tags (cdr (get-keyword :tags rest))))
316+
(memq tag (cl-etypecase tags
317+
(list tags)
318+
(symbol (list tags)))))))
319+
(setq tag (cl-etypecase tag
320+
(tag tag)
321+
(string (intern tag))))
322+
(push tag use-package-tags-current-profile)
323+
(condition-case err
324+
(use-package-tags--with-package-forms
325+
(use-package-tags--source-buffer-list t)
326+
(let ((exp (read (current-buffer))))
327+
(when (has-tag-p tag exp)
328+
(message "Loading package %s configured at %s..."
329+
(nth 1 exp)
330+
(abbreviate-file-name (buffer-file-name)))
331+
(eval exp))))
332+
(error (progn
333+
(message "Error while loading the package: %s" err)
334+
(cl-delete tag use-package-tags-current-profile)
335+
(error err))))))
336+
293337
(provide 'use-package-tags)
294338
;;; use-package-tags.el ends here

0 commit comments

Comments
 (0)