Skip to content

Commit

Permalink
some legacy vim code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mosheavni committed Dec 10, 2023
1 parent 57ed206 commit 65b4bfd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 71 deletions.
30 changes: 11 additions & 19 deletions .config/nvim/lua/user/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -402,28 +402,15 @@ function! RipGrepCWORD(bang, visualmode, ...) abort
let search_word = a:1
if a:visualmode
" Get the line and column of the visual selection marks
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
" Get all the lines represented by this range
let lines = getline(lnum1, lnum2)
" The last line might need to be cut if the visual selection didn't end on the last column
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
" The first line might need to be trimmed if the visual selection didn't start on the first column
let lines[0] = lines[0][col1 - 1:]
" Get the desired text
let search_word = join(lines, "\n")
let search_word = GetMotion('gv')
endif
if search_word ==? ''
let search_word = expand('<cword>')
endif
" Set bang command for literal search (no regexp expansion)
let search_message_literally = "for " . search_word
if a:bang == "!"
if a:bang == "!" || a:bang == v:true
let search_message_literally = "literally for " . search_word
let search_word = get(g:, 'grep_literal_flag', "") . ' ' . shellescape(search_word)
endif
Expand All @@ -435,11 +422,16 @@ function! RipGrepCWORD(bang, visualmode, ...) abort
let grepcmd = 'silent grep! ' . search_word
execute grepcmd
endfunction
command! -bang -range -nargs=? -complete=file_in_path RipGrepCWORD call RipGrepCWORD("<bang>", v:false, <q-args>)
command! -bang -range -nargs=? -complete=file_in_path RipGrepCWORDVisual call RipGrepCWORD("<bang>", v:true, <q-args>)
nnoremap <c-f> :RipGrepCWORD!<Space>
vnoremap <c-f> :RipGrepCWORDVisual!<cr>
]]
vim.api.nvim_create_user_command('RipGrepCWORD', function(f_opts)
vim.fn.RipGrepCWORD(f_opts.bang, false, f_opts.args)
end, { bang = true, range = true, nargs = '?', complete = 'file_in_path' })
vim.api.nvim_create_user_command('RipGrepCWORDVisual', function(f_opts)
vim.fn.RipGrepCWORD(f_opts.bang, true, f_opts.args)
end, { bang = true, range = true, nargs = '?', complete = 'file_in_path' })
vim.keymap.set({ 'n', 'v' }, '<C-f>', function()
return vim.fn.mode() == 'v' and ':RipGrepCWORDVisual!<cr>' or ':RipGrepCWORD!<Space>'
end, opts.no_remap_expr)

----------------------------
-- Sort Json Array by key --
Expand Down
30 changes: 0 additions & 30 deletions .config/nvim/lua/user/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,36 +192,6 @@ endfunction
nnoremap <silent> <F3> :call ExecuteFile()<CR>
]]

-- Helper functions
vim.cmd [[
function! GetVisualSelection() abort
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
let entire_selection = join(lines, "\n")
return entire_selection
endfunction
function! GetMotion(motion)
let saved_register = getreg('a')
defer setreg('a', saved_register)
exe 'normal! ' .. a:motion .. '"ay'
return @a
endfunction
function! ReplaceMotion(motion, text)
let saved_register = getreg('a')
defer setreg('a', saved_register)
let @a = a:text
exe 'normal! ' .. a:motion .. '"ap'
endfunction
]]

-- disable some builtin vim plugins
local default_plugins = {
'2html_plugin',
Expand Down
50 changes: 28 additions & 22 deletions .config/nvim/lua/user/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,35 @@ M.xnoremap = function(lhs, rhs, silent)
M.keymap('x', lhs, rhs, M.check_silent(silent, M.map_opts.no_remap))
end

local vim = vim
local api = vim.api
-- Helper functions
vim.cmd [[
function! GetVisualSelection() abort
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
let entire_selection = join(lines, "\n")
return entire_selection
endfunction
function M.get_selection()
-- does not handle rectangular selection
local s_start = vim.fn.getpos "'<"
local s_end = vim.fn.getpos "'>"
local n_lines = math.abs(s_end[2] - s_start[2]) + 1
if s_start[2] == 0 then
s_start[2] = 1
s_end[2] = 2
s_end[3] = 1
end
local lines = api.nvim_buf_get_lines(0, s_start[2] - 1, s_end[2], false)
-- return
lines[1] = string.sub(lines[1], s_start[3], -1)
if n_lines == 1 then
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3] - s_start[3] + 1)
else
lines[n_lines] = string.sub(lines[n_lines], 1, s_end[3])
end
return lines
end
function! GetMotion(motion)
let saved_register = getreg('a')
defer setreg('a', saved_register)
exe 'normal! ' .. a:motion .. '"ay'
return @a
endfunction
function! ReplaceMotion(motion, text)
let saved_register = getreg('a')
defer setreg('a', saved_register)
let @a = a:text
exe 'normal! ' .. a:motion .. '"ap'
endfunction
]]

M.pretty_print = function(message, title, icon)
if not icon then
Expand Down

0 comments on commit 65b4bfd

Please sign in to comment.