diff --git a/.config/nvim/autoload/zeroknight/util.vim b/.config/nvim/autoload/zeroknight/util.vim deleted file mode 100644 index 2a241b3..0000000 --- a/.config/nvim/autoload/zeroknight/util.vim +++ /dev/null @@ -1,60 +0,0 @@ -" ZeroKnight's Misc Vim Functions - -" Check if buffer has an LSP client attached -function! zeroknight#util#has_lsp(...) abort - let buffer = get(a:, 1, 0) - return luaeval('not vim.tbl_isempty(vim.lsp.buf_get_clients(_A))', buffer) -endfunction - -" Save and execute file -function! zeroknight#util#save_and_exec() abort - write - if &filetype ==# 'vim' - source % - elseif &filetype ==# 'lua' - luafile % - else - echo 'No execution strategy for filetype' &filetype - endif -endfunction - -" Trim trailing whitespace while preserving buffer state -function! zeroknight#util#TrimTrailingSpace() abort - let trimmed = map(nvim_buf_get_lines(0, 0, -1, v:true), {_, v -> substitute(v, '\s\+$', '', '')}) - call nvim_buf_set_lines(0, 0, -1, v:true, trimmed) -endfunction - -" Redirect the output of a Vim command into a Scratch buffer -function! zeroknight#util#Redir(split_type, ...) abort - if a:0 < 1 - echoerr 'Not enough arguments. Must specify split_type, and a command with optional arguments.' - return - endif - redir => l:output - if a:1 =~# '^!' - " If given a :! command, run it through system(), as execute() on a :! - " command doesn't return the proper output - let l:args = deepcopy(a:000) - let l:args[0] = substitute(l:args[0], '^!', '', '') - echo system(join(l:args)) - else - call execute(join(a:000)) - endif - redir END - if a:split_type ==# 'vertical' - vnew - elseif a:split_type ==# 'tab' - tabnew - else - new - endif - setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile - call setline(1, split(l:output, "\n")) -endfunction - -function! zeroknight#util#Pcol(...) abort - let a:above = get(a:, 1, 0) - let l:col = virtcol('.') - execute 'normal!' a:above ? 'P' : 'p' - call cursor('.', l:col) -endfunction diff --git a/.config/nvim/lua/zeroknight/config/keymaps.lua b/.config/nvim/lua/zeroknight/config/keymaps.lua index 83fece1..606c763 100644 --- a/.config/nvim/lua/zeroknight/config/keymaps.lua +++ b/.config/nvim/lua/zeroknight/config/keymaps.lua @@ -96,7 +96,7 @@ wk.register({ L = { vim.diagnostic.setloclist, 'Dump diagnostics to location list' }, Q = { vim.diagnostic.setqflist, 'Dump diagnostics to quickfix list' }, }, - wx = { 'call zeroknight#util#save_and_exec()', 'Write and Execute' }, + wx = { 'write | source %', 'Write and Execute' }, y = { '"+y', 'Yank to clipboard', mode = { 'n', 'v' } }, Y = { '"+Y', 'Yank til EOL to clipboard' }, }, { prefix = '' }) @@ -106,7 +106,7 @@ wk.register({ wk.register({ K = { 'K', 'Run keywordprg on word' }, r = { - s = { 'call zeroknight#util#TrimTrailingSpace()', 'Trim trailing spaces' }, + s = { util.trim_whitespace, 'Trim trailing spaces' }, w = { ':%s/\\<\\>//gI', 'Substitute cursor word' }, W = { ':%s/\\<\\>//gI', 'Substitute cursor WORD' }, }, diff --git a/.config/nvim/lua/zeroknight/util/init.lua b/.config/nvim/lua/zeroknight/util/init.lua index e4b282c..fc63b31 100644 --- a/.config/nvim/lua/zeroknight/util/init.lua +++ b/.config/nvim/lua/zeroknight/util/init.lua @@ -115,6 +115,15 @@ function M.toggle_background() end end +-- Trim trailing whitespace in buffer while preserving state/position +function M.trim_whitespace(buffer) + buffer = buffer or 0 + local trimmed = vim.tbl_map(function(x) + return x:gsub('%s+$', '') + end, vim.api.nvim_buf_get_lines(buffer, 0, -1, true)) + vim.api.nvim_buf_set_lines(buffer, 0, -1, true, trimmed) +end + -- From LazyVim -- returns the root directory based on: -- * lsp workspace folders