33ccbd173bcd3f7620f47f36590157f156789cbf
[superbchemistry/superbchemistry.git] / extension / SuperbChemistry / Main.xba
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
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
4 &apos;
5 &apos; Applies superscript and subscript formatting to chemical formulas in text.
6 &apos;
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;
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+}
27 &apos; SO42- ==&gt; SO_4^{2-}
28 &apos; C1232+ ==&gt; C_{123}^{2+}
29 &apos; N2- ==&gt; N^{2-}
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
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.
41 sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
42
43 dim rd as object
44 rd = doc.createReplaceDescriptor()
45
46 rd.SearchCaseSensitive = true
47 rd.SearchRegularExpression = true
48 rd.setSearchString(searchStr)
49 rd.setReplaceString(replaceStr)
50
51 if 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)
62 end if
63
64 doc.replaceAll(rd)
65
66 end sub
67
68 &apos; Formats the current document
69 sub FormatDocument
70
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 ).
76 SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]*[-+−]&quot;, &quot;&amp;@g@&quot;, 0)
77
78 &apos; Disqualify + and - in compound words, etc.
79 SuperbReplace(ThisComponent, &quot;@g@[[({A-Za-z0-9&lt;&gt;]&quot;, &quot;@G@&amp;&quot;, 0)
80 SuperbReplace(ThisComponent, &quot;@G@@g@&quot;, &quot;&quot;, 0)
81
82 &apos; O and )]} grab a single digit as quantity.
83 SuperbReplace(ThisComponent, &quot;[\])}O][0-9]&quot;, &quot;&amp;@n@&quot;, 0)
84
85 &apos; Real minus signs in charges.
86 SuperbReplace(ThisComponent, &quot;-@g@&quot;, &quot;−@g@&quot;, 0)
87
88 &apos; Make charges: at most one digit.
89 SuperbReplace(ThisComponent, &quot;[0-9]?[−+]@g@&quot;, &quot;@q@&amp;&quot;, 1)
90
91 &apos; Remove the O and ) markers in case of O57.
92 SuperbReplace(ThisComponent, &quot;@n@&quot;, &quot;&quot;, 0)
93
94 &apos; Tag quantities: as many digits as we can still grab.
95 SuperbReplace(ThisComponent, &quot;([A-Z][a-z]?|[\])}])[0-9]+&quot;, &quot;&amp;@n@&quot;, 0)
96
97 &apos; Make quantities.
98 SuperbReplace(ThisComponent, &quot;[0-9]+@n@&quot;, &quot;&amp;&quot;, -1)
99
100 &apos; Clean up all markers.
101 SuperbReplace(ThisComponent, &quot;@[gGnq]@&quot;, &quot;&quot;, 0)
102
103 end sub
104
105 </script:module>