summaryrefslogtreecommitdiff
path: root/.config/emacs
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-11-09 12:28:34 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-11-09 12:28:34 +0100
commitbdaa647365048b571dab3d5ee611b3a1f20da684 (patch)
treeee6df6f6cdb1e3f054223554587d6d736ad9dac6 /.config/emacs
parent721e4fb8e5f2353ade743f828bd30da705c0e371 (diff)
emacs: Add more commands for using multiple cursors
Diffstat (limited to '.config/emacs')
-rw-r--r--.config/emacs/editing.el46
-rw-r--r--.config/emacs/modules/mm-keybindings.el3
2 files changed, 48 insertions, 1 deletions
diff --git a/.config/emacs/editing.el b/.config/emacs/editing.el
index 9ff270d..72d5b48 100644
--- a/.config/emacs/editing.el
+++ b/.config/emacs/editing.el
@@ -226,4 +226,48 @@ This function is identical to `mm-search-forward-char' with N negated."
(prefix-numeric-value current-prefix-arg)))
(mm-search-forward-char char (- n)))
-(provide 'editing)
+(defun mm-add-cursor-to-next-thing (thing)
+ "Add a fake cursor to the next occurance of THING.
+THING is any symbol that can be given to ‘bounds-of-thing-at-point’.
+
+If there is an active region, the next THING will be marked."
+ (require 'multiple-cursors)
+ (let ((bounds (bounds-of-thing-at-point thing)))
+ (if (null bounds)
+ (progn
+ (forward-thing thing)
+ (goto-char (car (bounds-of-thing-at-point thing))))
+ (mc/save-excursion
+ (when (> (mc/num-cursors) 1)
+ (goto-char (overlay-end (mc/furthest-cursor-after-point))))
+ (goto-char (cdr (bounds-of-thing-at-point thing)))
+ (forward-thing thing)
+ (let ((bounds (bounds-of-thing-at-point thing)))
+ (goto-char (car bounds))
+ (when (use-region-p)
+ (push-mark (cdr bounds)))
+ (mc/create-fake-cursor-at-point))))))
+
+(defun mm-add-cursor-to-next-word ()
+ "Add a fake cursor to the next word."
+ (declare (interactive-only t))
+ (interactive)
+ (mm-add-cursor-to-next-thing 'word)
+ (mc/maybe-multiple-cursors-mode))
+
+(defun mm-transpose-cursor-regions (n)
+ "Interchange the regions of each cursor.
+With prefix arg N, the regions are rotated N places (backwards if N is
+negative)."
+ (interactive "p")
+ (require 'multiple-cursors)
+ (when (= (mc/num-cursors) 1)
+ (user-error "Cannot transpose with only one cursor."))
+ (unless (use-region-p)
+ (user-error "No active region."))
+ (setq mc--strings-to-replace
+ (funcall (if (< n 0) #'mm-rotate-left #'mm-rotate-right)
+ (abs n) (mc--ordered-region-strings)))
+ (mc--replace-region-strings))
+
+(provide 'editing) \ No newline at end of file
diff --git a/.config/emacs/modules/mm-keybindings.el b/.config/emacs/modules/mm-keybindings.el
index 4c9f29e..23ac64a 100644
--- a/.config/emacs/modules/mm-keybindings.el
+++ b/.config/emacs/modules/mm-keybindings.el
@@ -116,6 +116,9 @@ the first command is remapped to the second command."
"C-]" #'mm-search-forward-char
"M-]" #'mm-search-backward-char
+ "C-M-@" #'mm-add-cursor-to-next-word
+
+ "C-c c t" #'mm-transpose-cursor-regions
"C-c d" #'duplicate-dwim
"C-c t a" #'e/align-regexp
"C-c t f" #'fill-paragraph