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