This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdelve-transient.el
104 lines (85 loc) · 3.97 KB
/
delve-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
;;; delve-transient.el --- transient keymaps for Delve -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2023
;; Author: <[email protected]>
;;
;; 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 transient keymaps.
;;; Code:
(require 'transient)
(require 'dash)
;; * Generic Utilities for working with Transients
(defun delve-transient--split-switch (s)
"Return S `--val=key' as a list with value-key-pair."
(let* ((re (rx (*? blank)
"--" (group-n 1 (+? (not "=")))
"=" (group-n 2 (* (not blank)))
(*? blank))))
(when (string-match re s)
(list (intern (concat ":" (match-string 1 s)))
(match-string 2 s)))))
(defun delve-transient--args-to-plist (args)
"Return transient ARGS with --val=key pairs as a property list."
(-flatten (-map #'delve-transient--split-switch (-flatten args))))
;; * Define infix delve-transient-switches for switching between options.
(defclass delve-transient-switches (transient-option)
((choices :initarg :choices) ;; return value
(pretty-choices :initarg :pretty-choices) ;; display value
(allow-nil :initarg :allow-nil :initform t) ;; allow unsetting?
(always-read :initform t)
(reader :initform #'delve-transient--toggle-reader))
"Transient switch for mutually exclusive values.
The transient returns the value from `:choices', but presents to
the user the corresponding value (same index) from
`:pretty-choices'. Per default, the user can switch between these
values and then unset the whole list (which will return a nil
value). Set `:allow-nil' to nil to prevent nil values.")
(defun delve-transient--initial-switch-value (obj)
"Return the initial value for switch OBJ.
OBJ must be an instance of `delve-transient-switches'."
(and (not (oref obj allow-nil)) (elt (oref obj choices) 0)))
(cl-defmethod transient-init-value ((obj delve-transient-switches))
"Set initial value for switch OBJ."
(oset obj value (delve-transient--initial-switch-value obj)))
(cl-defmethod transient-format-value ((obj delve-transient-switches))
"Format the value list of OBJ."
(let* ((value (oref obj value))
(choices (oref obj choices))
(n 0))
(mapconcat (lambda (pretty-choice)
(cl-incf n)
(propertize (or pretty-choice "")
'face (if (equal value (elt choices (1- n)))
'transient-value
'transient-inactive-value)))
(oref obj pretty-choices)
"|")))
(defun delve-transient--toggle-reader (&rest _)
"Shift value to the next value.
Move forward to the next element in slot `choices', wrapping
around if the end of the list is reached. If slot `allow-nil' is
non-nil, return nil after the last choice and move to the first
choice after nil."
(let* ((obj (transient-suffix-object))
(value (oref obj value))
(choices (oref obj choices)))
;; move from nil -> first value
(if (not value)
(elt choices 0)
;; move from any value -> next value
(let ((n (1+ (-elem-index value choices))))
(if (< n (length choices))
(elt choices n)
;; end of list? either unset or go back to first value
(delve-transient--initial-switch-value obj))))))
(provide 'delve-transient)
;;; delve-transient.el ends here