commit f9df322d0c894129ac96e9a4fba03077957157be
parent 5dc825a8a2d739343e93ee2664c40deef8f461a4
Author: Wojciech Siewierski <wojciech@siewierski.eu>
Date: Thu, 17 Mar 2022 03:28:36 +0100
Improve the reliability of kmacro-x-mc
Diffstat:
2 files changed, 31 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
@@ -26,7 +26,7 @@ While this mode is enabled, the kmacro executions can be undo-ed
atomically, as a single operation. Often the user doesn't want to
undo a single step of a kmacro but rather the kmacro as a whole.
-**kmacro-x-mc-region**
+**kmacro-x-mc**
Emulates the typical multiple-cursors workflow using kmacros.
This way all the pitfalls of rolling a custom implementation of
@@ -36,11 +36,11 @@ counters, queries and kmacro editing, are gained virtually for free.
It is assumed the user didn't rebind the basic isearch commands,
otherwise the behavior may be unpredictable.
-A typical workflow with `kmacro-x-mc-region`:
+A typical workflow with `kmacro-x-mc`:
1. Select the text whose occurences are to be manipulated (in
a trivial case: a symbol to be renamed).
-2. <kbd>M-x kmacro-x-mc-region RET</kbd>
+2. <kbd>M-x kmacro-x-mc RET</kbd>
3. Do the necessary edits, either within the region or in its vicinity
outside of it (this is the part that cannot be easily achieved with
other mc alternatives such as iedit or query-replace). They will
diff --git a/kmacro-x.el b/kmacro-x.el
@@ -65,6 +65,14 @@ If HIGHLIGHT is non-nil (or with the prefix argument when using
interactively) highlight the query using `highlight-regexp'.
Use `\\[unhighlight-regexp]' to remove the highlight later."
(interactive "r\nP")
+ (unless (and (= start (region-beginning))
+ (= end (region-end)))
+ ;; Ensure a consistent behavior when called interactively and when
+ ;; called from Elisp with arguments not being the current region.
+ ;; Considering the interactive nature of the kmacros, tampering
+ ;; with the user's region seems very much justified.
+ (push-mark start)
+ (goto-char end))
(let ((query (buffer-substring-no-properties start end)))
(when (< (point) (mark))
(exchange-point-and-mark))
@@ -78,6 +86,26 @@ Use `\\[unhighlight-regexp]' to remove the highlight later."
'kmacro-x-mc-highlight-face))
(start-kbd-macro 'append 'no-exec)))
+;;;###autoload
+(defun kmacro-x-mc (&optional highlight)
+ "A more user friendly version of `kmacro-x-mc-region'.
+
+If selection is active, it does the same thing as
+`kmacro-x-mc-region'. If there is no active selection, it uses
+the symbol at point instead.
+
+See `kmacro-x-mc-region' for the START, END and
+HIGHLIGHT arguments."
+ (interactive "P")
+ (let ((bounds (if (use-region-p)
+ (cons (region-beginning) (region-end))
+ (bounds-of-thing-at-point 'symbol))))
+ (unless bounds
+ (error "%s" "No region or symbol to act on"))
+ (let ((start (car bounds))
+ (end (cdr bounds)))
+ (kmacro-x-mc-region start end highlight))))
+
;;;###autoload
(define-minor-mode kmacro-x-atomic-undo-mode