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