custom comment char support

Signed-off-by: Pavel Lutskov <pavel.lutskov@gmail.com>
This commit is contained in:
2018-04-11 11:15:30 +02:00
parent 1bbf319ae5
commit 82116a8c1a
2 changed files with 31 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
let s:comment_filetype_map = { let s:comment_map = {
\ 'c': '\/\/', \ 'c': '\/\/',
\ 'cpp': '\/\/', \ 'cpp': '\/\/',
\ 'java': '\/\/', \ 'java': '\/\/',
@@ -6,10 +6,11 @@ let s:comment_filetype_map = {
\ 'python': '#', \ 'python': '#',
\ 'vim': '"', \ 'vim': '"',
\ 'tex': '%', \ 'tex': '%',
\ 'plaintex': '%',
\ 'sh': '#' \ 'sh': '#'
\ } \ }
let s:block_filetype_map = { let s:block_map = {
\ 'c': ['\/\*', '\*\/'], \ 'c': ['\/\*', '\*\/'],
\ 'java': ['\/\*', '\*\/'], \ 'java': ['\/\*', '\*\/'],
\ 'cpp': ['\/\*', '\*\/'], \ 'cpp': ['\/\*', '\*\/'],
@@ -18,21 +19,29 @@ let s:block_filetype_map = {
\ } \ }
function! MyCommenter(uncomment) range function! MyCommenter(uncomment) range
let comment = get(s:comment_filetype_map, &ft, '') let comment = get(s:comment_map, &ft, '')
if exists('g:custom_comment_map')
let comment = get(g:custom_comment_map, &ft, '')
endif
if !len(comment) if !len(comment)
return return
endif endif
let search_range = a:firstline . ',' . a:lastline let search_range = a:firstline . ',' . a:lastline
" Uncomment the line (when commenting this avoids multiple comment chars)
execute search_range . 's/\(^\s*\)\(' . comment . '\s*\)\+/\1/e'
if !a:uncomment if !a:uncomment
" Comment
execute search_range . 's/\(^\s*\)\(\S\)/\1' . comment . ' \2/e' execute search_range . 's/\(^\s*\)\(\S\)/\1' . comment . ' \2/e'
else
execute search_range . 's/\(^\s*\)' . comment . '\s*/\1/e'
endif endif
nohl nohl
endfunction endfunction
function! MyBlocker(unblock) range function! MyBlocker(unblock) range
let block = get(s:block_filetype_map, &ft, []) let block = get(s:block_map, &ft, [])
if exists('g:custom_block_map')
let block = get(g:custom_block_map, &ft, [])
endif
if !len(block) if !len(block)
return return
endif endif
@@ -45,10 +54,10 @@ function! MyBlocker(unblock) range
execute 'normal! ?' . block[0] . "\<cr>" . 'dd' execute 'normal! ?' . block[0] . "\<cr>" . 'dd'
execute 'normal! /' . block[1] . "\<cr>" . 'dd' execute 'normal! /' . block[1] . "\<cr>" . 'dd'
endif endif
noh nohl
endfunction endfunction
command! -range CommenseComment <line1>,<line2>call MyCommenter(0) command! -range CommenseComment silent <line1>,<line2>call MyCommenter(0)
command! -range CommenseUncomment <line1>,<line2>call MyCommenter(1) command! -range CommenseUncomment silent <line1>,<line2>call MyCommenter(1)
command! -range CommenseBlock <line1>,<line2>call MyBlocker(0) command! -range CommenseBlock silent <line1>,<line2>call MyBlocker(0)
command! -range CommenseUnblock <line1>,<line2>call MyBlocker(1) command! -range CommenseUnblock silent <line1>,<line2>call MyBlocker(1)

View File

@@ -27,6 +27,7 @@ set incsearch
set laststatus=2 set laststatus=2
set nojoinspaces set nojoinspaces
set backspace=
" set splitbelow " set splitbelow
" }}} " }}}
@@ -62,7 +63,7 @@ let g:LatexBox_latexmk_options = '-xelatex -outdir=build'
" YouCompleteMe stuff " YouCompleteMe stuff
" {{{ " {{{
set completeopt-=preview set completeopt-=preview
let g:ycm_autoclose_preview_window_after_completion = 1 let g:ycm_autoclose_preview_window_after_completion = 0
let g:ycm_filetype_blacklist = { let g:ycm_filetype_blacklist = {
\ 'tex': 1, \ 'tex': 1,
\ 'markdown': 1, \ 'markdown': 1,
@@ -122,16 +123,17 @@ else
let my_settings_file = "$HOME/.vim/plugin/settings.vim" let my_settings_file = "$HOME/.vim/plugin/settings.vim"
endif endif
nnoremap <leader>c mc:CommenseComment<cr>`c:delm c<cr> nnoremap <silent> <leader>c mc:CommenseComment<cr>`c:delm c<cr>
nnoremap <leader>xc mc:CommenseUncomment<cr>`c:delm c<cr> nnoremap <silent> <leader>xc mc:CommenseUncomment<cr>`c:delm c<cr>
vnoremap <leader>c :CommenseComment<cr> vnoremap <silent> <leader>c :CommenseComment<cr>
vnoremap <leader>xc :CommenseUncomment<cr> vnoremap <silent> <leader>xc :CommenseUncomment<cr>
vnoremap <leader>b :CommenseBlock<cr> vnoremap <silent> <leader>b :CommenseBlock<cr>
nnoremap <leader>xb mc:CommenseUnblock<cr>`c:delm c<cr> nnoremap <silent> <leader>xb mc:CommenseUnblock<cr>`c:delm c<cr>
nnoremap <leader>sl :tabe $MYVIMRC<cr> nnoremap <leader>sl :pedit $MYVIMRC<cr>
nnoremap <leader>ss :execute 'tabe' my_settings_file<cr> nnoremap <leader>ss :execute 'pedit' my_settings_file<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>:execute 'source' my_settings_file<cr> nnoremap <leader>sv :source $MYVIMRC<cr>:execute 'source' my_settings_file<cr>
nnoremap <leader>st :source %<cr>
nnoremap <leader>k <c-w><c-k> nnoremap <leader>k <c-w><c-k>
nnoremap <leader>j <c-w><c-j> nnoremap <leader>j <c-w><c-j>
nnoremap <leader>h <c-w><c-h> nnoremap <leader>h <c-w><c-h>