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*", "math", "displaymath", "flalign", "flalign*",
31 // These require extra args
32 "alignat", "alignat*", "xalignat", "xalignat*", "xxalignat",
35 for (char const ** what = known_math_envs; *what; ++what)
42 void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
45 Token const & t = p.get_token();
48 cerr << "t: " << t << " flags: " << flags << "\n";
51 if (flags & FLAG_ITEM) {
52 if (t.cat() == catSpace)
56 if (t.cat() == catBegin) {
57 // skip the brace and collect everything to the next matching
59 flags |= FLAG_BRACE_LAST;
63 // handle only this single token, leave the loop if done
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
78 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
80 p.get_token(); // skip the second '$' token
85 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
90 else if (flags & FLAG_SIMPLE) {
91 // this is the end of the formula
96 cerr << "\nmode: " << mode << endl;
97 p.error("something strange in the parser\n");
102 else if (t.cat() == catLetter ||
103 t.cat() == catSpace ||
104 t.cat() == catSuper ||
106 t.cat() == catOther ||
107 t.cat() == catAlign ||
108 t.cat() == catActive ||
109 t.cat() == catParameter)
112 else if (t.cat() == catNewline) {
113 //if (p.next_token().cat() == catNewline) {
117 os << "\n "; // note the space
121 else if (t.cat() == catBegin) {
123 parse_math(p, os, FLAG_BRACE_LAST, mode);
127 else if (t.cat() == catEnd) {
128 if (flags & FLAG_BRACE_LAST)
130 os << "unexpected '}' in math\n";
133 else if (t.cat() == catComment)
140 else if (t.cs() == "(") {
142 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
146 else if (t.cs() == "[") {
147 // special handling of a few common SW user quirks
149 //if (p.next_token().cs() ==
151 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
155 else if (t.cs() == "protect")
156 // ignore \\protect, will hopefully be re-added during output
159 else if (t.cs() == "begin") {
160 string const name = p.getArg('{', '}');
161 active_environments.push_back(name);
162 os << "\\begin{" << name << "}";
163 if (name == "tabular")
164 parse_math(p, os, FLAG_END, MATHTEXT_MODE);
166 parse_math(p, os, FLAG_END, mode);
167 os << "\\end{" << name << "}";
168 active_environments.pop_back();
171 else if (t.cs() == "end") {
172 if (flags & FLAG_END) {
173 // eat environment name
174 string const name = p.getArg('{', '}');
175 if (name != active_environment())
176 p.error("\\end{" + name + "} does not match \\begin{"
177 + active_environment() + "}");
180 p.error("found 'end' unexpectedly");
183 else if (t.cs() == ")") {
184 if (flags & FLAG_SIMPLE2)
186 p.error("found '\\)' unexpectedly");
189 else if (t.cs() == "]") {
190 if (flags & FLAG_EQUATION)
192 p.error("found '\\]' unexpectedly");
195 else if (t.cs() == "textrm" || t.cs() == "textsf" || t.cs() == "textbf"
196 || t.cs() == "texttt" || t.cs() == "textsc"
197 || t.cs() == "text" || t.cs() == "intertext") {
198 os << '\\' << t.cs() << '{';
199 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
203 else if (t.cs() == "mbox" || t.cs() == "fbox") {
204 os << "\\" << t.cs() << '{';
205 parse_math(p, os, FLAG_ITEM, MATHTEXT_MODE);
209 else if (t.cs() == "\"") {
210 string const name = p.verbatim_item();
211 if (name == "a") os << 'ä';
212 else if (name == "o") os << 'ö';
213 else if (name == "u") os << 'ü';
214 else if (name == "A") os << 'Ä';
215 else if (name == "O") os << 'Ö';
216 else if (name == "U") os << 'Ü';
217 else os << "\"{" << name << "}";
220 else if (t.cs() == "ss")
226 if (flags & FLAG_LEAVE) {
227 flags &= ~FLAG_LEAVE;