1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Query utility
6 ;;; --------------------------------------------------------------------------
8 ;;; (C) 2010 Philippe Brochard <hocwp@free.fr>
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
* nil
)
35 (defparameter *query-message
* nil
)
36 (defparameter *query-string
* nil
)
37 (defparameter *query-pos
* nil
)
38 (defparameter *query-return
* nil
)
42 (defun query-show-paren (orig-string pos
)
43 "Replace matching parentheses with brackets"
44 (let ((string (copy-seq orig-string
)))
45 (labels ((have-to-find-right?
()
46 (and (< pos
(length string
)) (char= (aref string pos
) #\
()))
47 (have-to-find-left?
()
48 (and (> (1- pos
) 0) (char= (aref string
(1- pos
)) #\
))))
50 (loop :for p
:from
(1+ pos
) :below
(length string
)
51 :with level
= 1 :for c
= (aref string p
)
52 :do
(when (char= c
#\
() (incf level
))
53 (when (char= c
#\
)) (decf level
))
54 (when (= level
0) (return p
))))
56 (loop :for p
:from
(- pos
2) :downto
0
57 :with level
= 1 :for c
= (aref string p
)
58 :do
(when (char= c
#\
() (decf level
))
59 (when (char= c
#\
)) (incf level
))
60 (when (= level
0) (return p
))))
61 (draw-bloc (p &optional
(color *query-parent-color
*))
62 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color color
))
63 (xlib:draw-rectangle
*pixmap-buffer
* *query-gc
*
64 (+ 10 (* p
(xlib:max-char-width
*query-font
*)))
65 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*) 7)
66 (xlib:max-char-width
*query-font
*)
67 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))
69 (cond ((have-to-find-left?
) (let ((p (pos-left)))
71 (progn (draw-bloc p
) (draw-bloc (1- pos
)))
72 (draw-bloc (1- pos
) *query-parent-error-color
*))))
73 ((have-to-find-right?
) (let ((p (pos-right)))
75 (progn (draw-bloc p
) (draw-bloc pos
))
76 (draw-bloc pos
*query-parent-error-color
*))))))))
79 (defun clear-query-history ()
80 "Clear the query-string history"
81 (setf *query-history
* nil
))
85 (defun leave-query-mode (&optional
(return :Escape
))
86 "Leave the query mode"
87 (setf *query-return
* return
)
88 (throw 'exit-query-loop nil
))
91 (defun leave-query-mode-valid ()
92 (leave-query-mode :Return
))
94 (defun leave-query-mode-complet ()
95 (leave-query-mode :Complet
))
97 (add-hook *binding-hook
* 'init-
*query-keys
*)
100 (defun query-print-string ()
101 (clear-pixmap-buffer *query-window
* *query-gc
*)
102 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-message-color
*))
103 (xlib:draw-glyphs
*pixmap-buffer
* *query-gc
* 5 (+ (xlib:max-char-ascent
*query-font
*) 5) *query-message
*)
104 (when (< *query-pos
* 0)
105 (setf *query-pos
* 0))
106 (when (> *query-pos
* (length *query-string
*))
107 (setf *query-pos
* (length *query-string
*)))
108 (query-show-paren *query-string
* *query-pos
*)
109 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-foreground
*))
110 (xlib:draw-glyphs
*pixmap-buffer
* *query-gc
*
112 (+ (* 2 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 5)
114 (setf (xlib:gcontext-foreground
*query-gc
*) (get-color *query-cursor-color
*))
115 (xlib:draw-line
*pixmap-buffer
* *query-gc
*
116 (+ 10 (* *query-pos
* (xlib:max-char-width
*query-font
*)))
117 (+ (* 2 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 6)
118 (+ 10 (* *query-pos
* (xlib:max-char-width
*query-font
*)))
119 (+ (* 1 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*))) 7))
120 (copy-pixmap-buffer *query-window
* *query-gc
*))
124 (defun query-enter-function ()
125 (setf *query-font
* (xlib:open-font
*display
* *query-font-string
*))
126 (let ((width (- (xlib:screen-width
*screen
*) 2))
127 (height (* 3 (+ (xlib:max-char-ascent
*query-font
*) (xlib:max-char-descent
*query-font
*)))))
128 (with-placement (*query-mode-placement
* x y width height
)
129 (setf *query-window
* (xlib:create-window
:parent
*root
*
133 :background
(get-color *query-background
*)
135 :border
(get-color *query-border
*)
136 :colormap
(xlib:screen-default-colormap
*screen
*)
137 :event-mask
'(:exposure
:key-press
))
138 *query-gc
* (xlib:create-gcontext
:drawable
*query-window
*
139 :foreground
(get-color *query-foreground
*)
140 :background
(get-color *query-background
*)
143 (map-window *query-window
*)
145 (wait-no-key-or-button-press))))
149 (defun query-leave-function ()
150 (xlib:destroy-window
*query-window
*)
151 (xlib:close-font
*query-font
*)
152 (wait-no-key-or-button-press))
154 (defun query-loop-function ()
155 (raise-window *query-window
*))
159 (labels ((generic-backspace (del-pos)
161 (setf *query-string
* (concatenate 'string
162 (subseq *query-string
* 0 del-pos
)
163 (subseq *query-string
* *query-pos
*))
164 *query-pos
* del-pos
))))
165 (defun query-backspace ()
166 "Delete a character backward"
167 (generic-backspace (1- *query-pos
*)))
169 (defun query-backspace-word ()
170 "Delete a word backward"
171 (generic-backspace (or (position #\Space
*query-string
* :from-end t
:end
*query-pos
*) 0))))
174 (labels ((generic-delete (del-pos)
175 (when (<= del-pos
(length *query-string
*))
176 (setf *query-string
* (concatenate 'string
177 (subseq *query-string
* 0 *query-pos
*)
178 (subseq *query-string
* del-pos
))))))
179 (defun query-delete ()
180 "Delete a character forward"
181 (generic-delete (1+ *query-pos
*)))
183 (defun query-delete-word ()
184 "Delete a word forward"
185 (generic-delete (1+ (or (position #\Space
*query-string
* :start
*query-pos
*)
186 (1- (length *query-string
*)))))))
191 "Move cursor to line begining"
192 (setf *query-pos
* 0))
195 "Move cursor to line end"
196 (setf *query-pos
* (length *query-string
*)))
200 "Move cursor to left"
201 (when (> *query-pos
* 0)
202 (setf *query-pos
* (1- *query-pos
*))))
204 (defun query-left-word ()
205 "Move cursor to left word"
206 (when (> *query-pos
* 0)
207 (setf *query-pos
* (let ((p (position #\Space
*query-string
*
208 :end
(min (1- *query-pos
*) (length *query-string
*))
212 (defun query-right ()
213 "Move cursor to right"
214 (when (< *query-pos
* (length *query-string
*))
215 (setf *query-pos
* (1+ *query-pos
*))))
217 (defun query-right-word ()
218 "Move cursor to right word"
219 (when (< *query-pos
* (length *query-string
*))
220 (setf *query-pos
* (let ((p (position #\Space
*query-string
*
221 :start
(min (1+ *query-pos
*) (length *query-string
*)))))
222 (if p p
(length *query-string
*))))))
224 (defun query-previous-history ()
225 "Circulate backward in history"
226 (setf *query-string
* (first *query-history
*)
227 *query-pos
* (length *query-string
*)
228 *query-history
* (rotate-list *query-history
*)))
231 (defun query-next-history ()
232 "Circulate forward in history"
233 (setf *query-string
* (first *query-history
*)
234 *query-pos
* (length *query-string
*)
235 *query-history
* (anti-rotate-list *query-history
*)))
239 (defun query-delete-eof ()
240 "Delete the end of the line"
241 (setf *query-string
* (subseq *query-string
* 0 *query-pos
*)))
244 (add-hook *binding-hook
* 'set-default-query-keys
)
246 (defun set-default-query-keys ()
247 (define-query-key ("Return") 'leave-query-mode-valid
)
248 (define-query-key ("Escape") 'leave-query-mode
)
249 (define-query-key ("g" :control
) 'leave-query-mode
)
250 (define-query-key ("Tab") 'leave-query-mode-complet
)
251 (define-query-key ("BackSpace") 'query-backspace
)
252 (define-query-key ("BackSpace" :control
) 'query-backspace-word
)
253 (define-query-key ("Delete") 'query-delete
)
254 (define-query-key ("Delete" :control
) 'query-delete-word
)
255 (define-query-key ("Home") 'query-home
)
256 (define-query-key ("End") 'query-end
)
257 (define-query-key ("Left") 'query-left
)
258 (define-query-key ("Left" :control
) 'query-left-word
)
259 (define-query-key ("Right") 'query-right
)
260 (define-query-key ("Right" :control
) 'query-right-word
)
261 (define-query-key ("Up") 'query-previous-history
)
262 (define-query-key ("Down") 'query-next-history
)
263 (define-query-key ("k" :control
) 'query-delete-eof
))
267 (defun add-in-query-string (code state
)
268 (let* ((modifiers (state->modifiers state
))
269 (keysym (keycode->keysym code modifiers
))
270 (char (xlib:keysym-
>character
*display
* keysym state
)))
271 (when (and char
(characterp char
))
272 (setf *query-string
* (concatenate 'string
273 (when (<= *query-pos
* (length *query-string
*))
274 (subseq *query-string
* 0 *query-pos
*))
276 (when (< *query-pos
* (length *query-string
*))
277 (subseq *query-string
* *query-pos
*))))
278 (incf *query-pos
*))))
282 (define-handler query-mode
:key-press
(code state
)
283 (unless (funcall-key-from-code *query-keys
* code state
)
284 (add-in-query-string code state
))
285 (query-print-string))
289 (defun query-string (message &optional
(default ""))
290 "Query a string from the keyboard. Display msg as prompt"
291 (let ((grab-keyboard-p (xgrab-keyboard-p))
292 (grab-pointer-p (xgrab-pointer-p)))
293 (setf *query-message
* message
294 *query-string
* default
295 *query-pos
* (length default
))
296 (xgrab-pointer *root
* 92 93)
297 (unless grab-keyboard-p
299 (xgrab-keyboard *root
*))
300 (generic-mode 'query-mode
'exit-query-loop
301 :enter-function
#'query-enter-function
302 :loop-function
#'query-loop-function
303 :leave-function
#'query-leave-function
304 :original-mode
'(main-mode))
305 (unless grab-keyboard-p
309 (xgrab-pointer *root
* 66 67)
311 (when (member *query-return
* '(:Return
:Complet
))
312 (pushnew default
*query-history
* :test
#'equal
)
313 (push *query-string
* *query-history
*))
314 (values *query-string
*
319 (defun query-number (msg &optional
(default 0))
320 "Query a number from the query input"
321 (parse-integer (or (query-string msg
(format nil
"~A" default
)) "") :junk-allowed t
))