3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
26 bool is_math_env(string const & name)
28 static char const * known_math_envs[] = { "equation", "equation*",
29 "eqnarray", "eqnarray*", "align", "align*", "gather", "gather*",
30 "multline", "multline*", 0};
32 for (char const ** what = known_math_envs; *what; ++what)
39 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
42 Token const & t = p.get_token();
45 cerr << "t: " << t << " flags: " << flags << "\n";
48 if (flags & FLAG_ITEM) {
49 if (t.cat() == catSpace)
53 if (t.cat() == catBegin) {
54 // skip the brace and collect everything to the next matching
56 flags |= FLAG_BRACE_LAST;
60 // handle only this single token, leave the loop if done
68 if (t.cat() == catMath) {
69 if (mode == MATHTEXT_MODE) {
70 // we are inside some text mode thingy, so opening new math is allowed
71 Token const & n = p.get_token();
72 if (n.cat() == catMath) {
73 // TeX's $$...$$ syntax for displayed math
75 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
77 p.get_token(); // skip the second '$' token
82 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
87 else if (flags & FLAG_SIMPLE) {
88 // this is the end of the formula
93 cerr << "\nmode: " << mode << endl;
94 p.error("something strange in the parser\n");
99 else if (t.cat() == catLetter ||
100 t.cat() == catSpace ||
101 t.cat() == catSuper ||
103 t.cat() == catOther ||
104 t.cat() == catAlign ||
105 t.cat() == catActive ||
106 t.cat() == catParameter)
109 else if (t.cat() == catNewline) {
110 //if (p.next_token().cat() == catNewline) {
114 os << "\n "; // note the space
118 else if (t.cat() == catBegin) {
120 parse_math(p, os, FLAG_BRACE_LAST, mode);
124 else if (t.cat() == catEnd) {
125 if (flags & FLAG_BRACE_LAST)
127 os << "unexpected '}' in math\n";
130 else if (t.cat() == catComment)
137 else if (t.cs() == "(") {
139 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
143 else if (t.cs() == "[") {
144 // special handling of a few common SW user quirks
146 //if (p.next_token().cs() ==
148 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
152 else if (t.cs() == "protect")
153 // ignore \\protect, will hopefully be re-added during output
156 else if (t.cs() == "begin") {
157 string const name = p.getArg('{', '}');
158 active_environments.push_back(name);
159 os << "\\begin{" << name << "}";
160 if (name == "tabular")
161 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
163 parse_math(p, os, FLAG_END, mode);
164 os << "\\end{" << name << "}";
167 else if (t.cs() == "end") {
168 if (flags & FLAG_END) {
169 // eat environment name
170 string const name = p.getArg('{', '}');
171 if (name != active_environment())
172 p.error("\\end{" + name + "} does not match \\begin{"
173 + active_environment() + "}");
174 active_environments.pop_back();
177 p.error("found 'end' unexpectedly");
180 else if (t.cs() == ")") {
181 if (flags & FLAG_SIMPLE2)
183 p.error("found '\\)' unexpectedly");
186 else if (t.cs() == "]") {
187 if (flags & FLAG_EQUATION)
189 p.error("found '\\]' unexpectedly");
192 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
193 || t.cs() == "texttt" || t.cs() == "textsc") {
194 os << '\\' << t.cs() << '{';
195 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
199 else if (t.cs() == "mbox") {
201 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
205 else if (t.cs() == "\"") {
206 string const name = p.verbatim_item();
207 if (name == "a") os << 'ä';
208 else if (name == "o") os << 'ö';
209 else if (name == "u") os << 'ü';
210 else if (name == "A") os << 'Ä';
211 else if (name == "O") os << 'Ö';
212 else if (name == "U") os << 'Ü';
213 else os << "\"{" << name << "}";
216 else if (t.cs() == "ss")
222 if (flags & FLAG_LEAVE) {
223 flags &= ~FLAG_LEAVE;