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

Re: Finding unmatched )

Expand Messages
  • Benji Fisher
    Just to elaborate a bit, regular expressions cannot deal with nesting; regular here comes from the idea you may have learned in a computer science class long
    Message 1 of 12 , Aug 2, 2002
    • 0 Attachment
      Just to elaborate a bit, regular expressions cannot deal with nesting;
      "regular" here comes from the idea you may have learned in a computer science
      class long ago. When I first started using vi and tex (for my undergraduate
      thesis) I got in the habit of adding the lines

      {[(
      )]}

      at the top and bottom of my file, then banging on the % key.

      HTH --Benji Fisher

      Peter Jay Salzman wrote:
      >
      > hi dave, do you know about "%"?
      >
      > type "%" while the cursor is over a parenthesis and vim will find the
      > matching one. if the cursor doesn't move, it has no matching
      > parenthesis.
      >
      > also works with [] and {}.
      >
      > it doesn't answer your question, but if you're just looking for a way to
      > find missing ()'s, it's very helpful.
      >
      > pete
      >
      > begin David Fishburn <fishburn@...>
      > >
      > > Looking for a little bit of help coming up with a regular expression
      > > that will find an unmatched closing paranthesis.
      > >
      > > So I want to find the following lines:
      > > )
      > > In )
      > > Some (int) )
      > > Again(int (char)) ) begin
      > >
      > > In all these cases I need to know if there is an additional ) than (.
    • David Fishburn
      Let me clarify what I am trying to do a bit more. I am building an indent file for vim. So I really need to *count* the unmatched parentheses so I know to
      Message 2 of 12 , Aug 2, 2002
      • 0 Attachment
        Let me clarify what I am trying to do a bit more.

        I am building an indent file for vim.

        So I really need to *count* the unmatched parentheses so I know to
        perform an indent or not.

        If (count unmatched ")" on this line ) > 0
        let indent = indent - &shiftwidth
        endif

        So for lines like this:
        Create table dave (
        c1 int,
        c2 char(30)
        )

        But also for something like this:
        Create table dave (
        c1 int,
        c2 char(30) )

        The unmatched ) on the previous line should move the indent back once.

        So in my indent file for vim, I need to know if there exists an
        unmatched ), I don't need to actually find it.

        I will look into the other suggestions.

        Thanks,
        Dave


        -----Original Message-----
        From: Benji Fisher [mailto:benji@...]
        Sent: Friday, August 02, 2002 8:35 AM
        To: Peter Jay Salzman
        Cc: David Fishburn; vim@...
        Subject: Re: Finding unmatched )


        Just to elaborate a bit, regular expressions cannot deal with
        nesting; "regular" here comes from the idea you may have learned in a
        computer science class long ago. When I first started using vi and tex
        (for my undergraduate
        thesis) I got in the habit of adding the lines

        {[(
        )]}

        at the top and bottom of my file, then banging on the % key.

        HTH --Benji Fisher

        Peter Jay Salzman wrote:
        >
        > hi dave, do you know about "%"?
        >
        > type "%" while the cursor is over a parenthesis and vim will find the
        > matching one. if the cursor doesn't move, it has no matching
        > parenthesis.
        >
        > also works with [] and {}.
        >
        > it doesn't answer your question, but if you're just looking for a way
        > to find missing ()'s, it's very helpful.
        >
        > pete
        >
        > begin David Fishburn <fishburn@...>
        > >
        > > Looking for a little bit of help coming up with a regular expression

        > > that will find an unmatched closing paranthesis.
        > >
        > > So I want to find the following lines:
        > > )
        > > In )
        > > Some (int) )
        > > Again(int (char)) ) begin
        > >
        > > In all these cases I need to know if there is an additional ) than
        > > (.
      • Colin Keith
        ... Look at the indent/perl.vim file in the distribution, that does what you want. ... 1 Create table ( 2 --- ---c1 int, 3 --- ---c2 char(30), 4 ) 5 6 create
        Message 3 of 12 , Aug 2, 2002
        • 0 Attachment
          On Fri, Aug 02, 2002 at 09:10:49AM -0400, David Fishburn wrote:
          > So I really need to *count* the unmatched parentheses so I know to
          > perform an indent or not.

          Look at the indent/perl.vim file in the distribution, that does what you
          want.

          :indent filetype on
          :set fo=tcql
          :set ft=perl

          1 Create table (
          2 >--->---c1 int,
          3 >--->---c2 char(30),
          4 )
          5
          6 create table (
          7 >--->---c1 int,
          8 >--->---c2 char(30), )
          9

          > So in my indent file for vim, I need to know if there exists an
          > unmatched ), I don't need to actually find it.

          Urm indents don't work like that. You'd just get everything after your
          unmatched bracket indented one level furhter. I.e.

          11 create table (
          12 >--->---c1 int,
          13 >--->---c2 char((30),
          14 >--->---)


          It doesn't however pick up when it goes <0, so this wouldn't show as a
          problem because the second ) on line 17 lowers the indent level to 0. The
          ) on line 18 should decrement the indent level to -1, but doesn't.

          15 create table (
          16 >--->---c1 int,
          17 >--->---c2 char(30)),
          18 )

          So if you carry on typing this isn't picked up:

          19
          20 create table (
          21 >--->---c1 int,
          22 >--->---c2 char(30)
          23 )

          > I will look into the other suggestions.

          I've played with indents a few times, so far without much success so I
          hope it goes well :)


          --
          Then graphics games came along and the computer using portion of the human race
          forgot all about 500,000 years of language evolution and went straight back to
          the electronic equivalent of banging rocks together - the point'n'click game
          - http://www.douglasadams.com/creations/infocomjava.html
        • Matthew Winn
          ... One way is to search along the line character by character counting parentheses. Alternatively take a copy of the line and try the ... All you ll be left
          Message 4 of 12 , Aug 2, 2002
          • 0 Attachment
            On Fri, Aug 02, 2002 at 09:10:49AM -0400, David Fishburn wrote:
            > Let me clarify what I am trying to do a bit more.
            >
            > I am building an indent file for vim.
            >
            > So I really need to *count* the unmatched parentheses so I know to
            > perform an indent or not.
            >
            > If (count unmatched ")" on this line ) > 0
            > let indent = indent - &shiftwidth
            > endif
            >
            > So for lines like this:
            > Create table dave (
            > c1 int,
            > c2 char(30)
            > )
            >
            > But also for something like this:
            > Create table dave (
            > c1 int,
            > c2 char(30) )
            >
            > The unmatched ) on the previous line should move the indent back once.
            >
            > So in my indent file for vim, I need to know if there exists an
            > unmatched ), I don't need to actually find it.

            One way is to search along the line character by character counting
            parentheses. Alternatively take a copy of the line and try the
            following substitutions in order:

            :s/[^()]\+//g
            :s/()\|)(//g (repeat until no match)

            All you'll be left with is the unbalanced parentheses.

            --
            Matthew Winn (matthew@...)
          • Benji Fisher
            ... Then you can test :if rp lp ... (definition of rp is left as an exercise). If you want to be fancier, you can add a step to strip comments from l. HTH
            Message 5 of 12 , Aug 2, 2002
            • 0 Attachment
              David Fishburn wrote:
              >
              > Let me clarify what I am trying to do a bit more.
              >
              > I am building an indent file for vim.
              >
              > So I really need to *count* the unmatched parentheses so I know to
              > perform an indent or not.
              >
              > If (count unmatched ")" on this line ) > 0
              > let indent = indent - &shiftwidth
              > endif
              >
              > So for lines like this:
              > Create table dave (
              > c1 int,
              > c2 char(30)
              > )
              >
              > But also for something like this:
              > Create table dave (
              > c1 int,
              > c2 char(30) )
              >
              > The unmatched ) on the previous line should move the indent back once.
              >
              > So in my indent file for vim, I need to know if there exists an
              > unmatched ), I don't need to actually find it.
              >
              > I will look into the other suggestions.

              Try this:

              :let l = getline(".")
              :echo l
              :let lp = substitute(l, '[^(]', '', 'g')
              :echo lp strlen(lp)

              Then you can test :if rp > lp ... (definition of rp is left as an exercise). If
              you want to be fancier, you can add a step to strip comments from l.

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