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

Re: Indenting blocks

Expand Messages
  • Antony Scriven
    ... This is neither thoroughly tested nor especially elegant, but might give you an idea of one way to proceed. ... That s supposed to be all one line. It
    Message 1 of 5 , Jun 2 1:22 PM
      Tim Chase wrote:

      > It's that ol' VB code again. I've got a whole bunch of "Select Case"
      > statements of the form
      >
      > Select Case iFoo
      > Case 1
      > {code}
      > Case 2
      > {more code}
      > Case Else
      > {yet more code}
      > End Select
      >
      > And I'd like to exdent the contents. In the generic form, it's an easy
      >
      > g/Select Case/+1,/End Select/-1<<<<
      >
      > (my shiftwidth is currently set to 1, so this exdents those 4 leading
      > spaces properly)
      >
      > However, the rub is that these "Select Case...End Select" statements can
      > be nested like
      >
      >
      > Select Case iFoo
      > Case 1
      > {code}
      > Case 2
      > Select Case iBar
      > Case 1
      > {stuff}
      > Case 2
      > {more stuff}
      > Case Else
      > {yet more stuff}
      > End Select ' <-- problematic/premature End Select found
      > Case Else
      > {yet more code}
      > End Select
      >
      > The problem is that with my first stab (the :g statement above), the
      > "inner block" is prematurely terminated. Is there some way to do
      > something like
      >
      > g/^\(\s*\)Select Case/+1,/^\1End Select"/-1<<<<
      >
      > which would make sure that the "Select Case" matched the indent of the
      > "End Select" over which it was effected? The problem is that the
      > \(...\) and the \1 are in disjoint regexps. :( Additionally, I'm not
      > sure that this doesn't change the marks (or the location of the "next
      > marked line on which to operate"), so it might totally jump over the
      > inner Select Case statement because of the positioning after the search
      > for the End Select.
      >
      > Any suggestions would be greatly appreciated. Thanks,

      This is neither thoroughly tested nor especially elegant,
      but might give you an idea of one way to proceed.

      :g/^\s*Select Case/let c=indent('.') + 1
      | exe '+ , /\%' . c . 'vEnd Select/- <<<<'

      That's supposed to be all one line. It seemed to work okay
      on your sample code. Of course a drawback is that your code
      needs to be indented properly in the first place.

      Perhaps a more thorough method would be to write a custom
      indent plugin. It should be quite straightforward to defer
      to the standard indent plugin but do something different for
      `Case' statements. Using the matchit plugin as suggested by
      Antoine also sounds like a reasonable idea.

      Hope this helps,

      Antony
    • gumnos (Tim Chase)
      ... Worked like a charm! Fortunately, all the code was properly indented to begin with, so it wasn t a problem. Thanks! -tim
      Message 2 of 5 , Jun 2 1:45 PM
        > This is neither thoroughly tested nor especially elegant,
        > but might give you an idea of one way to proceed.
        >
        > :g/^\s*Select Case/let c=indent('.') + 1
        > | exe '+ , /\%' . c . 'vEnd Select/- <<<<'
        >
        > That's supposed to be all one line. It seemed to work okay
        > on your sample code. Of course a drawback is that your code
        > needs to be indented properly in the first place.

        Worked like a charm! Fortunately, all the code was properly indented to
        begin with, so it wasn't a problem. Thanks!

        -tim
      • Benji Fisher
        ... Well, I would not say typical since it involves a :g command, but as long as you use :normal and not :normal!, it should work fine. I tried ... on the
        Message 3 of 5 , Jun 9 6:16 PM
          On Wed, Jun 02, 2004 at 08:29:27PM +0200, Antoine J. Mechelynck wrote:
          > gumnos (Tim Chase) <gumnos@...> wrote:
          > > It's that ol' VB code again. I've got a whole bunch of "Select Case"
          > > statements of the form
          > >
          > > Select Case iFoo
          > > Case 1
          > > {code}
          > > Case 2
          > > {more code}
          > > Case Else
          > > {yet more code}
          > > End Select
          > >
          > > And I'd like to exdent the contents. In the generic form, it's an
          > > easy
          > >
          > > g/Select Case/+1,/End Select/-1<<<<
          > >
          > > (my shiftwidth is currently set to 1, so this exdents those 4 leading
          > > spaces properly)
          > >
          > > However, the rub is that these "Select Case...End Select" statements
          > > can be nested like
          > >
          > >
          > > Select Case iFoo
          > > Case 1
          > > {code}
          > > Case 2
          > > Select Case iBar
          > > Case 1
          > > {stuff}
          > > Case 2
          > > {more stuff}
          > > Case Else
          > > {yet more stuff}
          > > End Select ' <-- problematic/premature End Select found
          > > Case Else
          > > {yet more code}
          > > End Select
          > >
          > > The problem is that with my first stab (the :g statement above), the
          > > "inner block" is prematurely terminated. Is there some way to do
          > > something like
          > >
          > > g/^\(\s*\)Select Case/+1,/^\1End Select"/-1<<<<
          > >
          > > which would make sure that the "Select Case" matched the indent of the
          > > "End Select" over which it was effected? The problem is that the
          > > \(...\) and the \1 are in disjoint regexps. :( Additionally, I'm not
          > > sure that this doesn't change the marks (or the location of the "next
          > > marked line on which to operate"), so it might totally jump over the
          > > inner Select Case statement because of the positioning after the
          > > search for the End Select.
          > >
          > > Any suggestions would be greatly appreciated. Thanks,
          > >
          > > -tim
          >
          > I'm not 100% sure but this sounds like a typical job for the matchit plugin.
          > Try reading its help, starting at ":help matchit.txt", and if that doesn't
          > work, see ":help matchit-install" about how to include it.

          Well, I would not say "typical" since it involves a :g command, but
          as long as you use :normal and not :normal!, it should work fine. I
          tried

          :g/Select Case/+normal v]%k2<

          on the sample text (after :set sw=1) and it worked like a charm. I
          mention this in part to advertise the ]% motion: there is more to
          matchit than just % ! Assuming you have installed the help file, see

          :help matchit-intro

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