-
Notifications
You must be signed in to change notification settings - Fork 4
/
minibuffer.lisp
351 lines (298 loc) · 12.8 KB
/
minibuffer.lisp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(in-package #:neomacs)
(sera:export-always
'(minibuffer-input-element minibuffer-input update-completion-buffer
prompt read-from-minibuffer read-yes-or-no read-password
completing-read make-completion-buffer complete-minibuffer-aux))
(define-mode minibuffer-mode ()
((prompt :initarg :prompt)
(initial :initform nil :initarg :initial)
(completed :initform nil)
(history-var :initform nil :initarg :history)
(history-index :initform -1)
(saved-input :initform nil)))
(define-keys minibuffer-mode
"enter" 'exit-minibuffer
"C-g" 'exit-recursive-edit
"M-p" 'minibuffer-previous-history
"M-n" 'minibuffer-next-history)
(defmethod selectable-p-aux ((buffer minibuffer-mode) pos)
(and (pos-up-until pos (alex:rcurry #'class-p "input"))
(call-next-method)))
(defmethod window-decoration-aux ((buffer minibuffer-mode))
(dom `(:div :class "minibuffer" :selectable ""
(:div :class "main content" :buffer ,(id buffer)))))
(define-command exit-minibuffer
:mode minibuffer-mode ()
(when-let (var (history-var (current-buffer)))
(push (child-nodes (minibuffer-input-element (current-buffer)))
(symbol-value var)))
(setf (completed (current-buffer)) t))
(defun minibuffer-input-element (buffer)
(only-elt (get-elements-by-class-name (document-root buffer) "input")))
(define-command minibuffer-previous-history
:mode minibuffer-mode ()
(let* ((buffer (current-buffer))
(index (1+ (history-index buffer)))
(history (symbol-value (or (history-var buffer)
(error 'not-supported :operation 'history
:buffer buffer))))
(input (minibuffer-input-element buffer)))
(if (< index (length history))
(progn
(when (= (history-index buffer) -1)
(setf (saved-input buffer)
(child-nodes input)))
(setf (history-index buffer) index)
(delete-nodes (pos-down input) nil)
(apply #'insert-nodes (end-pos input)
(mapcar #'clone-node (nth index history))))
(user-error "No previous history"))))
(define-command minibuffer-next-history
:mode minibuffer-mode ()
(let* ((buffer (current-buffer))
(index (1- (history-index buffer)))
(history (symbol-value (or (history-var buffer)
(error 'not-supported :operation 'history
:buffer buffer))))
(input (minibuffer-input-element buffer)))
(if (>= index -1)
(progn
(setf (history-index buffer) index)
(delete-nodes (pos-down input) nil)
(apply #'insert-nodes (end-pos input)
(if (= index -1)
(saved-input buffer)
(mapcar #'clone-node (nth index history)))))
(user-error "No next history"))))
(defgeneric minibuffer-input (buffer)
(:method ((buffer minibuffer-mode))
(text-content (minibuffer-input-element buffer)))
(:documentation "Input text from BUFFER.
This is used for completion and should always return a string. To
return objects from `read-from-minibuffer', add methods to
`minibuffer-result'."))
(defgeneric minibuffer-result (buffer)
(:method ((buffer minibuffer-mode))
(minibuffer-input buffer))
(:documentation "Result to be returned by `read-from-minibuffer'."))
(defmethod revert-buffer-aux ((buffer minibuffer-mode))
(let ((input (dom `(:span :class "input"
,@ (when (initial buffer)
(list (initial buffer)))))))
(setf (attribute (document-root (current-buffer))
'read-only)
t)
(insert-nodes (end-pos (document-root (current-buffer)))
(lret ((node (dom `(:span :class "prompt"
,(prompt buffer)))))
(setf (attribute node 'read-only) t))
input)
(setf (pos (focus)) (end-pos input))))
(defun read-from-minibuffer (prompt &rest args)
"Read and return a string from minibuffer with PROMPT.
ARGS are passed to `make-buffer' to create the minibuffer."
(unless (getf args :mode)
(setf (getf args :mode) 'minibuffer-mode))
(let ((minibuf
(apply #'make-buffer "*minibuffer*"
:prompt prompt args))
(saved-focus (focused-buffer)))
(split-window-below minibuf)
(focus-buffer minibuf)
(unwind-protect
(with-current-buffer minibuf
(revert-buffer)
(recursive-edit (lambda () (not (completed minibuf))))
(minibuffer-result minibuf))
(focus-buffer saved-focus)
(close-window minibuf)
(delete-buffer minibuf))))
(defun read-yes-or-no (prompt)
(let ((answer (read-from-minibuffer
(str:concat prompt "(yes or no) "))))
(cond ((member answer '("y" "yes") :test 'equal)
t)
((member answer '("n" "no") :test 'equal)
nil)
(t (read-yes-or-no prompt)))))
(define-mode minibuffer-password-mode (minibuffer-mode) ())
(defsheet minibuffer-password-mode `((".input" :-webkit-text-security "disc"
:-webkit-appearance "textfield")
(".focus-tail::after" :content "")))
(defun read-password (prompt)
(read-from-minibuffer prompt :mode 'minibuffer-password-mode))
(defun completing-read (prompt list-mode &rest args)
"Read and return a presentation from minibuffer with completion.
This is a wrapper around `read-from-minibuffer' that creates a completion buffer in LIST-MODE. The LIST-MODE should attach a presentation for each row, which will be returned by `completing-read'."
(read-from-minibuffer
prompt
:mode 'minibuffer-completion-mode
:completion-buffer
(apply #'make-completion-buffer
(list list-mode 'completion-buffer-mode)
args)))
(define-mode completion-mode ()
((completion-buffer :initarg :completion-buffer))
(:documentation
"Abstract mode for buffers that has a completion buffer."))
(define-mode minibuffer-completion-mode
(completion-mode minibuffer-mode) ()
(:documentation
"Mode for minibuffer that supports completion."))
(define-keys minibuffer-completion-mode
"tab" 'complete-minibuffer
'exit-minibuffer 'complete-exit-minibuffer
'next-line 'next-completion
'previous-line 'previous-completion
'scroll-down-command 'scroll-down-completion
'scroll-up-command 'scroll-up-completion
'beginning-of-buffer 'first-completion
'end-of-buffer 'last-completion)
(defgeneric update-completion-buffer (buffer)
(:method ((buffer minibuffer-completion-mode))
(setf (occur-query (completion-buffer buffer))
(minibuffer-input buffer))))
(defgeneric complete-minibuffer-aux (buffer)
(:method ((buffer minibuffer-completion-mode))
(let ((input (only-elt (get-elements-by-class-name
(document-root buffer) "input")))
(selection (node-after (focus (completion-buffer buffer)))))
(when (selectable-p selection)
(unless (class-p selection "dummy-row")
(delete-nodes (pos-down input) nil)
(insert-nodes (pos-down input)
(text-content (first-child selection))))))))
(defmethod on-post-command progn ((buffer minibuffer-completion-mode))
(update-completion-buffer buffer))
(defmethod minibuffer-result ((buffer minibuffer-completion-mode))
(if (require-match (completion-buffer buffer))
(let ((node (node-after (focus (completion-buffer buffer)))))
(when (selectable-p node)
(attribute node 'presentation)))
(minibuffer-input buffer)))
(define-mode completion-buffer-mode (list-mode)
((rows :initform nil)
(occur-query :initform "")
(completion-limit :default 300)
(require-match
:initform t :initarg :require-match
:documentation
"Whether a match is required.
If this slot is NIL, an invisible selectable dummy row is inserted at
the beginning of the completion buffer. No completion is performed
when this row is selected.")))
(defmethod generate-rows :around ((buffer completion-buffer-mode))
(if (require-match buffer) (call-next-method)
(cons (make-element "tr" :class "dummy-row")
(call-next-method))))
(defmethod revert-buffer-aux ((buffer completion-buffer-mode))
(erase-buffer)
(let ((body-node (make-element "tbody")))
(insert-nodes (end-pos (document-root buffer))
(make-element "table" :children (list body-node)))
(setf (rows buffer) (generate-rows buffer))
(iter (for row in (rows buffer))
(for _ below (completion-limit buffer))
(insert-nodes (end-pos body-node) row))
(setf (pos (focus))
(or (npos-right-ensure
(pos-down body-node)
(lambda (p) (not (class-p p "dummy-row"))))
(error 'top-of-subtree)))))
(defmethod occur-p-aux :around ((buffer completion-buffer-mode)
(query t) element)
(if (class-p element "dummy-row") t (call-next-method)))
(defmethod (setf occur-query) :around (new-val (buffer completion-buffer-mode))
(let ((old-val (slot-value buffer 'occur-query)))
(prog1 (call-next-method)
(unless (equal old-val new-val)
(with-current-buffer buffer
(let* ((*inhibit-read-only* t)
(body-node (last-child (first-child (document-root buffer))))
(pos (pos-down body-node)))
(clear-match-highlight buffer)
(iter (with i = 0)
(for row in (rows buffer))
(for matches = (occur-p-aux buffer new-val row))
(when matches
(if (host row)
(progn
(delete-nodes pos row)
(setf pos (pos-right row)))
(insert-nodes pos row))
(render-match-highlight buffer matches)
(incf i))
(while (< i (completion-limit buffer))))
(delete-nodes pos nil)))))))
(defsheet completion-buffer-mode
`(("dummy-row" :display "none")
("::highlight(occur)" :inherit match)))
(defmethod selectable-p-aux ((buffer completion-buffer-mode) pos)
(tag-name-p (node-after pos) "tr"))
(defmethod on-mouse-click progn ((buffer completion-buffer-mode)
(x t) (y t))
(setf (adjust-marker-direction buffer) 'backward))
(defmethod window-decoration-aux ((buffer minibuffer-completion-mode))
(dom `(:div :class "buffer" :selectable ""
(:div :class "main content" :style "flex:0 0 2em;"
:buffer ,(id buffer))
(:div :class "content"
:buffer ,(id (completion-buffer buffer))))))
(defmethod on-delete-buffer progn ((buffer minibuffer-completion-mode))
(delete-buffer (completion-buffer buffer)))
(define-command complete-minibuffer
:mode minibuffer-completion-mode ()
(complete-minibuffer-aux (current-buffer)))
(define-command complete-exit-minibuffer
:mode minibuffer-completion-mode ()
(complete-minibuffer)
(exit-minibuffer))
(define-command next-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(forward-element)))
(define-command previous-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(backward-element)))
(define-command scroll-down-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(dotimes (_ (scroll-lines (current-buffer)))
(forward-element))))
(define-command scroll-up-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(dotimes (_ (scroll-lines (current-buffer)))
(backward-element))))
(define-command first-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(beginning-of-buffer)))
(define-command last-completion
:mode completion-mode ()
(with-current-buffer (completion-buffer (current-buffer))
(end-of-buffer)))
(defun make-completion-buffer (modes &rest args)
(apply #'make-buffer " *completion*" :mode modes :revert t
args))
(defun collect-keybindings (keymaps)
(lret (keys (table (make-hash-table)))
(iter (for keymap in keymaps)
(traverse-keymap
keymap (lambda (kseq cmd)
(declare (ignore cmd))
(push kseq keys))))
(iter (for k in (delete-duplicates keys :test 'equal))
(push k (gethash (lookup-keybind k keymaps) table)))))
(define-command execute-command ()
(call-interactively
(completing-read
"M-x " 'command-list-mode
:include-modes (modes (current-buffer))
:keybinding-index
(collect-keybindings (keymaps (current-buffer))))))
(define-keys :global
"M-x" 'execute-command)
(defstyle minibuffer-prompt `(:inherit bold))
(defsheet minibuffer `((".prompt" :inherit minibuffer-prompt)))