' Matt McCutchen's SuperbChemistry for OpenOffice, version 1 ' ' Applies superscript and subscript formatting to chemical formulas in text. ' ' Examples: ' C12345 ==> C_{12345} ' H+ ==> H^+ ' Cl- ==> Cl^- ' Fe3+ ==> Fe^{3+} ' C1232+ ==> C_{123}^{2+} ' N2- ==> N^{2-} ' Exception for O and ): NO3- ==> NO_3^-, Fe(OH)2- ==> Fe(OH)_2^- ' But still O12 ==> O_{12} ' 4+ ==> 4+ (not a superscript by itself) ' Regular expression replace in the document, ' creating superscripts if superb > 0 or subscripts if superb < 0. ' Used by SuperbChemistry. sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer) dim rd as object rd = doc.createReplaceDescriptor() rd.SearchRegularExpression = true rd.setSearchString(searchStr) rd.setReplaceString(replaceStr) if superb <> 0 then dim replaceAttrs(1) as new com.sun.star.beans.PropertyValue replaceAttrs(0).Name = "CharEscapement" if superb > 0 then replaceAttrs(0).Value = 33 else replaceAttrs(0).Value = -9 end if replaceAttrs(1).Name = "CharEscapementHeight" replaceAttrs(1).Value = 58 rd.setReplaceAttributes(replaceAttrs) end if doc.replaceAll(rd) end sub ' Formats the current document sub FormatDocument ' Mark candidate superscripts so we know they follow letters or ). SuperbReplace(ThisComponent, "[A-Za-z)][0-9]*[-+−]", "&@l@", 0) ' O and ) grab a single digit. Block it off from becoming a superscript. SuperbReplace(ThisComponent, "[O)][0-9]", "&@n@", 0) ' Real minus signs in superscripts. SuperbReplace(ThisComponent, "-@l@", "−@l@", 0) ' Make superscripts: at most one digit. SuperbReplace(ThisComponent, "[0-9]?[−+]@l@", "@q@&", 1) ' Remove the O and ) markers. SuperbReplace(ThisComponent, "@n@", "", 0) ' Mark off subscripts: as many digits as we can still grab. SuperbReplace(ThisComponent, "[A-Za-z)][0-9]+", "&@n@", 0) ' Make subscripts. SuperbReplace(ThisComponent, "[0-9]+@n@", "&", -1) ' Clean up all markers. SuperbReplace(ThisComponent, "@[lnq]@", "", 0) end sub