update tcomment

git-svn-id: https://vimsuite.svn.sourceforge.net/svnroot/vimsuite/trunk@200 eb2d0018-73a3-4aeb-bfe9-1def61c9ec69
This commit is contained in:
alterdepp 2011-05-26 13:09:04 +00:00
parent 0642385224
commit fe3c306d74
4 changed files with 494 additions and 314 deletions

View File

@ -3,44 +3,99 @@
" @Website: http://www.vim.org/account/profile.php?user_id=4037 " @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt) " @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 2007-09-17. " @Created: 2007-09-17.
" @Last Change: 2010-09-29. " @Last Change: 2011-04-12.
" @Revision: 0.0.249 " @Revision: 0.0.407
" call tlog#Log('Load: '. expand('<sfile>')) " vimtlib-sfile " call tlog#Log('Load: '. expand('<sfile>')) " vimtlib-sfile
" If true, comment blank lines too
if !exists("g:tcommentBlankLines") if !exists("g:tcommentBlankLines")
let g:tcommentBlankLines = 1 " If true, comment blank lines too
let g:tcommentBlankLines = 1 "{{{2
endif endif
" Guess the file type based on syntax names always or for some fileformat only if !exists("g:tcommentOpModeExtra")
if !exists("g:tcommentGuessFileType") " Modifies how the operator works.
let g:tcommentGuessFileType = 0 " > ... Move the cursor to the end of the comment
let g:tcommentOpModeExtra = '' "{{{2
endif
if !exists('g:tcommentOptions')
" Other key-value options used by |tcomment#Comment()|.
"
" Example: If you want to put the opening comment marker always in
" the first column regardless of the block's indentation, put this
" into your |vimrc| file: >
" let g:tcommentOptions = {'col': 1}
let g:tcommentOptions = {} "{{{2
endif
if !exists('g:tcomment#ignore_char_type')
" |text-objects| for use with |tcomment#Operator| can have different
" types: line, block, char etc. Text objects like aB, it, at etc.
" have type char but this may not work reliably. By default,
" tcomment handles those text objects most often as if they were of
" type line. Set this variable to 0 in order to change this
" behaviour. Be prepared that the result may not always match your
" intentions.
let g:tcomment#ignore_char_type = 1 "{{{2
endif
if !exists("g:tcommentGuessFileType")
" Guess the file type based on syntax names always or for some fileformat only
" If non-zero, try to guess filetypes.
" tcomment also checks g:tcommentGuessFileType_{&filetype} for
" filetype specific values.
"
" Values:
" 0 ... don't guess
" 1 ... guess
" FILETYPE ... assume this filetype
let g:tcommentGuessFileType = 0 "{{{2
endif endif
" In php documents, the php part is usually marked as phpRegion. We thus
" assume that the buffers default comment style isn't php but html
if !exists("g:tcommentGuessFileType_dsl") if !exists("g:tcommentGuessFileType_dsl")
let g:tcommentGuessFileType_dsl = 'xml' " For dsl documents, assumet filetype = xml.
let g:tcommentGuessFileType_dsl = 'xml' "{{{2
endif endif
if !exists("g:tcommentGuessFileType_php") if !exists("g:tcommentGuessFileType_php")
let g:tcommentGuessFileType_php = 'html' " In php documents, the php part is usually marked as phpRegion. We
" thus assume that the buffers default comment style isn't php but
" html.
let g:tcommentGuessFileType_php = 'html' "{{{2
endif endif
if !exists("g:tcommentGuessFileType_html") if !exists("g:tcommentGuessFileType_html")
let g:tcommentGuessFileType_html = 1 let g:tcommentGuessFileType_html = 1 "{{{2
endif endif
if !exists("g:tcommentGuessFileType_tskeleton") if !exists("g:tcommentGuessFileType_tskeleton")
let g:tcommentGuessFileType_tskeleton = 1 let g:tcommentGuessFileType_tskeleton = 1 "{{{2
endif endif
if !exists("g:tcommentGuessFileType_vim") if !exists("g:tcommentGuessFileType_vim")
let g:tcommentGuessFileType_vim = 1 let g:tcommentGuessFileType_vim = 1 "{{{2
endif
if !exists("g:tcommentGuessFileType_django")
let g:tcommentGuessFileType_django = 1 "{{{2
endif endif
if !exists("g:tcommentIgnoreTypes_php") if !exists("g:tcommentIgnoreTypes_php")
let g:tcommentIgnoreTypes_php = 'sql' " In php files, some syntax regions are wongly highlighted as sql
" markup. We thus ignore sql syntax when guessing the filetype in
" php files.
let g:tcommentIgnoreTypes_php = 'sql' "{{{2
endif endif
if !exists('g:tcommentSyntaxMap') "{{{2 if !exists('g:tcomment#syntax_substitute')
" :read: let g:tcomment#syntax_substitute = {...} "{{{2
" Perform replacements on the syntax name.
let g:tcomment#syntax_substitute = {
\ '\C^javaScript': {'sub': 'javascript'}
\ }
endif
if !exists('g:tcommentSyntaxMap')
" tcomment guesses filetypes based on the name of the current syntax
" region. This works well if the syntax names match
" /filetypeSomeName/. Other syntax names have to be explicitly
" mapped onto the corresponding filetype.
" :read: let g:tcommentSyntaxMap = {...} "{{{2
let g:tcommentSyntaxMap = { let g:tcommentSyntaxMap = {
\ 'vimMzSchemeRegion': 'scheme', \ 'vimMzSchemeRegion': 'scheme',
\ 'vimPerlRegion': 'perl', \ 'vimPerlRegion': 'perl',
@ -50,17 +105,9 @@ if !exists('g:tcommentSyntaxMap') "{{{2
\ } \ }
endif endif
" If you don't define these variables, TComment will use &commentstring
" instead. We override the default values here in order to have a blank after
" the comment marker. Block comments work only if we explicitly define the
" markup.
" The format for block comments is similar to normal commentstrings with the
" exception that the format strings for blocks can contain a second line that
" defines how "middle lines" (see :h format-comments) should be displayed.
" I personally find this style rather irritating but here is an alternative
" definition that does this left-handed bar thing
if !exists("g:tcommentBlockC") if !exists("g:tcommentBlockC")
" Generic c-like block comments.
" :read: let g:tcommentBlockC = {...} "{{{2
let g:tcommentBlockC = { let g:tcommentBlockC = {
\ 'commentstring': '/*%s */', \ 'commentstring': '/*%s */',
\ 'middle': ' * ', \ 'middle': ' * ',
@ -70,6 +117,8 @@ if !exists("g:tcommentBlockC")
\ } \ }
endif endif
if !exists("g:tcommentBlockC2") if !exists("g:tcommentBlockC2")
" Generic c-like block comments (alternative markup).
" :read: let g:tcommentBlockC2 = {...} "{{{2
let g:tcommentBlockC2 = { let g:tcommentBlockC2 = {
\ 'commentstring': '/**%s */', \ 'commentstring': '/**%s */',
\ 'middle': ' * ', \ 'middle': ' * ',
@ -79,21 +128,38 @@ if !exists("g:tcommentBlockC2")
\ } \ }
endif endif
if !exists("g:tcommentInlineC") if !exists("g:tcommentInlineC")
let g:tcommentInlineC = "/* %s */" " Generic c-like comments.
let g:tcommentInlineC = "/* %s */" "{{{2
endif endif
if !exists("g:tcommentBlockXML") if !exists("g:tcommentBlockXML")
let g:tcommentBlockXML = "<!--%s-->\n " " Generic xml-like block comments.
let g:tcommentBlockXML = "<!--%s-->\n " "{{{2
endif endif
if !exists("g:tcommentInlineXML") if !exists("g:tcommentInlineXML")
let g:tcommentInlineXML = "<!-- %s -->" " Generic xml-like comments.
let g:tcommentInlineXML = "<!-- %s -->" "{{{2
endif endif
let s:typesDirty = 1 let s:typesDirty = 1
let s:definitions = {} let s:definitions = {}
" Currently this function just sets a variable " If you don't explicitly define a comment style, |:TComment| will use
" 'commentstring' instead. We override the default values here in order
" to have a blank after the comment marker. Block comments work only if
" we explicitly define the markup.
"
" The comment definition can be either a string or a dictionary.
"
" If it is a string:
" The format for block comments is similar to 'commentstrings' with the
" exception that the format strings for blocks can contain a second line
" that defines how "middle lines" (see :h format-comments) should be
" displayed.
"
" If it is a dictionary:
" See the help on the args argument of |tcomment#Comment|.
function! tcomment#DefineType(name, commentdef) function! tcomment#DefineType(name, commentdef)
if !has_key(s:definitions, a:name) if !has_key(s:definitions, a:name)
if type(a:commentdef) == 4 if type(a:commentdef) == 4
@ -107,6 +173,7 @@ function! tcomment#DefineType(name, commentdef)
let s:typesDirty = 1 let s:typesDirty = 1
endf endf
" :nodoc:
" Return 1 if a comment type is defined. " Return 1 if a comment type is defined.
function! tcomment#TypeExists(name) function! tcomment#TypeExists(name)
return has_key(s:definitions, a:name) return has_key(s:definitions, a:name)
@ -135,7 +202,11 @@ call tcomment#DefineType('crontab', '# %s' )
call tcomment#DefineType('cs', '// %s' ) call tcomment#DefineType('cs', '// %s' )
call tcomment#DefineType('cs_inline', g:tcommentInlineC ) call tcomment#DefineType('cs_inline', g:tcommentInlineC )
call tcomment#DefineType('cs_block', g:tcommentBlockC ) call tcomment#DefineType('cs_block', g:tcommentBlockC )
call tcomment#DefineType('debsources', '# %s' )
call tcomment#DefineType('debcontrol', '# %s' )
call tcomment#DefineType('desktop', '# %s' ) call tcomment#DefineType('desktop', '# %s' )
call tcomment#DefineType('django', '{# %s #}' )
call tcomment#DefineType('django_block', "{%% comment %%}%s{%% endcomment %%}\n ")
call tcomment#DefineType('docbk', '<!-- %s -->' ) call tcomment#DefineType('docbk', '<!-- %s -->' )
call tcomment#DefineType('docbk_inline', g:tcommentInlineXML) call tcomment#DefineType('docbk_inline', g:tcommentInlineXML)
call tcomment#DefineType('docbk_block', g:tcommentBlockXML ) call tcomment#DefineType('docbk_block', g:tcommentBlockXML )
@ -146,8 +217,12 @@ call tcomment#DefineType('dylan', '// %s' )
call tcomment#DefineType('eiffel', '-- %s' ) call tcomment#DefineType('eiffel', '-- %s' )
call tcomment#DefineType('erlang', '%%%% %s' ) call tcomment#DefineType('erlang', '%%%% %s' )
call tcomment#DefineType('eruby', '<%%# %s' ) call tcomment#DefineType('eruby', '<%%# %s' )
call tcomment#DefineType('fstab', '# %s' )
call tcomment#DefineType('gitcommit', '# %s' ) call tcomment#DefineType('gitcommit', '# %s' )
call tcomment#DefineType('gtkrc', '# %s' ) call tcomment#DefineType('gtkrc', '# %s' )
call tcomment#DefineType('go', '// %s' )
call tcomment#DefineType('go_inline', g:tcommentInlineC )
call tcomment#DefineType('go_block', g:tcommentBlockC )
call tcomment#DefineType('groovy', '// %s' ) call tcomment#DefineType('groovy', '// %s' )
call tcomment#DefineType('groovy_inline', g:tcommentInlineC ) call tcomment#DefineType('groovy_inline', g:tcommentInlineC )
call tcomment#DefineType('groovy_block', g:tcommentBlockC ) call tcomment#DefineType('groovy_block', g:tcommentBlockC )
@ -172,10 +247,12 @@ call tcomment#DefineType('java_doc_block', g:tcommentBlockC2 )
call tcomment#DefineType('jproperties', '# %s' ) call tcomment#DefineType('jproperties', '# %s' )
call tcomment#DefineType('lisp', '; %s' ) call tcomment#DefineType('lisp', '; %s' )
call tcomment#DefineType('lynx', '# %s' ) call tcomment#DefineType('lynx', '# %s' )
call tcomment#DefineType('matlab', '%% %s' )
call tcomment#DefineType('m4', 'dnl %s' ) call tcomment#DefineType('m4', 'dnl %s' )
call tcomment#DefineType('mail', '> %s' ) call tcomment#DefineType('mail', '> %s' )
call tcomment#DefineType('msidl', '// %s' ) call tcomment#DefineType('msidl', '// %s' )
call tcomment#DefineType('msidl_block', g:tcommentBlockC ) call tcomment#DefineType('msidl_block', g:tcommentBlockC )
call tcomment#DefineType('nginx', '# %s' )
call tcomment#DefineType('nroff', '.\\" %s' ) call tcomment#DefineType('nroff', '.\\" %s' )
call tcomment#DefineType('nsis', '# %s' ) call tcomment#DefineType('nsis', '# %s' )
call tcomment#DefineType('objc', '/* %s */' ) call tcomment#DefineType('objc', '/* %s */' )
@ -189,12 +266,13 @@ call tcomment#DefineType('pascal_inline', '(* %s *)' )
call tcomment#DefineType('pascal_block', "(*%s*)\n " ) call tcomment#DefineType('pascal_block', "(*%s*)\n " )
call tcomment#DefineType('perl', '# %s' ) call tcomment#DefineType('perl', '# %s' )
call tcomment#DefineType('perl_block', "=cut%s=cut" ) call tcomment#DefineType('perl_block', "=cut%s=cut" )
call tcomment#DefineType('php', '// %s' ) call tcomment#DefineType('php', {'commentstring_rx': '\%%(//\|#\) %s', 'commentstring': '// %s'})
call tcomment#DefineType('php_inline', g:tcommentInlineC ) call tcomment#DefineType('php_inline', g:tcommentInlineC )
call tcomment#DefineType('php_block', g:tcommentBlockC ) call tcomment#DefineType('php_block', g:tcommentBlockC )
call tcomment#DefineType('php_2_block', g:tcommentBlockC2 ) call tcomment#DefineType('php_2_block', g:tcommentBlockC2 )
call tcomment#DefineType('po', '# %s' ) call tcomment#DefineType('po', '# %s' )
call tcomment#DefineType('prolog', '%% %s' ) call tcomment#DefineType('prolog', '%% %s' )
call tcomment#DefineType('python', '# %s' )
call tcomment#DefineType('rc', '// %s' ) call tcomment#DefineType('rc', '// %s' )
call tcomment#DefineType('readline', '# %s' ) call tcomment#DefineType('readline', '# %s' )
call tcomment#DefineType('ruby', '# %s' ) call tcomment#DefineType('ruby', '# %s' )
@ -202,8 +280,12 @@ call tcomment#DefineType('ruby_3', '### %s' )
call tcomment#DefineType('ruby_block', "=begin rdoc%s=end") call tcomment#DefineType('ruby_block', "=begin rdoc%s=end")
call tcomment#DefineType('ruby_nodoc_block', "=begin%s=end" ) call tcomment#DefineType('ruby_nodoc_block', "=begin%s=end" )
call tcomment#DefineType('r', '# %s' ) call tcomment#DefineType('r', '# %s' )
call tcomment#DefineType('samba', '# %s' )
call tcomment#DefineType('sbs', "' %s" ) call tcomment#DefineType('sbs', "' %s" )
call tcomment#DefineType('scheme', '; %s' ) call tcomment#DefineType('scheme', '; %s' )
call tcomment#DefineType('scss', '// %s' )
call tcomment#DefineType('scss_inline', g:tcommentInlineC )
call tcomment#DefineType('scss_block', g:tcommentBlockC )
call tcomment#DefineType('sed', '# %s' ) call tcomment#DefineType('sed', '# %s' )
call tcomment#DefineType('sgml', '<!-- %s -->' ) call tcomment#DefineType('sgml', '<!-- %s -->' )
call tcomment#DefineType('sgml_inline', g:tcommentInlineXML) call tcomment#DefineType('sgml_inline', g:tcommentInlineXML)
@ -218,6 +300,7 @@ call tcomment#DefineType('spss_block', "* %s." )
call tcomment#DefineType('tcl', '# %s' ) call tcomment#DefineType('tcl', '# %s' )
call tcomment#DefineType('tex', '%% %s' ) call tcomment#DefineType('tex', '%% %s' )
call tcomment#DefineType('tpl', '<!-- %s -->' ) call tcomment#DefineType('tpl', '<!-- %s -->' )
call tcomment#DefineType('typoscript', '# %s' )
call tcomment#DefineType('viki', '%% %s' ) call tcomment#DefineType('viki', '%% %s' )
call tcomment#DefineType('viki_3', '%%%%%% %s' ) call tcomment#DefineType('viki_3', '%%%%%% %s' )
call tcomment#DefineType('viki_inline', '{cmt: %s}' ) call tcomment#DefineType('viki_inline', '{cmt: %s}' )
@ -250,18 +333,25 @@ let s:nullCommentString = '%s'
" tcomment#Comment(line1, line2, ?commentMode, ?commentAnyway, ?args...) " tcomment#Comment(line1, line2, ?commentMode, ?commentAnyway, ?args...)
" args... are either: " args... are either:
" 1. a list of key=value pairs where known keys are: " 1. a list of key=value pairs where known keys are (see also
" as=STRING ... Use a specific comment definition " |g:tcommentOptions|):
" col=N ... Start the comment at column N (in block mode; must " as=STRING ... Use a specific comment definition
" be smaller than |indent()|) " col=N ... Start the comment at column N (in block
" mode=STRING ... See the notes below on the "commentMode" argument " mode; must be smaller than |indent()|)
" begin=STRING ... Comment prefix " mode=STRING ... See the notes below on the "commentMode" argument
" end=STRING ... Comment postfix " begin=STRING ... Comment prefix
" middle=STRING ... Middle line comments in block mode " end=STRING ... Comment postfix
" rxbeg=N ... Regexp to find the substring of "begin" that " middle=STRING ... Middle line comments in block mode
" should be multipied by "count" " rxbeg=N ... Regexp to find the substring of "begin"
" rxend=N ... The above for "end" " that should be multipied by "count"
" rxmid=N ... The above for "middle" " rxend=N ... The above for "end"
" rxmid=N ... The above for "middle"
" commentstring_rx ... A regexp format string that matches
" commented lines (no new groups may be
" introduced, the |regexp| is |\V|; % have
" to be doubled); "commentstring", "begin"
" and optionally "end" must be defined or
" deducible.
" 2. 1-2 values for: ?commentPrefix, ?commentPostfix " 2. 1-2 values for: ?commentPrefix, ?commentPostfix
" 3. a dictionary (internal use only) " 3. a dictionary (internal use only)
" "
@ -285,13 +375,14 @@ function! tcomment#Comment(beg, end, ...)
if commentMode =~# 'i' if commentMode =~# 'i'
let commentMode = substitute(commentMode, '\Ci', line("'<") == line("'>") ? 'I' : 'G', 'g') let commentMode = substitute(commentMode, '\Ci', line("'<") == line("'>") ? 'I' : 'G', 'g')
endif endif
let [cstart, cend] = s:GetStartEnd(commentMode) let [lbeg, cbeg, lend, cend] = s:GetStartEnd(a:beg, a:end, commentMode)
" TLogVAR commentMode, cstart, cend " TLogVAR commentMode, lbeg, cbeg, lend, cend
" get the correct commentstring " get the correct commentstring
let cdef = copy(g:tcommentOptions)
if a:0 >= 3 && type(a:3) == 4 if a:0 >= 3 && type(a:3) == 4
let cdef = a:3 call extend(cdef, a:3)
else else
let cdef = s:GetCommentDefinition(a:beg, a:end, commentMode) call extend(cdef, s:GetCommentDefinition(lbeg, lend, commentMode))
let ax = 3 let ax = 3
if a:0 >= 3 && a:3 != '' && stridx(a:3, '=') == -1 if a:0 >= 3 && a:3 != '' && stridx(a:3, '=') == -1
let ax = 4 let ax = 4
@ -302,7 +393,7 @@ function! tcomment#Comment(beg, end, ...)
endif endif
endif endif
if a:0 >= ax if a:0 >= ax
call extend(cdef, s:ParseArgs(a:beg, a:end, commentMode, a:000[ax - 1 : -1])) call extend(cdef, s:ParseArgs(lbeg, lend, commentMode, a:000[ax - 1 : -1]))
endif endif
if !empty(get(cdef, 'begin', '')) || !empty(get(cdef, 'end', '')) if !empty(get(cdef, 'begin', '')) || !empty(get(cdef, 'end', ''))
let cdef.commentstring = s:EncodeCommentPart(get(cdef, 'begin', '')) let cdef.commentstring = s:EncodeCommentPart(get(cdef, 'begin', ''))
@ -315,15 +406,14 @@ function! tcomment#Comment(beg, end, ...)
call s:RepeatCommentstring(cdef) call s:RepeatCommentstring(cdef)
endif endif
" echom "DBG" string(cdef) string(a:000) " echom "DBG" string(cdef) string(a:000)
let cms0 = s:BlockGetCommentString(cdef) let cms0 = s:BlockGetCommentRx(cdef)
let cms0 = escape(cms0, '\')
" make whitespace optional; this conflicts with comments that require some " make whitespace optional; this conflicts with comments that require some
" whitespace " whitespace
let cmtCheck = substitute(cms0, '\([ ]\)', '\1\\?', 'g') let cmtCheck = substitute(cms0, '\([ ]\)', '\1\\?', 'g')
" turn commentstring into a search pattern " turn commentstring into a search pattern
let cmtCheck = s:SPrintF(cmtCheck, '\(\_.\{-}\)') let cmtCheck = printf(cmtCheck, '\(\_.\{-}\)')
" set commentMode and indentStr " set commentMode and indentStr
let [indentStr, uncomment] = s:CommentDef(a:beg, a:end, cmtCheck, commentMode, cstart, cend) let [indentStr, uncomment] = s:CommentDef(lbeg, lend, cmtCheck, commentMode, cbeg, cend)
" TLogVAR indentStr, uncomment " TLogVAR indentStr, uncomment
let col = get(cdef, 'col', -1) let col = get(cdef, 'col', -1)
if col >= 0 if col >= 0
@ -341,16 +431,19 @@ function! tcomment#Comment(beg, end, ...)
" go " go
if commentMode =~# 'B' if commentMode =~# 'B'
" We want a comment block " We want a comment block
call s:CommentBlock(a:beg, a:end, uncomment, cmtCheck, cdef, indentStr) call s:CommentBlock(lbeg, lend, uncomment, cmtCheck, cdef, indentStr)
else else
" call s:CommentLines(a:beg, a:end, cstart, cend, uncomment, cmtCheck, cms0, indentStr) " call s:CommentLines(lbeg, lend, cbeg, cend, uncomment, cmtCheck, cms0, indentStr)
" We want commented lines " We want commented lines
" final search pattern for uncommenting " final search pattern for uncommenting
let cmtCheck = escape('\V\^\(\s\{-}\)'. cmtCheck .'\$', '"/\') let cmtCheck = escape('\V\^\(\s\{-}\)'. cmtCheck .'\$', '"/\')
" final pattern for commenting " final pattern for commenting
let cmtReplace = escape(cms0, '"/') let cmtReplace = s:GetCommentReplace(cdef, cms0)
silent exec a:beg .','. a:end .'s/\V'. " echom "DBG tcomment#Comment" lbeg .','. lend .'s/\V'.
\ s:StartRx(cstart) . indentStr .'\zs\(\.\{-}\)'. s:EndRx(cend) .'/'. " \ s:StartPosRx(commentMode, lbeg, cbeg) . indentStr .'\zs\(\_.\{-}\)'. s:EndPosRx(commentMode, lend, cend) .'/'.
" \ '\=s:ProcessedLine('. uncomment .', submatch(0), "'. cmtCheck .'", "'. cmtReplace .'")/ge'
exec lbeg .','. lend .'s/\V'.
\ s:StartPosRx(commentMode, lbeg, cbeg) . indentStr .'\zs\(\_.\{-}\)'. s:EndPosRx(commentMode, lend, cend) .'/'.
\ '\=s:ProcessedLine('. uncomment .', submatch(0), "'. cmtCheck .'", "'. cmtReplace .'")/ge' \ '\=s:ProcessedLine('. uncomment .', submatch(0), "'. cmtCheck .'", "'. cmtReplace .'")/ge'
endif endif
" reposition cursor " reposition cursor
@ -364,27 +457,34 @@ function! tcomment#Comment(beg, end, ...)
endf endf
function! s:GetStartEnd(commentMode) "{{{3 function! s:GetStartEnd(beg, end, commentMode) "{{{3
let commentMode = a:commentMode if type(a:beg) == 3
if commentMode =~# 'R' || commentMode =~# 'I' let [lbeg, cbeg] = a:beg
let cstart = col("'<") let [lend, cend] = a:end
if cstart == 0
let cstart = col('.')
endif
if commentMode =~# 'R'
let commentMode = substitute(commentMode, '\CR', 'G', 'g')
let cend = 0
else
let cend = col("'>")
if commentMode =~# 'o'
let cend += 1
endif
endif
else else
let cstart = 0 let lbeg = a:beg
let cend = 0 let lend = a:end
let commentMode = a:commentMode
if commentMode =~# 'R' || commentMode =~# 'I'
let cbeg = col("'<")
if cbeg == 0
let cbeg = col('.')
endif
if commentMode =~# 'R'
let commentMode = substitute(commentMode, '\CR', 'G', 'g')
let cend = 0
else
let cend = col("'>")
if commentMode =~# 'o'
let cend += 1
endif
endif
else
let cbeg = 0
let cend = 0
endif
endif endif
return [cstart, cend] return [lbeg, cbeg, lend, cend]
endf endf
@ -434,7 +534,7 @@ function! tcomment#Operator(type, ...) "{{{3
let w:tcommentPos = getpos(".") let w:tcommentPos = getpos(".")
endif endif
let sel_save = &selection let sel_save = &selection
let &selection = "inclusive" set selection=inclusive
let reg_save = @@ let reg_save = @@
" let pos = getpos('.') " let pos = getpos('.')
" TLogVAR a:type " TLogVAR a:type
@ -445,6 +545,9 @@ function! tcomment#Operator(type, ...) "{{{3
elseif a:type == 'block' elseif a:type == 'block'
silent exe "normal! `[\<C-V>`]" silent exe "normal! `[\<C-V>`]"
let commentMode1 = 'I' let commentMode1 = 'I'
elseif a:type == 'char' && !g:tcomment#ignore_char_type
silent exe "normal! `[v`]"
let commentMode1 = 'I'
else else
silent exe "normal! `[v`]" silent exe "normal! `[v`]"
let commentMode1 = 'i' let commentMode1 = 'i'
@ -452,19 +555,32 @@ function! tcomment#Operator(type, ...) "{{{3
if empty(commentMode) if empty(commentMode)
let commentMode = commentMode1 let commentMode = commentMode1
endif endif
let beg = line("'[") let lbeg = line("'[")
let end = line("']") let lend = line("']")
let cbeg = col("'[")
let cend = col("']")
" echom "DBG tcomment#Operator" lbeg col("'[") col("'<") lend col("']") col("'>")
norm!  norm! 
let commentMode .= g:tcommentOpModeExtra let commentMode .= g:tcommentOpModeExtra
call tcomment#Comment(beg, end, commentMode.'o', bang) if a:type =~ 'line\|block' || g:tcomment#ignore_char_type
call tcomment#Comment(lbeg, lend, commentMode.'o', bang)
else
call tcomment#Comment([lbeg, cbeg], [lend, cend], commentMode.'o', bang)
endif
finally finally
let &selection = sel_save let &selection = sel_save
let @@ = reg_save let @@ = reg_save
if g:tcommentOpModeExtra !~ '>' if g:tcommentOpModeExtra !~ '>'
" TLogVAR pos " TLogVAR pos
" call setpos('.', pos) " call setpos('.', pos)
call setpos('.', w:tcommentPos) if exists('w:tcommentPos')
unlet! w:tcommentPos call setpos('.', w:tcommentPos)
unlet! w:tcommentPos
else
echohl WarningMsg
echom "TComment: w:tcommentPos wasn't set. Please report this to the plugin author"
echohl NONE
endif
endif endif
endtry endtry
endf endf
@ -518,6 +634,7 @@ endf
" collect all known comment types " collect all known comment types
" :nodoc:
function! tcomment#CollectFileTypes() function! tcomment#CollectFileTypes()
if s:typesDirty if s:typesDirty
let s:types = keys(s:definitions) let s:types = keys(s:definitions)
@ -530,12 +647,14 @@ call tcomment#CollectFileTypes()
" return a list of filetypes for which a tcomment_{&ft} is defined " return a list of filetypes for which a tcomment_{&ft} is defined
" :nodoc:
function! tcomment#Complete(ArgLead, CmdLine, CursorPos) "{{{3 function! tcomment#Complete(ArgLead, CmdLine, CursorPos) "{{{3
call tcomment#CollectFileTypes() call tcomment#CollectFileTypes()
let completions = copy(s:types) let completions = copy(s:types)
if index(completions, &filetype) != -1 let filetype = s:Filetype()
" TLogVAR &filetype if index(completions, filetype) != -1
call insert(completions, &filetype) " TLogVAR filetype
call insert(completions, filetype)
endif endif
if !empty(a:ArgLead) if !empty(a:ArgLead)
call filter(completions, 'v:val =~ ''\V\^''.a:ArgLead') call filter(completions, 'v:val =~ ''\V\^''.a:ArgLead')
@ -545,6 +664,7 @@ function! tcomment#Complete(ArgLead, CmdLine, CursorPos) "{{{3
endf endf
" :nodoc:
function! tcomment#CompleteArgs(ArgLead, CmdLine, CursorPos) "{{{3 function! tcomment#CompleteArgs(ArgLead, CmdLine, CursorPos) "{{{3
let completions = ['as=', 'col=', 'count=', 'mode=', 'begin=', 'end='] let completions = ['as=', 'col=', 'count=', 'mode=', 'begin=', 'end=']
if !empty(a:ArgLead) if !empty(a:ArgLead)
@ -587,67 +707,57 @@ function! s:GetCommentDefinition(beg, end, commentMode, ...)
endif endif
let cms = get(cdef, 'commentstring', '') let cms = get(cdef, 'commentstring', '')
if empty(cms) if empty(cms)
let filetype = s:Filetype()
if exists('b:commentstring') if exists('b:commentstring')
let cms = b:commentstring let cms = b:commentstring
return s:GetCustomCommentString(&filetype, a:commentMode, cms) return s:GetCustomCommentString(filetype, a:commentMode, cms)
elseif exists('b:commentStart') && b:commentStart != '' elseif exists('b:commentStart') && b:commentStart != ''
let cms = s:EncodeCommentPart(b:commentStart) .' %s' let cms = s:EncodeCommentPart(b:commentStart) .' %s'
if exists('b:commentEnd') && b:commentEnd != '' if exists('b:commentEnd') && b:commentEnd != ''
let cms = cms .' '. s:EncodeCommentPart(b:commentEnd) let cms = cms .' '. s:EncodeCommentPart(b:commentEnd)
endif endif
return s:GetCustomCommentString(&filetype, a:commentMode, cms) return s:GetCustomCommentString(filetype, a:commentMode, cms)
elseif g:tcommentGuessFileType || (exists('g:tcommentGuessFileType_'. &filetype) elseif g:tcommentGuessFileType || (exists('g:tcommentGuessFileType_'. filetype)
\ && g:tcommentGuessFileType_{&filetype} =~ '[^0]') \ && g:tcommentGuessFileType_{filetype} =~ '[^0]')
if g:tcommentGuessFileType_{&filetype} == 1 if g:tcommentGuessFileType_{filetype} == 1
let altFiletype = '' let altFiletype = ''
else else
let altFiletype = g:tcommentGuessFileType_{&filetype} let altFiletype = g:tcommentGuessFileType_{filetype}
endif endif
return s:GuessFileType(a:beg, a:end, a:commentMode, &filetype, altFiletype) return s:GuessFileType(a:beg, a:end, a:commentMode, filetype, altFiletype)
else else
return s:GetCustomCommentString(&filetype, a:commentMode, s:GuessCurrentCommentString(a:commentMode)) return s:GetCustomCommentString(filetype, a:commentMode, s:GuessCurrentCommentString(a:commentMode))
endif endif
let cdef.commentstring = cms let cdef.commentstring = cms
endif endif
return cdef return cdef
endf endf
" s:SPrintF(formatstring, ?values ...) function! s:StartPosRx(mode, line, col)
" => string if a:mode =~# 'I'
function! s:SPrintF(string, ...) return s:StartLineRx(a:line) . s:StartColRx(a:col)
let n = 1 else
let r = '' return s:StartColRx(a:col)
let s = a:string endif
while 1
let i = match(s, '%\(.\)')
if i >= 0
let x = s[i + 1]
let r = r . strpart(s, 0, i)
let s = strpart(s, i + 2)
if x == '%'
let r = r.'%'
else
if a:0 >= n
let v = a:{n}
let n = n + 1
else
echoerr 'Malformed format string (too many arguments required): '. a:string
endif
if x ==# 's'
let r = r.v
elseif x ==# 'S'
let r = r.'"'.v.'"'
else
echoerr 'Malformed format string: '. a:string
endif
endif
else
return r.s
endif
endwh
endf endf
function! s:StartRx(pos) function! s:EndPosRx(mode, line, col)
if a:mode =~# 'I'
return s:EndLineRx(a:line) . s:EndColRx(a:col)
else
return s:EndColRx(a:col)
endif
endf
function! s:StartLineRx(pos)
return '\%'. a:pos .'l'
endf
function! s:EndLineRx(pos)
return '\%'. a:pos .'l'
endf
function! s:StartColRx(pos)
if a:pos == 0 if a:pos == 0
return '\^' return '\^'
else else
@ -655,7 +765,7 @@ function! s:StartRx(pos)
endif endif
endf endf
function! s:EndRx(pos) function! s:EndColRx(pos)
if a:pos == 0 if a:pos == 0
return '\$' return '\$'
else else
@ -669,7 +779,8 @@ function! s:GetIndentString(line, start)
endf endf
function! s:CommentDef(beg, end, checkRx, commentMode, cstart, cend) function! s:CommentDef(beg, end, checkRx, commentMode, cstart, cend)
let mdrx = '\V'. s:StartRx(a:cstart) .'\s\*'. a:checkRx .'\s\*'. s:EndRx(0) let mdrx = '\V'. s:StartColRx(a:cstart) .'\s\*'. a:checkRx .'\s\*'. s:EndColRx(0)
" let mdrx = '\V'. s:StartPosRx(a:commentMode, a:beg, a:cstart) .'\s\*'. a:checkRx .'\s\*'. s:EndPosRx(a:commentMode, a:end, 0)
let line = getline(a:beg) let line = getline(a:beg)
if a:cstart != 0 && a:cend != 0 if a:cstart != 0 && a:cend != 0
let line = strpart(line, 0, a:cend - 1) let line = strpart(line, 0, a:cend - 1)
@ -714,32 +825,35 @@ function! s:ProcessedLine(uncomment, match, checkRx, replace)
if a:uncomment if a:uncomment
let rv = substitute(a:match, a:checkRx, '\1\2', '') let rv = substitute(a:match, a:checkRx, '\1\2', '')
else else
let rv = s:SPrintF(a:replace, a:match) let rv = printf(a:replace, a:match)
endif endif
" TLogVAR rv " TLogVAR rv
" let md = len(rv) - ml " let md = len(rv) - ml
let s:pos_end = getpos('.') let s:pos_end = getpos('.')
let s:pos_end[2] += len(rv) let s:pos_end[2] += len(rv)
" TLogVAR pe, md, a:match " TLogVAR pe, md, a:match
" TLogVAR rv
if v:version > 702 || (v:version == 702 && has('patch407')) if v:version > 702 || (v:version == 702 && has('patch407'))
let rv = escape(rv, ' ') let rv = escape(rv, ' ')
else else
let rv = escape(rv, '\ ') let rv = escape(rv, '\ ')
endif endif
let rv = substitute(rv, '\n', '\\\n', 'g') " TLogVAR rv
" let rv = substitute(rv, '\n', '\\\n', 'g')
" TLogVAR rv
return rv return rv
endf endf
function! s:CommentLines(beg, end, cstart, cend, uncomment, cmtCheck, cms0, indentStr) "{{{3 " function! s:CommentLines(beg, end, cstart, cend, uncomment, cmtCheck, cms0, indentStr) "{{{3
" We want commented lines " " We want commented lines
" final search pattern for uncommenting " " final search pattern for uncommenting
let cmtCheck = escape('\V\^\(\s\{-}\)'. a:cmtCheck .'\$', '"/\') " let cmtCheck = escape('\V\^\(\s\{-}\)'. a:cmtCheck .'\$', '"/\')
" final pattern for commenting " " final pattern for commenting
let cmtReplace = escape(a:cms0, '"/') " let cmtReplace = escape(a:cms0, '"/')
silent exec a:beg .','. a:end .'s/\V'. " silent exec a:beg .','. a:end .'s/\V'.
\ s:StartRx(a:cstart) . a:indentStr .'\zs\(\.\{-}\)'. s:EndRx(a:cend) .'/'. " \ s:StartColRx(a:cstart) . a:indentStr .'\zs\(\.\{-}\)'. s:EndColRx(a:cend) .'/'.
\ '\=s:ProcessedLine('. a:uncomment .', submatch(0), "'. a:cmtCheck .'", "'. cmtReplace .'")/ge' " \ '\=s:ProcessedLine('. a:uncomment .', submatch(0), "'. a:cmtCheck .'", "'. cmtReplace .'")/ge'
endf " endf
function! s:CommentBlock(beg, end, uncomment, checkRx, cdef, indentStr) function! s:CommentBlock(beg, end, uncomment, checkRx, cdef, indentStr)
let t = @t let t = @t
@ -763,7 +877,7 @@ function! s:CommentBlock(beg, end, uncomment, checkRx, cdef, indentStr)
let @t = substitute(@t, '^'. a:indentStr, '', 'g') let @t = substitute(@t, '^'. a:indentStr, '', 'g')
let @t = ms . substitute(@t, '\n'. a:indentStr, '\n'. mx, 'g') let @t = ms . substitute(@t, '\n'. a:indentStr, '\n'. mx, 'g')
endif endif
let @t = s:SPrintF(cs, "\n". @t ."\n") let @t = printf(cs, "\n". @t ."\n")
endif endif
silent norm! "tP silent norm! "tP
finally finally
@ -771,6 +885,14 @@ function! s:CommentBlock(beg, end, uncomment, checkRx, cdef, indentStr)
endtry endtry
endf endf
function! s:Filetype(...) "{{{3
let ft = a:0 >= 1 ? a:1 : &filetype
let ft = substitute(ft, '\..*$', '', '')
return ft
endf
" inspired by Meikel Brandmeyer's EnhancedCommentify.vim " inspired by Meikel Brandmeyer's EnhancedCommentify.vim
" this requires that a syntax names are prefixed by the filetype name " this requires that a syntax names are prefixed by the filetype name
" s:GuessFileType(beg, end, commentMode, filetype, ?fallbackFiletype) " s:GuessFileType(beg, end, commentMode, filetype, ?fallbackFiletype)
@ -790,7 +912,7 @@ function! s:GuessFileType(beg, end, commentMode, filetype, ...)
let le = len(getline(n)) let le = len(getline(n))
" TLogVAR m, le " TLogVAR m, le
while m < le while m < le
let syntaxName = synIDattr(synID(n, m, 1), 'name') let syntaxName = s:GetSyntaxName(n, m)
" TLogVAR syntaxName, n, m " TLogVAR syntaxName, n, m
let ftypeMap = get(g:tcommentSyntaxMap, syntaxName) let ftypeMap = get(g:tcommentSyntaxMap, syntaxName)
if !empty(ftypeMap) if !empty(ftypeMap)
@ -815,6 +937,21 @@ function! s:GuessFileType(beg, end, commentMode, filetype, ...)
return cdef return cdef
endf endf
function! s:GetSyntaxName(lnum, col) "{{{3
let syntaxName = synIDattr(synID(a:lnum, a:col, 1), 'name')
if !empty(g:tcomment#syntax_substitute)
for [rx, subdef] in items(g:tcomment#syntax_substitute)
if !has_key(subdef, 'if') || eval(subdef.if)
let syntaxName = substitute(syntaxName, rx, subdef.sub, 'g')
endif
endfor
endif
" TLogVAR syntaxName
return syntaxName
endf
function! s:CommentMode(commentMode, newmode) "{{{3 function! s:CommentMode(commentMode, newmode) "{{{3
return substitute(a:commentMode, '\w\+', a:newmode, 'g') return substitute(a:commentMode, '\w\+', a:newmode, 'g')
endf endf
@ -896,6 +1033,25 @@ function! s:GetCustomCommentString(ft, commentMode, ...)
return cdef return cdef
endf endf
function! s:GetCommentReplace(cdef, cms0)
if has_key(a:cdef, 'commentstring_rx')
let rs = s:BlockGetCommentString(a:cdef)
else
let rs = a:cms0
endif
return escape(rs, '"/')
endf
function! s:BlockGetCommentRx(cdef)
if has_key(a:cdef, 'commentstring_rx')
return a:cdef.commentstring_rx
else
let cms0 = s:BlockGetCommentString(a:cdef)
let cms0 = escape(cms0, '\')
return cms0
endif
endf
function! s:BlockGetCommentString(cdef) function! s:BlockGetCommentString(cdef)
if has_key(a:cdef, 'middle') if has_key(a:cdef, 'middle')
return a:cdef.commentstring return a:cdef.commentstring

View File

@ -1514,6 +1514,22 @@ g:netrw_use_nt_rcp pi_netrw.txt /*g:netrw_use_nt_rcp*
g:netrw_win95ftp pi_netrw.txt /*g:netrw_win95ftp* g:netrw_win95ftp pi_netrw.txt /*g:netrw_win95ftp*
g:netrw_winsize pi_netrw.txt /*g:netrw_winsize* g:netrw_winsize pi_netrw.txt /*g:netrw_winsize*
g:netrw_xstrlen pi_netrw.txt /*g:netrw_xstrlen* g:netrw_xstrlen pi_netrw.txt /*g:netrw_xstrlen*
g:tcomment#ignore_char_type tcomment.txt /*g:tcomment#ignore_char_type*
g:tcomment#syntax_substitute tcomment.txt /*g:tcomment#syntax_substitute*
g:tcommentBlankLines tcomment.txt /*g:tcommentBlankLines*
g:tcommentBlockC tcomment.txt /*g:tcommentBlockC*
g:tcommentBlockC2 tcomment.txt /*g:tcommentBlockC2*
g:tcommentBlockXML tcomment.txt /*g:tcommentBlockXML*
g:tcommentGuessFileType tcomment.txt /*g:tcommentGuessFileType*
g:tcommentGuessFileType_django tcomment.txt /*g:tcommentGuessFileType_django*
g:tcommentGuessFileType_dsl tcomment.txt /*g:tcommentGuessFileType_dsl*
g:tcommentGuessFileType_html tcomment.txt /*g:tcommentGuessFileType_html*
g:tcommentGuessFileType_php tcomment.txt /*g:tcommentGuessFileType_php*
g:tcommentGuessFileType_tskeleton tcomment.txt /*g:tcommentGuessFileType_tskeleton*
g:tcommentGuessFileType_vim tcomment.txt /*g:tcommentGuessFileType_vim*
g:tcommentIgnoreTypes_php tcomment.txt /*g:tcommentIgnoreTypes_php*
g:tcommentInlineC tcomment.txt /*g:tcommentInlineC*
g:tcommentInlineXML tcomment.txt /*g:tcommentInlineXML*
g:tcommentMapLeader1 tComment.txt /*g:tcommentMapLeader1* g:tcommentMapLeader1 tComment.txt /*g:tcommentMapLeader1*
g:tcommentMapLeader2 tComment.txt /*g:tcommentMapLeader2* g:tcommentMapLeader2 tComment.txt /*g:tcommentMapLeader2*
g:tcommentMapLeaderOp1 tComment.txt /*g:tcommentMapLeaderOp1* g:tcommentMapLeaderOp1 tComment.txt /*g:tcommentMapLeaderOp1*
@ -1522,6 +1538,8 @@ g:tcommentMapLeaderOp2 tComment.txt /*g:tcommentMapLeaderOp2*
g:tcommentMapLeaderOp2 tcomment.txt /*g:tcommentMapLeaderOp2* g:tcommentMapLeaderOp2 tcomment.txt /*g:tcommentMapLeaderOp2*
g:tcommentOpModeExtra tComment.txt /*g:tcommentOpModeExtra* g:tcommentOpModeExtra tComment.txt /*g:tcommentOpModeExtra*
g:tcommentOpModeExtra tcomment.txt /*g:tcommentOpModeExtra* g:tcommentOpModeExtra tcomment.txt /*g:tcommentOpModeExtra*
g:tcommentOptions tcomment.txt /*g:tcommentOptions*
g:tcommentSyntaxMap tcomment.txt /*g:tcommentSyntaxMap*
g:vimball_home pi_vimball.txt /*g:vimball_home* g:vimball_home pi_vimball.txt /*g:vimball_home*
g:vimball_mkdir pi_vimball.txt /*g:vimball_mkdir* g:vimball_mkdir pi_vimball.txt /*g:vimball_mkdir*
g:visincr_datedivset visincr.txt /*g:visincr_datedivset* g:visincr_datedivset visincr.txt /*g:visincr_datedivset*
@ -1782,17 +1800,13 @@ tComment-Key-Bindings tComment.txt /*tComment-Key-Bindings*
tComment-Usage tComment.txt /*tComment-Usage* tComment-Usage tComment.txt /*tComment-Usage*
tComment-commands tComment.txt /*tComment-commands* tComment-commands tComment.txt /*tComment-commands*
tComment.txt tComment.txt /*tComment.txt* tComment.txt tComment.txt /*tComment.txt*
tcomment#CollectFileTypes() tcomment.txt /*tcomment#CollectFileTypes()*
tcomment#Comment() tcomment.txt /*tcomment#Comment()* tcomment#Comment() tcomment.txt /*tcomment#Comment()*
tcomment#CommentAs() tcomment.txt /*tcomment#CommentAs()* tcomment#CommentAs() tcomment.txt /*tcomment#CommentAs()*
tcomment#Complete() tcomment.txt /*tcomment#Complete()*
tcomment#CompleteArgs() tcomment.txt /*tcomment#CompleteArgs()*
tcomment#DefineType() tcomment.txt /*tcomment#DefineType()* tcomment#DefineType() tcomment.txt /*tcomment#DefineType()*
tcomment#Operator() tcomment.txt /*tcomment#Operator()* tcomment#Operator() tcomment.txt /*tcomment#Operator()*
tcomment#OperatorAnyway() tcomment.txt /*tcomment#OperatorAnyway()* tcomment#OperatorAnyway() tcomment.txt /*tcomment#OperatorAnyway()*
tcomment#OperatorLine() tcomment.txt /*tcomment#OperatorLine()* tcomment#OperatorLine() tcomment.txt /*tcomment#OperatorLine()*
tcomment#OperatorLineAnyway() tcomment.txt /*tcomment#OperatorLineAnyway()* tcomment#OperatorLineAnyway() tcomment.txt /*tcomment#OperatorLineAnyway()*
tcomment#TypeExists() tcomment.txt /*tcomment#TypeExists()*
tcomment-maps tcomment.txt /*tcomment-maps* tcomment-maps tcomment.txt /*tcomment-maps*
tcomment.txt tcomment.txt /*tcomment.txt* tcomment.txt tcomment.txt /*tcomment.txt*
v_[% matchit.txt /*v_[%* v_[% matchit.txt /*v_[%*
@ -1806,6 +1820,7 @@ vimball-extract pi_vimball.txt /*vimball-extract*
vimball-history pi_vimball.txt /*vimball-history* vimball-history pi_vimball.txt /*vimball-history*
vimball-intro pi_vimball.txt /*vimball-intro* vimball-intro pi_vimball.txt /*vimball-intro*
vimball-manual pi_vimball.txt /*vimball-manual* vimball-manual pi_vimball.txt /*vimball-manual*
vimball-windows pi_vimball.txt /*vimball-windows*
viscinr-I visincr.txt /*viscinr-I* viscinr-I visincr.txt /*viscinr-I*
viscinr-contents visincr.txt /*viscinr-contents* viscinr-contents visincr.txt /*viscinr-contents*
visincr visincr.txt /*visincr* visincr visincr.txt /*visincr*

View File

@ -15,6 +15,9 @@ override the default choice.
TComment can properly handle an embedded syntax, e.g., ruby/python/perl TComment can properly handle an embedded syntax, e.g., ruby/python/perl
regions in vim scripts, HTML or JavaScript in php code etc. regions in vim scripts, HTML or JavaScript in php code etc.
Demo:
http://vimsomnia.blogspot.com/2010/11/tcomment-vim-plugin.html
*tcomment-maps* *tcomment-maps*
Key bindings~ Key bindings~
@ -34,9 +37,8 @@ and g:tcommentMapLeaderOp2):
gC{motion} :: Comment region gC{motion} :: Comment region
gCc :: Comment the current line gCc :: Comment the current line
*g:tcommentOpModeExtra*
By default the cursor stays put. If you want the cursor to the end of By default the cursor stays put. If you want the cursor to the end of
the commented text, set g:tcommentOpModeExtra to '>' (but this may not the commented text, set |g:tcommentOpModeExtra| to '>' (but this may not
work properly with exclusive motions). work properly with exclusive motions).
Primary key maps: Primary key maps:
@ -79,23 +81,38 @@ please make sure, you have the current version of vimball (vimscript
======================================================================== ========================================================================
Contents~ Contents~
:TComment ..................... |:TComment| :TComment ........................... |:TComment|
:TCommentAs ................... |:TCommentAs| :TCommentAs ......................... |:TCommentAs|
:TCommentRight ................ |:TCommentRight| :TCommentRight ...................... |:TCommentRight|
:TCommentBlock ................ |:TCommentBlock| :TCommentBlock ...................... |:TCommentBlock|
:TCommentInline ............... |:TCommentInline| :TCommentInline ..................... |:TCommentInline|
:TCommentMaybeInline .......... |:TCommentMaybeInline| :TCommentMaybeInline ................ |:TCommentMaybeInline|
tcomment#DefineType ........... |tcomment#DefineType()| g:tcommentBlankLines ................ |g:tcommentBlankLines|
tcomment#TypeExists ........... |tcomment#TypeExists()| g:tcommentOpModeExtra ............... |g:tcommentOpModeExtra|
tcomment#Comment .............. |tcomment#Comment()| g:tcommentOptions ................... |g:tcommentOptions|
tcomment#Operator ............. |tcomment#Operator()| g:tcomment#ignore_char_type ......... |g:tcomment#ignore_char_type|
tcomment#OperatorLine ......... |tcomment#OperatorLine()| g:tcommentGuessFileType ............. |g:tcommentGuessFileType|
tcomment#OperatorAnyway ....... |tcomment#OperatorAnyway()| g:tcommentGuessFileType_dsl ......... |g:tcommentGuessFileType_dsl|
tcomment#OperatorLineAnyway ... |tcomment#OperatorLineAnyway()| g:tcommentGuessFileType_php ......... |g:tcommentGuessFileType_php|
tcomment#CommentAs ............ |tcomment#CommentAs()| g:tcommentGuessFileType_html ........ |g:tcommentGuessFileType_html|
tcomment#CollectFileTypes ..... |tcomment#CollectFileTypes()| g:tcommentGuessFileType_tskeleton ... |g:tcommentGuessFileType_tskeleton|
tcomment#Complete ............. |tcomment#Complete()| g:tcommentGuessFileType_vim ......... |g:tcommentGuessFileType_vim|
tcomment#CompleteArgs ......... |tcomment#CompleteArgs()| g:tcommentGuessFileType_django ...... |g:tcommentGuessFileType_django|
g:tcommentIgnoreTypes_php ........... |g:tcommentIgnoreTypes_php|
g:tcomment#syntax_substitute ........ |g:tcomment#syntax_substitute|
g:tcommentSyntaxMap ................. |g:tcommentSyntaxMap|
g:tcommentBlockC .................... |g:tcommentBlockC|
g:tcommentBlockC2 ................... |g:tcommentBlockC2|
g:tcommentInlineC ................... |g:tcommentInlineC|
g:tcommentBlockXML .................. |g:tcommentBlockXML|
g:tcommentInlineXML ................. |g:tcommentInlineXML|
tcomment#DefineType ................. |tcomment#DefineType()|
tcomment#Comment .................... |tcomment#Comment()|
tcomment#Operator ................... |tcomment#Operator()|
tcomment#OperatorLine ............... |tcomment#OperatorLine()|
tcomment#OperatorAnyway ............. |tcomment#OperatorAnyway()|
tcomment#OperatorLineAnyway ......... |tcomment#OperatorLineAnyway()|
tcomment#CommentAs .................. |tcomment#CommentAs()|
======================================================================== ========================================================================
@ -163,30 +180,146 @@ plugin/tcomment.vim~
======================================================================== ========================================================================
autoload/tcomment.vim~ autoload/tcomment.vim~
*g:tcommentBlankLines*
g:tcommentBlankLines (default: 1)
If true, comment blank lines too
*g:tcommentOpModeExtra*
g:tcommentOpModeExtra (default: '')
Modifies how the operator works.
> ... Move the cursor to the end of the comment
*g:tcommentOptions*
g:tcommentOptions (default: {})
Other key-value options used by |tcomment#Comment()|.
Example: If you want to put the opening comment marker always in
the first column regardless of the block's indentation, put this
into your |vimrc| file: >
let g:tcommentOptions = {'col': 1}
<
*g:tcomment#ignore_char_type*
g:tcomment#ignore_char_type (default: 1)
|text-objects| for use with |tcomment#Operator| can have different
types: line, block, char etc. Text objects like aB, it, at etc.
have type char but this may not work reliably. By default,
tcomment handles those text objects most often as if they were of
type line. Set this variable to 0 in order to change this
behaviour. Be prepared that the result may not always match your
intentions.
*g:tcommentGuessFileType*
g:tcommentGuessFileType (default: 0)
Guess the file type based on syntax names always or for some fileformat only
If non-zero, try to guess filetypes.
tcomment also checks g:tcommentGuessFileType_{&filetype} for
filetype specific values.
Values:
0 ... don't guess
1 ... guess
FILETYPE ... assume this filetype
*g:tcommentGuessFileType_dsl*
g:tcommentGuessFileType_dsl (default: 'xml')
For dsl documents, assumet filetype = xml.
*g:tcommentGuessFileType_php*
g:tcommentGuessFileType_php (default: 'html')
In php documents, the php part is usually marked as phpRegion. We
thus assume that the buffers default comment style isn't php but
html.
*g:tcommentGuessFileType_html*
g:tcommentGuessFileType_html (default: 1)
*g:tcommentGuessFileType_tskeleton*
g:tcommentGuessFileType_tskeleton (default: 1)
*g:tcommentGuessFileType_vim*
g:tcommentGuessFileType_vim (default: 1)
*g:tcommentGuessFileType_django*
g:tcommentGuessFileType_django (default: 1)
*g:tcommentIgnoreTypes_php*
g:tcommentIgnoreTypes_php (default: 'sql')
In php files, some syntax regions are wongly highlighted as sql
markup. We thus ignore sql syntax when guessing the filetype in
php files.
*g:tcomment#syntax_substitute*
g:tcomment#syntax_substitute (default: {...})
*g:tcommentSyntaxMap*
g:tcommentSyntaxMap (default: {...})
tcomment guesses filetypes based on the name of the current syntax
region. This works well if the syntax names match
/filetypeSomeName/. Other syntax names have to be explicitly
mapped onto the corresponding filetype.
*g:tcommentBlockC*
g:tcommentBlockC (default: {...})
Generic c-like block comments.
*g:tcommentBlockC2*
g:tcommentBlockC2 (default: {...})
Generic c-like block comments (alternative markup).
*g:tcommentInlineC*
g:tcommentInlineC (default: "/* %s */")
Generic c-like comments.
*g:tcommentBlockXML*
g:tcommentBlockXML (default: "<!--%s-->\n ")
Generic xml-like block comments.
*g:tcommentInlineXML*
g:tcommentInlineXML (default: "<!-- %s -->")
Generic xml-like comments.
*tcomment#DefineType()* *tcomment#DefineType()*
tcomment#DefineType(name, commentdef) tcomment#DefineType(name, commentdef)
Currently this function just sets a variable If you don't explicitly define a comment style, |:TComment| will use
'commentstring' instead. We override the default values here in order
*tcomment#TypeExists()* to have a blank after the comment marker. Block comments work only if
tcomment#TypeExists(name) we explicitly define the markup.
Return 1 if a comment type is defined.
The comment definition can be either a string or a dictionary.
If it is a string:
The format for block comments is similar to 'commentstrings' with the
exception that the format strings for blocks can contain a second line
that defines how "middle lines" (see :h format-comments) should be
displayed.
If it is a dictionary:
See the help on the args argument of |tcomment#Comment|.
*tcomment#Comment()* *tcomment#Comment()*
tcomment#Comment(beg, end, ...) tcomment#Comment(beg, end, ...)
tcomment#Comment(line1, line2, ?commentMode, ?commentAnyway, ?args...) tcomment#Comment(line1, line2, ?commentMode, ?commentAnyway, ?args...)
args... are either: args... are either:
1. a list of key=value pairs where known keys are: 1. a list of key=value pairs where known keys are (see also
as=STRING ... Use a specific comment definition |g:tcommentOptions|):
col=N ... Start the comment at column N (in block mode; must as=STRING ... Use a specific comment definition
be smaller than |indent()|) col=N ... Start the comment at column N (in block
mode=STRING ... See the notes below on the "commentMode" argument mode; must be smaller than |indent()|)
begin=STRING ... Comment prefix mode=STRING ... See the notes below on the "commentMode" argument
end=STRING ... Comment postfix begin=STRING ... Comment prefix
middle=STRING ... Middle line comments in block mode end=STRING ... Comment postfix
rxbeg=N ... Regexp to find the substring of "begin" that middle=STRING ... Middle line comments in block mode
should be multipied by "count" rxbeg=N ... Regexp to find the substring of "begin"
rxend=N ... The above for "end" that should be multipied by "count"
rxmid=N ... The above for "middle" rxend=N ... The above for "end"
rxmid=N ... The above for "middle"
commentstring_rx ... A regexp format string that matches
commented lines (no new groups may be
introduced, the |regexp| is |\V|; % have
to be doubled); "commentstring", "begin"
and optionally "end" must be defined or
deducible.
2. 1-2 values for: ?commentPrefix, ?commentPostfix 2. 1-2 values for: ?commentPrefix, ?commentPostfix
3. a dictionary (internal use only) 3. a dictionary (internal use only)
@ -221,17 +354,6 @@ tcomment#CommentAs(beg, end, commentAnyway, filetype, ?args...)
|tcomment#Comment()|) |tcomment#Comment()|)
comment text as if it were of a specific filetype comment text as if it were of a specific filetype
*tcomment#CollectFileTypes()*
tcomment#CollectFileTypes()
collect all known comment types
*tcomment#Complete()*
tcomment#Complete(ArgLead, CmdLine, CursorPos)
return a list of filetypes for which a tcomment_{&ft} is defined
*tcomment#CompleteArgs()*
tcomment#CompleteArgs(ArgLead, CmdLine, CursorPos)
vim:tw=78:fo=tcq2:isk=!-~,^*,^|,^":ts=8:ft=help:norl: vim:tw=78:fo=tcq2:isk=!-~,^*,^|,^":ts=8:ft=help:norl:

View File

@ -2,15 +2,14 @@
" @Author: Tom Link (micathom AT gmail com) " @Author: Tom Link (micathom AT gmail com)
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt) " @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 27-Dez-2004. " @Created: 27-Dez-2004.
" @Last Change: 2010-10-02. " @Last Change: 2011-05-26.
" @Revision: 709 " @Revision: 720
" " GetLatestVimScripts: 1173 1 tcomment.vim
" GetLatestVimScripts: 1173 1 tComment.vim
if &cp || exists('loaded_tcomment') if &cp || exists('loaded_tcomment')
finish finish
endif endif
let loaded_tcomment = 201 let loaded_tcomment = 205
if !exists("g:tcommentMapLeader1") if !exists("g:tcommentMapLeader1")
" g:tcommentMapLeader1 should be a shortcut that can be used with " g:tcommentMapLeader1 should be a shortcut that can be used with
@ -28,9 +27,6 @@ endif
if !exists("g:tcommentMapLeaderOp2") if !exists("g:tcommentMapLeaderOp2")
let g:tcommentMapLeaderOp2 = 'gC' let g:tcommentMapLeaderOp2 = 'gC'
endif endif
if !exists("g:tcommentOpModeExtra")
let g:tcommentOpModeExtra = ''
endif
" :display: :[range]TComment[!] ?ARGS... " :display: :[range]TComment[!] ?ARGS...
@ -137,119 +133,10 @@ if (g:tcommentMapLeaderOp1 != '')
exec 'nnoremap <silent> '. g:tcommentMapLeaderOp1 .' :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#Operator<cr>g@' exec 'nnoremap <silent> '. g:tcommentMapLeaderOp1 .' :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#Operator<cr>g@'
exec 'nnoremap <silent> '. g:tcommentMapLeaderOp1 .'c :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorLine<cr>g@$' exec 'nnoremap <silent> '. g:tcommentMapLeaderOp1 .'c :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorLine<cr>g@$'
exec 'xnoremap <silent> '. g:tcommentMapLeaderOp1 .' :TCommentMaybeInline<cr>' exec 'xnoremap <silent> '. g:tcommentMapLeaderOp1 .' :TCommentMaybeInline<cr>'
endif endif
if (g:tcommentMapLeaderOp2 != '') if (g:tcommentMapLeaderOp2 != '')
exec 'nnoremap <silent> '. g:tcommentMapLeaderOp2 .' :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorAnyway<cr>g@' exec 'nnoremap <silent> '. g:tcommentMapLeaderOp2 .' :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorAnyway<cr>g@'
exec 'nnoremap <silent> '. g:tcommentMapLeaderOp2 .'c :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorLineAnyway<cr>g@$' exec 'nnoremap <silent> '. g:tcommentMapLeaderOp2 .'c :let w:tcommentPos = getpos(".") \| set opfunc=tcomment#OperatorLineAnyway<cr>g@$'
exec 'xnoremap <silent> '. g:tcommentMapLeaderOp2 .' :TCommentMaybeInline<cr>' exec 'xnoremap <silent> '. g:tcommentMapLeaderOp2 .' :TCommentMaybeInline<cr>'
endif endif
finish
-----------------------------------------------------------------------
History
0.1
- Initial release
0.2
- Fixed uncommenting of non-aligned comments
- improved support for block comments (with middle lines and indentation)
- using TCommentBlock for file types that don't have block comments creates
single line comments
- removed the TCommentAsBlock command (TCommentAs provides its functionality)
- removed g:tcommentSetCMS
- the default key bindings have slightly changed
1.3
- slightly improved recognition of embedded syntax
- if no commentstring is defined in whatever way, reconstruct one from
&comments
- The TComment... commands now have bang variants that don't act as toggles
but always comment out the selected text
- fixed problem with commentstrings containing backslashes
- comment as visual block (allows commenting text to the right of the main
text, i.e., this command doesn't work on whole lines but on the text to the
right of the cursor)
- enable multimode for dsl, vim filetypes
- added explicit support for some other file types I ran into
1.4
- Fixed problem when &commentstring was invalid (e.g. lua)
- perl_block by Kyosuke Takayama.
- <c-_>s mapped to :TCommentAs <c-r>=&ft<cr>
1.5
- "Inline" visual comments (uses the &filetype_inline style if
available; doesn't check if the filetype actually supports this kind of
comments); tComment can't currently deduce inline comment styles from
&comments or &commentstring (I personally hardly ever use them); default
map: <c-_>i or <c-_>I
- In visual mode: if the selection spans several lines, normal mode is
selected; if the selection covers only a part of one line, inline mode
is selected
- Fixed problem with lines containing ^M or ^@ characters.
- It's no longer necessary to call TCommentCollectFileTypes() after
defining a new filetype via TCommentDefineType()
- Disabled single <c-_> mappings
- Renamed TCommentVisualBlock to TCommentRight
- FIX: Forgot 'x' in ExtractCommentsPart() (thanks to Fredrik Acosta)
1.6
- Ignore sql when guessing the comment string in php files; tComment
sometimes chooses the wrong comment string because the use of sql syntax
is used too loosely in php files; if you want to comment embedded sql
code you have to use TCommentAs
- Use keepjumps in commands.
- Map <c-_>p & <L>_p to vip:TComment<cr>
- Made key maps configurable via g:tcommentMapLeader1 and
g:tcommentMapLeader2
1.7
- gc{motion} (see g:tcommentMapLeaderOp1) functions as a comment toggle
operator (i.e., something like gcl... works, mostly); gC{motion} (see
g:tcommentMapLeaderOp2) will unconditionally comment the text.
- TCommentAs takes an optional second argument (the comment level)
- New "n" map: TCommentAs &filetype [COUNT]
- Defined mail comments/citations
- g:tcommentSyntaxMap: Map syntax names to filetypes for buffers with
mixed syntax groups that don't match the filetypeEmbeddedsyntax scheme (e.g.
'vimRubyRegion', which should be commented as ruby syntax, not as vim
syntax)
- FIX: Comments in vim*Region
- TComment: The use of the type argument has slightly changed (IG -> i,
new: >)
1.8
- Definitly require vim7
- Split the plugin into autoload & plugin.
- g:TCommentFileTypes is a list
- Fixed some block comment strings
- Removed extraneous newline in some block comments.
- Maps for visal mode (thanks Krzysztof Goj)
1.9
- Fix left offset for inline comments (via operator binding)
1.10
- tcomment#Operator defines w:tcommentPos if invoked repeatedly
- s:GuessFileType: use len(getline()) instead of col()
1.11
- Support for erlang (thanks to Zhang Jinzhu)
1.12
- Moved the definition of some variables from plugin/tComment.vim to
autoload/tcomment.vim
- Changed comment string for eruby (proposed by Vinicius Baggio)
- Support for x86conf
2.0
- Enabled key=value pairs to configure commenting
- Renamed the file plugin/tComment.vim to plugin/tcomment.vim
- Renamed certain global functions to tcomment#...
2.1
- FIX