;;; repeat.el --- convenient way to repeat the previous command
-;; Copyright (C) 1998 Free Software Foundation, Inc.
+;; Copyright (C) 1998, 2001, 2002, 2003, 2004, 2005,
+;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
;; Author: Will Mengarini <seldon@eskimo.com>
;; Created: Mo 02 Mar 98
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
:type '(repeat function))
;; If the last command was self-insert-command, the char to be inserted was
-;; obtained by that command from last-command-char, which has now been
+;; obtained by that command from last-command-event, which has now been
;; clobbered by the command sequence that invoked `repeat'. We could get it
-;; from (recent-keys) & set last-command-char to that, "unclobbering" it, but
+;; from (recent-keys) & set last-command-event to that, "unclobbering" it, but
;; this has the disadvantage that if the user types a sequence of different
;; chars then invokes repeat, only the final char will be inserted. In vi,
;; the dot command can reinsert the entire most-recently-inserted sequence.
;; with auto-filling. Most problems are eliminated by remembering what we're
;; self-inserting, so we only need to get it from the undo information once.
+;; With Emacs 22.2 the variable `last-repeatable-command' stores the
+;; most recently executed command that was not bound to an input event.
+;; `repeat' now repeats that command instead of `real-last-command' to
+;; avoid a "... must be bound to an event with parameters" error.
+
(defvar repeat-last-self-insert nil
"If last repeated command was `self-insert-command', it inserted this.")
(defvar repeat-previous-repeated-command nil
"The previous repeated command.")
+;; The following variable counts repeated self-insertions. The idea is
+;; that repeating a self-insertion command and subsequently undoing it
+;; should have almost the same effect as if the characters were inserted
+;; manually. The basic difference is that we leave in one undo-boundary
+;; between the original insertion and its first repetition.
+(defvar repeat-undo-count nil
+ "Number of self-insertions since last `undo-boundary'.")
+
;;;###autoload
(defun repeat (repeat-arg)
"Repeat most recently executed command.
-With prefix arg, apply new prefix arg to that command; otherwise, use
-the prefix arg that was used before (if any).
+With prefix arg, apply new prefix arg to that command; otherwise,
+use the prefix arg that was used before (if any).
This command is like the `.' command in the vi editor.
-If this command is invoked by a multi-character key sequence, it can then
-be repeated by repeating the final character of that sequence. This behavior
-can be modified by the global variable `repeat-on-final-keystroke'."
+If this command is invoked by a multi-character key sequence, it
+can then be repeated by repeating the final character of that
+sequence. This behavior can be modified by the global variable
+`repeat-on-final-keystroke'.
+
+`repeat' ignores commands bound to input events. Hence the term
+\"most recently executed command\" shall be read as \"most
+recently executed command not bound to an input event\"."
;; The most recently executed command could be anything, so surprises could
;; result if it were re-executed in a context where new dynamically
;; localized variables were shadowing global variables in a `let' clause in
;; "repeat-" prefix, reserved by this package, for *local* variables that
;; might be visible to re-executed commands, including this function's arg.
(interactive "P")
- (when (eq real-last-command 'repeat)
- (setq real-last-command repeat-previous-repeated-command))
- (when (null real-last-command)
+ (when (eq last-repeatable-command 'repeat)
+ (setq last-repeatable-command repeat-previous-repeated-command))
+ (cond
+ ((null last-repeatable-command)
(error "There is nothing to repeat"))
- (when (eq real-last-command 'mode-exit)
- (error "real-last-command is mode-exit & can't be repeated"))
- (when (memq real-last-command repeat-too-dangerous)
- (error "Command %S too dangerous to repeat automatically" real-last-command))
- (setq this-command real-last-command
- repeat-num-input-keys-at-repeat num-input-keys)
- (setq repeat-previous-repeated-command this-command)
+ ((eq last-repeatable-command 'mode-exit)
+ (error "last-repeatable-command is mode-exit & can't be repeated"))
+ ((memq last-repeatable-command repeat-too-dangerous)
+ (error "Command %S too dangerous to repeat automatically"
+ last-repeatable-command)))
+ (setq this-command last-repeatable-command
+ repeat-previous-repeated-command last-repeatable-command
+ repeat-num-input-keys-at-repeat num-input-keys)
(when (null repeat-arg)
(setq repeat-arg last-prefix-arg))
;; Now determine whether to loop on repeated taps of the final character
;; of the key sequence that invoked repeat. The Emacs global
- ;; last-command-char contains the final character now, but may not still
+ ;; last-command-event contains the final character now, but may not still
;; contain it after the previous command is repeated, so the character
;; needs to be saved.
(let ((repeat-repeat-char
(if (eq repeat-on-final-keystroke t)
- ;; allow any final input event that was a character
- (when (eq last-command-char
- last-command-event)
- last-command-char)
+ last-command-event
;; allow only specified final keystrokes
- (car (memq last-command-char
+ (car (memq last-command-event
(listify-key-sequence
repeat-on-final-keystroke))))))
- (if (memq real-last-command '(exit-minibuffer
- minibuffer-complete-and-exit
- self-insert-and-exit))
+ (if (memq last-repeatable-command '(exit-minibuffer
+ minibuffer-complete-and-exit
+ self-insert-and-exit))
(let ((repeat-command (car command-history)))
(repeat-message "Repeating %S" repeat-command)
(eval repeat-command))
(if (null repeat-arg)
- (repeat-message "Repeating command %S" real-last-command)
- (setq current-prefix-arg repeat-arg)
- (repeat-message "Repeating command %S %S" repeat-arg real-last-command))
- (if (eq real-last-command 'self-insert-command)
+ (repeat-message "Repeating command %S" last-repeatable-command)
+ (setq current-prefix-arg repeat-arg)
+ (repeat-message
+ "Repeating command %S %S" repeat-arg last-repeatable-command))
+ (if (eq last-repeatable-command 'self-insert-command)
(let ((insertion
(if (<= (- num-input-keys
repeat-num-input-keys-at-self-insert)
(setq insertion (substring insertion -1))
(let ((count (prefix-numeric-value repeat-arg))
(i 0))
+ ;; Run pre- and post-command hooks for self-insertion too.
+ (run-hooks 'pre-command-hook)
+ (cond
+ ((not repeat-undo-count))
+ ((< repeat-undo-count 20)
+ ;; Don't make an undo-boundary here.
+ (setq repeat-undo-count (1+ repeat-undo-count)))
+ (t
+ ;; Make an undo-boundary after 20 repetitions only.
+ (undo-boundary)
+ (setq repeat-undo-count 1)))
(while (< i count)
(repeat-self-insert insertion)
- (setq i (1+ i)))))
- (let ((indirect (indirect-function real-last-command)))
+ (setq i (1+ i)))
+ (run-hooks 'post-command-hook)))
+ (let ((indirect (indirect-function last-repeatable-command)))
+ ;; Make each repetition undo separately.
+ (undo-boundary)
(if (or (stringp indirect)
(vectorp indirect))
- ;; Bind real-last-command so that executing the macro
- ;; does not alter it.
- (let ((real-last-command real-last-command))
- (execute-kbd-macro real-last-command))
- (call-interactively real-last-command)))))
+ ;; Bind real-last-command so that executing the macro does
+ ;; not alter it. Do the same for last-repeatable-command.
+ (let ((real-last-command real-last-command)
+ (last-repeatable-command last-repeatable-command))
+ (execute-kbd-macro last-repeatable-command))
+ (run-hooks 'pre-command-hook)
+ (call-interactively last-repeatable-command)
+ (run-hooks 'post-command-hook)))))
(when repeat-repeat-char
;; A simple recursion here gets into trouble with max-lisp-eval-depth
;; on long sequences of repetitions of a command like `forward-word'
;; (only 32 repetitions are possible given the default value of 200 for
;; max-lisp-eval-depth), but if I now locally disable the repeat char I
;; can iterate indefinitely here around a single level of recursion.
- (let (repeat-on-final-keystroke)
- (while (eq (read-event) repeat-repeat-char)
- ;; Make each repetition undo separately.
- (undo-boundary)
- (repeat repeat-arg))
+ (let (repeat-on-final-keystroke
+ ;; Bind `undo-inhibit-record-point' to t in order to avoid
+ ;; recording point in `buffer-undo-list' here. We have to
+ ;; do this since the command loop does not set the last
+ ;; position of point thus confusing the point recording
+ ;; mechanism when inserting or deleting text.
+ (undo-inhibit-record-point t))
+ (setq real-last-command 'repeat)
+ (setq repeat-undo-count 1)
+ (unwind-protect
+ (while (let ((evt (read-key)))
+ ;; For clicks, we need to strip the meta-data to
+ ;; check the underlying event name.
+ (eq (or (car-safe evt) evt)
+ (or (car-safe repeat-repeat-char)
+ repeat-repeat-char)))
+ (repeat repeat-arg))
+ ;; Make sure `repeat-undo-count' is reset.
+ (setq repeat-undo-count nil))
(setq unread-command-events (list last-input-event))))))
(defun repeat-self-insert (string)
(let ((i 0))
(while (< i (length string))
- (let ((last-command-char (aref string i)))
+ (let ((last-command-event (aref string i)))
(self-insert-command 1))
(setq i (1+ i)))))
(provide 'repeat)
-;;; arch-tag: cd569600-a1ad-4fa7-9062-bb91dfeaf1db
+;; arch-tag: cd569600-a1ad-4fa7-9062-bb91dfeaf1db
;;; repeat.el ends here