]> code.delx.au - dotfiles/blob - .vim/commenter.vim
xmonad: default to tabbed layout
[dotfiles] / .vim / commenter.vim
1 " Commenting of lines! Stolen & modified from vim.org's ToggleCommentify
2 map <C-_> :call ToggleCommentify()<CR>j
3 imap <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 \'groovy',
50 \],
51 \
52 \'"""': [
53 \'vim',
54 \],
55 \
56 \'!!!': [
57 \'xdefaults',
58 \],
59 \
60 \'%%%': [
61 \'matlab',
62 \'tex',
63 \],
64 \
65 \'---': [
66 \'sql',
67 \'haskell',
68 \]
69 \}
70
71 for commentChar in keys(commentMapping)
72 for name in commentMapping[commentChar]
73 if &filetype == name
74 let commentSymbol = commentChar
75 endif
76 endfor
77 endfor
78
79 if commentSymbol == ''
80 execute 'echo "ToggleCommentify has not (yet) been implemented for the file-type " . &filetype'
81 else
82 if isCommented == commentSymbol
83 " if the line is already commented, uncomment
84 call UnCommentify(commentSymbol)
85 else
86 " if the line is uncommented, comment
87 call Commentify(commentSymbol)
88 endif
89 endif
90 endfunction
91
92 function! Commentify(commentSymbol)
93 execute ':s+^+'.a:commentSymbol.'+'
94 nohlsearch
95 endfunction
96
97 function! UnCommentify(commentSymbol)
98 execute ':s+'.a:commentSymbol.'++'
99 nohlsearch
100 endfunction
101