- 论坛徽章:
- 0
|
这两天研究了下VIM的脚本...已经可以实现 用alt+/ 注释与反注释了..ctrl+/还是无法使用...原因不明.....
贴下脚本...目前使用还算正常..不知道还有没有BUG
noremap <A-/> :call MK2_Comment()<CR>
vnoremap <A-/> :<BS><BS><BS><BS><BS>call MK2_CommentV()<CR>
inoremap <A-/> <Esc>:call MK2_Comment()<CR>a
function MK2_Comment()
let colNum=col(".")
let lineNum=line(".")
let isComment=1
let isComment=s:IsComment(lineNum)
call s:SetComment(isComment)
call s:SetCursor(isComment, lineNum, colNum)
endfunction
function MK2_CommentV()
let colNum=col(".")
let lineNum=line(".")
let lineStart=line("'<")
let lineEnd=line("'>")
let i=lineStart
while i<=lineEnd
let isComment=s:IsComment(i)
if isComment==0
break
endif
let i=i+1
endwhile
let i=lineStart
while i<=lineEnd
call cursor(i, 0)
call s:SetComment(isComment)
let i=i+1
endwhile
call s:SetCursor(isComment, lineNum, colNum)
endfunction
function s:IsComment(currentLineNum)
let currentLine=getline(a:currentLineNum)
let lineLen=len(currentLine)
let i=0
while i<lineLen
if !((currentLine==' ') || (currentLine==' '))
if (currentLine=='/' && currentLine[i+1]=='/')
return 1
else
return 0
endif
endif
let i=i+1
endwhile
"if match(line,"^\(\t\|\s\)*\/\/") != -1
" let isComment=1
"endif
return 0
endfunction
function s:SetComment(isComment)
if a:isComment==0
s/^/\/\//
else
s/^\(\(\t\|\s\)*\)\/\//\1/
endif
endfunction
function s:SetCursor(isComment, lineNum, colNum)
if a:isComment==0
call cursor(a:lineNum,a:colNum+2)
else
call cursor(a:lineNum,a:colNum-2)
endif
endfunction
[ 本帖最后由 fcymk2 于 2009-4-22 00:48 编辑 ] |
|