Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created August 28, 2010 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/554634 to your computer and use it in GitHub Desktop.
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
" 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