1 ;;; cmdloop.el --- support functions for the top-level command loop.
3 ;; Copyright (C) 1992-4, 1997 Free Software Foundation, Inc.
5 ;; Author: Richard Mlynarik
7 ;; Maintainer: SXEmacs Development Team
8 ;; Keywords: internal, dumped
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; SXEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: FSF 19.30. (Some of the stuff below is in FSF's subr.el.)
29 ;; This file is dumped with SXEmacs.
33 (defun recursion-depth ()
34 "Return the current depth in recursive edits."
35 (+ command-loop-level (minibuffer-depth)))
38 "Exit all recursive editing levels."
40 (throw 'top-level nil))
42 (defun exit-recursive-edit ()
43 "Exit from the innermost recursive edit or minibuffer."
45 (if (> (recursion-depth) 0)
47 (error "No recursive edit is in progress"))
49 (defun abort-recursive-edit ()
50 "Abort the command that requested this recursive edit or minibuffer input."
52 (if (> (recursion-depth) 0)
54 (error "No recursive edit is in progress"))
56 ;; (defun keyboard-quit ()
57 ;; "Signal a `quit' condition."
60 ;; (signal 'quit nil))
62 ;; moved here from pending-del.
63 (defun keyboard-quit ()
64 "Signal a `quit' condition.
65 If this character is typed while lisp code is executing, it will be treated
67 If this character is typed at top-level, this simply beeps.
68 If `zmacs-regions' is true, and the zmacs region is active in this buffer,
69 then this key deactivates the region without beeping or signalling."
72 ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
73 ;; deactivating the region. If it is inactive, beep.
77 (defvar buffer-quit-function nil
78 "Function to call to \"quit\" the current buffer, or nil if none.
79 \\[keyboard-escape-quit] calls this function when its more local actions
80 \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
82 (defun keyboard-escape-quit ()
83 "Exit the current \"mode\" (in a generalized sense of the word).
84 This command can exit an interactive command such as `query-replace',
85 can clear out a prefix argument or a region,
86 can get out of the minibuffer or other recursive edit,
87 cancel the use of the current buffer (for special-purpose buffers),
88 or go back to just one window (by deleting all but the selected window)."
90 (cond ((eq last-command 'mode-exited) nil)
91 ((> (minibuffer-depth) 0)
92 (abort-recursive-edit))
96 (zmacs-deactivate-region))
97 ((> (recursion-depth) 0)
98 (exit-recursive-edit))
100 (funcall buffer-quit-function))
101 ((not (one-window-p t))
102 (delete-other-windows))
103 ((string-match "^ \\*" (buffer-name (current-buffer)))
106 ;; `cancel-mode-internal' is a function of a misc-user event, which is
107 ;; queued when window system directs XEmacs frame to cancel any modal
108 ;; behavior it exposes, like mouse pointer grabbing.
110 ;; This function does nothing at the top level, but the code which
111 ;; runs modal event loops, such as selection drag loop in `mouse-track',
112 ;; check if misc-user function symbol is `cancel-mode-internal', and
113 ;; takes necessary cleanup actions.
114 (defun cancel-mode-internal (object)
115 (setq zmacs-region-stays t))
117 ;; Someone wrote: "This should really be a ring of last errors."
119 ;; But why bother? This stuff is not all that necessary now that we
120 ;; have message log, anyway.
121 (defvar last-error nil
122 "Object describing the last signaled error.")
124 (defcustom errors-deactivate-region nil
125 "*Non-nil means that errors will cause the region to be deactivated."
127 :group 'editing-basics)
129 (defun command-error (error-object)
130 (let* ((old-debug-on-error debug-on-error)
133 (etype (car-safe error-object)))
135 (setq standard-output t)
136 (setq standard-input t)
137 (setq executing-kbd-macro nil)
138 (and errors-deactivate-region
139 (zmacs-deactivate-region))
142 (setq last-error error-object)
145 (ding nil (cond ((eq etype 'undefined-keystroke-sequence)
146 (if (and (vectorp (nth 1 error-object))
147 (/= 0 (length (nth 1 error-object)))
148 (button-event-p (aref (nth 1 error-object) 0)))
153 ((memq etype '(end-of-buffer beginning-of-buffer))
155 ((eq etype 'buffer-read-only)
158 (display-error error-object t)
162 (if old-debug-on-error
164 (message "Backtrace:\n\n")
167 (message "%s exiting\n." emacs-program-name)
171 (defun describe-last-error ()
172 "Redisplay the last error-message. See the variable `last-error'."
175 (with-displaying-help-buffer
177 (princ "Last error was:\n" standard-output)
178 (display-error last-error standard-output)))
179 (message "No error yet")))
182 ;;#### Must be done later in the loadup sequence
183 ;(define-key (symbol-function 'help-command) "e" 'describe-last-error)
186 (defun truncate-command-history-for-gc ()
187 ;; We should try to avoid accessing any bindings to speak of in this
188 ;; function; as this hook is called asynchronously, the search for
189 ;; those bindings might search local bindings from essentially
190 ;; arbitrary functions. We force the body of the function to run at
191 ;; command-loop level, where the danger of local bindings is much
192 ;; reduced; the code can still do its job because the command history
193 ;; and values list will not grow before then anyway.
195 ;; Nothing is done in batch mode, both because it is a waste of time
196 ;; (there is no command loop!) and because this any GCs during dumping
197 ;; will invoke this code, and if it were to enqueue an eval event,
198 ;; the portable dumper would try to dump it and fail.
199 (if (not (noninteractive))
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))))
208 (add-hook 'pre-gc-hook 'truncate-command-history-for-gc)
211 ;;;; Object-oriented programming at its finest
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)))
218 ; (and (consp error-object)
220 ; ;;(stringp (get type 'error-message))
221 ; (consp (get type 'error-conditions))
222 ; (let ((tail (cdr error-object)))
223 ; (while (not (null tail))
225 ; (setq tail (cdr tail))
226 ; (throw 'error nil)))
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)
240 ; (setq tail (cdr tail)))))
242 ; #'(lambda (error-object stream)
243 ; (let ((type (car error-object))
244 ; (tail (cdr error-object))
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)
253 ; (princ (if first ": " ", ") stream)
254 ; (prin1 (car tail) stream)
255 ; (setq tail (cdr tail)
257 ; #'(lambda (error-object stream)
258 ; (princ (gettext "Peculiar error ") stream)
259 ; (prin1 error-object stream)))
260 ; error-object stream))
262 (put 'file-error 'display-error
263 #'(lambda (error-object stream)
264 (let ((tail (cdr error-object))
266 (princ (car tail) stream)
267 (while (setq tail (cdr tail))
268 (princ (if first ": " ", ") stream)
269 (princ (car tail) stream)
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.
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'."
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."
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.")
307 ;(make-obsolete-variable 'suggest-key-bindings 'teach-extended-commands-p)
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."
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
321 ;; with all GNU Emacs's.
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 '-)
328 ((equal prefix-arg '(4))
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)))
338 (if (and teach-extended-commands-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,
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)))
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
398 ; (setq command-history
399 ; (cons (list 'execute-kbd-macro _cmd _prefix)
401 ; (execute-kbd-macro _cmd _prefix))
403 ; (call-interactively _command _record-flag)))))
405 (defun y-or-n-p-minibuf (prompt)
406 "Ask user a \"y or n\" question. Return t if answer is \"y\", nil if \"n\".
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."
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))
419 (if (let ((cursor-in-echo-area t)
421 (message "%s%s%s" pre prompt yn)
422 (setq event (next-command-event event))
425 (or quit-flag (eq 'keyboard-quit (key-binding event)))
426 (setq quit-flag nil))
427 (wrong-type-argument t)))
429 (message "%s%s%s%s" pre prompt yn (single-key-description event))
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)
438 (message "%s%sYes" prompt yn)
442 ((or (eq def 'quit) (eq def 'exit-prefix))
444 ((button-release-event-p event) ; ignore them
447 (message "%s%s%s%s" pre prompt yn
448 (single-key-description event))
451 (if (= (length pre) 0)
452 (setq pre (gettext "Please answer y or n. ")))))))
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."
462 (let ((p (concat (gettext prompt) (gettext "(yes or no) ")))
465 (setq ans (downcase (read-string p nil t))) ;no history
466 (cond ((string-equal ans (gettext "yes"))
468 ((string-equal ans (gettext "no"))
471 (ding nil 'yes-or-no-p)
473 (message "Please answer yes or no.")
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)))
488 (defun y-or-n-p (prompt)
489 "Ask user a \"y or n\" question. Return t if answer is \"y\", nil if \"n\".
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)))
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."
508 (let ((event (next-command-event)))
510 (and (event-matches-key-specifier-p event (quit-char))
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)))))
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."
529 (setq event (next-command-event))
531 (and (event-matches-key-specifier-p event (quit-char))
533 (setq ch (event-to-character event))
534 (deallocate-event event)
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)))
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.
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)
557 (list "key read cannot be inserted in a buffer"
559 (if inhibit-quit (setq quit-flag nil)))
560 (cond ((<= ?0 char ?7)
561 (setq code (+ (* code 8) (- char ?0))
564 (display-message 'prompt
565 (setq prompt (format "%s %c" prompt char)))))
567 (setq unread-command-event event
569 (t (setq code (char-int char)
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))))
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.
588 (modified (buffer-modified-p))
589 (name buffer-file-name)
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)
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))))
616 (delete-region pos insert-end)))
617 (setq buffer-file-name name)
618 (set-buffer-modified-p modified))))
620 ;;; cmdloop.el ends here