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) (save-excursion
(last-pos (point))) (if narf-mark-first
(unless (bound-and-true-p evil-iedit-state-local-minor-mode) (let ((i (if backwards-p (cdr narf-mark-index) (car narf-mark-index))))
(save-excursion (goto-char narf-mark-pt)
(funcall (intern (format "evil-visualstar/begin-search-%s" (if backwards-p "backward" "forward"))) (while (and (> i 0)
beg end)) (evil-ex-find-next nil (if backwards-p 'backward 'forward) t))
(when backwards-p (decf i))
(goto-char beg)) (if (> i 0)
(evil-visual-make-region beg end) (message "End of the buffer!")
(call-interactively 'evil-iedit-state/iedit-mode) (incf (if backwards-p
(save-excursion (cdr narf-mark-index)
(narf:iedit-restrict-to-region beg end))) (car narf-mark-index))))
(evil-ex-find-next nil (if backwards-p 'backward 'forward) t) (unless (iedit-find-current-occurrence-overlay)
;; (evil-ex-search-next) (iedit-toggle-selection)))
(evil-ex-nohighlight) (let* ((bounds (if (evil-visual-state-p)
(when (and (iedit-find-current-occurrence-overlay) (cons evil-visual-beginning evil-visual-end)
(if backwards-p t (not (= last-pos (point))))) (bounds-of-thing-at-point 'word)))
(iedit-toggle-selection)))) (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