From: Ben North <ben@redfrontdoor.org>
To: emacs-devel@gnu.org
Subject: Undo behaviour of kbd macros
Date: Mon, 26 Feb 2007 13:28:32 +0000 [thread overview]
Message-ID: <1172496512.45e2e0808d298@imp.hosting365.ie> (raw)
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)
xs
=> (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)))
xs)
(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)))
(undo-boundary)
(push ',sentinel buffer-undo-list)
(unwind-protect
(progn
,@body)
(with-current-buffer ,cb
(setq buffer-undo-list
(n-remove-NILs-until-sentinel ',sentinel
buffer-undo-list)))))))
(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)
(with-no-undo-boundaries
(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
(progn
(if (null last-kbd-macro)
(error "No keyboard macro has been defined"))
(setq macro last-kbd-macro)))
(save-excursion
(with-no-undo-boundaries ;;; <-------- ADDED ------------
(let ((end-marker (copy-marker bottom))
next-line-marker)
(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)
(save-excursion
(forward-line 1)
(set-marker next-line-marker (point)))
(save-excursion
(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?
next reply other threads:[~2007-02-26 13:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-02-26 13:28 Ben North [this message]
2007-02-26 23:21 ` Undo behaviour of kbd macros Mathias Dahl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1172496512.45e2e0808d298@imp.hosting365.ie \
--to=ben@redfrontdoor.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.