[lice @ indent-region (buggy) + eval-defun]
[lice.git] / minibuffer.lisp
blob1cbfe848cc835ca255d081d801a6ea23761f31de
1 (in-package :lice)
3 (defvar *history-length* 30
4 "Maximum length for history lists before truncation takes place.
5 A number means that length; t means infinite. Truncation takes place
6 just after a new element is inserted. Setting the :HISTORY-LENGTH
7 property of a history variable overrides this default.")
9 (defvar *minibuffer-text-before-history* nil
10 "Text that was in this minibuffer before any history commands.
11 This is nil if there have not yet been any history commands
12 in this use of the minibuffer.")
14 (defclass minibuffer-window (window)
15 ())
17 (define-major-mode minibuffer-read-mode
18 (:name "minibuffer mode"
19 :map (let ((m (make-sparse-keymap)))
20 (define-key m (make-instance 'key :char #\m :control t) 'exit-minibuffer)
21 (define-key m (make-instance 'key :char #\Newline) 'exit-minibuffer)
22 (define-key m (make-instance 'key :char #\Return) 'exit-minibuffer)
23 (define-key m (make-instance 'key :char #\j :control t) 'exit-minibuffer)
24 (define-key m (make-instance 'key :char #\p :meta t) 'previous-history-element)
25 (define-key m (make-instance 'key :char #\n :meta t) 'next-history-element)
26 (define-key m (make-instance 'key :char #\g :control t) 'abort-recursive-edit)
27 m))
28 "minibuffer read mode"
29 ;; empty init
32 (define-major-mode minibuffer-complete-mode
33 (:name "minibuffer mode"
34 :map (let ((m (make-sparse-keymap)))
35 (define-key m (make-instance 'key :char #\m :control t) 'exit-minibuffer)
36 (define-key m (make-instance 'key :char #\Newline) 'exit-minibuffer)
37 (define-key m (make-instance 'key :char #\Return) 'exit-minibuffer)
38 (define-key m (make-instance 'key :char #\j :control t) 'exit-minibuffer)
39 (define-key m (make-instance 'key :char #\p :meta t) 'previous-history-element)
40 (define-key m (make-instance 'key :char #\n :meta t) 'next-history-element)
41 (define-key m (make-instance 'key :char #\i :control t) 'minibuffer-complete)
42 (define-key m (make-instance 'key :char #\Tab) 'minibuffer-complete)
43 (define-key m (make-instance 'key :char #\g :control t) 'abort-recursive-edit)
44 m))
45 "minibuffer complete mode"
46 ;; empty init
49 (define-major-mode minibuffer-must-match-mode
50 (:name "minibuffer mode"
51 :map (let ((m (make-sparse-keymap)))
52 (define-key m (make-instance 'key :char #\m :control t) 'minibuffer-complete-and-exit)
53 (define-key m (make-instance 'key :char #\Newline) 'minibuffer-complete-and-exit)
54 (define-key m (make-instance 'key :char #\Return) 'minibuffer-complete-and-exit)
55 (define-key m (make-instance 'key :char #\j :control t) 'minibuffer-complete-and-exit)
56 (define-key m (make-instance 'key :char #\p :meta t) 'previous-history-element)
57 (define-key m (make-instance 'key :char #\n :meta t) 'next-history-element)
58 (define-key m (make-instance 'key :char #\i :control t) 'minibuffer-complete)
59 (define-key m (make-instance 'key :char #\Tab) 'minibuffer-complete)
60 (define-key m (make-instance 'key :char #\g :control t) 'abort-recursive-edit)
61 m))
62 "minibuffer complete mode"
63 ;; empty init
66 (defvar *minibuffer-completion-table* nil
67 "Alist or obarray used for completion in the minibuffer.
68 This becomes the ALIST argument to `try-completion' and `all-completions'.
69 The value can also be a list of strings or a hash table.
71 The value may alternatively be a function, which is given three arguments:
72 STRING, the current buffer contents;
73 PREDICATE, the predicate for filtering possible matches;
74 CODE, which says what kind of things to do.
75 CODE can be nil, t or `lambda'.
76 nil means to return the best completion of STRING, or nil if there is none.
77 t means to return a list of all possible completions of STRING.
78 `lambda' means to return t if STRING is a valid completion as it stands.")
80 (defvar *minibuffer-history* nil
81 "Default minibuffer history list.
82 This is used for all minibuffer input
83 except when an alternate history list is specified.")
85 (defvar *minibuffer-history-position* nil
86 "Current position of redoing in the history list.")
88 (defvar *minibuffer-history-variable* '*minibuffer-history*
89 "History list symbol to add minibuffer values to.
90 Each string of minibuffer input, as it appears on exit from the minibuffer,
91 is added with
92 ** (set minibuffer-history-variable
93 ** (cons STRING (symbol-value minibuffer-history-variable)))")
95 (defvar *minibuffer-completion-predicate* nil
96 "Within call to `completing-read', this holds the PREDICATE argument.")
98 (defun make-minibuffer (major-mode)
99 "Return a fresh minibuffer with major mode, MAJOR-MODE."
100 ;; FIXME: Emacs prefixes it with a space so it doesn't show up in
101 ;; buffer listings. How are we gonna do this?
102 (let ((mb (get-buffer-create (generate-new-buffer-name " *minibuffer*"))))
103 (setf (buffer-major-mode mb) major-mode
104 (buffer-mode-line mb) nil)
105 mb))
107 (defun make-minibuffer-window (height cols)
108 (let* ((w (make-instance 'minibuffer-window
109 :x 0 :y (- height 1) :w cols :h 1
110 :line-state (make-array 1 :fill-pointer 1
111 :element-type 'integer :initial-element -1)
112 :cache (make-instance 'line-cache :valid t)
113 :top-line 0
114 :bottom-line 0
115 :point-col 0
116 :point-line 0
117 :buffer (make-minibuffer minibuffer-read-mode)
118 :top (make-marker)
119 :bottom (make-marker)
120 :bpoint (make-marker)
121 :point-col 0
122 :point-line 0)))
123 (set-marker (window-top w) 0 (window-buffer w))
124 (set-marker (window-bottom w) 0 (window-buffer w))
127 ;; (defun clear-minibuffer ()
128 ;; "Erase the contents of the minibuffer when it isn't active."
129 ;; (let ((minibuffer (window-buffer (frame-minibuffer-window (selected-frame)))))
130 ;; (erase-buffer minibuffer)))
132 (defun minibuffer-window (&optional (frame (selected-frame)))
133 "Return the window used now for minibuffers.
134 If the optional argument FRAME is specified, return the minibuffer window
135 used by that frame."
136 (frame-minibuffer-window frame))
138 (defun message (string &rest arguments)
139 "Print a one-line message at the bottom of the screen."
140 ;; FIXME: properly implement the echo area
141 (when (zerop (frame-minibuffers-active (selected-frame)))
142 (let ((minibuffer (window-buffer (frame-minibuffer-window (selected-frame))))
143 (msg (apply #'format nil string arguments)))
144 (erase-buffer minibuffer)
145 (buffer-insert minibuffer msg)
146 (with-current-buffer (get-buffer-create "*messages*")
147 (goto-char (point-max))
148 (insert msg #\Newline)))))
150 (defun clear-minibuffer ()
151 "Erase the text in the minibuffer, unless it's active."
152 (when (zerop (frame-minibuffers-active (selected-frame)))
153 (erase-buffer (window-buffer (frame-minibuffer-window (selected-frame))))))
155 (defun show-minibuffer-prompt (frame prompt)
156 "Show PROMPT in the minibuffer. Flip a bit in FRAME to allow
157 switching to the minibuffer."
158 (declare (type string prompt)
159 (ignore frame))
160 (let ((minibuffer (window-buffer (frame-minibuffer-window (selected-frame))))
161 (field (npropertize prompt 'field 't 'front-sticky t 'rear-nonsticky t)))
162 (dformat +debug-v+ "~a~%" field)
163 (erase-buffer minibuffer)
164 (buffer-insert minibuffer field)))
166 (defun minibuffer-prompt-end (&optional (minibuf (current-buffer)))
167 "Return the buffer position of the end of the minibuffer prompt.
168 Return (point-min) if current buffer is not a mini-buffer."
169 (let ((beg (begv minibuf)))
170 (multiple-value-bind (start end) (find-field beg nil :beg t :end t :buf minibuf)
171 (dformat +debug-v+ "exit-mb: ~a ~a ~a~%" start end (buffer-size minibuf))
172 (if (and (= end (zv minibuf))
173 (null (get-char-property beg 'field minibuf)))
175 end))))
177 (defun minibuffer-contents (&optional (minibuf (current-buffer)))
178 "Return the user input in a minbuffer as a string.
179 If MINIBUF is omitted, default to the current buffer.
180 MINIBUF must be a minibuffer."
181 (buffer-substring (minibuffer-prompt-end minibuf) (zv minibuf) minibuf))
183 (defun minibuffer-contents-no-properties (&optional (minibuf (current-buffer)))
184 "Return the user input in a minbuffer as a string.
185 If MINIBUF is omitted, default to the current buffer.
186 MINIBUF must be a minibuffer."
187 (buffer-substring-no-properties (minibuffer-prompt-end minibuf) (zv minibuf) minibuf))
189 (defun minibuffer-completion-contents (&optional (minibuf (current-buffer)))
190 "Return the user input in a minibuffer before point as a string.
191 That is what completion commands operate on.
192 The current buffer must be a minibuffer."
193 ;; FIXME: the emacs source produces an error when the pt is not at
194 ;; the end. I Don't know why, though.
195 (buffer-substring (minibuffer-prompt-end minibuf) (zv minibuf) minibuf))
197 (defun delete-minibuffer-contents (&optional (minibuf (current-buffer)))
198 "Delete all user input in a minibuffer.
199 MINIBUF must be a minibuffer."
200 (let ((end (minibuffer-prompt-end minibuf)))
201 (when (< end (zv minibuf))
202 (delete-region end (zv minibuf)))))
204 (defun setup-minibuffer-for-read (major-mode prompt initial-contents history)
205 (save-window-excursion
206 ;; Create a new minibuffer
207 (let* ((frame (selected-frame))
208 (*minibuffer-history-variable* history)
209 (*minibuffer-history-position* 0)
210 (*minibuffer-text-before-history* nil)
211 (old-minibuffer (window-buffer (frame-minibuffer-window frame)))
212 (new-minibuffer (make-minibuffer major-mode)))
213 (window-save-point (get-current-window))
214 ;; attach it to the current frame
215 (set-window-buffer (frame-minibuffer-window frame) new-minibuffer)
216 (select-window (frame-minibuffer-window frame))
217 ;; Show the prompt
218 (show-minibuffer-prompt frame prompt)
219 ;; move to the end of input
220 (setf (marker-position (buffer-point new-minibuffer)) (buffer-size new-minibuffer))
221 (when initial-contents (insert initial-contents))
222 ;; enter recursive edit
223 (dformat +debug-v+ "ya ohoe~%")
224 (incf (frame-minibuffers-active frame))
225 (unwind-protect
226 (progn
227 (recursive-edit)
228 (let* ((val (minibuffer-contents new-minibuffer))
229 (hist-string (when (> (length val) 0)
230 val)))
231 (when (and *minibuffer-history-variable* hist-string)
232 (let ((hist-val (symbol-value *minibuffer-history-variable*)))
233 ;; If the caller wanted to save the value read on a history list,
234 ;; then do so if the value is not already the front of the list.
235 (when (or (null hist-val)
236 (and (consp hist-val)
237 (not (equal hist-string (car hist-val)))))
238 (push hist-string hist-val)
239 (setf (symbol-value *minibuffer-history-variable*) hist-val))
240 ;; truncate if requested
241 (let ((len (or (get *minibuffer-history-variable* :history-length)
242 *history-length*)))
243 (when (integerp len)
244 (if (< len 0)
245 (setf (symbol-value *minibuffer-history-variable*) nil)
246 (let ((tmp (nthcdr len hist-val)))
247 (when tmp
248 (rplacd tmp nil))))))))
249 ;; return the value
250 val))
251 ;; Restore the buffer
252 (dformat +debug-v+ "minibuffer~%")
253 (set-window-buffer (frame-minibuffer-window frame) old-minibuffer)
254 (kill-buffer new-minibuffer)
255 (decf (frame-minibuffers-active frame))))))
257 (defun test-completion (string alist &optional predicate)
258 "Return non-nil if string is a valid completion.
259 Takes the same arguments as `all-completions' and `try-completion'.
260 If alist is a function, it is called with three arguments:
261 the values string, predicate and `lambda'."
262 (let ((matches (all-completions string alist predicate)))
263 (and (= (length matches) 1)
264 (string= (first matches) string))))
266 (defcommand minibuffer-complete-and-exit ()
267 "If the minibuffer contents is a valid completion then exit.
268 Otherwise try to complete it. If completion leads to a valid completion,
269 a repetition of this command will exit."
270 (let ((str (minibuffer-contents)))
271 (if (test-completion str *minibuffer-completion-table* *minibuffer-completion-predicate*)
272 (throw 'exit nil)
273 (minibuffer-complete))))
276 (defun read-from-minibuffer (prompt &key initial-contents keymap read (history '*minibuffer-history*) default-value)
277 "Read a string from the minibuffer, prompting with string PROMPT.
278 The optional second arg INITIAL-CONTENTS is an obsolete alternative to
279 DEFAULT-VALUE. It normally should be nil in new code, except when
280 HISTORY is a cons. It is discussed in more detail below.
281 Third arg KEYMAP is a keymap to use whilst reading;
282 if omitted or nil, the default is `minibuffer-local-map'.
283 If fourth arg READ is non-nil, then interpret the result as a Lisp object
284 and return that object:
285 in other words, do `(car (read-from-string INPUT-STRING))'
286 Fifth arg HISTORY, if non-nil, specifies a history list and optionally
287 the initial position in the list. It can be a symbol, which is the
288 history list variable to use, or it can be a cons cell
289 (HISTVAR . HISTPOS). In that case, HISTVAR is the history list variable
290 to use, and HISTPOS is the initial position for use by the minibuffer
291 history commands. For consistency, you should also specify that
292 element of the history as the value of INITIAL-CONTENTS. Positions
293 are counted starting from 1 at the beginning of the list.
294 Sixth arg DEFAULT-VALUE is the default value. If non-nil, it is available
295 for history commands; but, unless READ is non-nil, `read-from-minibuffer'
296 does NOT return DEFAULT-VALUE if the user enters empty input! It returns
297 the empty string.
298 Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer inherits
299 the current input method and the setting of `enable-multibyte-characters'.
300 Eight arg KEEP-ALL, if non-nil, says to put all inputs in the history list,
301 even empty or duplicate inputs.
302 If the variable `minibuffer-allow-text-properties' is non-nil,
303 then the string which is returned includes whatever text properties
304 were present in the minibuffer. Otherwise the value has no text properties.
306 The remainder of this documentation string describes the
307 INITIAL-CONTENTS argument in more detail. It is only relevant when
308 studying existing code, or when HISTORY is a cons. If non-nil,
309 INITIAL-CONTENTS is a string to be inserted into the minibuffer before
310 reading input. Normally, point is put at the end of that string.
311 However, if INITIAL-CONTENTS is \(STRING . POSITION), the initial
312 input is STRING, but point is placed at _one-indexed_ position
313 POSITION in the minibuffer. Any integer value less than or equal to
314 one puts point at the beginning of the string. *Note* that this
315 behavior differs from the way such arguments are used in `completing-read'
316 and some related functions, which use zero-indexing for POSITION."
317 (declare (ignore default-value read keymap))
318 (setup-minibuffer-for-read minibuffer-read-mode prompt initial-contents history))
320 (defun tree-find (tree obj &key (test #'eq))
321 "find OBJ in TREE. Return the OBJ or nil."
322 (cond ((typep tree obj)
323 (when (funcall test tree obj)
324 tree))
325 (t (or (tree-find (car tree) obj :test test)
326 (tree-find (cdr tree) obj :test test)))))
328 (defun tree-sibling (tree obj &key (test #'eq))
329 "Return the OBJ's sibling in tree or nil."
330 (declare (type (or list window) tree))
331 (cond ((typep tree obj)
332 nil)
333 ((funcall test obj (car tree))
334 (cdr tree))
335 ((funcall test obj (cdr tree))
336 (car tree))
337 (t (or (tree-sibling (car tree) obj :test test)
338 (tree-sibling (cdr tree) obj :test test)))))
340 (defun frame-for-window (window)
341 "Return the frame that holds WINDOW."
342 (find-if (lambda (f)
343 (tree-find (frame-window-tree f) window)) *frame-list*))
345 (defcommand ask-user ()
347 (message "user typed: ~a" (read-from-minibuffer "input: ")))
349 (defcommand exit-minibuffer ()
351 (dformat +debug-v+ "exit-minibuffer~%")
352 (throw 'exit nil))
354 (defcommand abort-recursive-edit ()
355 (throw 'exit t))
357 (defgeneric all-completions (string alist &optional predicate hide-spaces)
358 (:documentation "Return a list of possible matches."))
360 (defmethod all-completions (string (alist list) &optional predicate hide-spaces)
361 (declare (ignore hide-spaces))
362 (let ((tester (or predicate
363 (lambda (s)
364 (string= string s :end2 (min (length string)
365 (length s)))))))
366 (loop for elt in alist
367 for i = (cond ((consp elt)
368 (car elt))
369 ((symbolp elt)
370 ;; FIXME: this is a hack. isn't there a
371 ;; global that decides whether they're
372 ;; printed upcase or not?
373 (string-downcase (symbol-name elt)))
374 (t elt))
375 when (funcall tester i)
376 collect i)))
378 (defmethod all-completions (string (fn function) &optional predicate hide-spaces)
379 (declare (ignore hide-spaces))
380 (funcall fn string predicate nil))
382 (defun try-completion (string alist &optional predicate)
383 (labels ((all-are-good (match strings)
384 (loop for i in strings
385 never (string/= match i :end2 (min (length match)
386 (length i))))))
387 (let* ((possibles (all-completions string alist predicate))
388 (match (make-array 100 ; MOVITZ: the match can't be more than 100 chars
389 :element-type 'character
390 :fill-pointer 0
391 ;; :adjustable t
393 ;; FIXME: this dubplicates effort since the first (length string)
394 ;; chars will be the same.
395 (when possibles
396 (loop for i from 0 below (length (first possibles))
397 do (vector-push-extend (char (first possibles) i) match)
398 unless (all-are-good match possibles)
399 do (progn
400 (decf (fill-pointer match))
401 (return)))
402 match))))
404 (define-condition history-end (lice-condition)
405 () (:documentation "raised when at the end of the history"))
407 (define-condition history-beginning (lice-condition)
408 () (:documentation "raised when at the begining of the history"))
410 (defcommand next-history-element ((&optional n)
411 :prefix)
412 (let ((narg (- *minibuffer-history-position* n))
413 (minimum 0)
414 elt)
415 (when (and (zerop *minibuffer-history-position*)
416 (null *minibuffer-text-before-history*))
417 (setf *minibuffer-text-before-history*
418 (minibuffer-contents-no-properties)))
419 (when (< narg minimum)
420 (signal 'history-end #|"End of history; no next item"|#))
421 (when (> narg (length (symbol-value *minibuffer-history-variable*)))
422 (signal 'history-beginning #|"Beginning of history; no preceding item"|#))
423 (goto-char (point-max))
424 (delete-minibuffer-contents)
425 (setf *minibuffer-history-position* narg)
426 (cond ((= narg 0)
427 (setf elt (or *minibuffer-text-before-history* "")
428 *minibuffer-text-before-history* nil))
430 (setf elt (nth (1- *minibuffer-history-position*)
431 (symbol-value *minibuffer-history-variable*)))))
432 (insert
433 ;; (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth))
434 ;; (not minibuffer-returned-to-present))
435 ;; (let ((*print-level* nil))
436 ;; (prin1-to-string elt))
437 elt)
438 (goto-char (point-max))))
441 (defcommand previous-history-element ()
442 (next-history-element -1))
444 (defcommand minibuffer-complete ()
445 (let* ((txt (minibuffer-contents))
446 (match (try-completion txt *minibuffer-completion-table*)))
447 (dformat +debug-v+ "txt: ~a match: ~a~%" txt match)
448 (when match
449 (if (= (length match)
450 (length txt))
451 ;; no new text was added, so list the possibilities
452 (let* ((txt (minibuffer-contents))
453 (strings (all-completions txt *minibuffer-completion-table*)))
454 (with-current-buffer (get-buffer-create "*Completions*")
455 (erase-buffer)
456 (insert (format nil "Here are the completions.~%"))
457 (loop for c in strings
458 do (insert (format nil "~a~%" c)))
459 (goto-char (point-min))
460 (display-buffer (current-buffer))))
461 (progn
462 (goto-char (point-max))
463 (insert (subseq match (length txt))))))))
465 (defun completing-read (prompt table &key predicate require-match
466 initial-input (history '*minibuffer-history*) def)
467 "Read a string in the minibuffer, with completion.
468 PROMPT is a string to prompt with; normally it ends in a colon and a space.
469 TABLE is an alist whose elements' cars are strings, or an obarray.
470 TABLE can also be a function to do the completion itself.
471 PREDICATE limits completion to a subset of TABLE.
472 See `try-completion' and `all-completions' for more details
473 on completion, TABLE, and PREDICATE.
475 If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
476 the input is (or completes to) an element of TABLE or is null.
477 If it is also not t, Return does not exit if it does non-null completion.
478 If the input is null, `completing-read' returns an empty string,
479 regardless of the value of REQUIRE-MATCH.
481 If INITIAL-INPUT is non-nil, insert it in the minibuffer initially.
482 If it is (STRING . POSITION), the initial input
483 is STRING, but point is placed POSITION characters into the string.
484 This feature is deprecated--it is best to pass nil for INITIAL.
485 HISTORY, if non-nil, specifies a history list
486 and optionally the initial position in the list.
487 It can be a symbol, which is the history list variable to use,
488 or it can be a cons cell (HISTVAR . HISTPOS).
489 In that case, HISTVAR is the history list variable to use,
490 and HISTPOS is the initial position (the position in the list
491 which INITIAL-INPUT corresponds to).
492 Positions are counted starting from 1 at the beginning of the list.
493 DEF, if non-nil, is the default value."
494 (declare (ignore def))
495 (let ((*minibuffer-completion-table* table)
496 (*minibuffer-completion-predicate* predicate))
497 (setup-minibuffer-for-read (if require-match
498 minibuffer-must-match-mode
499 minibuffer-complete-mode)
500 prompt initial-input history)))
502 ;; (defun y-or-n-p (prompt)
503 ;; "Ask user a \"y or n\" question. Return t if answer is \"y\".
504 ;; Takes one argument, which is the string to display to ask the question.
505 ;; It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
506 ;; No confirmation of the answer is requested; a single character is enough.
507 ;; Also accepts Space to mean yes, or Delete to mean no. (Actually, it uses
508 ;; the bindings in `query-replace-map'; see the documentation of that variable
509 ;; for more information. In this case, the useful bindings are `act', `skip',
510 ;; `recenter', and `quit'.)
512 ;; Under a windowing system a dialog box will be used if `last-nonmenu-event'
513 ;; is nil and `use-dialog-box' is non-nil."
514 ;; ;; FIXME: This needs to be redone when the ECHO AREA works.
515 ;; (string-equal "y" (read-from-minibuffer (concatenate 'string prompt "(y on n)"))))
517 (provide :lice-0.1/minibuffer)