Permalink
Browse files

Create a new gromacs filetype and associate a gromacs plugin to it.

The gromacs plugin allows to comment/uncomment selections and to jump to
the next/previous section.

Shortcuts are :
]; -- Comment the selection
]: -- Uncomment the selection
], -- Jump to the next section
]! -- Jump to the previous section

To have the gromacs plugin activated only for gromacs files you need to
allow filetype-plugin so you need to have "filetype plugin on" in your
.vimrc.

To have the gromacs plugin always activated move gromacs.vim from
.vim/ftplugin to .vim/plugin.
  • Loading branch information...
1 parent 0ea508c commit c4f5e9d02805a41277004dd107ea0d988b376f69 @jbarnoud jbarnoud committed Feb 23, 2012
Showing with 79 additions and 8 deletions.
  1. +8 −8 ftdetect/gromacs.vim
  2. +71 −0 ftplugin/gromacs.vim
View
16 ftdetect/gromacs.vim
@@ -1,14 +1,14 @@
" Molecular Dynamics Parameter file (mdp)
-au BufNewFile,BufRead *.mdp setf mdp
-au BufNewFile,BufRead *.MDP setf mdp
+au BufNewFile,BufRead *.mdp setf mdp.gromacs
+au BufNewFile,BufRead *.MDP setf mdp.gromacs
" Index file (ndx)
-au BufNewFile,BufRead *.ndx setf ndx
-au BufNewFile,BufRead *.NDX setf ndx
+au BufNewFile,BufRead *.ndx setf ndx.gromacs
+au BufNewFile,BufRead *.NDX setf ndx.gromacs
"Topology file (top, itp)
-au BufNewFile,BufRead *.top setf top
-au BufNewFile,BufRead *.top setf top
+au BufNewFile,BufRead *.top setf top.gromacs
+au BufNewFile,BufRead *.TOP setf top.gromacs
-au BufNewFile,BufRead *.itp setf top
-au BufNewFile,BufRead *.itp setf top
+au BufNewFile,BufRead *.itp setf top.gromacs
+au BufNewFile,BufRead *.ITP setf top.gromacs
View
71 ftplugin/gromacs.vim
@@ -0,0 +1,71 @@
+"
+" This program is free software: you can redistribute it and/or modify
+" it under the terms of the GNU General Public License as published by
+" the Free Software Foundation, either version 3 of the License, or
+" (at your option) any later version.
+"
+" This program is distributed in the hope that it will be useful,
+" but WITHOUT ANY WARRANTY; without even the implied warranty of
+" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+" GNU General Public License for more details.
+"
+" A copy of the GNU General Public License is available at
+" <http://www.gnu.org/licenses/>.
+
+
+" " Maintainer: Jonathan Barnoud <jonathan@barnoud.net>
+" " Last Change: 23 Feb 2012
+
+" Shortcuts:
+" ]; -- Comment the selected block
+" ]: -- Uncomment the selected block
+" ], -- Jump to the previous section
+" ]! -- Jump to the next section
+
+
+if exists("b:loaded_gmx_plugin")
+ finish
+endif
+let b:loaded_gmx_plugin = 1
+
+map ]; :call GromacsCommentSelection()<CR>
+vmap ]; :call GromacsCommentSelection()<CR>
+map ]: :call GromacsUncommentSelection()<CR>
+vmap ]: :call GromacsUncommentSelection()<CR>
+map ], :call GromacsMoveSection(1)<CR>
+map ]! :call GromacsMoveSection(-1)<CR>
+
+" Comment out selected lines
+" Add the comment character as first column
+function GromacsCommentSelection() range
+ let commentString = ";"
+ let cl = a:firstline
+ while (cl <= a:lastline)
+ let ul = substitute(getline(cl), "^\\(.*\\)$", commentString."\\1", "")
+ call setline(cl, ul)
+ let cl = cl + 1
+ endwhile
+endfunction
+
+" Uncomment selected lines
+" Remove the first comment character
+function GromacsUncommentSelection() range
+ let commentString = ";"
+ let cl = a:firstline
+ while (cl <= a:lastline)
+ let ul = substitute(getline(cl),
+ \"\\([ \\t]*\\\)".commentString."\\(.*\\)$", "\\1\\2", "")
+ call setline(cl, ul)
+ let cl = cl + 1
+ endwhile
+endfunction
+
+" Move to the next (1) or previous (-1) section
+function GromacsMoveSection(direction)
+ let regexp = "\\[.*\\]"
+ let flag = "W"
+ if (a:direction == -1)
+ let flag = flag."b"
+ endif
+ let res = search(regexp, flag)
+endfunction

0 comments on commit c4f5e9d

Please sign in to comment.