Skip to content

Instantly share code, notes, and snippets.

@Milly
Created February 10, 2010 08:30
Show Gist options
  • Save Milly/300143 to your computer and use it in GitHub Desktop.
Save Milly/300143 to your computer and use it in GitHub Desktop.
Mapping keys to commentout(\c) and uncomment(\u).
" Vim plugin file - commentout
"
" Last Change: 08 February 2008
" Maintainer: Milly
" Purpose: Mapping keys to commentout(\c) and uncomment(\u).
"=============================================================================
" Define "{{{1
if exists("loaded_commentout")
finish
endif
" Functions "{{{1
function! s:Escape(str)
let s = substitute(a:str, '\\,', ',', '')
return escape(s, '/\')
endf
function! s:CommentOut(cout)
if exists('b:comment_pattern')
let starts = matchstr(b:comment_pattern, '\v^([^\,]|\\\\|\\,)*')
let ends = strpart(b:comment_pattern, strlen(starts) + 1)
let starts = s:Escape(starts)
let ends = s:Escape(ends)
if strlen(ends)
if a:cout
let pat = '^\(\s*\)\(\S.*\)$'
let rep = '\1' . starts . ' \2 ' . ends
else
let pat = '^\(\s*\)\V' . starts . '\m \?\(.\{-}\) \?\V' . ends . '\m\s*$'
let rep = '\1\2'
endif
else
if a:cout
let pat = '^'
let rep = starts . ' '
else
let pat = '^\V' . starts . ' \?'
let rep = ''
endif
endif
let search = @/
exec ':sm/' . pat . '/' . rep . '/e'
let @/ = search
endif
endf
function! s:CommentPattern(bang, ...)
if 2 < a:0
echohl Error
echomsg 'Trailing characters'
echohl None
return
endif
if 0 == a:0
exec 'autocmd' . a:bang 'CommentOut FileType'
return
endif
let filetypes = a:1
if 1 == a:0
exec 'autocmd' . a:bang 'CommentOut FileType' filetypes
return
endif
if '!' != a:bang && exists('#CommentOut#FileType#' . filetypes)
echohl Error
echomsg 'Filetypes already exists:' filetypes
echohl None
else
let pattern = a:2
exec 'autocmd! CommentOut FileType' filetypes
\ 'let b:comment_pattern=' string(pattern)
if -1 != index(split(filetypes, ','), &l:filetype)
let b:comment_pattern = pattern
endif
endif
endf
function! s:GetFiletype(ArgLead, CmdLine, CursorPos)
let p = match(a:CmdLine, '^\v(\S+\s+){2}\zs')
if -1 != p && p <= a:CursorPos
return ''
endif
return &l:filetype
endf
" Key mapping "{{{1
noremap <silent> <Leader>c :call <SID>CommentOut(1)<CR>
noremap <silent> <Leader>u :call <SID>CommentOut(0)<CR>
" CommentPattern command "{{{1
command! -nargs=* -bang -complete=custom,<SID>GetFiletype
\ CommentPattern call <SID>CommentPattern('<bang>', <f-args>)
" Set comment patterns "{{{1
augroup CommentOut
autocmd!
augroup END
CommentPattern vim "
CommentPattern sh,csh,zsh,cfg,conf,config,awk,perl,ruby,python,jproperties,tcl,make,gnuplot,apache,crontab,screen,yaml #
CommentPattern basic,vb '
CommentPattern fortran !
CommentPattern asm,dosini,lisp,scheme ;
CommentPattern prolog,postscr,tex %
CommentPattern java,javascript,php,cpp,cs,d,verilog //
CommentPattern dosbatch ::
CommentPattern ada,sql,haskel --
CommentPattern cobol \ \ \ \ \ \ /
CommentPattern c,css /*,*/
CommentPattern pascal {,}
CommentPattern xml,html,dtd,ant,aspvbs,aspperl <!--,-->
CommentPattern jsp <%--,--%>
" Done "{{{1
let loaded_commentout=1
" vim: foldmethod=marker:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment