Find next unbalanced parentheses

Everything related to our flagship word processor.
Post Reply
User avatar
useeger
Posts: 89
Joined: 2004-06-28 00:03:01
Location: Germany

Find next unbalanced parentheses

Post by useeger »

Does there exist a Power Find Pro expression that finds the next unbalanced parentheses in a paragraph or "soft" paragraph (…) […] {…} ?

thanks for help and
greetings from Karlsruhe (Germany)
Ulrich
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find next unbalanced parentheses

Post by phspaelti »

It's very difficult to solve this problem with a Find/Replace expression, and I'm pretty sure it would not be possible to do it for several types of parentheses at the same time. This could be done by macro though.

It works like this.
Find all parenthesis, at the same time, and save the result in an array.
Then go through the array in order.
If you find an opening parenthesis, put it on a stack.
If you find a closing parenthesis, and it matches the last opening one on the stack, throw the pair away.
If the above two tests fail, you have found an unbalanced pair.

The above procedure is 'search forward', but you can search in the opposite direction, if you prefer.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find next unbalanced parentheses

Post by phspaelti »

Here is now a macro that will insert a parenthesis to balance the nearest unbalanced opening parenthesis. It will also select the matching parentheses and their contents.
If you repeatedly use it will add matching parentheses until everything is closed and balanced.
The macro will stop with a warning, if an unreconcilable mismatch is found.

Note: this macro only looks to the left of the insertion point. If you use it inside a balanced parenthesis it will leave an unbalanced closing parenthesis to the right of the cursor.

[Update: macro modified to throw errors in certain limit cases.]
Attachments
Balance Parentheses (insert).nwm
(5.46 KiB) Downloaded 322 times
Last edited by phspaelti on 2015-07-06 15:51:58, edited 1 time in total.
philip
User avatar
useeger
Posts: 89
Joined: 2004-06-28 00:03:01
Location: Germany

Re: Find next unbalanced parentheses

Post by useeger »

Thank you very much, Philip, for your macro. I am not familiar with the Nisus macro language and could not have made it.

It produces an error in line 22. I try to translate the error from the German: the attribute "location" requires an object, but instead of that "undefined" was found by "$parens.lastValue".

By the way: I would prefer to correct the unbalanced parentheses manually, the macro should only find the next occurence and do nothing. The very large document I am working on has about 24.000 {…} , 20.000 (…), but only 120 […]. Often (…) is inside {…}.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find next unbalanced parentheses

Post by phspaelti »

useeger wrote:It produces an error in line 22. I try to translate the error from the German: the attribute "location" requires an object, but instead of that "undefined" was found by "$parens.lastValue".
I'm guessing this is because you tried to run the macro with the insertion point at the beginning of the file. The macro as written would be pointless in that case. Since you seem to want a different way of working, I'll post a different version when I can find some time later.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Find next unbalanced parentheses

Post by phspaelti »

Okay, so here is a modified form that checks the whole text for balanced parentheses. If it finds an error it selects the error and reports the problem. There are three potential problems: extra opening, extra closing, and mismatched pair.

Note 1: One feature that I have kept from the previous version, is that it always checks the text object which is currently selected. So it will work inside a table cell, a header or a footer, too. This means however that if you run it with the selection in the header, it will not check the whole document. So make sure the insertion point is in the main text, if you want to check the text body. It will work in Footnotes/Endnotes only up to a point. If there is an opening parenthesis in one note and a closing parenthesis in a following note, the two will balance, and it won't report a problem.

Note 2: useeger's original request, was to find the "first" unbalanced. parenthesis. But this macro performs a depth first search, which means it finds the "deepest" mismatched problem first. If the very first parenthesis in the document is an unmatched opening parenthesis, it will only find it when everything else balances, i.e, last. That's just the way it is. There's always a chance that the matching parenthesis might be at the very end of the document.
Attachments
Balance Parentheses.nwm
(4.92 KiB) Downloaded 314 times
philip
User avatar
useeger
Posts: 89
Joined: 2004-06-28 00:03:01
Location: Germany

Re: Find next unbalanced parentheses

Post by useeger »

Perfect! It does exactly what I wanted.

And I found that it is really better to do the correction manually, because there are for example occurences {…) where the macro can not decide what is correct (…) or {…}.

Thank you very much!
Post Reply