-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorgqda-transient.el
243 lines (198 loc) · 8.8 KB
/
orgqda-transient.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
;;; orgqda-transient.el --- Transients for invoking orgqda commands -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2022 Anders Johansson
;; Author: Anders Johansson <[email protected]>
;; Version: 0.2
;; Created: 2021-04-12
;; Modified: 2022-02-23
;; Package-Requires: ((orgqda "0.5") (transient "0.3.0"))
;; Keywords: convenience, wp
;; URL: https://www.gitlab.com/andersjohansson/orgqda
;; 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:
;; Define useful transients for orgqda. Bind ‘orgqda-transient’ to a
;; suitable key in ‘orgqda-mode-map’. With my swedish keyboard I use:
;; (bind-key "C-c C-ö" #'orgqda-transient orgqda-mode-map)
;;; Code:
(require 'orgqda)
(require 'transient)
(require 'validate)
;;;; Starting point
(transient-define-prefix orgqda-transient ()
"Transient for invoking orgqda commands."
[["Listing options"
("-m" orgqda-transient-only-count-matching)
("-i" orgqda-transient-use-tag-inheritance)
("-x" orgqda-transient-exclude-tags)]
["List tags"
:pad-keys t
("-s" orgqda-transient-sort)
("-e" orgqda-transient-full)
("-f" orgqda-transient-notagfiles)
("-p" orgqda-transient-startprefix)
("l" "List tags" orgqda-transient-list-tags-suffix)]]
[["Taglist buffer"
:if (lambda () (or orgqda-list-mode orgqda-codebook-mode))
("g" "Revert list (update buffer)" orgqda-revert-taglist)
;; ("o" orgqda-transient-sort)
("-n" "Non-recursive" orgqda-transient-buffer-non-recursive)
("s" "Sort taglist at point or region" orgqda-transient-sort-taglist)
("S" "Sort taglists in whole buffer" orgqda-transient-sort-taglist-buffer)]
["Collect"
("c" "Collect tagged" orgqda-collect-tagged)]
["Toggle"
("m" "Toggle mode" orgqda-mode)]]
[["Relations"
:pad-keys t
("r" "Tag relations" orgqda-transient-tag-relations)
("-k" orgqda-transient-tag-relations-k)]
["Csv"
("v" "Collect csv" orgqda-collect-tagged-csv)
("V" "Collect csv save" orgqda-collect-tagged-csv-save)
("B" "Save all tag collections as csv" orgqda-collect-tagged-csv-save-all)]
["Tag Actions"
("m" "Refile and merge" orgqda-refile-and-merge-tags)
("n" "Rename tag" orgqda-rename-tag)
("p" "Prefix tag" orgqda-prefix-tag)
("P" "Rename prefix on this tag" orgqda-rename-prefix-on-one-tag)
("i" "Rename prefix on all tags" orgqda-rename-prefix)
("k" "Delete tag" orgqda-delete-tag)]])
(defvar-local orgqda-transient--options nil)
;;;; Listing and options
(defclass orgqda-transient-option (transient-infix)
((argument :initform "")
(var :initform 'orgqda-transient--options :allocation :class)
(param :initarg :param)
(format :initform " %k %d %v"))
"Class for storing orgqda options in local lisp variables.
Uses slots var and param for determining variable and plist key
to store in.")
(defclass orgqda-transient-switch (orgqda-transient-option)
())
(cl-defmethod transient-init-value ((obj orgqda-transient-option))
(oset obj value
(plist-get (symbol-value (oref obj var)) (oref obj param))))
(cl-defmethod transient-format-value ((obj orgqda-transient-option))
(propertize (pcase (oref obj value)
;; (plist-get (oref obj var) (oref obj param))
((pred (eq t)) "Yes")
((pred (eq nil)) "")
((and (pred symbolp) symb)
(symbol-name symb))
((and (pred stringp) str)
str)
((and (pred numberp) num)
(number-to-string num))
(_ ""))
'face 'transient-argument))
(defclass orgqda-transient-taglist-parameter (orgqda-transient-option)
((var :initform 'orgqda--taglist-parameters)))
(defclass orgqda-transient-taglist-parameter-switch (orgqda-transient-taglist-parameter orgqda-transient-switch)
())
(cl-defmethod transient-infix-read ((obj orgqda-transient-switch))
"Toggle the switch on or off."
(if (oref obj value) nil t))
(cl-defmethod transient-infix-set ((obj orgqda-transient-option) val)
"Set orgqda list option defined by OBJ to VAL."
(oset obj value val)
(setf (plist-get (symbol-value (oref obj var)) (oref obj param)) val))
(transient-define-infix orgqda-transient-full ()
:class 'orgqda-transient-taglist-parameter-switch
:param :full
:description "Include extracts")
(transient-define-infix orgqda-transient-notagfiles ()
:class 'orgqda-transient-taglist-parameter-switch
:param :no-tag-files
:description "Ignore ‘orgqda-tag-files’")
(transient-define-infix orgqda-transient-sort ()
:class 'orgqda-transient-taglist-parameter
:param :sort
:always-read t
:description "Sort"
:reader (lambda (_p ii hist)
(intern (completing-read
"Sort: "
(mapcar #'car orgqda-sort-parameters)
nil t ii hist))))
(transient-define-infix orgqda-transient-startprefix ()
:class 'orgqda-transient-taglist-parameter
:param :startprefix
:description "Start prefix"
:reader (lambda (_p ii hist)
(concat (completing-read
"Start prefix: "
(orgqda--get-prefixes-for-completion)
nil nil ii hist)
orgqda-hierarchy-delimiter)))
(transient-define-suffix orgqda-transient-list-tags-suffix ()
:description "Run orgqda-list-tags with given options."
(interactive)
(apply #'orgqda-list-tags nil orgqda--taglist-parameters))
;;;; Tag relations
(transient-define-suffix orgqda-transient-tag-relations (k)
:description "Tag relations"
(interactive (list (plist-get orgqda-transient--options :tag-relations-k)))
(orgqda-view-tag-relations (or k 2)))
(transient-define-infix orgqda-transient-tag-relations-k ()
:description "K-tuples"
:class 'orgqda-transient-option
:param :tag-relations-k
:reader (lambda (_p ii hist)
(string-to-number (transient-read-number-N+ "k-tuple: " ii hist))))
;;;; taglist-buffer sorting
(transient-define-infix orgqda-transient-buffer-non-recursive ()
:description "Non-recursive"
:class 'orgqda-transient-switch
:param :buffer-non-recursive)
(transient-define-suffix orgqda-transient-sort-taglist-buffer (sort)
:description "orgqda-sort-taglist-buffer"
(interactive (list (plist-get orgqda--taglist-parameters :sort)))
(orgqda-sort-taglist-buffer sort))
(transient-define-suffix orgqda-transient-sort-taglist (sort non-recursive)
:description "orgqda-sort-taglist"
(interactive (list (plist-get orgqda--taglist-parameters :sort)
(plist-get orgqda-transient--options :buffer-non-recursive)))
(orgqda-sort-taglist sort non-recursive))
;;; variables
(defclass orgqda-transient-lisp-variable (transient-lisp-variable)
((set-value :initarg :set-value :initform #'orgqda-transient--variable-set-local)
(reader :initform #'orgqda-transient--variable-reader)))
(defun orgqda-transient--variable-reader (prompt initial-input _history)
(validate (read--expression prompt initial-input)))
(defun orgqda-transient--variable-set-local (var val)
(set-variable
(make-local-variable var)
(validate-value val (custom-variable-type var))))
(cl-defmethod transient-format-value ((obj orgqda-transient-lisp-variable))
(concat (when (with-current-buffer transient--original-buffer
(local-variable-p (oref obj variable)))
(propertize "local: " 'face 'shadow))
(propertize (prin1-to-string (oref obj value))
'face 'transient-value)))
(cl-defmethod transient-infix-set ((obj orgqda-transient-lisp-variable) value)
(funcall (oref obj set-value)
(oref obj variable)
(oset obj value value)))
(cl-defmethod transient-format-value ((obj orgqda-transient-lisp-variable))
(let ((print-length 3))
(propertize (prin1-to-string (oref obj value))
'face 'transient-value)))
(transient-define-infix orgqda-transient-only-count-matching ()
:class 'orgqda-transient-lisp-variable
:variable 'orgqda-only-count-matching)
(transient-define-infix orgqda-transient-use-tag-inheritance ()
:class 'orgqda-transient-lisp-variable
:variable 'orgqda-use-tag-inheritance)
(transient-define-infix orgqda-transient-exclude-tags ()
:class 'orgqda-transient-lisp-variable
:variable 'orgqda-exclude-tags)
(provide 'orgqda-transient)
;;; orgqda-transient.el ends here