From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ben North Newsgroups: gmane.emacs.devel Subject: Undo behaviour of kbd macros Date: Mon, 26 Feb 2007 13:28:32 +0000 Message-ID: <1172496512.45e2e0808d298@imp.hosting365.ie> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: sea.gmane.org 1172519433 21873 80.91.229.12 (26 Feb 2007 19:50:33 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 26 Feb 2007 19:50:33 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Feb 26 20:50:18 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HLlrV-0001ce-EG for ged-emacs-devel@m.gmane.org; Mon, 26 Feb 2007 20:50:17 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HLlrV-0004L0-0n for ged-emacs-devel@m.gmane.org; Mon, 26 Feb 2007 14:50:17 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1HLfu9-0002Z4-Ms for emacs-devel@gnu.org; Mon, 26 Feb 2007 08:28:37 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1HLfu9-0002Yp-8N for emacs-devel@gnu.org; Mon, 26 Feb 2007 08:28:37 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HLfu9-0002Ym-1n for emacs-devel@gnu.org; Mon, 26 Feb 2007 08:28:37 -0500 Original-Received: from ns2.hosting365.ie ([82.195.128.192] helo=web.hosting365.ie) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.52) id 1HLfu8-00038Q-Bk for emacs-devel@gnu.org; Mon, 26 Feb 2007 08:28:36 -0500 Original-Received: from web.hosting365.ie (localhost [127.0.0.1]) by web.hosting365.ie (8.12.11.20060308/8.12.11) with ESMTP id l1QDSYtP027350 for ; Mon, 26 Feb 2007 13:28:34 GMT Original-Received: (from httpd@localhost) by web.hosting365.ie (8.12.11.20060308/8.12.11/Submit) id l1QDSWv8027345 for emacs-devel@gnu.org; Mon, 26 Feb 2007 13:28:32 GMT Original-Received: from ext159.sig.com (ext159.sig.com [141.162.101.50]) by imp.hosting365.ie (IMP) with HTTP for ; Mon, 26 Feb 2007 13:28:32 +0000 User-Agent: Internet Messaging Program (IMP) 3.2.8 X-Originating-IP: 141.162.101.50 X-detected-kernel: Linux 2.4-2.6 X-Mailman-Approved-At: Mon, 26 Feb 2007 14:48:48 -0500 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:66869 Archived-At: 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?