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