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.
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
21 #include "FloatList.h"
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"
38 using namespace lyx::support;
43 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
44 Context const & context)
46 Context newcontext(true, context.textclass);
47 newcontext.font = context.font;
48 parse_text(p, os, flags, outer, newcontext);
49 newcontext.check_end_layout(os);
55 /// parses a paragraph snippet, useful for example for \\emph{...}
56 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
59 Context newcontext(context);
60 // Don't inherit the paragraph-level extra stuff
61 newcontext.par_extra_stuff.clear();
62 parse_text(p, os, flags, outer, newcontext);
63 // Make sure that we don't create invalid .lyx files
64 context.need_layout = newcontext.need_layout;
65 context.need_end_layout = newcontext.need_end_layout;
70 * Thin wrapper around parse_text_snippet() using a string.
72 * We completely ignore \c context.need_layout and \c context.need_end_layout,
73 * because our return value is not used directly (otherwise the stream version
74 * of parse_text_snippet() could be used). That means that the caller needs
75 * to do layout management manually.
76 * This is intended to parse text that does not create any layout changes.
78 string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
81 Context newcontext(context);
82 newcontext.need_layout = false;
83 newcontext.need_end_layout = false;
84 newcontext.new_layout_allowed = false;
85 // Avoid warning by Context::~Context()
86 newcontext.par_extra_stuff.clear();
88 parse_text_snippet(p, os, flags, outer, newcontext);
93 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
94 "vpageref", "prettyref", "eqref", 0 };
98 * The starred forms are also known except for "citefullauthor",
99 * "citeyear" and "citeyearpar".
101 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
102 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
103 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
107 * No starred form other than "cite*" known.
109 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
110 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
111 // jurabib commands not (yet) supported by LyX:
113 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
114 // "footciteauthor", "footciteyear", "footciteyearpar",
115 "citefield", "citetitle", 0 };
117 /// LaTeX names for quotes
118 char const * const known_quotes[] = { "dq", "guillemotleft", "flqq", "og",
119 "guillemotright", "frqq", "fg", "glq", "glqq", "textquoteleft", "grq", "grqq",
120 "quotedblbase", "textquotedblleft", "quotesinglbase", "textquoteright", "flq",
121 "guilsinglleft", "frq", "guilsinglright", 0};
123 /// the same as known_quotes with .lyx names
124 char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
125 "ald", "ald", "ald", "gls", "gld", "els", "els", "grd",
126 "gld", "grd", "gls", "ers", "fls",
127 "fls", "frs", "frs", 0};
129 /// LaTeX names for font sizes
130 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
131 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
133 /// the same as known_sizes with .lyx names
134 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
135 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
137 /// LaTeX 2.09 names for font families
138 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
140 /// LaTeX names for font families
141 char const * const known_font_families[] = { "rmfamily", "sffamily",
144 /// the same as known_old_font_families and known_font_families with .lyx names
145 char const * const known_coded_font_families[] = { "roman", "sans",
148 /// LaTeX 2.09 names for font series
149 char const * const known_old_font_series[] = { "bf", 0};
151 /// LaTeX names for font series
152 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
154 /// the same as known_old_font_series and known_font_series with .lyx names
155 char const * const known_coded_font_series[] = { "bold", "medium", 0};
157 /// LaTeX 2.09 names for font shapes
158 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
160 /// LaTeX names for font shapes
161 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
164 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
165 char const * const known_coded_font_shapes[] = { "italic", "slanted",
166 "smallcaps", "up", 0};
169 * Graphics file extensions known by the dvips driver of the graphics package.
170 * These extensions are used to complete the filename of an included
171 * graphics file if it does not contain an extension.
172 * The order must be the same that latex uses to find a file, because we
173 * will use the first extension that matches.
174 * This is only an approximation for the common cases. If we would want to
175 * do it right in all cases, we would need to know which graphics driver is
176 * used and know the extensions of every driver of the graphics package.
178 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
179 "ps.gz", "eps.Z", "ps.Z", 0};
182 * Graphics file extensions known by the pdftex driver of the graphics package.
183 * \sa known_dvips_graphics_formats
185 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
189 * Known file extensions for TeX files as used by \\include.
191 char const * const known_tex_extensions[] = {"tex", 0};
193 /// spaces known by InsetSpace
194 char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
195 "qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
196 "leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
198 /// the same as known_spaces with .lyx names
199 char const * const known_coded_spaces[] = { "space{}", "space{}",
200 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
201 "negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
202 "rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
204 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
205 /// them in ERT. "LaTeXe" must come before "LaTeX"!
206 char const * const known_phrases[] = {"LyX", "TeX", "LaTeXe", "LaTeX", 0};
207 char const * const known_coded_phrases[] = {"LyX", "TeX", "LaTeX2e", "LaTeX", 0};
208 int const known_phrase_lengths[] = {3, 5, 7, 0};
211 /// splits "x=z, y=b" into a map and an ordered keyword vector
212 void split_map(string const & s, map<string, string> & res, vector<string> & keys)
217 keys.resize(v.size());
218 for (size_t i = 0; i < v.size(); ++i) {
219 size_t const pos = v[i].find('=');
220 string const index = trim(v[i].substr(0, pos));
221 string const value = trim(v[i].substr(pos + 1, string::npos));
229 * Split a LaTeX length into value and unit.
230 * The latter can be a real unit like "pt", or a latex length variable
231 * like "\textwidth". The unit may contain additional stuff like glue
232 * lengths, but we don't care, because such lengths are ERT anyway.
233 * \returns true if \p value and \p unit are valid.
235 bool splitLatexLength(string const & len, string & value, string & unit)
239 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
240 //'4,5' is a valid LaTeX length number. Change it to '4.5'
241 string const length = subst(len, ',', '.');
242 if (i == string::npos)
245 if (len[0] == '\\') {
246 // We had something like \textwidth without a factor
252 value = trim(string(length, 0, i));
256 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
257 if (contains(len, '\\'))
258 unit = trim(string(len, i));
260 unit = ascii_lowercase(trim(string(len, i)));
265 /// A simple function to translate a latex length to something LyX can
266 /// understand. Not perfect, but rather best-effort.
267 bool translate_len(string const & length, string & valstring, string & unit)
269 if (!splitLatexLength(length, valstring, unit))
271 // LyX uses percent values
273 istringstream iss(valstring);
278 string const percentval = oss.str();
280 if (unit.empty() || unit[0] != '\\')
282 string::size_type const i = unit.find(' ');
283 string const endlen = (i == string::npos) ? string() : string(unit, i);
284 if (unit == "\\textwidth") {
285 valstring = percentval;
286 unit = "text%" + endlen;
287 } else if (unit == "\\columnwidth") {
288 valstring = percentval;
289 unit = "col%" + endlen;
290 } else if (unit == "\\paperwidth") {
291 valstring = percentval;
292 unit = "page%" + endlen;
293 } else if (unit == "\\linewidth") {
294 valstring = percentval;
295 unit = "line%" + endlen;
296 } else if (unit == "\\paperheight") {
297 valstring = percentval;
298 unit = "pheight%" + endlen;
299 } else if (unit == "\\textheight") {
300 valstring = percentval;
301 unit = "theight%" + endlen;
309 string translate_len(string const & length)
313 if (translate_len(length, value, unit))
315 // If the input is invalid, return what we have.
323 * Translates a LaTeX length into \p value, \p unit and
324 * \p special parts suitable for a box inset.
325 * The difference from translate_len() is that a box inset knows about
326 * some special "units" that are stored in \p special.
328 void translate_box_len(string const & length, string & value, string & unit, string & special)
330 if (translate_len(length, value, unit)) {
331 if (unit == "\\height" || unit == "\\depth" ||
332 unit == "\\totalheight" || unit == "\\width") {
333 special = unit.substr(1);
334 // The unit is not used, but LyX requires a dummy setting
347 * Find a file with basename \p name in path \p path and an extension
350 string find_file(string const & name, string const & path,
351 char const * const * extensions)
353 for (char const * const * what = extensions; *what; ++what) {
354 string const trial = addExtension(name, *what);
355 if (makeAbsPath(trial, path).exists())
362 void begin_inset(ostream & os, string const & name)
364 os << "\n\\begin_inset " << name;
368 void begin_command_inset(ostream & os, string const & name,
369 string const & latexname)
371 begin_inset(os, "CommandInset ");
372 os << name << "\nLatexCommand " << latexname << '\n';
376 void end_inset(ostream & os)
378 os << "\n\\end_inset\n\n";
382 bool skip_braces(Parser & p)
384 if (p.next_token().cat() != catBegin)
387 if (p.next_token().cat() == catEnd) {
396 void handle_ert(ostream & os, string const & s, Context & context)
398 // We must have a valid layout before outputting the ERT inset.
399 context.check_layout(os);
400 Context newcontext(true, context.textclass);
401 begin_inset(os, "ERT");
402 os << "\nstatus collapsed\n";
403 newcontext.check_layout(os);
404 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
406 os << "\n\\backslash\n";
407 else if (*it == '\n') {
408 newcontext.new_paragraph(os);
409 newcontext.check_layout(os);
413 newcontext.check_end_layout(os);
418 void handle_comment(ostream & os, string const & s, Context & context)
420 // TODO: Handle this better
421 Context newcontext(true, context.textclass);
422 begin_inset(os, "ERT");
423 os << "\nstatus collapsed\n";
424 newcontext.check_layout(os);
425 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
427 os << "\n\\backslash\n";
431 // make sure that our comment is the last thing on the line
432 newcontext.new_paragraph(os);
433 newcontext.check_layout(os);
434 newcontext.check_end_layout(os);
439 Layout const * findLayout(TextClass const & textclass, string const & name)
441 DocumentClass::const_iterator lit = textclass.begin();
442 DocumentClass::const_iterator len = textclass.end();
443 for (; lit != len; ++lit)
444 if (lit->latexname() == name)
450 void eat_whitespace(Parser &, ostream &, Context &, bool);
454 * Skips whitespace and braces.
455 * This should be called after a command has been parsed that is not put into
456 * ERT, and where LyX adds "{}" if needed.
458 void skip_spaces_braces(Parser & p, bool keepws = false)
460 /* The following four examples produce the same typeset output and
461 should be handled by this function:
469 // Unfortunately we need to skip comments, too.
470 // We can't use eat_whitespace since writing them after the {}
471 // results in different output in some cases.
472 bool const skipped_spaces = p.skip_spaces(true);
473 bool const skipped_braces = skip_braces(p);
474 if (keepws && skipped_spaces && !skipped_braces)
475 // put back the space (it is better handled by check_space)
476 p.unskip_spaces(true);
480 void output_command_layout(ostream & os, Parser & p, bool outer,
481 Context & parent_context,
482 Layout const * newlayout)
484 TeXFont const oldFont = parent_context.font;
485 // save the current font size
486 string const size = oldFont.size;
487 // reset the font size to default, because the font size switches
488 // don't affect section headings and the like
489 parent_context.font.size = Context::normalfont.size;
490 // we only need to write the font change if we have an open layout
491 if (!parent_context.atParagraphStart())
492 output_font_change(os, oldFont, parent_context.font);
493 parent_context.check_end_layout(os);
494 Context context(true, parent_context.textclass, newlayout,
495 parent_context.layout, parent_context.font);
496 if (parent_context.deeper_paragraph) {
497 // We are beginning a nested environment after a
498 // deeper paragraph inside the outer list environment.
499 // Therefore we don't need to output a "begin deeper".
500 context.need_end_deeper = true;
502 context.check_deeper(os);
503 context.check_layout(os);
504 unsigned int optargs = 0;
505 while (optargs < context.layout->optargs) {
506 eat_whitespace(p, os, context, false);
507 if (p.next_token().cat() == catEscape ||
508 p.next_token().character() != '[')
510 p.get_token(); // eat '['
511 begin_inset(os, "OptArg\n");
512 os << "status collapsed\n\n";
513 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
515 eat_whitespace(p, os, context, false);
518 unsigned int reqargs = 0;
519 while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
520 eat_whitespace(p, os, context, false);
521 if (p.next_token().cat() != catBegin)
523 p.get_token(); // eat '{'
524 begin_inset(os, "OptArg\n");
525 os << "status collapsed\n\n";
526 parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
528 eat_whitespace(p, os, context, false);
531 parse_text(p, os, FLAG_ITEM, outer, context);
532 context.check_end_layout(os);
533 if (parent_context.deeper_paragraph) {
534 // We must suppress the "end deeper" because we
535 // suppressed the "begin deeper" above.
536 context.need_end_deeper = false;
538 context.check_end_deeper(os);
539 // We don't need really a new paragraph, but
540 // we must make sure that the next item gets a \begin_layout.
541 parent_context.new_paragraph(os);
542 // Set the font size to the original value. No need to output it here
543 // (Context::begin_layout() will do that if needed)
544 parent_context.font.size = size;
549 * Output a space if necessary.
550 * This function gets called for every whitespace token.
552 * We have three cases here:
553 * 1. A space must be suppressed. Example: The lyxcode case below
554 * 2. A space may be suppressed. Example: Spaces before "\par"
555 * 3. A space must not be suppressed. Example: A space between two words
557 * We currently handle only 1. and 3 and from 2. only the case of
558 * spaces before newlines as a side effect.
560 * 2. could be used to suppress as many spaces as possible. This has two effects:
561 * - Reimporting LyX generated LaTeX files changes almost no whitespace
562 * - Superflous whitespace from non LyX generated LaTeX files is removed.
563 * The drawback is that the logic inside the function becomes
564 * complicated, and that is the reason why it is not implemented.
566 void check_space(Parser & p, ostream & os, Context & context)
568 Token const next = p.next_token();
569 Token const curr = p.curr_token();
570 // A space before a single newline and vice versa must be ignored
571 // LyX emits a newline before \end{lyxcode}.
572 // This newline must be ignored,
573 // otherwise LyX will add an additional protected space.
574 if (next.cat() == catSpace ||
575 next.cat() == catNewline ||
576 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
579 context.check_layout(os);
585 * Parse all arguments of \p command
587 void parse_arguments(string const & command,
588 vector<ArgumentType> const & template_arguments,
589 Parser & p, ostream & os, bool outer, Context & context)
591 string ert = command;
592 size_t no_arguments = template_arguments.size();
593 for (size_t i = 0; i < no_arguments; ++i) {
594 switch (template_arguments[i]) {
596 // This argument contains regular LaTeX
597 handle_ert(os, ert + '{', context);
598 eat_whitespace(p, os, context, false);
599 parse_text(p, os, FLAG_ITEM, outer, context);
603 // This argument consists only of a single item.
604 // The presence of '{' or not must be preserved.
606 if (p.next_token().cat() == catBegin)
607 ert += '{' + p.verbatim_item() + '}';
609 ert += p.verbatim_item();
612 // This argument may contain special characters
613 ert += '{' + p.verbatim_item() + '}';
616 // true because we must not eat whitespace
617 ert += p.getOpt(true);
621 handle_ert(os, ert, context);
626 * Check whether \p command is a known command. If yes,
627 * handle the command with all arguments.
628 * \return true if the command was parsed, false otherwise.
630 bool parse_command(string const & command, Parser & p, ostream & os,
631 bool outer, Context & context)
633 if (known_commands.find(command) != known_commands.end()) {
634 parse_arguments(command, known_commands[command], p, os,
642 /// Parses a minipage or parbox
643 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
644 Context & parent_context, bool use_parbox)
648 // We need to set the height to the LaTeX default of 1\\totalheight
649 // for the case when no height argument is given
650 string height_value = "1";
651 string height_unit = "in";
652 string height_special = "totalheight";
655 position = p.getArg('[', ']');
656 if (position != "t" && position != "c" && position != "b") {
658 cerr << "invalid position for minipage/parbox" << endl;
661 latex_height = p.getArg('[', ']');
662 translate_box_len(latex_height, height_value, height_unit, height_special);
665 inner_pos = p.getArg('[', ']');
666 if (inner_pos != "c" && inner_pos != "t" &&
667 inner_pos != "b" && inner_pos != "s") {
668 inner_pos = position;
669 cerr << "invalid inner_pos for minipage/parbox"
677 string const latex_width = p.verbatim_item();
678 translate_len(latex_width, width_value, width_unit);
679 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
680 // LyX can't handle length variables
685 ss << "\\begin{minipage}";
686 if (!position.empty())
687 ss << '[' << position << ']';
688 if (!latex_height.empty())
689 ss << '[' << latex_height << ']';
690 if (!inner_pos.empty())
691 ss << '[' << inner_pos << ']';
692 ss << "{" << latex_width << "}";
695 handle_ert(os, ss.str(), parent_context);
696 parent_context.new_paragraph(os);
697 parse_text_in_inset(p, os, flags, outer, parent_context);
699 handle_ert(os, "}", parent_context);
701 handle_ert(os, "\\end{minipage}", parent_context);
703 // LyX does not like empty positions, so we have
704 // to set them to the LaTeX default values here.
705 if (position.empty())
707 if (inner_pos.empty())
708 inner_pos = position;
709 parent_context.check_layout(os);
710 begin_inset(os, "Box Frameless\n");
711 os << "position \"" << position << "\"\n";
712 os << "hor_pos \"c\"\n";
713 os << "has_inner_box 1\n";
714 os << "inner_pos \"" << inner_pos << "\"\n";
715 os << "use_parbox " << use_parbox << "\n";
716 os << "width \"" << width_value << width_unit << "\"\n";
717 os << "special \"none\"\n";
718 os << "height \"" << height_value << height_unit << "\"\n";
719 os << "height_special \"" << height_special << "\"\n";
720 os << "status open\n\n";
721 parse_text_in_inset(p, os, flags, outer, parent_context);
723 #ifdef PRESERVE_LAYOUT
724 // LyX puts a % after the end of the minipage
725 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
727 //handle_comment(os, "%dummy", parent_context);
730 parent_context.new_paragraph(os);
732 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
733 //handle_comment(os, "%dummy", parent_context);
736 // We add a protected space if something real follows
737 if (p.good() && p.next_token().cat() != catComment) {
738 begin_inset(os, "space ~\n");
747 /// parse an unknown environment
748 void parse_unknown_environment(Parser & p, string const & name, ostream & os,
749 unsigned flags, bool outer,
750 Context & parent_context)
752 if (name == "tabbing")
753 // We need to remember that we have to handle '\=' specially
754 flags |= FLAG_TABBING;
756 // We need to translate font changes and paragraphs inside the
757 // environment to ERT if we have a non standard font.
758 // Otherwise things like
759 // \large\begin{foo}\huge bar\end{foo}
761 bool const specialfont =
762 (parent_context.font != parent_context.normalfont);
763 bool const new_layout_allowed = parent_context.new_layout_allowed;
765 parent_context.new_layout_allowed = false;
766 handle_ert(os, "\\begin{" + name + "}", parent_context);
767 parse_text_snippet(p, os, flags, outer, parent_context);
768 handle_ert(os, "\\end{" + name + "}", parent_context);
770 parent_context.new_layout_allowed = new_layout_allowed;
774 void parse_environment(Parser & p, ostream & os, bool outer,
775 string & last_env, Context & parent_context)
777 Layout const * newlayout;
778 string const name = p.getArg('{', '}');
779 const bool is_starred = suffixIs(name, '*');
780 string const unstarred_name = rtrim(name, "*");
781 active_environments.push_back(name);
783 if (is_math_env(name)) {
784 parent_context.check_layout(os);
785 begin_inset(os, "Formula ");
786 os << "\\begin{" << name << "}";
787 parse_math(p, os, FLAG_END, MATH_MODE);
788 os << "\\end{" << name << "}";
792 else if (name == "tabular" || name == "longtable") {
793 eat_whitespace(p, os, parent_context, false);
794 parent_context.check_layout(os);
795 begin_inset(os, "Tabular ");
796 handle_tabular(p, os, name == "longtable", parent_context);
801 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
802 eat_whitespace(p, os, parent_context, false);
803 parent_context.check_layout(os);
804 begin_inset(os, "Float " + unstarred_name + "\n");
806 os << "placement " << p.getArg('[', ']') << '\n';
807 os << "wide " << convert<string>(is_starred)
808 << "\nsideways false"
809 << "\nstatus open\n\n";
810 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
812 // We don't need really a new paragraph, but
813 // we must make sure that the next item gets a \begin_layout.
814 parent_context.new_paragraph(os);
818 else if (name == "minipage") {
819 eat_whitespace(p, os, parent_context, false);
820 parse_box(p, os, FLAG_END, outer, parent_context, false);
824 else if (name == "comment") {
825 eat_whitespace(p, os, parent_context, false);
826 parent_context.check_layout(os);
827 begin_inset(os, "Note Comment\n");
828 os << "status open\n";
829 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
832 skip_braces(p); // eat {} that might by set by LyX behind comments
835 else if (name == "lyxgreyedout") {
836 eat_whitespace(p, os, parent_context, false);
837 parent_context.check_layout(os);
838 begin_inset(os, "Note Greyedout\n");
839 os << "status open\n";
840 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
845 else if (name == "framed" || name == "shaded") {
846 eat_whitespace(p, os, parent_context, false);
847 parent_context.check_layout(os);
848 if (name == "framed")
849 begin_inset(os, "Box Framed\n");
851 begin_inset(os, "Box Shaded\n");
852 os << "position \"t\"\n"
857 "width \"100col%\"\n"
860 "height_special \"totalheight\"\n"
862 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
867 else if (!parent_context.new_layout_allowed)
868 parse_unknown_environment(p, name, os, FLAG_END, outer,
871 // Alignment and spacing settings
872 // FIXME (bug xxxx): These settings can span multiple paragraphs and
873 // therefore are totally broken!
874 // Note that \centering, raggedright, and raggedleft cannot be handled, as
875 // they are commands not environments. They are furthermore switches that
876 // can be ended by another switches, but also by commands like \footnote or
877 // \parbox. So the only safe way is to leave them untouched.
878 else if (name == "center" || name == "centering" ||
879 name == "flushleft" || name == "flushright" ||
880 name == "singlespace" || name == "onehalfspace" ||
881 name == "doublespace" || name == "spacing") {
882 eat_whitespace(p, os, parent_context, false);
883 // We must begin a new paragraph if not already done
884 if (! parent_context.atParagraphStart()) {
885 parent_context.check_end_layout(os);
886 parent_context.new_paragraph(os);
888 if (name == "flushleft")
889 parent_context.add_extra_stuff("\\align left\n");
890 else if (name == "flushright")
891 parent_context.add_extra_stuff("\\align right\n");
892 else if (name == "center" || name == "centering")
893 parent_context.add_extra_stuff("\\align center\n");
894 else if (name == "singlespace")
895 parent_context.add_extra_stuff("\\paragraph_spacing single\n");
896 else if (name == "onehalfspace")
897 parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
898 else if (name == "doublespace")
899 parent_context.add_extra_stuff("\\paragraph_spacing double\n");
900 else if (name == "spacing")
901 parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
902 parse_text(p, os, FLAG_END, outer, parent_context);
903 // Just in case the environment is empty
904 parent_context.extra_stuff.erase();
905 // We must begin a new paragraph to reset the alignment
906 parent_context.new_paragraph(os);
910 // The single '=' is meant here.
911 else if ((newlayout = findLayout(parent_context.textclass, name)) &&
912 newlayout->isEnvironment()) {
913 eat_whitespace(p, os, parent_context, false);
914 Context context(true, parent_context.textclass, newlayout,
915 parent_context.layout, parent_context.font);
916 if (parent_context.deeper_paragraph) {
917 // We are beginning a nested environment after a
918 // deeper paragraph inside the outer list environment.
919 // Therefore we don't need to output a "begin deeper".
920 context.need_end_deeper = true;
922 parent_context.check_end_layout(os);
923 if (last_env == name) {
924 // we need to output a separator since LyX would export
925 // the two environments as one otherwise (bug 5716)
926 docstring const sep = from_ascii("--Separator--");
927 TeX2LyXDocClass const & textclass(parent_context.textclass);
928 if (textclass.hasLayout(sep)) {
929 Context newcontext(parent_context);
930 newcontext.layout = &(textclass[sep]);
931 newcontext.check_layout(os);
932 newcontext.check_end_layout(os);
934 parent_context.check_layout(os);
935 begin_inset(os, "Note Note\n");
936 os << "status closed\n";
937 Context newcontext(true, textclass,
938 &(textclass.defaultLayout()));
939 newcontext.check_layout(os);
940 newcontext.check_end_layout(os);
942 parent_context.check_end_layout(os);
945 switch (context.layout->latextype) {
946 case LATEX_LIST_ENVIRONMENT:
947 context.add_par_extra_stuff("\\labelwidthstring "
948 + p.verbatim_item() + '\n');
951 case LATEX_BIB_ENVIRONMENT:
952 p.verbatim_item(); // swallow next arg
958 context.check_deeper(os);
959 parse_text(p, os, FLAG_END, outer, context);
960 context.check_end_layout(os);
961 if (parent_context.deeper_paragraph) {
962 // We must suppress the "end deeper" because we
963 // suppressed the "begin deeper" above.
964 context.need_end_deeper = false;
966 context.check_end_deeper(os);
967 parent_context.new_paragraph(os);
971 else if (name == "appendix") {
972 // This is no good latex style, but it works and is used in some documents...
973 eat_whitespace(p, os, parent_context, false);
974 parent_context.check_end_layout(os);
975 Context context(true, parent_context.textclass, parent_context.layout,
976 parent_context.layout, parent_context.font);
977 context.check_layout(os);
978 os << "\\start_of_appendix\n";
979 parse_text(p, os, FLAG_END, outer, context);
980 context.check_end_layout(os);
984 else if (known_environments.find(name) != known_environments.end()) {
985 vector<ArgumentType> arguments = known_environments[name];
986 // The last "argument" denotes wether we may translate the
987 // environment contents to LyX
988 // The default required if no argument is given makes us
989 // compatible with the reLyXre environment.
990 ArgumentType contents = arguments.empty() ?
993 if (!arguments.empty())
994 arguments.pop_back();
995 // See comment in parse_unknown_environment()
996 bool const specialfont =
997 (parent_context.font != parent_context.normalfont);
998 bool const new_layout_allowed =
999 parent_context.new_layout_allowed;
1001 parent_context.new_layout_allowed = false;
1002 parse_arguments("\\begin{" + name + "}", arguments, p, os,
1003 outer, parent_context);
1004 if (contents == verbatim)
1005 handle_ert(os, p.verbatimEnvironment(name),
1008 parse_text_snippet(p, os, FLAG_END, outer,
1010 handle_ert(os, "\\end{" + name + "}", parent_context);
1012 parent_context.new_layout_allowed = new_layout_allowed;
1016 parse_unknown_environment(p, name, os, FLAG_END, outer,
1020 active_environments.pop_back();
1024 /// parses a comment and outputs it to \p os.
1025 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
1027 LASSERT(t.cat() == catComment, return);
1028 if (!t.cs().empty()) {
1029 context.check_layout(os);
1030 handle_comment(os, '%' + t.cs(), context);
1031 if (p.next_token().cat() == catNewline) {
1032 // A newline after a comment line starts a new
1034 if (context.new_layout_allowed) {
1035 if(!context.atParagraphStart())
1036 // Only start a new paragraph if not already
1037 // done (we might get called recursively)
1038 context.new_paragraph(os);
1040 handle_ert(os, "\n", context);
1041 eat_whitespace(p, os, context, true);
1044 // "%\n" combination
1051 * Reads spaces and comments until the first non-space, non-comment token.
1052 * New paragraphs (double newlines or \\par) are handled like simple spaces
1053 * if \p eatParagraph is true.
1054 * Spaces are skipped, but comments are written to \p os.
1056 void eat_whitespace(Parser & p, ostream & os, Context & context,
1060 Token const & t = p.get_token();
1061 if (t.cat() == catComment)
1062 parse_comment(p, os, t, context);
1063 else if ((! eatParagraph && p.isParagraph()) ||
1064 (t.cat() != catSpace && t.cat() != catNewline)) {
1073 * Set a font attribute, parse text and reset the font attribute.
1074 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
1075 * \param currentvalue Current value of the attribute. Is set to the new
1076 * value during parsing.
1077 * \param newvalue New value of the attribute
1079 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
1080 Context & context, string const & attribute,
1081 string & currentvalue, string const & newvalue)
1083 context.check_layout(os);
1084 string const oldvalue = currentvalue;
1085 currentvalue = newvalue;
1086 os << '\n' << attribute << ' ' << newvalue << "\n";
1087 parse_text_snippet(p, os, flags, outer, context);
1088 context.check_layout(os);
1089 os << '\n' << attribute << ' ' << oldvalue << "\n";
1090 currentvalue = oldvalue;
1094 /// get the arguments of a natbib or jurabib citation command
1095 void get_cite_arguments(Parser & p, bool natbibOrder,
1096 string & before, string & after)
1098 // We need to distinguish "" and "[]", so we can't use p.getOpt().
1100 // text before the citation
1102 // text after the citation
1103 after = p.getFullOpt();
1105 if (!after.empty()) {
1106 before = p.getFullOpt();
1107 if (natbibOrder && !before.empty())
1108 swap(before, after);
1113 /// Convert filenames with TeX macros and/or quotes to something LyX
1115 string const normalize_filename(string const & name)
1117 Parser p(trim(name, "\""));
1120 Token const & t = p.get_token();
1121 if (t.cat() != catEscape)
1123 else if (t.cs() == "lyxdot") {
1124 // This is used by LyX for simple dots in relative
1128 } else if (t.cs() == "space") {
1138 /// Convert \p name from TeX convention (relative to master file) to LyX
1139 /// convention (relative to .lyx file) if it is relative
1140 void fix_relative_filename(string & name)
1142 if (FileName::isAbsolute(name))
1145 name = to_utf8(makeRelPath(from_utf8(makeAbsPath(name, getMasterFilePath()).absFileName()),
1146 from_utf8(getParentFilePath())));
1150 /// Parse a NoWeb Scrap section. The initial "<<" is already parsed.
1151 void parse_noweb(Parser & p, ostream & os, Context & context)
1153 // assemble the rest of the keyword
1157 Token const & t = p.get_token();
1158 if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1161 scrap = (p.good() && p.next_token().asInput() == "=");
1163 name += p.get_token().asInput();
1166 name += t.asInput();
1169 if (!scrap || !context.new_layout_allowed ||
1170 !context.textclass.hasLayout(from_ascii("Scrap"))) {
1171 cerr << "Warning: Could not interpret '" << name
1172 << "'. Ignoring it." << endl;
1176 // We use new_paragraph instead of check_end_layout because the stuff
1177 // following the noweb chunk needs to start with a \begin_layout.
1178 // This may create a new paragraph even if there was none in the
1179 // noweb file, but the alternative is an invalid LyX file. Since
1180 // noweb code chunks are implemented with a layout style in LyX they
1181 // always must be in an own paragraph.
1182 context.new_paragraph(os);
1183 Context newcontext(true, context.textclass,
1184 &context.textclass[from_ascii("Scrap")]);
1185 newcontext.check_layout(os);
1188 Token const & t = p.get_token();
1189 // We abuse the parser a bit, because this is no TeX syntax
1191 if (t.cat() == catEscape)
1192 os << subst(t.asInput(), "\\", "\n\\backslash\n");
1195 begin_inset(oss, "Newline newline");
1197 os << subst(t.asInput(), "\n", oss.str());
1199 // The scrap chunk is ended by an @ at the beginning of a line.
1200 // After the @ the line may contain a comment and/or
1201 // whitespace, but nothing else.
1202 if (t.asInput() == "@" && p.prev_token().cat() == catNewline &&
1203 (p.next_token().cat() == catSpace ||
1204 p.next_token().cat() == catNewline ||
1205 p.next_token().cat() == catComment)) {
1206 while (p.good() && p.next_token().cat() == catSpace)
1207 os << p.get_token().asInput();
1208 if (p.next_token().cat() == catComment)
1209 // The comment includes a final '\n'
1210 os << p.get_token().asInput();
1212 if (p.next_token().cat() == catNewline)
1219 newcontext.check_end_layout(os);
1223 /// detects \\def, \\long\\def and \\global\\long\\def with ws and comments
1224 bool is_macro(Parser & p)
1226 Token first = p.curr_token();
1227 if (first.cat() != catEscape || !p.good())
1229 if (first.cs() == "def")
1231 if (first.cs() != "global" && first.cs() != "long")
1233 Token second = p.get_token();
1235 while (p.good() && !p.isParagraph() && (second.cat() == catSpace ||
1236 second.cat() == catNewline || second.cat() == catComment)) {
1237 second = p.get_token();
1240 bool secondvalid = second.cat() == catEscape;
1242 bool thirdvalid = false;
1243 if (p.good() && first.cs() == "global" && secondvalid &&
1244 second.cs() == "long") {
1245 third = p.get_token();
1247 while (p.good() && !p.isParagraph() &&
1248 (third.cat() == catSpace ||
1249 third.cat() == catNewline ||
1250 third.cat() == catComment)) {
1251 third = p.get_token();
1254 thirdvalid = third.cat() == catEscape;
1256 for (int i = 0; i < pos; ++i)
1261 return (first.cs() == "global" || first.cs() == "long") &&
1262 second.cs() == "def";
1263 return first.cs() == "global" && second.cs() == "long" &&
1264 third.cs() == "def";
1268 /// Parse a macro definition (assumes that is_macro() returned true)
1269 void parse_macro(Parser & p, ostream & os, Context & context)
1271 context.check_layout(os);
1272 Token first = p.curr_token();
1275 string command = first.asInput();
1276 if (first.cs() != "def") {
1278 eat_whitespace(p, os, context, false);
1279 second = p.curr_token();
1280 command += second.asInput();
1281 if (second.cs() != "def") {
1283 eat_whitespace(p, os, context, false);
1284 third = p.curr_token();
1285 command += third.asInput();
1288 eat_whitespace(p, os, context, false);
1289 string const name = p.get_token().cs();
1290 eat_whitespace(p, os, context, false);
1296 while (p.next_token().cat() != catBegin) {
1297 if (p.next_token().cat() == catParameter) {
1302 // followed by number?
1303 if (p.next_token().cat() == catOther) {
1304 char c = p.getChar();
1306 // number = current arity + 1?
1307 if (c == arity + '0' + 1)
1312 paramtext += p.get_token().cs();
1314 paramtext += p.get_token().cs();
1319 // only output simple (i.e. compatible) macro as FormulaMacros
1320 string ert = '\\' + name + ' ' + paramtext + '{' + p.verbatim_item() + '}';
1322 context.check_layout(os);
1323 begin_inset(os, "FormulaMacro");
1324 os << "\n\\def" << ert;
1327 handle_ert(os, command + ert, context);
1330 } // anonymous namespace
1333 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
1336 Layout const * newlayout = 0;
1337 // Store the latest bibliographystyle and nocite{*} option
1338 // (needed for bibtex inset)
1340 string bibliographystyle;
1341 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
1342 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
1345 Token const & t = p.get_token();
1348 debugToken(cerr, t, flags);
1351 if (flags & FLAG_ITEM) {
1352 if (t.cat() == catSpace)
1355 flags &= ~FLAG_ITEM;
1356 if (t.cat() == catBegin) {
1357 // skip the brace and collect everything to the next matching
1359 flags |= FLAG_BRACE_LAST;
1363 // handle only this single token, leave the loop if done
1364 flags |= FLAG_LEAVE;
1367 if (t.cat() != catEscape && t.character() == ']' &&
1368 (flags & FLAG_BRACK_LAST))
1370 if (t.cat() == catEnd && (flags & FLAG_BRACE_LAST))
1373 // If there is anything between \end{env} and \begin{env} we
1374 // don't need to output a separator.
1375 if (t.cat() != catSpace && t.cat() != catNewline &&
1376 t.asInput() != "\\begin")
1382 if (t.cat() == catMath) {
1383 // we are inside some text mode thingy, so opening new math is allowed
1384 context.check_layout(os);
1385 begin_inset(os, "Formula ");
1386 Token const & n = p.get_token();
1387 if (n.cat() == catMath && outer) {
1388 // TeX's $$...$$ syntax for displayed math
1390 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1392 p.get_token(); // skip the second '$' token
1394 // simple $...$ stuff
1397 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
1403 else if (t.cat() == catSuper || t.cat() == catSub)
1404 cerr << "catcode " << t << " illegal in text mode\n";
1406 // Basic support for english quotes. This should be
1407 // extended to other quotes, but is not so easy (a
1408 // left english quote is the same as a right german
1410 else if (t.asInput() == "`" && p.next_token().asInput() == "`") {
1411 context.check_layout(os);
1412 begin_inset(os, "Quotes ");
1418 else if (t.asInput() == "'" && p.next_token().asInput() == "'") {
1419 context.check_layout(os);
1420 begin_inset(os, "Quotes ");
1427 else if (t.asInput() == ">" && p.next_token().asInput() == ">") {
1428 context.check_layout(os);
1429 begin_inset(os, "Quotes ");
1436 else if (t.asInput() == "<" && p.next_token().asInput() == "<") {
1437 context.check_layout(os);
1438 begin_inset(os, "Quotes ");
1445 else if (t.asInput() == "<"
1446 && p.next_token().asInput() == "<" && noweb_mode) {
1448 parse_noweb(p, os, context);
1451 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
1452 check_space(p, os, context);
1454 else if (t.character() == '[' && noweb_mode &&
1455 p.next_token().character() == '[') {
1456 // These can contain underscores
1458 string const s = p.getFullOpt() + ']';
1459 if (p.next_token().character() == ']')
1462 cerr << "Warning: Inserting missing ']' in '"
1463 << s << "'." << endl;
1464 handle_ert(os, s, context);
1467 else if (t.cat() == catLetter) {
1468 context.check_layout(os);
1469 // Workaround for bug 4752.
1470 // FIXME: This whole code block needs to be removed
1471 // when the bug is fixed and tex2lyx produces
1472 // the updated file format.
1473 // The replacement algorithm in LyX is so stupid that
1474 // it even translates a phrase if it is part of a word.
1475 bool handled = false;
1476 for (int const * l = known_phrase_lengths; *l; ++l) {
1477 string phrase = t.cs();
1478 for (int i = 1; i < *l && p.next_token().isAlnumASCII(); ++i)
1479 phrase += p.get_token().cs();
1480 if (is_known(phrase, known_coded_phrases)) {
1481 handle_ert(os, phrase, context);
1485 for (size_t i = 1; i < phrase.length(); ++i)
1493 else if (t.cat() == catOther ||
1494 t.cat() == catAlign ||
1495 t.cat() == catParameter) {
1496 // This translates "&" to "\\&" which may be wrong...
1497 context.check_layout(os);
1501 else if (p.isParagraph()) {
1502 if (context.new_layout_allowed)
1503 context.new_paragraph(os);
1505 handle_ert(os, "\\par ", context);
1506 eat_whitespace(p, os, context, true);
1509 else if (t.cat() == catActive) {
1510 context.check_layout(os);
1511 if (t.character() == '~') {
1512 if (context.layout->free_spacing)
1515 begin_inset(os, "space ~\n");
1522 else if (t.cat() == catBegin &&
1523 p.next_token().cat() == catEnd) {
1525 Token const prev = p.prev_token();
1527 if (p.next_token().character() == '`' ||
1528 (prev.character() == '-' &&
1529 p.next_token().character() == '-'))
1530 ; // ignore it in {}`` or -{}-
1532 handle_ert(os, "{}", context);
1536 else if (t.cat() == catBegin) {
1537 context.check_layout(os);
1538 // special handling of font attribute changes
1539 Token const prev = p.prev_token();
1540 Token const next = p.next_token();
1541 TeXFont const oldFont = context.font;
1542 if (next.character() == '[' ||
1543 next.character() == ']' ||
1544 next.character() == '*') {
1546 if (p.next_token().cat() == catEnd) {
1551 handle_ert(os, "{", context);
1552 parse_text_snippet(p, os,
1555 handle_ert(os, "}", context);
1557 } else if (! context.new_layout_allowed) {
1558 handle_ert(os, "{", context);
1559 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1561 handle_ert(os, "}", context);
1562 } else if (is_known(next.cs(), known_sizes)) {
1563 // next will change the size, so we must
1565 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1567 if (!context.atParagraphStart())
1569 << context.font.size << "\n";
1570 } else if (is_known(next.cs(), known_font_families)) {
1571 // next will change the font family, so we
1572 // must reset it here
1573 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1575 if (!context.atParagraphStart())
1577 << context.font.family << "\n";
1578 } else if (is_known(next.cs(), known_font_series)) {
1579 // next will change the font series, so we
1580 // must reset it here
1581 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1583 if (!context.atParagraphStart())
1585 << context.font.series << "\n";
1586 } else if (is_known(next.cs(), known_font_shapes)) {
1587 // next will change the font shape, so we
1588 // must reset it here
1589 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1591 if (!context.atParagraphStart())
1593 << context.font.shape << "\n";
1594 } else if (is_known(next.cs(), known_old_font_families) ||
1595 is_known(next.cs(), known_old_font_series) ||
1596 is_known(next.cs(), known_old_font_shapes)) {
1597 // next will change the font family, series
1598 // and shape, so we must reset it here
1599 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1601 if (!context.atParagraphStart())
1603 << context.font.family
1605 << context.font.series
1607 << context.font.shape << "\n";
1609 handle_ert(os, "{", context);
1610 parse_text_snippet(p, os, FLAG_BRACE_LAST,
1612 handle_ert(os, "}", context);
1616 else if (t.cat() == catEnd) {
1617 if (flags & FLAG_BRACE_LAST) {
1620 cerr << "stray '}' in text\n";
1621 handle_ert(os, "}", context);
1624 else if (t.cat() == catComment)
1625 parse_comment(p, os, t, context);
1628 // control sequences
1631 else if (t.cs() == "(") {
1632 context.check_layout(os);
1633 begin_inset(os, "Formula");
1635 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1640 else if (t.cs() == "[") {
1641 context.check_layout(os);
1642 begin_inset(os, "Formula");
1644 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1649 else if (t.cs() == "begin")
1650 parse_environment(p, os, outer, last_env, context);
1652 else if (t.cs() == "end") {
1653 if (flags & FLAG_END) {
1654 // eat environment name
1655 string const name = p.getArg('{', '}');
1656 if (name != active_environment())
1657 cerr << "\\end{" + name + "} does not match \\begin{"
1658 + active_environment() + "}\n";
1661 p.error("found 'end' unexpectedly");
1664 else if (t.cs() == "item") {
1667 bool optarg = false;
1668 if (p.next_token().cat() != catEscape &&
1669 p.next_token().character() == '[') {
1670 p.get_token(); // eat '['
1671 s = parse_text_snippet(p, FLAG_BRACK_LAST,
1676 context.check_layout(os);
1677 if (context.has_item) {
1678 // An item in an unknown list-like environment
1679 // FIXME: Do this in check_layout()!
1680 context.has_item = false;
1682 handle_ert(os, "\\item", context);
1684 handle_ert(os, "\\item ", context);
1687 if (context.layout->labeltype != LABEL_MANUAL) {
1688 // LyX does not support \item[\mybullet]
1689 // in itemize environments
1690 handle_ert(os, "[", context);
1692 handle_ert(os, "]", context);
1693 } else if (!s.empty()) {
1694 // The space is needed to separate the
1695 // item from the rest of the sentence.
1697 eat_whitespace(p, os, context, false);
1702 else if (t.cs() == "bibitem") {
1704 context.check_layout(os);
1705 begin_command_inset(os, "bibitem", "bibitem");
1706 os << "label \"" << p.getOptContent() << "\"\n";
1707 os << "key \"" << p.verbatim_item() << "\"\n";
1711 else if (is_macro(p))
1712 parse_macro(p, os, context);
1714 else if (t.cs() == "noindent") {
1716 context.add_par_extra_stuff("\\noindent\n");
1719 else if (t.cs() == "appendix") {
1720 context.add_par_extra_stuff("\\start_of_appendix\n");
1721 // We need to start a new paragraph. Otherwise the
1722 // appendix in 'bla\appendix\chapter{' would start
1724 context.new_paragraph(os);
1725 // We need to make sure that the paragraph is
1726 // generated even if it is empty. Otherwise the
1727 // appendix in '\par\appendix\par\chapter{' would
1729 context.check_layout(os);
1730 // FIXME: This is a hack to prevent paragraph
1731 // deletion if it is empty. Handle this better!
1733 "%dummy comment inserted by tex2lyx to "
1734 "ensure that this paragraph is not empty",
1736 // Both measures above may generate an additional
1737 // empty paragraph, but that does not hurt, because
1738 // whitespace does not matter here.
1739 eat_whitespace(p, os, context, true);
1742 // Starred section headings
1743 // Must attempt to parse "Section*" before "Section".
1744 else if ((p.next_token().asInput() == "*") &&
1745 context.new_layout_allowed &&
1746 (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
1747 newlayout->isCommand()) {
1750 output_command_layout(os, p, outer, context, newlayout);
1754 // Section headings and the like
1755 else if (context.new_layout_allowed &&
1756 (newlayout = findLayout(context.textclass, t.cs())) &&
1757 newlayout->isCommand()) {
1759 output_command_layout(os, p, outer, context, newlayout);
1763 else if (t.cs() == "caption") {
1765 context.check_layout(os);
1767 begin_inset(os, "Caption\n\n");
1768 Context newcontext(true, context.textclass);
1769 newcontext.font = context.font;
1770 newcontext.check_layout(os);
1771 if (p.next_token().cat() != catEscape &&
1772 p.next_token().character() == '[') {
1773 p.get_token(); // eat '['
1774 begin_inset(os, "OptArg\n");
1775 os << "status collapsed\n";
1776 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
1778 eat_whitespace(p, os, context, false);
1780 parse_text(p, os, FLAG_ITEM, outer, context);
1781 context.check_end_layout(os);
1782 // We don't need really a new paragraph, but
1783 // we must make sure that the next item gets a \begin_layout.
1784 context.new_paragraph(os);
1787 newcontext.check_end_layout(os);
1790 else if (t.cs() == "includegraphics") {
1791 bool const clip = p.next_token().asInput() == "*";
1794 string const arg = p.getArg('[', ']');
1795 map<string, string> opts;
1796 vector<string> keys;
1797 split_map(arg, opts, keys);
1799 opts["clip"] = string();
1800 string name = normalize_filename(p.verbatim_item());
1802 string const path = getMasterFilePath();
1803 // We want to preserve relative / absolute filenames,
1804 // therefore path is only used for testing
1805 if (!makeAbsPath(name, path).exists()) {
1806 // The file extension is probably missing.
1807 // Now try to find it out.
1808 string const dvips_name =
1809 find_file(name, path,
1810 known_dvips_graphics_formats);
1811 string const pdftex_name =
1812 find_file(name, path,
1813 known_pdftex_graphics_formats);
1814 if (!dvips_name.empty()) {
1815 if (!pdftex_name.empty()) {
1816 cerr << "This file contains the "
1818 "\"\\includegraphics{"
1820 "However, files\n\""
1821 << dvips_name << "\" and\n\""
1822 << pdftex_name << "\"\n"
1823 "both exist, so I had to make a "
1824 "choice and took the first one.\n"
1825 "Please move the unwanted one "
1826 "someplace else and try again\n"
1827 "if my choice was wrong."
1831 } else if (!pdftex_name.empty()) {
1837 if (makeAbsPath(name, path).exists())
1838 fix_relative_filename(name);
1840 cerr << "Warning: Could not find graphics file '"
1841 << name << "'." << endl;
1843 context.check_layout(os);
1844 begin_inset(os, "Graphics ");
1845 os << "\n\tfilename " << name << '\n';
1846 if (opts.find("width") != opts.end())
1848 << translate_len(opts["width"]) << '\n';
1849 if (opts.find("height") != opts.end())
1851 << translate_len(opts["height"]) << '\n';
1852 if (opts.find("scale") != opts.end()) {
1853 istringstream iss(opts["scale"]);
1857 os << "\tscale " << val << '\n';
1859 if (opts.find("angle") != opts.end()) {
1860 os << "\trotateAngle "
1861 << opts["angle"] << '\n';
1862 vector<string>::const_iterator a =
1863 find(keys.begin(), keys.end(), "angle");
1864 vector<string>::const_iterator s =
1865 find(keys.begin(), keys.end(), "width");
1866 if (s == keys.end())
1867 s = find(keys.begin(), keys.end(), "height");
1868 if (s == keys.end())
1869 s = find(keys.begin(), keys.end(), "scale");
1870 if (s != keys.end() && distance(s, a) > 0)
1871 os << "\tscaleBeforeRotation\n";
1873 if (opts.find("origin") != opts.end()) {
1875 string const opt = opts["origin"];
1876 if (opt.find('l') != string::npos) ss << "left";
1877 if (opt.find('r') != string::npos) ss << "right";
1878 if (opt.find('c') != string::npos) ss << "center";
1879 if (opt.find('t') != string::npos) ss << "Top";
1880 if (opt.find('b') != string::npos) ss << "Bottom";
1881 if (opt.find('B') != string::npos) ss << "Baseline";
1882 if (!ss.str().empty())
1883 os << "\trotateOrigin " << ss.str() << '\n';
1885 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1887 if (opts.find("keepaspectratio") != opts.end())
1888 os << "\tkeepAspectRatio\n";
1889 if (opts.find("clip") != opts.end())
1891 if (opts.find("draft") != opts.end())
1893 if (opts.find("bb") != opts.end())
1894 os << "\tBoundingBox "
1895 << opts["bb"] << '\n';
1896 int numberOfbbOptions = 0;
1897 if (opts.find("bbllx") != opts.end())
1898 numberOfbbOptions++;
1899 if (opts.find("bblly") != opts.end())
1900 numberOfbbOptions++;
1901 if (opts.find("bburx") != opts.end())
1902 numberOfbbOptions++;
1903 if (opts.find("bbury") != opts.end())
1904 numberOfbbOptions++;
1905 if (numberOfbbOptions == 4)
1906 os << "\tBoundingBox "
1907 << opts["bbllx"] << " " << opts["bblly"] << " "
1908 << opts["bburx"] << " " << opts["bbury"] << '\n';
1909 else if (numberOfbbOptions > 0)
1910 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1911 numberOfbbOptions = 0;
1912 if (opts.find("natwidth") != opts.end())
1913 numberOfbbOptions++;
1914 if (opts.find("natheight") != opts.end())
1915 numberOfbbOptions++;
1916 if (numberOfbbOptions == 2)
1917 os << "\tBoundingBox 0bp 0bp "
1918 << opts["natwidth"] << " " << opts["natheight"] << '\n';
1919 else if (numberOfbbOptions > 0)
1920 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1921 ostringstream special;
1922 if (opts.find("hiresbb") != opts.end())
1923 special << "hiresbb,";
1924 if (opts.find("trim") != opts.end())
1926 if (opts.find("viewport") != opts.end())
1927 special << "viewport=" << opts["viewport"] << ',';
1928 if (opts.find("totalheight") != opts.end())
1929 special << "totalheight=" << opts["totalheight"] << ',';
1930 if (opts.find("type") != opts.end())
1931 special << "type=" << opts["type"] << ',';
1932 if (opts.find("ext") != opts.end())
1933 special << "ext=" << opts["ext"] << ',';
1934 if (opts.find("read") != opts.end())
1935 special << "read=" << opts["read"] << ',';
1936 if (opts.find("command") != opts.end())
1937 special << "command=" << opts["command"] << ',';
1938 string s_special = special.str();
1939 if (!s_special.empty()) {
1940 // We had special arguments. Remove the trailing ','.
1941 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1943 // TODO: Handle the unknown settings better.
1944 // Warn about invalid options.
1945 // Check whether some option was given twice.
1949 else if (t.cs() == "footnote" ||
1950 (t.cs() == "thanks" && context.layout->intitle)) {
1952 context.check_layout(os);
1953 begin_inset(os, "Foot\n");
1954 os << "status collapsed\n\n";
1955 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1959 else if (t.cs() == "marginpar") {
1961 context.check_layout(os);
1962 begin_inset(os, "Marginal\n");
1963 os << "status collapsed\n\n";
1964 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1968 else if (t.cs() == "ensuremath") {
1970 context.check_layout(os);
1971 string const s = p.verbatim_item();
1972 //FIXME: this never triggers in UTF8
1973 if (s == "\xb1" || s == "\xb3" || s == "\xb2" || s == "\xb5")
1976 handle_ert(os, "\\ensuremath{" + s + "}",
1980 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1981 // FIXME: Somehow prevent title layouts if
1982 // "maketitle" was not found
1984 skip_spaces_braces(p);
1987 else if (t.cs() == "tableofcontents") {
1988 context.check_layout(os);
1989 begin_command_inset(os, "toc", "tableofcontents");
1991 skip_spaces_braces(p);
1994 else if (t.cs() == "listoffigures") {
1995 context.check_layout(os);
1996 begin_inset(os, "FloatList figure\n");
1998 skip_spaces_braces(p);
2001 else if (t.cs() == "listoftables") {
2002 context.check_layout(os);
2003 begin_inset(os, "FloatList table\n");
2005 skip_spaces_braces(p);
2008 else if (t.cs() == "listof") {
2009 p.skip_spaces(true);
2010 string const name = p.get_token().cs();
2011 if (context.textclass.floats().typeExist(name)) {
2012 context.check_layout(os);
2013 begin_inset(os, "FloatList ");
2016 p.get_token(); // swallow second arg
2018 handle_ert(os, "\\listof{" + name + "}", context);
2021 else if (t.cs() == "textrm")
2022 parse_text_attributes(p, os, FLAG_ITEM, outer,
2023 context, "\\family",
2024 context.font.family, "roman");
2026 else if (t.cs() == "textsf")
2027 parse_text_attributes(p, os, FLAG_ITEM, outer,
2028 context, "\\family",
2029 context.font.family, "sans");
2031 else if (t.cs() == "texttt")
2032 parse_text_attributes(p, os, FLAG_ITEM, outer,
2033 context, "\\family",
2034 context.font.family, "typewriter");
2036 else if (t.cs() == "textmd")
2037 parse_text_attributes(p, os, FLAG_ITEM, outer,
2038 context, "\\series",
2039 context.font.series, "medium");
2041 else if (t.cs() == "textbf")
2042 parse_text_attributes(p, os, FLAG_ITEM, outer,
2043 context, "\\series",
2044 context.font.series, "bold");
2046 else if (t.cs() == "textup")
2047 parse_text_attributes(p, os, FLAG_ITEM, outer,
2049 context.font.shape, "up");
2051 else if (t.cs() == "textit")
2052 parse_text_attributes(p, os, FLAG_ITEM, outer,
2054 context.font.shape, "italic");
2056 else if (t.cs() == "textsl")
2057 parse_text_attributes(p, os, FLAG_ITEM, outer,
2059 context.font.shape, "slanted");
2061 else if (t.cs() == "textsc")
2062 parse_text_attributes(p, os, FLAG_ITEM, outer,
2064 context.font.shape, "smallcaps");
2066 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
2067 context.check_layout(os);
2068 TeXFont oldFont = context.font;
2069 context.font.init();
2070 context.font.size = oldFont.size;
2071 os << "\n\\family " << context.font.family << "\n";
2072 os << "\n\\series " << context.font.series << "\n";
2073 os << "\n\\shape " << context.font.shape << "\n";
2074 if (t.cs() == "textnormal") {
2075 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2076 output_font_change(os, context.font, oldFont);
2077 context.font = oldFont;
2079 eat_whitespace(p, os, context, false);
2082 else if (t.cs() == "textcolor") {
2083 // scheme is \textcolor{color name}{text}
2084 string const color = p.verbatim_item();
2085 // we only support the predefined colors of the color package
2086 if (color == "black" || color == "blue" || color == "cyan"
2087 || color == "green" || color == "magenta" || color == "red"
2088 || color == "white" || color == "yellow") {
2089 context.check_layout(os);
2090 os << "\n\\color " << color << "\n";
2091 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2092 context.check_layout(os);
2093 os << "\n\\color inherit\n";
2095 // for custom defined colors
2096 handle_ert(os, t.asInput() + "{" + color + "}", context);
2099 else if (t.cs() == "underbar") {
2100 // Do NOT handle \underline.
2101 // \underbar cuts through y, g, q, p etc.,
2102 // \underline does not.
2103 context.check_layout(os);
2104 os << "\n\\bar under\n";
2105 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2106 context.check_layout(os);
2107 os << "\n\\bar default\n";
2110 else if (t.cs() == "emph" || t.cs() == "noun") {
2111 context.check_layout(os);
2112 os << "\n\\" << t.cs() << " on\n";
2113 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
2114 context.check_layout(os);
2115 os << "\n\\" << t.cs() << " default\n";
2118 else if (t.cs() == "lyxline") {
2119 context.check_layout(os);
2123 else if (is_known(t.cs(), known_phrases)) {
2124 // FIXME: This needs to be changed when bug 4752 is fixed.
2125 char const * const * where = is_known(t.cs(), known_phrases);
2126 context.check_layout(os);
2127 os << known_coded_phrases[where - known_phrases];
2128 skip_spaces_braces(p);
2131 else if (is_known(t.cs(), known_ref_commands)) {
2132 context.check_layout(os);
2133 begin_command_inset(os, "ref", t.cs());
2134 // LyX cannot handle newlines in a latex command
2135 // FIXME: Move the substitution into parser::getOpt()?
2136 os << subst(p.getOpt(), "\n", " ");
2137 os << "reference " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2141 else if (use_natbib &&
2142 is_known(t.cs(), known_natbib_commands) &&
2143 ((t.cs() != "citefullauthor" &&
2144 t.cs() != "citeyear" &&
2145 t.cs() != "citeyearpar") ||
2146 p.next_token().asInput() != "*")) {
2147 context.check_layout(os);
2148 string command = t.cs();
2149 if (p.next_token().asInput() == "*") {
2153 if (command == "citefullauthor")
2154 // alternative name for "\\citeauthor*"
2155 command = "citeauthor*";
2157 // text before the citation
2159 // text after the citation
2161 get_cite_arguments(p, true, before, after);
2163 if (command == "cite") {
2164 // \cite without optional argument means
2165 // \citet, \cite with at least one optional
2166 // argument means \citep.
2167 if (before.empty() && after.empty())
2172 if (before.empty() && after == "[]")
2173 // avoid \citet[]{a}
2175 else if (before == "[]" && after == "[]") {
2176 // avoid \citet[][]{a}
2180 // remove the brackets around after and before
2181 if (!after.empty()) {
2183 after.erase(after.length() - 1, 1);
2184 // LyX cannot handle newlines in the parameter
2185 after = subst(after, "\n", " ");
2187 if (!before.empty()) {
2189 before.erase(before.length() - 1, 1);
2190 // LyX cannot handle newlines in the parameter
2191 before = subst(before, "\n", " ");
2193 begin_command_inset(os, "citation", command);
2194 os << "after " << '"' << after << '"' << "\n";
2195 os << "before " << '"' << before << '"' << "\n";
2196 os << "key " << '"' << p.verbatim_item() << '"' << "\n";
2200 else if (use_jurabib &&
2201 is_known(t.cs(), known_jurabib_commands) &&
2202 (t.cs() == "cite" || p.next_token().asInput() != "*")) {
2203 context.check_layout(os);
2204 string command = t.cs();
2205 if (p.next_token().asInput() == "*") {
2209 char argumentOrder = '\0';
2210 vector<string> const & options = used_packages["jurabib"];
2211 if (find(options.begin(), options.end(),
2212 "natbiborder") != options.end())
2213 argumentOrder = 'n';
2214 else if (find(options.begin(), options.end(),
2215 "jurabiborder") != options.end())
2216 argumentOrder = 'j';
2218 // text before the citation
2220 // text after the citation
2222 get_cite_arguments(p, argumentOrder != 'j', before, after);
2224 string const citation = p.verbatim_item();
2225 if (!before.empty() && argumentOrder == '\0') {
2226 cerr << "Warning: Assuming argument order "
2227 "of jurabib version 0.6 for\n'"
2228 << command << before << after << '{'
2229 << citation << "}'.\n"
2230 "Add 'jurabiborder' to the jurabib "
2231 "package options if you used an\n"
2232 "earlier jurabib version." << endl;
2234 if (!after.empty()) {
2236 after.erase(after.length() - 1, 1);
2238 if (!before.empty()) {
2240 before.erase(before.length() - 1, 1);
2242 begin_command_inset(os, "citation", command);
2243 os << "after " << '"' << after << '"' << "\n";
2244 os << "before " << '"' << before << '"' << "\n";
2245 os << "key " << '"' << citation << '"' << "\n";
2249 else if (t.cs() == "cite"
2250 || t.cs() == "nocite") {
2251 context.check_layout(os);
2252 // LyX cannot handle newlines in a latex command
2253 string after = subst(p.getOptContent(), "\n", " ");
2254 string key = subst(p.verbatim_item(), "\n", " ");
2255 // store the case that it is "\nocite{*}" to use it later for
2258 begin_command_inset(os, "citation", t.cs());
2259 os << "after " << '"' << after << '"' << "\n";
2260 os << "key " << '"' << key << '"' << "\n";
2262 } else if (t.cs() == "nocite")
2266 else if (t.cs() == "index") {
2267 context.check_layout(os);
2268 begin_inset(os, "Index\n");
2269 os << "status collapsed\n";
2270 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2274 else if (t.cs() == "nomenclature") {
2275 context.check_layout(os);
2276 begin_command_inset(os, "nomenclature", "nomenclature");
2277 // LyX cannot handle newlines in a latex command
2278 string prefix = subst(p.getOptContent(), "\n", " ");
2279 if (!prefix.empty())
2280 os << "prefix " << '"' << prefix << '"' << "\n";
2281 os << "symbol " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2282 os << "description " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2286 else if (t.cs() == "label") {
2287 context.check_layout(os);
2288 begin_command_inset(os, "label", "label");
2289 // LyX cannot handle newlines in a latex command
2290 os << "name " << '"' << subst(p.verbatim_item(), "\n", " ") << '"' << "\n";
2294 else if (t.cs() == "printindex") {
2295 context.check_layout(os);
2296 begin_command_inset(os, "index_print", "printindex");
2298 skip_spaces_braces(p);
2301 else if (t.cs() == "printnomenclature") {
2302 context.check_layout(os);
2303 begin_command_inset(os, "nomencl_print", "printnomenclature");
2305 skip_spaces_braces(p);
2308 else if (t.cs() == "url") {
2309 context.check_layout(os);
2310 begin_inset(os, "Flex URL\n");
2311 os << "status collapsed\n";
2312 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2316 else if (LYX_FORMAT >= 408 &&
2317 (t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
2318 context.check_layout(os);
2319 begin_inset(os, "script ");
2320 os << t.cs().substr(4) << '\n';
2321 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
2325 else if (is_known(t.cs(), known_quotes)) {
2326 char const * const * where = is_known(t.cs(), known_quotes);
2327 context.check_layout(os);
2328 begin_inset(os, "Quotes ");
2329 os << known_coded_quotes[where - known_quotes];
2331 // LyX adds {} after the quote, so we have to eat
2332 // spaces here if there are any before a possible
2334 eat_whitespace(p, os, context, false);
2338 else if (is_known(t.cs(), known_sizes) &&
2339 context.new_layout_allowed) {
2340 char const * const * where = is_known(t.cs(), known_sizes);
2341 context.check_layout(os);
2342 TeXFont const oldFont = context.font;
2343 context.font.size = known_coded_sizes[where - known_sizes];
2344 output_font_change(os, oldFont, context.font);
2345 eat_whitespace(p, os, context, false);
2348 else if (is_known(t.cs(), known_font_families) &&
2349 context.new_layout_allowed) {
2350 char const * const * where =
2351 is_known(t.cs(), known_font_families);
2352 context.check_layout(os);
2353 TeXFont const oldFont = context.font;
2354 context.font.family =
2355 known_coded_font_families[where - known_font_families];
2356 output_font_change(os, oldFont, context.font);
2357 eat_whitespace(p, os, context, false);
2360 else if (is_known(t.cs(), known_font_series) &&
2361 context.new_layout_allowed) {
2362 char const * const * where =
2363 is_known(t.cs(), known_font_series);
2364 context.check_layout(os);
2365 TeXFont const oldFont = context.font;
2366 context.font.series =
2367 known_coded_font_series[where - known_font_series];
2368 output_font_change(os, oldFont, context.font);
2369 eat_whitespace(p, os, context, false);
2372 else if (is_known(t.cs(), known_font_shapes) &&
2373 context.new_layout_allowed) {
2374 char const * const * where =
2375 is_known(t.cs(), known_font_shapes);
2376 context.check_layout(os);
2377 TeXFont const oldFont = context.font;
2378 context.font.shape =
2379 known_coded_font_shapes[where - known_font_shapes];
2380 output_font_change(os, oldFont, context.font);
2381 eat_whitespace(p, os, context, false);
2383 else if (is_known(t.cs(), known_old_font_families) &&
2384 context.new_layout_allowed) {
2385 char const * const * where =
2386 is_known(t.cs(), known_old_font_families);
2387 context.check_layout(os);
2388 TeXFont const oldFont = context.font;
2389 context.font.init();
2390 context.font.size = oldFont.size;
2391 context.font.family =
2392 known_coded_font_families[where - known_old_font_families];
2393 output_font_change(os, oldFont, context.font);
2394 eat_whitespace(p, os, context, false);
2397 else if (is_known(t.cs(), known_old_font_series) &&
2398 context.new_layout_allowed) {
2399 char const * const * where =
2400 is_known(t.cs(), known_old_font_series);
2401 context.check_layout(os);
2402 TeXFont const oldFont = context.font;
2403 context.font.init();
2404 context.font.size = oldFont.size;
2405 context.font.series =
2406 known_coded_font_series[where - known_old_font_series];
2407 output_font_change(os, oldFont, context.font);
2408 eat_whitespace(p, os, context, false);
2411 else if (is_known(t.cs(), known_old_font_shapes) &&
2412 context.new_layout_allowed) {
2413 char const * const * where =
2414 is_known(t.cs(), known_old_font_shapes);
2415 context.check_layout(os);
2416 TeXFont const oldFont = context.font;
2417 context.font.init();
2418 context.font.size = oldFont.size;
2419 context.font.shape =
2420 known_coded_font_shapes[where - known_old_font_shapes];
2421 output_font_change(os, oldFont, context.font);
2422 eat_whitespace(p, os, context, false);
2425 else if (t.cs() == "selectlanguage") {
2426 context.check_layout(os);
2427 // save the language for the case that a
2428 // \foreignlanguage is used
2430 // FIXME: \lang needs a LyX name, but we set a LaTeX name
2431 context.font.language = subst(p.verbatim_item(), "\n", " ");
2432 os << "\\lang " << context.font.language << "\n";
2435 else if (t.cs() == "foreignlanguage") {
2436 context.check_layout(os);
2437 // FIXME: \lang needs a LyX name, but we set a LaTeX name
2438 os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
2439 os << subst(p.verbatim_item(), "\n", " ");
2440 // FIXME: the second argument of selectlanguage
2441 // has to be parsed (like for \textsf, for
2443 // set back to last selectlanguage
2444 os << "\n\\lang " << context.font.language << "\n";
2447 else if (t.cs() == "inputencoding") {
2448 // nothing to write here
2449 string const enc = subst(p.verbatim_item(), "\n", " ");
2453 else if (t.cs() == "ldots") {
2454 context.check_layout(os);
2455 os << "\\SpecialChar \\ldots{}\n";
2456 skip_spaces_braces(p);
2459 else if (t.cs() == "lyxarrow") {
2460 context.check_layout(os);
2461 os << "\\SpecialChar \\menuseparator\n";
2462 skip_spaces_braces(p);
2465 else if (t.cs() == "textcompwordmark") {
2466 context.check_layout(os);
2467 os << "\\SpecialChar \\textcompwordmark{}\n";
2468 skip_spaces_braces(p);
2471 else if (t.cs() == "slash") {
2472 context.check_layout(os);
2473 os << "\\SpecialChar \\slash{}\n";
2474 skip_spaces_braces(p);
2477 else if (t.cs() == "nobreakdash" && p.next_token().asInput() == "-") {
2478 context.check_layout(os);
2479 os << "\\SpecialChar \\nobreakdash-\n";
2483 else if (t.cs() == "textquotedbl") {
2484 context.check_layout(os);
2489 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
2490 context.check_layout(os);
2491 os << "\\SpecialChar \\@.\n";
2495 else if (t.cs() == "-") {
2496 context.check_layout(os);
2497 os << "\\SpecialChar \\-\n";
2500 else if (t.cs() == "textasciitilde") {
2501 context.check_layout(os);
2503 skip_spaces_braces(p);
2506 else if (t.cs() == "textasciicircum") {
2507 context.check_layout(os);
2509 skip_spaces_braces(p);
2512 else if (t.cs() == "textbackslash") {
2513 context.check_layout(os);
2514 os << "\n\\backslash\n";
2515 skip_spaces_braces(p);
2518 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
2519 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
2521 context.check_layout(os);
2525 else if (t.cs() == "char") {
2526 context.check_layout(os);
2527 if (p.next_token().character() == '`') {
2529 if (p.next_token().cs() == "\"") {
2534 handle_ert(os, "\\char`", context);
2537 handle_ert(os, "\\char", context);
2541 else if (t.cs() == "verb") {
2542 context.check_layout(os);
2543 char const delimiter = p.next_token().character();
2544 string const arg = p.getArg(delimiter, delimiter);
2546 oss << "\\verb" << delimiter << arg << delimiter;
2547 handle_ert(os, oss.str(), context);
2550 // Problem: \= creates a tabstop inside the tabbing environment
2551 // and else an accent. In the latter case we really would want
2552 // \={o} instead of \= o.
2553 else if (t.cs() == "=" && (flags & FLAG_TABBING))
2554 handle_ert(os, t.asInput(), context);
2556 // accents (see Table 6 in Comprehensive LaTeX Symbol List)
2557 else if (t.cs().size() == 1
2558 && contains("\"'.=^`bcdHkrtuv~", t.cs())) {
2559 context.check_layout(os);
2560 // try to see whether the string is in unicodesymbols
2562 string command = t.asInput() + "{"
2563 + trim(p.verbatim_item())
2565 docstring s = encodings.fromLaTeXCommand(from_utf8(command), rem);
2568 cerr << "When parsing " << command
2569 << ", result is " << to_utf8(s)
2570 << "+" << to_utf8(rem) << endl;
2573 // we did not find a non-ert version
2574 handle_ert(os, command, context);
2577 else if (t.cs() == "\\") {
2578 context.check_layout(os);
2580 handle_ert(os, "\\\\" + p.getOpt(), context);
2581 else if (p.next_token().asInput() == "*") {
2583 // getOpt() eats the following space if there
2584 // is no optional argument, but that is OK
2585 // here since it has no effect in the output.
2586 handle_ert(os, "\\\\*" + p.getOpt(), context);
2589 begin_inset(os, "Newline newline");
2594 else if (t.cs() == "newline" ||
2595 (t.cs() == "linebreak" && !p.hasOpt())) {
2596 context.check_layout(os);
2597 begin_inset(os, "Newline ");
2600 skip_spaces_braces(p);
2603 else if (t.cs() == "input" || t.cs() == "include"
2604 || t.cs() == "verbatiminput") {
2605 string name = t.cs();
2606 if (t.cs() == "verbatiminput"
2607 && p.next_token().asInput() == "*")
2608 name += p.get_token().asInput();
2609 context.check_layout(os);
2610 string filename(normalize_filename(p.getArg('{', '}')));
2611 string const path = getMasterFilePath();
2612 // We want to preserve relative / absolute filenames,
2613 // therefore path is only used for testing
2614 if ((t.cs() == "include" || t.cs() == "input") &&
2615 !makeAbsPath(filename, path).exists()) {
2616 // The file extension is probably missing.
2617 // Now try to find it out.
2618 string const tex_name =
2619 find_file(filename, path,
2620 known_tex_extensions);
2621 if (!tex_name.empty())
2622 filename = tex_name;
2624 bool external = false;
2626 if (makeAbsPath(filename, path).exists()) {
2627 string const abstexname =
2628 makeAbsPath(filename, path).absFileName();
2629 string const abslyxname =
2630 changeExtension(abstexname, ".lyx");
2631 string const absfigname =
2632 changeExtension(abstexname, ".fig");
2633 fix_relative_filename(filename);
2634 string const lyxname =
2635 changeExtension(filename, ".lyx");
2637 external = FileName(absfigname).exists();
2638 if (t.cs() == "input") {
2639 string const ext = getExtension(abstexname);
2641 // Combined PS/LaTeX:
2642 // x.eps, x.pstex_t (old xfig)
2643 // x.pstex, x.pstex_t (new xfig, e.g. 3.2.5)
2644 FileName const absepsname(
2645 changeExtension(abstexname, ".eps"));
2646 FileName const abspstexname(
2647 changeExtension(abstexname, ".pstex"));
2648 bool const xfigeps =
2649 (absepsname.exists() ||
2650 abspstexname.exists()) &&
2653 // Combined PDF/LaTeX:
2654 // x.pdf, x.pdftex_t (old xfig)
2655 // x.pdf, x.pdf_t (new xfig, e.g. 3.2.5)
2656 FileName const abspdfname(
2657 changeExtension(abstexname, ".pdf"));
2658 bool const xfigpdf =
2659 abspdfname.exists() &&
2660 (ext == "pdftex_t" || ext == "pdf_t");
2664 // Combined PS/PDF/LaTeX:
2665 // x_pspdftex.eps, x_pspdftex.pdf, x.pspdftex
2666 string const absbase2(
2667 removeExtension(abstexname) + "_pspdftex");
2668 FileName const abseps2name(
2669 addExtension(absbase2, ".eps"));
2670 FileName const abspdf2name(
2671 addExtension(absbase2, ".pdf"));
2672 bool const xfigboth =
2673 abspdf2name.exists() &&
2674 abseps2name.exists() && ext == "pspdftex";
2676 xfig = xfigpdf || xfigeps || xfigboth;
2677 external = external && xfig;
2680 outname = changeExtension(filename, ".fig");
2682 // Don't try to convert, the result
2683 // would be full of ERT.
2685 } else if (t.cs() != "verbatiminput" &&
2686 tex2lyx(abstexname, FileName(abslyxname),
2693 cerr << "Warning: Could not find included file '"
2694 << filename << "'." << endl;
2698 begin_inset(os, "External\n");
2699 os << "\ttemplate XFig\n"
2700 << "\tfilename " << outname << '\n';
2702 begin_command_inset(os, "include", name);
2703 os << "preview false\n"
2704 "filename \"" << outname << "\"\n";
2709 else if (t.cs() == "bibliographystyle") {
2710 // store new bibliographystyle
2711 bibliographystyle = p.verbatim_item();
2712 // If any other command than \bibliography and
2713 // \nocite{*} follows, we need to output the style
2714 // (because it might be used by that command).
2715 // Otherwise, it will automatically be output by LyX.
2718 for (Token t2 = p.get_token(); p.good(); t2 = p.get_token()) {
2719 if (t2.cat() == catBegin)
2721 if (t2.cat() != catEscape)
2723 if (t2.cs() == "nocite") {
2724 if (p.getArg('{', '}') == "*")
2726 } else if (t2.cs() == "bibliography")
2733 "\\bibliographystyle{" + bibliographystyle + '}',
2738 else if (t.cs() == "bibliography") {
2739 context.check_layout(os);
2740 begin_command_inset(os, "bibtex", "bibtex");
2741 if (!btprint.empty()) {
2742 os << "btprint " << '"' << "btPrintAll" << '"' << "\n";
2743 // clear the string because the next BibTeX inset can be without the
2744 // \nocite{*} option
2747 os << "bibfiles " << '"' << p.verbatim_item() << '"' << "\n";
2748 // Do we have a bibliographystyle set?
2749 if (!bibliographystyle.empty())
2750 os << "options " << '"' << bibliographystyle << '"' << "\n";
2754 else if (t.cs() == "parbox")
2755 parse_box(p, os, FLAG_ITEM, outer, context, true);
2757 //\makebox() is part of the picture environment and different from \makebox{}
2758 //\makebox{} will be parsed by parse_box when bug 2956 is fixed
2759 else if (t.cs() == "makebox") {
2760 string arg = t.asInput();
2761 if (p.next_token().character() == '(')
2762 //the syntax is: \makebox(x,y)[position]{content}
2763 arg += p.getFullParentheseArg();
2765 //the syntax is: \makebox[width][position]{content}
2766 arg += p.getFullOpt();
2767 handle_ert(os, arg + p.getFullOpt(), context);
2770 else if (t.cs() == "smallskip" ||
2771 t.cs() == "medskip" ||
2772 t.cs() == "bigskip" ||
2773 t.cs() == "vfill") {
2774 context.check_layout(os);
2775 begin_inset(os, "VSpace ");
2778 skip_spaces_braces(p);
2781 else if (is_known(t.cs(), known_spaces)) {
2782 char const * const * where = is_known(t.cs(), known_spaces);
2783 context.check_layout(os);
2784 begin_inset(os, "space ");
2785 os << '\\' << known_coded_spaces[where - known_spaces]
2788 // LaTeX swallows whitespace after all spaces except
2789 // "\\,". We have to do that here, too, because LyX
2790 // adds "{}" which would make the spaces significant.
2792 eat_whitespace(p, os, context, false);
2793 // LyX adds "{}" after all spaces except "\\ " and
2794 // "\\,", so we have to remove "{}".
2795 // "\\,{}" is equivalent to "\\," in LaTeX, so we
2796 // remove the braces after "\\,", too.
2801 else if (t.cs() == "newpage" ||
2802 (t.cs() == "pagebreak" && !p.hasOpt()) ||
2803 t.cs() == "clearpage" ||
2804 t.cs() == "cleardoublepage") {
2805 context.check_layout(os);
2806 begin_inset(os, "Newpage ");
2809 skip_spaces_braces(p);
2812 else if (t.cs() == "newcommand" ||
2813 t.cs() == "providecommand" ||
2814 t.cs() == "renewcommand") {
2815 // providecommand could be handled by parse_command(),
2816 // but we need to call add_known_command() here.
2817 string name = t.asInput();
2818 if (p.next_token().asInput() == "*") {
2819 // Starred form. Eat '*'
2823 string const command = p.verbatim_item();
2824 string const opt1 = p.getOpt();
2825 string const opt2 = p.getFullOpt();
2826 add_known_command(command, opt1, !opt2.empty());
2827 string const ert = name + '{' + command + '}' +
2829 '{' + p.verbatim_item() + '}';
2831 if (t.cs() == "providecommand")
2832 handle_ert(os, ert, context);
2834 context.check_layout(os);
2835 begin_inset(os, "FormulaMacro");
2841 else if (t.cs() == "let" && p.next_token().asInput() != "*") {
2842 // let could be handled by parse_command(),
2843 // but we need to call add_known_command() here.
2844 string ert = t.asInput();
2847 if (p.next_token().cat() == catBegin) {
2848 name = p.verbatim_item();
2849 ert += '{' + name + '}';
2851 name = p.verbatim_item();
2856 if (p.next_token().cat() == catBegin) {
2857 command = p.verbatim_item();
2858 ert += '{' + command + '}';
2860 command = p.verbatim_item();
2863 // If command is known, make name known too, to parse
2864 // its arguments correctly. For this reason we also
2865 // have commands in syntax.default that are hardcoded.
2866 CommandMap::iterator it = known_commands.find(command);
2867 if (it != known_commands.end())
2868 known_commands[t.asInput()] = it->second;
2869 handle_ert(os, ert, context);
2872 else if (t.cs() == "hspace" || t.cs() == "vspace") {
2873 bool starred = false;
2874 if (p.next_token().asInput() == "*") {
2878 string name = t.asInput();
2879 string const length = p.verbatim_item();
2882 bool valid = splitLatexLength(length, valstring, unit);
2883 bool known_hspace = false;
2884 bool known_vspace = false;
2885 bool known_unit = false;
2888 istringstream iss(valstring);
2891 if (t.cs()[0] == 'h') {
2892 if (unit == "\\fill") {
2897 known_hspace = true;
2900 if (unit == "\\smallskipamount") {
2902 known_vspace = true;
2903 } else if (unit == "\\medskipamount") {
2905 known_vspace = true;
2906 } else if (unit == "\\bigskipamount") {
2908 known_vspace = true;
2909 } else if (unit == "\\fill") {
2911 known_vspace = true;
2915 if (!known_hspace && !known_vspace) {
2916 switch (unitFromString(unit)) {
2937 if (t.cs()[0] == 'h' && (known_unit || known_hspace)) {
2938 // Literal horizontal length or known variable
2939 context.check_layout(os);
2940 begin_inset(os, "space ");
2948 if (known_unit && !known_hspace)
2950 << translate_len(length);
2952 } else if (known_unit || known_vspace) {
2953 // Literal vertical length or known variable
2954 context.check_layout(os);
2955 begin_inset(os, "VSpace ");
2963 // LyX can't handle other length variables in Inset VSpace/space
2968 handle_ert(os, name + '{' + unit + '}', context);
2969 else if (value == -1.0)
2970 handle_ert(os, name + "{-" + unit + '}', context);
2972 handle_ert(os, name + '{' + valstring + unit + '}', context);
2974 handle_ert(os, name + '{' + length + '}', context);
2979 // try to see whether the string is in unicodesymbols
2980 // Only use text mode commands, since we are in text mode here,
2981 // and math commands may be invalid (bug 6797)
2983 docstring s = encodings.fromLaTeXCommand(from_utf8(t.asInput()),
2984 rem, Encodings::TEXT_CMD);
2987 cerr << "When parsing " << t.cs()
2988 << ", result is " << to_utf8(s)
2989 << "+" << to_utf8(rem) << endl;
2990 context.check_layout(os);
2992 skip_spaces_braces(p);
2994 //cerr << "#: " << t << " mode: " << mode << endl;
2995 // heuristic: read up to next non-nested space
2997 string s = t.asInput();
2998 string z = p.verbatim_item();
2999 while (p.good() && z != " " && z.size()) {
3000 //cerr << "read: " << z << endl;
3002 z = p.verbatim_item();
3004 cerr << "found ERT: " << s << endl;
3005 handle_ert(os, s + ' ', context);
3008 string name = t.asInput();
3009 if (p.next_token().asInput() == "*") {
3010 // Starred commands like \vspace*{}
3011 p.get_token(); // Eat '*'
3014 if (!parse_command(name, p, os, outer, context))
3015 handle_ert(os, name, context);
3019 if (flags & FLAG_LEAVE) {
3020 flags &= ~FLAG_LEAVE;