-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstumpwm-ido.lisp
329 lines (280 loc) · 11.5 KB
/
stumpwm-ido.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
;;;; stumpwm-ido.lisp
(in-package #:stumpwm-ido)
(declaim (optimize (speed 0) (debug 3)))
(defvar *completions-set* nil)
(defvar *ido-match-function* *ido-flex-matcher*)
(defvar *ido-max-inline-completions* 30
"Maximum number of completions displayed in the input window. This
should be set reasonably low as it impacts the speed of the completion
formatting function.")
(pstr:defface :default
:family "terminus"
:pixel-size 12
:foreground "white"
:slant "r")
(pstr:defface :cursor
:background "magenta"
:foreground "black")
(pstr:defface :title1
:pixel-size 24
:foreground "red")
(pstr:defface :title2
:pixel-size 20
:inherit '(:title1))
(pstr:defface :title3
:pixel-size 16)
(pstr:defface :italic
:slant "i")
(pstr:defface :oblique
:slant "o")
(pstr:defface :emph
:inherit '(:italic))
(pstr:defface ido-matches-separator
:documentation "Face for symbols used when displaying completions matches."
:foreground "red")
(pstr:defface ido-matches
:documentation "Face used by ido for displaying normal matches."
:foreground "Gray80")
(pstr:defface ido-exact-match
:documentation "Face used by ido for highlighting an exact match."
:foreground "green")
(pstr:defface ido-first-match
:documentation "Face used by ido for highlighting the first match in the list."
:foreground "cyan"
:weight "bold")
(pstr:defface ido-matches-alt-1
:documentation "Face used by ido for highlighting matches having :match-type
property set to 1."
:foreground "blue")
(pstr:defface ido-matches-alt-2
:documentation "Face used by ido for highlighting matches having :match-type
property set to 2."
:foreground "violet")
(pstr:defface :ido-error
:documentation "Face used by ido for highlighting inline error messages."
:foreground "magenta"
:inherit '(:italic))
(pstr:defface :input-prompt
:foreground "green"
:weight "bold")
;;; FIXME: Completions should not be calculated every time, but only when input changes
;;; TODO: Make it possible to catch changes in the input text
;;; This can probably be done checking what the last command was, and update the
;;; completions only in that case. The current completion list should be stored
;;; somewhere.
;;; Symbols internal in STUMPWM used:
;;; screen-font
;;; screen-input-window
;;; screen-message-gc
;;; screen-width
;;; make-input-string
;;; input-line-string
;;; input-line-position
;;; input-line-password
;;; input-find-completions FIXME
;;; input-insert-string
;;; is-modifier
;;; *input-completions*
;;; read-key-or-selections
;;; code-state->key
;;; *input-last-command*
;;; *input-map*
;;; *input-history*
;;; shutdown-input-window
(defun ido-setup-input-window (screen prompt input)
"Set the window input up to read input."
(let ((height (+ (xlib:font-descent (stumpwm::screen-font screen)) ;FIXME: calculate height for input (pstring)
(xlib:font-descent (stumpwm::screen-font screen))))
(win (stumpwm::screen-input-window screen)))
;; window dimensions
(xlib:with-state (win)
(setf (xlib:window-priority win) :above
(xlib:drawable-height win) height))
;; Draw the prompt
(xlib:map-window win)
(ido-draw-input-bucket screen prompt input)))
(defun propertize-match (match)
(let ((face (case (pstr:pstring-get-property match :match-type 0)
(nil :ido-matches)
(1 :ido-matches-alt-1)
(2 :ido-matches-alt-2))))
(pstr:pstring-propertize match :face face)))
(defun format-matches (completions)
(cond ((null completions)
(pstr:pstring-concat (pstr:pstring-propertize "[" :face :ido-matches-separator)
(pstr:pstring-propertize "No match" :face :ido-error)
(pstr:pstring-propertize "]" :face :ido-matches-separator)))
(t (flet ((csymbol (text)
(pstr:pstring-propertize text :face :ido-matches-separator))
(add-sep (match)
(list (propertize-match match)
(pstr:pstring-propertize " | " :face :ido-matches-separator))))
;; separated completions
(let* ((scmps-1 (butlast (mapcan #'add-sep completions)))
;; first-match
(scmps (cons (pstr:pstring-propertize (first scmps-1) :face :ido-first-match)
(cdr scmps-1))))
(reduce #'pstr:pstring-concat
(nconc (list (csymbol "{ "))
scmps
(list (csymbol " }")))))))))
(defun ido-inline-completions (prompt input completions screen)
(let ((gcontext (stumpwm::screen-message-gc screen))
(maxwidth (- (stumpwm::screen-width screen) (* 2 stumpwm:*message-window-padding*)))
(n (length completions)))
(flet ((make-display-string (comps)
(pstr:pstring-concat prompt (stumpwm::input-line-string input) " " (format-matches comps) " ... ")))
(do ((comps (reverse (subseq completions 0 (min *ido-max-inline-completions* n))) (cdr comps)))
((or (null comps) (< (pstr:xlib-pstring-extents gcontext (make-display-string comps)) maxwidth))
(reverse (append (and (> n (length comps)) (list "...")) comps)))))))
(defparameter *ido-current-completions* nil)
(defparameter *ido-input-map*
(stumpwm::copy-kmap stumpwm::*input-map*))
(defun ido-process-input (input code state)
"Process the key (code and state) given the current input
buffer. Returns a new modified input buffer."
(labels ((process-key (code state)
"Call the appropriate function based on the key
pressed. Return 'done when the user has signalled the finish of his
input (pressing Return), nil otherwise."
(let* ((key (stumpwm::code-state->key code state))
(command (and key (stumpwm::lookup-key *ido-input-map* key t))))
(if command
(prog1
(funcall command input key)
(setf stumpwm::*input-last-command* command))
:error))))
(case (process-key code state)
(:done
(unless (or (stumpwm::input-line-password input)
(and stumpwm:*input-history-ignore-duplicates*
(string= (stumpwm::input-line-string input) (first stumpwm::*input-history*))))
(push (stumpwm::input-line-string input) stumpwm::*input-history*))
:done)
(:abort (throw :abort t))
(:error
;; FIXME draw inverted text
:error)
(t nil))))
(defun process-key-or-selection (key input)
(cond ((stringp key)
(stumpwm::input-insert-string input key))
((stumpwm::is-modifier (car key)))
(t (case (ido-process-input input (car key) (cdr key))
(:error :error)
(:done :done)
(t nil)))))
(defun ido-read-one-line (screen prompt &key (initial-input "") require-match password)
"Read a line of input through stumpwm and return it. Returns nil if
the user aborted."
(let ((stumpwm::*input-last-command* nil)
(input (stumpwm::make-input-line :string (stumpwm::make-input-string initial-input)
:position (length initial-input)
:history -1
:password password)))
(labels ((match-input ()
(let* ((in (string-trim " " (stumpwm::input-line-string input)))
(compls (stumpwm::input-find-completions in stumpwm::*input-completions*)))
(and (consp compls)
(string= in (car compls)))))
(input-changed (old-input-string old-input-position input-line)
(declare (ignorable old-input-position))
(not (string= old-input-string (stumpwm::input-line-string input-line))))
(key-loop ()
(let ((old-input initial-input)
(old-input-pos (length initial-input)))
(loop for key = (stumpwm::read-key-or-selection) do
(let ((status (process-key-or-selection key input)))
;; handle end of input
(when (and (eq status :done)
(or (not require-match) (match-input)))
(return (stumpwm::input-line-string input)))
;; handle input change
(when (input-changed old-input old-input-pos input)
(setf old-input (copy-sequence 'string (stumpwm::input-line-string input)))
(setf old-input-pos (stumpwm::input-line-position input))
(setf *ido-current-completions* (ido-input-completions old-input
old-input-pos
*completions-set*
*ido-match-function*)))
;; draw
(ido-draw-input-bucket screen prompt input
(format-matches (ido-inline-completions prompt
input
*ido-current-completions*
screen))
(eq status :error)))))))
(ido-setup-input-window screen prompt input)
(catch :abort
(unwind-protect
(stumpwm::with-focus (stumpwm::screen-input-window screen)
(key-loop))
(stumpwm::shutdown-input-window screen))))))
(defun color-cursor (text point)
(let ((face (pstr:copy-face
(or (pstr:lookup-face
(pstr:pstring-get-property text :face point))
(pstr:lookup-face :default)))))
(setf face (pstr::merge-faces (pstr:lookup-face :cursor) face))
(pstr:pstring-put-property text point (1+ point) :face face)))
(defun ido-draw-input-bucket (screen prompt input &optional (tail "") errorp)
(declare (ignorable errorp))
(let* ((text-to-print (pstr:pstring-concat prompt
(color-cursor (stumpwm::input-line-string input)
(stumpwm::input-line-position input))
" "
tail))
(gcontext (stumpwm::screen-message-gc screen))
(win (stumpwm::screen-input-window screen)))
(multiple-value-bind (w h a)
(pstr:xlib-pstring-extents gcontext text-to-print)
(let* ((width (+ w (* 2 stumpwm:*message-window-padding*)))
(height (+ h (* 1 stumpwm:*message-window-padding*))))
(setf (xlib:drawable-width win) width
(xlib:drawable-height win) height)
(stumpwm::setup-win-gravity screen win stumpwm:*input-window-gravity*)
(pstr:xlib-draw-pstring text-to-print
win
gcontext
stumpwm:*message-window-padding*
(+ a stumpwm:*message-window-padding*)
t)))
(when errorp
(stumpwm::invert-rect screen win 0 0 (xlib:drawable-width win) (xlib:drawable-height win))
(xlib:display-force-output stumpwm::*display*)
(sleep 0.05)
(stumpwm::invert-rect screen win 0 0 (xlib:drawable-width win) (xlib:drawable-height win)))
))
(stumpwm:defcommand prova () ()
(print (ido-read-one-line (stumpwm:current-screen)
(pstr:pstring-propertize "Cmd: " :face :input-prompt))))
(stumpwm:defcommand prova3 () ()
(let ((*completions-set* *command-completions*))
(ido-read-one-line (stumpwm::current-screen)
(pstr:pstring-propertize "CMD: " :face :input-prompt))))
(stumpwm:defcommand prova2() ()
(let ((*completions-set* *pathname-completions*))
(print (ido-read-one-line (stumpwm:current-screen)
(pstr:pstring-propertize "Cmd: " :face :input-prompt)))))
(stumpwm:defcommand ido-rotate-fwd (input key) ()
(setf *ido-current-completions* (rotate *ido-current-completions* -1)))
(stumpwm:defcommand ido-rotate-bwd (input key) ()
(setf *ido-current-completions* (rotate *ido-current-completions*)))
(stumpwm:define-key *ido-input-map* (stumpwm:kbd "C-s") 'ido-rotate-fwd)
(stumpwm:define-key *ido-input-map* (stumpwm:kbd "C-r") 'ido-rotate-bwd)
(defun ido-complete-input (input key)
(declare (ignore key))
(setf (stumpwm::input-line-string input) (stumpwm::make-input-string (pstr:pstring-string (car *ido-current-completions*)))
(stumpwm::input-line-position input) (pstr:pstring-length (car *ido-current-completions*))))
(stumpwm:define-key *ido-input-map* (stumpwm:kbd "C-Return") 'ido-complete-input)
(defun ido-expand-input (input key)
(declare (ignore key))
(setf (stumpwm::input-line-string input)
(stumpwm::make-input-string
(ido-cset-expand-input (stumpwm::input-line-string input)
(stumpwm::input-line-position input)
*completions-set*
*ido-match-function*)))
(setf (stumpwm::input-line-position input)
(length (stumpwm::input-line-string input))))
(stumpwm:define-key *ido-input-map* (stumpwm:kbd "Tab") 'ido-expand-input)