Loading ...
Sorry, an error occurred while loading the content.

Re: matchit.vim with c

Expand Messages
  • Benji Fisher
    ... Off the top of my head, and with no testing, something like this should work (assuming Vim 6.0) ...
    Message 1 of 2 , Sep 29, 2001
    • 0 Attachment
      David Menuhin wrote:
      >
      > Has anyone successfully managed to get matchit.vim to cycle through the
      > 'if/else if/else if/else' or the cases of a switch statement in c? I've
      > been mucking around with it for a while but can't seem to get the regular
      > expressions right :(

      Off the top of my head, and with no testing, something like this should
      work (assuming Vim 6.0)

      :let b:match_words =
      \ '\%(else\s\+\)\@<!\<if\>:\<else\s\+if\>:\<else\>\%(\s\+if\)\@!,'
      \ . '\<switch\>:\<case\>:\<default\>'

      The switch statement should work pretty well, provided that your coding style
      always requires the default case, which is optional. The if statement will
      only work if your coding style requires an else, which is probably more
      annoying than it is worth. Otherwise, matchit will get confused by

      if(foo){
      let bar = 1;
      if(foobar)
      let barbar = 2;
      }
      else /* matchit thinks you are closing the inner if, and
      never finds a close to the outer if! */
      let bar = 2;

      Technical details:

      1. I used '\%(...\)' instead of '\(...\)' because it does not count towards
      the limit of 9 groups in a regexp.

      2. All the \@<! and \@! is needed so that the script can distinguish between
      the "if" that starts an if block and the second part of the "end if" that
      continues it. (There must be a better way to say that...)

      Reminder: matchit.vim (and the docs, matchit.txt) is in $VIMRUNTIME/macros/
      in the Vim 6.0 distro.

      HTH --Benji Fisher
    Your message has been successfully submitted and would be delivered to recipients shortly.