-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmacro-x-mc-legacy.el
110 lines (86 loc) · 3.98 KB
/
kmacro-x-mc-legacy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;;; kmacro-x-mc-legacy.el --- The old implementation of the multiple cursors -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2023 Wojciech Siewierski
;; Author: Wojciech Siewierski
;; URL: https://github.com/vifon/kmacro-x.el
;; Keywords: convenience
;; Version: 0.9
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; The obsolete version of the multiple cursors based of keyboard
;; macros. Less robust but with different semantics. No longer
;; maintained but might be useful.
;;; Code:
(require 'kmacro-x-mc)
(defgroup kmacro-x-mc-legacy nil
"Multiple cursors implemented with keyboard macros (legacy)."
:group 'kmacro-x-mc)
(defface kmacro-x-mc-legacy-highlight-face
'((t (:inherit hi-yellow))) ;`highlight-regexp' hardcodes `hi-yellow'
"The face used by `kmacro-x-mc-legacy-region'.")
(defcustom kmacro-x-mc-legacy-region-sequence-fmt "C-s %s C-r RET 2*C-s RET"
"The sequence of keys used by `kmacro-x-mc-legacy-region'.
This sequence should search for the next occurrence of the query
and leave the region in a predictable state for the user to use.
The search query will be spliced into it using `format', and so
it should contain exactly one `%s' placeholder."
:type 'string)
;;;###autoload
(defun kmacro-x-mc-legacy-region (start end &optional highlight)
"Record a keyboard macro emulating multiple cursors.
The kmacro will first search for current region and then execute
the rest of the recorded kmacro. During the kmacro execution,
mark is always at the beginning of the match and point is at
the end.
START and END mark the region.
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 (mark)
(= 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))
(deactivate-mark)
(kmacro-push-ring)
(setq last-kbd-macro
(read-kbd-macro (format kmacro-x-mc-legacy-region-sequence-fmt
(format-kbd-macro query))))
(when highlight
(highlight-regexp (regexp-quote query)
'kmacro-x-mc-legacy-highlight-face))
(start-kbd-macro 'append 'no-exec)))
;;;###autoload
(defun kmacro-x-mc-legacy (&optional highlight)
"A more user friendly version of `kmacro-x-mc-legacy-region'.
If selection is active, it does the same thing as
`kmacro-x-mc-legacy-region'. If there is no active selection, it uses
the symbol at point instead.
See `kmacro-x-mc-legacy-region' for the HIGHLIGHT argument."
(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-legacy-region start end highlight))))
(provide 'kmacro-x-mc-legacy)
;;; kmacro-x-mc-legacy.el ends here