-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2002-11-05 1.51 FeralToggleCommentify.vim
521 lines (486 loc) · 17.7 KB
/
2002-11-05 1.51 FeralToggleCommentify.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
" {{{ File header information
" vim:ff=unix ts=4 ss=4
" vim60:fdm=marker
" \file FeralToggleCommentify.vim
"
" \brief Adds, removes or toggles comment characters on col1 at the touch
" of a key. Ranges are supported as are custom text to
" insert/remove/toggle.
" Maintainer: (of this version anyway)
" \author Robert KellyIV <[email protected]> (Rot13ed)
" Based On: {{{
" \note Based On Vincent Nijs's ToggleCommentify.vim v1.2 Last Change:
" Sunday, June 9th, 2001
" \author Vincent Nijs <[email protected]>
" \note Some comment definitions extracted from EnhancedCommentify.vim by
" Meikel Brandmeyer
" \author Meikel Brandmeyer <[email protected]>
" }}}
"
" \date Tue, 05 Nov 2002 15:18 Pacific Standard Time
" \version $Id: feraltogglecommentify.vim,v 1.5 2002/11/04 10:12:17 root Exp $
" Version: 1.51
" History: {{{
" [Feral:308/02@02:11] 1.51
" Hacked in eol comments and added html's <!-- --> comments.
" [Feral:300/02@07:03] 1.5
" Rewrite, condenced toggle, comment and uncomment into one function,
" much less duplicate code this way, hehe.
" Now saves the cursor position (I believe) properly.
" TODO could be better by handeling ranges internal.
" [Feral:249/02@00:41] v1.4:
" Bugfix, a comment char of + failed.
" Modification: commands always place cursor on col 1 this is for
" consistancy... comment left on col1, uncomment on first word.
" I would prefer the cursor's column be unchanged however a range
" command starts with cursor on col1, thus the cursor col would have to
" be saved in a map or something that calls the commands.
"
" Sat, 20 Jul 2002 02:26:29 Pacific Daylight Time: v1.3
" Massive changes. Proper handling of ranges (not that it didn't work before
" mind you)
" Made ToggleCommentify, UnCommentify and Commentify commands (:TC, :UC and
" :CC respectivly) so you can choose what to do to a line, or line
" range. (I like to comment off entire sections of code, sometimes those
" sections contain line comments.. toggleing in this instance has
" undesired results.) Now I can simply highlight and :CC
"
" Another (possibly) nice addition is the ability to specify the comment
" symbol on after the command, I.e. :CC // which would add // chars to
" the start of the range of lines.
"
" The range defaults to the currnet line, as per VIM documentation.
"
" }}}
"
" Note: If someone wants this to not work on blank lines or has comment
" strings or any other suggestions just email me and I'll look into
" it.
"
" Examples:
" :CC--
" :CC --
" " spaces are ate between the command and the string, if you want a space
" use \ , i.e.
" :CC\ --
"
" Holding: Based on {{{
"
" ToggleCommentify.vim
" Maintainer: Vincent Nijs <[email protected]>
" Version: 1.2
" Last Change: Sunday, June 9th, 2001
" Disription:
" This is a (very) simple script to comment lines in a program. Currently supported languages are
" C, C++, PHP, the vim scripting language, python, and ox. Given the simplicity of the program it
" very easy to add support for new languages. The comments in the file should provide sufficient
" information on how to proceed.
" Install Details:
" You can put the functions in the attachment directly into your .vimrc or in a separate file to
" be sourced. If you choose for the latter option add a statement such as ...
" execute source ~/vim/myVimFiles/ToggleCommentify.vim |" DO PUT source ... vim BETWEEN DOUBLE QUOTES !!)
" ... to your .vimrc file
" To call the functions add the following mappings in your .vimrc.
" map <M-c> :call ToggleCommentify()<CR>j
" imap <M-c> <ESC>:call ToggleCommentify()<CR>j
" The nice thing about these mapping is that you don't have to select a visual block to comment
" ... just keep the ALT-key pressed down and tap on 'c' as often as you need.
" Note: some people have reported that <M-c> doesn't work for them ... try <\-c> instead.
" [Feral:201/02@02:57] Old version; what I have been using (works great!) up
" untill I got playing with this :) .. changes from when I downloaded from
" vim.sf.net ... few more fileTypes recognised ... I think a bug fix with the
" makefile file type (been a long while thought..).. changed how isCommented
" is calcualted to accomidate longer commentsymbols.
"function! ToggleCommentify(Style) " {{{
" let lineString = getline(".")
" " [Feral:201/02@01:15] But but I want to comment empty lines!
"" if lineString != $ " don't comment empty lines
" let fileType = &ft " finding out the file-type, and specifying the comment symbol
" " {{{ Supported file types each have an if here, look at 'vim' filetype as an example.
" " [Feral:201/02@01:17] ftf is my hypertext markup format. (which is to say txt with a few special chars)
" if fileType == 'ox' || fileType == 'cpp' || fileType == 'php' || fileType == 'java'
" let commentSymbol = '//'
" elseif fileType == 'vim'
" let commentSymbol = '"'
" elseif fileType == 'lisp' || fileType == 'scheme' || fileType == 'dosini'
" let commentSymbol = ';'
" elseif fileType == 'tex'
" let commentSymbol = '%'
" elseif fileType == 'caos'
" let commentSymbol = '*'
" elseif fileType == 'm4' || fileType == 'config' || fileType == 'automake'
" let commentSymbol = 'dnl '
" elseif fileType == 'python' || fileType == 'perl' || fileType == 'make' || fileType =~ '[^w]sh$' || fileType == 'tcl' || fileType == 'jproperties'
" let commentSymbol = '#'
" elseif fileType == 'vb' || fileType == 'aspvbs'
" let commentSymbol == "'"
" elseif fileType == 'plsql' || fileType == 'lua'
" let commentSymbol = '--'
" else
"" execute 'echo "ToggleCommentify has not (yet) been implemented for this file-type"'
"" let commentSymbol = ''
"" execute 'echo "ToggleCommentify: Unknown filetype, defaulting to CPP style //"'
" echo "ToggleCommentify: Unknown filetype, defaulting to CPP style //"
" let commentSymbol = '//'
" endif
" " }}}
"
" " [Feral:201/02@01:24] toggle the comment (what it was, and default)
" if a:Style == 'c'
" call Commentify(commentSymbol)
" elseif a:Style == 'u'
" call UnCommentify(commentSymbol)
" else
" let isCommented = strpart(lineString,0,strlen(commentSymbol) ) " FERAL: extract the first x chars of the line, where x is the width/length of the comment symbol.
" if isCommented == commentSymbol
" call UnCommentify(commentSymbol) " if the line is already commented, uncomment
" else
" call Commentify(commentSymbol) " if the line is uncommented, comment
" endif
" endif
"
"" endif
"endfunction " }}}
" }}}
"
" }}}
if exists("loaded_feraltogglecommentify")
finish
endif
let loaded_feraltogglecommentify = 1
" Look to the vim entry here to add your own file types.
function s:FindCommentify() " {{{
" finding out the file-type, and specifying the comment symbol
let fileType = &ft
if fileType == 'ox' || fileType == 'cpp' || fileType == 'php' || fileType == 'java'
let commentSymbol_L = '//'
let commentSymbol_R = ''
" [Feral:201/02@01:17] ftf is my hypertext markup format. (which is to say txt with a few special chars)
elseif fileType == 'ftf'
let commentSymbol_L = '//'
let commentSymbol_R = ''
"[Feral:283/02@04:14] torque-script
elseif fileType == 'torquescript'
let commentSymbol_L = '//'
let commentSymbol_R = ''
"[Feral:303/02@19:20] fte
elseif fileType == 'fte'
let commentSymbol_L = 'FTE:'
let commentSymbol_R = ''
"[Feral:308/02@02:02] html -- first start/end comment
elseif fileType == 'html'
let commentSymbol_L = '<!-- '
let commentSymbol_R = ' -->'
"[Feral:308/02@02:03] old style c /* */ comments; just because. -- not testing it now thought!
" elseif fileType == 'c'
" let commentSymbol_L = '/*'
" let commentSymbol_R = '*/'
" The rest...
elseif fileType == 'pov'
let commentSymbol_L = '//'
let commentSymbol_R = ''
elseif fileType == 'vim'
let commentSymbol_L = '"'
let commentSymbol_R = ''
elseif fileType == 'lisp' || fileType == 'scheme' || fileType == 'dosini'
let commentSymbol_L = ';'
let commentSymbol_R = ''
elseif fileType == 'tex'
let commentSymbol_L = '%'
let commentSymbol_R = ''
elseif fileType == 'caos'
let commentSymbol_L = '*'
let commentSymbol_R = ''
elseif fileType == 'm4' || fileType == 'config' || fileType == 'automake'
let commentSymbol_L = 'dnl '
let commentSymbol_R = ''
elseif fileType == 'python' || fileType == 'perl' || fileType == 'make' || fileType =~ '[^w]sh$' || fileType == 'tcl' || fileType == 'jproperties'
let commentSymbol_L = '#'
let commentSymbol_R = ''
elseif fileType == 'vb' || fileType == 'aspvbs'
let commentSymbol_L == "'"
let commentSymbol_R = ''
elseif fileType == 'plsql' || fileType == 'lua'
let commentSymbol_L = '--'
let commentSymbol_R = ''
else
execute 'echo "ToggleCommentify has not (yet) been implemented for this file-type"'
let commentSymbol_L = ''
let commentSymbol_R = ''
" echo "ToggleCommentify: Unknown filetype, defaulting to CPP style //"
" let commentSymbol_L = '//'
" let commentSymbol_R = ''
endif
" this function is ment to be executed as a way of returning two vars; see
" :h return
return "let CommentSymbol_L = '" . commentSymbol_L . "' | let CommentSymbol_R = '" . commentSymbol_R ."'"
endfunction " }}}
function s:DoCommentify(DaMode, DaOldCol, ...) " {{{
"[Feral:300/02@07:24] To work with the range param, just add in the LR
"sniplet, the LR.openfold sniplet and then add LR as the range to the
"below subistutes. (I don't want to test these changes now!) TODO!
"i.e. {{{
"//" let LR = a:firstline.",".a:lastline
"//"" echo confirm(LR)
"//"
"//" " expand folds (required), else :s will operate on the entire fold count
"//" " times, with count being the number of lines in the fold.
"//" execute ":silent! ".LR."foldopen!"
"//"
"//" silent! execute LR.':s/^\(FTE:.\{-}\)\s\+FTE:.*$/\1/'
"//" "
"//" }}}
if(a:0 == 0)
execute s:FindCommentify()
elseif a:0 == 2
let CommentSymbol_L = a:1
let CommentSymbol_R = a:2
else
let CommentSymbol_L = a:1
let CommentSymbol_R = ""
endif
" [Feral:201/02@01:46] GATE: nothing to do if we have no comment symbol.
"[Feral:308/02@02:04] CommentSymbol_R is allowed to be blank so we only
" check CommentSymbol_L
if strlen(CommentSymbol_L) == 0
return
endif
" echo confirm(a:DaMode."\n".a:DaOldCol."\n".CommentSymbol_L)
" Save where we are
let SavedMark = line('.').'G'.a:DaOldCol.'|'
normal! H
let SavedMark = 'normal! '.line('.').'Gzt'.SavedMark
execute SavedMark
" " [Feral:201/02@01:15] I want to comment empty lines so this is remed out.
" let IsBlankLineString = getline(".")
" if IsBlankLineString != $
" " don't comment empty lines
" return
" endif
" [Feral:201/02@03:43] folded lines must be opend because a substitute
" operation on a fold effects all lines of the fold. When called from a
" range the result is that the lines of the fold have the substitute
" command executed on them as many times as there is folded lines.
" So, as a HACK if there is a fold, open it.
if(foldclosed(line(".")) != -1)
:foldopen
endif
let lineString = getline(".")
" FERAL: extract the first x chars of the line, where x is the width/length of the comment symbol.
let isCommented = strpart(lineString,0,strlen(CommentSymbol_L) )
" 0 = toggle
" 1 = comment
" 2 = uncomment.
" if a:DaMode == 1
let ModeOfOperation = a:DaMode
if ModeOfOperation == 0
if isCommented == CommentSymbol_L
" already commented, so uncomment.
let ModeOfOperation = 2
else
" not already commented, so comment.
let ModeOfOperation = 1
endif
endif
let CommentSymbol_L = escape(CommentSymbol_L, '/\\')
let CommentSymbol_R = escape(CommentSymbol_R, '/\\')
set nohlsearch
if ModeOfOperation == 2
" Uncomment -- remove the comment markers.
silent execute ':s/^'.CommentSymbol_L.'//'
if strlen(CommentSymbol_R)
silent execute ':s/'.CommentSymbol_R.'$//'
endif
else
" else ModeOfOperation == 1
" Comment -- add the comment markers.
silent execute ':s/^/'.CommentSymbol_L.'/'
if strlen(CommentSymbol_R)
silent execute ':s/$/'.CommentSymbol_R.'/'
endif
endif
set hlsearch
" Return to where we were
execute SavedMark
unlet SavedMark
endfunction
" }}}
" Old Method:
" Holding: {{{
"function! <SID>ToggleCommentify(...) " {{{
"
" if(a:0 == 0)
" let CommentSymbol = <SID>FindCommentify()
" else
" let CommentSymbol = a:1
" endif
"
" " [Feral:201/02@01:46] GATE: nothing to do if we have no comment symbol.
" if CommentSymbol == ''
" return
" endif
"
"" " [Feral:201/02@01:15] I want to comment empty lines so this is remed out.
"" let IsBlankLineString = getline(".")
"" if IsBlankLineString != $
"" " don't comment empty lines
"" return
"" endif
"
" " [Feral:201/02@03:43] folded lines must be opend because a substitute
" " operation on a fold effects all lines of the fold. When called from a
" " range the result is that the lines of the fold have the substitute
" " command executed on them as many times as there is folded lines.
" " So, as a HACK if there is a fold, open it.
" if(foldclosed(line(".")) != -1)
" :foldopen
" endif
"
" let lineString = getline(".")
" " FERAL: extract the first x chars of the line, where x is the width/length of the comment symbol.
" let isCommented = strpart(lineString,0,strlen(CommentSymbol) )
" if isCommented == CommentSymbol
" " if the line is already commented, uncomment
" let CommentSymbol = escape(CommentSymbol, '/\\')
" set nohlsearch
" silent execute ':s/'.CommentSymbol.'//'
" set hlsearch
" else
" " if the line is uncommented, comment
" let CommentSymbol = escape(CommentSymbol, '/\\')
" set nohlsearch
" silent execute ':s/^/'.CommentSymbol.'/'
" set hlsearch
" endif
"
" " Feral: I would prefer the cursor position be saved, however a range
" " command always starts with the cursor on col 1.
" execute "normal 0"
"endfunction " }}}
"function! <SID>Commentify(...) " {{{
"
" if(a:0 == 0)
" let CommentSymbol = <SID>FindCommentify()
" else
" let CommentSymbol = a:1
" endif
"
" " [Feral:201/02@01:46] GATE: nothing to do if we have no comment symbol.
" if CommentSymbol == ''
" return
" endif
"
"" " [Feral:201/02@01:15] I want to comment empty lines so this is remed out.
"" let IsBlankLineString = getline(".")
"" if IsBlankLineString != $
"" " don't comment empty lines
"" return
"" endif
"
" " [Feral:201/02@03:43] folded lines must be opend because a substitute
" " operation on a fold effects all lines of the fold. When called from a
" " range the result is that the lines of the fold have the substitute
" " command executed on them as many times as there is folded lines.
" " So, as a HACK if there is a fold, open it.
" if(foldclosed(line(".")) != -1)
" :foldopen
" endif
"
" let CommentSymbol = escape(CommentSymbol, '/\\')
" set nohlsearch
" " go to the beginning of the line and insert the comment symbol
" execute ':s/^/'.CommentSymbol.'/'
" set hlsearch
"" execute 'normal 0i'.CommentSymbol
"
" " Feral: I would prefer the cursor position be saved, however a range
" " command always starts with the cursor on col 1.
" execute "normal 0"
"endfunction " }}}
"function! <SID>UnCommentify(...) " {{{
"
" if(a:0 == 0)
" let CommentSymbol = <SID>FindCommentify()
" else
" let CommentSymbol = a:1
" endif
"
" " [Feral:201/02@01:46] GATE: nothing to do if we have no comment symbol.
" if CommentSymbol == ''
" return
" endif
"
"" " [Feral:201/02@01:15] I want to comment empty lines so this is remed out.
"" let IsBlankLineString = getline(".")
"" if IsBlankLineString != $
"" " don't comment empty lines
"" return
"" endif
"
" " [Feral:201/02@03:43] folded lines must be opend because a substitute
" " operation on a fold effects all lines of the fold. When called from a
" " range the result is that the lines of the fold have the substitute
" " command executed on them as many times as there is folded lines.
" " So, as a HACK if there is a fold, open it.
" if(foldclosed(line(".")) != -1)
" :foldopen
" endif
"
" let lineString = getline(".")
" " FERAL: extract the first x chars of the line, where x is the width/length of the comment symbol.
" let isCommented = strpart(lineString,0,strlen(CommentSymbol) )
" " [Feral:201/02@02:24] Only uncomment if the comment char is there (produces a string not found error otherwise).
" if isCommented == CommentSymbol
" let CommentSymbol = escape(CommentSymbol, '/\\')
" set nohlsearch
" " remove the first comment symbol found on a line
" execute ':s/'.CommentSymbol.'//'
" set hlsearch
" endif
"
" " Feral: I would prefer the cursor position be saved, however a range
" " command always starts with the cursor on col 1.
" execute "normal 0"
"endfunction " }}}
" }}}
"*****************************************************************
" Commands: {{{
"*****************************************************************
"Holding: {{{
":command -nargs=? -range TC :<line1>,<line2>call <SID>ToggleCommentify(<f-args>)
":command -nargs=? -range CC :<line1>,<line2>call <SID>Commentify(<f-args>)
":command -nargs=? -range UC :<line1>,<line2>call <SID>UnCommentify(<f-args>)
"}}}
if !exists(":TC")
:command -nargs=? -range TC :let b:FTCSaveCol = virtcol('.')|<line1>,<line2>call <SID>DoCommentify(0, b:FTCSaveCol, <f-args>)|:unlet b:FTCSaveCol
endif
if !exists(":CC")
:command -nargs=? -range CC :let b:FTCSaveCol = virtcol('.')|<line1>,<line2>call <SID>DoCommentify(1, b:FTCSaveCol, <f-args>)|:unlet b:FTCSaveCol
endif
if !exists(":UC")
:command -nargs=? -range UC :let b:FTCSaveCol = virtcol('.')|<line1>,<line2>call <SID>DoCommentify(2, b:FTCSaveCol, <f-args>)|:unlet b:FTCSaveCol
endif
if !hasmapto('<Plug>FtcTc')
nmap <unique> <M-c> <Plug>FbmTc
imap <unique> <M-c> <esc><Plug>FbmTc
vmap <unique> <M-c> <Plug>FbmTc
endif
noremap <unique> <script> <Plug>FbmTc :TC<CR>j
" Holding Work: {{{
"nnoremap <M-c> :TC<CR>j
"inoremap <M-c> <ESC>:TC<CR>j
"vnoremap <M-c> :TC<CR>j
" keep the place of the cursor by using mark z
"nnoremap <M-c> mz:TC<CR>`zj
"inoremap <M-c> <ESC>mz:TC<CR>`zj
"vnoremap <M-c> mz:TC<CR>`zj
"map <M-c>x :TC<CR>j
"map <M-c>c :CC<CR>j
"map <M-c>v :UC<CR>j
"imap <M-c>x <ESC>:TC<CR>j
"imap <M-c>c <ESC>:CC<CR>j
"imap <M-c>v <ESC>:UC<CR>j
" }}}
"
" }}}
"End of file