Import SuperbChemistry version 2
[superbchemistry/superbchemistry.git] / extension / SuperbChemistry / Main.xba
CommitLineData
071359bb
MM
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
95510495 3<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
071359bb
MM
4&apos;
5&apos; Applies superscript and subscript formatting to chemical formulas in text.
6&apos;
95510495
MM
7&apos; Rules:
8&apos; - Quantities [0-9]+ and charges [0-9]*[-+−] are recognized after an element
9&apos; symbol [A-Z][a-z]? or a closing delimiter [])}] . Hyphens are converted
10&apos; into real minus signs.
11&apos; - A charge sign [-+−] is ignored if it is followed by a letter, digit,
12&apos; opening delimiter, or [&lt;&gt;] . (Charges should appear only at the end of a
13&apos; formula, and we want to avoid matching ordinary hyphens in text.)
14&apos; - When digits followed by a charge sign are recognized, the last digit
15&apos; becomes part of the charge and the remaining digits become the quantity.
16&apos; (Charges rarely have absolute value more than 9.)
17&apos; - Exception: If a single digit follows O or a closing delimiter, that digit
18&apos; is always the quantity. (Handle NO3- and Fe(OH)2+. I think oxygen is the
19&apos; only element that frequently has a quantity as part of a +/-1 ion. A group
20&apos; is rarely parenthesized unless it has a quantity.)
21&apos;
071359bb
MM
22&apos; Examples:
23&apos; C12345 ==&gt; C_{12345}
24&apos; H+ ==&gt; H^+
25&apos; Cl- ==&gt; Cl^-
26&apos; Fe3+ ==&gt; Fe^{3+}
95510495 27&apos; SO42- ==&gt; SO_4^{2-}
071359bb
MM
28&apos; C1232+ ==&gt; C_{123}^{2+}
29&apos; N2- ==&gt; N^{2-}
95510495
MM
30&apos; NO3- ==&gt; NO_3^-
31&apos; Fe(OH)2- ==&gt; Fe(OH)_2^-
32&apos; O12 ==&gt; O_{12}
33&apos; y4- ==&gt; y4-
34&apos; x2 ==&gt; x2
35&apos; Foo2 ==&gt; Foo2
36&apos; TI-89 ==&gt; TI-89
071359bb
MM
37
38&apos; Regular expression replace in the document,
39&apos; creating superscripts if superb &gt; 0 or subscripts if superb &lt; 0.
40&apos; Used by SuperbChemistry.
41sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
42
43dim rd as object
44rd = doc.createReplaceDescriptor()
45
95510495 46rd.SearchCaseSensitive = true
071359bb
MM
47rd.SearchRegularExpression = true
48rd.setSearchString(searchStr)
49rd.setReplaceString(replaceStr)
50
51if superb &lt;&gt; 0 then
52 dim replaceAttrs(1) as new com.sun.star.beans.PropertyValue
53 replaceAttrs(0).Name = &quot;CharEscapement&quot;
54 if superb &gt; 0 then
55 replaceAttrs(0).Value = 33
56 else
57 replaceAttrs(0).Value = -9
58 end if
59 replaceAttrs(1).Name = &quot;CharEscapementHeight&quot;
60 replaceAttrs(1).Value = 58
61 rd.setReplaceAttributes(replaceAttrs)
62end if
63
64doc.replaceAll(rd)
65
66end sub
67
68&apos; Formats the current document
69sub FormatDocument
70
95510495
MM
71&apos; Idiom: Match something and tag it on the left or right with @x@
72&apos; for further processing. If the replacement text could use
73&apos; backreferences, this would be easier.
74
75&apos; Tag candidate quantity/charges following symbols or ).
76SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]*[-+−]&quot;, &quot;&amp;@g@&quot;, 0)
77
78&apos; Disqualify + and - in compound words, etc.
79SuperbReplace(ThisComponent, &quot;@g@[[({A-Za-z0-9&lt;&gt;]&quot;, &quot;@G@&amp;&quot;, 0)
80SuperbReplace(ThisComponent, &quot;@G@@g@&quot;, &quot;&quot;, 0)
071359bb 81
95510495
MM
82&apos; O and )]} grab a single digit as quantity.
83SuperbReplace(ThisComponent, &quot;[\])}O][0-9]&quot;, &quot;&amp;@n@&quot;, 0)
071359bb 84
95510495
MM
85&apos; Real minus signs in charges.
86SuperbReplace(ThisComponent, &quot;-@g@&quot;, &quot;−@g@&quot;, 0)
071359bb 87
95510495
MM
88&apos; Make charges: at most one digit.
89SuperbReplace(ThisComponent, &quot;[0-9]?[−+]@g@&quot;, &quot;@q@&amp;&quot;, 1)
071359bb 90
95510495 91&apos; Remove the O and ) markers in case of O57.
071359bb
MM
92SuperbReplace(ThisComponent, &quot;@n@&quot;, &quot;&quot;, 0)
93
95510495
MM
94&apos; Tag quantities: as many digits as we can still grab.
95SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]+&quot;, &quot;&amp;@n@&quot;, 0)
071359bb 96
95510495 97&apos; Make quantities.
071359bb
MM
98SuperbReplace(ThisComponent, &quot;[0-9]+@n@&quot;, &quot;&amp;&quot;, -1)
99
100&apos; Clean up all markers.
95510495 101SuperbReplace(ThisComponent, &quot;@[gGnq]@&quot;, &quot;&quot;, 0)
071359bb
MM
102
103end sub
104
105</script:module>