Created
August 28, 2010 03:15
-
-
Save bootleq/554634 to your computer and use it in GitHub Desktop.
[.vimrc] Simple text object for continuous comment @see http://bootleq.blogspot.com/2010/08/text-object.html
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
" NOTE: limitations | |
" 1. Only test if first non-blank character is highlighted with "Comment". | |
" 2. Always linewise. | |
vnoremap <silent> ac :<C-U>call TxtObjComment()<CR> | |
onoremap <silent> ac :<C-U>call TxtObjComment()<CR> | |
fun! TxtObjComment() | |
if exists("g:syntax_on") | |
if ! IsInComment() | |
echomsg 'Not in a Comment region.' | |
return 0 | |
else | |
call cursor(SearchContinuousComment(0, line("."))) | |
normal! 0V | |
call cursor(SearchContinuousComment(1, line("."))) | |
endif | |
else | |
echohl ErrorMsg | echoerr "TxtObjComment: syntax highlighting not enabled." | echohl None | |
endif | |
endf | |
fun! SearchContinuousComment(forward, lnum) | |
let line = a:lnum | |
while line > 0 && line <= line("$") | |
let next = a:forward ? line + 1 : line - 1 | |
if next > 0 && next < line("$") && IsInComment(next) | |
let line = next | |
else | |
break | |
endif | |
endwhile | |
return [line, 0] | |
endf | |
fun! IsInComment(...) | |
let lnum = a:0 > 0 ? a:1 : line(".") | |
let col = a:0 > 1 ? a:2 : indent(lnum) + 1 | |
let synStack = synstack(lnum, col) | |
let inComment = 0 | |
for syn in synStack | |
if synIDattr(synIDtrans(syn), "name") ==# 'Comment' | |
let inComment = 1 | |
endif | |
endfor | |
return inComment | |
endf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment