]> code.delx.au - dotfiles/blob - .vim/commenter.vim
CMakeLists.txt syntax
[dotfiles] / .vim / commenter.vim
1 " Commenting of lines! Stolen & modified from vim.org's ToggleCommentify
2 map <C-c> :call ToggleCommentify()<CR>j
3 imap <C-c> <ESC>:call ToggleCommentify()<CR>j
4 " The nice thing about these mapping is that you don't have to select a visual
5 " block to comment ... just keep the CTRL-key pressed down and tap on 'c' as
6 " often as you need.
7
8 function! ToggleCommentify()
9 let lineString = getline(".")
10 if strlen(lineString) == 0
11 " don't comment empty lines
12 return
13 endif
14
15 let isCommented = strpart(lineString,0,3)
16 let commentSymbol = ''
17
18 let commentMapping = {
19 \'###': [
20 \'conf',
21 \'debsources',
22 \'exports',
23 \'fstab',
24 \'make',
25 \'mplayerconf',
26 \'muttrc',
27 \'perl',
28 \'procmail',
29 \'python',
30 \'readline',
31 \'ruby',
32 \'screen',
33 \'sh',
34 \'sshconfig',
35 \'sudoers',
36 \'terminfo',
37 \'vrml',
38 \'xf86conf',
39 \],
40 \
41 \'///': [
42 \'c',
43 \'cpp',
44 \'java',
45 \'javascript',
46 \'objc',
47 \'ox',
48 \'php',
49 \],
50 \
51 \'"""': [
52 \'vim',
53 \],
54 \
55 \'!!!': [
56 \'xdefaults',
57 \],
58 \
59 \'%%%': [
60 \'matlab',
61 \'tex',
62 \],
63 \
64 \'---': [
65 \'sql',
66 \]
67 \}
68
69 for commentChar in keys(commentMapping)
70 for name in commentMapping[commentChar]
71 if &filetype == name
72 let commentSymbol = commentChar
73 endif
74 endfor
75 endfor
76
77 if commentSymbol == ''
78 execute 'echo "ToggleCommentify has not (yet) been implemented for the file-type " . &filetype'
79 else
80 if isCommented == commentSymbol
81 " if the line is already commented, uncomment
82 call UnCommentify(commentSymbol)
83 else
84 " if the line is uncommented, comment
85 call Commentify(commentSymbol)
86 endif
87 endif
88 endfunction
89
90 function! Commentify(commentSymbol)
91 execute ':s+^+'.a:commentSymbol.'+'
92 nohlsearch
93 endfunction
94
95 function! UnCommentify(commentSymbol)
96 execute ':s+'.a:commentSymbol.'++'
97 nohlsearch
98 endfunction
99