-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-package-tags.el
294 lines (254 loc) · 9.98 KB
/
use-package-tags.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; use-package-tags.el --- Group packages using tags -*- lexical-binding: t -*-
;; Copyright (C) 2020 Akira Komamura
;; Author: Akira Komamura <[email protected]>
;; Version: 0.1
;; Package-Requires: ((emacs "25.1") (dash "2.12"))
;; Keywords: lisp maint tools
;; URL: https://github.com/akirak/use-package-tags
;; This file is not part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This library adds :tags keyword to use-package, which can be used
;; to enable only packages that belong to certain groups.
;; You can group packages using the keyword, and select packages
;; to enable by setting `use-package-tags-current-profile' variable.
;;
;; It also provides a function for enumerating packages in a
;; file/buffer based on tags. This can be used to install packages
;; for your init file without relying on package.el.
;;; Code:
(require 'cl-lib)
(require 'dash)
(defgroup use-package-tags
nil
"Group packages using tags."
:group 'use-package)
(declare-function epkg-provided-by "ext:epkg")
(declare-function epkg-builtin-package-p "ext:epkg")
(declare-function epkg "ext:epkg")
(declare-function use-package-process-keywords "ext:use-package")
(defvar use-package-keywords)
;;;; use-package :tags keyword
(defcustom use-package-tags-current-profile nil
"List of tags used to select packages to activate."
:type '(repeat symbol))
(when (require 'use-package nil t)
(fset 'use-package-handler/:tags 'use-package-tags-handler)
(fset 'use-package-normalize/:tags 'use-package-normalize-test)
(fset 'use-package-autoloads/:tags 'use-package-autoloads-mode)
(add-to-list 'use-package-keywords :tags))
(defun use-package-tags-handler (name _keyword tags rest state)
"Handler for :tags keyword in use-package.
NAME, _KEYWORD, TAGS, REST, and STATE are arguments that follow
the conventions of use-package."
(let ((body (use-package-process-keywords name rest state)))
(if tags
`((when (cl-intersection ,tags use-package-tags-current-profile)
,@body))
body)))
;;;; Extract package names from an init file
(defcustom use-package-tags-init-files
(list (or user-init-file
(expand-file-name "init.el" user-emacs-directory)))
"List of files to look for package declarations."
:type '(repeat file))
(defun use-package-tags--source-buffer-list (source)
"Return a list of buffers for SOURCE.
If the argument is nil, it returns a list containing the current
buffer.
Alternatively, the argument can be one of the following:
* t for `use-package-tags-init-files'.
* File name.
* Buffer.
* List of files.
* List of buffers."
(cl-labels
((to-buffer
(item)
(cl-etypecase item
(buffer item)
;; (symbol (to-buffer (eval symbol)))
(file-exists (or (find-buffer-visiting item)
(find-file-noselect item)))
(string (error "File does not exist: %s" item)))))
(cl-typecase source
(null (list (current-buffer)))
(symbol (if (eq t source)
(mapcar #'to-buffer use-package-tags-init-files)
;; TODO: Return the variable value when a symbol is given
;; (list (to-buffer source))
(error "Symbol is not accepted: %s" source)))
(list (mapcar #'to-buffer source))
(otherwise (list (to-buffer source))))))
(defsubst use-package-tags--normalize-query (query)
"Normalize QUERY into a list or t."
(cl-etypecase query
(listp query)
(symbolp (or (eq t query)
(list query)))))
(defmacro use-package-tags--with-package-forms (buffers &rest progn)
"In BUFFERS, evaluate PROGN at every `use-package' form."
(declare (indent 1))
`(dolist (buf ,buffers)
(with-current-buffer buf
(save-excursion
(goto-char (point-min))
(while (re-search-forward (rx "(use-package" space) nil t)
(beginning-of-defun-raw)
,@progn
(end-of-defun))))))
;;;###autoload
(cl-defun use-package-tags-select (query &key from installable
(as 'symbols))
"Get a list of packages declared in `use-package' forms.
This function returns a list of package names declared in a source
based on tags in QUERY.
The query should be a list of tags. It selects packages that have
at least one tag in the query or have no tags. Alternatively, you
can select all packages declared in the source by specifying t as
the query.
FROM specifies the source.
See `use-package-tags--source-buffer-list'.
When INSTALLABLE is set to non-nil, it returns a list of packages
available on the Emacs-Mirror. You will need epkg.el for this feature.
When AS is given, it converts the result to a certain type.
It accepts the following values (the default: symbols):
* symbols: a list of symbols.
* strings: a list of strings.
* lines: a single string joined by newlines."
(declare (indent 1))
(let (alist
(query (use-package-tags--normalize-query query)))
(cl-labels
((get-keyword (prop rest) (-some->> (member prop rest)
(nth 1))))
(use-package-tags--with-package-forms
(use-package-tags--source-buffer-list from)
(let* ((exp (read (current-buffer)))
(name (nth 1 exp))
(disabled (get-keyword :disabled exp))
;; TODO: Handle dependencies
;; (after (get-keyword :after))
;; (requires (get-keyword :requires))
;; TODO: Add support for :when and :unless
(if-expr (get-keyword :if exp))
(tags (get-keyword :tags exp)))
(when (and (not disabled)
(not (and if-expr
(not (eval if-expr))))
;; TODO: :requires keyword
;; (or (not requires)
;; (-all-p (lambda (feature)
;; (assoc feature alist))
;; (cl-etypecase requires
;; (list requires)
;; (symbol (list requires)))))
(or (eq t query)
(not tags)
(cl-intersection tags query)))
(push (list name)
;; TODO: Handle dependencies
;; (list name
;; :after after
;; :requires requires)
alist)))))
(cl-labels
((enabled-p
;; The dependency handle is work in progress.
;; It must also support :requires keyword based on :after,
;; but I won't work on it for now.
;;
;; (package)
;; (let* ((cell (assoc package alist))
;; (plist (cdr cell))
;; (after (plist-get plist :after)))
;; (and cell
;; (or (not after)
;; (test-after after))))
(_package)
t)
(test-after
(selector)
(pcase selector
((pred symbolp) (enabled-p selector))
(`(:all . ,conds) (-all-p #'test-after conds))
(`(:any . ,conds) (-any-p #'test-after conds))
((pred listp) (-all-p #'test-after selector))))
(map-result
(packages)
(if installable
(use-package-tags-map-installable packages)
packages))
(convert
(packages)
(cl-ecase as
(symbols (-map #'to-symbol packages))
(strings (-map #'to-string packages))
(lines (mapconcat #'to-string packages "\n"))))
(to-string
(name)
(cl-etypecase name
(symbol (symbol-name name))
(string name)))
(to-symbol
(name)
(cl-etypecase name
(symbol name)
(string (intern name)))))
(->> (nreverse (-map #'car alist))
(-filter #'enabled-p)
(map-result)
(convert)))))
(defun use-package-tags-map-installable (packages)
"Get a list of packages to be installed to activate PACKAGES."
(require 'epkg)
(cl-labels
((query (pkg)
(let* ((name (symbol-name pkg))
(obj (epkg name)))
(cond
((and obj (epkg-builtin-package-p obj))
nil)
(obj
name)
(t
(epkg-provided-by pkg))))))
(->> packages
(-map #'query)
(delq nil)
(-uniq))))
;;;###autoload
(cl-defun use-package-tags-collect-tags (source &key sort)
"Collect package tags from a source.
For SOURCE, see `use-package-tags--source-buffer-list'.
If SORT is non-nil, the result will be lexicographically sorted."
(cl-labels
((get-keyword (prop rest) (-some->> (member prop rest)
(nth 1)))
(order (items) (if sort
(cl-sort items #'string< :key #'symbol-name)
items)))
(let (result)
(use-package-tags--with-package-forms
(use-package-tags--source-buffer-list source)
(let* ((exp (read (current-buffer)))
(tags (get-keyword :tags exp)))
(when tags
(push tags result))))
(->> (-flatten-n 1 result)
(cl-remove-duplicates)
(order)))))
(provide 'use-package-tags)
;;; use-package-tags.el ends here