Created
December 16, 2015 10:25
-
-
Save chemzqm/0bcb2523c25913ca0edc to your computer and use it in GitHub Desktop.
A perfect comment vim plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ============================================================================ | |
" Description: An easy comment plugin | |
" Author: Qiming Zhao <chemzqm@gmail.com> | |
" Licence: Vim licence | |
" Version: 0.1 | |
" ============================================================================ | |
"let g:comment_debug = 1 | |
if exists("g:comment_loaded") && !exists("g:comment_debug") | |
finish | |
endif | |
let g:comment_loaded = 1 | |
let s:save_cpo = &cpo | |
set cpo&vim | |
vnoremap <leader>c :call <SID>CommentFromSelected(visualmode(), 1)<cr> | |
nnoremap <leader>c :set operatorfunc=<SID>CommentFromSelected<cr>g@ | |
nnoremap <leader>cc :<C-u>call <SID>CommentFromSelected('c')<cr> | |
let s:xmls = ['html', 'xhtml', 'xml'] | |
function! s:CommentFromSelected(type, ...) | |
if a:0 | |
let start = line('v') | |
let end = line('.') | |
elseif a:type ==#'c' | |
let start = line('.') | |
let end = line('.') | |
else | |
normal '[ | |
let start = line('.') | |
normal! `] | |
let end = line('.') | |
endif | |
if index(s:xmls, &ft) != -1 | |
call s:CommentXml() | |
else | |
call s:CommentLines(start, end) | |
endif | |
endfunction | |
function! s:CommentXml() | |
call emmet#toggleComment() | |
endfunction | |
let s:comment_begin = { | |
\"c" : "/*", | |
\"cpp" : "//", | |
\"css" : "/*", | |
\"default" : "#", | |
\"go" : "//", | |
\"java" : "//", | |
\"javascript" : "//", | |
\"make" : "#", | |
\"perl" : "#", | |
\"plaintex" : "%", | |
\"python" : "#", | |
\"ruby" : "#", | |
\"sh" : "#", | |
\"tex" : "%", | |
\"vim" : "\" ", | |
\} | |
" (optional) | |
let s:comment_end = { | |
\"c" : "*/", | |
\"css" : "*/", | |
\"default" : "", | |
\} | |
let s:regex = '^\s*' | |
function! s:CommentToggle(lnum, com_beg, com_end) | |
let line = getline(a:lnum) | |
if (len(line) == 0) | return line | endif | |
let indent = matchstr(line, s:regex) | |
let sl = len(indent) | |
if line[sl : len(a:com_beg) + sl -1] != a:com_beg | |
let line = indent . a:com_beg . line[sl : ] . a:com_end | |
else | |
let line = indent . line[len(a:com_beg) + sl : len(line)-len(a:com_end)-1] | |
endif | |
return line | |
endfunction | |
function! s:CommentLines(start, end) | |
let lines = [] | |
let ft = &ft | |
let com_begin = has_key(s:comment_begin, ft) ? s:comment_begin[ft] : s:comment_begin['default'] | |
let com_end = has_key(s:comment_end, ft) ? s:comment_end[ft] : s:comment_end['default'] | |
for lnum in range(a:start, a:end) | |
call add(lines, s:CommentToggle(lnum, com_begin, com_end)) | |
endfor | |
call setline(a:start, lines) | |
endfunction | |
let &cpo = s:save_cpo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment