├── .gitignore ├── LICENSE ├── README.md ├── autoload └── cursorword.vim ├── doc └── cursorword.txt └── plugin └── cursorword.vim /.gitignore: -------------------------------------------------------------------------------- 1 | /doc/tags 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2022 itchyny 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-cursorword 2 | ## Underlines the word under the cursor 3 | ![vim-cursorword](https://raw.githubusercontent.com/wiki/itchyny/vim-cursorword/image/image.gif) 4 | 5 | ## Installation 6 | Install with your favorite plugin manager. 7 | 8 | ## Author 9 | itchyny (https://github.com/itchyny) 10 | 11 | ## License 12 | This software is released under the MIT License, see LICENSE. 13 | -------------------------------------------------------------------------------- /autoload/cursorword.vim: -------------------------------------------------------------------------------- 1 | " ============================================================================= 2 | " Filename: autoload/cursorword.vim 3 | " Author: itchyny 4 | " License: MIT License 5 | " Last Change: 2022/11/17 08:57:47. 6 | " ============================================================================= 7 | 8 | let s:save_cpo = &cpo 9 | set cpo&vim 10 | 11 | function! cursorword#highlight() abort 12 | if !get(g:, 'cursorword_highlight', 1) | return | endif 13 | highlight default CursorWord term=underline cterm=underline gui=underline 14 | endfunction 15 | 16 | let s:alphabets = '^[\x00-\x7f\xb5\xc0-\xd6\xd8-\xf6\xf8-\u01bf\u01c4-\u02af\u0370-\u0373\u0376\u0377\u0386-\u0481\u048a-\u052f]\+$' 17 | 18 | function! cursorword#matchadd(...) abort 19 | let enable = get(b:, 'cursorword', get(g:, 'cursorword', 1)) && !has('vim_starting') 20 | if !enable && !get(w:, 'cursorword_match') | return | endif 21 | let i = (a:0 ? a:1 : mode() ==# 'i' || mode() ==# 'R') && col('.') > 1 22 | let line = getline('.') 23 | let word = matchstr(line[:(col('.')-i-1)], '\k*$') . matchstr(line[(col('.')-i-1):], '^\k*')[1:] 24 | if get(w:, 'cursorword_state', []) ==# [ word, enable ] | return | endif 25 | let w:cursorword_state = [ word, enable ] 26 | if get(w:, 'cursorword_match') 27 | silent! call matchdelete(w:cursorword_id) 28 | endif 29 | let w:cursorword_match = 0 30 | if !enable || word ==# '' || len(word) !=# strchars(word) && word !~# s:alphabets || len(word) > 1000 | return | endif 31 | let pattern = '\<' . escape(word, '~"\.^$[]*') . '\>' 32 | let w:cursorword_id = matchadd('CursorWord', pattern, -100) 33 | let w:cursorword_match = 1 34 | endfunction 35 | 36 | let s:delay = get(g:, 'cursorword_delay', 50) 37 | if has('timers') && s:delay > 0 38 | let s:timer = 0 39 | function! cursorword#cursormoved() abort 40 | if get(w:, 'cursorword_match') 41 | silent! call matchdelete(w:cursorword_id) 42 | let w:cursorword_match = 0 43 | let w:cursorword_state = [] 44 | endif 45 | call timer_stop(s:timer) 46 | let s:timer = timer_start(s:delay, 'cursorword#timer_callback') 47 | endfunction 48 | function! cursorword#timer_callback(...) abort 49 | call cursorword#matchadd() 50 | endfunction 51 | else 52 | function! cursorword#cursormoved() abort 53 | call cursorword#matchadd() 54 | endfunction 55 | endif 56 | 57 | let &cpo = s:save_cpo 58 | unlet s:save_cpo 59 | -------------------------------------------------------------------------------- /doc/cursorword.txt: -------------------------------------------------------------------------------- 1 | *cursorword.txt* Underlines the word under the cursor 2 | 3 | Author: itchyny (https://github.com/itchyny) 4 | License: MIT License 5 | Repository: https://github.com/itchyny/vim-cursorword 6 | Last Change: 2022/11/17 18:55:06. 7 | 8 | CONTENTS *cursorword-contents* 9 | 10 | Introduction |cursorword-introduction| 11 | Options |cursorword-options| 12 | Changelog |cursorword-changelog| 13 | 14 | ============================================================================== 15 | INTRODUCTION *cursorword-introduction* 16 | This *cursorword* plugin underlines the word under the cursor. That's it. 17 | 18 | While we are coding, we always pay attention to the variable under the cursor. 19 | Where did we define the variable? In which statement do we use the variable? 20 | Searching the variable using |star| or |#| is a simple solution to highlight all 21 | the places the variable is used. However, it costs too much time to highlight 22 | a word, move around to check the places and clear the highlight. This plugin 23 | |cursorword| provides you the modest underlines for the variables under the 24 | cursor to let you know all the places the variable is used at a glance without 25 | stopping coding. 26 | 27 | The brightest plugin (https://github.com/osyo-manga/vim-brightest) is a nice 28 | plugin to fulfill highlights of the word under the cursor. However, it uses 29 | too long codes and is too slow. I had been using the plugin for a while but I 30 | could not bear the slowness caused by brightest on the cursor motions. This is 31 | why I created |cursorword|. I think that a plugin which is running all the time 32 | should be coded with the performance efficiency in mind. When we publish a 33 | plugin which stays running in all buffers, we should be aware that it will use 34 | much resource of all the users. 35 | 36 | The code of |cursorword| is very tiny. It's very tiny that |cursorword| runs ten 37 | times faster than brightest. Instead of its efficiency, |cursorword| is totally 38 | unconfigurable. But you will find this plugin just comfortable; the modest 39 | underlines are enough for us. A good configurable plugin would have many 40 | options that users can configure almost all the features. But be relaxed and 41 | imagine, do we need such a lot of options for a plugin to highlight the word 42 | under the cursor? Most people would never need such a lot of options. We 43 | should not implement features for a few power users at the sacrifice of the 44 | performance of the software. Keep your software small for the happiness of 45 | most users. Too much configurability makes your software dirty and causes 46 | unwanted slowness on many users. We have to rethink what good configurability 47 | is and for what kind of software such configurability is required. 48 | 49 | ============================================================================== 50 | OPTIONS *cursorword-options* 51 | 52 | g:cursorword *g:cursorword* 53 | Set this variable to 0 to disable this plugin globally. 54 | 55 | b:cursorword *b:cursorword* 56 | If you set this variable to 0, the plugin stops highlighting 57 | the word under the cursor in the buffer. This variable has 58 | priority over |g:cursorword|. 59 | 60 | g:cursorword_highlight *g:cursorword_highlight* 61 | Set this variable to 0 to disable the default highlighting 62 | style. If you set this variable to 0, you need to define your 63 | own style for the CursorWord |highlight-groups|. 64 | 65 | g:cursorword_delay *g:cursorword_delay* 66 | The delay duration in milliseconds for setting the word 67 | highlight after cursor motions. When the value is set to 0, the 68 | plugin highlights the word synchronously, but there is a 69 | performance sacrifice. The default value is 50. 70 | 71 | ============================================================================== 72 | vim:tw=78:sw=4:ts=8:ft=help:norl:noet: 73 | -------------------------------------------------------------------------------- /plugin/cursorword.vim: -------------------------------------------------------------------------------- 1 | " ============================================================================= 2 | " Filename: plugin/cursorword.vim 3 | " Author: itchyny 4 | " License: MIT License 5 | " Last Change: 2020/05/01 19:05:29. 6 | " ============================================================================= 7 | 8 | if exists('g:loaded_cursorword') || v:version < 703 9 | finish 10 | endif 11 | let g:loaded_cursorword = 1 12 | 13 | let s:save_cpo = &cpo 14 | set cpo&vim 15 | 16 | augroup cursorword 17 | autocmd! 18 | if has('vim_starting') 19 | autocmd VimEnter * call cursorword#highlight() | 20 | \ autocmd cursorword WinEnter,BufEnter * call cursorword#matchadd() 21 | else 22 | call cursorword#highlight() 23 | autocmd WinEnter,BufEnter * call cursorword#matchadd() 24 | endif 25 | autocmd ColorScheme * call cursorword#highlight() 26 | autocmd CursorMoved,CursorMovedI * call cursorword#cursormoved() 27 | autocmd InsertEnter * call cursorword#matchadd(1) 28 | autocmd InsertLeave * call cursorword#matchadd(0) 29 | augroup END 30 | 31 | let &cpo = s:save_cpo 32 | unlet s:save_cpo 33 | --------------------------------------------------------------------------------