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 "support/lstrings.h"
20 #include "support/tostr.h"
21 #include "support/filetools.h"
33 using std::ostringstream;
34 using std::istringstream;
38 using lyx::support::rtrim;
39 using lyx::support::suffixIs;
42 // thin wrapper around parse_text using a string
43 string parse_text(Parser & p, unsigned flags, const bool outer,
47 parse_text(p, os, flags, outer, context);
51 // parses a subdocument, usually useful in insets (whence the name)
52 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
55 Context newcontext(true, context.textclass);
56 parse_text(p, os, flags, outer, newcontext);
57 newcontext.check_end_layout(os);
61 // parses a paragraph snippet, useful for example for \emph{...}
62 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
65 Context newcontext(false, context.textclass);
66 parse_text(p, os, flags, outer, newcontext);
67 // should not be needed
68 newcontext.check_end_layout(os);
74 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
75 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
77 // LaTeX names for quotes
78 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
79 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
81 // the same as known_quotes with .lyx names
82 char const * known_coded_quotes[] = { "gld", "grd", "gld",
83 "grd", "gls", "fls", "frd", 0};
85 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
86 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
88 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
89 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
91 // splits "x=z, y=b" into a map
92 map<string, string> split_map(string const & s)
94 map<string, string> res;
97 for (size_t i = 0; i < v.size(); ++i) {
98 size_t const pos = v[i].find('=');
99 string const index = v[i].substr(0, pos);
100 string const value = v[i].substr(pos + 1, string::npos);
101 res[trim(index)] = trim(value);
106 // A simple function to translate a latex length to something lyx can
107 // understand. Not perfect, but rather best-effort.
108 string translate_len(string const & len)
110 const string::size_type i = len.find_first_not_of(" -0123456789.,");
111 //'4,5' is a valid LaTeX length number. Change it to '4.5'
112 string const length = lyx::support::subst(len, ',', '.');
114 if (i == string::npos || len[i] != '\\')
118 // We had something like \textwidth without a factor
121 istringstream iss(string(length, 0, i));
127 string const valstring = oss.str();
128 const string::size_type i2 = length.find(" ", i);
129 string const unit = string(len, i, i2 - i);
130 string const endlen = (i2 == string::npos) ? string() : string(len, i2);
131 if (unit == "\\textwidth")
132 return valstring + "text%" + endlen;
133 else if (unit == "\\columnwidth")
134 return valstring + "col%" + endlen;
135 else if (unit == "\\paperwidth")
136 return valstring + "page%" + endlen;
137 else if (unit == "\\linewidth")
138 return valstring + "line%" + endlen;
139 else if (unit == "\\paperheight")
140 return valstring + "pheight%" + endlen;
141 else if (unit == "\\textheight")
142 return valstring + "theight%" + endlen;
148 void begin_inset(ostream & os, string const & name)
150 os << "\n\\begin_inset " << name;
154 void end_inset(ostream & os)
156 os << "\n\\end_inset \n\n";
160 void skip_braces(Parser & p)
162 if (p.next_token().cat() != catBegin)
165 if (p.next_token().cat() == catEnd) {
173 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
176 // We must have a valid layout before outputting the ERT inset.
177 context.check_layout(os);
179 Context newcontext(true, context.textclass);
180 begin_inset(os, "ERT");
181 os << "\nstatus Collapsed\n";
182 newcontext.check_layout(os);
183 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
185 os << "\n\\backslash \n";
189 newcontext.check_end_layout(os);
194 void handle_comment(ostream & os, string const & s, Context & context)
196 // TODO: Handle this better
197 Context newcontext(true, context.textclass);
198 begin_inset(os, "ERT");
199 os << "\nstatus Collapsed\n";
200 newcontext.check_layout(os);
201 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
203 os << "\n\\backslash \n";
207 // make sure that our comment is the last thing on the line
209 newcontext.check_end_layout(os);
215 isLayout(string const name) : name_(name) {}
216 bool operator()(LyXLayout_ptr const & ptr) {
217 return ptr.get() && ptr->latexname() == name_;
224 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
227 LyXTextClass::const_iterator it = textclass.begin();
228 LyXTextClass::const_iterator end = textclass.end();
229 it = std::find_if(it, end, isLayout(name));
230 return (it == end) ? LyXLayout_ptr() : *it;
234 void output_command_layout(ostream & os, Parser & p, bool outer,
235 Context & parent_context,
236 LyXLayout_ptr newlayout)
238 parent_context.check_end_layout(os);
239 Context context(true, parent_context.textclass, newlayout,
240 parent_context.layout);
241 context.check_deeper(os);
242 context.check_layout(os);
243 if (context.layout->optionalargs > 0) {
245 if (p.next_token().character() == '[') {
246 p.get_token(); // eat '['
247 begin_inset(os, "OptArg\n");
248 os << "collapsed true\n\n";
249 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
253 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
254 context.check_end_layout(os);
255 context.check_end_deeper(os);
256 // We don't need really a new paragraph, but
257 // we must make sure that the next item gets a \begin_layout.
258 parent_context.new_paragraph(os);
263 * Output a space if necessary.
264 * This function gets called for every whitespace token.
266 * We have three cases here:
267 * 1. A space must be suppressed. Example: The lyxcode case below
268 * 2. A space may be suppressed. Example: Spaces before "\par"
269 * 3. A space must not be suppressed. Example: A space between two words
271 * We currently handle only 1. and 3 and from 2. only the case of
272 * spaces before newlines as a side effect.
274 * 2. could be used to suppress as many spaces as possible. This has two effects:
275 * - Reimporting LyX generated LaTeX files changes almost no whitespace
276 * - Superflous whitespace from non LyX generated LaTeX files is removed.
277 * The drawback is that the logic inside the function becomes
278 * complicated, and that is the reason why it is not implemented.
280 void check_space(Parser const & p, ostream & os, Context & context)
282 Token const next = p.next_token();
283 Token const curr = p.curr_token();
284 // A space before a single newline and vice versa must be ignored
285 // LyX emits a newline before \end{lyxcode}.
286 // This newline must be ignored,
287 // otherwise LyX will add an additional protected space.
288 if (next.cat() == catSpace ||
289 next.cat() == catNewline ||
290 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
293 context.check_layout(os);
299 * Check wether \param command is a known command. If yes,
300 * handle the command with all arguments.
301 * \return true if the command was parsed, false otherwise.
303 bool parse_command(string const & command, Parser & p, ostream & os,
304 bool outer, Context & context)
306 if (known_commands.find(command) != known_commands.end()) {
307 vector<ArgumentType> const & template_arguments = known_commands[command];
308 string ert = command;
309 size_t no_arguments = template_arguments.size();
310 for (size_t i = 0; i < no_arguments; ++i) {
311 switch (template_arguments[i]) {
313 // This argument contains regular LaTeX
314 handle_ert(os, ert + '{', context);
315 parse_text(p, os, FLAG_ITEM, outer, context);
319 // This argument may contain special characters
320 ert += '{' + p.verbatim_item() + '}';
327 handle_ert(os, ert, context);
334 void parse_environment(Parser & p, ostream & os, bool outer,
335 Context & parent_context)
337 LyXLayout_ptr newlayout;
338 string const name = p.getArg('{', '}');
339 const bool is_starred = suffixIs(name, '*');
340 string const unstarred_name = rtrim(name, "*");
341 active_environments.push_back(name);
344 if (is_math_env(name)) {
345 parent_context.check_layout(os);
346 begin_inset(os, "Formula ");
347 os << "\\begin{" << name << "}";
348 parse_math(p, os, FLAG_END, MATH_MODE);
349 os << "\\end{" << name << "}";
353 else if (name == "tabular") {
354 parent_context.check_layout(os);
355 begin_inset(os, "Tabular ");
356 handle_tabular(p, os, parent_context);
360 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
361 parent_context.check_layout(os);
362 begin_inset(os, "Float " + unstarred_name + "\n");
363 if (p.next_token().asInput() == "[") {
364 os << "placement " << p.getArg('[', ']') << '\n';
366 os << "wide " << tostr(is_starred)
367 << "\ncollapsed false\n\n";
368 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
370 // We don't need really a new paragraph, but
371 // we must make sure that the next item gets a \begin_layout.
372 parent_context.new_paragraph(os);
375 else if (name == "minipage") {
376 string position = "1";
377 string inner_pos = "0";
378 string height = "0pt";
379 string latex_position;
380 string latex_inner_pos;
382 if (p.next_token().asInput() == "[") {
383 latex_position = p.getArg('[', ']');
384 switch(latex_position[0]) {
385 case 't': position = "0"; break;
386 case 'c': position = "1"; break;
387 case 'b': position = "2"; break;
389 cerr << "invalid position for minipage"
393 if (p.next_token().asInput() == "[") {
394 latex_height = p.getArg('[', ']');
395 height = translate_len(latex_height);
397 if (p.next_token().asInput() == "[") {
398 latex_inner_pos = p.getArg('[', ']');
399 switch(latex_inner_pos[0]) {
400 case 'c': inner_pos = "0"; break;
401 case 't': inner_pos = "1"; break;
402 case 'b': inner_pos = "2"; break;
403 case 's': inner_pos = "3"; break;
405 cerr << "invalid inner_pos for minipage"
412 string width = translate_len(p.verbatim_item());
413 if (width[0] == '\\') {
414 // lyx can't handle length variables
416 ss << "\\begin{minipage}";
417 if (!latex_position.empty())
418 ss << '[' << latex_position << ']';
419 if (!latex_height.empty())
420 ss << '[' << latex_height << ']';
421 if (!latex_inner_pos.empty())
422 ss << '[' << latex_inner_pos << ']';
423 ss << "{" << width << "}";
424 handle_ert(os, ss.str(), parent_context);
425 parent_context.new_paragraph(os);
426 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
427 handle_ert(os, "\\end{minipage}", parent_context);
429 parent_context.check_layout(os);
430 begin_inset(os, "Minipage\n");
431 os << "position " << position << '\n';
432 os << "inner_position " << inner_pos << '\n';
433 os << "height \"" << height << "\"\n";
434 os << "width \"" << width << "\"\n";
435 os << "collapsed false\n\n";
436 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
441 // Alignment settings
442 else if (name == "center" || name == "flushleft" || name == "flushright" ||
443 name == "centering" || name == "raggedright" || name == "raggedleft") {
444 // We must begin a new paragraph if not already done
445 if (! parent_context.atParagraphStart()) {
446 parent_context.check_end_layout(os);
447 parent_context.new_paragraph(os);
449 if (name == "flushleft" || name == "raggedright")
450 parent_context.extra_stuff += "\\align left ";
451 else if (name == "flushright" || name == "raggedleft")
452 parent_context.extra_stuff += "\\align right ";
454 parent_context.extra_stuff += "\\align center ";
455 parse_text(p, os, FLAG_END, outer, parent_context);
456 // Just in case the environment is empty ..
457 parent_context.extra_stuff.erase();
458 // We must begin a new paragraph to reset the alignment
459 parent_context.new_paragraph(os);
462 // The single '=' is meant here.
463 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
464 newlayout->isEnvironment()) {
465 Context context(true, parent_context.textclass, newlayout,
466 parent_context.layout);
467 parent_context.check_end_layout(os);
468 switch (context.layout->latextype) {
469 case LATEX_LIST_ENVIRONMENT:
470 context.extra_stuff = "\\labelwidthstring "
471 + p.verbatim_item() + '\n';
474 case LATEX_BIB_ENVIRONMENT:
475 p.verbatim_item(); // swallow next arg
481 context.check_deeper(os);
482 parse_text(p, os, FLAG_END, outer, context);
483 context.check_end_layout(os);
484 context.check_end_deeper(os);
485 parent_context.new_paragraph(os);
488 else if (name == "appendix") {
489 // This is no good latex style, but it works and is used in some documents...
490 parent_context.check_end_layout(os);
491 Context context(true, parent_context.textclass, parent_context.layout,
492 parent_context.layout);
493 context.check_layout(os);
494 os << "\\start_of_appendix\n";
495 parse_text(p, os, FLAG_END, outer, context);
496 context.check_end_layout(os);
499 else if (name == "comment") {
500 parent_context.check_layout(os);
501 begin_inset(os, "Comment\n");
502 os << "collapsed false\n";
503 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
507 else if (name == "tabbing") {
508 // We need to remember that we have to handle '\=' specially
509 handle_ert(os, "\\begin{" + name + "}", parent_context);
510 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
511 handle_ert(os, "\\end{" + name + "}", parent_context);
515 handle_ert(os, "\\begin{" + name + "}", parent_context);
516 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
517 handle_ert(os, "\\end{" + name + "}", parent_context);
520 active_environments.pop_back();
525 } // anonymous namespace
530 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
533 LyXLayout_ptr newlayout;
534 // Store the latest bibliographystyle (needed for bibtex inset)
535 string bibliographystyle;
537 Token const & t = p.get_token();
540 cerr << "t: " << t << " flags: " << flags << "\n";
543 if (flags & FLAG_ITEM) {
544 if (t.cat() == catSpace)
548 if (t.cat() == catBegin) {
549 // skip the brace and collect everything to the next matching
551 flags |= FLAG_BRACE_LAST;
555 // handle only this single token, leave the loop if done
559 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
565 if (t.cat() == catMath) {
566 // we are inside some text mode thingy, so opening new math is allowed
567 context.check_layout(os);
568 begin_inset(os, "Formula ");
569 Token const & n = p.get_token();
570 if (n.cat() == catMath && outer) {
571 // TeX's $$...$$ syntax for displayed math
573 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
575 p.get_token(); // skip the second '$' token
577 // simple $...$ stuff
580 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
586 else if (t.cat() == catSuper || t.cat() == catSub)
587 cerr << "catcode " << t << " illegal in text mode\n";
589 // Basic support for english quotes. This should be
590 // extended to other quotes, but is not so easy (a
591 // left english quote is the same as a right german
593 else if (t.asInput() == "`"
594 && p.next_token().asInput() == "`") {
595 context.check_layout(os);
596 begin_inset(os, "Quotes ");
602 else if (t.asInput() == "'"
603 && p.next_token().asInput() == "'") {
604 context.check_layout(os);
605 begin_inset(os, "Quotes ");
612 else if (t.cat() == catSpace || (t.cat() == catNewline && t.cs().size() == 1))
613 check_space(p, os, context);
615 else if (t.cat() == catLetter ||
616 t.cat() == catOther ||
617 t.cat() == catAlign ||
618 t.cat() == catParameter) {
619 context.check_layout(os);
623 else if (t.cat() == catNewline || (t.cat() == catEscape && t.cs() == "par")) {
625 context.new_paragraph(os);
628 else if (t.cat() == catActive) {
629 context.check_layout(os);
630 if (t.character() == '~') {
631 if (context.layout->free_spacing)
634 os << "\\InsetSpace ~\n";
639 else if (t.cat() == catBegin) {
640 // special handling of size changes
641 context.check_layout(os);
642 bool const is_size = is_known(p.next_token().cs(), known_sizes);
643 Token const prev = p.prev_token();
644 string const s = parse_text(p, FLAG_BRACE_LAST, outer, context);
645 if (s.empty() && (p.next_token().character() == '`' ||
646 (prev.character() == '-' && p.next_token().character())))
647 ; // ignore it in {}`` or -{}-
648 else if (is_size || s == "[" || s == "]" || s == "*")
651 handle_ert(os, "{", context, false);
652 // s will end the current layout and begin a new one if necessary
654 handle_ert(os, "}", context);
658 else if (t.cat() == catEnd) {
659 if (flags & FLAG_BRACE_LAST) {
662 cerr << "stray '}' in text\n";
663 handle_ert(os, "}", context);
666 else if (t.cat() == catComment) {
667 context.check_layout(os);
668 if (!t.cs().empty()) {
669 handle_comment(os, '%' + t.cs(), context);
670 if (p.next_token().cat() == catNewline) {
671 // A newline after a comment line starts a new paragraph
672 context.new_paragraph(os);
685 else if (t.cs() == "(") {
686 context.check_layout(os);
687 begin_inset(os, "Formula");
689 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
694 else if (t.cs() == "[") {
695 context.check_layout(os);
696 begin_inset(os, "Formula");
698 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
703 else if (t.cs() == "begin")
704 parse_environment(p, os, outer, context);
706 else if (t.cs() == "end") {
707 if (flags & FLAG_END) {
708 // eat environment name
709 string const name = p.getArg('{', '}');
710 if (name != active_environment())
711 cerr << "\\end{" + name + "} does not match \\begin{"
712 + active_environment() + "}\n";
715 p.error("found 'end' unexpectedly");
718 else if (t.cs() == "item") {
722 if (p.next_token().character() == '[') {
723 p.get_token(); // eat '['
724 Context newcontext(false, context.textclass);
725 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
729 context.check_layout(os);
731 if (context.layout->labeltype != LABEL_MANUAL) {
732 // lyx does not support \item[\mybullet] in itemize environments
733 handle_ert(os, "[", context);
735 handle_ert(os, "]", context);
736 } else if (!s.empty()) {
737 // The space is needed to separate the item from the rest of the sentence.
744 else if (t.cs() == "bibitem") {
746 context.check_layout(os);
749 os << '{' << p.verbatim_item() << '}' << "\n";
752 else if (t.cs() == "def") {
754 context.check_layout(os);
755 string name = p.get_token().cs();
756 while (p.next_token().cat() != catBegin)
757 name += p.get_token().asString();
758 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
761 else if (t.cs() == "noindent") {
763 context.extra_stuff += "\\noindent ";
766 else if (t.cs() == "appendix") {
768 context.extra_stuff += "\\start_of_appendix ";
771 // Must attempt to parse "Section*" before "Section".
772 else if ((p.next_token().asInput() == "*") &&
773 // The single '=' is meant here.
774 (newlayout = findLayout(context.textclass,
775 t.cs() + '*')).get() &&
776 newlayout->isCommand()) {
778 output_command_layout(os, p, outer, context, newlayout);
782 // The single '=' is meant here.
783 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
784 newlayout->isCommand()) {
785 output_command_layout(os, p, outer, context, newlayout);
789 else if (t.cs() == "includegraphics") {
790 map<string, string> opts = split_map(p.getArg('[', ']'));
791 string name = p.verbatim_item();
793 context.check_layout(os);
794 begin_inset(os, "Graphics ");
795 os << "\n\tfilename " << name << '\n';
796 if (opts.find("width") != opts.end())
798 << translate_len(opts["width"]) << '\n';
799 if (opts.find("height") != opts.end())
801 << translate_len(opts["height"]) << '\n';
802 if (opts.find("scale") != opts.end()) {
803 istringstream iss(opts["scale"]);
807 os << "\tscale " << val << '\n';
809 if (opts.find("angle") != opts.end())
810 os << "\trotateAngle "
811 << opts["angle"] << '\n';
812 if (opts.find("origin") != opts.end()) {
814 string const opt = opts["origin"];
815 if (opt.find('l') != string::npos) ss << "left";
816 if (opt.find('r') != string::npos) ss << "right";
817 if (opt.find('c') != string::npos) ss << "center";
818 if (opt.find('t') != string::npos) ss << "Top";
819 if (opt.find('b') != string::npos) ss << "Bottom";
820 if (opt.find('B') != string::npos) ss << "Baseline";
821 if (!ss.str().empty())
822 os << "\trotateOrigin " << ss.str() << '\n';
824 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
826 if (opts.find("keepaspectratio") != opts.end())
827 os << "\tkeepAspectRatio\n";
828 if (opts.find("clip") != opts.end())
830 if (opts.find("draft") != opts.end())
832 if (opts.find("bb") != opts.end())
833 os << "\tBoundingBox "
834 << opts["bb"] << '\n';
835 int numberOfbbOptions = 0;
836 if (opts.find("bbllx") != opts.end())
838 if (opts.find("bblly") != opts.end())
840 if (opts.find("bburx") != opts.end())
842 if (opts.find("bbury") != opts.end())
844 if (numberOfbbOptions == 4)
845 os << "\tBoundingBox "
846 << opts["bbllx"] << opts["bblly"]
847 << opts["bburx"] << opts["bbury"] << '\n';
848 else if (numberOfbbOptions > 0)
849 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
850 numberOfbbOptions = 0;
851 if (opts.find("natwidth") != opts.end())
853 if (opts.find("natheight") != opts.end())
855 if (numberOfbbOptions == 2)
856 os << "\tBoundingBox 0bp 0bp "
857 << opts["natwidth"] << opts["natheight"] << '\n';
858 else if (numberOfbbOptions > 0)
859 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
860 ostringstream special;
861 if (opts.find("hiresbb") != opts.end())
862 special << "hiresbb,";
863 if (opts.find("trim") != opts.end())
865 if (opts.find("viewport") != opts.end())
866 special << "viewport=" << opts["viewport"] << ',';
867 if (opts.find("totalheight") != opts.end())
868 special << "totalheight=" << opts["totalheight"] << ',';
869 if (opts.find("type") != opts.end())
870 special << "type=" << opts["type"] << ',';
871 if (opts.find("ext") != opts.end())
872 special << "ext=" << opts["ext"] << ',';
873 if (opts.find("read") != opts.end())
874 special << "read=" << opts["read"] << ',';
875 if (opts.find("command") != opts.end())
876 special << "command=" << opts["command"] << ',';
877 string s_special = special.str();
878 if (!s_special.empty()) {
879 // We had special arguments. Remove the trailing ','.
880 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
882 // TODO: Handle the unknown settings better.
883 // Warn about invalid options.
884 // Check wether some option was given twice.
888 else if (t.cs() == "footnote") {
890 context.check_layout(os);
891 begin_inset(os, "Foot\n");
892 os << "collapsed true\n\n";
893 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
897 else if (t.cs() == "marginpar") {
899 context.check_layout(os);
900 begin_inset(os, "Marginal\n");
901 os << "collapsed true\n\n";
902 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
906 else if (t.cs() == "ensuremath") {
908 context.check_layout(os);
909 Context newcontext(false, context.textclass);
910 string s = parse_text(p, FLAG_ITEM, false, newcontext);
911 if (s == "±" || s == "³" || s == "²" || s == "µ")
914 handle_ert(os, "\\ensuremath{" + s + "}",
918 else if (t.cs() == "hfill") {
919 context.check_layout(os);
925 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
927 skip_braces(p); // swallow this
930 else if (t.cs() == "tableofcontents") {
932 context.check_layout(os);
933 begin_inset(os, "LatexCommand \\tableofcontents\n");
935 skip_braces(p); // swallow this
938 else if (t.cs() == "listoffigures") {
940 context.check_layout(os);
941 begin_inset(os, "FloatList figure\n");
943 skip_braces(p); // swallow this
946 else if (t.cs() == "listoftables") {
948 context.check_layout(os);
949 begin_inset(os, "FloatList table\n");
951 skip_braces(p); // swallow this
954 else if (t.cs() == "listof") {
956 string const name = p.get_token().asString();
957 if (context.textclass.floats().typeExist(name)) {
958 context.check_layout(os);
959 begin_inset(os, "FloatList ");
962 p.get_token(); // swallow second arg
964 handle_ert(os, "\\listof{" + name + "}", context);
967 else if (t.cs() == "textrm") {
968 context.check_layout(os);
969 os << "\n\\family roman \n";
970 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
971 os << "\n\\family default \n";
974 else if (t.cs() == "textsf") {
975 context.check_layout(os);
976 os << "\n\\family sans \n";
977 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
978 os << "\n\\family default \n";
981 else if (t.cs() == "textsl") {
982 context.check_layout(os);
983 os << "\n\\shape slanted \n";
984 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
985 os << "\n\\shape default \n";
988 else if (t.cs() == "texttt") {
989 context.check_layout(os);
990 os << "\n\\family typewriter \n";
991 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
992 os << "\n\\family default \n";
995 else if (t.cs() == "textit") {
996 context.check_layout(os);
997 os << "\n\\shape italic \n";
998 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
999 os << "\n\\shape default \n";
1002 else if (t.cs() == "textsc") {
1003 context.check_layout(os);
1004 os << "\n\\noun on \n";
1005 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1006 os << "\n\\noun default \n";
1009 else if (t.cs() == "textbf") {
1010 context.check_layout(os);
1011 os << "\n\\series bold \n";
1012 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1013 os << "\n\\series default \n";
1016 else if (t.cs() == "underbar") {
1017 context.check_layout(os);
1018 os << "\n\\bar under \n";
1019 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1020 os << "\n\\bar default \n";
1023 else if (t.cs() == "emph" || t.cs() == "noun") {
1024 context.check_layout(os);
1025 os << "\n\\" << t.cs() << " on \n";
1026 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1027 os << "\n\\" << t.cs() << " default \n";
1030 else if (is_known(t.cs(), known_latex_commands)) {
1031 context.check_layout(os);
1032 begin_inset(os, "LatexCommand ");
1033 os << '\\' << t.cs();
1036 os << '{' << p.verbatim_item() << "}\n";
1040 else if (is_known(t.cs(), known_quotes)) {
1041 char const ** where = is_known(t.cs(), known_quotes);
1042 context.check_layout(os);
1043 begin_inset(os, "Quotes ");
1044 os << known_coded_quotes[where - known_quotes];
1049 else if (is_known(t.cs(), known_sizes)) {
1050 char const ** where = is_known(t.cs(), known_sizes);
1051 context.check_layout(os);
1052 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
1056 else if (t.cs() == "LyX" || t.cs() == "TeX"
1057 || t.cs() == "LaTeX") {
1058 context.check_layout(os);
1060 skip_braces(p); // eat {}
1063 else if (t.cs() == "LaTeXe") {
1064 context.check_layout(os);
1066 skip_braces(p); // eat {}
1069 else if (t.cs() == "ldots") {
1070 context.check_layout(os);
1072 os << "\\SpecialChar \\ldots{}\n";
1075 else if (t.cs() == "lyxarrow") {
1076 context.check_layout(os);
1077 os << "\\SpecialChar \\menuseparator\n";
1081 else if (t.cs() == "textcompwordmark") {
1082 context.check_layout(os);
1083 os << "\\SpecialChar \\textcompwordmark{}\n";
1087 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1088 context.check_layout(os);
1089 os << "\\SpecialChar \\@.\n";
1093 else if (t.cs() == "-") {
1094 context.check_layout(os);
1095 os << "\\SpecialChar \\-\n";
1098 else if (t.cs() == "textasciitilde") {
1099 context.check_layout(os);
1104 else if (t.cs() == "textasciicircum") {
1105 context.check_layout(os);
1110 else if (t.cs() == "textbackslash") {
1111 context.check_layout(os);
1112 os << "\n\\backslash \n";
1116 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1117 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1119 context.check_layout(os);
1123 else if (t.cs() == "char") {
1124 context.check_layout(os);
1125 if (p.next_token().character() == '`') {
1127 if (p.next_token().cs() == "\"") {
1132 handle_ert(os, "\\char`", context);
1135 handle_ert(os, "\\char", context);
1139 else if (t.cs() == "\"") {
1140 context.check_layout(os);
1141 string const name = p.verbatim_item();
1142 if (name == "a") os << 'ä';
1143 else if (name == "o") os << 'ö';
1144 else if (name == "u") os << 'ü';
1145 else if (name == "A") os << 'Ä';
1146 else if (name == "O") os << 'Ö';
1147 else if (name == "U") os << 'Ü';
1148 else handle_ert(os, "\"{" + name + "}", context);
1151 // Problem: \= creates a tabstop inside the tabbing environment
1152 // and else an accent. In the latter case we really would want
1153 // \={o} instead of \= o.
1154 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1155 || t.cs() == "~" || t.cs() == "." || (t.cs() == "=" && ! (flags & FLAG_TABBING))) {
1156 // we need the trim as the LyX parser chokes on such spaces
1157 context.check_layout(os);
1158 os << "\n\\i \\" << t.cs() << "{"
1159 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1162 else if (t.cs() == "ss") {
1163 context.check_layout(os);
1167 else if (t.cs() == "i" || t.cs() == "j") {
1168 context.check_layout(os);
1169 os << "\\" << t.cs() << ' ';
1172 else if (t.cs() == "\\") {
1173 context.check_layout(os);
1174 string const next = p.next_token().asInput();
1176 handle_ert(os, "\\\\" + p.getOpt(), context);
1177 else if (next == "*") {
1179 handle_ert(os, "\\\\*" + p.getOpt(), context);
1182 os << "\n\\newline \n";
1186 else if (t.cs() == "input" || t.cs() == "include"
1187 || t.cs() == "verbatiminput") {
1188 string name = '\\' + t.cs();
1189 if (t.cs() == "verbatiminput"
1190 && p.next_token().asInput() == "*")
1191 name += p.get_token().asInput();
1192 context.check_layout(os);
1193 begin_inset(os, "Include ");
1194 string filename(p.getArg('{', '}'));
1195 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1196 if (tex2lyx(filename, lyxname)) {
1197 os << name << '{' << lyxname << "}\n";
1199 os << name << '{' << filename << "}\n";
1201 os << "preview false\n";
1205 else if (t.cs() == "fancyhead") {
1206 context.check_layout(os);
1208 ss << "\\fancyhead";
1210 ss << '{' << p.verbatim_item() << "}\n";
1211 handle_ert(os, ss.str(), context);
1214 else if (t.cs() == "bibliographystyle") {
1215 // store new bibliographystyle
1216 bibliographystyle = p.verbatim_item();
1217 // output new bibliographystyle.
1218 // This is only necessary if used in some other macro than \bibliography.
1219 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1222 else if (t.cs() == "bibliography") {
1223 context.check_layout(os);
1224 begin_inset(os, "LatexCommand ");
1226 // Do we have a bibliographystyle set?
1227 if (!bibliographystyle.empty()) {
1228 os << '[' << bibliographystyle << ']';
1230 os << '{' << p.verbatim_item() << "}\n";
1234 else if ( t.cs() == "smallskip" ||
1235 t.cs() == "medskip" ||
1236 t.cs() == "bigskip" ||
1237 t.cs() == "vfill" ||
1238 (t.cs() == "vspace" && p.next_token().asInput() != "*")) {
1240 if (t.cs() == "vspace")
1241 arg = p.getArg('{', '}');
1244 // We may only add the vspace to the current context if the
1245 // current paragraph is not empty.
1246 if (context.atParagraphStart()
1247 && (p.next_token().cat() != catNewline || p.next_token().cs().size() == 1)
1248 && (! (p.next_token().cat() == catEscape && p.next_token().cs() == "end"))
1249 && (! (p.next_token().cat() == catEscape && p.next_token().cs() == "par"))) {
1250 context.extra_stuff += "\\added_space_top " + arg + " ";
1253 if (t.cs() == "vspace")
1254 handle_ert(os, t.asInput() + '{' + arg + '}', context);
1256 handle_ert(os, t.asInput(), context);
1258 // Would be nice to recognize added_space_bottom too...
1259 // At the moment this is parsed as added_space_top of the
1264 //cerr << "#: " << t << " mode: " << mode << endl;
1265 // heuristic: read up to next non-nested space
1267 string s = t.asInput();
1268 string z = p.verbatim_item();
1269 while (p.good() && z != " " && z.size()) {
1270 //cerr << "read: " << z << endl;
1272 z = p.verbatim_item();
1274 cerr << "found ERT: " << s << endl;
1275 handle_ert(os, s + ' ', context);
1277 string name = t.asInput();
1278 if (p.next_token().asInput() == "*") {
1279 // Starred commands like \vspace*{}
1280 p.get_token(); // Eat '*'
1283 if (! parse_command(t.asInput(), p, os, outer, context))
1284 handle_ert(os, name, context);
1287 if (flags & FLAG_LEAVE) {
1288 flags &= ~FLAG_LEAVE;