[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Undo behaviour of kbd macros

From: Ben North
Subject: Undo behaviour of kbd macros
Date: Mon, 26 Feb 2007 13:28:32 +0000
User-agent: Internet Messaging Program (IMP) 3.2.8

A follow-up in some sense to my recent email about mark not being
deactivated after `apply-macro-to-region-lines':  When using keyboard
macros, I find that I expect `undo' to undo the actions of the macro as
one atomic unit, rather than undoing the individual commands within the
macro one by one.  I have this expectation of some personal
customisations I've written too, so wrote the following code to make it
easier to write commands which only generate one undo unit:

(defun n-remove-NILs-until-sentinel (sentinel xs)
  "Remove NILs from XS until SENTINEL is found; then remove SENTINEL.
Starting from the head of XS, remove NILs, destructively where
possible.  This is repeated until SENTINEL is encountered (which is
tested for using EQ).  The SENTINEL is then destructively removed
from XS as well, and the resulting list returned.  For example:

   (setq xs '(nil a b nil c nil XX d nil e f))
        => (nil a b nil c nil XX d nil e f)
   (n-remove-NILs-until-sentinel 'XX xs)
        => (a b c d nil e f)
        => (nil a b c d nil e f)

If the SENTINEL is not found in the list, an error is signalled."
  (while (and (consp xs) (null (car xs)))
    (setq xs (cdr xs)))
  (let* ((head xs)
         (tail (cdr head)))
    (while (progn (while (and (consp tail) (null (car tail)))
                    (setcdr head (setq tail (cdr tail))))
                  (if (and (consp tail) (not (eq (car tail) sentinel)))
                      (setq head tail
                            tail (cdr head)))))
    (if (null tail)
        (error "sentinel not found"))
    (setcdr head (cdr tail)))

(defmacro with-no-undo-boundaries (&rest body)
  "Execute the BODY, ensuring that no undo boundaries are created therein.
An undo boundary is created before executing BODY, but any undo boundaries
added to the `buffer-undo-list' by the execution of BODY are stripped out
afterwards, including in the case of abnormal exit."
  (let ((sentinel (make-symbol "sentinel"))
        (cb (make-symbol "cb")))
    `(let ((,cb (current-buffer)))
       (push ',sentinel buffer-undo-list)
         (with-current-buffer ,cb
           (setq buffer-undo-list
                 (n-remove-NILs-until-sentinel ',sentinel
(put 'with-no-undo-boundaries 'lisp-indent-function 0)

This macro could be used, for example in (a wrapper round)
`call-last-kbd-macro' to give what I think is the expected behaviour,
namely that `call-last-kbd-macro' is undo-able as one unit.  The
existing ability to undo one piece at a time could be preserved by
supplying `C--' as prefix arg, currently treated the same as a zero
prefix arg:

(defun wrapped-call-last-kbd-macro (rpt)
  "Control over undo units generated by `call-last-kbd-macro'."
  (interactive "P")
  (if (eq rpt '-)
      (call-last-kbd-macro 1)
      (call-last-kbd-macro rpt))))

Similarly for `apply-macro-to-region-lines':

(defun apply-macro-to-region-lines (top bottom &optional macro)
  "... docstring ..."
  (interactive "r")
  (or macro
        (if (null last-kbd-macro)
            (error "No keyboard macro has been defined"))
        (setq macro last-kbd-macro)))
    (with-no-undo-boundaries    ;;; <-------- ADDED ------------
      (let ((end-marker (copy-marker bottom))
        (goto-char top)
        (if (not (bolp))
            (forward-line 1))
        (setq next-line-marker (point-marker))
        (while (< next-line-marker end-marker)
          (goto-char next-line-marker)
            (forward-line 1)
            (set-marker next-line-marker (point)))
            (let ((mark-active nil))
              (execute-kbd-macro (or macro last-kbd-macro)))))
        (set-marker end-marker nil)
        (set-marker next-line-marker nil)))))

Alternatively, an option could be added to preserve current undo
behaviour, maybe providing "all as one undo unit", "each invocation of
the macro as an undo unit" and "every individual command as an undo
unit" choices.

(Incidentally, could the "(or macro last-kbd macro)" as argument to
`execute-kbd-macro' there be replaced with just "macro", since "macro"
is "setq"d at the top of the function?)

Would other users find this "one undo unit" behaviour more intuitive?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]