Skip to content

Commit

Permalink
feat(plugins): add internal cursorword plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ecosse3 committed Aug 8, 2022
1 parent eaddca7 commit 098db24
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require('colorscheme')
require('keymappings')
require('autocmds')
require('functions')
require('internal.cursorword')

require('lsp.config')
require('lsp.setup')
Expand Down
81 changes: 81 additions & 0 deletions lua/internal/cursorword.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
local function highlight_cursorword()
if vim.g.cursorword_highlight ~= false then
vim.cmd('highlight CursorWord term=underline cterm=underline gui=underline')
end
end

local function disable_cursorword()
local disable_ft = {
['alpha'] = true,
['lspsagafinder'] = true,
['NeogitStatus'] = true,
['text'] = true,
}
if not disable_ft[vim.bo.ft] then
return
end
if vim.w.cursorword_id ~= 0 and vim.w.cursorword_id ~= nil and vim.w.cursorword_match ~= 0 then
vim.fn.matchdelete(vim.w.cursorword_id)
vim.w.cursorword_id = nil
vim.w.cursorword_match = nil
vim.w.cursorword = nil
end
end

local function matchadd()
local disable_ft = {
['alpha'] = true,
['NvimTree'] = true,
['lspsagafinder'] = true,
['dashboard'] = true,
}
if disable_ft[vim.bo.ft] then
return
end

if vim.api.nvim_get_mode().mode == 'i' then
return
end

local column = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local cursorword = vim.fn.matchstr(line:sub(1, column + 1), [[\k*$]])
.. vim.fn.matchstr(line:sub(column + 1), [[^\k*]]):sub(2)

if cursorword == vim.w.cursorword then
return
end
vim.w.cursorword = cursorword
if vim.w.cursorword_match == 1 then
vim.call('matchdelete', vim.w.cursorword_id)
end
vim.w.cursorword_match = 0
if cursorword == ''
or #cursorword > 100
or #cursorword < 3
or string.find(cursorword, '[\192-\255]+') ~= nil
then
return
end
local pattern = [[\<]] .. cursorword .. [[\>]]
vim.w.cursorword_id = vim.fn.matchadd('CursorWord', pattern, -1)
vim.w.cursorword_match = 1
end

local function cursor_moved()
if vim.api.nvim_get_mode().mode == 'n' then
matchadd()
end
end

highlight_cursorword()

vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
pattern = '*',
callback = cursor_moved,
})

vim.api.nvim_create_autocmd({ 'InsertEnter', 'BufWinEnter' }, {
pattern = '*',
callback = disable_cursorword,
})

0 comments on commit 098db24

Please sign in to comment.