<?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">' Matt McCutchen's SuperbChemistry for OpenOffice, version 2
+<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Main" script:language="StarBasic">' Matt McCutchen's SuperbChemistry for OpenOffice, version 2.1
'
' Applies superscript and subscript formatting to chemical formulas in text.
'
' for further processing. If the replacement text could use
' backreferences, this would be easier.
-' Tag candidate quantity/charges following symbols or ).
-SuperbReplace(ThisComponent, "([A-Z][a-z]?|[\])}])[0-9]*[-+−]", "&@g@", 0)
-
-' Disqualify + and - in compound words, etc.
-SuperbReplace(ThisComponent, "@g@[[({A-Za-z0-9<>]", "@G@&", 0)
-SuperbReplace(ThisComponent, "@G@@g@", "", 0)
+' Tag candidate charges following symbols or ), but not in compound words, etc.
+' Acceptable next character. (Has to be before end of line to avoid matching @g@ tag itself.)
+SuperbReplace(ThisComponent, "([A-Z][a-z]?|[\])}])[0-9]*[-+−][^[({A-Za-z0-9<>]", "&@G@", 0)
+' Retag in front.
+SuperbReplace(ThisComponent, ".@G@", "@g@&", 0)
+' End of line.
+SuperbReplace(ThisComponent, "([A-Z][a-z]?|[\])}])[0-9]*[-+−]$", "&@g@", 0)
' O and )]} grab a single digit as quantity.
SuperbReplace(ThisComponent, "[\])}O][0-9]", "&@n@", 0)