├── README.md └── evil-visualstar.el /README.md: -------------------------------------------------------------------------------- 1 | evil-visualstar 2 | =============== 3 | 4 | This is a port of one of the many visual-star plugins for Vim to work with [evil-mode](https://bitbucket.org/lyro/evil/wiki/Home). 5 | 6 | installation 7 | ============ 8 | 9 | Install `evil-visualstar` from [MELPA][1]. 10 | 11 | usage 12 | ===== 13 | 14 | Add `(global-evil-visualstar-mode)` to your configuration. 15 | 16 | Make a visual selection with `v` or `V`, and then hit `*` to search that selection forward, or `#` to search that selection backward. 17 | 18 | If the `evil-visualstar/persistent` option is not nil, visual-state will 19 | remain in effect, allowing for repeated `*` or `#`. 20 | 21 | Note that you than have to exit visualstar-mode before hitting a 22 | direction key to avoid extending the selection. 23 | 24 | [1]: http://melpa.org 25 | -------------------------------------------------------------------------------- /evil-visualstar.el: -------------------------------------------------------------------------------- 1 | ;;; evil-visualstar.el --- Starts a * or # search from the visual selection 2 | 3 | ;; Copyright (C) 2013 by Bailey Ling 4 | ;; Author: Bailey Ling 5 | ;; URL: https://github.com/bling/evil-visualstar 6 | ;; Filename: evil-visualstar.el 7 | ;; Description: Starts a * or # search from the visual selection 8 | ;; Created: 2013-09-24 9 | ;; Version: 0.2.0 10 | ;; Keywords: evil vim visualstar 11 | ;; Package-Requires: ((evil "0")) 12 | ;; 13 | ;; This file is not part of GNU Emacs. 14 | ;; 15 | ;; This program is free software; you can redistribute it and/or 16 | ;; modify it under the terms of the GNU General Public License as 17 | ;; published by the Free Software Foundation; either version 3, or 18 | ;; (at your option) any later version. 19 | ;; 20 | ;; This program is distributed in the hope that it will be useful, 21 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 | ;; General Public License for more details. 24 | ;; 25 | ;; You should have received a copy of the GNU General Public License 26 | ;; along with this program; see the file COPYING. If not, write to 27 | ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth 28 | ;; Floor, Boston, MA 02110-1301, USA. 29 | ;; 30 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 31 | 32 | ;;; Commentary: 33 | ;; 34 | ;; Install: 35 | ;; (require 'evil-visualstar) 36 | ;; 37 | ;; Usage: 38 | ;; 39 | ;; (global-evil-visualstar-mode t) 40 | ;; 41 | ;; Make a visual selection with `v` or `V`, and then hit `*` to search 42 | ;; the selection forward, or # to search that selection backward. 43 | ;; 44 | ;; If the evil-visualstar/persistent option is not nil, visual-state 45 | ;; will remain in effect, allowing for repeated * or #. 46 | 47 | ;;; Code: 48 | 49 | (require 'evil) 50 | 51 | (defgroup evil-visualstar nil 52 | "evil-visualstar configuration options." 53 | :prefix "evil-visualstar" 54 | :group 'evil) 55 | 56 | (defcustom evil-visualstar/persistent nil 57 | "Set to `t` if `*` and `#` should keep visual-mode. 58 | That would visually-select the found occurrence, allowing for 59 | repeated searches. 60 | You will need to hit escape to leave visual-mode." 61 | :group 'evil-visualstar 62 | :type 'boolean) 63 | 64 | (defun evil-visualstar/begin-search (beg end direction) 65 | (when (evil-visual-state-p) 66 | (evil-exit-visual-state) 67 | (let ((found) 68 | (selection (regexp-quote (buffer-substring-no-properties beg end)))) 69 | (if (eq evil-search-module 'isearch) 70 | (progn 71 | (setq isearch-forward direction) 72 | (setq found (evil-search selection direction t))) 73 | (let ((pattern (evil-ex-make-search-pattern selection)) 74 | (direction (if direction 'forward 'backward))) 75 | (setq evil-ex-search-direction direction) 76 | (setq evil-ex-search-pattern pattern) 77 | (evil-ex-search-activate-highlight pattern) 78 | ;; update search history unless this pattern equals the 79 | ;; previous pattern 80 | (unless (equal (car-safe evil-ex-search-history) selection) 81 | (push selection evil-ex-search-history)) 82 | (evil-push-search-history selection (eq direction 'forward)) 83 | (setq found (evil-ex-search-next)))) 84 | (when (and evil-visualstar/persistent found) 85 | (push-mark (+ (point) (- end beg)) nil t))))) 86 | 87 | (evil-define-motion evil-visualstar/begin-search-forward (beg end) 88 | "Search for the visual selection forwards." 89 | :jump t 90 | :repeat nil 91 | (interactive "") 92 | (evil-visualstar/begin-search beg end t)) 93 | 94 | (evil-define-motion evil-visualstar/begin-search-backward (beg end) 95 | "Search for the visual selection backwards." 96 | :jump t 97 | :repeat nil 98 | (interactive "") 99 | (evil-visualstar/begin-search beg end nil)) 100 | 101 | ;;;###autoload 102 | (define-minor-mode evil-visualstar-mode 103 | "Minor mode for visual star selection." 104 | :keymap (let ((map (make-sparse-keymap))) 105 | (evil-define-key 'visual map (kbd "*") #'evil-visualstar/begin-search-forward) 106 | (evil-define-key 'visual map (kbd "#") #'evil-visualstar/begin-search-backward) 107 | map) 108 | (evil-normalize-keymaps)) 109 | 110 | ;;;###autoload 111 | (define-globalized-minor-mode global-evil-visualstar-mode 112 | evil-visualstar-mode turn-on-evil-visualstar-mode) 113 | 114 | ;;;###autoload 115 | (defun turn-on-evil-visualstar-mode () 116 | "Turns on visual star selection." 117 | (interactive) 118 | (evil-visualstar-mode t)) 119 | 120 | ;;;###autoload 121 | (defun turn-off-evil-visualstar-mode () 122 | "Turns off visual star selection." 123 | (interactive) 124 | (evil-visualstar-mode -1)) 125 | 126 | (provide 'evil-visualstar) 127 | ;;; evil-visualstar.el ends here 128 | --------------------------------------------------------------------------------