]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/text.cpp
More paper sizes for tex2lyx
[lyx.git] / src / tex2lyx / text.cpp
1 /**
2  * \file tex2lyx/text.cpp
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  * \author Jean-Marc Lasgouttes
8  * \author Uwe Stöhr
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 // {[(
14
15 #include <config.h>
16
17 #include "tex2lyx.h"
18
19 #include "Context.h"
20 #include "Encoding.h"
21 #include "FloatList.h"
22 #include "Layout.h"
23 #include "Length.h"
24
25 #include "support/lassert.h"
26 #include "support/convert.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30
31 #include <algorithm>
32 #include <iostream>
33 #include <map>
34 #include <sstream>
35 #include <vector>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44                 Context const & context, InsetLayout const * layout)
45 {
46         bool const forcePlainLayout =
47                 layout ? layout->forcePlainLayout() : false;
48         Context newcontext(true, context.textclass);
49         if (forcePlainLayout)
50                 newcontext.layout = &context.textclass.plainLayout();
51         else
52                 newcontext.font = context.font;
53         parse_text(p, os, flags, outer, newcontext);
54         newcontext.check_end_layout(os);
55 }
56
57
58 namespace {
59
60 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
61                 Context const & context, string const & name)
62 {
63         InsetLayout const * layout = 0;
64         DocumentClass::InsetLayouts::const_iterator it =
65                 context.textclass.insetLayouts().find(from_ascii(name));
66         if (it != context.textclass.insetLayouts().end())
67                 layout = &(it->second);
68         parse_text_in_inset(p, os, flags, outer, context, layout);
69 }
70
71 /// parses a paragraph snippet, useful for example for \\emph{...}
72 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
73                 Context & context)
74 {
75         Context newcontext(context);
76         // Don't inherit the paragraph-level extra stuff
77         newcontext.par_extra_stuff.clear();
78         parse_text(p, os, flags, outer, newcontext);
79         // Make sure that we don't create invalid .lyx files
80         context.need_layout = newcontext.need_layout;
81         context.need_end_layout = newcontext.need_end_layout;
82 }
83
84
85 /*!
86  * Thin wrapper around parse_text_snippet() using a string.
87  *
88  * We completely ignore \c context.need_layout and \c context.need_end_layout,
89  * because our return value is not used directly (otherwise the stream version
90  * of parse_text_snippet() could be used). That means that the caller needs
91  * to do layout management manually.
92  * This is intended to parse text that does not create any layout changes.
93  */
94 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
95                   Context & context)
96 {
97         Context newcontext(context);
98         newcontext.need_layout = false;
99         newcontext.need_end_layout = false;
100         newcontext.new_layout_allowed = false;
101         // Avoid warning by Context::~Context()
102         newcontext.par_extra_stuff.clear();
103         ostringstream os;
104         parse_text_snippet(p, os, flags, outer, newcontext);
105         return os.str();
106 }
107
108
109 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
110  "vpageref", "prettyref", "eqref", 0 };
111
112 char const * const known_coded_ref_commands[] = { "ref", "pageref", "vref",
113  "vpageref", "formatted", "eqref", 0 };
114
115 /*!
116  * natbib commands.
117  * The starred forms are also known except for "citefullauthor",
118  * "citeyear" and "citeyearpar".
119  */
120 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
121 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
122 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
123
124 /*!
125  * jurabib commands.
126  * No starred form other than "cite*" known.
127  */
128 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
129 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
130 // jurabib commands not (yet) supported by LyX:
131 // "fullcite",
132 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
133 // "footciteauthor", "footciteyear", "footciteyearpar",
134 "citefield", "citetitle", 0 };
135
136 /// LaTeX names for quotes
137 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
138 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
139 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
140 "guilsinglleft", "frq", "guilsinglright", 0};
141
142 /// the same as known_quotes with .lyx names
143 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
144 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
145 "gld", "grd", "gls", "ers", "fls",
146 "fls", "frs", "frs", 0};
147
148 /// LaTeX names for font sizes
149 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
150 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
151
152 /// the same as known_sizes with .lyx names
153 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
154 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
155
156 /// LaTeX 2.09 names for font families
157 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
158
159 /// LaTeX names for font families
160 char const * const known_font_families[] = { "rmfamily", "sffamily",
161 "ttfamily", 0};
162
163 /// the same as known_old_font_families and known_font_families with .lyx names
164 char const * const known_coded_font_families[] = { "roman", "sans",
165 "typewriter", 0};
166
167 /// LaTeX 2.09 names for font series
168 char const * const known_old_font_series[] = { "bf", 0};
169
170 /// LaTeX names for font series
171 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
172
173 /// the same as known_old_font_series and known_font_series with .lyx names
174 char const * const known_coded_font_series[] = { "bold", "medium", 0};
175
176 /// LaTeX 2.09 names for font shapes
177 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
178
179 /// LaTeX names for font shapes
180 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
181 "upshape", 0};
182
183 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
184 char const * const known_coded_font_shapes[] = { "italic", "slanted",
185 "smallcaps", "up", 0};
186
187 /*!
188  * Graphics file extensions known by the dvips driver of the graphics package.
189  * These extensions are used to complete the filename of an included
190  * graphics file if it does not contain an extension.
191  * The order must be the same that latex uses to find a file, because we
192  * will use the first extension that matches.
193  * This is only an approximation for the common cases. If we would want to
194  * do it right in all cases, we would need to know which graphics driver is
195  * used and know the extensions of every driver of the graphics package.
196  */
197 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
198 "ps.gz", "eps.Z", "ps.Z", 0};
199
200 /*!
201  * Graphics file extensions known by the pdftex driver of the graphics package.
202  * \sa known_dvips_graphics_formats
203  */
204 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
205 "mps", "tif", 0};
206
207 /*!
208  * Known file extensions for TeX files as used by \\include.
209  */
210 char const * const known_tex_extensions[] = {"tex", 0};
211
212 /// spaces known by InsetSpace
213 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
214 "qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
215 "leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
216
217 /// the same as known_spaces with .lyx names
218 char const * const known_coded_spaces[] = { "space{}", "space{}",
219 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
220 "negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
221 "rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
222
223 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
224 /// them in ERT. "LaTeXe" must come before "LaTeX"!
225 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
226 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
227 int const known_phrase_lengths[] = {3, 5, 7, 0};
228
229
230 /// splits "x=z, y=b" into a map and an ordered keyword vector
231 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
232 {
233         vector<string> v;
234         split(s, v);
235         res.clear();
236         keys.resize(v.size());
237         for (size_t i = 0; i < v.size(); ++i) {
238                 size_t const pos   = v[i].find('=');
239                 string const index = trimSpaceAndEol(v[i].substr(0, pos));
240                 string const value = trimSpaceAndEol(v[i].substr(pos + 1, string::npos));
241                 res[index] = value;
242                 keys[i] = index;
243         }
244 }
245
246
247 /*!
248  * Split a LaTeX length into value and unit.
249  * The latter can be a real unit like "pt", or a latex length variable
250  * like "\textwidth". The unit may contain additional stuff like glue
251  * lengths, but we don't care, because such lengths are ERT anyway.
252  * \returns true if \p value and \p unit are valid.
253  */
254 bool splitLatexLength(string const & len, string & value, string & unit)
255 {
256         if (len.empty())
257                 return false;
258         const string::size_type i = len.find_first_not_of(" -+0123456789.,");
259         //'4,5' is a valid LaTeX length number. Change it to '4.5'
260         string const length = subst(len, ',', '.');
261         if (i == string::npos)
262                 return false;
263         if (i == 0) {
264                 if (len[0] == '\\') {
265                         // We had something like \textwidth without a factor
266                         value = "1.0";
267                 } else {
268                         return false;
269                 }
270         } else {
271                 value = trimSpaceAndEol(string(length, 0, i));
272         }
273         if (value == "-")
274                 value = "-1.0";
275         // 'cM' is a valid LaTeX length unit. Change it to 'cm'
276         if (contains(len, '\\'))
277                 unit = trimSpaceAndEol(string(len, i));
278         else
279                 unit = ascii_lowercase(trimSpaceAndEol(string(len, i)));
280         return true;
281 }
282
283
284 /// A simple function to translate a latex length to something LyX can
285 /// understand. Not perfect, but rather best-effort.
286 bool translate_len(string const & length, string & valstring, string & unit)
287 {
288         if (!splitLatexLength(length, valstring, unit))
289                 return false;
290         // LyX uses percent values
291         double value;
292         istringstream iss(valstring);
293         iss >> value;
294         value *= 100;
295         ostringstream oss;
296         oss << value;
297         string const percentval = oss.str();
298         // a normal length
299         if (unit.empty() || unit[0] != '\\')
300                 return true;
301         string::size_type const i = unit.find(' ');
302         string const endlen = (i == string::npos) ? string() : string(unit, i);
303         if (unit == "\\textwidth") {
304                 valstring = percentval;
305                 unit = "text%" + endlen;
306         } else if (unit == "\\columnwidth") {
307                 valstring = percentval;
308                 unit = "col%" + endlen;
309         } else if (unit == "\\paperwidth") {
310                 valstring = percentval;
311                 unit = "page%" + endlen;
312         } else if (unit == "\\linewidth") {
313                 valstring = percentval;
314                 unit = "line%" + endlen;
315         } else if (unit == "\\paperheight") {
316                 valstring = percentval;
317                 unit = "pheight%" + endlen;
318         } else if (unit == "\\textheight") {
319                 valstring = percentval;
320                 unit = "theight%" + endlen;
321         }
322         return true;
323 }
324
325 }
326
327
328 string translate_len(string const & length)
329 {
330         string unit;
331         string value;
332         if (translate_len(length, value, unit))
333                 return value + unit;
334         // If the input is invalid, return what we have.
335         return length;
336 }
337
338
339 namespace {
340
341 /*!
342  * Translates a LaTeX length into \p value, \p unit and
343  * \p special parts suitable for a box inset.
344  * The difference from translate_len() is that a box inset knows about
345  * some special "units" that are stored in \p special.
346  */
347 void translate_box_len(string const & length, string & value, string & unit, string & special)
348 {
349         if (translate_len(length, value, unit)) {
350                 if (unit == "\\height" || unit == "\\depth" ||
351                     unit == "\\totalheight" || unit == "\\width") {
352                         special = unit.substr(1);
353                         // The unit is not used, but LyX requires a dummy setting
354                         unit = "in";
355                 } else
356                         special = "none";
357         } else {
358                 value.clear();
359                 unit = length;
360                 special = "none";
361         }
362 }
363
364
365 /*!
366  * Find a file with basename \p name in path \p path and an extension
367  * in \p extensions.
368  */
369 string find_file(string const & name, string const & path,
370                  char const * const * extensions)
371 {
372         for (char const * const * what = extensions; *what; ++what) {
373                 string const trial = addExtension(name, *what);
374                 if (makeAbsPath(trial, path).exists())
375                         return trial;
376         }
377         return string();
378 }
379
380
381 void begin_inset(ostream & os, string const & name)
382 {
383         os << "\n\\begin_inset " << name;
384 }
385
386
387 void begin_command_inset(ostream & os, string const & name,
388                          string const & latexname)
389 {
390         begin_inset(os, "CommandInset ");
391         os << name << "\nLatexCommand " << latexname << '\n';
392 }
393
394
395 void end_inset(ostream & os)
396 {
397         os << "\n\\end_inset\n\n";
398 }
399
400
401 bool skip_braces(Parser & p)
402 {
403         if (p.next_token().cat() != catBegin)
404                 return false;
405         p.get_token();
406         if (p.next_token().cat() == catEnd) {
407                 p.get_token();
408                 return true;
409         }
410         p.putback();
411         return false;
412 }
413
414
415 /// replace LaTeX commands in \p s from the unicodesymbols file with their
416 /// unicode points
417 docstring convert_unicodesymbols(docstring s)
418 {
419         odocstringstream os;
420         for (size_t i = 0; i < s.size();) {
421                 if (s[i] != '\\') {
422                         os.put(s[i++]);
423                         continue;
424                 }
425                 s = s.substr(i);
426                 docstring rem;
427                 docstring parsed = encodings.fromLaTeXCommand(s, rem,
428                                 Encodings::TEXT_CMD);
429                 os << parsed;
430                 s = rem;
431                 if (s.empty() || s[0] != '\\')
432                         i = 0;
433                 else
434                         i = 1;
435         }
436         return os.str();
437 }
438
439
440 /// try to convert \p s to a valid InsetCommand argument
441 string convert_command_inset_arg(string s)
442 {
443         if (isAscii(s))
444                 // since we don't know the input encoding we can't use from_utf8
445                 s = to_utf8(convert_unicodesymbols(from_ascii(s)));
446         // LyX cannot handle newlines in a latex command
447         return subst(s, "\n", " ");
448 }
449
450
451 void handle_backslash(ostream & os, string const & s)
452 {
453         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
454                 if (*it == '\\')
455                         os << "\n\\backslash\n";
456                 else
457                         os << *it;
458         }
459 }
460
461
462 void handle_ert(ostream & os, string const & s, Context & context)
463 {
464         // We must have a valid layout before outputting the ERT inset.
465         context.check_layout(os);
466         Context newcontext(true, context.textclass);
467         begin_inset(os, "ERT");
468         os << "\nstatus collapsed\n";
469         newcontext.check_layout(os);
470         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
471                 if (*it == '\\')
472                         os << "\n\\backslash\n";
473                 else if (*it == '\n') {
474                         newcontext.new_paragraph(os);
475                         newcontext.check_layout(os);
476                 } else
477                         os << *it;
478         }
479         newcontext.check_end_layout(os);
480         end_inset(os);
481 }
482
483
484 void handle_comment(ostream & os, string const & s, Context & context)
485 {
486         // TODO: Handle this better
487         Context newcontext(true, context.textclass);
488         begin_inset(os, "ERT");
489         os << "\nstatus collapsed\n";
490         newcontext.check_layout(os);
491         handle_backslash(os, s);
492         // make sure that our comment is the last thing on the line
493         newcontext.new_paragraph(os);
494         newcontext.check_layout(os);
495         newcontext.check_end_layout(os);
496         end_inset(os);
497 }
498
499
500 Layout const * findLayout(TextClass const & textclass, string const & name, bool command)
501 {
502         Layout const * layout = findLayoutWithoutModule(textclass, name, command);
503         if (layout)
504                 return layout;
505         if (checkModule(name, command))
506                 return findLayoutWithoutModule(textclass, name, command);
507         return layout;
508 }
509
510
511 InsetLayout const * findInsetLayout(TextClass const & textclass, string const & name, bool command)
512 {
513         InsetLayout const * insetlayout = findInsetLayoutWithoutModule(textclass, name, command);
514         if (insetlayout)
515                 return insetlayout;
516         if (checkModule(name, command))
517                 return findInsetLayoutWithoutModule(textclass, name, command);
518         return insetlayout;
519 }
520
521
522 void eat_whitespace(Parser &, ostream &, Context &, bool);
523
524
525 /*!
526  * Skips whitespace and braces.
527  * This should be called after a command has been parsed that is not put into
528  * ERT, and where LyX adds "{}" if needed.
529  */
530 void skip_spaces_braces(Parser & p, bool keepws = false)
531 {
532         /* The following four examples produce the same typeset output and
533            should be handled by this function:
534            - abc \j{} xyz
535            - abc \j {} xyz
536            - abc \j 
537              {} xyz
538            - abc \j %comment
539              {} xyz
540          */
541         // Unfortunately we need to skip comments, too.
542         // We can't use eat_whitespace since writing them after the {}
543         // results in different output in some cases.
544         bool const skipped_spaces = p.skip_spaces(true);
545         bool const skipped_braces = skip_braces(p);
546         if (keepws && skipped_spaces && !skipped_braces)
547                 // put back the space (it is better handled by check_space)
548                 p.unskip_spaces(true);
549 }
550
551
552 void output_command_layout(ostream & os, Parser & p, bool outer,
553                            Context & parent_context,
554                            Layout const * newlayout)
555 {
556         TeXFont const oldFont = parent_context.font;
557         // save the current font size
558         string const size = oldFont.size;
559         // reset the font size to default, because the font size switches
560         // don't affect section headings and the like
561         parent_context.font.size = Context::normalfont.size;
562         // we only need to write the font change if we have an open layout
563         if (!parent_context.atParagraphStart())
564                 output_font_change(os, oldFont, parent_context.font);
565         parent_context.check_end_layout(os);
566         Context context(true, parent_context.textclass, newlayout,
567                         parent_context.layout, parent_context.font);
568         if (parent_context.deeper_paragraph) {
569                 // We are beginning a nested environment after a
570                 // deeper paragraph inside the outer list environment.
571                 // Therefore we don't need to output a "begin deeper".
572                 context.need_end_deeper = true;
573         }
574         context.check_deeper(os);
575         context.check_layout(os);
576         unsigned int optargs = 0;
577         while (optargs < context.layout->optargs) {
578                 eat_whitespace(p, os, context, false);
579                 if (p.next_token().cat() == catEscape ||
580                     p.next_token().character() != '[') 
581                         break;
582                 p.get_token(); // eat '['
583                 begin_inset(os, "Argument\n");
584                 os << "status collapsed\n\n";
585                 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
586                 end_inset(os);
587                 eat_whitespace(p, os, context, false);
588                 ++optargs;
589         }
590         unsigned int reqargs = 0;
591         while (reqargs < context.layout->reqargs) {
592                 eat_whitespace(p, os, context, false);
593                 if (p.next_token().cat() != catBegin)
594                         break;
595                 p.get_token(); // eat '{'
596                 begin_inset(os, "Argument\n");
597                 os << "status collapsed\n\n";
598                 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
599                 end_inset(os);
600                 eat_whitespace(p, os, context, false);
601                 ++reqargs;
602         }
603         parse_text(p, os, FLAG_ITEM, outer, context);
604         context.check_end_layout(os);
605         if (parent_context.deeper_paragraph) {
606                 // We must suppress the "end deeper" because we
607                 // suppressed the "begin deeper" above.
608                 context.need_end_deeper = false;
609         }
610         context.check_end_deeper(os);
611         // We don't need really a new paragraph, but
612         // we must make sure that the next item gets a \begin_layout.
613         parent_context.new_paragraph(os);
614         // Set the font size to the original value. No need to output it here
615         // (Context::begin_layout() will do that if needed)
616         parent_context.font.size = size;
617 }
618
619
620 /*!
621  * Output a space if necessary.
622  * This function gets called for every whitespace token.
623  *
624  * We have three cases here:
625  * 1. A space must be suppressed. Example: The lyxcode case below
626  * 2. A space may be suppressed. Example: Spaces before "\par"
627  * 3. A space must not be suppressed. Example: A space between two words
628  *
629  * We currently handle only 1. and 3 and from 2. only the case of
630  * spaces before newlines as a side effect.
631  *
632  * 2. could be used to suppress as many spaces as possible. This has two effects:
633  * - Reimporting LyX generated LaTeX files changes almost no whitespace
634  * - Superflous whitespace from non LyX generated LaTeX files is removed.
635  * The drawback is that the logic inside the function becomes
636  * complicated, and that is the reason why it is not implemented.
637  */
638 void check_space(Parser & p, ostream & os, Context & context)
639 {
640         Token const next = p.next_token();
641         Token const curr = p.curr_token();
642         // A space before a single newline and vice versa must be ignored
643         // LyX emits a newline before \end{lyxcode}.
644         // This newline must be ignored,
645         // otherwise LyX will add an additional protected space.
646         if (next.cat() == catSpace ||
647             next.cat() == catNewline ||
648             (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
649                 return;
650         }
651         context.check_layout(os);
652         os << ' ';
653 }
654
655
656 /*!
657  * Parse all arguments of \p command
658  */
659 void parse_arguments(string const & command,
660                      vector<ArgumentType> const & template_arguments,
661                      Parser & p, ostream & os, bool outer, Context & context)
662 {
663         string ert = command;
664         size_t no_arguments = template_arguments.size();
665         for (size_t i = 0; i < no_arguments; ++i) {
666                 switch (template_arguments[i]) {
667                 case required:
668                         // This argument contains regular LaTeX
669                         handle_ert(os, ert + '{', context);
670                         eat_whitespace(p, os, context, false);
671                         parse_text(p, os, FLAG_ITEM, outer, context);
672                         ert = "}";
673                         break;
674                 case item:
675                         // This argument consists only of a single item.
676                         // The presence of '{' or not must be preserved.
677                         p.skip_spaces();
678                         if (p.next_token().cat() == catBegin)
679                                 ert += '{' + p.verbatim_item() + '}';
680                         else
681                                 ert += p.verbatim_item();
682                         break;
683                 case verbatim:
684                         // This argument may contain special characters
685                         ert += '{' + p.verbatim_item() + '}';
686                         break;
687                 case optional:
688                         // true because we must not eat whitespace
689                         // if an optional arg follows we must not strip the
690                         // brackets from this one
691                         if (i < no_arguments - 1 &&
692                             template_arguments[i+1] == optional)
693                                 ert += p.getFullOpt(true);
694                         else
695                                 ert += p.getOpt(true);
696                         break;
697                 }
698         }
699         handle_ert(os, ert, context);
700 }
701
702
703 /*!
704  * Check whether \p command is a known command. If yes,
705  * handle the command with all arguments.
706  * \return true if the command was parsed, false otherwise.
707  */
708 bool parse_command(string const & command, Parser & p, ostream & os,
709                    bool outer, Context & context)
710 {
711         if (known_commands.find(command) != known_commands.end()) {
712                 parse_arguments(command, known_commands[command], p, os,
713                                 outer, context);
714                 return true;
715         }
716         return false;
717 }
718
719
720 /// Parses a minipage or parbox
721 void parse_box(Parser & p, ostream & os, unsigned outer_flags,
722                unsigned inner_flags, bool outer, Context & parent_context,
723                string const & outer_type, string const & special,
724                string const & inner_type)
725 {
726         string position;
727         string inner_pos;
728         string hor_pos = "c";
729         // We need to set the height to the LaTeX default of 1\\totalheight
730         // for the case when no height argument is given
731         string height_value = "1";
732         string height_unit = "in";
733         string height_special = "totalheight";
734         string latex_height;
735         if (!inner_type.empty() && p.hasOpt()) {
736                 position = p.getArg('[', ']');
737                 if (position != "t" && position != "c" && position != "b") {
738                         cerr << "invalid position " << position << " for "
739                              << inner_type << endl;
740                         position = "c";
741                 }
742                 if (p.hasOpt()) {
743                         latex_height = p.getArg('[', ']');
744                         translate_box_len(latex_height, height_value, height_unit, height_special);
745
746                         if (p.hasOpt()) {
747                                 inner_pos = p.getArg('[', ']');
748                                 if (inner_pos != "c" && inner_pos != "t" &&
749                                     inner_pos != "b" && inner_pos != "s") {
750                                         cerr << "invalid inner_pos "
751                                              << inner_pos << " for "
752                                              << inner_type << endl;
753                                         inner_pos = position;
754                                 }
755                         }
756                 }
757         }
758         string width_value;
759         string width_unit;
760         string latex_width;
761         if (inner_type.empty()) {
762                 if (special.empty())
763                         latex_width = "\\columnwidth";
764                 else {
765                         Parser p2(special);
766                         latex_width = p2.getArg('[', ']');
767                         string const opt = p2.getArg('[', ']');
768                         if (!opt.empty()) {
769                                 hor_pos = opt;
770                                 if (hor_pos != "l" && hor_pos != "c" &&
771                                     hor_pos != "r") {
772                                         cerr << "invalid hor_pos " << hor_pos
773                                              << " for " << outer_type << endl;
774                                         hor_pos = "c";
775                                 }
776                         }
777                 }
778         } else
779                 latex_width = p.verbatim_item();
780         translate_len(latex_width, width_value, width_unit);
781         // LyX can't handle length variables
782         bool use_ert = contains(width_unit, '\\') || contains(height_unit, '\\');
783         if (!use_ert && !outer_type.empty() && !inner_type.empty()) {
784                 // Look whether there is some content after the end of the
785                 // inner box, but before the end of the outer box.
786                 // If yes, we need to output ERT.
787                 p.pushPosition();
788                 if (inner_flags & FLAG_END)
789                         p.verbatimEnvironment(inner_type);
790                 else
791                         p.verbatim_item();
792                 p.skip_spaces(true);
793                 if ((outer_type == "framed" && p.next_token().asInput() != "\\end") ||
794                     (outer_type != "framed" && p.next_token().cat() != catEnd)) {
795                         // something is between the end of the inner box and
796                         // the end of the outer box, so we need to use ERT.
797                         use_ert = true;
798                 }
799                 p.popPosition();
800         }
801         if (use_ert) {
802                 ostringstream ss;
803                 if (!outer_type.empty()) {
804                         if (outer_flags & FLAG_END)
805                                 ss << "\\begin{" << outer_type << '}';
806                         else {
807                                 ss << '\\' << outer_type << '{';
808                                 if (!special.empty())
809                                         ss << special;
810                         }
811                 }
812                 if (!inner_type.empty()) {
813                         if (inner_flags & FLAG_END)
814                                 ss << "\\begin{" << inner_type << '}';
815                         else
816                                 ss << '\\' << inner_type;
817                         if (!position.empty())
818                                 ss << '[' << position << ']';
819                         if (!latex_height.empty())
820                                 ss << '[' << latex_height << ']';
821                         if (!inner_pos.empty())
822                                 ss << '[' << inner_pos << ']';
823                         ss << '{' << latex_width << '}';
824                         if (!(inner_flags & FLAG_END))
825                                 ss << '{';
826                 }
827                 handle_ert(os, ss.str(), parent_context);
828                 if (!inner_type.empty()) {
829                         parse_text(p, os, inner_flags, outer, parent_context);
830                         if (inner_flags & FLAG_END)
831                                 handle_ert(os, "\\end{" + inner_type + '}',
832                                            parent_context);
833                         else
834                                 handle_ert(os, "}", parent_context);
835                 }
836                 if (!outer_type.empty()) {
837                         parse_text(p, os, outer_flags, outer, parent_context);
838                         if (outer_flags & FLAG_END)
839                                 handle_ert(os, "\\end{" + outer_type + '}',
840                                            parent_context);
841                         else
842                                 handle_ert(os, "}", parent_context);
843                 }
844         } else {
845                 // LyX does not like empty positions, so we have
846                 // to set them to the LaTeX default values here.
847                 if (position.empty())
848                         position = "c";
849                 if (inner_pos.empty())
850                         inner_pos = position;
851                 parent_context.check_layout(os);
852                 begin_inset(os, "Box ");
853                 if (outer_type == "framed")
854                         os << "Framed\n";
855                 else if (outer_type == "framebox")
856                         os << "Boxed\n";
857                 else if (outer_type == "shadowbox")
858                         os << "Shadowbox\n";
859                 else if (outer_type == "shaded")
860                         os << "Shaded\n";
861                 else if (outer_type == "doublebox")
862                         os << "Doublebox\n";
863                 else if (outer_type.empty())
864                         os << "Frameless\n";
865                 else
866                         os << outer_type << '\n';
867                 os << "position \"" << position << "\"\n";
868                 os << "hor_pos \"" << hor_pos << "\"\n";
869                 os << "has_inner_box " << !inner_type.empty() << "\n";
870                 os << "inner_pos \"" << inner_pos << "\"\n";
871                 os << "use_parbox " << (inner_type == "parbox") << '\n';
872                 os << "use_makebox 0\n";
873                 os << "width \"" << width_value << width_unit << "\"\n";
874                 os << "special \"none\"\n";
875                 os << "height \"" << height_value << height_unit << "\"\n";
876                 os << "height_special \"" << height_special << "\"\n";
877                 os << "status open\n\n";
878                 Context context(true, parent_context.textclass);
879                 context.font = parent_context.font;
880
881                 // If we have no inner box the contens will be read with the outer box
882                 if (!inner_type.empty())
883                         parse_text(p, os, inner_flags, outer, context);
884
885                 // Ensure that the end of the outer box is parsed correctly:
886                 // The opening brace has been eaten by parse_outer_box()
887                 if (!outer_type.empty() && (outer_flags & FLAG_ITEM)) {
888                         outer_flags &= ~FLAG_ITEM;
889                         outer_flags |= FLAG_BRACE_LAST;
890                 }
891
892                 // Find end of outer box, output contents if inner_type is
893                 // empty and output possible comments
894                 if (!outer_type.empty()) {
895                         // This does not output anything but comments if
896                         // inner_type is not empty (see use_ert)
897                         parse_text(p, os, outer_flags, outer, context);
898                 }
899
900                 context.check_end_layout(os);
901                 end_inset(os);
902 #ifdef PRESERVE_LAYOUT
903                 // LyX puts a % after the end of the minipage
904                 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
905                         // new paragraph
906                         //handle_comment(os, "%dummy", parent_context);
907                         p.get_token();
908                         p.skip_spaces();
909                         parent_context.new_paragraph(os);
910                 }
911                 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
912                         //handle_comment(os, "%dummy", parent_context);
913                         p.get_token();
914                         p.skip_spaces();
915                         // We add a protected space if something real follows
916                         if (p.good() && p.next_token().cat() != catComment) {
917                                 begin_inset(os, "space ~\n");
918                                 end_inset(os);
919                         }
920                 }
921 #endif
922         }
923 }
924
925
926 void parse_outer_box(Parser & p, ostream & os, unsigned flags, bool outer,
927                      Context & parent_context, string const & outer_type,
928                      string const & special)
929 {
930         eat_whitespace(p, os, parent_context, false);
931         if (flags & FLAG_ITEM) {
932                 // Eat '{'
933                 if (p.next_token().cat() == catBegin)
934                         p.get_token();
935                 else
936                         cerr << "Warning: Ignoring missing '{' after \\"
937                              << outer_type << '.' << endl;
938                 eat_whitespace(p, os, parent_context, false);
939         }
940         string inner;
941         unsigned int inner_flags = 0;
942         if (outer_type == "shaded") {
943                 // These boxes never have an inner box
944                 ;
945         } else if (p.next_token().asInput() == "\\parbox") {
946                 inner = p.get_token().cs();
947                 inner_flags = FLAG_ITEM;
948         } else if (p.next_token().asInput() == "\\begin") {
949                 // Is this a minipage?
950                 p.pushPosition();
951                 p.get_token();
952                 inner = p.getArg('{', '}');
953                 p.popPosition();
954                 if (inner == "minipage") {
955                         p.get_token();
956                         p.getArg('{', '}');
957                         eat_whitespace(p, os, parent_context, false);
958                         inner_flags = FLAG_END;
959                 } else
960                         inner = "";
961         }
962         if (inner_flags == FLAG_END) {
963                 active_environments.push_back(inner);
964                 parse_box(p, os, flags, FLAG_END, outer, parent_context,
965                           outer_type, special, inner);
966                 active_environments.pop_back();
967         } else {
968                 parse_box(p, os, flags, inner_flags, outer, parent_context,
969                           outer_type, special, inner);
970         }
971 }
972
973
974 void parse_listings(Parser & p, ostream & os, Context & parent_context)
975 {
976         parent_context.check_layout(os);
977         begin_inset(os, "listings\n");
978         os << "inline false\n"
979            << "status collapsed\n";
980         Context context(true, parent_context.textclass);
981         context.layout = &parent_context.textclass.plainLayout();
982         context.check_layout(os);
983         string const s = p.verbatimEnvironment("lstlisting");
984         for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
985                 if (*it == '\\')
986                         os << "\n\\backslash\n";
987                 else if (*it == '\n') {
988                         // avoid adding an empty paragraph at the end
989                         if (it + 1 != et) {
990                                 context.new_paragraph(os);
991                                 context.check_layout(os);
992                         }
993                 } else
994                         os << *it;
995         }
996         context.check_end_layout(os);
997         end_inset(os);
998 }
999
1000
1001 /// parse an unknown environment
1002 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
1003                                unsigned flags, bool outer,
1004                                Context & parent_context)
1005 {
1006         if (name == "tabbing")
1007                 // We need to remember that we have to handle '\=' specially
1008                 flags |= FLAG_TABBING;
1009
1010         // We need to translate font changes and paragraphs inside the
1011         // environment to ERT if we have a non standard font.
1012         // Otherwise things like
1013         // \large\begin{foo}\huge bar\end{foo}
1014         // will not work.
1015         bool const specialfont =
1016                 (parent_context.font != parent_context.normalfont);
1017         bool const new_layout_allowed = parent_context.new_layout_allowed;
1018         if (specialfont)
1019                 parent_context.new_layout_allowed = false;
1020         handle_ert(os, "\\begin{" + name + "}", parent_context);
1021         parse_text_snippet(p, os, flags, outer, parent_context);
1022         handle_ert(os, "\\end{" + name + "}", parent_context);
1023         if (specialfont)
1024                 parent_context.new_layout_allowed = new_layout_allowed;
1025 }
1026
1027
1028 void parse_environment(Parser & p, ostream & os, bool outer,
1029                        string & last_env, Context & parent_context)
1030 {
1031         Layout const * newlayout;
1032         InsetLayout const * newinsetlayout = 0;
1033         string const name = p.getArg('{', '}');
1034         const bool is_starred = suffixIs(name, '*');
1035         string const unstarred_name = rtrim(name, "*");
1036         active_environments.push_back(name);
1037
1038         if (is_math_env(name)) {
1039                 parent_context.check_layout(os);
1040                 begin_inset(os, "Formula ");
1041                 os << "\\begin{" << name << "}";
1042                 parse_math(p, os, FLAG_END, MATH_MODE);
1043                 os << "\\end{" << name << "}";
1044                 end_inset(os);
1045         }
1046
1047         else if (name == "tabular" || name == "longtable") {
1048                 eat_whitespace(p, os, parent_context, false);
1049                 parent_context.check_layout(os);
1050                 begin_inset(os, "Tabular ");
1051                 handle_tabular(p, os, name == "longtable", parent_context);
1052                 end_inset(os);
1053                 p.skip_spaces();
1054         }
1055
1056         else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
1057                 eat_whitespace(p, os, parent_context, false);
1058                 parent_context.check_layout(os);
1059                 begin_inset(os, "Float " + unstarred_name + "\n");
1060                 if (p.hasOpt())
1061                         os << "placement " << p.getArg('[', ']') << '\n';
1062                 os << "wide " << convert<string>(is_starred)
1063                    << "\nsideways false"
1064                    << "\nstatus open\n\n";
1065                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1066                 end_inset(os);
1067                 // We don't need really a new paragraph, but
1068                 // we must make sure that the next item gets a \begin_layout.
1069                 parent_context.new_paragraph(os);
1070                 p.skip_spaces();
1071         }
1072
1073         else if (name == "minipage") {
1074                 eat_whitespace(p, os, parent_context, false);
1075                 parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
1076                 p.skip_spaces();
1077         }
1078
1079         else if (name == "comment") {
1080                 eat_whitespace(p, os, parent_context, false);
1081                 parent_context.check_layout(os);
1082                 begin_inset(os, "Note Comment\n");
1083                 os << "status open\n";
1084                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1085                 end_inset(os);
1086                 p.skip_spaces();
1087                 skip_braces(p); // eat {} that might by set by LyX behind comments
1088         }
1089
1090         else if (name == "lyxgreyedout") {
1091                 eat_whitespace(p, os, parent_context, false);
1092                 parent_context.check_layout(os);
1093                 begin_inset(os, "Note Greyedout\n");
1094                 os << "status open\n";
1095                 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
1096                 end_inset(os);
1097                 p.skip_spaces();
1098         }
1099
1100         else if (name == "framed" || name == "shaded") {
1101                 eat_whitespace(p, os, parent_context, false);
1102                 parse_outer_box(p, os, FLAG_END, outer, parent_context, name, "");
1103                 p.skip_spaces();
1104         }
1105
1106         else if (name == "lstlisting") {
1107                 eat_whitespace(p, os, parent_context, false);
1108                 // FIXME handle listings with parameters
1109                 if (p.hasOpt())
1110                         parse_unknown_environment(p, name, os, FLAG_END,
1111                                                   outer, parent_context);
1112                 else
1113                         parse_listings(p, os, parent_context);
1114                 p.skip_spaces();
1115         }
1116
1117         else if (!parent_context.new_layout_allowed)
1118                 parse_unknown_environment(p, name, os, FLAG_END, outer,
1119                                           parent_context);
1120
1121         // Alignment and spacing settings
1122         // FIXME (bug xxxx): These settings can span multiple paragraphs and
1123         //                                       therefore are totally broken!
1124         // Note that \centering, raggedright, and raggedleft cannot be handled, as
1125         // they are commands not environments. They are furthermore switches that
1126         // can be ended by another switches, but also by commands like \footnote or
1127         // \parbox. So the only safe way is to leave them untouched.
1128         else if (name == "center" || name == "centering" ||
1129                  name == "flushleft" || name == "flushright" ||
1130                  name == "singlespace" || name == "onehalfspace" ||
1131                  name == "doublespace" || name == "spacing") {
1132                 eat_whitespace(p, os, parent_context, false);
1133                 // We must begin a new paragraph if not already done
1134                 if (! parent_context.atParagraphStart()) {
1135                         parent_context.check_end_layout(os);
1136                         parent_context.new_paragraph(os);
1137                 }
1138                 if (name == "flushleft")
1139                         parent_context.add_extra_stuff("\\align left\n");
1140                 else if (name == "flushright")
1141                         parent_context.add_extra_stuff("\\align right\n");
1142                 else if (name == "center" || name == "centering")
1143                         parent_context.add_extra_stuff("\\align center\n");
1144                 else if (name == "singlespace")
1145                         parent_context.add_extra_stuff("\\paragraph_spacing single\n");
1146                 else if (name == "onehalfspace")
1147                         parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
1148                 else if (name == "doublespace")
1149                         parent_context.add_extra_stuff("\\paragraph_spacing double\n");
1150                 else if (name == "spacing")
1151                         parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
1152                 parse_text(p, os, FLAG_END, outer, parent_context);
1153                 // Just in case the environment is empty
1154                 parent_context.extra_stuff.erase();
1155                 // We must begin a new paragraph to reset the alignment
1156                 parent_context.new_paragraph(os);
1157                 p.skip_spaces();
1158         }
1159
1160         // The single '=' is meant here.
1161         else if ((newlayout = findLayout(parent_context.textclass, name, false))) {
1162                 eat_whitespace(p, os, parent_context, false);
1163                 Context context(true, parent_context.textclass, newlayout,
1164                                 parent_context.layout, parent_context.font);
1165                 if (parent_context.deeper_paragraph) {
1166                         // We are beginning a nested environment after a
1167                         // deeper paragraph inside the outer list environment.
1168                         // Therefore we don't need to output a "begin deeper".
1169                         context.need_end_deeper = true;
1170                 }
1171                 parent_context.check_end_layout(os);
1172                 if (last_env == name) {
1173                         // we need to output a separator since LyX would export
1174                         // the two environments as one otherwise (bug 5716)
1175                         docstring const sep = from_ascii("--Separator--");
1176                         TeX2LyXDocClass const & textclass(parent_context.textclass);
1177                         if (textclass.hasLayout(sep)) {
1178                                 Context newcontext(parent_context);
1179                                 newcontext.layout = &(textclass[sep]);
1180                                 newcontext.check_layout(os);
1181                                 newcontext.check_end_layout(os);
1182                         } else {
1183                                 parent_context.check_layout(os);
1184                                 begin_inset(os, "Note Note\n");
1185                                 os << "status closed\n";
1186                                 Context newcontext(true, textclass,
1187                                                 &(textclass.defaultLayout()));
1188                                 newcontext.check_layout(os);
1189                                 newcontext.check_end_layout(os);
1190                                 end_inset(os);
1191                                 parent_context.check_end_layout(os);
1192                         }
1193                 }
1194                 switch (context.layout->latextype) {
1195                 case  LATEX_LIST_ENVIRONMENT:
1196                         context.add_par_extra_stuff("\\labelwidthstring "
1197                                                     + p.verbatim_item() + '\n');
1198                         p.skip_spaces();
1199                         break;
1200                 case  LATEX_BIB_ENVIRONMENT:
1201                         p.verbatim_item(); // swallow next arg
1202                         p.skip_spaces();
1203                         break;
1204                 default:
1205                         break;
1206                 }
1207                 context.check_deeper(os);
1208                 // handle known optional and required arguments
1209                 // layouts require all optional arguments before the required ones
1210                 // Unfortunately LyX can't handle arguments of list arguments (bug 7468):
1211                 // It is impossible to place anything after the environment name,
1212                 // but before the first \\item.
1213                 if (context.layout->latextype == LATEX_ENVIRONMENT) {
1214                         bool need_layout = true;
1215                         unsigned int optargs = 0;
1216                         while (optargs < context.layout->optargs) {
1217                                 eat_whitespace(p, os, context, false);
1218                                 if (p.next_token().cat() == catEscape ||
1219                                     p.next_token().character() != '[') 
1220                                         break;
1221                                 p.get_token(); // eat '['
1222                                 if (need_layout) {
1223                                         context.check_layout(os);
1224                                         need_layout = false;
1225                                 }
1226                                 begin_inset(os, "Argument\n");
1227                                 os << "status collapsed\n\n";
1228                                 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1229                                 end_inset(os);
1230                                 eat_whitespace(p, os, context, false);
1231                                 ++optargs;
1232                         }
1233                         unsigned int reqargs = 0;
1234                         while (reqargs < context.layout->reqargs) {
1235                                 eat_whitespace(p, os, context, false);
1236                                 if (p.next_token().cat() != catBegin)
1237                                         break;
1238                                 p.get_token(); // eat '{'
1239                                 if (need_layout) {
1240                                         context.check_layout(os);
1241                                         need_layout = false;
1242                                 }
1243                                 begin_inset(os, "Argument\n");
1244                                 os << "status collapsed\n\n";
1245                                 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
1246                                 end_inset(os);
1247                                 eat_whitespace(p, os, context, false);
1248                                 ++reqargs;
1249                         }
1250                 }
1251                 parse_text(p, os, FLAG_END, outer, context);
1252                 context.check_end_layout(os);
1253                 if (parent_context.deeper_paragraph) {
1254                         // We must suppress the "end deeper" because we
1255                         // suppressed the "begin deeper" above.
1256                         context.need_end_deeper = false;
1257                 }
1258                 context.check_end_deeper(os);
1259                 parent_context.new_paragraph(os);
1260                 p.skip_spaces();
1261         }
1262
1263         // The single '=' is meant here.
1264         else if ((newinsetlayout = findInsetLayout(parent_context.textclass, name, false))) {
1265                 eat_whitespace(p, os, parent_context, false);
1266                 parent_context.check_layout(os);
1267                 begin_inset(os, "Flex ");
1268                 os << to_utf8(newinsetlayout->name()) << '\n'
1269                    << "status collapsed\n";
1270                 parse_text_in_inset(p, os, FLAG_END, false, parent_context, newinsetlayout);
1271                 end_inset(os);
1272         }
1273
1274         else if (name == "appendix") {
1275                 // This is no good latex style, but it works and is used in some documents...
1276                 eat_whitespace(p, os, parent_context, false);
1277                 parent_context.check_end_layout(os);
1278                 Context context(true, parent_context.textclass, parent_context.layout,
1279                                 parent_context.layout, parent_context.font);
1280                 context.check_layout(os);
1281                 os << "\\start_of_appendix\n";
1282                 parse_text(p, os, FLAG_END, outer, context);
1283                 context.check_end_layout(os);
1284                 p.skip_spaces();
1285         }
1286
1287         else if (known_environments.find(name) != known_environments.end()) {
1288                 vector<ArgumentType> arguments = known_environments[name];
1289                 // The last "argument" denotes wether we may translate the
1290                 // environment contents to LyX
1291                 // The default required if no argument is given makes us
1292                 // compatible with the reLyXre environment.
1293                 ArgumentType contents = arguments.empty() ?
1294                         required :
1295                         arguments.back();
1296                 if (!arguments.empty())
1297                         arguments.pop_back();
1298                 // See comment in parse_unknown_environment()
1299                 bool const specialfont =
1300                         (parent_context.font != parent_context.normalfont);
1301                 bool const new_layout_allowed =
1302                         parent_context.new_layout_allowed;
1303                 if (specialfont)
1304                         parent_context.new_layout_allowed = false;
1305                 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1306                                 outer, parent_context);
1307                 if (contents == verbatim)
1308                         handle_ert(os, p.verbatimEnvironment(name),
1309                                    parent_context);
1310                 else
1311                         parse_text_snippet(p, os, FLAG_END, outer,
1312                                            parent_context);
1313                 handle_ert(os, "\\end{" + name + "}", parent_context);
1314                 if (specialfont)
1315                         parent_context.new_layout_allowed = new_layout_allowed;
1316         }
1317
1318         else
1319                 parse_unknown_environment(p, name, os, FLAG_END, outer,
1320                                           parent_context);
1321
1322         last_env = name;
1323         active_environments.pop_back();
1324 }
1325
1326
1327 /// parses a comment and outputs it to \p os.
1328 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1329 {
1330         LASSERT(t.cat() == catComment, return);
1331         if (!t.cs().empty()) {
1332                 context.check_layout(os);
1333                 handle_comment(os, '%' + t.cs(), context);
1334                 if (p.next_token().cat() == catNewline) {
1335                         // A newline after a comment line starts a new
1336                         // paragraph
1337                         if (context.new_layout_allowed) {
1338                                 if(!context.atParagraphStart())
1339                                         // Only start a new paragraph if not already
1340                                         // done (we might get called recursively)
1341                                         context.new_paragraph(os);
1342                         } else
1343                                 handle_ert(os, "\n", context);
1344                         eat_whitespace(p, os, context, true);
1345                 }
1346         } else {
1347                 // "%\n" combination
1348                 p.skip_spaces();
1349         }
1350 }
1351
1352
1353 /*!
1354  * Reads spaces and comments until the first non-space, non-comment token.
1355  * New paragraphs (double newlines or \\par) are handled like simple spaces
1356  * if \p eatParagraph is true.
1357  * Spaces are skipped, but comments are written to \p os.
1358  */
1359 void eat_whitespace(Parser & p, ostream & os, Context & context,
1360                     bool eatParagraph)
1361 {
1362         while (p.good()) {
1363                 Token const & t = p.get_token();
1364                 if (t.cat() == catComment)
1365                         parse_comment(p, os, t, context);
1366                 else if ((! eatParagraph && p.isParagraph()) ||
1367                          (t.cat() != catSpace && t.cat() != catNewline)) {
1368                         p.putback();
1369                         return;
1370                 }
1371         }
1372 }
1373
1374
1375 /*!
1376  * Set a font attribute, parse text and reset the font attribute.
1377  * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1378  * \param currentvalue Current value of the attribute. Is set to the new
1379  * value during parsing.
1380  * \param newvalue New value of the attribute
1381  */
1382 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1383                            Context & context, string const & attribute,
1384                            string & currentvalue, string const & newvalue)
1385 {
1386         context.check_layout(os);
1387         string const oldvalue = currentvalue;
1388         currentvalue = newvalue;
1389         os << '\n' << attribute << ' ' << newvalue << "\n";
1390         parse_text_snippet(p, os, flags, outer, context);
1391         context.check_layout(os);
1392         os << '\n' << attribute << ' ' << oldvalue << "\n";
1393         currentvalue = oldvalue;
1394 }
1395
1396
1397 /// get the arguments of a natbib or jurabib citation command
1398 void get_cite_arguments(Parser & p, bool natbibOrder,
1399         string & before, string & after)
1400 {
1401         // We need to distinguish "" and "[]", so we can't use p.getOpt().
1402
1403         // text before the citation
1404         before.clear();
1405         // text after the citation
1406         after = p.getFullOpt();
1407
1408         if (!after.empty()) {
1409                 before = p.getFullOpt();
1410                 if (natbibOrder && !before.empty())
1411                         swap(before, after);
1412         }
1413 }
1414
1415
1416 /// Convert filenames with TeX macros and/or quotes to something LyX
1417 /// can understand
1418 string const normalize_filename(string const & name)
1419 {
1420         Parser p(trim(name, "\""));
1421         ostringstream os;
1422         while (p.good()) {
1423                 Token const & t = p.get_token();
1424                 if (t.cat() != catEscape)
1425                         os << t.asInput();
1426                 else if (t.cs() == "lyxdot") {
1427                         // This is used by LyX for simple dots in relative
1428                         // names
1429                         os << '.';
1430                         p.skip_spaces();
1431                 } else if (t.cs() == "space") {
1432                         os << ' ';
1433                         p.skip_spaces();
1434                 } else
1435                         os << t.asInput();
1436         }
1437         return os.str();
1438 }
1439
1440
1441 /// Convert \p name from TeX convention (relative to master file) to LyX
1442 /// convention (relative to .lyx file) if it is relative
1443 void fix_relative_filename(string & name)
1444 {
1445         if (FileName::isAbsolute(name))
1446                 return;
1447
1448         name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1449                                    from_utf8(getParentFilePath())));
1450 }
1451
1452
1453 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1454 void parse_noweb(Parser & p, ostream & os, Context & context)
1455 {
1456         // assemble the rest of the keyword
1457         string name("<<");
1458         bool scrap = false;
1459         while (p.good()) {
1460                 Token const & t = p.get_token();
1461                 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1462                         name += ">>";
1463                         p.get_token();
1464                         scrap = (p.good() && p.next_token().asInput() == "=");
1465                         if (scrap)
1466                                 name += p.get_token().asInput();
1467                         break;
1468                 }
1469                 name += t.asInput();
1470         }
1471
1472         if (!scrap || !context.new_layout_allowed ||
1473             !context.textclass.hasLayout(from_ascii("Scrap"))) {
1474                 cerr << "Warning: Could not interpret '" << name
1475                      << "'. Ignoring it." << endl;
1476                 return;
1477         }
1478
1479         // We use new_paragraph instead of check_end_layout because the stuff
1480         // following the noweb chunk needs to start with a \begin_layout.
1481         // This may create a new paragraph even if there was none in the
1482         // noweb file, but the alternative is an invalid LyX file. Since
1483         // noweb code chunks are implemented with a layout style in LyX they
1484         // always must be in an own paragraph.
1485         context.new_paragraph(os);
1486         Context newcontext(true, context.textclass,
1487                 &context.textclass[from_ascii("Scrap")]);
1488         newcontext.check_layout(os);
1489         os << name;
1490         while (p.good()) {
1491                 Token const & t = p.get_token();
1492                 // We abuse the parser a bit, because this is no TeX syntax
1493                 // at all.
1494                 if (t.cat() == catEscape)
1495                         os << subst(t.asInput(), "\\", "\n\\backslash\n");
1496                 else {
1497                         ostringstream oss;
1498                         Context tmp(false, context.textclass,
1499                                     &context.textclass[from_ascii("Scrap")]);
1500                         tmp.need_end_layout = true;
1501                         tmp.check_layout(oss);
1502                         os << subst(t.asInput(), "\n", oss.str());
1503                 }
1504                 // The scrap chunk is ended by an @ at the beginning of a line.
1505                 // After the @ the line may contain a comment and/or
1506                 // whitespace, but nothing else.
1507                 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1508                     (p.next_token().cat() == catSpace ||
1509                      p.next_token().cat() == catNewline ||
1510                      p.next_token().cat() == catComment)) {
1511                         while (p.good() && p.next_token().cat() == catSpace)
1512                                 os << p.get_token().asInput();
1513                         if (p.next_token().cat() == catComment)
1514                                 // The comment includes a final '\n'
1515                                 os << p.get_token().asInput();
1516                         else {
1517                                 if (p.next_token().cat() == catNewline)
1518                                         p.get_token();
1519                                 os << '\n';
1520                         }
1521                         break;
1522                 }
1523         }
1524         newcontext.check_end_layout(os);
1525 }
1526
1527
1528 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1529 bool is_macro(Parser & p)
1530 {
1531         Token first = p.curr_token();
1532         if (first.cat() != catEscape || !p.good())
1533                 return false;
1534         if (first.cs() == "def")
1535                 return true;
1536         if (first.cs() != "global" && first.cs() != "long")
1537                 return false;
1538         Token second = p.get_token();
1539         int pos = 1;
1540         while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1541                second.cat() == catNewline || second.cat() == catComment)) {
1542                 second = p.get_token();
1543                 pos++;
1544         }
1545         bool secondvalid = second.cat() == catEscape;
1546         Token third;
1547         bool thirdvalid = false;
1548         if (p.good() && first.cs() == "global" && secondvalid &&
1549             second.cs() == "long") {
1550                 third = p.get_token();
1551                 pos++;
1552                 while (p.good() && !p.isParagraph() &&
1553                        (third.cat() == catSpace ||
1554                         third.cat() == catNewline ||
1555                         third.cat() == catComment)) {
1556                         third = p.get_token();
1557                         pos++;
1558                 }
1559                 thirdvalid = third.cat() == catEscape;
1560         }
1561         for (int i = 0; i < pos; ++i)
1562                 p.putback();
1563         if (!secondvalid)
1564                 return false;
1565         if (!thirdvalid)
1566                 return (first.cs() == "global" || first.cs() == "long") &&
1567                        second.cs() == "def";
1568         return first.cs() == "global" && second.cs() == "long" &&
1569                third.cs() == "def";
1570 }
1571
1572
1573 /// Parse a macro definition (assumes that is_macro() returned true)
1574 void parse_macro(Parser & p, ostream & os, Context & context)
1575 {
1576         context.check_layout(os);
1577         Token first = p.curr_token();
1578         Token second;
1579         Token third;
1580         string command = first.asInput();
1581         if (first.cs() != "def") {
1582                 p.get_token();
1583                 eat_whitespace(p, os, context, false);
1584                 second = p.curr_token();
1585                 command += second.asInput();
1586                 if (second.cs() != "def") {
1587                         p.get_token();
1588                         eat_whitespace(p, os, context, false);
1589                         third = p.curr_token();
1590                         command += third.asInput();
1591                 }
1592         }
1593         eat_whitespace(p, os, context, false);
1594         string const name = p.get_token().cs();
1595         eat_whitespace(p, os, context, false);
1596
1597         // parameter text
1598         bool simple = true;
1599         string paramtext;
1600         int arity = 0;
1601         while (p.next_token().cat() != catBegin) {
1602                 if (p.next_token().cat() == catParameter) {
1603                         // # found
1604                         p.get_token();
1605                         paramtext += "#";
1606
1607                         // followed by number?
1608                         if (p.next_token().cat() == catOther) {
1609                                 char c = p.getChar();
1610                                 paramtext += c;
1611                                 // number = current arity + 1?
1612                                 if (c == arity + '0' + 1)
1613                                         ++arity;
1614                                 else
1615                                         simple = false;
1616                         } else
1617                                 paramtext += p.get_token().cs();
1618                 } else {
1619                         paramtext += p.get_token().cs();
1620                         simple = false;
1621                 }
1622         }
1623
1624         // only output simple (i.e. compatible) macro as FormulaMacros
1625         string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1626         if (simple) {
1627                 context.check_layout(os);
1628                 begin_inset(os, "FormulaMacro");
1629                 os << "\n\\def" << ert;
1630                 end_inset(os);
1631         } else
1632                 handle_ert(os, command + ert, context);
1633 }
1634
1635 } // anonymous namespace
1636
1637
1638 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1639                 Context & context)
1640 {
1641         Layout const * newlayout = 0;
1642         InsetLayout const * newinsetlayout = 0;
1643         // Store the latest bibliographystyle and nocite{*} option
1644         // (needed for bibtex inset)
1645         string btprint;
1646         string bibliographystyle;
1647         bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1648         bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1649         string last_env;
1650         while (p.good()) {
1651                 Token const & t = p.get_token();
1652
1653 #ifdef FILEDEBUG
1654                 debugToken(cerr, t, flags);
1655 #endif
1656
1657                 if (flags & FLAG_ITEM) {
1658                         if (t.cat() == catSpace)
1659                                 continue;
1660
1661                         flags &= ~FLAG_ITEM;
1662                         if (t.cat() == catBegin) {
1663                                 // skip the brace and collect everything to the next matching
1664                                 // closing brace
1665                                 flags |= FLAG_BRACE_LAST;
1666                                 continue;
1667                         }
1668
1669                         // handle only this single token, leave the loop if done
1670                         flags |= FLAG_LEAVE;
1671                 }
1672
1673                 if (t.cat() != catEscape && t.character() == ']' &&
1674                     (flags & FLAG_BRACK_LAST))
1675                         return;
1676                 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1677                         return;
1678
1679                 // If there is anything between \end{env} and \begin{env} we
1680                 // don't need to output a separator.
1681                 if (t.cat() != catSpace && t.cat() != catNewline &&
1682                     t.asInput() != "\\begin")
1683                         last_env = "";
1684
1685                 //
1686                 // cat codes
1687                 //
1688                 if (t.cat() == catMath) {
1689                         // we are inside some text mode thingy, so opening new math is allowed
1690                         context.check_layout(os);
1691                         begin_inset(os, "Formula ");
1692                         Token const & n = p.get_token();
1693                         if (n.cat() == catMath && outer) {
1694                                 // TeX's $$...$$ syntax for displayed math
1695                                 os << "\\[";
1696                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1697                                 os << "\\]";
1698                                 p.get_token(); // skip the second '$' token
1699                         } else {
1700                                 // simple $...$  stuff
1701                                 p.putback();
1702                                 os << '$';
1703                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1704                                 os << '$';
1705                         }
1706                         end_inset(os);
1707                 }
1708
1709                 else if (t.cat() == catSuper || t.cat() == catSub)
1710                         cerr << "catcode " << t << " illegal in text mode\n";
1711
1712                 // Basic support for english quotes. This should be
1713                 // extended to other quotes, but is not so easy (a
1714                 // left english quote is the same as a right german
1715                 // quote...)
1716                 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1717                         context.check_layout(os);
1718                         begin_inset(os, "Quotes ");
1719                         os << "eld";
1720                         end_inset(os);
1721                         p.get_token();
1722                         skip_braces(p);
1723                 }
1724                 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1725                         context.check_layout(os);
1726                         begin_inset(os, "Quotes ");
1727                         os << "erd";
1728                         end_inset(os);
1729                         p.get_token();
1730                         skip_braces(p);
1731                 }
1732
1733                 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1734                         context.check_layout(os);
1735                         begin_inset(os, "Quotes ");
1736                         os << "ald";
1737                         end_inset(os);
1738                         p.get_token();
1739                         skip_braces(p);
1740                 }
1741
1742                 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1743                         context.check_layout(os);
1744                         begin_inset(os, "Quotes ");
1745                         os << "ard";
1746                         end_inset(os);
1747                         p.get_token();
1748                         skip_braces(p);
1749                 }
1750
1751                 else if (t.asInput() == "<"
1752                          && p.next_token().asInput() == "<" && noweb_mode) {
1753                         p.get_token();
1754                         parse_noweb(p, os, context);
1755                 }
1756
1757                 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1758                         check_space(p, os, context);
1759
1760                 else if (t.character() == '[' && noweb_mode &&
1761                          p.next_token().character() == '[') {
1762                         // These can contain underscores
1763                         p.putback();
1764                         string const s = p.getFullOpt() + ']';
1765                         if (p.next_token().character() == ']')
1766                                 p.get_token();
1767                         else
1768                                 cerr << "Warning: Inserting missing ']' in '"
1769                                      << s << "'." << endl;
1770                         handle_ert(os, s, context);
1771                 }
1772
1773                 else if (t.cat() == catLetter) {
1774                         context.check_layout(os);
1775                         // Workaround for bug 4752.
1776                         // FIXME: This whole code block needs to be removed
1777                         //        when the bug is fixed and tex2lyx produces
1778                         //        the updated file format.
1779                         // The replacement algorithm in LyX is so stupid that
1780                         // it even translates a phrase if it is part of a word.
1781                         bool handled = false;
1782                         for (int const * l = known_phrase_lengths; *l; ++l) {
1783                                 string phrase = t.cs();
1784                                 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1785                                         phrase += p.get_token().cs();
1786                                 if (is_known(phrase, known_coded_phrases)) {
1787                                         handle_ert(os, phrase, context);
1788                                         handled = true;
1789                                         break;
1790                                 } else {
1791                                         for (size_t i = 1; i < phrase.length(); ++i)
1792                                                 p.putback();
1793                                 }
1794                         }
1795                         if (!handled)
1796                                 os << t.cs();
1797                 }
1798
1799                 else if (t.cat() == catOther ||
1800                                t.cat() == catAlign ||
1801                                t.cat() == catParameter) {
1802                         // This translates "&" to "\\&" which may be wrong...
1803                         context.check_layout(os);
1804                         os << t.cs();
1805                 }
1806
1807                 else if (p.isParagraph()) {
1808                         if (context.new_layout_allowed)
1809                                 context.new_paragraph(os);
1810                         else
1811                                 handle_ert(os, "\\par ", context);
1812                         eat_whitespace(p, os, context, true);
1813                 }
1814
1815                 else if (t.cat() == catActive) {
1816                         context.check_layout(os);
1817                         if (t.character() == '~') {
1818                                 if (context.layout->free_spacing)
1819                                         os << ' ';
1820                                 else {
1821                                         begin_inset(os, "space ~\n");
1822                                         end_inset(os);
1823                                 }
1824                         } else
1825                                 os << t.cs();
1826                 }
1827
1828                 else if (t.cat() == catBegin &&
1829                          p.next_token().cat() == catEnd) {
1830                         // {}
1831                         Token const prev = p.prev_token();
1832                         p.get_token();
1833                         if (p.next_token().character() == '`' ||
1834                             (prev.character() == '-' &&
1835                              p.next_token().character() == '-'))
1836                                 ; // ignore it in {}`` or -{}-
1837                         else
1838                                 handle_ert(os, "{}", context);
1839
1840                 }
1841
1842                 else if (t.cat() == catBegin) {
1843                         context.check_layout(os);
1844                         // special handling of font attribute changes
1845                         Token const prev = p.prev_token();
1846                         Token const next = p.next_token();
1847                         TeXFont const oldFont = context.font;
1848                         if (next.character() == '[' ||
1849                             next.character() == ']' ||
1850                             next.character() == '*') {
1851                                 p.get_token();
1852                                 if (p.next_token().cat() == catEnd) {
1853                                         os << next.cs();
1854                                         p.get_token();
1855                                 } else {
1856                                         p.putback();
1857                                         handle_ert(os, "{", context);
1858                                         parse_text_snippet(p, os,
1859                                                         FLAG_BRACE_LAST,
1860                                                         outer, context);
1861                                         handle_ert(os, "}", context);
1862                                 }
1863                         } else if (! context.new_layout_allowed) {
1864                                 handle_ert(os, "{", context);
1865                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1866                                                    outer, context);
1867                                 handle_ert(os, "}", context);
1868                         } else if (is_known(next.cs(), known_sizes)) {
1869                                 // next will change the size, so we must
1870                                 // reset it here
1871                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1872                                                    outer, context);
1873                                 if (!context.atParagraphStart())
1874                                         os << "\n\\size "
1875                                            << context.font.size << "\n";
1876                         } else if (is_known(next.cs(), known_font_families)) {
1877                                 // next will change the font family, so we
1878                                 // must reset it here
1879                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1880                                                    outer, context);
1881                                 if (!context.atParagraphStart())
1882                                         os << "\n\\family "
1883                                            << context.font.family << "\n";
1884                         } else if (is_known(next.cs(), known_font_series)) {
1885                                 // next will change the font series, so we
1886                                 // must reset it here
1887                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1888                                                    outer, context);
1889                                 if (!context.atParagraphStart())
1890                                         os << "\n\\series "
1891                                            << context.font.series << "\n";
1892                         } else if (is_known(next.cs(), known_font_shapes)) {
1893                                 // next will change the font shape, so we
1894                                 // must reset it here
1895                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1896                                                    outer, context);
1897                                 if (!context.atParagraphStart())
1898                                         os << "\n\\shape "
1899                                            << context.font.shape << "\n";
1900                         } else if (is_known(next.cs(), known_old_font_families) ||
1901                                    is_known(next.cs(), known_old_font_series) ||
1902                                    is_known(next.cs(), known_old_font_shapes)) {
1903                                 // next will change the font family, series
1904                                 // and shape, so we must reset it here
1905                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1906                                                    outer, context);
1907                                 if (!context.atParagraphStart())
1908                                         os <<  "\n\\family "
1909                                            << context.font.family
1910                                            << "\n\\series "
1911                                            << context.font.series
1912                                            << "\n\\shape "
1913                                            << context.font.shape << "\n";
1914                         } else {
1915                                 handle_ert(os, "{", context);
1916                                 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1917                                                    outer, context);
1918                                 handle_ert(os, "}", context);
1919                         }
1920                 }
1921
1922                 else if (t.cat() == catEnd) {
1923                         if (flags & FLAG_BRACE_LAST) {
1924                                 return;
1925                         }
1926                         cerr << "stray '}' in text\n";
1927                         handle_ert(os, "}", context);
1928                 }
1929
1930                 else if (t.cat() == catComment)
1931                         parse_comment(p, os, t, context);
1932
1933                 //
1934                 // control sequences
1935                 //
1936
1937                 else if (t.cs() == "(") {
1938                         context.check_layout(os);
1939                         begin_inset(os, "Formula");
1940                         os << " \\(";
1941                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1942                         os << "\\)";
1943                         end_inset(os);
1944                 }
1945
1946                 else if (t.cs() == "[") {
1947                         context.check_layout(os);
1948                         begin_inset(os, "Formula");
1949                         os << " \\[";
1950                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1951                         os << "\\]";
1952                         end_inset(os);
1953                 }
1954
1955                 else if (t.cs() == "begin")
1956                         parse_environment(p, os, outer, last_env, context);
1957
1958                 else if (t.cs() == "end") {
1959                         if (flags & FLAG_END) {
1960                                 // eat environment name
1961                                 string const name = p.getArg('{', '}');
1962                                 if (name != active_environment())
1963                                         cerr << "\\end{" + name + "} does not match \\begin{"
1964                                                 + active_environment() + "}\n";
1965                                 return;
1966                         }
1967                         p.error("found 'end' unexpectedly");
1968                 }
1969
1970                 else if (t.cs() == "item") {
1971                         p.skip_spaces();
1972                         string s;
1973                         bool optarg = false;
1974                         if (p.next_token().cat() != catEscape &&
1975                             p.next_token().character() == '[') {
1976                                 p.get_token(); // eat '['
1977                                 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1978                                                        outer, context);
1979                                 optarg = true;
1980                         }
1981                         context.set_item();
1982                         context.check_layout(os);
1983                         if (context.has_item) {
1984                                 // An item in an unknown list-like environment
1985                                 // FIXME: Do this in check_layout()!
1986                                 context.has_item = false;
1987                                 if (optarg)
1988                                         handle_ert(os, "\\item", context);
1989                                 else
1990                                         handle_ert(os, "\\item ", context);
1991                         }
1992                         if (optarg) {
1993                                 if (context.layout->labeltype != LABEL_MANUAL) {
1994                                         // LyX does not support \item[\mybullet]
1995                                         // in itemize environments
1996                                         handle_ert(os, "[", context);
1997                                         os << s;
1998                                         handle_ert(os, "]", context);
1999                                 } else if (!s.empty()) {
2000                                         // The space is needed to separate the
2001                                         // item from the rest of the sentence.
2002                                         os << s << ' ';
2003                                         eat_whitespace(p, os, context, false);
2004                                 }
2005                         }
2006                 }
2007
2008                 else if (t.cs() == "bibitem") {
2009                         context.set_item();
2010                         context.check_layout(os);
2011                         string label = convert_command_inset_arg(p.getArg('[', ']'));
2012                         string key = convert_command_inset_arg(p.verbatim_item());
2013                         if (contains(label, '\\') || contains(key, '\\')) {
2014                                 // LyX can't handle LaTeX commands in labels or keys
2015                                 handle_ert(os, t.asInput() + '[' + label +
2016                                                "]{" + p.verbatim_item() + '}',
2017                                            context);
2018                         } else {
2019                                 begin_command_inset(os, "bibitem", "bibitem");
2020                                 os << "label \"" << label << "\"\n"
2021                                       "key \"" << key << "\"\n";
2022                                 end_inset(os);
2023                         }
2024                 }
2025
2026                 else if (is_macro(p))
2027                         parse_macro(p, os, context);
2028
2029                 else if (t.cs() == "noindent") {
2030                         p.skip_spaces();
2031                         context.add_par_extra_stuff("\\noindent\n");
2032                 }
2033
2034                 else if (t.cs() == "appendix") {
2035                         context.add_par_extra_stuff("\\start_of_appendix\n");
2036                         // We need to start a new paragraph. Otherwise the
2037                         // appendix in 'bla\appendix\chapter{' would start
2038                         // too late.
2039                         context.new_paragraph(os);
2040                         // We need to make sure that the paragraph is
2041                         // generated even if it is empty. Otherwise the
2042                         // appendix in '\par\appendix\par\chapter{' would
2043                         // start too late.
2044                         context.check_layout(os);
2045                         // FIXME: This is a hack to prevent paragraph
2046                         // deletion if it is empty. Handle this better!
2047                         handle_comment(os,
2048                                 "%dummy comment inserted by tex2lyx to "
2049                                 "ensure that this paragraph is not empty",
2050                                 context);
2051                         // Both measures above may generate an additional
2052                         // empty paragraph, but that does not hurt, because
2053                         // whitespace does not matter here.
2054                         eat_whitespace(p, os, context, true);
2055                 }
2056
2057                 // Starred section headings
2058                 // Must attempt to parse "Section*" before "Section".
2059                 else if ((p.next_token().asInput() == "*") &&
2060                          context.new_layout_allowed &&
2061                          (newlayout = findLayout(context.textclass, t.cs() + '*', true))) {
2062                         // write the layout
2063                         p.get_token();
2064                         output_command_layout(os, p, outer, context, newlayout);
2065                         p.skip_spaces();
2066                 }
2067
2068                 // Section headings and the like
2069                 else if (context.new_layout_allowed &&
2070                          (newlayout = findLayout(context.textclass, t.cs(), true))) {
2071                         // write the layout
2072                         output_command_layout(os, p, outer, context, newlayout);
2073                         p.skip_spaces();
2074                 }
2075
2076                 else if (t.cs() == "caption") {
2077                         p.skip_spaces();
2078                         context.check_layout(os);
2079                         p.skip_spaces();
2080                         begin_inset(os, "Caption\n\n");
2081                         Context newcontext(true, context.textclass);
2082                         newcontext.font = context.font;
2083                         newcontext.check_layout(os);
2084                         if (p.next_token().cat() != catEscape &&
2085                             p.next_token().character() == '[') {
2086                                 p.get_token(); // eat '['
2087                                 begin_inset(os, "Argument\n");
2088                                 os << "status collapsed\n";
2089                                 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
2090                                 end_inset(os);
2091                                 eat_whitespace(p, os, context, false);
2092                         }
2093                         parse_text(p, os, FLAG_ITEM, outer, context);
2094                         context.check_end_layout(os);
2095                         // We don't need really a new paragraph, but
2096                         // we must make sure that the next item gets a \begin_layout.
2097                         context.new_paragraph(os);
2098                         end_inset(os);
2099                         p.skip_spaces();
2100                         newcontext.check_end_layout(os);
2101                 }
2102
2103                 else if (t.cs() == "includegraphics") {
2104                         bool const clip = p.next_token().asInput() == "*";
2105                         if (clip)
2106                                 p.get_token();
2107                         string const arg = p.getArg('[', ']');
2108                         map<string, string> opts;
2109                         vector<string> keys;
2110                         split_map(arg, opts, keys);
2111                         if (clip)
2112                                 opts["clip"] = string();
2113                         string name = normalize_filename(p.verbatim_item());
2114
2115                         string const path = getMasterFilePath();
2116                         // We want to preserve relative / absolute filenames,
2117                         // therefore path is only used for testing
2118                         if (!makeAbsPath(name, path).exists()) {
2119                                 // The file extension is probably missing.
2120                                 // Now try to find it out.
2121                                 string const dvips_name =
2122                                         find_file(name, path,
2123                                                   known_dvips_graphics_formats);
2124                                 string const pdftex_name =
2125                                         find_file(name, path,
2126                                                   known_pdftex_graphics_formats);
2127                                 if (!dvips_name.empty()) {
2128                                         if (!pdftex_name.empty()) {
2129                                                 cerr << "This file contains the "
2130                                                         "latex snippet\n"
2131                                                         "\"\\includegraphics{"
2132                                                      << name << "}\".\n"
2133                                                         "However, files\n\""
2134                                                      << dvips_name << "\" and\n\""
2135                                                      << pdftex_name << "\"\n"
2136                                                         "both exist, so I had to make a "
2137                                                         "choice and took the first one.\n"
2138                                                         "Please move the unwanted one "
2139                                                         "someplace else and try again\n"
2140                                                         "if my choice was wrong."
2141                                                      << endl;
2142                                         }
2143                                         name = dvips_name;
2144                                 } else if (!pdftex_name.empty()) {
2145                                         name = pdftex_name;
2146                                         pdflatex = true;
2147                                 }
2148                         }
2149
2150                         if (makeAbsPath(name, path).exists())
2151                                 fix_relative_filename(name);
2152                         else
2153                                 cerr << "Warning: Could not find graphics file '"
2154                                      << name << "'." << endl;
2155
2156                         context.check_layout(os);
2157                         begin_inset(os, "Graphics ");
2158                         os << "\n\tfilename " << name << '\n';
2159                         if (opts.find("width") != opts.end())
2160                                 os << "\twidth "
2161                                    << translate_len(opts["width"]) << '\n';
2162                         if (opts.find("height") != opts.end())
2163                                 os << "\theight "
2164                                    << translate_len(opts["height"]) << '\n';
2165                         if (opts.find("scale") != opts.end()) {
2166                                 istringstream iss(opts["scale"]);
2167                                 double val;
2168                                 iss >> val;
2169                                 val = val*100;
2170                                 os << "\tscale " << val << '\n';
2171                         }
2172                         if (opts.find("angle") != opts.end()) {
2173                                 os << "\trotateAngle "
2174                                    << opts["angle"] << '\n';
2175                                 vector<string>::const_iterator a =
2176                                         find(keys.begin(), keys.end(), "angle");
2177                                 vector<string>::const_iterator s =
2178                                         find(keys.begin(), keys.end(), "width");
2179                                 if (s == keys.end())
2180                                         s = find(keys.begin(), keys.end(), "height");
2181                                 if (s == keys.end())
2182                                         s = find(keys.begin(), keys.end(), "scale");
2183                                 if (s != keys.end() && distance(s, a) > 0)
2184                                         os << "\tscaleBeforeRotation\n";
2185                         }
2186                         if (opts.find("origin") != opts.end()) {
2187                                 ostringstream ss;
2188                                 string const opt = opts["origin"];
2189                                 if (opt.find('l') != string::npos) ss << "left";
2190                                 if (opt.find('r') != string::npos) ss << "right";
2191                                 if (opt.find('c') != string::npos) ss << "center";
2192                                 if (opt.find('t') != string::npos) ss << "Top";
2193                                 if (opt.find('b') != string::npos) ss << "Bottom";
2194                                 if (opt.find('B') != string::npos) ss << "Baseline";
2195                                 if (!ss.str().empty())
2196                                         os << "\trotateOrigin " << ss.str() << '\n';
2197                                 else
2198                                         cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
2199                         }
2200                         if (opts.find("keepaspectratio") != opts.end())
2201                                 os << "\tkeepAspectRatio\n";
2202                         if (opts.find("clip") != opts.end())
2203                                 os << "\tclip\n";
2204                         if (opts.find("draft") != opts.end())
2205                                 os << "\tdraft\n";
2206                         if (opts.find("bb") != opts.end())
2207                                 os << "\tBoundingBox "
2208                                    << opts["bb"] << '\n';
2209                         int numberOfbbOptions = 0;
2210                         if (opts.find("bbllx") != opts.end())
2211                                 numberOfbbOptions++;
2212                         if (opts.find("bblly") != opts.end())
2213                                 numberOfbbOptions++;
2214                         if (opts.find("bburx") != opts.end())
2215                                 numberOfbbOptions++;
2216                         if (opts.find("bbury") != opts.end())
2217                                 numberOfbbOptions++;
2218                         if (numberOfbbOptions == 4)
2219                                 os << "\tBoundingBox "
2220                                    << opts["bbllx"] << " " << opts["bblly"] << " "
2221                                    << opts["bburx"] << " " << opts["bbury"] << '\n';
2222                         else if (numberOfbbOptions > 0)
2223                                 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2224                         numberOfbbOptions = 0;
2225                         if (opts.find("natwidth") != opts.end())
2226                                 numberOfbbOptions++;
2227                         if (opts.find("natheight") != opts.end())
2228                                 numberOfbbOptions++;
2229                         if (numberOfbbOptions == 2)
2230                                 os << "\tBoundingBox 0bp 0bp "
2231                                    << opts["natwidth"] << " " << opts["natheight"] << '\n';
2232                         else if (numberOfbbOptions > 0)
2233                                 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
2234                         ostringstream special;
2235                         if (opts.find("hiresbb") != opts.end())
2236                                 special << "hiresbb,";
2237                         if (opts.find("trim") != opts.end())
2238                                 special << "trim,";
2239                         if (opts.find("viewport") != opts.end())
2240                                 special << "viewport=" << opts["viewport"] << ',';
2241                         if (opts.find("totalheight") != opts.end())
2242                                 special << "totalheight=" << opts["totalheight"] << ',';
2243                         if (opts.find("type") != opts.end())
2244                                 special << "type=" << opts["type"] << ',';
2245                         if (opts.find("ext") != opts.end())
2246                                 special << "ext=" << opts["ext"] << ',';
2247                         if (opts.find("read") != opts.end())
2248                                 special << "read=" << opts["read"] << ',';
2249                         if (opts.find("command") != opts.end())
2250                                 special << "command=" << opts["command"] << ',';
2251                         string s_special = special.str();
2252                         if (!s_special.empty()) {
2253                                 // We had special arguments. Remove the trailing ','.
2254                                 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
2255                         }
2256                         // TODO: Handle the unknown settings better.
2257                         // Warn about invalid options.
2258                         // Check whether some option was given twice.
2259                         end_inset(os);
2260                 }
2261
2262                 else if (t.cs() == "footnote" ||
2263                          (t.cs() == "thanks" && context.layout->intitle)) {
2264                         p.skip_spaces();
2265                         context.check_layout(os);
2266                         begin_inset(os, "Foot\n");
2267                         os << "status collapsed\n\n";
2268                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2269                         end_inset(os);
2270                 }
2271
2272                 else if (t.cs() == "marginpar") {
2273                         p.skip_spaces();
2274                         context.check_layout(os);
2275                         begin_inset(os, "Marginal\n");
2276                         os << "status collapsed\n\n";
2277                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2278                         end_inset(os);
2279                 }
2280
2281                 else if (t.cs() == "ensuremath") {
2282                         p.skip_spaces();
2283                         context.check_layout(os);
2284                         string const s = p.verbatim_item();
2285                         //FIXME: this never triggers in UTF8
2286                         if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
2287                                 os << s;
2288                         else
2289                                 handle_ert(os, "\\ensuremath{" + s + "}",
2290                                            context);
2291                 }
2292
2293                 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
2294                         // FIXME: Somehow prevent title layouts if
2295                         // "maketitle" was not found
2296                         // swallow this
2297                         skip_spaces_braces(p);
2298                 }
2299
2300                 else if (t.cs() == "tableofcontents") {
2301                         context.check_layout(os);
2302                         begin_command_inset(os, "toc", "tableofcontents");
2303                         end_inset(os);
2304                         skip_spaces_braces(p);
2305                 }
2306
2307                 else if (t.cs() == "listoffigures") {
2308                         context.check_layout(os);
2309                         begin_inset(os, "FloatList figure\n");
2310                         end_inset(os);
2311                         skip_spaces_braces(p);
2312                 }
2313
2314                 else if (t.cs() == "listoftables") {
2315                         context.check_layout(os);
2316                         begin_inset(os, "FloatList table\n");
2317                         end_inset(os);
2318                         skip_spaces_braces(p);
2319                 }
2320
2321                 else if (t.cs() == "listof") {
2322                         p.skip_spaces(true);
2323                         string const name = p.get_token().cs();
2324                         if (context.textclass.floats().typeExist(name)) {
2325                                 context.check_layout(os);
2326                                 begin_inset(os, "FloatList ");
2327                                 os << name << "\n";
2328                                 end_inset(os);
2329                                 p.get_token(); // swallow second arg
2330                         } else
2331                                 handle_ert(os, "\\listof{" + name + "}", context);
2332                 }
2333
2334                 else if (t.cs() == "textrm")
2335                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2336                                               context, "\\family",
2337                                               context.font.family, "roman");
2338
2339                 else if (t.cs() == "textsf")
2340                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2341                                               context, "\\family",
2342                                               context.font.family, "sans");
2343
2344                 else if (t.cs() == "texttt")
2345                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2346                                               context, "\\family",
2347                                               context.font.family, "typewriter");
2348
2349                 else if (t.cs() == "textmd")
2350                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2351                                               context, "\\series",
2352                                               context.font.series, "medium");
2353
2354                 else if (t.cs() == "textbf")
2355                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2356                                               context, "\\series",
2357                                               context.font.series, "bold");
2358
2359                 else if (t.cs() == "textup")
2360                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2361                                               context, "\\shape",
2362                                               context.font.shape, "up");
2363
2364                 else if (t.cs() == "textit")
2365                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2366                                               context, "\\shape",
2367                                               context.font.shape, "italic");
2368
2369                 else if (t.cs() == "textsl")
2370                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2371                                               context, "\\shape",
2372                                               context.font.shape, "slanted");
2373
2374                 else if (t.cs() == "textsc")
2375                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2376                                               context, "\\shape",
2377                                               context.font.shape, "smallcaps");
2378
2379                 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2380                         context.check_layout(os);
2381                         TeXFont oldFont = context.font;
2382                         context.font.init();
2383                         context.font.size = oldFont.size;
2384                         os << "\n\\family " << context.font.family << "\n";
2385                         os << "\n\\series " << context.font.series << "\n";
2386                         os << "\n\\shape " << context.font.shape << "\n";
2387                         if (t.cs() == "textnormal") {
2388                                 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2389                                 output_font_change(os, context.font, oldFont);
2390                                 context.font = oldFont;
2391                         } else
2392                                 eat_whitespace(p, os, context, false);
2393                 }
2394
2395                 else if (t.cs() == "textcolor") {
2396                         // scheme is \textcolor{color name}{text}
2397                         string const color = p.verbatim_item();
2398                         // we only support the predefined colors of the color package
2399                         if (color == "black" || color == "blue" || color == "cyan"
2400                                 || color == "green" || color == "magenta" || color == "red"
2401                                 || color == "white" || color == "yellow") {
2402                                         context.check_layout(os);
2403                                         os << "\n\\color " << color << "\n";
2404                                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2405                                         context.check_layout(os);
2406                                         os << "\n\\color inherit\n";
2407                         } else
2408                                 // for custom defined colors
2409                                 handle_ert(os, t.asInput() + "{" + color + "}", context);
2410                 }
2411
2412                 else if (t.cs() == "underbar" || t.cs() == "uline") {
2413                         // \underbar is not 100% correct (LyX outputs \uline
2414                         // of ulem.sty). The difference is that \ulem allows
2415                         // line breaks, and \underbar does not.
2416                         // Do NOT handle \underline.
2417                         // \underbar cuts through y, g, q, p etc.,
2418                         // \underline does not.
2419                         context.check_layout(os);
2420                         os << "\n\\bar under\n";
2421                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2422                         context.check_layout(os);
2423                         os << "\n\\bar default\n";
2424                 }
2425
2426                 else if (t.cs() == "emph" || t.cs() == "noun") {
2427                         context.check_layout(os);
2428                         os << "\n\\" << t.cs() << " on\n";
2429                         parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2430                         context.check_layout(os);
2431                         os << "\n\\" << t.cs() << " default\n";
2432                 }
2433
2434                 else if (t.cs() == "lyxline") {
2435                         // swallow size argument (it is not used anyway)
2436                         p.getArg('{', '}');
2437                         if (!context.atParagraphStart()) {
2438                                 // so our line is in the middle of a paragraph
2439                                 // we need to add a new line, lest this line
2440                                 // follow the other content on that line and
2441                                 // run off the side of the page
2442                                 // FIXME: This may create an empty paragraph,
2443                                 //        but without that it would not be
2444                                 //        possible to set noindent below.
2445                                 //        Fortunately LaTeX does not care
2446                                 //        about the empty paragraph.
2447                                 context.new_paragraph(os);
2448                         }
2449                         if (h_paragraph_separation == "indent") {
2450                                 // we need to unindent, lest the line be too long
2451                                 context.add_par_extra_stuff("\\noindent\n");
2452                         }
2453                         context.check_layout(os);
2454                         begin_command_inset(os, "line", "rule");
2455                         os << "offset \"0.5ex\"\n"
2456                               "width \"100line%\"\n"
2457                               "height \"1pt\"\n";
2458                         end_inset(os);
2459                 }
2460
2461                 else if (is_known(t.cs(), known_phrases) ||
2462                          (t.cs() == "protect" &&
2463                           p.next_token().cat() == catEscape &&
2464                           is_known(p.next_token().cs(), known_phrases))) {
2465                         // LyX sometimes puts a \protect in front, so we have to ignore it
2466                         // FIXME: This needs to be changed when bug 4752 is fixed.
2467                         char const * const * where = is_known(
2468                                 t.cs() == "protect" ? p.get_token().cs() : t.cs(),
2469                                 known_phrases);
2470                         context.check_layout(os);
2471                         os << known_coded_phrases[where - known_phrases];
2472                         skip_spaces_braces(p);
2473                 }
2474
2475                 else if (is_known(t.cs(), known_ref_commands)) {
2476                         string const opt = p.getOpt();
2477                         if (opt.empty()) {
2478                                 context.check_layout(os);
2479                                 char const * const * where = is_known(t.cs(),
2480                                         known_ref_commands);
2481                                 begin_command_inset(os, "ref",
2482                                         known_coded_ref_commands[where - known_ref_commands]);
2483                                 os << "reference \""
2484                                    << convert_command_inset_arg(p.verbatim_item())
2485                                    << "\"\n";
2486                                 end_inset(os);
2487                         } else {
2488                                 // LyX does not support optional arguments of ref commands
2489                                 handle_ert(os, t.asInput() + '[' + opt + "]{" +
2490                                                p.verbatim_item() + "}", context);
2491                         }
2492                 }
2493
2494                 else if (use_natbib &&
2495                          is_known(t.cs(), known_natbib_commands) &&
2496                          ((t.cs() != "citefullauthor" &&
2497                            t.cs() != "citeyear" &&
2498                            t.cs() != "citeyearpar") ||
2499                           p.next_token().asInput() != "*")) {
2500                         context.check_layout(os);
2501                         string command = t.cs();
2502                         if (p.next_token().asInput() == "*") {
2503                                 command += '*';
2504                                 p.get_token();
2505                         }
2506                         if (command == "citefullauthor")
2507                                 // alternative name for "\\citeauthor*"
2508                                 command = "citeauthor*";
2509
2510                         // text before the citation
2511                         string before;
2512                         // text after the citation
2513                         string after;
2514                         get_cite_arguments(p, true, before, after);
2515
2516                         if (command == "cite") {
2517                                 // \cite without optional argument means
2518                                 // \citet, \cite with at least one optional
2519                                 // argument means \citep.
2520                                 if (before.empty() && after.empty())
2521                                         command = "citet";
2522                                 else
2523                                         command = "citep";
2524                         }
2525                         if (before.empty() && after == "[]")
2526                                 // avoid \citet[]{a}
2527                                 after.erase();
2528                         else if (before == "[]" && after == "[]") {
2529                                 // avoid \citet[][]{a}
2530                                 before.erase();
2531                                 after.erase();
2532                         }
2533                         // remove the brackets around after and before
2534                         if (!after.empty()) {
2535                                 after.erase(0, 1);
2536                                 after.erase(after.length() - 1, 1);
2537                                 after = convert_command_inset_arg(after);
2538                         }
2539                         if (!before.empty()) {
2540                                 before.erase(0, 1);
2541                                 before.erase(before.length() - 1, 1);
2542                                 before = convert_command_inset_arg(before);
2543                         }
2544                         begin_command_inset(os, "citation", command);
2545                         os << "after " << '"' << after << '"' << "\n";
2546                         os << "before " << '"' << before << '"' << "\n";
2547                         os << "key \""
2548                            << convert_command_inset_arg(p.verbatim_item())
2549                            << "\"\n";
2550                         end_inset(os);
2551                 }
2552
2553                 else if (use_jurabib &&
2554                          is_known(t.cs(), known_jurabib_commands) &&
2555                          (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2556                         context.check_layout(os);
2557                         string command = t.cs();
2558                         if (p.next_token().asInput() == "*") {
2559                                 command += '*';
2560                                 p.get_token();
2561                         }
2562                         char argumentOrder = '\0';
2563                         vector<string> const & options = used_packages["jurabib"];
2564                         if (find(options.begin(), options.end(),
2565                                       "natbiborder") != options.end())
2566                                 argumentOrder = 'n';
2567                         else if (find(options.begin(), options.end(),
2568                                            "jurabiborder") != options.end())
2569                                 argumentOrder = 'j';
2570
2571                         // text before the citation
2572                         string before;
2573                         // text after the citation
2574                         string after;
2575                         get_cite_arguments(p, argumentOrder != 'j', before, after);
2576
2577                         string const citation = p.verbatim_item();
2578                         if (!before.empty() && argumentOrder == '\0') {
2579                                 cerr << "Warning: Assuming argument order "
2580                                         "of jurabib version 0.6 for\n'"
2581                                      << command << before << after << '{'
2582                                      << citation << "}'.\n"
2583                                         "Add 'jurabiborder' to the jurabib "
2584                                         "package options if you used an\n"
2585                                         "earlier jurabib version." << endl;
2586                         }
2587                         if (!after.empty()) {
2588                                 after.erase(0, 1);
2589                                 after.erase(after.length() - 1, 1);
2590                         }
2591                         if (!before.empty()) {
2592                                 before.erase(0, 1);
2593                                 before.erase(before.length() - 1, 1);
2594                         }
2595                         begin_command_inset(os, "citation", command);
2596                         os << "after " << '"' << after << '"' << "\n";
2597                         os << "before " << '"' << before << '"' << "\n";
2598                         os << "key " << '"' << citation << '"' << "\n";
2599                         end_inset(os);
2600                 }
2601
2602                 else if (t.cs() == "cite"
2603                         || t.cs() == "nocite") {
2604                         context.check_layout(os);
2605                         string after = convert_command_inset_arg(p.getArg('[', ']'));
2606                         string key = convert_command_inset_arg(p.verbatim_item());
2607                         // store the case that it is "\nocite{*}" to use it later for
2608                         // the BibTeX inset
2609                         if (key != "*") {
2610                                 begin_command_inset(os, "citation", t.cs());
2611                                 os << "after " << '"' << after << '"' << "\n";
2612                                 os << "key " << '"' << key << '"' << "\n";
2613                                 end_inset(os);
2614                         } else if (t.cs() == "nocite")
2615                                 btprint = key;
2616                 }
2617
2618                 else if (t.cs() == "index") {
2619                         context.check_layout(os);
2620                         begin_inset(os, "Index idx\n");
2621                         os << "status collapsed\n";
2622                         parse_text_in_inset(p, os, FLAG_ITEM, false, context, "Index");
2623                         end_inset(os);
2624                 }
2625
2626                 else if (t.cs() == "nomenclature") {
2627                         context.check_layout(os);
2628                         begin_command_inset(os, "nomenclature", "nomenclature");
2629                         string prefix = convert_command_inset_arg(p.getArg('[', ']'));
2630                         if (!prefix.empty())
2631                                 os << "prefix " << '"' << prefix << '"' << "\n";
2632                         os << "symbol " << '"'
2633                            << convert_command_inset_arg(p.verbatim_item());
2634                         os << "\"\ndescription \""
2635                            << convert_command_inset_arg(p.verbatim_item())
2636                            << "\"\n";
2637                         end_inset(os);
2638                 }
2639                 
2640                 else if (t.cs() == "label") {
2641                         context.check_layout(os);
2642                         begin_command_inset(os, "label", "label");
2643                         os << "name \""
2644                            << convert_command_inset_arg(p.verbatim_item())
2645                            << "\"\n";
2646                         end_inset(os);
2647                 }
2648
2649                 else if (t.cs() == "printindex") {
2650                         context.check_layout(os);
2651                         begin_command_inset(os, "index_print", "printindex");
2652                         os << "type \"idx\"\n";
2653                         end_inset(os);
2654                         skip_spaces_braces(p);
2655                 }
2656
2657                 else if (t.cs() == "printnomenclature") {
2658                         context.check_layout(os);
2659                         begin_command_inset(os, "nomencl_print", "printnomenclature");
2660                         end_inset(os);
2661                         skip_spaces_braces(p);
2662                 }
2663
2664                 else if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2665                         context.check_layout(os);
2666                         begin_inset(os, "script ");
2667                         os << t.cs().substr(4) << '\n';
2668                         parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2669                         end_inset(os);
2670                 }
2671
2672                 else if (is_known(t.cs(), known_quotes)) {
2673                         char const * const * where = is_known(t.cs(), known_quotes);
2674                         context.check_layout(os);
2675                         begin_inset(os, "Quotes ");
2676                         os << known_coded_quotes[where - known_quotes];
2677                         end_inset(os);
2678                         // LyX adds {} after the quote, so we have to eat
2679                         // spaces here if there are any before a possible
2680                         // {} pair.
2681                         eat_whitespace(p, os, context, false);
2682                         skip_braces(p);
2683                 }
2684
2685                 else if (is_known(t.cs(), known_sizes) &&
2686                          context.new_layout_allowed) {
2687                         char const * const * where = is_known(t.cs(), known_sizes);
2688                         context.check_layout(os);
2689                         TeXFont const oldFont = context.font;
2690                         context.font.size = known_coded_sizes[where - known_sizes];
2691                         output_font_change(os, oldFont, context.font);
2692                         eat_whitespace(p, os, context, false);
2693                 }
2694
2695                 else if (is_known(t.cs(), known_font_families) &&
2696                          context.new_layout_allowed) {
2697                         char const * const * where =
2698                                 is_known(t.cs(), known_font_families);
2699                         context.check_layout(os);
2700                         TeXFont const oldFont = context.font;
2701                         context.font.family =
2702                                 known_coded_font_families[where - known_font_families];
2703                         output_font_change(os, oldFont, context.font);
2704                         eat_whitespace(p, os, context, false);
2705                 }
2706
2707                 else if (is_known(t.cs(), known_font_series) &&
2708                          context.new_layout_allowed) {
2709                         char const * const * where =
2710                                 is_known(t.cs(), known_font_series);
2711                         context.check_layout(os);
2712                         TeXFont const oldFont = context.font;
2713                         context.font.series =
2714                                 known_coded_font_series[where - known_font_series];
2715                         output_font_change(os, oldFont, context.font);
2716                         eat_whitespace(p, os, context, false);
2717                 }
2718
2719                 else if (is_known(t.cs(), known_font_shapes) &&
2720                          context.new_layout_allowed) {
2721                         char const * const * where =
2722                                 is_known(t.cs(), known_font_shapes);
2723                         context.check_layout(os);
2724                         TeXFont const oldFont = context.font;
2725                         context.font.shape =
2726                                 known_coded_font_shapes[where - known_font_shapes];
2727                         output_font_change(os, oldFont, context.font);
2728                         eat_whitespace(p, os, context, false);
2729                 }
2730                 else if (is_known(t.cs(), known_old_font_families) &&
2731                          context.new_layout_allowed) {
2732                         char const * const * where =
2733                                 is_known(t.cs(), known_old_font_families);
2734                         context.check_layout(os);
2735                         TeXFont const oldFont = context.font;
2736                         context.font.init();
2737                         context.font.size = oldFont.size;
2738                         context.font.family =
2739                                 known_coded_font_families[where - known_old_font_families];
2740                         output_font_change(os, oldFont, context.font);
2741                         eat_whitespace(p, os, context, false);
2742                 }
2743
2744                 else if (is_known(t.cs(), known_old_font_series) &&
2745                          context.new_layout_allowed) {
2746                         char const * const * where =
2747                                 is_known(t.cs(), known_old_font_series);
2748                         context.check_layout(os);
2749                         TeXFont const oldFont = context.font;
2750                         context.font.init();
2751                         context.font.size = oldFont.size;
2752                         context.font.series =
2753                                 known_coded_font_series[where - known_old_font_series];
2754                         output_font_change(os, oldFont, context.font);
2755                         eat_whitespace(p, os, context, false);
2756                 }
2757
2758                 else if (is_known(t.cs(), known_old_font_shapes) &&
2759                          context.new_layout_allowed) {
2760                         char const * const * where =
2761                                 is_known(t.cs(), known_old_font_shapes);
2762                         context.check_layout(os);
2763                         TeXFont const oldFont = context.font;
2764                         context.font.init();
2765                         context.font.size = oldFont.size;
2766                         context.font.shape =
2767                                 known_coded_font_shapes[where - known_old_font_shapes];
2768                         output_font_change(os, oldFont, context.font);
2769                         eat_whitespace(p, os, context, false);
2770                 }
2771
2772                 else if (t.cs() == "selectlanguage") {
2773                         context.check_layout(os);
2774                         // save the language for the case that a
2775                         // \foreignlanguage is used 
2776
2777                         context.font.language = babel2lyx(p.verbatim_item());
2778                         os << "\n\\lang " << context.font.language << "\n";
2779                 }
2780
2781                 else if (t.cs() == "foreignlanguage") {
2782                         string const lang = babel2lyx(p.verbatim_item());
2783                         parse_text_attributes(p, os, FLAG_ITEM, outer,
2784                                               context, "\\lang",
2785                                               context.font.language, lang);
2786                 }
2787
2788                 else if (t.cs() == "inputencoding") {
2789                         // nothing to write here
2790                         string const enc = subst(p.verbatim_item(), "\n", " ");
2791                         p.setEncoding(enc);
2792                 }
2793
2794                 else if (t.cs() == "ldots") {
2795                         context.check_layout(os);
2796                         os << "\\SpecialChar \\ldots{}\n";
2797                         skip_spaces_braces(p);
2798                 }
2799
2800                 else if (t.cs() == "lyxarrow") {
2801                         context.check_layout(os);
2802                         os << "\\SpecialChar \\menuseparator\n";
2803                         skip_spaces_braces(p);
2804                 }
2805
2806                 else if (t.cs() == "textcompwordmark") {
2807                         context.check_layout(os);
2808                         os << "\\SpecialChar \\textcompwordmark{}\n";
2809                         skip_spaces_braces(p);
2810                 }
2811
2812                 else if (t.cs() == "slash") {
2813                         context.check_layout(os);
2814                         os << "\\SpecialChar \\slash{}\n";
2815                         skip_spaces_braces(p);
2816                 }
2817
2818                 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2819                         context.check_layout(os);
2820                         os << "\\SpecialChar \\nobreakdash-\n";
2821                         p.get_token();
2822                 }
2823
2824                 else if (t.cs() == "textquotedbl") {
2825                         context.check_layout(os);
2826                         os << "\"";
2827                         skip_braces(p);
2828                 }
2829
2830                 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2831                         context.check_layout(os);
2832                         os << "\\SpecialChar \\@.\n";
2833                         p.get_token();
2834                 }
2835
2836                 else if (t.cs() == "-") {
2837                         context.check_layout(os);
2838                         os << "\\SpecialChar \\-\n";
2839                 }
2840
2841                 else if (t.cs() == "textasciitilde") {
2842                         context.check_layout(os);
2843                         os << '~';
2844                         skip_spaces_braces(p);
2845                 }
2846
2847                 else if (t.cs() == "textasciicircum") {
2848                         context.check_layout(os);
2849                         os << '^';
2850                         skip_spaces_braces(p);
2851                 }
2852
2853                 else if (t.cs() == "textbackslash") {
2854                         context.check_layout(os);
2855                         os << "\n\\backslash\n";
2856                         skip_spaces_braces(p);
2857                 }
2858
2859                 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2860                             || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2861                             || t.cs() == "%") {
2862                         context.check_layout(os);
2863                         os << t.cs();
2864                 }
2865
2866                 else if (t.cs() == "char") {
2867                         context.check_layout(os);
2868                         if (p.next_token().character() == '`') {
2869                                 p.get_token();
2870                                 if (p.next_token().cs() == "\"") {
2871                                         p.get_token();
2872                                         os << '"';
2873                                         skip_braces(p);
2874                                 } else {
2875                                         handle_ert(os, "\\char`", context);
2876                                 }
2877                         } else {
2878                                 handle_ert(os, "\\char", context);
2879                         }
2880                 }
2881
2882                 else if (t.cs() == "verb") {
2883                         context.check_layout(os);
2884                         char const delimiter = p.next_token().character();
2885                         string const arg = p.getArg(delimiter, delimiter);
2886                         ostringstream oss;
2887                         oss << "\\verb" << delimiter << arg << delimiter;
2888                         handle_ert(os, oss.str(), context);
2889                 }
2890
2891                 // Problem: \= creates a tabstop inside the tabbing environment
2892                 // and else an accent. In the latter case we really would want
2893                 // \={o} instead of \= o.
2894                 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2895                         handle_ert(os, t.asInput(), context);
2896
2897                 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2898                 else if (t.cs().size() == 1 
2899                          && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2900                         context.check_layout(os);
2901                         // try to see whether the string is in unicodesymbols
2902                         docstring rem;
2903                         string command = t.asInput() + "{" 
2904                                 + trimSpaceAndEol(p.verbatim_item())
2905                                 + "}";
2906                         docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2907                         if (!s.empty()) {
2908                                 if (!rem.empty())
2909                                         cerr << "When parsing " << command 
2910                                              << ", result is " << to_utf8(s)
2911                                              << "+" << to_utf8(rem) << endl;
2912                                 os << to_utf8(s);
2913                         } else
2914                                 // we did not find a non-ert version
2915                                 handle_ert(os, command, context);
2916                 }
2917
2918                 else if (t.cs() == "\\") {
2919                         context.check_layout(os);
2920                         if (p.hasOpt())
2921                                 handle_ert(os, "\\\\" + p.getOpt(), context);
2922                         else if (p.next_token().asInput() == "*") {
2923                                 p.get_token();
2924                                 // getOpt() eats the following space if there
2925                                 // is no optional argument, but that is OK
2926                                 // here since it has no effect in the output.
2927                                 handle_ert(os, "\\\\*" + p.getOpt(), context);
2928                         }
2929                         else {
2930                                 begin_inset(os, "Newline newline");
2931                                 end_inset(os);
2932                         }
2933                 }
2934
2935                 else if (t.cs() == "newline" ||
2936                          (t.cs() == "linebreak" && !p.hasOpt())) {
2937                         context.check_layout(os);
2938                         begin_inset(os, "Newline ");
2939                         os << t.cs();
2940                         end_inset(os);
2941                         skip_spaces_braces(p);
2942                 }
2943
2944                 else if (t.cs() == "input" || t.cs() == "include"
2945                          || t.cs() == "verbatiminput") {
2946                         string name = t.cs();
2947                         if (t.cs() == "verbatiminput"
2948                             && p.next_token().asInput() == "*")
2949                                 name += p.get_token().asInput();
2950                         context.check_layout(os);
2951                         string filename(normalize_filename(p.getArg('{', '}')));
2952                         string const path = getMasterFilePath();
2953                         // We want to preserve relative / absolute filenames,
2954                         // therefore path is only used for testing
2955                         if ((t.cs() == "include" || t.cs() == "input") &&
2956                             !makeAbsPath(filename, path).exists()) {
2957                                 // The file extension is probably missing.
2958                                 // Now try to find it out.
2959                                 string const tex_name =
2960                                         find_file(filename, path,
2961                                                   known_tex_extensions);
2962                                 if (!tex_name.empty())
2963                                         filename = tex_name;
2964                         }
2965                         bool external = false;
2966                         string outname;
2967                         if (makeAbsPath(filename, path).exists()) {
2968                                 string const abstexname =
2969                                         makeAbsPath(filename, path).absFileName();
2970                                 string const abslyxname =
2971                                         changeExtension(abstexname, ".lyx");
2972                                 string const absfigname =
2973                                         changeExtension(abstexname, ".fig");
2974                                 fix_relative_filename(filename);
2975                                 string const lyxname =
2976                                         changeExtension(filename, ".lyx");
2977                                 bool xfig = false;
2978                                 external = FileName(absfigname).exists();
2979                                 if (t.cs() == "input") {
2980                                         string const ext = getExtension(abstexname);
2981
2982                                         // Combined PS/LaTeX:
2983                                         // x.eps, x.pstex_t (old xfig)
2984                                         // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2985                                         FileName const absepsname(
2986                                                 changeExtension(abstexname, ".eps"));
2987                                         FileName const abspstexname(
2988                                                 changeExtension(abstexname, ".pstex"));
2989                                         bool const xfigeps =
2990                                                 (absepsname.exists() ||
2991                                                  abspstexname.exists()) &&
2992                                                 ext == "pstex_t";
2993
2994                                         // Combined PDF/LaTeX:
2995                                         // x.pdf, x.pdftex_t (old xfig)
2996                                         // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2997                                         FileName const abspdfname(
2998                                                 changeExtension(abstexname, ".pdf"));
2999                                         bool const xfigpdf =
3000                                                 abspdfname.exists() &&
3001                                                 (ext == "pdftex_t" || ext == "pdf_t");
3002                                         if (xfigpdf)
3003                                                 pdflatex = true;
3004
3005                                         // Combined PS/PDF/LaTeX:
3006                                         // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
3007                                         string const absbase2(
3008                                                 removeExtension(abstexname) + "_pspdftex");
3009                                         FileName const abseps2name(
3010                                                 addExtension(absbase2, ".eps"));
3011                                         FileName const abspdf2name(
3012                                                 addExtension(absbase2, ".pdf"));
3013                                         bool const xfigboth =
3014                                                 abspdf2name.exists() &&
3015                                                 abseps2name.exists() && ext == "pspdftex";
3016
3017                                         xfig = xfigpdf || xfigeps || xfigboth;
3018                                         external = external && xfig;
3019                                 }
3020                                 if (external) {
3021                                         outname = changeExtension(filename, ".fig");
3022                                 } else if (xfig) {
3023                                         // Don't try to convert, the result
3024                                         // would be full of ERT.
3025                                         outname = filename;
3026                                 } else if (t.cs() != "verbatiminput" &&
3027                                     tex2lyx(abstexname, FileName(abslyxname),
3028                                             p.getEncoding())) {
3029                                         outname = lyxname;
3030                                 } else {
3031                                         outname = filename;
3032                                 }
3033                         } else {
3034                                 cerr << "Warning: Could not find included file '"
3035                                      << filename << "'." << endl;
3036                                 outname = filename;
3037                         }
3038                         if (external) {
3039                                 begin_inset(os, "External\n");
3040                                 os << "\ttemplate XFig\n"
3041                                    << "\tfilename " << outname << '\n';
3042                         } else {
3043                                 begin_command_inset(os, "include", name);
3044                                 os << "preview false\n"
3045                                       "filename \"" << outname << "\"\n";
3046                         }
3047                         end_inset(os);
3048                 }
3049
3050                 else if (t.cs() == "bibliographystyle") {
3051                         // store new bibliographystyle
3052                         bibliographystyle = p.verbatim_item();
3053                         // If any other command than \bibliography and
3054                         // \nocite{*} follows, we need to output the style
3055                         // (because it might be used by that command).
3056                         // Otherwise, it will automatically be output by LyX.
3057                         p.pushPosition();
3058                         bool output = true;
3059                         for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
3060                                 if (t2.cat() == catBegin)
3061                                         break;
3062                                 if (t2.cat() != catEscape)
3063                                         continue;
3064                                 if (t2.cs() == "nocite") {
3065                                         if (p.getArg('{', '}') == "*")
3066                                                 continue;
3067                                 } else if (t2.cs() == "bibliography")
3068                                         output = false;
3069                                 break;
3070                         }
3071                         p.popPosition();
3072                         if (output) {
3073                                 handle_ert(os,
3074                                         "\\bibliographystyle{" + bibliographystyle + '}',
3075                                         context);
3076                         }
3077                 }
3078
3079                 else if (t.cs() == "bibliography") {
3080                         context.check_layout(os);
3081                         begin_command_inset(os, "bibtex", "bibtex");
3082                         if (!btprint.empty()) {
3083                                 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
3084                                 // clear the string because the next BibTeX inset can be without the
3085                                 // \nocite{*} option
3086                                 btprint.clear();
3087                         }
3088                         os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
3089                         // Do we have a bibliographystyle set?
3090                         if (!bibliographystyle.empty())
3091                                 os << "options " << '"' << bibliographystyle << '"' << "\n";
3092                         end_inset(os);
3093                 }
3094
3095                 else if (t.cs() == "parbox")
3096                         parse_box(p, os, 0, FLAG_ITEM, outer, context, "", "", t.cs());
3097
3098                 else if (t.cs() == "ovalbox" || t.cs() == "Ovalbox" ||
3099                          t.cs() == "shadowbox" || t.cs() == "doublebox")
3100                         parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), "");
3101
3102                 else if (t.cs() == "framebox") {
3103                         string special = p.getFullOpt();
3104                         special += p.getOpt();
3105                         parse_outer_box(p, os, FLAG_ITEM, outer, context, t.cs(), special);
3106                 }
3107
3108                 //\makebox() is part of the picture environment and different from \makebox{}
3109                 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
3110                 else if (t.cs() == "makebox") {
3111                         string arg = t.asInput();
3112                         if (p.next_token().character() == '(')
3113                                 //the syntax is: \makebox(x,y)[position]{content}
3114                                 arg += p.getFullParentheseArg();
3115                         else
3116                                 //the syntax is: \makebox[width][position]{content}
3117                                 arg += p.getFullOpt();
3118                         handle_ert(os, arg + p.getFullOpt(), context);
3119                 }
3120
3121                 else if (t.cs() == "smallskip" ||
3122                          t.cs() == "medskip" ||
3123                          t.cs() == "bigskip" ||
3124                          t.cs() == "vfill") {
3125                         context.check_layout(os);
3126                         begin_inset(os, "VSpace ");
3127                         os << t.cs();
3128                         end_inset(os);
3129                         skip_spaces_braces(p);
3130                 }
3131
3132                 else if (is_known(t.cs(), known_spaces)) {
3133                         char const * const * where = is_known(t.cs(), known_spaces);
3134                         context.check_layout(os);
3135                         begin_inset(os, "space ");
3136                         os << '\\' << known_coded_spaces[where - known_spaces]
3137                            << '\n';
3138                         end_inset(os);
3139                         // LaTeX swallows whitespace after all spaces except
3140                         // "\\,". We have to do that here, too, because LyX
3141                         // adds "{}" which would make the spaces significant.
3142                         if (t.cs() !=  ",")
3143                                 eat_whitespace(p, os, context, false);
3144                         // LyX adds "{}" after all spaces except "\\ " and
3145                         // "\\,", so we have to remove "{}".
3146                         // "\\,{}" is equivalent to "\\," in LaTeX, so we
3147                         // remove the braces after "\\,", too.
3148                         if (t.cs() != " ")
3149                                 skip_braces(p);
3150                 }
3151
3152                 else if (t.cs() == "newpage" ||
3153                          (t.cs() == "pagebreak" && !p.hasOpt()) ||
3154                          t.cs() == "clearpage" ||
3155                          t.cs() == "cleardoublepage") {
3156                         context.check_layout(os);
3157                         begin_inset(os, "Newpage ");
3158                         os << t.cs();
3159                         end_inset(os);
3160                         skip_spaces_braces(p);
3161                 }
3162
3163                 else if (t.cs() == "DeclareRobustCommand" ||
3164                          t.cs() == "DeclareRobustCommandx" ||
3165                          t.cs() == "newcommand" ||
3166                          t.cs() == "newcommandx" ||
3167                          t.cs() == "providecommand" ||
3168                          t.cs() == "providecommandx" ||
3169                          t.cs() == "renewcommand" ||
3170                          t.cs() == "renewcommandx") {
3171                         // DeclareRobustCommand, DeclareRobustCommandx,
3172                         // providecommand and providecommandx could be handled
3173                         // by parse_command(), but we need to call
3174                         // add_known_command() here.
3175                         string name = t.asInput();
3176                         if (p.next_token().asInput() == "*") {
3177                                 // Starred form. Eat '*'
3178                                 p.get_token();
3179                                 name += '*';
3180                         }
3181                         string const command = p.verbatim_item();
3182                         string const opt1 = p.getFullOpt();
3183                         string const opt2 = p.getFullOpt();
3184                         add_known_command(command, opt1, !opt2.empty());
3185                         string const ert = name + '{' + command + '}' +
3186                                            opt1 + opt2 +
3187                                            '{' + p.verbatim_item() + '}';
3188
3189                         if (t.cs() == "DeclareRobustCommand" ||
3190                             t.cs() == "DeclareRobustCommandx" ||
3191                             t.cs() == "providecommand" ||
3192                             t.cs() == "providecommandx" ||
3193                             name[name.length()-1] == '*')
3194                                 handle_ert(os, ert, context);
3195                         else {
3196                                 context.check_layout(os);
3197                                 begin_inset(os, "FormulaMacro");
3198                                 os << "\n" << ert;
3199                                 end_inset(os);
3200                         }
3201                 }
3202
3203                 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
3204                         // let could be handled by parse_command(),
3205                         // but we need to call add_known_command() here.
3206                         string ert = t.asInput();
3207                         string name;
3208                         p.skip_spaces();
3209                         if (p.next_token().cat() == catBegin) {
3210                                 name = p.verbatim_item();
3211                                 ert += '{' + name + '}';
3212                         } else {
3213                                 name = p.verbatim_item();
3214                                 ert += name;
3215                         }
3216                         string command;
3217                         p.skip_spaces();
3218                         if (p.next_token().cat() == catBegin) {
3219                                 command = p.verbatim_item();
3220                                 ert += '{' + command + '}';
3221                         } else {
3222                                 command = p.verbatim_item();
3223                                 ert += command;
3224                         }
3225                         // If command is known, make name known too, to parse
3226                         // its arguments correctly. For this reason we also
3227                         // have commands in syntax.default that are hardcoded.
3228                         CommandMap::iterator it = known_commands.find(command);
3229                         if (it != known_commands.end())
3230                                 known_commands[t.asInput()] = it->second;
3231                         handle_ert(os, ert, context);
3232                 }
3233
3234                 else if (t.cs() == "hspace" || t.cs() == "vspace") {
3235                         bool starred = false;
3236                         if (p.next_token().asInput() == "*") {
3237                                 p.get_token();
3238                                 starred = true;
3239                         }
3240                         string name = t.asInput();
3241                         string const length = p.verbatim_item();
3242                         string unit;
3243                         string valstring;
3244                         bool valid = splitLatexLength(length, valstring, unit);
3245                         bool known_hspace = false;
3246                         bool known_vspace = false;
3247                         bool known_unit = false;
3248                         double value;
3249                         if (valid) {
3250                                 istringstream iss(valstring);
3251                                 iss >> value;
3252                                 if (value == 1.0) {
3253                                         if (t.cs()[0] == 'h') {
3254                                                 if (unit == "\\fill") {
3255                                                         if (!starred) {
3256                                                                 unit = "";
3257                                                                 name = "\\hfill";
3258                                                         }
3259                                                         known_hspace = true;
3260                                                 }
3261                                         } else {
3262                                                 if (unit == "\\smallskipamount") {
3263                                                         unit = "smallskip";
3264                                                         known_vspace = true;
3265                                                 } else if (unit == "\\medskipamount") {
3266                                                         unit = "medskip";
3267                                                         known_vspace = true;
3268                                                 } else if (unit == "\\bigskipamount") {
3269                                                         unit = "bigskip";
3270                                                         known_vspace = true;
3271                                                 } else if (unit == "\\fill") {
3272                                                         unit = "vfill";
3273                                                         known_vspace = true;
3274                                                 }
3275                                         }
3276                                 }
3277                                 if (!known_hspace && !known_vspace) {
3278                                         switch (unitFromString(unit)) {
3279                                         case Length::SP:
3280                                         case Length::PT:
3281                                         case Length::BP:
3282                                         case Length::DD:
3283                                         case Length::MM:
3284                                         case Length::PC:
3285                                         case Length::CC:
3286                                         case Length::CM:
3287                                         case Length::IN:
3288                                         case Length::EX:
3289                                         case Length::EM:
3290                                         case Length::MU:
3291                                                 known_unit = true;
3292                                                 break;
3293                                         default:
3294                                                 break;
3295                                         }
3296                                 }
3297                         }
3298
3299                         if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
3300                                 // Literal horizontal length or known variable
3301                                 context.check_layout(os);
3302                                 begin_inset(os, "space ");
3303                                 os << name;
3304                                 if (starred)
3305                                         os << '*';
3306                                 os << '{';
3307                                 if (known_hspace)
3308                                         os << unit;
3309                                 os << "}";
3310                                 if (known_unit && !known_hspace)
3311                                         os << "\n\\length "
3312                                            << translate_len(length);
3313                                 end_inset(os);
3314                         } else if (known_unit || known_vspace) {
3315                                 // Literal vertical length or known variable
3316                                 context.check_layout(os);
3317                                 begin_inset(os, "VSpace ");
3318                                 if (known_unit)
3319                                         os << value;
3320                                 os << unit;
3321                                 if (starred)
3322                                         os << '*';
3323                                 end_inset(os);
3324                         } else {
3325                                 // LyX can't handle other length variables in Inset VSpace/space
3326                                 if (starred)
3327                                         name += '*';
3328                                 if (valid) {
3329                                         if (value == 1.0)
3330                                                 handle_ert(os, name + '{' + unit + '}', context);
3331                                         else if (value == -1.0)
3332                                                 handle_ert(os, name + "{-" + unit + '}', context);
3333                                         else
3334                                                 handle_ert(os, name + '{' + valstring + unit + '}', context);
3335                                 } else
3336                                         handle_ert(os, name + '{' + length + '}', context);
3337                         }
3338                 }
3339
3340                 // The single '=' is meant here.
3341                 else if ((newinsetlayout = findInsetLayout(context.textclass, t.cs(), true))) {
3342                         p.skip_spaces();
3343                         context.check_layout(os);
3344                         begin_inset(os, "Flex ");
3345                         os << to_utf8(newinsetlayout->name()) << '\n'
3346                            << "status collapsed\n";
3347                         parse_text_in_inset(p, os, FLAG_ITEM, false, context, newinsetlayout);
3348                         end_inset(os);
3349                 }
3350
3351                 else {
3352                         // try to see whether the string is in unicodesymbols
3353                         // Only use text mode commands, since we are in text mode here,
3354                         // and math commands may be invalid (bug 6797)
3355                         docstring rem;
3356                         docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
3357                                                                  rem, Encodings::TEXT_CMD);
3358                         if (!s.empty()) {
3359                                 if (!rem.empty())
3360                                         cerr << "When parsing " << t.cs() 
3361                                              << ", result is " << to_utf8(s)
3362                                              << "+" << to_utf8(rem) << endl;
3363                                 context.check_layout(os);
3364                                 os << to_utf8(s);
3365                                 skip_spaces_braces(p);
3366                         }
3367                         //cerr << "#: " << t << " mode: " << mode << endl;
3368                         // heuristic: read up to next non-nested space
3369                         /*
3370                         string s = t.asInput();
3371                         string z = p.verbatim_item();
3372                         while (p.good() && z != " " && z.size()) {
3373                                 //cerr << "read: " << z << endl;
3374                                 s += z;
3375                                 z = p.verbatim_item();
3376                         }
3377                         cerr << "found ERT: " << s << endl;
3378                         handle_ert(os, s + ' ', context);
3379                         */
3380                         else {
3381                                 string name = t.asInput();
3382                                 if (p.next_token().asInput() == "*") {
3383                                         // Starred commands like \vspace*{}
3384                                         p.get_token();  // Eat '*'
3385                                         name += '*';
3386                                 }
3387                                 if (!parse_command(name, p, os, outer, context))
3388                                         handle_ert(os, name, context);
3389                         }
3390                 }
3391
3392                 if (flags & FLAG_LEAVE) {
3393                         flags &= ~FLAG_LEAVE;
3394                         break;
3395                 }
3396         }
3397 }
3398
3399 // }])
3400
3401
3402 } // namespace lyx