Import SuperbChemistry version 2.2
authorMatt McCutchen <matt@mattmccutchen.net>
Tue, 8 Sep 2020 00:16:14 +0000 (20:16 -0400)
committerMatt McCutchen <matt@mattmccutchen.net>
Tue, 8 Sep 2020 00:16:14 +0000 (20:16 -0400)
Modification time: 2008-10-26 23:54:49 -0400

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

index 3802718..9740bbc 100644 (file)
Binary files a/SuperbChemistry-test.odt and b/SuperbChemistry-test.odt differ
index 3b0640e..530cf8d 100644 (file)
@@ -1,12 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
-<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Main" script:language="StarBasic">&apos; Matt McCutchen&apos;s SuperbChemistry for OpenOffice, version 2.1
+<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Main" script:language="StarBasic">&apos; SuperbChemistry version 2.2
+&apos; http://mattmccutchen.net/schem/
+&apos; Written and maintained by Matt McCutchen &lt;matt@mattmccutchen.net&gt;
 &apos;
 &apos;
-&apos; Applies superscript and subscript formatting to chemical formulas in text.
+&apos; Applies superscript and subscript formatting to chemical formulas in
+&apos; OpenOffice.org Writer documents.
 &apos;
 &apos; Rules:
 &apos; - Quantities [0-9]+ and charges [0-9]*[-+−] are recognized after an element
 &apos;
 &apos; Rules:
 &apos; - Quantities [0-9]+ and charges [0-9]*[-+−] are recognized after an element
-&apos;   symbol [A-Z][a-z]? or a closing delimiter [])}] .  Hyphens are converted
+&apos;   symbol [A-Z][a-z]? or a closing delimiter [\])}] .  Hyphens are converted
 &apos;   into real minus signs.
 &apos; - A charge sign [-+−] is ignored if it is followed by a letter, digit,
 &apos;   opening delimiter, or [&lt;&gt;] .  (Charges should appear only at the end of a
 &apos;   into real minus signs.
 &apos; - A charge sign [-+−] is ignored if it is followed by a letter, digit,
 &apos;   opening delimiter, or [&lt;&gt;] .  (Charges should appear only at the end of a
 &apos; - When digits followed by a charge sign are recognized, the last digit
 &apos;   becomes part of the charge and the remaining digits become the quantity.
 &apos;   (Charges rarely have absolute value more than 9.)
 &apos; - When digits followed by a charge sign are recognized, the last digit
 &apos;   becomes part of the charge and the remaining digits become the quantity.
 &apos;   (Charges rarely have absolute value more than 9.)
-&apos; - Exception: If a single digit follows O or a closing delimiter, that digit
-&apos;   is always the quantity.  (Handle NO3- and Fe(OH)2+.  I think oxygen is the
-&apos;   only element that frequently has a quantity as part of a +/-1 ion.  A group
-&apos;   is rarely parenthesized unless it has a quantity.)
+&apos; - In cases like X2-, we have to guess whether the digit is an atom/group
+&apos;   quantity or a charge amount.  We guess atom/group quantity if X is H (NH4+),
+&apos;   O (NO3-), a halogen (SbF6-, AlCl4-, etc.), or a closing parenthesis
+&apos;   (Fe(OH)2+; the group likely would not have been parenthesized unless it had
+&apos;   a quantity).  Otherwise we guess charge amount (Fe3+).  This heuristic
+&apos;   should be right most of the time.
 &apos;
 &apos; Examples:
 &apos; C12345 ==&gt; C_{12345}
 &apos;
 &apos; Examples:
 &apos; C12345 ==&gt; C_{12345}
 &apos; Fe3+ ==&gt; Fe^{3+}
 &apos; SO42- ==&gt; SO_4^{2-}
 &apos; C1232+ ==&gt; C_{123}^{2+}
 &apos; Fe3+ ==&gt; Fe^{3+}
 &apos; SO42- ==&gt; SO_4^{2-}
 &apos; C1232+ ==&gt; C_{123}^{2+}
-&apos; N2- ==&gt; N^{2-}
+&apos; N3- ==&gt; N^{3-}
+&apos; N|_3^- not recognized (| represents &quot;no-width no break&quot;)
+&apos; NH4+ ==&gt; NH_4^+
 &apos; NO3- ==&gt; NO_3^-
 &apos; NO3- ==&gt; NO_3^-
-&apos; Fe(OH)2- ==&gt; Fe(OH)_2^-
+&apos; AlCl4- =&gt; AlCl_4^-
+&apos; Fe(OH)2+ ==&gt; Fe(OH)_2^+
 &apos; O12 ==&gt; O_{12}
 &apos; O12 ==&gt; O_{12}
-&apos; y4- ==&gt; y4-
-&apos; x2 ==&gt; x2
-&apos; Foo2 ==&gt; Foo2
-&apos; TI-89 ==&gt; TI-89
+&apos; y4- not recognized
+&apos; x2 not recognized
+&apos; Foo2 not recognized
+&apos; TI-89 not recognized
+&apos;
+&apos; To format the current document, run the FormatDocument macro: go to Tools -&gt;
+&apos; Macros -&gt;  Run Macro... -&gt; My Macros -&gt; SuperbChemistry -&gt; Main -&gt;
+&apos; FormatDocument -&gt; Run.  I realize that this is ugly.  I tried to make the
+&apos; package install a menu item to format the document, but the resulting package
+&apos; caused OpenOffice.org to crash regularly (I didn&apos;t investigate why), so I
+&apos; abandoned that idea.  Note that you can add a menu item as a user
+&apos; customization (Tools -&gt; Customize), and I recommend it if you plan to use
+&apos; SuperbChemistry frequently.
+&apos;
+&apos; FormatDocument uses a sequence of regular expression find-and-replace
+&apos; operations since that was easy to implement and makes the rules easy to
+&apos; change.  The operations appear in the undo history, so you can undo a
+&apos; formatting run by undoing the block of &quot;Replace&quot; entries at the top of the
+&apos; history.
+&apos;
+&apos; I would like to support formatting a selection, but the OpenOffice.org API
+&apos; does not appear to support replace-all within a selection.  I could find
+&apos; within the selection and implement the replacing myself, but that is more
+&apos; work than I want to do.
+&apos;
+&apos; If SuperbChemistry makes a mistake (e.g., recognizes a &quot;formula&quot; that isn&apos;t
+&apos; or formats a formula incorrectly), you can correct the formatting yourself
+&apos; and prevent future runs of the macro from recognizing the offending text by
+&apos; inserting a &quot;No-width no break&quot; character in the middle of it.  This character
+&apos; is available in the &quot;Insert -&gt; Formatting Mark&quot; menu when &quot;Tools -&gt; Options -&gt;
+&apos; Language Settings -&gt; Languages -&gt; Enhanced language support -&gt;
+&apos; Enabled for complex text layout (CTL)&quot; is enabled.
+
+&apos; ==============================================================================
 
 &apos; Regular expression replace in the document,
 &apos; creating superscripts if superb &gt; 0 or subscripts if superb &lt; 0.
 
 &apos; Regular expression replace in the document,
 &apos; creating superscripts if superb &gt; 0 or subscripts if superb &lt; 0.
-&apos; Used by SuperbChemistry.
+&apos; Used by FormatDocument.
 sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
 
 dim rd as object
 sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
 
 dim rd as object
@@ -70,7 +108,9 @@ sub FormatDocument
 
 &apos; Idiom: Match something and tag it on the left or right with @x@
 &apos; for further processing.  If the replacement text could use
 
 &apos; Idiom: Match something and tag it on the left or right with @x@
 &apos; for further processing.  If the replacement text could use
-&apos; backreferences, this would be easier.
+&apos; backreferences, this would be easier.  (I think backreferences were added
+&apos; since I originally wrote this code, but I see no need to rewrite it to take
+&apos; advantage of them. - Matt 2008-10-26)
 
 &apos; Tag candidate charges following symbols or ), but not in compound words, etc.
 &apos; Acceptable next character.  (Has to be before end of line to avoid matching @g@ tag itself.)
 
 &apos; Tag candidate charges following symbols or ), but not in compound words, etc.
 &apos; Acceptable next character.  (Has to be before end of line to avoid matching @g@ tag itself.)
@@ -80,8 +120,9 @@ SuperbReplace(ThisComponent, &quot;.@G@&quot;, &quot;@g@&amp;&quot;, 0)
 &apos; End of line.
 SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]*[-+−]$&quot;, &quot;&amp;@g@&quot;, 0)
 
 &apos; End of line.
 SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]*[-+−]$&quot;, &quot;&amp;@g@&quot;, 0)
 
-&apos; O and )]} grab a single digit as quantity.
-SuperbReplace(ThisComponent, &quot;[\])}O][0-9]&quot;, &quot;&amp;@n@&quot;, 0)
+&apos; Some groups grab a single following digit as a quantity rather than a charge amount.
+&apos; See detailed rationale above.
+SuperbReplace(ThisComponent, &quot;(H|O|F|Cl|Br|I|\))[0-9]&quot;, &quot;&amp;@n@&quot;, 0)
 
 &apos; Real minus signs in charges.
 SuperbReplace(ThisComponent, &quot;-@g@&quot;, &quot;−@g@&quot;, 0)
 
 &apos; Real minus signs in charges.
 SuperbReplace(ThisComponent, &quot;-@g@&quot;, &quot;−@g@&quot;, 0)