-
Notifications
You must be signed in to change notification settings - Fork 0
/
minibuffer.lisp
234 lines (191 loc) · 6.94 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
;;; minibuffer.lisp --- emacs-like minibuffer for blocky
;; Copyright (C) 2012, 2013 David O'Toole
;; Author: David O'Toole <[email protected]>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(in-package :blocky)
;;; Modeline
(defun-memo modeline-position-string (x y)
(:key #'identity :test 'equal :validator #'identity)
(format nil "X:~S Y:~S" x y))
(defun-memo modeline-database-string (local global)
(:key #'identity :test 'equal :validator #'identity)
(format nil "~S/~S objects" local global))
(define-block-macro modeline
(:super phrase
:fields
((orientation :initform :horizontal)
(no-background :initform t)
(spacing :initform 4))
:inputs (:project-id (new 'label :read-only t)
:buffer-id (new 'label :read-only t)
:position (new 'label :read-only t)
:mode (new 'label :read-only t)
:objects (new 'label :read-only t))))
(define-method update modeline ()
(mapc #'pin %inputs)
(set-value %%project-id *project*)
(set-value %%buffer-id (or (%buffer-name (current-buffer)) "nil"))
(set-value %%objects (modeline-database-string (hash-table-count (%objects (current-buffer)))
(hash-table-count *database*)))
(set-value %%position
(modeline-position-string
(%window-x (current-buffer))
(%window-y (current-buffer))))
(set-value %%mode
(if (current-buffer)
(if (%paused (current-buffer))
"(paused)"
"(playing)")
"(empty)")))
;;; Custom data entry for Minibuffer. See also entry.lisp
(define-block (minibuffer-prompt :super entry)
(background :initform nil)
output)
(defun debug-on-error ()
(setf *debug-on-error* t))
(defun print-on-error ()
(setf *debug-on-error* nil))
(define-method set-output minibuffer-prompt (output)
(setf %output output))
(define-method can-pick minibuffer-prompt () nil)
(define-method pick minibuffer-prompt ()
%parent)
(define-method enter minibuffer-prompt (&optional no-clear)
(prompt%enter self))
(define-method evaluate-here minibuffer-prompt ()
(prompt%enter self))
(define-method do-sexp minibuffer-prompt (sexp)
(with-fields (output) self
(assert output)
(let ((container (get-parent output))
(result nil))
(assert (blockyp container))
;; output messages too
(let ((*message-function*
#'(lambda (text)
(format t "~A" text)
(accept container (new 'label :line text)))))
;; execute lisp expressions
(setf result (eval (first sexp)))
;; we didn't crash, at least. output the reusable sexp
(accept container (new 'expression :line %last-line))
;; do something with result
(let ((new-block
(if result
(if (blockyp result)
result
(new 'expression :value result)))))
;; spit out result block, if any
(when new-block
(accept container new-block)))))))
(define-method lose-focus minibuffer-prompt ()
(cancel-editing self))
;; (define-method label-width minibuffer-prompt ()
;; (dash 2 (font-text-width *default-prompt-string* *font*)))
(define-method do-after-evaluate minibuffer-prompt ()
;; print any error output
(when (and %parent (stringp %error-output)
(plusp (length %error-output)))
(accept %parent (new 'text %error-output))))
;;; The Minibuffer is a pop-up command program and Forth prompt. Only
;;; shows one line, like in Emacs.
(define-block (minibuffer :super phrase)
(sidebar :initform nil)
(temporary :initform t)
(display-lines :initform 12))
(defparameter *minimum-minibuffer-width* 200)
(define-method initialize minibuffer ()
(with-fields (image inputs) self
(let ((prompt (new 'minibuffer-prompt))
(modeline (new 'modeline)))
(initialize%super self)
(set-output prompt prompt)
(setf inputs (list modeline prompt))
(set-parent prompt self)
(set-parent modeline self)
(setf %sidebar (new 'sidebar))
(pin prompt)
(pin modeline))))
(define-method layout minibuffer ()
(layout %sidebar)
(with-fields (height width parent inputs) self
(setf height 0)
(setf width 0)
;; update all child dimensions
(dolist (element inputs)
(layout element)
(incf height (field-value :height element))
(callf max width (dash 2 (field-value :width element))))
;; now compute proper positions and re-layout
(let* ((x (+ (dash 1) (%window-x (current-buffer))))
(y0 (+ (%window-y (current-buffer))
*gl-screen-height*))
(y (- y0 height (dash 3))))
(dolist (element inputs)
(decf y0 (field-value :height element))
(move-to element x y0)
(layout element))
(setf %y y)
(setf %x x)
;; a little extra room at the top and sides
(incf height (dash 3)))))
;; ;; move to the right spot to keep the bottom on the bottom.
;; (setf y (- y0 (dash 1))))))
(define-method update minibuffer ()
(update (first %inputs))
(update (second %inputs)))
(define-method get-prompt minibuffer ()
(second %inputs))
(defun minibuffer-prompt ()
(when *minibuffer* (get-prompt *minibuffer*)))
(define-method enter minibuffer ()
(enter (get-prompt self)))
(define-method evaluate minibuffer ()
(evaluate (get-prompt self)))
(define-method focus minibuffer ()
(let ((prompt (get-prompt self)))
(set-read-only prompt nil)
(grab-focus prompt)))
(defparameter *minibuffer-rows* 9)
(define-method accept minibuffer (input &optional prepend)
(declare (ignore prepend))
(with-fields (inputs) self
(assert (not (null inputs))) ;; we always have a prompt
(prog1 t
(assert (valid-connection-p self input))
;; set parent if necessary
(adopt self input)
(setf inputs
(nconc (list (first inputs)
(second inputs)
input)
(nthcdr 2 inputs)))
;; drop last item in scrollback
(let ((len (length inputs)))
(when (> len *minibuffer-rows*)
(dolist (item (subseq inputs *minibuffer-rows*))
(destroy item))
(setf inputs (subseq inputs 0 (1- len)))))
;;
(add-item %sidebar (duplicate-safely input)))))
(defparameter *minibuffer-background-color* "gray20")
(define-method draw minibuffer ()
(with-fields (inputs x y height width) self
(draw-box (window-x) y *gl-screen-width* height :color *minibuffer-background-color*)
(mapc #'draw inputs)
(draw %sidebar)))
(define-method hit minibuffer (x y)
(or (hit %sidebar x y)
(when (within-extents x y %x %y (+ %x %width) (+ %y %height))
self)))
;;; minibuffer.lisp ends here