Skip to content

Instantly share code, notes, and snippets.

@pinealan
Created April 13, 2021 14:50
Show Gist options
  • Save pinealan/a75d7db846449ae484169a3f4a519ef4 to your computer and use it in GitHub Desktop.
Save pinealan/a75d7db846449ae484169a3f4a519ef4 to your computer and use it in GitHub Desktop.
Select second top form within comment
" from vim-sexp
function! s:get_visual_marks()
return [getpos("'<"), getpos("'>")]
endfunction
function! s:set_visual_marks(marks)
call setpos("'<", a:marks[0])
call setpos("'>", a:marks[1])
endfunction
" Return visually selected text without changing selection state and registers
function! GetSelectedCode() abort
let reg_save = @@
silent normal! y
let result = @@
let @@ = reg_save
silent normal! gv
return result
endfunction
function! SelectOuterUntilTop(top_code) abort
let reg_save = @@
try
while (v:true)
call sexp#select_current_list('v', 0, 1)
let current_marks = s:get_visual_marks()
call sexp#docount(2, 'sexp#select_current_list', 'v', 0, 1)
let next_code = GetSelectedCode()
if (next_code ==# a:top_code) | break | endif
endwhile
call s:set_visual_marks(current_marks)
normal! gv
finally
let @@ = reg_save
endtry
endfunction
function! SelectOuterTopList() abort
let curpos = getpos('.')
" First use vim-sexp optimized top form selector
call sexp#select_current_top_list('v', 0)
let top_code = GetSelectedCode()
if (stridx(top_code, '(comment') == 0)
" Select up one by one if the top list is a (comment ...) form
execute "normal! \<Esc>"
call setpos('.', curpos)
call SelectOuterUntilTop(top_code)
endif
endfunction
nmap <localleader>ee m`<Plug>(iced_eval):<c-u>call SelectOuterTopList()<cr>g``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment