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
9 * Full author contact details are available in file CREDITS.
18 #include "FloatList.h"
19 #include "lengthcommon.h"
20 #include "support/lstrings.h"
21 #include "support/convert.h"
22 #include "support/filetools.h"
24 #include <boost/filesystem/operations.hpp>
25 #include <boost/tuple/tuple.hpp>
32 using lyx::support::MakeAbsPath;
33 using lyx::support::rtrim;
34 using lyx::support::suffixIs;
35 using lyx::support::contains;
36 using lyx::support::subst;
43 using std::ostringstream;
44 using std::istringstream;
48 namespace fs = boost::filesystem;
51 /// thin wrapper around parse_text using a string
52 string parse_text(Parser & p, unsigned flags, const bool outer,
56 parse_text(p, os, flags, outer, context);
61 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
64 Context newcontext(true, context.textclass);
65 newcontext.font = context.font;
66 parse_text(p, os, flags, outer, newcontext);
67 newcontext.check_end_layout(os);
71 /// parses a paragraph snippet, useful for example for \emph{...}
72 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
75 Context newcontext(false, context.textclass);
76 newcontext.font = context.font;
77 parse_text(p, os, flags, outer, newcontext);
78 // should not be needed
79 newcontext.check_end_layout(os);
85 char const * const known_latex_commands[] = { "ref", "cite", "label", "index",
86 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
90 * We can't put these into known_latex_commands because the argument order
91 * is reversed in lyx if there are 2 arguments.
92 * The starred forms are also known.
94 char const * const known_natbib_commands[] = { "cite", "citet", "citep",
95 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar",
96 "citefullauthor", "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", 0 };
100 * We can't put these into known_latex_commands because the argument order
101 * is reversed in lyx if there are 2 arguments.
102 * No starred form other than "cite*" known.
104 char const * const known_jurabib_commands[] = { "cite", "citet", "citep",
105 "citealt", "citealp", "citeauthor", "citeyear", "citeyearpar", "fullcite",
106 // jurabib commands not (yet) supported by LyX:
107 // "footcite", "footcitet", "footcitep", "footcitealt", "footcitealp",
108 // "footciteauthor", "footciteyear", "footciteyearpar",
109 "citefield", "citetitle", "cite*", 0 };
111 /// LaTeX names for quotes
112 char const * const known_quotes[] = { "glqq", "grqq", "quotedblbase",
113 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
115 /// the same as known_quotes with .lyx names
116 char const * const known_coded_quotes[] = { "gld", "grd", "gld",
117 "grd", "gls", "fls", "frd", 0};
119 /// LaTeX names for font sizes
120 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
121 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
123 /// the same as known_sizes with .lyx names
124 char const * const known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
125 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
127 /// LaTeX 2.09 names for font families
128 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
130 /// LaTeX names for font families
131 char const * const known_font_families[] = { "rmfamily", "sffamily",
134 /// the same as known_old_font_families and known_font_families with .lyx names
135 char const * const known_coded_font_families[] = { "roman", "sans",
138 /// LaTeX 2.09 names for font series
139 char const * const known_old_font_series[] = { "bf", 0};
141 /// LaTeX names for font series
142 char const * const known_font_series[] = { "bfseries", "mdseries", 0};
144 /// the same as known_old_font_series and known_font_series with .lyx names
145 char const * const known_coded_font_series[] = { "bold", "medium", 0};
147 /// LaTeX 2.09 names for font shapes
148 char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
150 /// LaTeX names for font shapes
151 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
154 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
155 char const * const known_coded_font_shapes[] = { "italic", "slanted",
156 "smallcaps", "up", 0};
159 * Graphics file extensions known by the dvips driver of the graphics package.
160 * These extensions are used to complete the filename of an included
161 * graphics file if it does not contain an extension.
162 * The order must be the same that latex uses to find a file, because we
163 * will use the first extension that matches.
164 * This is only an approximation for the common cases. If we would want to
165 * do it right in all cases, we would need to know which graphics driver is
166 * used and know the extensions of every driver of the graphics package.
168 char const * const known_dvips_graphics_formats[] = {"eps", "ps", "eps.gz",
169 "ps.gz", "eps.Z", "ps.Z", 0};
172 * Graphics file extensions known by the pdftex driver of the graphics package.
173 * \see known_dvips_graphics_formats
175 char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
179 /// splits "x=z, y=b" into a map
180 map<string, string> split_map(string const & s)
182 map<string, string> res;
185 for (size_t i = 0; i < v.size(); ++i) {
186 size_t const pos = v[i].find('=');
187 string const index = v[i].substr(0, pos);
188 string const value = v[i].substr(pos + 1, string::npos);
189 res[trim(index)] = trim(value);
196 * Split a LaTeX length into value and unit.
197 * The latter can be a real unit like "pt", or a latex length variable
198 * like "\textwidth". The unit may contain additional stuff like glue
199 * lengths, but we don't care, because such lengths are ERT anyway.
200 * \return true if \param value and \param unit are valid.
202 bool splitLatexLength(string const & len, string & value, string & unit)
206 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
207 //'4,5' is a valid LaTeX length number. Change it to '4.5'
208 string const length = subst(len, ',', '.');
209 if (i == string::npos)
212 if (len[0] == '\\') {
213 // We had something like \textwidth without a factor
219 value = trim(string(length, 0, i));
223 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
224 if (contains(len, '\\'))
225 unit = trim(string(len, i));
227 unit = lyx::support::lowercase(trim(string(len, i)));
232 /// A simple function to translate a latex length to something lyx can
233 /// understand. Not perfect, but rather best-effort.
234 bool translate_len(string const & length, string & valstring, string & unit)
236 if (!splitLatexLength(length, valstring, unit))
238 // LyX uses percent values
240 istringstream iss(valstring);
245 string const percentval = oss.str();
247 if (unit.empty() || unit[0] != '\\')
249 string::size_type const i = unit.find(' ');
250 string const endlen = (i == string::npos) ? string() : string(unit, i);
251 if (unit == "\\textwidth") {
252 valstring = percentval;
253 unit = "text%" + endlen;
254 } else if (unit == "\\columnwidth") {
255 valstring = percentval;
256 unit = "col%" + endlen;
257 } else if (unit == "\\paperwidth") {
258 valstring = percentval;
259 unit = "page%" + endlen;
260 } else if (unit == "\\linewidth") {
261 valstring = percentval;
262 unit = "line%" + endlen;
263 } else if (unit == "\\paperheight") {
264 valstring = percentval;
265 unit = "pheight%" + endlen;
266 } else if (unit == "\\textheight") {
267 valstring = percentval;
268 unit = "theight%" + endlen;
274 string translate_len(string const & length)
278 if (translate_len(length, value, unit))
280 // If the input is invalid, return what we have.
286 * Translates a LaTeX length into \param value, \param unit and
287 * \param special parts suitable for a box inset.
288 * The difference from translate_len() is that a box inset knows about
289 * some special "units" that are stored in \param special.
291 void translate_box_len(string const & length, string & value, string & unit, string & special)
293 if (translate_len(length, value, unit)) {
294 if (unit == "\\height" || unit == "\\depth" ||
295 unit == "\\totalheight" || unit == "\\width") {
296 special = unit.substr(1);
297 // The unit is not used, but LyX requires a dummy setting
310 * Find a file with basename \p name in path \p path and an extension
313 string find_file(string const & name, string const & path,
314 char const * const * extensions)
316 for (char const * const * what = extensions; *what; ++what) {
317 // We don't use ChangeExtension() because it does the wrong
318 // thing if name contains a dot.
319 string const trial = name + '.' + (*what);
320 if (fs::exists(MakeAbsPath(trial, path)))
327 void begin_inset(ostream & os, string const & name)
329 os << "\n\\begin_inset " << name;
333 void end_inset(ostream & os)
335 os << "\n\\end_inset\n\n";
339 void skip_braces(Parser & p)
341 if (p.next_token().cat() != catBegin)
344 if (p.next_token().cat() == catEnd) {
352 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
355 // We must have a valid layout before outputting the ERT inset.
356 context.check_layout(os);
358 Context newcontext(true, context.textclass);
359 begin_inset(os, "ERT");
360 os << "\nstatus collapsed\n";
361 newcontext.check_layout(os);
362 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
364 os << "\n\\backslash\n";
365 else if (*it == '\n')
366 os << "\n\\newline\n";
370 newcontext.check_end_layout(os);
375 void handle_comment(ostream & os, string const & s, Context & context)
377 // TODO: Handle this better
378 Context newcontext(true, context.textclass);
379 begin_inset(os, "ERT");
380 os << "\nstatus collapsed\n";
381 newcontext.check_layout(os);
382 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
384 os << "\n\\backslash\n";
388 // make sure that our comment is the last thing on the line
390 newcontext.check_end_layout(os);
395 class isLayout : public std::unary_function<LyXLayout_ptr, bool> {
397 isLayout(string const name) : name_(name) {}
398 bool operator()(LyXLayout_ptr const & ptr) const {
399 return ptr->latexname() == name_;
406 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
409 LyXTextClass::const_iterator beg = textclass.begin();
410 LyXTextClass::const_iterator end = textclass.end();
412 LyXTextClass::const_iterator
413 it = std::find_if(beg, end, isLayout(name));
415 return (it == end) ? LyXLayout_ptr() : *it;
419 void eat_whitespace(Parser &, ostream &, Context &, bool);
422 void output_command_layout(ostream & os, Parser & p, bool outer,
423 Context & parent_context,
424 LyXLayout_ptr newlayout)
426 parent_context.check_end_layout(os);
427 Context context(true, parent_context.textclass, newlayout,
428 parent_context.layout, parent_context.font);
429 if (parent_context.deeper_paragraph) {
430 // We are beginning a nested environment after a
431 // deeper paragraph inside the outer list environment.
432 // Therefore we don't need to output a "begin deeper".
433 context.need_end_deeper = true;
435 context.check_deeper(os);
436 context.check_layout(os);
437 if (context.layout->optionalargs > 0) {
438 eat_whitespace(p, os, context, false);
439 if (p.next_token().character() == '[') {
440 p.get_token(); // eat '['
441 begin_inset(os, "OptArg\n");
442 os << "status collapsed\n\n";
443 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
445 eat_whitespace(p, os, context, false);
448 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
449 context.check_end_layout(os);
450 if (parent_context.deeper_paragraph) {
451 // We must suppress the "end deeper" because we
452 // suppressed the "begin deeper" above.
453 context.need_end_deeper = false;
455 context.check_end_deeper(os);
456 // We don't need really a new paragraph, but
457 // we must make sure that the next item gets a \begin_layout.
458 parent_context.new_paragraph(os);
463 * Output a space if necessary.
464 * This function gets called for every whitespace token.
466 * We have three cases here:
467 * 1. A space must be suppressed. Example: The lyxcode case below
468 * 2. A space may be suppressed. Example: Spaces before "\par"
469 * 3. A space must not be suppressed. Example: A space between two words
471 * We currently handle only 1. and 3 and from 2. only the case of
472 * spaces before newlines as a side effect.
474 * 2. could be used to suppress as many spaces as possible. This has two effects:
475 * - Reimporting LyX generated LaTeX files changes almost no whitespace
476 * - Superflous whitespace from non LyX generated LaTeX files is removed.
477 * The drawback is that the logic inside the function becomes
478 * complicated, and that is the reason why it is not implemented.
480 void check_space(Parser const & p, ostream & os, Context & context)
482 Token const next = p.next_token();
483 Token const curr = p.curr_token();
484 // A space before a single newline and vice versa must be ignored
485 // LyX emits a newline before \end{lyxcode}.
486 // This newline must be ignored,
487 // otherwise LyX will add an additional protected space.
488 if (next.cat() == catSpace ||
489 next.cat() == catNewline ||
490 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
493 context.check_layout(os);
499 * Check whether \p command is a known command. If yes,
500 * handle the command with all arguments.
501 * \return true if the command was parsed, false otherwise.
503 bool parse_command(string const & command, Parser & p, ostream & os,
504 bool outer, Context & context)
506 if (known_commands.find(command) != known_commands.end()) {
507 vector<ArgumentType> const & template_arguments = known_commands[command];
508 string ert = command;
509 size_t no_arguments = template_arguments.size();
510 for (size_t i = 0; i < no_arguments; ++i) {
511 switch (template_arguments[i]) {
513 // This argument contains regular LaTeX
514 handle_ert(os, ert + '{', context);
515 parse_text(p, os, FLAG_ITEM, outer, context);
519 // This argument may contain special characters
520 ert += '{' + p.verbatim_item() + '}';
527 handle_ert(os, ert, context);
534 /// Parses a minipage or parbox
535 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
536 Context & parent_context, bool use_parbox)
540 string height_value = "0";
541 string height_unit = "pt";
542 string height_special = "none";
544 if (p.next_token().asInput() == "[") {
545 position = p.getArg('[', ']');
546 if (position != "t" && position != "c" && position != "b") {
548 cerr << "invalid position for minipage/parbox" << endl;
550 if (p.next_token().asInput() == "[") {
551 latex_height = p.getArg('[', ']');
552 translate_box_len(latex_height, height_value, height_unit, height_special);
554 if (p.next_token().asInput() == "[") {
555 inner_pos = p.getArg('[', ']');
556 if (inner_pos != "c" && inner_pos != "t" &&
557 inner_pos != "b" && inner_pos != "s") {
558 inner_pos = position;
559 cerr << "invalid inner_pos for minipage/parbox"
567 string const latex_width = p.verbatim_item();
568 translate_len(latex_width, width_value, width_unit);
569 if (contains(width_unit, '\\') || contains(height_unit, '\\')) {
570 // LyX can't handle length variables
575 ss << "\\begin{minipage}";
576 if (!position.empty())
577 ss << '[' << position << ']';
578 if (!latex_height.empty())
579 ss << '[' << latex_height << ']';
580 if (!inner_pos.empty())
581 ss << '[' << inner_pos << ']';
582 ss << "{" << latex_width << "}";
585 handle_ert(os, ss.str(), parent_context);
586 parent_context.new_paragraph(os);
587 parse_text_in_inset(p, os, flags, outer, parent_context);
589 handle_ert(os, "}", parent_context);
591 handle_ert(os, "\\end{minipage}", parent_context);
593 // LyX does not like empty positions, so we have
594 // to set them to the LaTeX default values here.
595 if (position.empty())
597 if (inner_pos.empty())
598 inner_pos = position;
599 parent_context.check_layout(os);
600 begin_inset(os, "Box Frameless\n");
601 os << "position \"" << position << "\"\n";
602 os << "hor_pos \"c\"\n";
603 os << "has_inner_box 1\n";
604 os << "inner_pos \"" << inner_pos << "\"\n";
605 os << "use_parbox " << use_parbox << "\n";
606 os << "width \"" << width_value << width_unit << "\"\n";
607 os << "special \"none\"\n";
608 os << "height \"" << height_value << height_unit << "\"\n";
609 os << "height_special \"" << height_special << "\"\n";
610 os << "status open\n\n";
611 parse_text_in_inset(p, os, flags, outer, parent_context);
613 #ifdef PRESERVE_LAYOUT
614 // lyx puts a % after the end of the minipage
615 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
617 //handle_comment(os, "%dummy", parent_context);
620 parent_context.new_paragraph(os);
622 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
623 //handle_comment(os, "%dummy", parent_context);
626 // We add a protected space if something real follows
627 if (p.good() && p.next_token().cat() != catComment) {
628 os << "\\InsetSpace ~\n";
636 void parse_environment(Parser & p, ostream & os, bool outer,
637 Context & parent_context)
639 LyXLayout_ptr newlayout;
640 string const name = p.getArg('{', '}');
641 const bool is_starred = suffixIs(name, '*');
642 string const unstarred_name = rtrim(name, "*");
643 eat_whitespace(p, os, parent_context, false);
644 active_environments.push_back(name);
646 if (is_math_env(name)) {
647 parent_context.check_layout(os);
648 begin_inset(os, "Formula ");
649 os << "\\begin{" << name << "}";
650 parse_math(p, os, FLAG_END, MATH_MODE);
651 os << "\\end{" << name << "}";
655 else if (name == "tabular" || name == "longtable") {
656 parent_context.check_layout(os);
657 begin_inset(os, "Tabular ");
658 handle_tabular(p, os, name == "longtable", parent_context);
662 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
663 parent_context.check_layout(os);
664 begin_inset(os, "Float " + unstarred_name + "\n");
665 if (p.next_token().asInput() == "[") {
666 os << "placement " << p.getArg('[', ']') << '\n';
668 os << "wide " << convert<string>(is_starred)
669 << "\nsideways false"
670 << "\nstatus open\n\n";
671 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
673 // We don't need really a new paragraph, but
674 // we must make sure that the next item gets a \begin_layout.
675 parent_context.new_paragraph(os);
678 else if (name == "minipage")
679 parse_box(p, os, FLAG_END, outer, parent_context, false);
681 // Alignment settings
682 else if (name == "center" || name == "flushleft" || name == "flushright" ||
683 name == "centering" || name == "raggedright" || name == "raggedleft") {
684 // We must begin a new paragraph if not already done
685 if (! parent_context.atParagraphStart()) {
686 parent_context.check_end_layout(os);
687 parent_context.new_paragraph(os);
689 if (name == "flushleft" || name == "raggedright")
690 parent_context.add_extra_stuff("\\align left ");
691 else if (name == "flushright" || name == "raggedleft")
692 parent_context.add_extra_stuff("\\align right ");
694 parent_context.add_extra_stuff("\\align center ");
695 parse_text(p, os, FLAG_END, outer, parent_context);
696 // Just in case the environment is empty ..
697 parent_context.extra_stuff.erase();
698 // We must begin a new paragraph to reset the alignment
699 parent_context.new_paragraph(os);
702 // The single '=' is meant here.
703 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
704 newlayout->isEnvironment()) {
705 Context context(true, parent_context.textclass, newlayout,
706 parent_context.layout, parent_context.font);
707 if (parent_context.deeper_paragraph) {
708 // We are beginning a nested environment after a
709 // deeper paragraph inside the outer list environment.
710 // Therefore we don't need to output a "begin deeper".
711 context.need_end_deeper = true;
713 parent_context.check_end_layout(os);
714 switch (context.layout->latextype) {
715 case LATEX_LIST_ENVIRONMENT:
716 context.extra_stuff = "\\labelwidthstring "
717 + p.verbatim_item() + '\n';
720 case LATEX_BIB_ENVIRONMENT:
721 p.verbatim_item(); // swallow next arg
727 context.check_deeper(os);
728 parse_text(p, os, FLAG_END, outer, context);
729 context.check_end_layout(os);
730 if (parent_context.deeper_paragraph) {
731 // We must suppress the "end deeper" because we
732 // suppressed the "begin deeper" above.
733 context.need_end_deeper = false;
735 context.check_end_deeper(os);
736 parent_context.new_paragraph(os);
739 else if (name == "appendix") {
740 // This is no good latex style, but it works and is used in some documents...
741 parent_context.check_end_layout(os);
742 Context context(true, parent_context.textclass, parent_context.layout,
743 parent_context.layout, parent_context.font);
744 context.check_layout(os);
745 os << "\\start_of_appendix\n";
746 parse_text(p, os, FLAG_END, outer, context);
747 context.check_end_layout(os);
750 else if (name == "comment") {
751 parent_context.check_layout(os);
752 begin_inset(os, "Note Comment\n");
753 os << "status open\n";
754 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
758 else if (name == "lyxgreyedout") {
759 parent_context.check_layout(os);
760 begin_inset(os, "Note Greyedout\n");
761 os << "status open\n";
762 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
766 else if (name == "tabbing") {
767 // We need to remember that we have to handle '\=' specially
768 handle_ert(os, "\\begin{" + name + "}", parent_context);
769 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
770 handle_ert(os, "\\end{" + name + "}", parent_context);
774 handle_ert(os, "\\begin{" + name + "}", parent_context);
775 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
776 handle_ert(os, "\\end{" + name + "}", parent_context);
779 active_environments.pop_back();
784 /// parses a comment and outputs it to \p os.
785 void parse_comment(Parser & p, ostream & os, Token const & t, Context & context)
787 BOOST_ASSERT(t.cat() == catComment);
788 context.check_layout(os);
789 if (!t.cs().empty()) {
790 handle_comment(os, '%' + t.cs(), context);
791 if (p.next_token().cat() == catNewline) {
792 // A newline after a comment line starts a new
794 if(!context.atParagraphStart()) {
795 // Only start a new paragraph if not already
796 // done (we might get called recursively)
797 context.new_paragraph(os);
799 eat_whitespace(p, os, context, true);
809 * Reads spaces and comments until the first non-space, non-comment token.
810 * New paragraphs (double newlines or \\par) are handled like simple spaces
811 * if \p eatParagraph is true.
812 * Spaces are skipped, but comments are written to \p os.
814 void eat_whitespace(Parser & p, ostream & os, Context & context,
818 Token const & t = p.get_token();
819 if (t.cat() == catComment)
820 parse_comment(p, os, t, context);
821 else if ((! eatParagraph && p.isParagraph()) ||
822 (t.cat() != catSpace && t.cat() != catNewline)) {
831 * Set a font attribute, parse text and reset the font attribute.
832 * \param attribute Attribute name (e.g. \\family, \\shape etc.)
833 * \param currentvalue Current value of the attribute. Is set to the new
834 * value during parsing.
835 * \param newvalue New value of the attribute
837 void parse_text_attributes(Parser & p, ostream & os, unsigned flags, bool outer,
838 Context & context, string const & attribute,
839 string & currentvalue, string const & newvalue)
841 context.check_layout(os);
842 string oldvalue = currentvalue;
843 currentvalue = newvalue;
844 os << '\n' << attribute << ' ' << newvalue << "\n";
845 parse_text_snippet(p, os, flags, outer, context);
846 currentvalue = oldvalue;
847 os << '\n' << attribute << ' ' << oldvalue << "\n";
851 /// get the arguments of a natbib or jurabib citation command
852 std::pair<string, string> getCiteArguments(Parser & p, bool natbibOrder)
854 // We need to distinguish "" and "[]", so we can't use p.getOpt().
856 // text before the citation
858 // text after the citation
859 string after = p.getFullOpt();
861 if (!after.empty()) {
862 before = p.getFullOpt();
863 if (natbibOrder && !before.empty())
864 std::swap(before, after);
866 return std::make_pair(before, after);
869 } // anonymous namespace
872 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
875 LyXLayout_ptr newlayout;
876 // Store the latest bibliographystyle (needed for bibtex inset)
877 string bibliographystyle;
878 bool const use_natbib = used_packages.find("natbib") != used_packages.end();
879 bool const use_jurabib = used_packages.find("jurabib") != used_packages.end();
881 Token const & t = p.get_token();
884 cerr << "t: " << t << " flags: " << flags << "\n";
887 if (flags & FLAG_ITEM) {
888 if (t.cat() == catSpace)
892 if (t.cat() == catBegin) {
893 // skip the brace and collect everything to the next matching
895 flags |= FLAG_BRACE_LAST;
899 // handle only this single token, leave the loop if done
903 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
909 if (t.cat() == catMath) {
910 // we are inside some text mode thingy, so opening new math is allowed
911 context.check_layout(os);
912 begin_inset(os, "Formula ");
913 Token const & n = p.get_token();
914 if (n.cat() == catMath && outer) {
915 // TeX's $$...$$ syntax for displayed math
917 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
919 p.get_token(); // skip the second '$' token
921 // simple $...$ stuff
924 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
930 else if (t.cat() == catSuper || t.cat() == catSub)
931 cerr << "catcode " << t << " illegal in text mode\n";
933 // Basic support for english quotes. This should be
934 // extended to other quotes, but is not so easy (a
935 // left english quote is the same as a right german
937 else if (t.asInput() == "`"
938 && p.next_token().asInput() == "`") {
939 context.check_layout(os);
940 begin_inset(os, "Quotes ");
946 else if (t.asInput() == "'"
947 && p.next_token().asInput() == "'") {
948 context.check_layout(os);
949 begin_inset(os, "Quotes ");
956 else if (t.cat() == catSpace || (t.cat() == catNewline && ! p.isParagraph()))
957 check_space(p, os, context);
959 else if (t.cat() == catLetter ||
960 t.cat() == catOther ||
961 t.cat() == catAlign ||
962 t.cat() == catParameter) {
963 context.check_layout(os);
967 else if (p.isParagraph()) {
968 context.new_paragraph(os);
969 eat_whitespace(p, os, context, true);
972 else if (t.cat() == catActive) {
973 context.check_layout(os);
974 if (t.character() == '~') {
975 if (context.layout->free_spacing)
978 os << "\\InsetSpace ~\n";
983 else if (t.cat() == catBegin) {
984 context.check_layout(os);
985 // special handling of font attribute changes
986 Token const prev = p.prev_token();
987 Token const next = p.next_token();
988 Font const oldFont = context.font;
989 string const s = parse_text(p, FLAG_BRACE_LAST, outer,
991 context.font = oldFont;
992 if (s.empty() && (p.next_token().character() == '`' ||
993 (prev.character() == '-' &&
994 p.next_token().character() == '-')))
995 ; // ignore it in {}`` or -{}-
996 else if (s == "[" || s == "]" || s == "*")
998 else if (is_known(next.cs(), known_sizes)) {
999 // s will change the size, so we must reset
1002 if (!context.atParagraphStart())
1004 << context.font.size << "\n";
1005 } else if (is_known(next.cs(), known_font_families)) {
1006 // s will change the font family, so we must
1009 if (!context.atParagraphStart())
1011 << context.font.family << "\n";
1012 } else if (is_known(next.cs(), known_font_series)) {
1013 // s will change the font series, so we must
1016 if (!context.atParagraphStart())
1018 << context.font.series << "\n";
1019 } else if (is_known(next.cs(), known_font_shapes)) {
1020 // s will change the font shape, so we must
1023 if (!context.atParagraphStart())
1025 << context.font.shape << "\n";
1026 } else if (is_known(next.cs(), known_old_font_families) ||
1027 is_known(next.cs(), known_old_font_series) ||
1028 is_known(next.cs(), known_old_font_shapes)) {
1029 // s will change the font family, series
1030 // and shape, so we must reset it here
1032 if (!context.atParagraphStart())
1034 << context.font.family
1036 << context.font.series
1038 << context.font.shape << "\n";
1040 handle_ert(os, "{", context, false);
1041 // s will end the current layout and begin a
1042 // new one if necessary
1044 handle_ert(os, "}", context);
1048 else if (t.cat() == catEnd) {
1049 if (flags & FLAG_BRACE_LAST) {
1052 cerr << "stray '}' in text\n";
1053 handle_ert(os, "}", context);
1056 else if (t.cat() == catComment)
1057 parse_comment(p, os, t, context);
1060 // control sequences
1063 else if (t.cs() == "(") {
1064 context.check_layout(os);
1065 begin_inset(os, "Formula");
1067 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
1072 else if (t.cs() == "[") {
1073 context.check_layout(os);
1074 begin_inset(os, "Formula");
1076 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
1081 else if (t.cs() == "begin")
1082 parse_environment(p, os, outer, context);
1084 else if (t.cs() == "end") {
1085 if (flags & FLAG_END) {
1086 // eat environment name
1087 string const name = p.getArg('{', '}');
1088 if (name != active_environment())
1089 cerr << "\\end{" + name + "} does not match \\begin{"
1090 + active_environment() + "}\n";
1093 p.error("found 'end' unexpectedly");
1096 else if (t.cs() == "item") {
1099 bool optarg = false;
1100 if (p.next_token().character() == '[') {
1101 p.get_token(); // eat '['
1102 Context newcontext(false, context.textclass);
1103 newcontext.font = context.font;
1104 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
1108 context.check_layout(os);
1110 if (context.layout->labeltype != LABEL_MANUAL) {
1111 // lyx does not support \item[\mybullet]
1112 // in itemize environments
1113 handle_ert(os, "[", context);
1115 handle_ert(os, "]", context);
1116 } else if (!s.empty()) {
1117 // The space is needed to separate the
1118 // item from the rest of the sentence.
1120 eat_whitespace(p, os, context, false);
1125 else if (t.cs() == "bibitem") {
1127 context.check_layout(os);
1130 os << '{' << p.verbatim_item() << '}' << "\n";
1133 else if (t.cs() == "def") {
1134 context.check_layout(os);
1135 eat_whitespace(p, os, context, false);
1136 string name = p.get_token().cs();
1137 while (p.next_token().cat() != catBegin)
1138 name += p.get_token().asString();
1139 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
1142 else if (t.cs() == "noindent") {
1144 context.add_extra_stuff("\\noindent ");
1147 else if (t.cs() == "appendix") {
1149 context.add_extra_stuff("\\start_of_appendix ");
1152 // Must attempt to parse "Section*" before "Section".
1153 else if ((p.next_token().asInput() == "*") &&
1154 // The single '=' is meant here.
1155 (newlayout = findLayout(context.textclass,
1156 t.cs() + '*')).get() &&
1157 newlayout->isCommand()) {
1159 output_command_layout(os, p, outer, context, newlayout);
1163 // The single '=' is meant here.
1164 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
1165 newlayout->isCommand()) {
1166 output_command_layout(os, p, outer, context, newlayout);
1170 else if (t.cs() == "includegraphics") {
1171 map<string, string> opts = split_map(p.getArg('[', ']'));
1172 string name = subst(p.verbatim_item(), "\\lyxdot ", ".");
1174 string const path = getMasterFilePath();
1175 // We want to preserve relative / absolute filenames,
1176 // therefore path is only used for testing
1177 if (!fs::exists(MakeAbsPath(name, path))) {
1178 // The file extension is probably missing.
1179 // Now try to find it out.
1180 string const dvips_name =
1181 find_file(name, path,
1182 known_dvips_graphics_formats);
1183 string const pdftex_name =
1184 find_file(name, path,
1185 known_pdftex_graphics_formats);
1186 if (!dvips_name.empty()) {
1187 if (!pdftex_name.empty()) {
1188 cerr << "This file contains the "
1190 "\"\\includegraphics{"
1192 "However, files\n\""
1193 << dvips_name << "\" and\n\""
1194 << pdftex_name << "\"\n"
1195 "both exist, so I had to make a "
1196 "choice and took the first one.\n"
1197 "Please move the unwanted one "
1198 "someplace else and try again\n"
1199 "if my choice was wrong."
1203 } else if (!pdftex_name.empty())
1206 if (!fs::exists(MakeAbsPath(name, path)))
1207 cerr << "Warning: Could not find graphics file '"
1208 << name << "'." << endl;
1211 context.check_layout(os);
1212 begin_inset(os, "Graphics ");
1213 os << "\n\tfilename " << name << '\n';
1214 if (opts.find("width") != opts.end())
1216 << translate_len(opts["width"]) << '\n';
1217 if (opts.find("height") != opts.end())
1219 << translate_len(opts["height"]) << '\n';
1220 if (opts.find("scale") != opts.end()) {
1221 istringstream iss(opts["scale"]);
1225 os << "\tscale " << val << '\n';
1227 if (opts.find("angle") != opts.end())
1228 os << "\trotateAngle "
1229 << opts["angle"] << '\n';
1230 if (opts.find("origin") != opts.end()) {
1232 string const opt = opts["origin"];
1233 if (opt.find('l') != string::npos) ss << "left";
1234 if (opt.find('r') != string::npos) ss << "right";
1235 if (opt.find('c') != string::npos) ss << "center";
1236 if (opt.find('t') != string::npos) ss << "Top";
1237 if (opt.find('b') != string::npos) ss << "Bottom";
1238 if (opt.find('B') != string::npos) ss << "Baseline";
1239 if (!ss.str().empty())
1240 os << "\trotateOrigin " << ss.str() << '\n';
1242 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
1244 if (opts.find("keepaspectratio") != opts.end())
1245 os << "\tkeepAspectRatio\n";
1246 if (opts.find("clip") != opts.end())
1248 if (opts.find("draft") != opts.end())
1250 if (opts.find("bb") != opts.end())
1251 os << "\tBoundingBox "
1252 << opts["bb"] << '\n';
1253 int numberOfbbOptions = 0;
1254 if (opts.find("bbllx") != opts.end())
1255 numberOfbbOptions++;
1256 if (opts.find("bblly") != opts.end())
1257 numberOfbbOptions++;
1258 if (opts.find("bburx") != opts.end())
1259 numberOfbbOptions++;
1260 if (opts.find("bbury") != opts.end())
1261 numberOfbbOptions++;
1262 if (numberOfbbOptions == 4)
1263 os << "\tBoundingBox "
1264 << opts["bbllx"] << opts["bblly"]
1265 << opts["bburx"] << opts["bbury"] << '\n';
1266 else if (numberOfbbOptions > 0)
1267 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1268 numberOfbbOptions = 0;
1269 if (opts.find("natwidth") != opts.end())
1270 numberOfbbOptions++;
1271 if (opts.find("natheight") != opts.end())
1272 numberOfbbOptions++;
1273 if (numberOfbbOptions == 2)
1274 os << "\tBoundingBox 0bp 0bp "
1275 << opts["natwidth"] << opts["natheight"] << '\n';
1276 else if (numberOfbbOptions > 0)
1277 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
1278 ostringstream special;
1279 if (opts.find("hiresbb") != opts.end())
1280 special << "hiresbb,";
1281 if (opts.find("trim") != opts.end())
1283 if (opts.find("viewport") != opts.end())
1284 special << "viewport=" << opts["viewport"] << ',';
1285 if (opts.find("totalheight") != opts.end())
1286 special << "totalheight=" << opts["totalheight"] << ',';
1287 if (opts.find("type") != opts.end())
1288 special << "type=" << opts["type"] << ',';
1289 if (opts.find("ext") != opts.end())
1290 special << "ext=" << opts["ext"] << ',';
1291 if (opts.find("read") != opts.end())
1292 special << "read=" << opts["read"] << ',';
1293 if (opts.find("command") != opts.end())
1294 special << "command=" << opts["command"] << ',';
1295 string s_special = special.str();
1296 if (!s_special.empty()) {
1297 // We had special arguments. Remove the trailing ','.
1298 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1300 // TODO: Handle the unknown settings better.
1301 // Warn about invalid options.
1302 // Check whether some option was given twice.
1306 else if (t.cs() == "footnote") {
1308 context.check_layout(os);
1309 begin_inset(os, "Foot\n");
1310 os << "status collapsed\n\n";
1311 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1315 else if (t.cs() == "marginpar") {
1317 context.check_layout(os);
1318 begin_inset(os, "Marginal\n");
1319 os << "status collapsed\n\n";
1320 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1324 else if (t.cs() == "ensuremath") {
1326 context.check_layout(os);
1327 Context newcontext(false, context.textclass);
1328 newcontext.font = context.font;
1329 string s = parse_text(p, FLAG_ITEM, false, newcontext);
1330 if (s == "±" || s == "³" || s == "²" || s == "µ")
1333 handle_ert(os, "\\ensuremath{" + s + "}",
1337 else if (t.cs() == "hfill") {
1338 context.check_layout(os);
1339 os << "\n\\hfill\n";
1344 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1346 skip_braces(p); // swallow this
1349 else if (t.cs() == "tableofcontents") {
1351 context.check_layout(os);
1352 begin_inset(os, "LatexCommand \\tableofcontents\n");
1354 skip_braces(p); // swallow this
1357 else if (t.cs() == "listoffigures") {
1359 context.check_layout(os);
1360 begin_inset(os, "FloatList figure\n");
1362 skip_braces(p); // swallow this
1365 else if (t.cs() == "listoftables") {
1367 context.check_layout(os);
1368 begin_inset(os, "FloatList table\n");
1370 skip_braces(p); // swallow this
1373 else if (t.cs() == "listof") {
1374 p.skip_spaces(true);
1375 string const name = p.get_token().asString();
1376 if (context.textclass.floats().typeExist(name)) {
1377 context.check_layout(os);
1378 begin_inset(os, "FloatList ");
1381 p.get_token(); // swallow second arg
1383 handle_ert(os, "\\listof{" + name + "}", context);
1386 else if (t.cs() == "textrm")
1387 parse_text_attributes(p, os, FLAG_ITEM, outer,
1388 context, "\\family",
1389 context.font.family, "roman");
1391 else if (t.cs() == "textsf")
1392 parse_text_attributes(p, os, FLAG_ITEM, outer,
1393 context, "\\family",
1394 context.font.family, "sans");
1396 else if (t.cs() == "texttt")
1397 parse_text_attributes(p, os, FLAG_ITEM, outer,
1398 context, "\\family",
1399 context.font.family, "typewriter");
1401 else if (t.cs() == "textmd")
1402 parse_text_attributes(p, os, FLAG_ITEM, outer,
1403 context, "\\series",
1404 context.font.series, "medium");
1406 else if (t.cs() == "textbf")
1407 parse_text_attributes(p, os, FLAG_ITEM, outer,
1408 context, "\\series",
1409 context.font.series, "bold");
1411 else if (t.cs() == "textup")
1412 parse_text_attributes(p, os, FLAG_ITEM, outer,
1414 context.font.shape, "up");
1416 else if (t.cs() == "textit")
1417 parse_text_attributes(p, os, FLAG_ITEM, outer,
1419 context.font.shape, "italic");
1421 else if (t.cs() == "textsl")
1422 parse_text_attributes(p, os, FLAG_ITEM, outer,
1424 context.font.shape, "slanted");
1426 else if (t.cs() == "textsc")
1427 parse_text_attributes(p, os, FLAG_ITEM, outer,
1429 context.font.shape, "smallcaps");
1431 else if (t.cs() == "textnormal" || t.cs() == "normalfont") {
1432 context.check_layout(os);
1433 Font oldFont = context.font;
1434 context.font.init();
1435 context.font.size = oldFont.size;
1436 os << "\n\\family " << context.font.family << "\n";
1437 os << "\n\\series " << context.font.series << "\n";
1438 os << "\n\\shape " << context.font.shape << "\n";
1439 if (t.cs() == "textnormal") {
1440 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1441 context.font = oldFont;
1442 os << "\n\\shape " << oldFont.shape << "\n";
1443 os << "\n\\series " << oldFont.series << "\n";
1444 os << "\n\\family " << oldFont.family << "\n";
1446 eat_whitespace(p, os, context, false);
1449 else if (t.cs() == "underbar") {
1450 // Do NOT handle \underline.
1451 // \underbar cuts through y, g, q, p etc.,
1452 // \underline does not.
1453 context.check_layout(os);
1454 os << "\n\\bar under\n";
1455 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1456 os << "\n\\bar default\n";
1459 else if (t.cs() == "emph" || t.cs() == "noun") {
1460 context.check_layout(os);
1461 os << "\n\\" << t.cs() << " on\n";
1462 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1463 os << "\n\\" << t.cs() << " default\n";
1466 else if (use_natbib &&
1467 is_known(t.cs(), known_natbib_commands) &&
1468 ((t.cs() != "citefullauthor" &&
1469 t.cs() != "citeyear" &&
1470 t.cs() != "citeyearpar") ||
1471 p.next_token().asInput() != "*")) {
1472 context.check_layout(os);
1474 // \citet[before][after]{a} \citet[after][before]{a}
1475 // \citet[before][]{a} \citet[][before]{a}
1476 // \citet[after]{a} \citet[after]{a}
1477 // \citet{a} \citet{a}
1478 string command = '\\' + t.cs();
1479 if (p.next_token().asInput() == "*") {
1483 if (command == "\\citefullauthor")
1484 // alternative name for "\\citeauthor*"
1485 command = "\\citeauthor*";
1487 // text before the citation
1489 // text after the citation
1492 boost::tie(before, after) = getCiteArguments(p, true);
1493 if (command == "\\cite") {
1494 // \cite without optional argument means
1495 // \citet, \cite with at least one optional
1496 // argument means \citep.
1497 if (before.empty() && after.empty())
1498 command = "\\citet";
1500 command = "\\citep";
1502 if (before.empty() && after == "[]")
1503 // avoid \citet[]{a}
1505 else if (before == "[]" && after == "[]") {
1506 // avoid \citet[][]{a}
1510 begin_inset(os, "LatexCommand ");
1511 os << command << after << before
1512 << '{' << p.verbatim_item() << "}\n";
1516 else if (use_jurabib &&
1517 is_known(t.cs(), known_jurabib_commands)) {
1518 context.check_layout(os);
1519 string const command = '\\' + t.cs();
1520 char argumentOrder = '\0';
1521 vector<string> const & options = used_packages["jurabib"];
1522 if (std::find(options.begin(), options.end(),
1523 "natbiborder") != options.end())
1524 argumentOrder = 'n';
1525 else if (std::find(options.begin(), options.end(),
1526 "jurabiborder") != options.end())
1527 argumentOrder = 'j';
1529 // text before the citation
1531 // text after the citation
1534 boost::tie(before, after) =
1535 getCiteArguments(p, argumentOrder != 'j');
1536 string const citation = p.verbatim_item();
1537 if (!before.empty() && argumentOrder == '\0') {
1538 cerr << "Warning: Assuming argument order "
1539 << "of jurabib version 0.6 for\n'"
1540 << command << before << after << '{'
1541 << citation << "}'.\n"
1542 << "Add 'jurabiborder' to the jurabib "
1543 << "package options if you used an\n"
1544 << "earlier jurabib version." << endl;
1546 begin_inset(os, "LatexCommand ");
1547 os << command << after << before
1548 << '{' << citation << "}\n";
1552 else if (is_known(t.cs(), known_latex_commands)) {
1553 // This needs to be after the check for natbib and
1554 // jurabib commands, because "cite" has different
1555 // arguments with natbib and jurabib.
1556 context.check_layout(os);
1557 begin_inset(os, "LatexCommand ");
1558 os << '\\' << t.cs();
1559 // lyx cannot handle newlines in a latex command
1560 // FIXME: Move the substitution into parser::getOpt()?
1561 os << subst(p.getOpt(), "\n", " ");
1562 os << subst(p.getOpt(), "\n", " ");
1563 os << '{' << subst(p.verbatim_item(), "\n", " ") << "}\n";
1567 else if (is_known(t.cs(), known_quotes)) {
1568 char const * const * where = is_known(t.cs(), known_quotes);
1569 context.check_layout(os);
1570 begin_inset(os, "Quotes ");
1571 os << known_coded_quotes[where - known_quotes];
1573 // LyX adds {} after the quote, so we have to eat
1574 // spaces here if there are any before a possible
1576 eat_whitespace(p, os, context, false);
1580 else if (is_known(t.cs(), known_sizes)) {
1581 char const * const * where = is_known(t.cs(), known_sizes);
1582 context.check_layout(os);
1583 context.font.size = known_coded_sizes[where - known_sizes];
1584 os << "\n\\size " << context.font.size << '\n';
1585 eat_whitespace(p, os, context, false);
1588 else if (is_known(t.cs(), known_font_families)) {
1589 char const * const * where =
1590 is_known(t.cs(), known_font_families);
1591 context.check_layout(os);
1592 context.font.family =
1593 known_coded_font_families[where - known_font_families];
1594 os << "\n\\family " << context.font.family << '\n';
1595 eat_whitespace(p, os, context, false);
1598 else if (is_known(t.cs(), known_font_series)) {
1599 char const * const * where =
1600 is_known(t.cs(), known_font_series);
1601 context.check_layout(os);
1602 context.font.series =
1603 known_coded_font_series[where - known_font_series];
1604 os << "\n\\series " << context.font.series << '\n';
1605 eat_whitespace(p, os, context, false);
1608 else if (is_known(t.cs(), known_font_shapes)) {
1609 char const * const * where =
1610 is_known(t.cs(), known_font_shapes);
1611 context.check_layout(os);
1612 context.font.shape =
1613 known_coded_font_shapes[where - known_font_shapes];
1614 os << "\n\\shape " << context.font.shape << '\n';
1615 eat_whitespace(p, os, context, false);
1617 else if (is_known(t.cs(), known_old_font_families)) {
1618 char const * const * where =
1619 is_known(t.cs(), known_old_font_families);
1620 context.check_layout(os);
1621 string oldsize = context.font.size;
1622 context.font.init();
1623 context.font.size = oldsize;
1624 context.font.family =
1625 known_coded_font_families[where - known_old_font_families];
1626 os << "\n\\family " << context.font.family << "\n"
1627 << "\\series " << context.font.series << "\n"
1628 << "\\shape " << context.font.shape << "\n";
1629 eat_whitespace(p, os, context, false);
1632 else if (is_known(t.cs(), known_old_font_series)) {
1633 char const * const * where =
1634 is_known(t.cs(), known_old_font_series);
1635 context.check_layout(os);
1636 string oldsize = context.font.size;
1637 context.font.init();
1638 context.font.size = oldsize;
1639 context.font.series =
1640 known_coded_font_series[where - known_old_font_series];
1641 os << "\n\\family " << context.font.family << "\n"
1642 << "\\series " << context.font.series << "\n"
1643 << "\\shape " << context.font.shape << "\n";
1644 eat_whitespace(p, os, context, false);
1647 else if (is_known(t.cs(), known_old_font_shapes)) {
1648 char const * const * where =
1649 is_known(t.cs(), known_old_font_shapes);
1650 context.check_layout(os);
1651 string oldsize = context.font.size;
1652 context.font.init();
1653 context.font.size = oldsize;
1654 context.font.shape =
1655 known_coded_font_shapes[where - known_old_font_shapes];
1656 os << "\n\\family " << context.font.family << "\n"
1657 << "\\series " << context.font.series << "\n"
1658 << "\\shape " << context.font.shape << "\n";
1659 eat_whitespace(p, os, context, false);
1662 else if (t.cs() == "LyX" || t.cs() == "TeX"
1663 || t.cs() == "LaTeX") {
1664 context.check_layout(os);
1666 skip_braces(p); // eat {}
1669 else if (t.cs() == "LaTeXe") {
1670 context.check_layout(os);
1672 skip_braces(p); // eat {}
1675 else if (t.cs() == "ldots") {
1676 context.check_layout(os);
1678 os << "\\SpecialChar \\ldots{}\n";
1681 else if (t.cs() == "lyxarrow") {
1682 context.check_layout(os);
1683 os << "\\SpecialChar \\menuseparator\n";
1687 else if (t.cs() == "textcompwordmark") {
1688 context.check_layout(os);
1689 os << "\\SpecialChar \\textcompwordmark{}\n";
1693 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1694 context.check_layout(os);
1695 os << "\\SpecialChar \\@.\n";
1699 else if (t.cs() == "-") {
1700 context.check_layout(os);
1701 os << "\\SpecialChar \\-\n";
1704 else if (t.cs() == "textasciitilde") {
1705 context.check_layout(os);
1710 else if (t.cs() == "textasciicircum") {
1711 context.check_layout(os);
1716 else if (t.cs() == "textbackslash") {
1717 context.check_layout(os);
1718 os << "\n\\backslash\n";
1722 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1723 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1725 context.check_layout(os);
1729 else if (t.cs() == "char") {
1730 context.check_layout(os);
1731 if (p.next_token().character() == '`') {
1733 if (p.next_token().cs() == "\"") {
1738 handle_ert(os, "\\char`", context);
1741 handle_ert(os, "\\char", context);
1745 else if (t.cs() == "\"") {
1746 context.check_layout(os);
1747 string const name = p.verbatim_item();
1748 if (name == "a") os << 'ä';
1749 else if (name == "o") os << 'ö';
1750 else if (name == "u") os << 'ü';
1751 else if (name == "A") os << 'Ä';
1752 else if (name == "O") os << 'Ö';
1753 else if (name == "U") os << 'Ü';
1754 else handle_ert(os, "\"{" + name + "}", context);
1757 // Problem: \= creates a tabstop inside the tabbing environment
1758 // and else an accent. In the latter case we really would want
1759 // \={o} instead of \= o.
1760 else if (t.cs() == "=" && (flags & FLAG_TABBING))
1761 handle_ert(os, t.asInput(), context);
1763 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1764 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
1765 // we need the trim as the LyX parser chokes on such spaces
1766 context.check_layout(os);
1767 os << "\n\\i \\" << t.cs() << "{"
1768 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1771 else if (t.cs() == "ss") {
1772 context.check_layout(os);
1776 else if (t.cs() == "i" || t.cs() == "j") {
1777 context.check_layout(os);
1778 os << "\\" << t.cs() << ' ';
1781 else if (t.cs() == "\\") {
1782 context.check_layout(os);
1783 string const next = p.next_token().asInput();
1785 handle_ert(os, "\\\\" + p.getOpt(), context);
1786 else if (next == "*") {
1788 handle_ert(os, "\\\\*" + p.getOpt(), context);
1791 os << "\n\\newline\n";
1795 else if (t.cs() == "input" || t.cs() == "include"
1796 || t.cs() == "verbatiminput") {
1797 string name = '\\' + t.cs();
1798 if (t.cs() == "verbatiminput"
1799 && p.next_token().asInput() == "*")
1800 name += p.get_token().asInput();
1801 context.check_layout(os);
1802 begin_inset(os, "Include ");
1803 string filename(p.getArg('{', '}'));
1804 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1805 if (tex2lyx(filename, lyxname)) {
1806 os << name << '{' << lyxname << "}\n";
1808 os << name << '{' << filename << "}\n";
1810 os << "preview false\n";
1814 else if (t.cs() == "fancyhead") {
1815 context.check_layout(os);
1817 ss << "\\fancyhead";
1819 ss << '{' << p.verbatim_item() << "}\n";
1820 handle_ert(os, ss.str(), context);
1823 else if (t.cs() == "bibliographystyle") {
1824 // store new bibliographystyle
1825 bibliographystyle = p.verbatim_item();
1826 // output new bibliographystyle.
1827 // This is only necessary if used in some other macro than \bibliography.
1828 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1831 else if (t.cs() == "bibliography") {
1832 context.check_layout(os);
1833 begin_inset(os, "LatexCommand ");
1835 // Do we have a bibliographystyle set?
1836 if (!bibliographystyle.empty()) {
1837 os << '[' << bibliographystyle << ']';
1839 os << '{' << p.verbatim_item() << "}\n";
1843 else if (t.cs() == "parbox")
1844 parse_box(p, os, FLAG_ITEM, outer, context, true);
1846 else if (t.cs() == "smallskip" ||
1847 t.cs() == "medskip" ||
1848 t.cs() == "bigskip" ||
1849 t.cs() == "vfill") {
1850 context.check_layout(os);
1851 begin_inset(os, "VSpace ");
1857 else if (t.cs() == "newcommand" ||
1858 t.cs() == "providecommand" ||
1859 t.cs() == "renewcommand") {
1860 // these could be handled by parse_command(), but
1861 // we need to call add_known_command() here.
1862 string name = t.asInput();
1863 if (p.next_token().asInput() == "*") {
1864 // Starred form. Eat '*'
1868 string const command = p.verbatim_item();
1869 string const opt1 = p.getOpt();
1870 string const opt2 = p.getFullOpt();
1871 add_known_command(command, opt1, !opt2.empty());
1872 string const ert = name + '{' + command + '}' +
1874 '{' + p.verbatim_item() + '}';
1875 handle_ert(os, ert, context);
1878 else if (t.cs() == "vspace") {
1879 bool starred = false;
1880 if (p.next_token().asInput() == "*") {
1884 string const length = p.verbatim_item();
1887 bool valid = splitLatexLength(length, valstring, unit);
1888 bool known_vspace = false;
1889 bool known_unit = false;
1892 istringstream iss(valstring);
1895 if (unit == "\\smallskipamount") {
1897 known_vspace = true;
1898 } else if (unit == "\\medskipamount") {
1900 known_vspace = true;
1901 } else if (unit == "\\bigskipamount") {
1903 known_vspace = true;
1904 } else if (unit == "\\fill") {
1906 known_vspace = true;
1909 if (!known_vspace) {
1910 switch (unitFromString(unit)) {
1931 if (known_unit || known_vspace) {
1932 // Literal length or known variable
1933 context.check_layout(os);
1934 begin_inset(os, "VSpace ");
1942 // LyX can't handle other length variables in Inset VSpace
1943 string name = t.asInput();
1948 handle_ert(os, name + '{' + unit + '}', context);
1949 else if (value == -1.0)
1950 handle_ert(os, name + "{-" + unit + '}', context);
1952 handle_ert(os, name + '{' + valstring + unit + '}', context);
1954 handle_ert(os, name + '{' + length + '}', context);
1959 //cerr << "#: " << t << " mode: " << mode << endl;
1960 // heuristic: read up to next non-nested space
1962 string s = t.asInput();
1963 string z = p.verbatim_item();
1964 while (p.good() && z != " " && z.size()) {
1965 //cerr << "read: " << z << endl;
1967 z = p.verbatim_item();
1969 cerr << "found ERT: " << s << endl;
1970 handle_ert(os, s + ' ', context);
1972 string name = t.asInput();
1973 if (p.next_token().asInput() == "*") {
1974 // Starred commands like \vspace*{}
1975 p.get_token(); // Eat '*'
1978 if (! parse_command(name, p, os, outer, context))
1979 handle_ert(os, name, context);
1982 if (flags & FLAG_LEAVE) {
1983 flags &= ~FLAG_LEAVE;