1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Query utility
6 ;;; --------------------------------------------------------------------------
8 ;;; (C) 2005-2013 Philippe Brochard <pbrochard@common-lisp.net>
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.
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.
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.
24 ;;; --------------------------------------------------------------------------
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 add-char-in-query-string (char)
44 (setf *query-string
* (concatenate 'string
45 (when (<= *query-pos
* (length *query-string
*))
46 (subseq *query-string
* 0 *query-pos
*))
48 (when (< *query-pos
* (length *query-string
*))
49 (subseq *query-string
* *query-pos
*))))
53 (defun query-show-paren (orig-string pos dec
)
54 "Replace matching parentheses with brackets"
55 (let ((string (copy-seq orig-string
)))
56 (labels ((have-to-find-right?
()
57 (and (< pos
(length string
)) (char= (aref string pos
) #\
()))
58 (have-to-find-left?
()
59 (and (> (1- pos
) 0) (char= (aref string
(1- pos
)) #\
))))
61 (loop :for p
:from
(1+ pos
) :below
(length string
)
62 :with level
= 1 :for c
= (aref string p
)
63 :do
(when (char= c
#\
() (incf level
))
64 (when (char= c
#\
)) (decf level
))
65 (when (= level
0) (return p
))))
67 (loop :for p
:from
(- pos
2) :downto
0
68 :with level
= 1 :for c
= (aref string p
)
69 :do
(when (char= c
#\
() (decf level
))
70 (when (char= c
#\
)) (incf level
))
71 (when (= level
0) (return p
))))
72 (draw-bloc (p &optional
(color *query-parent-color
*))
73 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color color
))
74 (xlib:draw-rectangle
*pixmap-buffer
* *query-gc
*
75 (+ 10 (* p
(xlib:max-char-width
*query-font
*)) dec
)
76 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*) 7)
77 (xlib:max-char-width
*query-font
*)
78 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))
80 (cond ((have-to-find-left?
) (let ((p (pos-left)))
82 (progn (draw-bloc p
) (draw-bloc (1- pos
)))
83 (draw-bloc (1- pos
) *query-parent-error-color
*))))
84 ((have-to-find-right?
) (let ((p (pos-right)))
86 (progn (draw-bloc p
) (draw-bloc pos
))
87 (draw-bloc pos
*query-parent-error-color
*))))))))
90 (defun clear-query-history ()
91 "Clear the query-string history"
92 (setf *query-history
* (list "")))
96 (defun leave-query-mode (&optional
(return :Escape
))
97 "Leave the query mode"
98 (setf *query-return
* return
)
99 (throw 'exit-query-loop nil
))
102 (defun leave-query-mode-valid ()
103 (leave-query-mode :Return
))
105 (add-hook *binding-hook
* 'init-
*query-keys
*)
109 (defun query-find-complet-list ()
110 (let* ((pos (1+ (or (position-if-not #'extented-alphanumericp
*query-string
*
111 :end
*query-pos
* :from-end t
)
113 (str (subseq *query-string
* pos
*query-pos
*)))
114 (when (or (> (length str
) (1- *query-min-complet-char
*))
115 (< (length *query-complet-list
*) *query-max-complet-length
*))
116 (values (string-match str
*query-complet-list
*) pos
))))
119 (defun query-print-string ()
120 (let ((dec (min 0 (- (- (x-drawable-width *query-window
*) 10)
121 (+ 10 (* *query-pos
* (xlib:max-char-width
*query-font
*))))))
122 (complet (if *query-completion-state
*
123 (first *query-completion-state
*)
124 (query-find-complet-list))))
125 (clear-pixmap-buffer *query-window
* *query-gc
*)
126 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-message-color
*))
127 (xlib:draw-glyphs
*pixmap-buffer
* *query-gc
* 5 (+ (xlib:max-char-ascent
*query-font
*) 5)
128 (format nil
"~A ~{~A~^, ~}" *query-message
*
129 (if (< (length complet
) *query-max-complet-length
*)
131 (when (< *query-pos
* 0)
132 (setf *query-pos
* 0))
133 (when (> *query-pos
* (length *query-string
*))
134 (setf *query-pos
* (length *query-string
*)))
135 (query-show-paren *query-string
* *query-pos
* dec
)
136 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-foreground
*))
137 (xlib:draw-glyphs
*pixmap-buffer
* *query-gc
*
139 (+ (* 2 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 5)
141 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-cursor-color
*))
142 (xlib:draw-line
*pixmap-buffer
* *query-gc
*
143 (+ 10 (* *query-pos
* (xlib:max-char-width
*query-font
*)) dec
)
144 (+ (* 2 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 6)
145 (+ 10 (* *query-pos
* (xlib:max-char-width
*query-font
*)) dec
)
146 (+ (* 1 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 7))
147 (copy-pixmap-buffer *query-window
* *query-gc
*)))
151 (defun query-enter-function ()
152 (setf *query-font
* (xlib:open-font
*display
* *query-font-string
*))
153 (let ((width (- (xlib:screen-width
*screen
*) 2))
154 (height (* 3 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*)))))
155 (with-placement (*query-mode-placement
* x y width height
)
156 (setf *query-window
* (xlib:create-window
:parent
*root
*
160 :background
(get-color *query-background
*)
161 :border-width
*border-size
*
162 :border
(get-color *query-border
*)
163 :colormap
(xlib:screen-default-colormap
*screen
*)
164 :event-mask
'(:exposure
:key-press
))
165 *query-gc
* (xlib:create-gcontext
:drawable
*query-window
*
166 :foreground
(get-color *query-foreground
*)
167 :background
(get-color *query-background
*)
170 (setf (window-transparency *query-window
*) *query-transparency
*)
171 (map-window *query-window
*)
173 (wait-no-key-or-button-press))))
177 (defun query-leave-function ()
178 (xlib:destroy-window
*query-window
*)
179 (xlib:close-font
*query-font
*)
180 (wait-no-key-or-button-press))
182 (defun query-loop-function ()
183 (raise-window *query-window
*))
187 (labels ((generic-backspace (del-pos)
189 (setf *query-string
* (concatenate 'string
190 (subseq *query-string
* 0 del-pos
)
191 (subseq *query-string
* *query-pos
*))
192 *query-pos
* del-pos
))))
193 (defun query-backspace ()
194 "Delete a character backward"
195 (generic-backspace (1- *query-pos
*)))
197 (defun query-backspace-word ()
198 "Delete a word backward"
199 (generic-backspace (or (position #\Space
*query-string
* :from-end t
:end
*query-pos
*) 0)))
201 (defun query-backspace-clear ()
202 "Delete backwards until beginning"
203 (generic-backspace 0)))
205 (labels ((generic-delete (del-pos)
206 (when (<= del-pos
(length *query-string
*))
207 (setf *query-string
* (concatenate 'string
208 (subseq *query-string
* 0 *query-pos
*)
209 (subseq *query-string
* del-pos
))))))
210 (defun query-delete ()
211 "Delete a character forward"
212 (generic-delete (1+ *query-pos
*)))
214 (defun query-delete-word ()
215 "Delete a word forward"
216 (generic-delete (1+ (or (position #\Space
*query-string
* :start
*query-pos
*)
217 (1- (length *query-string
*)))))))
222 "Move cursor to line begining"
223 (setf *query-pos
* 0))
226 "Move cursor to line end"
227 (setf *query-pos
* (length *query-string
*)))
231 "Move cursor to left"
232 (when (> *query-pos
* 0)
233 (setf *query-pos
* (1- *query-pos
*))))
235 (defun query-left-word ()
236 "Move cursor to left word"
237 (when (> *query-pos
* 0)
238 (setf *query-pos
* (let ((p (position #\Space
*query-string
*
239 :end
(min (1- *query-pos
*) (length *query-string
*))
243 (defun query-right ()
244 "Move cursor to right"
245 (when (< *query-pos
* (length *query-string
*))
246 (setf *query-pos
* (1+ *query-pos
*))))
248 (defun query-right-word ()
249 "Move cursor to right word"
250 (when (< *query-pos
* (length *query-string
*))
251 (setf *query-pos
* (let ((p (position #\Space
*query-string
*
252 :start
(min (1+ *query-pos
*) (length *query-string
*)))))
253 (if p p
(length *query-string
*))))))
255 (defun query-previous-history ()
256 "Circulate backward in history"
257 (setf *query-string
* (first *query-history
*)
258 *query-pos
* (length *query-string
*)
259 *query-history
* (rotate-list *query-history
*)))
262 (defun query-next-history ()
263 "Circulate forward in history"
264 (setf *query-string
* (first *query-history
*)
265 *query-pos
* (length *query-string
*)
266 *query-history
* (anti-rotate-list *query-history
*)))
270 (defun query-delete-eof ()
271 "Delete the end of the line"
272 (setf *query-string
* (subseq *query-string
* 0 *query-pos
*)))
275 (defun query-mode-complet ()
276 (multiple-value-bind (complet pos
)
277 (query-find-complet-list)
279 (if (= (length complet
) 1)
280 (setf *query-string
* (concatenate 'string
281 (subseq *query-string
* 0 pos
)
283 (subseq *query-string
* *query-pos
*))
284 *query-pos
* (+ pos
(length (first complet
)) 1))
285 (let ((common (find-common-string (subseq *query-string
* pos
*query-pos
*) complet
)))
287 (setf *query-string
* (concatenate 'string
288 (subseq *query-string
* 0 pos
)
290 (subseq *query-string
* *query-pos
*))
291 *query-pos
* (+ pos
(length common
)))))))))
293 (defun query-mode-complete-suggest ()
294 (flet ((complete (completions completion-pos pos initial-pos
)
296 (let ((completion (if (equal completion-pos
(list-length completions
))
297 (subseq *query-string
* pos initial-pos
)
298 (nth completion-pos completions
))))
299 (setf *query-string
* (concatenate 'string
300 (subseq *query-string
* 0 pos
)
302 (subseq *query-string
* *query-pos
*))
303 *query-pos
* (+ pos
(length completion
))))
304 (setf *query-completion-state
*
305 (list completions completion-pos pos initial-pos
)))))
306 (if *query-completion-state
*
307 (complete (first *query-completion-state
*)
308 (mod (1+ (second *query-completion-state
*))
309 (1+ (list-length (first *query-completion-state
*))))
310 (third *query-completion-state
*)
311 (fourth *query-completion-state
*))
312 (multiple-value-bind (comps pos
) (query-find-complet-list)
313 (complete comps
0 pos
*query-pos
*)))))
315 (add-hook *query-key-press-hook
* 'query-mode-complete-suggest-reset
)
317 (defun query-mode-complete-suggest-reset (code state
)
318 "Reset the query-completion-state if another key was pressed than a key
319 that calls query-mode-complete-suggest."
320 (unless (equal 'query-mode-complete-suggest
321 (first (find-key-from-code *query-keys
* code state
)))
322 (setf *query-completion-state
* nil
)
323 (query-print-string)))
325 (add-hook *binding-hook
* 'set-default-query-keys
)
327 (defun set-default-query-keys ()
328 (define-query-key ("Return") 'leave-query-mode-valid
)
329 (define-query-key ("Escape") 'leave-query-mode
)
330 (define-query-key ("g" :control
) 'leave-query-mode
)
331 (define-query-key ("Tab") 'query-mode-complet
)
332 (define-query-key ("BackSpace") 'query-backspace
)
333 (define-query-key ("BackSpace" :control
) 'query-backspace-word
)
334 (define-query-key ("BackSpace" :control
:shift
) 'query-backspace-clear
)
335 (define-query-key ("u" :control
) 'query-backspace-clear
)
336 (define-query-key ("Delete") 'query-delete
)
337 (define-query-key ("Delete" :control
) 'query-delete-word
)
338 (define-query-key ("Home") 'query-home
)
339 (define-query-key ("a" :control
) 'query-home
)
340 (define-query-key ("End") 'query-end
)
341 (define-query-key ("e" :control
) 'query-end
)
342 (define-query-key ("Left") 'query-left
)
343 (define-query-key ("Left" :control
) 'query-left-word
)
344 (define-query-key ("Right") 'query-right
)
345 (define-query-key ("Right" :control
) 'query-right-word
)
346 (define-query-key ("Up") 'query-previous-history
)
347 (define-query-key ("Down") 'query-next-history
)
348 (define-query-key ("k" :control
) 'query-delete-eof
)
349 (define-query-key ("KP_Insert" :mod-2
) 'add-char-in-query-string
"0")
350 (define-query-key ("KP_End" :mod-2
) 'add-char-in-query-string
"1")
351 (define-query-key ("KP_Down" :mod-2
) 'add-char-in-query-string
"2")
352 (define-query-key ("KP_Page_Down" :mod-2
) 'add-char-in-query-string
"3")
353 (define-query-key ("KP_Left" :mod-2
) 'add-char-in-query-string
"4")
354 (define-query-key ("KP_Begin" :mod-2
) 'add-char-in-query-string
"5")
355 (define-query-key ("KP_Right" :mod-2
) 'add-char-in-query-string
"6")
356 (define-query-key ("KP_Home" :mod-2
) 'add-char-in-query-string
"7")
357 (define-query-key ("KP_Up" :mod-2
) 'add-char-in-query-string
"8")
358 (define-query-key ("KP_Page_Up" :mod-2
) 'add-char-in-query-string
"9")
359 (define-query-key ("KP_Delete" :mod-2
) 'add-char-in-query-string
".")
360 (define-query-key ("KP_Add" :mod-2
) 'add-char-in-query-string
"+")
361 (define-query-key ("KP_Subtract" :mod-2
) 'add-char-in-query-string
"-")
362 (define-query-key ("KP_Multiply" :mod-2
) 'add-char-in-query-string
"*")
363 (define-query-key ("KP_Divide" :mod-2
) 'add-char-in-query-string
"/")
364 (define-query-key ("KP_Enter" :mod-2
) 'leave-query-mode-valid
))
368 (defun add-in-query-string (code state
)
369 (let* ((modifiers (state->modifiers state
))
370 (keysym (keycode->keysym code modifiers
))
371 (char (xlib:keysym-
>character
*display
* keysym state
)))
372 (when (and char
(characterp char
))
373 (add-char-in-query-string char
))))
377 (define-handler query-mode
:key-press
(code state
)
378 (unless (funcall-key-from-code *query-keys
* code state
)
379 (add-in-query-string code state
))
381 (call-hook *query-key-press-hook
* code state
))
383 (define-handler query-mode
:button-press
(code state x y
)
384 (call-hook *query-button-press-hook
* code state x y
))
388 (defun query-string (message &optional
(default "") complet-list
)
389 "Query a string from the keyboard. Display msg as prompt"
390 (setf *query-message
* message
391 *query-string
* default
392 *query-pos
* (length default
)
393 *query-complet-list
* complet-list
394 *query-completion-state
* nil
)
395 (with-grab-keyboard-and-pointer (92 93 66 67 t
)
396 (generic-mode 'query-mode
'exit-query-loop
397 :enter-function
#'query-enter-function
398 :loop-function
#'query-loop-function
399 :leave-function
#'query-leave-function
400 :original-mode
'(main-mode)))
401 (when (equal *query-return
* :Return
)
402 (pushnew default
*query-history
* :test
#'equal
)
403 (push *query-string
* *query-history
*))
404 (values *query-string
*
409 (defun query-number (msg &optional
(default 0))
410 "Query a number from the query input"
411 (multiple-value-bind (string return
)
412 (query-string msg
(format nil
"~A" default
))
413 (values (if (equal return
:Return
)
414 (or (parse-integer (or string
"") :junk-allowed t
) default
)