├── .stylua.toml ├── LICENSE ├── README.md └── lua └── nvim-cursorline.lua /.stylua.toml: -------------------------------------------------------------------------------- 1 | indent_type = "Spaces" 2 | indent_width = 2 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 yamatsum 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 | # nvim-cursorline 2 | 3 | Highlight words and lines on the cursor for Neovim 4 | 5 | - Underlines the word under the cursor. 6 | 7 | https://user-images.githubusercontent.com/42740055/163586251-15c7c709-86bd-4c17-a298-07681bada220.mp4 8 | 9 | - Show / hide cursorline in connection with cursor moving. 10 | 11 | https://user-images.githubusercontent.com/42740055/163586272-17560f83-9195-4cb4-8c1c-557cfaf775ea.mp4 12 | 13 | ## Installation 14 | 15 | Install with your favorite plugin manager. 16 | 17 | ## Usage 18 | 19 | ```lua 20 | require('nvim-cursorline').setup { 21 | cursorline = { 22 | enable = true, 23 | timeout = 1000, 24 | number = false, 25 | }, 26 | cursorword = { 27 | enable = true, 28 | min_length = 3, 29 | hl = { underline = true }, 30 | } 31 | } 32 | ``` 33 | 34 | ## Acknowledgments 35 | 36 | Thanks goes to these people/projects for inspiration: 37 | 38 | - [delphinus/vim-auto-cursorline](https://github.com/delphinus/vim-auto-cursorline) 39 | - [itchyny/vim-cursorword](https://github.com/itchyny/vim-cursorword) 40 | 41 | ## License 42 | 43 | This software is released under the MIT License, see LICENSE. 44 | -------------------------------------------------------------------------------- /lua/nvim-cursorline.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | local w = vim.w 4 | local a = vim.api 5 | local wo = vim.wo 6 | local fn = vim.fn 7 | local hl = a.nvim_set_hl 8 | local au = a.nvim_create_autocmd 9 | local timer = vim.loop.new_timer() 10 | 11 | local DEFAULT_OPTIONS = { 12 | cursorline = { 13 | enable = true, 14 | timeout = 1000, 15 | number = false, 16 | }, 17 | cursorword = { 18 | enable = true, 19 | min_length = 3, 20 | hl = { underline = true }, 21 | }, 22 | } 23 | 24 | local function matchadd() 25 | local column = a.nvim_win_get_cursor(0)[2] 26 | local line = a.nvim_get_current_line() 27 | local cursorword = fn.matchstr(line:sub(1, column + 1), [[\k*$]]) 28 | .. fn.matchstr(line:sub(column + 1), [[^\k*]]):sub(2) 29 | 30 | if cursorword == w.cursorword then 31 | return 32 | end 33 | w.cursorword = cursorword 34 | if w.cursorword_id then 35 | vim.call("matchdelete", w.cursorword_id) 36 | w.cursorword_id = nil 37 | end 38 | if 39 | cursorword == "" 40 | or #cursorword > 100 41 | or #cursorword < M.options.cursorword.min_length 42 | or string.find(cursorword, "[\192-\255]+") ~= nil 43 | then 44 | return 45 | end 46 | local pattern = [[\<]] .. cursorword .. [[\>]] 47 | w.cursorword_id = fn.matchadd("CursorWord", pattern, -1) 48 | end 49 | 50 | function M.setup(options) 51 | M.options = vim.tbl_deep_extend("force", DEFAULT_OPTIONS, options or {}) 52 | 53 | if M.options.cursorline.enable then 54 | wo.cursorline = true 55 | au("WinEnter", { 56 | callback = function() 57 | wo.cursorline = true 58 | end, 59 | }) 60 | au("WinLeave", { 61 | callback = function() 62 | wo.cursorline = false 63 | end, 64 | }) 65 | au({ "CursorMoved", "CursorMovedI" }, { 66 | callback = function() 67 | if M.options.cursorline.number then 68 | wo.cursorline = false 69 | else 70 | wo.cursorlineopt = "number" 71 | end 72 | timer:start( 73 | M.options.cursorline.timeout, 74 | 0, 75 | vim.schedule_wrap(function() 76 | if M.options.cursorline.number then 77 | wo.cursorline = true 78 | else 79 | wo.cursorlineopt = "both" 80 | end 81 | end) 82 | ) 83 | end, 84 | }) 85 | end 86 | 87 | if M.options.cursorword.enable then 88 | au("VimEnter", { 89 | callback = function() 90 | hl(0, "CursorWord", M.options.cursorword.hl) 91 | matchadd() 92 | end, 93 | }) 94 | au({ "CursorMoved", "CursorMovedI" }, { 95 | callback = function() 96 | matchadd() 97 | end, 98 | }) 99 | end 100 | end 101 | 102 | M.options = nil 103 | 104 | return M 105 | --------------------------------------------------------------------------------