From 00c9060c2f0ec581db8841aa34fd92c0e9953693 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 Dec 2019 17:09:52 +0000 Subject: feat(emacs.d): Introduce custom mc/mark-dwim cursor marker Adds a "do what I mean" multiple-cursor selection with the logic that I find most useful: * If there is no active region, mark the next line (or lines, based on prefix argument) * If there is an active region that spans multiple lines, call `mc/edit-lines` * If there is an active region on a single line, trigger a custom selection hydra with functionality equivalent to `mc/mark-more-like-this-extended` but a slightly improved user experience Hopefully this will make it easier to get into the habit of actually using multiple-cursors without calling the mc commands via M-x --- tools/emacs/config/bindings.el | 1 + tools/emacs/config/functions.el | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tools/emacs/config/bindings.el b/tools/emacs/config/bindings.el index 626bdcea8eb2..502750aa9df6 100644 --- a/tools/emacs/config/bindings.el +++ b/tools/emacs/config/bindings.el @@ -24,6 +24,7 @@ ;; Miscellaneous editing commands (global-set-key (kbd "C-c w") 'whitespace-cleanup) (global-set-key (kbd "C-c a") 'align-regexp) +(global-set-key (kbd "C-c m") 'mc/mark-dwim) ;; Browse URLs (very useful for Gitlab's SSH output!) (global-set-key (kbd "C-c b p") 'browse-url-at-point) diff --git a/tools/emacs/config/functions.el b/tools/emacs/config/functions.el index a6cc07e88963..e50950a8ef9e 100644 --- a/tools/emacs/config/functions.el +++ b/tools/emacs/config/functions.el @@ -182,6 +182,33 @@ Including indent-buffer, which should not be called automatically on save." (inferior-erlang (format "nix-shell --command erl %s" (cdr (project-current))))) +(defhydra mc/mark-more-hydra (:color pink) + ("" mmlte--up "Mark previous like this") + ("" mc/mmlte--down "Mark next like this") + ("" mc/mmlte--left (if (eq mc/mark-more-like-this-extended-direction 'up) + "Skip past the cursor furthest up" + "Remove the cursor furthest down")) + ("" mc/mmlte--right (if (eq mc/mark-more-like-this-extended-direction 'up) + "Remove the cursor furthest up" + "Skip past the cursor furthest down")) + ("f" nil "Finish selecting")) + +;; Mute the message that mc/mmlte wants to print on its own +(advice-add 'mc/mmlte--message :around (lambda (&rest args) (ignore))) + +(defun mc/mark-dwim (arg) + "Select multiple things, but do what I mean." + + (interactive "p") + (if (not (region-active-p)) (mc/mark-next-lines arg) + (if (< 1 (count-lines (region-beginning) + (region-end))) + (mc/edit-lines arg) + ;; The following is almost identical to `mc/mark-more-like-this-extended', + ;; but uses a hydra (`mc/mark-more-hydra') instead of a transient key map. + (mc/mmlte--down) + (mc/mark-more-hydra/body)))) + (defun memespace-region () "Make a meme out of it." -- cgit 1.4.1