-
Notifications
You must be signed in to change notification settings - Fork 2
/
enlight-menu.el
205 lines (169 loc) · 6.26 KB
/
enlight-menu.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
;;; enlight.el --- Vertical menus for `enlight'. -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Ilya Chernyshov
;;; 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:
;; Vertical menus for `enlight'.
;; (enlight-menu
;; '(("Org Mode"
;; ("Org-Agenda (current day)" (org-agenda nil "a") "a"))
;; ("Other"
;; ("Projects" project-switch-project "p"))))
;;; Code:
(require 'subr-x)
(require 'seq)
(require 'text-property-search)
(defvar enlight-menu-count 0)
(defgroup enlight-menu nil
"Vertical menus for `enlight'."
:group 'applications
:prefix "enlight-menu-")
(defcustom enlight-menu-right-margin 5
"Right margin applied after the items column, in number of characters."
:type 'integer)
(defface enlight-menu-selected-face
'((t (:underline t)))
"Face used for currently selected item.")
(defface enlight-menu-section
'((t (:inherit font-lock-keyword-face)))
"Face used for sections.")
(defface enlight-menu-key
'((t (:inherit font-lock-constant-face)))
"Face used for key strings.")
(defun enlight--normalize-command (command)
"Normalize COMMAND."
`(lambda (&optional button)
(interactive)
,(if (commandp command)
`(call-interactively ',command)
command)))
(defvar enlight-mode-map)
(defun enlight-menu--apply-keys (alist)
"Set keys in `enlight-mode-map' from ALIST."
(mapc
(lambda (section)
(mapc
(pcase-lambda (`(,_ ,command ,shortcut))
(when shortcut
(let ((command (enlight--normalize-command command)))
(keymap-set enlight-mode-map shortcut command))))
(cdr section)))
alist)
(keymap-set enlight-mode-map "<remap> <next-line>" 'enlight-menu-forward-item)
(keymap-set enlight-mode-map "<remap> <right-char>" 'enlight-forward-menu)
(keymap-set enlight-mode-map "<remap> <forward-char>" 'enlight-forward-menu)
(keymap-set enlight-mode-map "<remap> <previous-line>" 'enlight-menu-backward-item)
(keymap-set enlight-mode-map "<remap> <backward-char>" 'enlight-backward-menu)
(keymap-set enlight-mode-map "<remap> <left-char>" 'enlight-backward-menu))
(defun enlight-menu--max-item-length (alist)
"Calculate max length of item-names in ALIST."
(thread-last alist
(seq-mapcat #'cdr)
(seq-map (lambda (x) (length (car x))))
(seq-max)))
;; Copied from `s-center' from s.el
(defun enlight-center-string (len s)
"If S is shorter than LEN, pad it with spaces so it is centered."
(let ((extra (max 0 (- len (length s)))))
(concat
(make-string (ceiling extra 2) ?\s)
s
(make-string (floor extra 2) ?\s))))
(defun enlight-menu--insert-section (menu-id width section)
"Insert SECTION using WIDTH in the current buffer to the menu with MENU-ID."
(pcase-let ((`(,section-name . ,items) section))
(insert
(propertize (enlight-center-string width section-name)
'face 'enlight-menu-section)
"\n")
(mapc (apply-partially #'enlight-menu--insert-item menu-id width) items)))
(defun enlight-menu--insert-item (menu-id width item)
"Insert ITEM using WIDTH in the current buffer to the menu with MENU-ID."
(pcase-let ((`(,desc ,command ,shortcut) item))
(insert-text-button
desc
'menu-id menu-id
'face 'default
'action (enlight--normalize-command command)
'help-echo nil
'cursor-face 'enlight-menu-selected-face
'mouse-face 'enlight-menu-selected-face)
(when shortcut
(insert
(make-string (- width (length desc)) ? )
(propertize shortcut 'face 'enlight-menu-key)))
(insert "\n")))
(defun enlight-menu-first-button ()
"Jumpt to first button in the current buffer."
(goto-char (point-min))
(forward-button 1 nil nil t))
;;;###autoload
(defun enlight-menu (alist)
"Generate a vertical menu using ALIST and return as a string.
The form of ALIST:
((\"Section-1\" ITEM ITEM ...)
(\"Section-2\" ITEM ITEM ...)
...)
Where ITEM is of the form:
(\"Item text\" SYMBOL-OR-FORM [KEY])
SYMBOL-OR-FORM is a form or a function symbol. If it's a
function symbol, the function will be called when a user presses
RET or clicks on that item.
KEY is a string acceptable for `keymap-set'. If it is specified,
the SYMBOL-OR-FORM is bound to this key in the dashboard buffer
keymap."
(let ((width (+ (enlight-menu--max-item-length alist)
enlight-menu-right-margin)))
(add-hook 'enlight-after-insert-hook #'enlight-menu-first-button)
(enlight-menu--apply-keys alist)
(with-temp-buffer
(mapc (apply-partially #'enlight-menu--insert-section
(cl-incf enlight-menu-count) width)
alist)
(buffer-string))))
(defun enlight-forward-menu ()
"Go to next menu in the buffer."
(interactive)
(if (text-property-search-forward
'menu-id (get-text-property (point) 'menu-id)
(lambda (val prop) (and prop (not (eq val prop)))))
(backward-button 1)
(goto-char (point-min))
(enlight-forward-menu)))
(defun enlight-backward-menu ()
"Go to previous menu in the buffer."
(interactive)
(unless (text-property-search-backward
'menu-id (get-text-property (point) 'menu-id)
(lambda (val prop) (and prop (not (eq val prop)))))
(goto-char (point-max))
(enlight-backward-menu)))
(defun enlight-menu-forward-item ()
"Go to next item in the current menu."
(interactive)
(let ((cur-menu-id (get-text-property (point) 'menu-id)))
(while
(progn
(forward-button 1 t)
(/= (get-text-property (point) 'menu-id)
cur-menu-id)))))
(defun enlight-menu-backward-item ()
"Go to previous item in the current menu."
(interactive)
(let ((cur-menu-id (get-text-property (point) 'menu-id)))
(while
(progn
(backward-button 1 t)
(/= (get-text-property (point) 'menu-id)
cur-menu-id)))))
(provide 'enlight-menu)
;;; enlight-menu.el ends here