Skip to content

Commit

Permalink
improve search and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
mosheavni committed Jan 30, 2025
1 parent bbce1d1 commit 3c43747
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 83 deletions.
10 changes: 3 additions & 7 deletions nvim/.config/nvim/lua/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ function _G.P(v, r)
return v
end

local original_print = vim.print
local original_vim_print = vim.print
vim.print = function(...)
local args = { ... }
if #args == 1 and type(args[1]) == 'table' then
original_print(vim.inspect(args[1]))
else
original_print(...)
end
local str = type(...) == 'table' and vim.inspect(...) or ...
original_vim_print(str)
end

---Write a temporary file with specified options
Expand Down
114 changes: 38 additions & 76 deletions nvim/.config/nvim/lua/user/search-replace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@ endif
let g:loaded_search_replace = 1

let s:chars = ['/', '?', '#', ':', '@']
let s:magic_list = ['\v', '\m', '\M', '\V', '']
let s:available_flags = ['g', 'c', 'i']

function! s:should_sar()
if getcmdtype() !=# ':' || get(g:, 'sar_active', v:false) == v:false
return v:false
endif
return v:true
function! s:should_sar() abort
return getcmdtype() ==# ':' && get(g:, 'sar_active', v:false)
endfunction

function! s:find_unique_char(chars, str)
" Iterate through each character in the list
for char in a:chars
" Check if the character is not in the string
if stridx(a:str, char) == -1
return char
endif
endfor
" If all characters are found in the string, return an appropriate value
return ''
function! s:find_unique_char(chars, str) abort
for char in a:chars
if stridx(a:str, char) == -1
return char
endif
endfor
return ''
endfunction

function! SarPopulateSearchline(mode)
if a:mode ==# 'n'
let g:sar_cword = expand('<cword>')
else
let g:sar_cword = GetMotion('gv')
endif
" Define a list of characters
function! SarPopulateSearchline(mode) abort
let g:sar_cword = a:mode ==# 'n' ? expand('<cword>') : GetMotion('gv')
let g:sar_sep = s:find_unique_char(s:chars, g:sar_cword)
let g:sar_magic = '\V'
let cmd = '.,$s' . g:sar_sep
Expand All @@ -45,7 +36,7 @@ endfunction
nnoremap <leader>r :<C-\>eSarPopulateSearchline('n')<CR>
vnoremap <leader>r :<C-\>eSarPopulateSearchline('v')<CR>
func SarToggleChar(char)
function! SarToggleChar(char) abort
let cmd = getcmdline()
if !s:should_sar()
return cmd
Expand All @@ -55,10 +46,8 @@ func SarToggleChar(char)
let cmd_splitted = split(cmd, sep, 1)
let cmd_flags = cmd_splitted[-1]
let cmd_pos = getcmdpos()
let available_flags = ['g', 'c', 'i']

if cmd_flags =~ a:char
" remove the flag
let new_flags = substitute(cmd_flags, a:char, '', '')
else
" add the flag
Expand All @@ -72,107 +61,81 @@ func SarToggleChar(char)

let cmd_splitted[-1] = new_flags
let cmd = join(cmd_splitted, sep)
" let cmd = cmd[:-len(cmd_flags) - 1] . new_flags

" place the cursor on the )
call setcmdpos(cmd_pos)
return cmd
endfunc
endfunction

func SarToggleReplaceTerm()
function! SarToggleReplaceTerm() abort
let cmd = getcmdline()
if !s:should_sar()
return cmd
endif

let sep = get(g:, 'sar_sep', '/')
let cmd_splitted = split(cmd, sep, 1)
let sar_cword = get(g:, 'sar_cword')
if empty(sar_cword)
let sar_cword = cmd_splitted[1]
let g:sar_cword = sar_cword
endif
let cmd_pos = getcmdpos()
let replace_term = cmd_splitted[-2]
if replace_term ==# ''
let replace_term = g:sar_cword
else
let replace_term = ''
endif
let g:sar_cword = get(g:, 'sar_cword', cmd_splitted[1])
let replace_term = cmd_splitted[-2] ==# '' ? g:sar_cword : ''
let cmd_splitted[-2] = replace_term

let cmd = join(cmd_splitted, sep)
call setcmdpos(len(cmd) - len(cmd_splitted[-1]))
return cmd
endfunc
endfunction

func SarToggleAllFile()
function! SarToggleAllFile() abort
let cmd = getcmdline()
if !s:should_sar()
return cmd
endif

let sep = get(g:, 'sar_sep', '/')
let cmd_splitted = split(cmd, sep, 1)
let cmd_pos = getcmdpos()
let all_file = cmd_splitted[0]
if all_file ==# '%s'
let all_file = '.,$s'
elseif all_file ==# '.,$s'
let all_file = '0,.s'
else
let all_file = '%s'
endif
let all_file = all_file ==# '%s' ? '.,$s' :
\ all_file ==# '.,$s' ? '0,.s' : '%s'
let cmd_splitted[0] = all_file

let cmd = join(cmd_splitted, sep)
call setcmdpos(len(cmd) - len(cmd_splitted[-1]))
return cmd
endfunc
endfunction

func SarToggleSeparator()
function! SarToggleSeparator() abort
let cmd = getcmdline()
if !s:should_sar()
return cmd
endif

let sep = get(g:, 'sar_sep', '/')
let cmd_splitted = split(cmd, sep, 1)
let cmd_pos = getcmdpos()
let new_char_idx = index(s:chars, sep) + 1
let new_sep = new_char_idx >= len(s:chars) ? s:chars[0] : s:chars[new_char_idx]
let g:sar_sep = new_sep
let cmd = join(cmd_splitted, new_sep)
let g:sar_sep = new_char_idx >= len(s:chars) ? s:chars[0] : s:chars[new_char_idx]

let cmd = join(cmd_splitted, g:sar_sep)
call setcmdpos(cmd_pos)
return cmd
endfunc
endfunction

func SarToggleMagic()
function! SarToggleMagic() abort
let cmd = getcmdline()
if !s:should_sar()
return cmd
endif
let magic_list = ['\v', '\m', '\M', '\V', '']

let sep = get(g:, 'sar_sep', '/')
let cmd_splitted = split(cmd, sep, 1)
let cmd_pos = getcmdpos()

" cword
let sar_cword = get(g:, 'sar_cword')
if empty(sar_cword)
let sar_cword = cmd_splitted[1]
let g:sar_cword = sar_cword
endif

" magic
let g:sar_cword = get(g:, 'sar_cword', cmd_splitted[1])
let magic = get(g:, 'sar_magic', '\V')
let new_magic_idx = index(magic_list, magic) + 1
let new_magic = new_magic_idx >= len(magic_list) ? magic_list[0] : magic_list[new_magic_idx]
let g:sar_magic = new_magic
let new_magic_idx = index(s:magic_list, magic) + 1
let g:sar_magic = new_magic_idx >= len(s:magic_list) ? s:magic_list[0] : s:magic_list[new_magic_idx]


let cmd_splitted[1] = new_magic . sar_cword
let cmd_splitted[1] = g:sar_magic . g:sar_cword
let cmd = join(cmd_splitted, sep)
call setcmdpos(cmd_pos)
return cmd
endfunc
endfunction

cmap <M-g> <C-\>eSarToggleChar('g')<CR>
cmap <M-c> <C-\>eSarToggleChar('c')<CR>
Expand All @@ -181,4 +144,3 @@ cmap <M-d> <C-\>eSarToggleReplaceTerm()<CR>
cmap <M-5> <C-\>eSarToggleAllFile()<CR>
cmap <M-/> <C-\>eSarToggleSeparator()<CR>
cmap <M-m> <C-\>eSarToggleMagic()<CR>

0 comments on commit 3c43747

Please sign in to comment.