428
|
1 ;;; cmdloop.el --- support functions for the top-level command loop.
|
|
2
|
|
3 ;; Copyright (C) 1992-4, 1997 Free Software Foundation, Inc.
|
771
|
4 ;; Copyright (C) 2001 Ben Wing.
|
428
|
5
|
|
6 ;; Author: Richard Mlynarik
|
|
7 ;; Date: 8-Jul-92
|
|
8 ;; Maintainer: XEmacs Development Team
|
|
9 ;; Keywords: internal, dumped
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: FSF 19.30. (Some of the stuff below is in FSF's subr.el.)
|
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; This file is dumped with XEmacs.
|
|
33
|
|
34 ;;; Code:
|
|
35
|
|
36 (defun recursion-depth ()
|
|
37 "Return the current depth in recursive edits."
|
|
38 (+ command-loop-level (minibuffer-depth)))
|
|
39
|
|
40 (defun top-level ()
|
|
41 "Exit all recursive editing levels."
|
|
42 (interactive)
|
|
43 (throw 'top-level nil))
|
|
44
|
|
45 (defun exit-recursive-edit ()
|
|
46 "Exit from the innermost recursive edit or minibuffer."
|
|
47 (interactive)
|
|
48 (if (> (recursion-depth) 0)
|
|
49 (throw 'exit nil))
|
|
50 (error "No recursive edit is in progress"))
|
|
51
|
|
52 (defun abort-recursive-edit ()
|
|
53 "Abort the command that requested this recursive edit or minibuffer input."
|
|
54 (interactive)
|
|
55 (if (> (recursion-depth) 0)
|
|
56 (throw 'exit t))
|
|
57 (error "No recursive edit is in progress"))
|
|
58
|
|
59 ;; (defun keyboard-quit ()
|
|
60 ;; "Signal a `quit' condition."
|
|
61 ;; (interactive)
|
|
62 ;; (deactivate-mark)
|
|
63 ;; (signal 'quit nil))
|
|
64
|
|
65 ;; moved here from pending-del.
|
|
66 (defun keyboard-quit ()
|
|
67 "Signal a `quit' condition.
|
|
68 If this character is typed while lisp code is executing, it will be treated
|
|
69 as an interrupt.
|
|
70 If this character is typed at top-level, this simply beeps.
|
|
71 If `zmacs-regions' is true, and the zmacs region is active in this buffer,
|
|
72 then this key deactivates the region without beeping or signalling."
|
|
73 (interactive)
|
|
74 (if (and (region-active-p)
|
|
75 (eq (current-buffer) (zmacs-region-buffer)))
|
|
76 ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
|
|
77 ;; deactivating the region. If it is inactive, beep.
|
|
78 nil
|
|
79 (signal 'quit nil)))
|
|
80
|
|
81 (defvar buffer-quit-function nil
|
|
82 "Function to call to \"quit\" the current buffer, or nil if none.
|
|
83 \\[keyboard-escape-quit] calls this function when its more local actions
|
|
84 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
|
|
85
|
|
86 (defun keyboard-escape-quit ()
|
|
87 "Exit the current \"mode\" (in a generalized sense of the word).
|
|
88 This command can exit an interactive command such as `query-replace',
|
|
89 can clear out a prefix argument or a region,
|
|
90 can get out of the minibuffer or other recursive edit,
|
|
91 cancel the use of the current buffer (for special-purpose buffers),
|
|
92 or go back to just one window (by deleting all but the selected window)."
|
|
93 (interactive)
|
|
94 (cond ((eq last-command 'mode-exited) nil)
|
|
95 ((> (minibuffer-depth) 0)
|
|
96 (abort-recursive-edit))
|
|
97 (current-prefix-arg
|
|
98 nil)
|
|
99 ((region-active-p)
|
|
100 (zmacs-deactivate-region))
|
|
101 ((> (recursion-depth) 0)
|
|
102 (exit-recursive-edit))
|
|
103 (buffer-quit-function
|
|
104 (funcall buffer-quit-function))
|
|
105 ((not (one-window-p t))
|
|
106 (delete-other-windows))
|
|
107 ((string-match "^ \\*" (buffer-name (current-buffer)))
|
|
108 (bury-buffer))))
|
|
109
|
|
110 ;; `cancel-mode-internal' is a function of a misc-user event, which is
|
|
111 ;; queued when window system directs XEmacs frame to cancel any modal
|
|
112 ;; behavior it exposes, like mouse pointer grabbing.
|
|
113 ;;
|
|
114 ;; This function does nothing at the top level, but the code which
|
|
115 ;; runs modal event loops, such as selection drag loop in `mouse-track',
|
|
116 ;; check if misc-user function symbol is `cancel-mode-internal', and
|
|
117 ;; takes necessary cleanup actions.
|
|
118 (defun cancel-mode-internal (object)
|
|
119 (setq zmacs-region-stays t))
|
|
120
|
|
121 ;; Someone wrote: "This should really be a ring of last errors."
|
|
122 ;;
|
|
123 ;; But why bother? This stuff is not all that necessary now that we
|
|
124 ;; have message log, anyway.
|
|
125 (defvar last-error nil
|
|
126 "Object describing the last signaled error.")
|
|
127
|
|
128 (defcustom errors-deactivate-region nil
|
|
129 "*Non-nil means that errors will cause the region to be deactivated."
|
|
130 :type 'boolean
|
|
131 :group 'editing-basics)
|
|
132
|
771
|
133 (defvar breakpoint-on-error nil
|
|
134 "Break into the C debugger when an unhandled error occurs noninteractively.
|
|
135 This happens by calling `force-debugging-signal'. This can be very
|
|
136 useful when debugging noninteractive errors in tricky situations,
|
|
137 e.g. makefiles, since you can set this variable using an environment
|
|
138 variable, like this:
|
|
139
|
|
140 \(using csh) setenv XEMACSDEBUG '(setq breakpoint-on-error t)'
|
|
141 \(using bash) export XEMACSDEBUG='(setq breakpoint-on-error t)'
|
|
142
|
|
143 XEMACSDEBUG is also useful for setting `stack-trace-on-error'. which will
|
|
144 display a Lisp backtrace when exiting noninteractively due to an unhandled
|
|
145 error.")
|
|
146
|
428
|
147 (defun command-error (error-object)
|
771
|
148 ;; if you want a backtrace before exiting, set stack-trace-on-error.
|
|
149 (let* ((inhibit-quit t)
|
442
|
150 (debug-on-error nil)
|
|
151 (etype (car-safe error-object)))
|
428
|
152 (setq quit-flag nil)
|
|
153 (setq standard-output t)
|
|
154 (setq standard-input t)
|
|
155 (setq executing-kbd-macro nil)
|
|
156 (and errors-deactivate-region
|
|
157 (zmacs-deactivate-region))
|
|
158 (discard-input)
|
|
159
|
|
160 (setq last-error error-object)
|
|
161
|
|
162 (message nil)
|
|
163 (ding nil (cond ((eq etype 'undefined-keystroke-sequence)
|
|
164 (if (and (vectorp (nth 1 error-object))
|
|
165 (/= 0 (length (nth 1 error-object)))
|
|
166 (button-event-p (aref (nth 1 error-object) 0)))
|
|
167 'undefined-click
|
|
168 'undefined-key))
|
|
169 ((eq etype 'quit)
|
|
170 'quit)
|
|
171 ((memq etype '(end-of-buffer beginning-of-buffer))
|
|
172 'buffer-bound)
|
|
173 ((eq etype 'buffer-read-only)
|
|
174 'read-only)
|
|
175 (t 'command-error)))
|
|
176 (display-error error-object t)
|
|
177
|
|
178 (if (noninteractive)
|
|
179 (progn
|
771
|
180 (when breakpoint-on-error
|
|
181 (force-debugging-signal))
|
442
|
182 (message "%s exiting\n." emacs-program-name)
|
428
|
183 (kill-emacs -1)))
|
|
184 t))
|
|
185
|
|
186 (defun describe-last-error ()
|
|
187 "Redisplay the last error-message. See the variable `last-error'."
|
|
188 (interactive)
|
|
189 (if last-error
|
|
190 (with-displaying-help-buffer
|
|
191 (lambda ()
|
|
192 (princ "Last error was:\n" standard-output)
|
|
193 (display-error last-error standard-output)))
|
|
194 (message "No error yet")))
|
|
195
|
|
196
|
|
197 ;;#### Must be done later in the loadup sequence
|
|
198 ;(define-key (symbol-function 'help-command) "e" 'describe-last-error)
|
|
199
|
|
200
|
|
201 (defun truncate-command-history-for-gc ()
|
|
202 (let ((tail (nthcdr 30 command-history)))
|
|
203 (if tail (setcdr tail nil)))
|
|
204 (let ((tail (nthcdr 30 values)))
|
|
205 (if tail (setcdr tail nil)))
|
|
206 )
|
|
207
|
|
208 (add-hook 'pre-gc-hook 'truncate-command-history-for-gc)
|
|
209
|
|
210
|
|
211 ;;;; Object-oriented programming at its finest
|
|
212
|
|
213 ;; Now in src/print.c; used by Ferror_message_string and others
|
|
214 ;(defun display-error (error-object stream) ;(defgeneric report-condition ...)
|
|
215 ; "Display `error-object' on `stream' in a user-friendly way."
|
|
216 ; (funcall (or (let ((type (car-safe error-object)))
|
|
217 ; (catch 'error
|
|
218 ; (and (consp error-object)
|
|
219 ; (symbolp type)
|
|
220 ; ;;(stringp (get type 'error-message))
|
|
221 ; (consp (get type 'error-conditions))
|
|
222 ; (let ((tail (cdr error-object)))
|
|
223 ; (while (not (null tail))
|
|
224 ; (if (consp tail)
|
|
225 ; (setq tail (cdr tail))
|
|
226 ; (throw 'error nil)))
|
|
227 ; t)
|
|
228 ; ;; (check-type condition condition)
|
|
229 ; (get type 'error-conditions)
|
|
230 ; ;; Search class hierarchy
|
|
231 ; (let ((tail (get type 'error-conditions)))
|
|
232 ; (while (not (null tail))
|
|
233 ; (cond ((not (and (consp tail)
|
|
234 ; (symbolp (car tail))))
|
|
235 ; (throw 'error nil))
|
|
236 ; ((get (car tail) 'display-error)
|
|
237 ; (throw 'error (get (car tail)
|
|
238 ; 'display-error)))
|
|
239 ; (t
|
|
240 ; (setq tail (cdr tail)))))
|
|
241 ; ;; Default method
|
|
242 ; #'(lambda (error-object stream)
|
|
243 ; (let ((type (car error-object))
|
|
244 ; (tail (cdr error-object))
|
|
245 ; (first t)
|
|
246 ; (print-message-label 'error))
|
|
247 ; (if (eq type 'error)
|
|
248 ; (progn (princ (car tail) stream)
|
|
249 ; (setq tail (cdr tail)))
|
|
250 ; (princ (or (gettext (get type 'error-message)) type)
|
|
251 ; stream))
|
|
252 ; (while tail
|
|
253 ; (princ (if first ": " ", ") stream)
|
|
254 ; (prin1 (car tail) stream)
|
|
255 ; (setq tail (cdr tail)
|
|
256 ; first nil))))))))
|
|
257 ; #'(lambda (error-object stream)
|
|
258 ; (princ (gettext "Peculiar error ") stream)
|
|
259 ; (prin1 error-object stream)))
|
|
260 ; error-object stream))
|
|
261
|
|
262 (put 'file-error 'display-error
|
|
263 #'(lambda (error-object stream)
|
|
264 (let ((tail (cdr error-object))
|
|
265 (first t))
|
|
266 (princ (car tail) stream)
|
|
267 (while (setq tail (cdr tail))
|
|
268 (princ (if first ": " ", ") stream)
|
|
269 (princ (car tail) stream)
|
|
270 (setq first nil)))))
|
|
271
|
|
272 (put 'undefined-keystroke-sequence 'display-error
|
|
273 #'(lambda (error-object stream)
|
|
274 (princ (key-description (car (cdr error-object))) stream)
|
|
275 ;; #### I18N3: doesn't localize properly.
|
|
276 (princ (gettext " not defined.") stream) ; doo dah, doo dah.
|
|
277 ))
|
|
278
|
|
279
|
|
280 (defcustom teach-extended-commands-p t
|
|
281 "*If true, then `\\[execute-extended-command]' will teach you keybindings.
|
|
282 Any time you execute a command with \\[execute-extended-command] which has a
|
|
283 shorter keybinding, you will be shown the alternate binding before the
|
|
284 command executes. There is a short pause after displaying the binding,
|
|
285 before executing it; the length can be controlled by
|
|
286 `teach-extended-commands-timeout'."
|
|
287 :type 'boolean
|
|
288 :group 'keyboard)
|
|
289
|
|
290 (defcustom teach-extended-commands-timeout 4
|
|
291 "*How long to pause after displaying a keybinding before executing.
|
|
292 The value is measured in seconds. This only applies if
|
|
293 `teach-extended-commands-p' is true."
|
|
294 :type 'number
|
|
295 :group 'keyboard)
|
|
296
|
|
297 ;That damn RMS went off and implemented something differently, after
|
|
298 ;we had already implemented it. We can't support both properly until
|
|
299 ;we have Lisp magic variables.
|
|
300 ;(defvar suggest-key-bindings t
|
|
301 ; "*FSFmacs equivalent of `teach-extended-commands-*'.
|
|
302 ;Provided for compatibility only.
|
|
303 ;Non-nil means show the equivalent key-binding when M-x command has one.
|
|
304 ;The value can be a length of time to show the message for.
|
|
305 ;If the value is non-nil and not a number, we wait 2 seconds.")
|
|
306 ;
|
|
307 ;(make-obsolete-variable 'suggest-key-bindings 'teach-extended-commands-p)
|
|
308
|
|
309 (defun execute-extended-command (prefix-arg)
|
|
310 "Read a command name from the minibuffer using 'completing-read'.
|
|
311 Then call the specified command using 'command-execute' and return its
|
|
312 return value. If the command asks for a prefix argument, supply the
|
|
313 value of the current raw prefix argument, or the value of PREFIX-ARG
|
|
314 when called from Lisp."
|
|
315 (interactive "P")
|
|
316 ;; Note: This doesn't hack "this-command-keys"
|
|
317 (let ((prefix-arg prefix-arg))
|
|
318 (setq this-command (read-command
|
|
319 ;; Note: this has the hard-wired
|
|
320 ;; "C-u" and "M-x" string bug in common
|
613
|
321 ;; with all Emacs's.
|
428
|
322 ;; (i.e. it prints C-u and M-x regardless of
|
|
323 ;; whether some other keys were actually bound
|
|
324 ;; to `execute-extended-command' and
|
|
325 ;; `universal-argument'.
|
|
326 (cond ((eq prefix-arg '-)
|
|
327 "- M-x ")
|
|
328 ((equal prefix-arg '(4))
|
|
329 "C-u M-x ")
|
|
330 ((integerp prefix-arg)
|
|
331 (format "%d M-x " prefix-arg))
|
|
332 ((and (consp prefix-arg)
|
|
333 (integerp (car prefix-arg)))
|
|
334 (format "%d M-x " (car prefix-arg)))
|
|
335 (t
|
|
336 "M-x ")))))
|
|
337
|
|
338 (if (and teach-extended-commands-p
|
|
339 (interactive-p))
|
|
340 ;; Remember the keys, run the command, and show the keys (if
|
|
341 ;; any). The funny variable names are a poor man's guarantee
|
|
342 ;; that we don't get tripped by this-command doing something
|
|
343 ;; funny. Quoth our forefathers: "We want lexical scope!"
|
|
344 (let ((_execute_command_keys_ (where-is-internal this-command))
|
|
345 (_execute_command_name_ this-command)) ; the name can change
|
|
346 (command-execute this-command t)
|
|
347 (when _execute_command_keys_
|
|
348 ;; Normally the region is adjusted in post_command_hook;
|
|
349 ;; however, it is not called until after we finish. It
|
|
350 ;; looks ugly for the region to get updated after the
|
|
351 ;; delays, so we do it now. The code below is a Lispified
|
|
352 ;; copy of code in event-stream.c:post_command_hook().
|
|
353 (if (and (not zmacs-region-stays)
|
|
354 (or (not (eq (selected-window) (minibuffer-window)))
|
|
355 (eq (zmacs-region-buffer) (current-buffer))))
|
|
356 (zmacs-deactivate-region)
|
|
357 (zmacs-update-region))
|
|
358 ;; Wait for a while, so the user can see a message printed,
|
|
359 ;; if any.
|
|
360 (when (sit-for 1)
|
|
361 (display-message
|
|
362 'no-log
|
|
363 (format (if (cdr _execute_command_keys_)
|
|
364 "Command `%s' is bound to keys: %s"
|
|
365 "Command `%s' is bound to key: %s")
|
|
366 _execute_command_name_
|
|
367 (sorted-key-descriptions _execute_command_keys_)))
|
|
368 (sit-for teach-extended-commands-timeout)
|
|
369 (clear-message 'no-log))))
|
|
370 ;; Else, just run the command.
|
|
371 (command-execute this-command t)))
|
|
372
|
|
373
|
|
374 ;;; C code calls this; the underscores in the variable names are to avoid
|
|
375 ;;; cluttering the specbind namespace (lexical scope! lexical scope!)
|
|
376 ;;; Putting this in Lisp instead of C slows kbd macros by 50%.
|
|
377 ;(defun command-execute (_command &optional _record-flag)
|
|
378 ; "Execute CMD as an editor command.
|
|
379 ;CMD must be a symbol that satisfies the `commandp' predicate.
|
|
380 ;Optional second arg RECORD-FLAG non-nil
|
|
381 ;means unconditionally put this command in `command-history'.
|
|
382 ;Otherwise, that is done only if an arg is read using the minibuffer."
|
|
383 ; (let ((_prefix prefix-arg)
|
|
384 ; (_cmd (indirect-function _command)))
|
|
385 ; (setq prefix-arg nil
|
|
386 ; this-command _command
|
|
387 ; current-prefix-arg _prefix
|
|
388 ; zmacs-region-stays nil)
|
|
389 ; ;; #### debug_on_next_call = 0;
|
|
390 ; (cond ((and (symbolp _command)
|
|
391 ; (get _command 'disabled))
|
|
392 ; (run-hooks disabled-command-hook))
|
|
393 ; ((or (stringp _cmd) (vectorp _cmd))
|
|
394 ; ;; If requested, place the macro in the command history.
|
|
395 ; ;; For other sorts of commands, call-interactively takes
|
|
396 ; ;; care of this.
|
|
397 ; (if _record-flag
|
|
398 ; (setq command-history
|
|
399 ; (cons (list 'execute-kbd-macro _cmd _prefix)
|
|
400 ; command-history)))
|
|
401 ; (execute-kbd-macro _cmd _prefix))
|
|
402 ; (t
|
|
403 ; (call-interactively _command _record-flag)))))
|
|
404
|
|
405 (defun y-or-n-p-minibuf (prompt)
|
|
406 "Ask user a \"y or n\" question. Return t if answer is \"y\".
|
|
407 Takes one argument, which is the string to display to ask the question.
|
|
408 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
|
|
409 No confirmation of the answer is requested; a single character is enough.
|
|
410 Also accepts Space to mean yes, or Delete to mean no."
|
|
411 (save-excursion
|
|
412 (let* ((pre "")
|
|
413 (yn (gettext "(y or n) "))
|
|
414 ;; we need to translate the prompt ourselves because of the
|
|
415 ;; strange way we handle it.
|
|
416 (prompt (gettext prompt))
|
|
417 event)
|
|
418 (while (stringp yn)
|
|
419 (if (let ((cursor-in-echo-area t)
|
|
420 (inhibit-quit t))
|
|
421 (message "%s%s%s" pre prompt yn)
|
|
422 (setq event (next-command-event event))
|
|
423 (condition-case nil
|
|
424 (prog1
|
|
425 (or quit-flag (eq 'keyboard-quit (key-binding event)))
|
|
426 (setq quit-flag nil))
|
|
427 (wrong-type-argument t)))
|
|
428 (progn
|
|
429 (message "%s%s%s%s" pre prompt yn (single-key-description event))
|
|
430 (setq quit-flag nil)
|
|
431 (signal 'quit '())))
|
|
432 (let* ((keys (events-to-keys (vector event)))
|
|
433 (def (lookup-key query-replace-map keys)))
|
|
434 (cond ((eq def 'skip)
|
|
435 (message "%s%sNo" prompt yn)
|
|
436 (setq yn nil))
|
|
437 ((eq def 'act)
|
|
438 (message "%s%sYes" prompt yn)
|
|
439 (setq yn t))
|
|
440 ((eq def 'recenter)
|
|
441 (recenter))
|
|
442 ((or (eq def 'quit) (eq def 'exit-prefix))
|
|
443 (signal 'quit '()))
|
|
444 ((button-release-event-p event) ; ignore them
|
|
445 nil)
|
|
446 (t
|
|
447 (message "%s%s%s%s" pre prompt yn
|
|
448 (single-key-description event))
|
|
449 (ding nil 'y-or-n-p)
|
|
450 (discard-input)
|
|
451 (if (= (length pre) 0)
|
|
452 (setq pre (gettext "Please answer y or n. ")))))))
|
|
453 yn)))
|
|
454
|
|
455 (defun yes-or-no-p-minibuf (prompt)
|
|
456 "Ask user a yes-or-no question. Return t if answer is yes.
|
|
457 Takes one argument, which is the string to display to ask the question.
|
|
458 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
|
|
459 The user must confirm the answer with RET,
|
|
460 and can edit it until it has been confirmed."
|
|
461 (save-excursion
|
|
462 (let ((p (concat (gettext prompt) (gettext "(yes or no) ")))
|
|
463 (ans ""))
|
|
464 (while (stringp ans)
|
|
465 (setq ans (downcase (read-string p nil t))) ;no history
|
|
466 (cond ((string-equal ans (gettext "yes"))
|
|
467 (setq ans t))
|
|
468 ((string-equal ans (gettext "no"))
|
|
469 (setq ans nil))
|
|
470 (t
|
|
471 (ding nil 'yes-or-no-p)
|
|
472 (discard-input)
|
|
473 (message "Please answer yes or no.")
|
|
474 (sleep-for 2))))
|
|
475 ans)))
|
|
476
|
442
|
477 (defun yes-or-no-p (prompt)
|
|
478 "Ask user a yes-or-no question. Return t if answer is yes.
|
|
479 The question is asked with a dialog box or the minibuffer, as appropriate.
|
|
480 Takes one argument, which is the string to display to ask the question.
|
|
481 It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
|
|
482 The user must confirm the answer with RET,
|
|
483 and can edit it until it as been confirmed."
|
|
484 (if (should-use-dialog-box-p)
|
|
485 (yes-or-no-p-dialog-box prompt)
|
|
486 (yes-or-no-p-minibuf prompt)))
|
|
487
|
|
488 (defun y-or-n-p (prompt)
|
|
489 "Ask user a \"y or n\" question. Return t if answer is \"y\".
|
|
490 Takes one argument, which is the string to display to ask the question.
|
|
491 The question is asked with a dialog box or the minibuffer, as appropriate.
|
|
492 It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
|
|
493 No confirmation of the answer is requested; a single character is enough.
|
|
494 Also accepts Space to mean yes, or Delete to mean no."
|
|
495 (if (should-use-dialog-box-p)
|
|
496 (yes-or-no-p-dialog-box prompt)
|
|
497 (y-or-n-p-minibuf prompt)))
|
|
498
|
428
|
499
|
|
500
|
|
501 (defun read-char ()
|
|
502 "Read a character from the command input (keyboard or macro).
|
|
503 If a mouse click or non-ASCII character is detected, an error is
|
|
504 signalled. The character typed is returned as an ASCII value. This
|
|
505 is most likely the wrong thing for you to be using: consider using
|
|
506 the `next-command-event' function instead."
|
|
507 (save-excursion
|
|
508 (let ((event (next-command-event)))
|
|
509 (or inhibit-quit
|
|
510 (and (event-matches-key-specifier-p event (quit-char))
|
|
511 (signal 'quit nil)))
|
|
512 (prog1 (or (event-to-character event)
|
|
513 ;; Kludge. If the event we read was a mouse-release,
|
|
514 ;; discard it and read the next one.
|
|
515 (if (button-release-event-p event)
|
|
516 (event-to-character (next-command-event event)))
|
|
517 (error "Key read has no ASCII equivalent %S" event))
|
|
518 ;; this is not necessary, but is marginally more efficient than GC.
|
|
519 (deallocate-event event)))))
|
|
520
|
|
521 (defun read-char-exclusive ()
|
|
522 "Read a character from the command input (keyboard or macro).
|
|
523 If a mouse click or non-ASCII character is detected, it is discarded.
|
|
524 The character typed is returned as an ASCII value. This is most likely
|
|
525 the wrong thing for you to be using: consider using the
|
|
526 `next-command-event' function instead."
|
|
527 (let (event ch)
|
|
528 (while (progn
|
|
529 (setq event (next-command-event))
|
|
530 (or inhibit-quit
|
|
531 (and (event-matches-key-specifier-p event (quit-char))
|
|
532 (signal 'quit nil)))
|
|
533 (setq ch (event-to-character event))
|
|
534 (deallocate-event event)
|
|
535 (null ch)))
|
|
536 ch))
|
|
537
|
|
538 (defun read-quoted-char (&optional prompt)
|
|
539 "Like `read-char', except that if the first character read is an octal
|
|
540 digit, we read up to two more octal digits and return the character
|
|
541 represented by the octal number consisting of those digits.
|
|
542 Optional argument PROMPT specifies a string to use to prompt the user."
|
|
543 (let ((count 0) (code 0) done
|
|
544 (prompt (and prompt (gettext prompt)))
|
|
545 char event)
|
|
546 (while (and (not done) (< count 3))
|
|
547 (let ((inhibit-quit (zerop count))
|
|
548 ;; Don't let C-h get the help message--only help function keys.
|
|
549 (help-char nil)
|
|
550 (help-form
|
|
551 "Type the special character you want to use,
|
|
552 or three octal digits representing its character code."))
|
|
553 (and prompt (display-message 'prompt (format "%s-" prompt)))
|
|
554 (setq event (next-command-event)
|
|
555 char (or (event-to-character event nil nil t)
|
|
556 (signal 'error
|
|
557 (list "key read cannot be inserted in a buffer"
|
|
558 event))))
|
|
559 (if inhibit-quit (setq quit-flag nil)))
|
|
560 (cond ((<= ?0 char ?7)
|
|
561 (setq code (+ (* code 8) (- char ?0))
|
|
562 count (1+ count))
|
|
563 (when prompt
|
|
564 (display-message 'prompt
|
|
565 (setq prompt (format "%s %c" prompt char)))))
|
|
566 ((> count 0)
|
|
567 (setq unread-command-event event
|
|
568 done t))
|
|
569 (t (setq code (char-int char)
|
|
570 done t))))
|
|
571 (int-char code)
|
|
572 ;; Turn a meta-character into a character with the 0200 bit set.
|
|
573 ; (logior (if (/= (logand code ?\M-\^@) 0) 128 0)
|
|
574 ; (logand 255 code))))
|
|
575 ))
|
|
576
|
|
577 (defun momentary-string-display (string pos &optional exit-char message)
|
|
578 "Momentarily display STRING in the buffer at POS.
|
|
579 Display remains until next character is typed.
|
|
580 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
|
|
581 otherwise it is then available as input (as a command if nothing else).
|
|
582 Display MESSAGE (optional fourth arg) in the echo area.
|
|
583 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
|
|
584 (or exit-char (setq exit-char ?\ ))
|
|
585 (let ((buffer-read-only nil)
|
|
586 ;; Don't modify the undo list at all.
|
|
587 (buffer-undo-list t)
|
|
588 (modified (buffer-modified-p))
|
|
589 (name buffer-file-name)
|
|
590 insert-end)
|
|
591 (unwind-protect
|
|
592 (progn
|
|
593 (save-excursion
|
|
594 (goto-char pos)
|
|
595 ;; defeat file locking... don't try this at home, kids!
|
|
596 (setq buffer-file-name nil)
|
|
597 (insert-before-markers (gettext string))
|
|
598 (setq insert-end (point))
|
|
599 ;; If the message end is off frame, recenter now.
|
|
600 (if (> (window-end) insert-end)
|
|
601 (recenter (/ (window-height) 2)))
|
|
602 ;; If that pushed message start off the frame,
|
|
603 ;; scroll to start it at the top of the frame.
|
|
604 (move-to-window-line 0)
|
|
605 (if (> (point) pos)
|
|
606 (progn
|
|
607 (goto-char pos)
|
|
608 (recenter 0))))
|
|
609 (message (or message (gettext "Type %s to continue editing."))
|
|
610 (single-key-description exit-char))
|
|
611 (let ((event (save-excursion (next-command-event))))
|
|
612 (or (eq (event-to-character event) exit-char)
|
|
613 (setq unread-command-event event))))
|
|
614 (if insert-end
|
|
615 (save-excursion
|
|
616 (delete-region pos insert-end)))
|
|
617 (setq buffer-file-name name)
|
|
618 (set-buffer-modified-p modified))))
|
|
619
|
|
620 ;;; cmdloop.el ends here
|