kmacro-x

a fork of vifon's kmacro-x
git clone https://git.trogloxene.org/kmacro-x.git
Log | Files | Refs | README | LICENSE

kmacro-x.el (2501B)


      1 ;;; kmacro-x.el --- Keyboard macro helpers and extensions  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2022-2025  Wojciech Siewierski
      4 
      5 ;; Author: Wojciech Siewierski
      6 ;; URL: https://github.com/vifon/kmacro-x.el
      7 ;; Keywords: convenience
      8 ;; Version: 0.9
      9 ;; Package-Requires: ((emacs "29.1"))
     10 
     11 ;; This program is free software; you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; This program is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;; A collection of commands, modes and functions building on top of
     27 ;; the keyboard macros (kmacros) system.
     28 
     29 ;;; Code:
     30 
     31 (require 'kmacro)
     32 
     33 
     34 (defgroup kmacro-x nil
     35   "Keyboard macro helpers and extensions."
     36   :group 'kmacro)
     37 
     38 ;;;###autoload
     39 (define-minor-mode kmacro-x-atomic-undo-mode
     40   "Undo the kmacro executions atomically."
     41   :global t
     42   :require 'kmacro-x
     43   :lighter " atomic-kmacro"
     44   (if kmacro-x-atomic-undo-mode
     45       (progn
     46         ;; Seemingly advising `execute-kbd-macro' should suffice, but
     47         ;; that's not true.  It might get called either from Elisp or
     48         ;; from `call-last-kbd-macro' which is a C function and so the
     49         ;; advice wouldn't apply for this call.  So we advice both to
     50         ;; cover hopefully all the possible entry points.
     51         (advice-add #'execute-kbd-macro :around
     52                     #'kmacro-x-undo-amalgamate-advice)
     53         (advice-add #'call-last-kbd-macro :around
     54                     #'kmacro-x-undo-amalgamate-advice))
     55     (advice-remove #'execute-kbd-macro
     56                    #'kmacro-x-undo-amalgamate-advice)
     57     (advice-remove #'call-last-kbd-macro
     58                    #'kmacro-x-undo-amalgamate-advice)))
     59 
     60 (defun kmacro-x-undo-amalgamate-advice (orig &rest args)
     61   "Advice the ORIG function to amalgamate all the edits into one undo.
     62 
     63 ARGS are passed verbatim to ORIG."
     64   ;; Place an `undo-boundary' to ensure the consecutive calls won't
     65   ;; get auto-amalgamated.
     66   (undo-boundary)
     67   (with-undo-amalgamate
     68     (apply orig args)))
     69 
     70 
     71 (provide 'kmacro-x)
     72 ;;; kmacro-x.el ends here