Draft fix and test case for formulas like Mg3(PO4)2.
authorMatt McCutchen <matt@mattmccutchen.net>
Sat, 15 Oct 2022 18:25:06 +0000 (14:25 -0400)
committerMatt McCutchen <matt@mattmccutchen.net>
Sat, 15 Oct 2022 18:25:06 +0000 (14:25 -0400)
Also improve some comments to hopefully make it easier for future
developers to understand what the code is supposed to be doing.

There's no update to the "Formatting rules" in the readme because this
brings the code into better alignment with what that section already
said.

SuperbChemistry-test.odt
extension/SuperbChemistry/Main.xba

index 33f2e1c..60323fe 100644 (file)
Binary files a/SuperbChemistry-test.odt and b/SuperbChemistry-test.odt differ
index 960dc6c..1c21b7c 100644 (file)
@@ -85,7 +85,8 @@ Sub FormatSelectionOrDocumentDebug()
        &apos; (https://bugs.documentfoundation.org/show_bug.cgi?id=136577), so we must
        &apos; avoid it.  Fortunately, avoiding it is pretty straightforward.
 
-       &apos; Insert @m@ between an item and a number or charge.
+       &apos; Step 1: Insert @m@ between an item and a number or charge that may be part of
+       &apos; a chemical formula (subject to later checks).
        If HaveSelection(ThisComponent) Then
                &apos; doc.replaceAll is not capable of searching a selection, while the
                &apos; dispatch-based replace API uses the current format options in the
@@ -116,33 +117,41 @@ Sub FormatSelectionOrDocumentDebug()
                SuperbReplace(ThisComponent, &quot;(?&lt;=[A-Z][a-z]?|[\])}])[-+−0-9]+&quot;, &quot;@m@&amp;&quot;, 0)
        End If
 
-       &apos; Insert @c@ after a charge.
+       &apos; Step 2: Insert @c@ after a charge symbol, if it&apos;s followed by one of the
+       &apos; allowed characters for the second kind of &quot;recognized sequence&quot; described in
+       &apos; the readme.
        SuperbReplace(ThisComponent, &quot;(?&lt;=@m@)([0-9]*[-+−])(?=[ \t\])}.,:;?!&apos;&quot;&quot;]|$)&quot;, &quot;&amp;@c@&quot;, 0)
 
-       &apos; Real minus signs in charges.
+       &apos; Step 3: Real minus signs in charges.
        SuperbReplace(ThisComponent, &quot;-@c@&quot;, &quot;−@c@&quot;, 0)
 
-       &apos; Some groups grab a single following digit as a quantity rather than a charge amount.
-       &apos; Insert @sq@ marker to prevent the charge from grabbing the digit.
+       &apos; Step 4: Some groups grab a single following digit as a quantity rather than a
+       &apos; charge amount.  Insert @sq@ marker to prevent the charge from grabbing the
+       &apos; digit.
        SuperbReplace(ThisComponent, &quot;(?&lt;=(H|O|F|Cl|Br|I|[\])}])@m@)[0-9]&quot;, &quot;&amp;@sq@&quot;, 0)
 
-       &apos; Each charge grabs at most one digit and moves the @c@ in front to prevent the
-       &apos; quantity from grabbing the digit.
+       &apos; Step 5: Each charge grabs at most one digit and moves the @c@ in front to
+       &apos; prevent the quantity from grabbing the digit.
        SuperbReplace(ThisComponent, &quot;([0-9]?[−+])@c@&quot;, &quot;@c@$1&quot;, 1)
 
-       &apos; Remove any @sq@ markers so items can grab all the digits that follow for the quantity.
+       &apos; Step 6: Remove any @sq@ markers so items can grab all the digits that follow
+       &apos; for the quantity.
        SuperbReplace(ThisComponent, &quot;(.)@sq@&quot;, &quot;$1&quot;, 0)
 
        &apos; At this point, we have only @m@ and @c@ markers left.
 
-       &apos; Format quantities: as many digits as we can still grab.
-       &apos; We have to allow @ as a following character for our own @c@ tag.
-       SuperbReplace(ThisComponent, &quot;(?&lt;=@m@)[0-9]+(?=[@A-Z \t\])}.,:;?!&apos;&quot;&quot;]|$)&quot;, &quot;&amp;&quot;, -1)
+       &apos; Step 7: Format quantities: as many digits as we can still grab.  The digits
+       &apos; must be followed by one of the allowed characters for the first kind of
+       &apos; &quot;recognized sequence&quot; described by a readme or by @, which we assume is part
+       &apos; of a @c@ tag we added in step 2.  The allowed characters A-Z\[({ represent
+       &apos; the beginning of another item; the other allowed characters are the same as
+       &apos; in step 2.
+       SuperbReplace(ThisComponent, &quot;(?&lt;=@m@)[0-9]+(?=[@A-Z\[({ \t\])}.,:;?!&apos;&quot;&quot;]|$)&quot;, &quot;&amp;&quot;, -1)
 
-       &apos; Clean up @c@ markers.  We know there is a charge sign after each.
+       &apos; Step 8: Clean up @c@ markers.  We know there is a charge sign after each.
        SuperbReplace(ThisComponent, &quot;@c@(.)&quot;, &quot;$1&quot;, 0)
 
-       &apos; Clean up @m@ markers.  We know there is some character before each.
+       &apos; Step 9: Clean up @m@ markers.  We know there is some character before each.
        SuperbReplace(ThisComponent, &quot;(.)@m@&quot;, &quot;$1&quot;, 0)
 
 End Sub