Fork commentary to work with template files

This commit is contained in:
2021-10-12 22:10:43 +01:00
parent fba50be42b
commit 47b7681b02
2 changed files with 164 additions and 3 deletions

View File

@@ -16,8 +16,9 @@ Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } " Fast fuzzy file searching
Plug 'junegunn/fzf.vim' Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-fugitive' " Adds git support to vim Plug 'tpope/vim-fugitive' " Adds git support to vim
Plug 'tpope/vim-surround' " Adds motions for dealing with parentheses Plug 'tpope/vim-surround' " Adds motions for dealing with parentheses
Plug 'tpope/vim-commentary' " Easily comment/uncomment lines with `gc` " Plug 'tpope/vim-commentary' " Easily comment/uncomment lines with `gc` (updated to work with style/script)
Plug 'tpope/vim-unimpaired' " A series of bindings for complementary actions [<space> and ]<space> for adding lines before and after Plug 'tpope/vim-unimpaired' " A series of bindings for complementary actions [<space> and ]<space> for adding lines before and after
Plug 'tpope/vim-vinegar' " Improvements to the file browser
Plug 'jiangmiao/auto-pairs' " Creates closing parentheses when writing the opening, use <m-e> to wrap the next word/group, <m-p> to disable Plug 'jiangmiao/auto-pairs' " Creates closing parentheses when writing the opening, use <m-e> to wrap the next word/group, <m-p> to disable
Plug 'mattn/emmet-vim' " Emmet support ,em to toggle it Plug 'mattn/emmet-vim' " Emmet support ,em to toggle it
Plug 'dylanaraps/wal.vim' " Set the theme to match the system colours Plug 'dylanaraps/wal.vim' " Set the theme to match the system colours
@@ -281,6 +282,24 @@ let g:chadtree_settings = {
\ } \ }
\ } \ }
" function s:set_commentary_format()
" echo "running command"
" if &filetype =~ 'vue\|html\|svelte'
" if searchpair('<\(script\|style\)', '', '</\(script\|style\)>', 'Wn')
" let b:commentary_format="// %s"
" else
" let b:commentary_format=&commentstring
" endif
" endif
" endfunction
" xnoremap gc :call <SID>set_commentary_format()<CR><Plug>Commentary
" nnoremap gc :call <SID>set_commentary_format()<CR><Plug>Commentary
" onoremap gc :call <SID>set_commentary_format()<CR><Plug>Commentary
" nnoremap gcc :call <SID>set_commentary_format()<CR><Plug>Commentary
" nnoremap cgc :call <SID>set_commentary_format()<CR><Plug>Commentary
" nnoremap cgu :call <SID>set_commentary_format()<CR><Plug>Commentary<Plug>Commentary
" }}} " }}}
" Moving around --- {{{ " Moving around --- {{{
@@ -325,6 +344,18 @@ augroup autofolding
autocmd FileType zsh setlocal foldmethod=marker autocmd FileType zsh setlocal foldmethod=marker
augroup END augroup END
"Set commentary formats
function s:setcommentary()
if &commentstring =~ '/\*\s*%s\s*\*/'
let b:commentary_format="// %s"
endif
endfunction
augroup autocomment
autocmd!
autocmd FileType * call <SID>setcommentary()
augroup END
"Automatically save files "Automatically save files
augroup autosaving augroup autosaving
autocmd! autocmd!
@@ -341,8 +372,7 @@ augroup END
augroup suckless augroup suckless
autocmd BufWritePost ~/.local/src/dwmblocks/config.h !cd ~/.local/src/dwmblocks/; doas make install && { killall -q dwmblocks;setsid -f dwmblocks } autocmd BufWritePost ~/.local/src/dwmblocks/config.h !cd ~/.local/src/dwmblocks/; doas make install && { killall -q dwmblocks;setsid -f dwmblocks }
autocmd BufWritePost ~/.local/src/dwm/config.h !cd ~/.local/src/dwm/; doas make clean install autocmd BufWritePost ~/.local/src/dwm/config.h !cd ~/.local/src/dwm/; doas make clean install
autocmd BufWritePost ~/.local/src/st/config.h !cd ~/.local/st/dwm/; doas make clean install autocmd BufWritePost ~/.local/src/st/config.h !cd ~/.local/src/st/; doas make clean install
" }}} " }}}
" Notes and tips " Notes and tips

131
plugin/commentary.vim Normal file
View File

@@ -0,0 +1,131 @@
" commentary.vim - Comment stuff out
" Maintainer: Tim Pope <http://tpo.pe/>
" Version: 1.3
" GetLatestVimScripts: 3695 1 :AutoInstall: commentary.vim
if exists("g:loaded_commentary") || v:version < 700
finish
endif
let g:loaded_commentary = 1
function! s:surroundings() abort
if &filetype =~ 'vue\|html\|svelte'
if searchpair('<\(script\|style\)', '', '</\(script\|style\)>', 'Wn')
let b:commentary_format="// %s"
else
let b:commentary_format=&commentstring
endif
endif
return split(get(b:, 'commentary_format', substitute(substitute(substitute(
\ &commentstring, '^$', '%s', ''), '\S\zs%s',' %s', '') ,'%s\ze\S', '%s ', '')), '%s', 1)
endfunction
function! s:strip_white_space(l,r,line) abort
let [l, r] = [a:l, a:r]
if l[-1:] ==# ' ' && stridx(a:line,l) == -1 && stridx(a:line,l[0:-2]) == 0
let l = l[:-2]
endif
if r[0] ==# ' ' && a:line[-strlen(r):] != r && a:line[1-strlen(r):] == r[1:]
let r = r[1:]
endif
return [l, r]
endfunction
function! s:go(...) abort
if !a:0
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
return 'g@'
elseif a:0 > 1
let [lnum1, lnum2] = [a:1, a:2]
else
let [lnum1, lnum2] = [line("'["), line("']")]
endif
let [l, r] = s:surroundings()
let uncomment = 2
let force_uncomment = a:0 > 2 && a:3
for lnum in range(lnum1,lnum2)
let line = matchstr(getline(lnum),'\S.*\s\@<!')
let [l, r] = s:strip_white_space(l,r,line)
if len(line) && (stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let uncomment = 0
endif
endfor
if get(b:, 'commentary_startofline')
let indent = '^'
else
let indent = '^\s*'
endif
let lines = []
for lnum in range(lnum1,lnum2)
let line = getline(lnum)
if strlen(r) > 2 && l.r !~# '\\'
let line = substitute(line,
\'\M' . substitute(l, '\ze\S\s*$', '\\zs\\d\\*\\ze', '') . '\|' . substitute(r, '\S\zs', '\\zs\\d\\*\\ze', ''),
\'\=substitute(submatch(0)+1-uncomment,"^0$\\|^-\\d*$","","")','g')
endif
if force_uncomment
if line =~ '^\s*' . l
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
endif
elseif uncomment
let line = substitute(line,'\S.*\s\@<!','\=submatch(0)[strlen(l):-strlen(r)-1]','')
else
let line = substitute(line,'^\%('.matchstr(getline(lnum1),indent).'\|\s*\)\zs.*\S\@<=','\=l.submatch(0).r','')
endif
call add(lines, line)
endfor
call setline(lnum1, lines)
let modelines = &modelines
try
set modelines=0
silent doautocmd User CommentaryPost
finally
let &modelines = modelines
endtry
return ''
endfunction
function! s:textobject(inner) abort
let [l, r] = s:surroundings()
let lnums = [line('.')+1, line('.')-2]
for [index, dir, bound, line] in [[0, -1, 1, ''], [1, 1, line('$'), '']]
while lnums[index] != bound && line ==# '' || !(stridx(line,l) || line[strlen(line)-strlen(r) : -1] != r)
let lnums[index] += dir
let line = matchstr(getline(lnums[index]+dir),'\S.*\s\@<!')
let [l, r] = s:strip_white_space(l,r,line)
endwhile
endfor
while (a:inner || lnums[1] != line('$')) && empty(getline(lnums[0]))
let lnums[0] += 1
endwhile
while a:inner && empty(getline(lnums[1]))
let lnums[1] -= 1
endwhile
if lnums[0] <= lnums[1]
execute 'normal! 'lnums[0].'GV'.lnums[1].'G'
endif
endfunction
command! -range -bar -bang Commentary call s:go(<line1>,<line2>,<bang>0)
xnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>Commentary <SID>go()
nnoremap <expr> <Plug>CommentaryLine <SID>go() . '_'
onoremap <silent> <Plug>Commentary :<C-U>call <SID>textobject(get(v:, 'operator', '') ==# 'c')<CR>
nnoremap <silent> <Plug>ChangeCommentary c:<C-U>call <SID>textobject(1)<CR>
nmap <silent> <Plug>CommentaryUndo :echoerr "Change your <Plug>CommentaryUndo map to <Plug>Commentary<Plug>Commentary"<CR>
if !hasmapto('<Plug>Commentary') || maparg('gc','n') ==# ''
xmap gc <Plug>Commentary
nmap gc <Plug>Commentary
omap gc <Plug>Commentary
nmap gcc <Plug>CommentaryLine
if maparg('c','n') ==# '' && !exists('v:operator')
nmap cgc <Plug>ChangeCommentary
endif
nmap gcu <Plug>Commentary<Plug>Commentary
endif
" vim:set et sw=2: