Skip to content

Commit 629f079

Browse files
committed
Added BlockComment to repository.
1 parent 02614f8 commit 629f079

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed

plugin/BlockComment.vim

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
" BlockComment.vim
2+
" Author: Chris Russell
3+
" Version: 1.1
4+
" License: GPL v2.0
5+
"
6+
" Description:
7+
" This script defineds functions and key mappings to block comment code.
8+
"
9+
" Help:
10+
" In brief, use '.c' to comment and '.C' to uncomment.
11+
"
12+
" Both commenting and uncommenting can be run on N lines at a time by
13+
" using a number before the command. They both support visual mode and
14+
" ranges.
15+
"
16+
" This script will not comment lines with an indent level less that the
17+
" initial line of the comment to preserve the control structure of code.
18+
"
19+
" Installation:
20+
" Simply drop this file into your plugin directory.
21+
"
22+
" Homepage: http://www.vim.org/scripts/script.php?script_id=473
23+
"
24+
" Changelog:
25+
" 2002-11-08 v1.1
26+
" Convert to Unix eol
27+
" 2002-11-05 v1.0
28+
" Initial release
29+
"
30+
" TODO:
31+
" Add more file types
32+
"
33+
34+
35+
"--------------------------------------------------
36+
" Avoid multiple sourcing
37+
"--------------------------------------------------
38+
if exists( "loaded_block_comment" )
39+
finish
40+
endif
41+
let loaded_block_comment = 1
42+
43+
44+
"--------------------------------------------------
45+
" Key mappings
46+
"--------------------------------------------------
47+
noremap <silent> .c :call Comment()<CR>
48+
noremap <silent> .C :call UnComment()<CR>
49+
50+
51+
"--------------------------------------------------
52+
" Set comment characters by filetype
53+
"--------------------------------------------------
54+
function! CommentStr()
55+
let s:comment_pad = '--------------------------------------------------'
56+
if &ft == "vim"
57+
let s:comment_strt = '"'
58+
let s:comment_mid0 = '" '
59+
let s:comment_mid1 = '"'
60+
let s:comment_stop = ' '
61+
let s:comment_bkup = 0
62+
elseif &ft == "c" || &ft == "css"
63+
let s:comment_strt = '/*'
64+
let s:comment_mid0 = '* '
65+
let s:comment_mid1 = '*'
66+
let s:comment_stop = '*/'
67+
let s:comment_bkup = 1
68+
let s:comment_strtbak = '/ *'
69+
let s:comment_stopbak = '* /'
70+
elseif &ft == "cpp" || &ft == "java" || &ft == "javascript" || &ft == "php"
71+
let s:comment_strt = '//'
72+
let s:comment_mid0 = '// '
73+
let s:comment_mid1 = '//'
74+
let s:comment_stop = ' '
75+
let s:comment_bkup = 0
76+
elseif &ft == "asm" || &ft == "lisp" || &ft == "scheme"
77+
let s:comment_strt = ';'
78+
let s:comment_mid0 = '; '
79+
let s:comment_mid1 = ';'
80+
let s:comment_stop = ' '
81+
let s:comment_bkup = 0
82+
elseif &ft == "vb"
83+
let s:comment_strt = '\''
84+
let s:comment_mid0 = '\' '
85+
let s:comment_mid1 = '\''
86+
let s:comment_stop = ' '
87+
let s:comment_bkup = 0
88+
elseif &ft == "html" || &ft == "xml" || &ft == "entity"
89+
let s:comment_strt = '<!--'
90+
let s:comment_mid0 = '! '
91+
let s:comment_mid1 = '!'
92+
let s:comment_stop = '-->'
93+
let s:comment_bkup = 1
94+
let s:comment_strtbak = '< !--'
95+
let s:comment_stopbak = '-- >'
96+
else
97+
let s:comment_strt = '#'
98+
let s:comment_mid0 = '# '
99+
let s:comment_mid1 = '#'
100+
let s:comment_stop = ' '
101+
let s:comment_bkup = 0
102+
endif
103+
endfunction
104+
105+
"--------------------------------------------------
106+
" Comment a block of code
107+
"--------------------------------------------------
108+
function! Comment() range
109+
" range variables
110+
let l:firstln = a:firstline
111+
let l:lastln = a:lastline
112+
" get comment chars
113+
call CommentStr()
114+
" get tab indent level
115+
let l:indent = indent( l:firstln ) / &tabstop
116+
" loop to get padding str
117+
let l:pad = ""
118+
let l:i = 0
119+
while l:i < l:indent
120+
let l:pad = l:pad . "\t"
121+
let l:i = l:i + 1
122+
endwhile
123+
" loop for each line
124+
let l:block = 0
125+
let l:midline = l:firstln
126+
while l:midline <= l:lastln
127+
" get line
128+
let l:line = getline( l:midline )
129+
" check if padding matches
130+
if strpart( l:line, 0, l:indent ) == l:pad
131+
" start comment block
132+
if l:block == 0
133+
call append( l:midline - 1, l:pad . s:comment_strt . s:comment_pad )
134+
let l:midline = l:midline + 1
135+
let l:lastln = l:lastln + 1
136+
let l:block = 1
137+
endif
138+
" append comment between indent and code
139+
let l:line = strpart( l:line, l:indent )
140+
" handle comments within comments
141+
if s:comment_bkup == 1
142+
let l:line = substitute( l:line, escape( s:comment_strt, '\*^$.~[]' ), s:comment_strtbak, "g" )
143+
let l:line = substitute( l:line, escape( s:comment_stop, '\*^$.~[]' ), s:comment_stopbak, "g" )
144+
endif
145+
call setline( l:midline, l:pad . s:comment_mid0 . l:line )
146+
" else end block
147+
elseif l:block == 1
148+
call append( l:midline - 1, l:pad . s:comment_mid1 . s:comment_pad . s:comment_stop )
149+
let l:midline = l:midline + 1
150+
let l:lastln = l:lastln + 1
151+
let l:block = 0
152+
endif
153+
let l:midline = l:midline + 1
154+
endwhile
155+
" end block
156+
if l:block == 1
157+
call append( l:lastln, l:pad . s:comment_mid1 . s:comment_pad . s:comment_stop )
158+
endif
159+
" return to first line of comment
160+
execute l:firstln
161+
endfunction
162+
163+
"--------------------------------------------------
164+
" Uncomment a block of code
165+
"--------------------------------------------------
166+
function! UnComment() range
167+
" range variables
168+
let l:firstln = a:firstline
169+
let l:lastln = a:lastline
170+
" get comment chars
171+
call CommentStr()
172+
" get length of comment string
173+
let l:clen = strlen( s:comment_mid0 )
174+
" loop for each line
175+
let l:midline = l:firstln
176+
while l:midline <= l:lastln
177+
" get indent level - process indent for each line instead of by block
178+
let l:indent = indent( l:midline ) / &tabstop
179+
let l:line = getline( l:midline )
180+
" begin comment block line - delete line
181+
if strpart( l:line, l:indent ) == s:comment_strt . s:comment_pad
182+
execute l:midline . "d"
183+
let l:midline = l:midline - 1
184+
let l:lastln = l:lastln - 1
185+
" end comment block line - delete line
186+
elseif strpart( l:line, l:indent ) == s:comment_mid1 . s:comment_pad . s:comment_stop
187+
execute l:midline . "d"
188+
let l:midline = l:midline - 1
189+
let l:lastln = l:lastln - 1
190+
" commented code line - remove comment
191+
elseif strpart( l:line, l:indent, l:clen ) == s:comment_mid0
192+
let l:pad = strpart( l:line, 0, l:indent )
193+
let l:line = strpart( l:line, l:indent + l:clen )
194+
" handle comments within comments
195+
if s:comment_bkup == 1
196+
let l:line = substitute( l:line, escape( s:comment_strtbak, '\*^$.~[]' ), s:comment_strt, "g" )
197+
let l:line = substitute( l:line, escape( s:comment_stopbak, '\*^$.~[]' ), s:comment_stop, "g" )
198+
endif
199+
call setline( l:midline, l:pad . l:line )
200+
endif
201+
let l:midline = l:midline + 1
202+
endwhile
203+
" look at line above block
204+
let l:indent = indent( l:firstln - 1 ) / &tabstop
205+
let l:line = getline( l:firstln - 1 )
206+
" abandoned begin comment block line - delete line
207+
if strpart( l:line, l:indent ) == s:comment_strt . s:comment_pad
208+
execute ( l:firstln - 1 ) . "d"
209+
let l:firstln = l:firstln - 1
210+
let l:lastln = l:lastln - 1
211+
" abandoned commented code line - insert end comment block line
212+
elseif strpart( l:line, l:indent, l:clen ) == s:comment_mid0
213+
let l:pad = strpart( l:line, 0, l:indent )
214+
call append( l:firstln - 1, l:pad . s:comment_mid1 . s:comment_pad . s:comment_stop )
215+
let l:lastln = l:lastln + 1
216+
endif
217+
" look at line belowe block
218+
let l:indent = indent( l:lastln + 1 ) / &tabstop
219+
let l:line = getline( l:lastln + 1 )
220+
" abandoned end comment block line - delete line
221+
if strpart( l:line, l:indent ) == s:comment_mid1 . s:comment_pad . s:comment_stop
222+
execute ( l:lastln + 1 ) . "d"
223+
let l:lastln = l:lastln - 1
224+
" abandoned commented code line - insert begin comment block line
225+
elseif strpart( l:line, l:indent, l:clen ) == s:comment_mid0
226+
let l:pad = strpart( l:line, 0, l:indent )
227+
call append( l:lastln, l:pad . s:comment_strt . s:comment_pad )
228+
endif
229+
endfunction
230+

0 commit comments

Comments
 (0)