Add multiple cursors implementation with iedit

This commit is contained in:
Henrik Lissner 2016-02-19 12:33:16 -05:00
parent bceebeca1e
commit 39c4700b3b
2 changed files with 55 additions and 21 deletions

View file

@ -13,33 +13,67 @@
(evil-previous-line)) (evil-previous-line))
(call-interactively 'evil-ret))) (call-interactively 'evil-ret)))
;; Many packages try to do the multiple-cursors thing, but when it comes to
;; multiple-cursors and evil-mode, things get complicated. `evil-mc' doesn't seem stable
;; enough (I haven't been able to get it to work with my setup), `multiple-cursors'
;; doesn't work well with evil's vimmish opinion of where the cursor is...
;;
;; So, this is my solution: using `iedit' and `evil-iedit-mode', I wrote
;; `narf/mark-and-next' and `narf/mark-and-prev', and it works like a charm! When I want
;; "match all" functionality, I use `evil-iedit-mode/iedit-mode' while in visual mode and
;; I get just that.
(defvar narf-mark-first nil)
(defvar narf-mark-pt nil)
(defvar narf-mark-index '(1 . 1))
(defun narf|mark-end ()
(setq narf-mark-first nil
narf-mark-pt nil
narf-mark-index (cons 1 1)))
(add-hook! (iedit-mode-end-hook iedit-aborting) 'narf|mark-end)
;;;###autoload ;;;###autoload
(defun narf/mark-and-prev () (defun narf/mark-and-prev ()
"See `narf/mark-and-next'"
(interactive) (interactive)
(narf/mark-and-next t)) (narf/mark-and-next t))
;;;###autoload ;;;###autoload
(defun narf/mark-and-next (&optional backwards-p) (defun narf/mark-and-next (&optional backwards-p)
"Emulates Sublime Text's (and Atom's) multiple cursors functionality by marking the
current word/selection and marking the next one on consecutive executions of this
function."
(interactive) (interactive)
(let ((beg evil-visual-beginning) (setq evil-ex-search-direction (if backwards-p 'backward 'forward))
(end evil-visual-end)
(last-pos (point)))
(unless (bound-and-true-p evil-iedit-state-local-minor-mode)
(save-excursion (save-excursion
(funcall (intern (format "evil-visualstar/begin-search-%s" (if backwards-p "backward" "forward"))) (if narf-mark-first
beg end)) (let ((i (if backwards-p (cdr narf-mark-index) (car narf-mark-index))))
(when backwards-p (goto-char narf-mark-pt)
(goto-char beg)) (while (and (> i 0)
(evil-visual-make-region beg end) (evil-ex-find-next nil (if backwards-p 'backward 'forward) t))
(call-interactively 'evil-iedit-state/iedit-mode) (decf i))
(save-excursion (if (> i 0)
(narf:iedit-restrict-to-region beg end))) (message "End of the buffer!")
(evil-ex-find-next nil (if backwards-p 'backward 'forward) t) (incf (if backwards-p
;; (evil-ex-search-next) (cdr narf-mark-index)
(evil-ex-nohighlight) (car narf-mark-index))))
(when (and (iedit-find-current-occurrence-overlay) (unless (iedit-find-current-occurrence-overlay)
(if backwards-p t (not (= last-pos (point))))) (iedit-toggle-selection)))
(iedit-toggle-selection)))) (let* ((bounds (if (evil-visual-state-p)
(cons evil-visual-beginning evil-visual-end)
(bounds-of-thing-at-point 'word)))
(beg (car bounds))
(end (cdr bounds))
(occurrence (buffer-substring-no-properties (car bounds) (cdr bounds))))
(setq narf-mark-first (if backwards-p beg end)
narf-mark-pt (if backwards-p beg end))
(evil-normal-state)
(setq iedit-initial-string-local occurrence)
(iedit-start (iedit-regexp-quote occurrence) beg end)
(evil-iedit-state)
(setq evil-ex-search-pattern (evil-ex-make-search-pattern (regexp-quote occurrence)))
(evil-ex-find-next nil nil t)))))
(provide 'defuns-iedit) (provide 'defuns-iedit)
;;; defuns-iedit.el ends here ;;; defuns-iedit.el ends here

View file

@ -258,8 +258,8 @@
;; iedit and my own version of multiple-cursors ;; iedit and my own version of multiple-cursors
:v "R" 'evil-iedit-state/iedit-mode ; edit all instances of marked region :v "R" 'evil-iedit-state/iedit-mode ; edit all instances of marked region
:v "M-d" 'narf/mark-and-next :nv "M-d" 'narf/mark-and-next
:v "M-D" 'narf/mark-and-prev :nv "M-D" 'narf/mark-and-prev
;; aliases for % ;; aliases for %
:m "%" 'evilmi-jump-items :m "%" 'evilmi-jump-items