]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/math.C
Georg's latest improvements
[lyx.git] / src / tex2lyx / math.C
1 /**
2  * \file math.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16
17 #include <iostream>
18
19 using std::cerr;
20 using std::endl;
21
22 using std::ostream;
23 using std::string;
24
25
26 bool is_math_env(string const & name)
27 {
28         static char const * known_math_envs[] = { "equation", "equation*",
29         "eqnarray", "eqnarray*", "align", "align*", "gather", "gather*",
30         "multline", "multline*", "math", "displaymath", "flalign", "flalign*",
31         // These require extra args
32         "alignat", "alignat*", "xalignat", "xalignat*", "xxalignat",
33         0};
34
35         for (char const ** what = known_math_envs; *what; ++what)
36                 if (*what == name)
37                         return true;
38         return false;
39 }
40
41
42 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
43 {
44         while (p.good()) {
45                 Token const & t = p.get_token();
46
47 #ifdef FILEDEBUG
48                 cerr << "t: " << t << " flags: " << flags << "\n";
49 #endif
50
51                 if (flags & FLAG_ITEM) {
52                         if (t.cat() == catSpace)
53                                 continue;
54
55                         flags &= ~FLAG_ITEM;
56                         if (t.cat() == catBegin) {
57                                 // skip the brace and collect everything to the next matching
58                                 // closing brace
59                                 flags |= FLAG_BRACE_LAST;
60                                 continue;
61                         }
62
63                         // handle only this single token, leave the loop if done
64                         flags |= FLAG_LEAVE;
65                 }
66
67
68                 //
69                 // cat codes
70                 //
71                 if (t.cat() == catMath) {
72                         if (mode == MATHTEXT_MODE) {
73                                 // we are inside some text mode thingy, so opening new math is allowed
74                                 Token const & n = p.get_token();
75                                 if (n.cat() == catMath) {
76                                         // TeX's $$...$$ syntax for displayed math
77                                         os << "\\[";
78                                         parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
79                                         os << "\\]";
80                                         p.get_token(); // skip the second '$' token
81                                 } else {
82                                         // simple $...$  stuff
83                                         p.putback();
84                                         os << '$';
85                                         parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
86                                         os << '$';
87                                 }
88                         }
89
90                         else if (flags & FLAG_SIMPLE) {
91                                 // this is the end of the formula
92                                 return;
93                         }
94
95                         else {
96                                 cerr << "\nmode: " << mode << endl;
97                                 p.error("something strange in the parser\n");
98                                 break;
99                         }
100                 }
101
102                 else if (t.cat() == catLetter ||
103                                t.cat() == catSuper ||
104                                t.cat() == catSub ||
105                                t.cat() == catOther ||
106                                t.cat() == catAlign ||
107                                t.cat() == catActive ||
108                                t.cat() == catParameter)
109                         os << t.character();
110
111                 else if (t.cat() == catBegin) {
112                         os << '{';
113                         parse_math(p, os, FLAG_BRACE_LAST, mode);
114                         os << '}';
115                 }
116
117                 else if (t.cat() == catEnd) {
118                         if (flags & FLAG_BRACE_LAST)
119                                 return;
120                         os << "unexpected '}' in math\n";
121                 }
122
123                 else if (t.cat() == catComment) {
124                         if (t.cs().size())
125                                 cerr << "Ignoring comment: " << t.asInput();
126                         else
127                                 // "%\n" combination
128                                 p.skip_spaces();
129                 }
130
131                 //
132                 // control sequences
133                 //
134
135                 else if (t.cs() == "(") {
136                         os << "\\(";
137                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
138                         os << "\\)";
139                 }
140
141                 else if (t.cs() == "[") {
142                         // special handling of a few common SW user quirks
143                         p.skip_spaces();
144                         //if (p.next_token().cs() ==
145                         os << "\\[";
146                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
147                         os << "\\]";
148                 }
149
150                 else if (t.cs() == "protect")
151                         // ignore \\protect, will hopefully be re-added during output
152                         ;
153
154                 else if (t.cs() == "begin") {
155                         string const name = p.getArg('{', '}');
156                         active_environments.push_back(name);
157                         os << "\\begin{" << name << "}";
158                         if (name == "tabular")
159                                 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
160                         else
161                                 parse_math(p, os, FLAG_END, mode);
162                         os << "\\end{" << name << "}";
163                         active_environments.pop_back();
164                 }
165
166                 else if (t.cs() == "end") {
167                         if (flags & FLAG_END) {
168                                 // eat environment name
169                                 string const name = p.getArg('{', '}');
170                                 if (name != active_environment())
171                                         p.error("\\end{" + name + "} does not match \\begin{"
172                                                 + active_environment() + "}");
173                                 return;
174                         }
175                         p.error("found 'end' unexpectedly");
176                 }
177
178                 else if (t.cs() == ")") {
179                         if (flags & FLAG_SIMPLE2)
180                                 return;
181                         p.error("found '\\)' unexpectedly");
182                 }
183
184                 else if (t.cs() == "]") {
185                         if (flags & FLAG_EQUATION)
186                                 return;
187                         p.error("found '\\]' unexpectedly");
188                 }
189
190                 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
191                                 || t.cs() == "texttt" || t.cs() == "textsc"
192                                 || t.cs() == "text" || t.cs() == "intertext") {
193                         os << '\\' << t.cs() << '{';
194                         parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
195                         os << '}';
196                 }
197
198                 else if (t.cs() == "mbox" || t.cs() == "fbox") {
199                         os << "\\" << t.cs() << '{';
200                         parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
201                         os << '}';
202                 }
203
204                 else if (t.cs() == "\"") {
205                         string const name = p.verbatim_item();
206                              if (name == "a") os << 'ä';
207                         else if (name == "o") os << 'ö';
208                         else if (name == "u") os << 'ü';
209                         else if (name == "A") os << 'Ä';
210                         else if (name == "O") os << 'Ö';
211                         else if (name == "U") os << 'Ü';
212                         else os << "\"{" << name << "}";
213                 }
214
215                 else if (t.cs() == "ss")
216                         os << "ß";
217
218                 else
219                         os << t.asInput();
220
221                 if (flags & FLAG_LEAVE) {
222                         flags &= ~FLAG_LEAVE;
223                         break;
224                 }
225         }
226 }
227
228
229
230
231 // }])