Import SuperbChemistry version 1
[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">
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 1
4&apos;
5&apos; Applies superscript and subscript formatting to chemical formulas in text.
6&apos;
7&apos; Examples:
8&apos; C12345 ==&gt; C_{12345}
9&apos; H+ ==&gt; H^+
10&apos; Cl- ==&gt; Cl^-
11&apos; Fe3+ ==&gt; Fe^{3+}
12&apos; C1232+ ==&gt; C_{123}^{2+}
13&apos; N2- ==&gt; N^{2-}
14&apos; Exception for O and ): NO3- ==&gt; NO_3^-, Fe(OH)2- ==&gt; Fe(OH)_2^-
15&apos; But still O12 ==&gt; O_{12}
16&apos; 4+ ==&gt; 4+ (not a superscript by itself)
17
18&apos; Regular expression replace in the document,
19&apos; creating superscripts if superb &gt; 0 or subscripts if superb &lt; 0.
20&apos; Used by SuperbChemistry.
21sub SuperbReplace(doc as object, searchStr as string, replaceStr as string, superb as integer)
22
23dim rd as object
24rd = doc.createReplaceDescriptor()
25
26rd.SearchRegularExpression = true
27rd.setSearchString(searchStr)
28rd.setReplaceString(replaceStr)
29
30if superb &lt;&gt; 0 then
31 dim replaceAttrs(1) as new com.sun.star.beans.PropertyValue
32 replaceAttrs(0).Name = &quot;CharEscapement&quot;
33 if superb &gt; 0 then
34 replaceAttrs(0).Value = 33
35 else
36 replaceAttrs(0).Value = -9
37 end if
38 replaceAttrs(1).Name = &quot;CharEscapementHeight&quot;
39 replaceAttrs(1).Value = 58
40 rd.setReplaceAttributes(replaceAttrs)
41end if
42
43doc.replaceAll(rd)
44
45end sub
46
47&apos; Formats the current document
48sub FormatDocument
49
50&apos; Mark candidate superscripts so we know they follow letters or ).
51SuperbReplace(ThisComponent, &quot;[A-Za-z)][0-9]*[-+−]&quot;, &quot;&amp;@l@&quot;, 0)
52
53&apos; O and ) grab a single digit. Block it off from becoming a superscript.
54SuperbReplace(ThisComponent, &quot;[O)][0-9]&quot;, &quot;&amp;@n@&quot;, 0)
55
56&apos; Real minus signs in superscripts.
57SuperbReplace(ThisComponent, &quot;-@l@&quot;, &quot;−@l@&quot;, 0)
58
59&apos; Make superscripts: at most one digit.
60SuperbReplace(ThisComponent, &quot;[0-9]?[−+]@l@&quot;, &quot;@q@&amp;&quot;, 1)
61
62&apos; Remove the O and ) markers.
63SuperbReplace(ThisComponent, &quot;@n@&quot;, &quot;&quot;, 0)
64
65&apos; Mark off subscripts: as many digits as we can still grab.
66SuperbReplace(ThisComponent, &quot;[A-Za-z)][0-9]+&quot;, &quot;&amp;@n@&quot;, 0)
67
68&apos; Make subscripts.
69SuperbReplace(ThisComponent, &quot;[0-9]+@n@&quot;, &quot;&amp;&quot;, -1)
70
71&apos; Clean up all markers.
72SuperbReplace(ThisComponent, &quot;@[lnq]@&quot;, &quot;&quot;, 0)
73
74end sub
75
76</script:module>