a178b799f085b14d23f165a26a16f536614f529f
[clfswm.git] / src / clfswm-query.lisp
bloba178b799f085b14d23f165a26a16f536614f529f
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Query utility
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; --------------------------------------------------------------------------
26 (in-package :clfswm)
29 (defparameter *query-window* nil)
30 (defparameter *query-font* nil)
31 (defparameter *query-gc* nil)
33 (defparameter *query-history* (list ""))
34 (defparameter *query-complet-list* nil)
35 (defparameter *query-completion-state* nil)
37 (defparameter *query-message* nil)
38 (defparameter *query-string* nil)
39 (defparameter *query-pos* nil)
40 (defparameter *query-return* nil)
43 (defun query-show-paren (orig-string pos dec)
44 "Replace matching parentheses with brackets"
45 (let ((string (copy-seq orig-string)))
46 (labels ((have-to-find-right? ()
47 (and (< pos (length string)) (char= (aref string pos) #\()))
48 (have-to-find-left? ()
49 (and (> (1- pos) 0) (char= (aref string (1- pos)) #\))))
50 (pos-right ()
51 (loop :for p :from (1+ pos) :below (length string)
52 :with level = 1 :for c = (aref string p)
53 :do (when (char= c #\() (incf level))
54 (when (char= c #\)) (decf level))
55 (when (= level 0) (return p))))
56 (pos-left ()
57 (loop :for p :from (- pos 2) :downto 0
58 :with level = 1 :for c = (aref string p)
59 :do (when (char= c #\() (decf level))
60 (when (char= c #\)) (incf level))
61 (when (= level 0) (return p))))
62 (draw-bloc (p &optional (color *query-parent-color*))
63 (setf (xlib:gcontext-foreground *query-gc*) (get-color color))
64 (xlib:draw-rectangle *pixmap-buffer* *query-gc*
65 (+ 10 (* p (xlib:max-char-width *query-font*)) dec)
66 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*) 7)
67 (xlib:max-char-width *query-font*)
68 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*))
69 t)))
70 (cond ((have-to-find-left?) (let ((p (pos-left)))
71 (if p
72 (progn (draw-bloc p) (draw-bloc (1- pos)))
73 (draw-bloc (1- pos) *query-parent-error-color*))))
74 ((have-to-find-right?) (let ((p (pos-right)))
75 (if p
76 (progn (draw-bloc p) (draw-bloc pos))
77 (draw-bloc pos *query-parent-error-color*))))))))
80 (defun clear-query-history ()
81 "Clear the query-string history"
82 (setf *query-history* (list "")))
86 (defun leave-query-mode (&optional (return :Escape))
87 "Leave the query mode"
88 (setf *query-return* return)
89 (throw 'exit-query-loop nil))
92 (defun leave-query-mode-valid ()
93 (leave-query-mode :Return))
95 (add-hook *binding-hook* 'init-*query-keys*)
99 (defun query-find-complet-list ()
100 (let* ((pos (1+ (or (position-if-not #'extented-alphanumericp *query-string*
101 :end *query-pos* :from-end t)
102 -1)))
103 (str (subseq *query-string* pos *query-pos*)))
104 (when (or (> (length str) (1- *query-min-complet-char*))
105 (< (length *query-complet-list*) *query-max-complet-length*))
106 (values (string-match str *query-complet-list*) pos))))
109 (defun query-print-string ()
110 (let ((dec (min 0 (- (- (x-drawable-width *query-window*) 10)
111 (+ 10 (* *query-pos* (xlib:max-char-width *query-font*))))))
112 (complet (if *query-completion-state*
113 (first *query-completion-state*)
114 (query-find-complet-list))))
115 (clear-pixmap-buffer *query-window* *query-gc*)
116 (setf (xlib:gcontext-foreground *query-gc*) (get-color *query-message-color*))
117 (xlib:draw-glyphs *pixmap-buffer* *query-gc* 5 (+ (xlib:max-char-ascent *query-font*) 5)
118 (format nil "~A ~{~A~^, ~}" *query-message*
119 (if (< (length complet) *query-max-complet-length*)
120 complet nil)))
121 (when (< *query-pos* 0)
122 (setf *query-pos* 0))
123 (when (> *query-pos* (length *query-string*))
124 (setf *query-pos* (length *query-string*)))
125 (query-show-paren *query-string* *query-pos* dec)
126 (setf (xlib:gcontext-foreground *query-gc*) (get-color *query-foreground*))
127 (xlib:draw-glyphs *pixmap-buffer* *query-gc*
128 (+ 10 dec)
129 (+ (* 2 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*))) 5)
130 *query-string*)
131 (setf (xlib:gcontext-foreground *query-gc*) (get-color *query-cursor-color*))
132 (xlib:draw-line *pixmap-buffer* *query-gc*
133 (+ 10 (* *query-pos* (xlib:max-char-width *query-font*)) dec)
134 (+ (* 2 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*))) 6)
135 (+ 10 (* *query-pos* (xlib:max-char-width *query-font*)) dec)
136 (+ (* 1 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*))) 7))
137 (copy-pixmap-buffer *query-window* *query-gc*)))
141 (defun query-enter-function ()
142 (setf *query-font* (xlib:open-font *display* *query-font-string*))
143 (let ((width (- (xlib:screen-width *screen*) 2))
144 (height (* 3 (+ (xlib:max-char-ascent *query-font*) (xlib:max-char-descent *query-font*)))))
145 (with-placement (*query-mode-placement* x y width height)
146 (setf *query-window* (xlib:create-window :parent *root*
147 :x x :y y
148 :width width
149 :height height
150 :background (get-color *query-background*)
151 :border-width *border-size*
152 :border (get-color *query-border*)
153 :colormap (xlib:screen-default-colormap *screen*)
154 :event-mask '(:exposure :key-press))
155 *query-gc* (xlib:create-gcontext :drawable *query-window*
156 :foreground (get-color *query-foreground*)
157 :background (get-color *query-background*)
158 :font *query-font*
159 :line-style :solid))
160 (setf (window-transparency *query-window*) *query-transparency*)
161 (map-window *query-window*)
162 (query-print-string)
163 (wait-no-key-or-button-press))))
167 (defun query-leave-function ()
168 (xlib:destroy-window *query-window*)
169 (xlib:close-font *query-font*)
170 (wait-no-key-or-button-press))
172 (defun query-loop-function ()
173 (raise-window *query-window*))
177 (labels ((generic-backspace (del-pos)
178 (when (>= del-pos 0)
179 (setf *query-string* (concatenate 'string
180 (subseq *query-string* 0 del-pos)
181 (subseq *query-string* *query-pos*))
182 *query-pos* del-pos))))
183 (defun query-backspace ()
184 "Delete a character backward"
185 (generic-backspace (1- *query-pos*)))
187 (defun query-backspace-word ()
188 "Delete a word backward"
189 (generic-backspace (or (position #\Space *query-string* :from-end t :end *query-pos*) 0)))
191 (defun query-backspace-clear ()
192 "Delete backwards until beginning"
193 (generic-backspace 0)))
195 (labels ((generic-delete (del-pos)
196 (when (<= del-pos (length *query-string*))
197 (setf *query-string* (concatenate 'string
198 (subseq *query-string* 0 *query-pos*)
199 (subseq *query-string* del-pos))))))
200 (defun query-delete ()
201 "Delete a character forward"
202 (generic-delete (1+ *query-pos*)))
204 (defun query-delete-word ()
205 "Delete a word forward"
206 (generic-delete (1+ (or (position #\Space *query-string* :start *query-pos*)
207 (1- (length *query-string*)))))))
211 (defun query-home ()
212 "Move cursor to line begining"
213 (setf *query-pos* 0))
215 (defun query-end ()
216 "Move cursor to line end"
217 (setf *query-pos* (length *query-string*)))
220 (defun query-left ()
221 "Move cursor to left"
222 (when (> *query-pos* 0)
223 (setf *query-pos* (1- *query-pos*))))
225 (defun query-left-word ()
226 "Move cursor to left word"
227 (when (> *query-pos* 0)
228 (setf *query-pos* (let ((p (position #\Space *query-string*
229 :end (min (1- *query-pos*) (length *query-string*))
230 :from-end t)))
231 (if p p 0)))))
233 (defun query-right ()
234 "Move cursor to right"
235 (when (< *query-pos* (length *query-string*))
236 (setf *query-pos* (1+ *query-pos*))))
238 (defun query-right-word ()
239 "Move cursor to right word"
240 (when (< *query-pos* (length *query-string*))
241 (setf *query-pos* (let ((p (position #\Space *query-string*
242 :start (min (1+ *query-pos*) (length *query-string*)))))
243 (if p p (length *query-string*))))))
245 (defun query-previous-history ()
246 "Circulate backward in history"
247 (setf *query-string* (first *query-history*)
248 *query-pos* (length *query-string*)
249 *query-history* (rotate-list *query-history*)))
252 (defun query-next-history ()
253 "Circulate forward in history"
254 (setf *query-string* (first *query-history*)
255 *query-pos* (length *query-string*)
256 *query-history* (anti-rotate-list *query-history*)))
260 (defun query-delete-eof ()
261 "Delete the end of the line"
262 (setf *query-string* (subseq *query-string* 0 *query-pos*)))
265 (defun query-mode-complet ()
266 (multiple-value-bind (complet pos)
267 (query-find-complet-list)
268 (when complet
269 (if (= (length complet) 1)
270 (setf *query-string* (concatenate 'string
271 (subseq *query-string* 0 pos)
272 (first complet) " "
273 (subseq *query-string* *query-pos*))
274 *query-pos* (+ pos (length (first complet)) 1))
275 (let ((common (find-common-string (subseq *query-string* pos *query-pos*) complet)))
276 (when common
277 (setf *query-string* (concatenate 'string
278 (subseq *query-string* 0 pos)
279 common
280 (subseq *query-string* *query-pos*))
281 *query-pos* (+ pos (length common)))))))))
283 (defun query-mode-complete-suggest ()
284 (flet ((complete (completions completion-pos pos initial-pos)
285 (when completions
286 (let ((completion (if (equal completion-pos (list-length completions))
287 (subseq *query-string* pos initial-pos)
288 (nth completion-pos completions))))
289 (setf *query-string* (concatenate 'string
290 (subseq *query-string* 0 pos)
291 completion
292 (subseq *query-string* *query-pos*))
293 *query-pos* (+ pos (length completion))))
294 (setf *query-completion-state*
295 (list completions completion-pos pos initial-pos)))))
296 (if *query-completion-state*
297 (complete (first *query-completion-state*)
298 (mod (1+ (second *query-completion-state*))
299 (1+ (list-length (first *query-completion-state*))))
300 (third *query-completion-state*)
301 (fourth *query-completion-state*))
302 (multiple-value-bind (comps pos) (query-find-complet-list)
303 (complete comps 0 pos *query-pos*)))))
305 (add-hook *query-key-press-hook* 'query-mode-complete-suggest-reset)
307 (defun query-mode-complete-suggest-reset (code state)
308 "Reset the query-completion-state if another key was pressed than a key
309 that calls query-mode-complete-suggest."
310 (unless (equal 'query-mode-complete-suggest
311 (first (find-key-from-code *query-keys* code state)))
312 (setf *query-completion-state* nil)
313 (query-print-string)))
315 (add-hook *binding-hook* 'set-default-query-keys)
317 (defun set-default-query-keys ()
318 (define-query-key ("Return") 'leave-query-mode-valid)
319 (define-query-key ("Escape") 'leave-query-mode)
320 (define-query-key ("g" :control) 'leave-query-mode)
321 (define-query-key ("Tab") 'query-mode-complet)
322 (define-query-key ("BackSpace") 'query-backspace)
323 (define-query-key ("BackSpace" :control) 'query-backspace-word)
324 (define-query-key ("BackSpace" :control :shift) 'query-backspace-clear)
325 (define-query-key ("u" :control) 'query-backspace-clear)
326 (define-query-key ("Delete") 'query-delete)
327 (define-query-key ("Delete" :control) 'query-delete-word)
328 (define-query-key ("Home") 'query-home)
329 (define-query-key ("a" :control) 'query-home)
330 (define-query-key ("End") 'query-end)
331 (define-query-key ("e" :control) 'query-end)
332 (define-query-key ("Left") 'query-left)
333 (define-query-key ("Left" :control) 'query-left-word)
334 (define-query-key ("Right") 'query-right)
335 (define-query-key ("Right" :control) 'query-right-word)
336 (define-query-key ("Up") 'query-previous-history)
337 (define-query-key ("Down") 'query-next-history)
338 (define-query-key ("k" :control) 'query-delete-eof))
342 (defun add-in-query-string (code state)
343 (let* ((modifiers (state->modifiers state))
344 (keysym (keycode->keysym code modifiers))
345 (char (xlib:keysym->character *display* keysym state)))
346 (when (and char (characterp char))
347 (setf *query-string* (concatenate 'string
348 (when (<= *query-pos* (length *query-string*))
349 (subseq *query-string* 0 *query-pos*))
350 (string char)
351 (when (< *query-pos* (length *query-string*))
352 (subseq *query-string* *query-pos*))))
353 (incf *query-pos*))))
357 (define-handler query-mode :key-press (code state)
358 (unless (funcall-key-from-code *query-keys* code state)
359 (add-in-query-string code state))
360 (query-print-string)
361 (call-hook *query-key-press-hook* code state))
363 (define-handler query-mode :button-press (code state x y)
364 (call-hook *query-button-press-hook* code state x y))
368 (defun query-string (message &optional (default "") complet-list)
369 "Query a string from the keyboard. Display msg as prompt"
370 (setf *query-message* message
371 *query-string* default
372 *query-pos* (length default)
373 *query-complet-list* complet-list
374 *query-completion-state* nil)
375 (with-grab-keyboard-and-pointer (92 93 66 67 t)
376 (generic-mode 'query-mode 'exit-query-loop
377 :enter-function #'query-enter-function
378 :loop-function #'query-loop-function
379 :leave-function #'query-leave-function
380 :original-mode '(main-mode)))
381 (when (equal *query-return* :Return)
382 (pushnew default *query-history* :test #'equal)
383 (push *query-string* *query-history*))
384 (values *query-string*
385 *query-return*))
389 (defun query-number (msg &optional (default 0))
390 "Query a number from the query input"
391 (multiple-value-bind (string return)
392 (query-string msg (format nil "~A" default))
393 (values (if (equal return :Return)
394 (or (parse-integer (or string "") :junk-allowed t) default)
395 default)
396 return)))