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);
297 void parse_environment(Parser & p, ostream & os, bool outer,
298 Context & parent_context)
300 LyXLayout_ptr newlayout;
301 string const name = p.getArg('{', '}');
302 const bool is_starred = suffixIs(name, '*');
303 string const unstarred_name = rtrim(name, "*");
304 active_environments.push_back(name);
307 if (is_math_env(name)) {
308 parent_context.check_layout(os);
309 begin_inset(os, "Formula ");
310 os << "\\begin{" << name << "}";
311 parse_math(p, os, FLAG_END, MATH_MODE);
312 os << "\\end{" << name << "}";
316 else if (name == "tabular") {
317 parent_context.check_layout(os);
318 begin_inset(os, "Tabular ");
319 handle_tabular(p, os, parent_context);
323 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
324 parent_context.check_layout(os);
325 begin_inset(os, "Float " + unstarred_name + "\n");
326 if (p.next_token().asInput() == "[") {
327 os << "placement " << p.getArg('[', ']') << '\n';
329 os << "wide " << tostr(is_starred)
330 << "\ncollapsed false\n\n";
331 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
333 // We don't need really a new paragraph, but
334 // we must make sure that the next item gets a \begin_layout.
335 parent_context.new_paragraph(os);
338 else if (name == "minipage") {
339 string position = "1";
340 string inner_pos = "0";
341 string height = "0pt";
342 string latex_position;
343 string latex_inner_pos;
345 if (p.next_token().asInput() == "[") {
346 latex_position = p.getArg('[', ']');
347 switch(latex_position[0]) {
348 case 't': position = "0"; break;
349 case 'c': position = "1"; break;
350 case 'b': position = "2"; break;
352 cerr << "invalid position for minipage"
356 if (p.next_token().asInput() == "[") {
357 latex_height = p.getArg('[', ']');
358 height = translate_len(latex_height);
360 if (p.next_token().asInput() == "[") {
361 latex_inner_pos = p.getArg('[', ']');
362 switch(latex_inner_pos[0]) {
363 case 'c': inner_pos = "0"; break;
364 case 't': inner_pos = "1"; break;
365 case 'b': inner_pos = "2"; break;
366 case 's': inner_pos = "3"; break;
368 cerr << "invalid inner_pos for minipage"
375 string width = translate_len(p.verbatim_item());
376 if (width[0] == '\\') {
377 // lyx can't handle length variables
379 ss << "\\begin{minipage}";
380 if (latex_position.size())
381 ss << '[' << latex_position << ']';
382 if (latex_height.size())
383 ss << '[' << latex_height << ']';
384 if (latex_inner_pos.size())
385 ss << '[' << latex_inner_pos << ']';
386 ss << "{" << width << "}";
387 handle_ert(os, ss.str(), parent_context);
388 parent_context.new_paragraph(os);
389 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
390 handle_ert(os, "\\end{minipage}", parent_context);
392 parent_context.check_layout(os);
393 begin_inset(os, "Minipage\n");
394 os << "position " << position << '\n';
395 os << "inner_position " << inner_pos << '\n';
396 os << "height \"" << height << "\"\n";
397 os << "width \"" << width << "\"\n";
398 os << "collapsed false\n\n";
399 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
404 // Alignment settings
405 else if (name == "center" || name == "flushleft" || name == "flushright" ||
406 name == "centering" || name == "raggedright" || name == "raggedleft") {
407 // We must begin a new paragraph if not already done
408 if (! parent_context.atParagraphStart()) {
409 parent_context.check_end_layout(os);
410 parent_context.new_paragraph(os);
412 if (name == "flushleft" || name == "raggedright")
413 parent_context.extra_stuff += "\\align left ";
414 else if (name == "flushright" || name == "raggedleft")
415 parent_context.extra_stuff += "\\align right ";
417 parent_context.extra_stuff += "\\align center ";
418 parse_text(p, os, FLAG_END, outer, parent_context);
419 // Just in case the environment is empty ..
420 parent_context.extra_stuff.erase();
421 // We must begin a new paragraph to reset the alignment
422 parent_context.new_paragraph(os);
425 // The single '=' is meant here.
426 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
427 newlayout->isEnvironment()) {
428 Context context(true, parent_context.textclass, newlayout,
429 parent_context.layout);
430 parent_context.check_end_layout(os);
431 switch (context.layout->latextype) {
432 case LATEX_LIST_ENVIRONMENT:
433 context.extra_stuff = "\\labelwidthstring "
434 + p.verbatim_item() + '\n';
437 case LATEX_BIB_ENVIRONMENT:
438 p.verbatim_item(); // swallow next arg
444 context.check_deeper(os);
445 parse_text(p, os, FLAG_END, outer, context);
446 context.check_end_layout(os);
447 context.check_end_deeper(os);
448 parent_context.new_paragraph(os);
451 else if (name == "appendix") {
452 // This is no good latex style, but it works and is used in some documents...
453 parent_context.check_end_layout(os);
454 Context context(true, parent_context.textclass, parent_context.layout,
455 parent_context.layout);
456 context.check_layout(os);
457 os << "\\start_of_appendix\n";
458 parse_text(p, os, FLAG_END, outer, context);
459 context.check_end_layout(os);
462 else if (name == "comment") {
463 parent_context.check_layout(os);
464 begin_inset(os, "Comment\n");
465 os << "collapsed false\n";
466 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
470 else if (name == "tabbing") {
471 // We need to remember that we have to handle '\=' specially
472 handle_ert(os, "\\begin{" + name + "}", parent_context);
473 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
474 handle_ert(os, "\\end{" + name + "}", parent_context);
478 handle_ert(os, "\\begin{" + name + "}", parent_context);
479 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
480 handle_ert(os, "\\end{" + name + "}", parent_context);
483 active_environments.pop_back();
488 } // anonymous namespace
493 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
496 LyXLayout_ptr newlayout;
497 // Store the latest bibliographystyle (needed for bibtex inset)
498 string bibliographystyle;
500 Token const & t = p.get_token();
503 cerr << "t: " << t << " flags: " << flags << "\n";
506 if (flags & FLAG_ITEM) {
507 if (t.cat() == catSpace)
511 if (t.cat() == catBegin) {
512 // skip the brace and collect everything to the next matching
514 flags |= FLAG_BRACE_LAST;
518 // handle only this single token, leave the loop if done
522 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
528 if (t.cat() == catMath) {
529 // we are inside some text mode thingy, so opening new math is allowed
530 context.check_layout(os);
531 begin_inset(os, "Formula ");
532 Token const & n = p.get_token();
533 if (n.cat() == catMath && outer) {
534 // TeX's $$...$$ syntax for displayed math
536 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
538 p.get_token(); // skip the second '$' token
540 // simple $...$ stuff
543 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
549 else if (t.cat() == catSuper || t.cat() == catSub)
550 cerr << "catcode " << t << " illegal in text mode\n";
552 // Basic support for english quotes. This should be
553 // extended to other quotes, but is not so easy (a
554 // left english quote is the same as a right german
556 else if (t.asInput() == "`"
557 && p.next_token().asInput() == "`") {
558 context.check_layout(os);
559 begin_inset(os, "Quotes ");
565 else if (t.asInput() == "'"
566 && p.next_token().asInput() == "'") {
567 context.check_layout(os);
568 begin_inset(os, "Quotes ");
575 else if (t.cat() == catSpace || (t.cat() == catNewline && t.cs().size() == 1))
576 check_space(p, os, context);
578 else if (t.cat() == catLetter ||
579 t.cat() == catOther ||
580 t.cat() == catAlign ||
581 t.cat() == catParameter) {
582 context.check_layout(os);
586 else if (t.cat() == catNewline || (t.cat() == catEscape && t.cs() == "par")) {
588 context.new_paragraph(os);
591 else if (t.cat() == catActive) {
592 context.check_layout(os);
593 if (t.character() == '~') {
594 if (context.layout->free_spacing)
597 os << "\\InsetSpace ~\n";
602 else if (t.cat() == catBegin) {
603 // special handling of size changes
604 context.check_layout(os);
605 bool const is_size = is_known(p.next_token().cs(), known_sizes);
606 Token const prev = p.prev_token();
607 string const s = parse_text(p, FLAG_BRACE_LAST, outer, context);
608 if (s.empty() && (p.next_token().character() == '`' ||
609 (prev.character() == '-' && p.next_token().character())))
610 ; // ignore it in {}`` or -{}-
611 else if (is_size || s == "[" || s == "]" || s == "*")
614 handle_ert(os, "{", context, false);
615 // s will end the current layout and begin a new one if necessary
617 handle_ert(os, "}", context);
621 else if (t.cat() == catEnd) {
622 if (flags & FLAG_BRACE_LAST) {
625 cerr << "stray '}' in text\n";
626 handle_ert(os, "}", context);
629 else if (t.cat() == catComment) {
630 context.check_layout(os);
632 handle_comment(os, '%' + t.cs(), context);
633 if (p.next_token().cat() == catNewline) {
634 // A newline after a comment line starts a new paragraph
635 context.new_paragraph(os);
648 else if (t.cs() == "(") {
649 context.check_layout(os);
650 begin_inset(os, "Formula");
652 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
657 else if (t.cs() == "[") {
658 context.check_layout(os);
659 begin_inset(os, "Formula");
661 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
666 else if (t.cs() == "begin")
667 parse_environment(p, os, outer, context);
669 else if (t.cs() == "end") {
670 if (flags & FLAG_END) {
671 // eat environment name
672 string const name = p.getArg('{', '}');
673 if (name != active_environment())
674 cerr << "\\end{" + name + "} does not match \\begin{"
675 + active_environment() + "}\n";
678 p.error("found 'end' unexpectedly");
681 else if (t.cs() == "item") {
685 if (p.next_token().character() == '[') {
686 p.get_token(); // eat '['
687 Context newcontext(false, context.textclass);
688 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
692 context.check_layout(os);
694 if (context.layout->labeltype != LABEL_MANUAL) {
695 // lyx does not support \item[\mybullet] in itemize environments
696 handle_ert(os, "[", context);
698 handle_ert(os, "]", context);
699 } else if (s.size()) {
700 // The space is needed to separate the item from the rest of the sentence.
707 else if (t.cs() == "bibitem") {
709 context.check_layout(os);
712 os << '{' << p.verbatim_item() << '}' << "\n";
715 else if (t.cs() == "def") {
717 context.check_layout(os);
718 string name = p.get_token().cs();
719 while (p.next_token().cat() != catBegin)
720 name += p.get_token().asString();
721 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
724 else if (t.cs() == "noindent") {
726 context.extra_stuff += "\\noindent ";
729 else if (t.cs() == "appendix") {
731 context.extra_stuff += "\\start_of_appendix ";
734 // Must attempt to parse "Section*" before "Section".
735 else if ((p.next_token().asInput() == "*") &&
736 // The single '=' is meant here.
737 (newlayout = findLayout(context.textclass,
738 t.cs() + '*')).get() &&
739 newlayout->isCommand()) {
741 output_command_layout(os, p, outer, context, newlayout);
745 // The single '=' is meant here.
746 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
747 newlayout->isCommand()) {
748 output_command_layout(os, p, outer, context, newlayout);
752 else if (t.cs() == "includegraphics") {
753 map<string, string> opts = split_map(p.getArg('[', ']'));
754 string name = p.verbatim_item();
756 context.check_layout(os);
757 begin_inset(os, "Graphics ");
758 os << "\n\tfilename " << name << '\n';
759 if (opts.find("width") != opts.end())
761 << translate_len(opts["width"]) << '\n';
762 if (opts.find("height") != opts.end())
764 << translate_len(opts["height"]) << '\n';
765 if (opts.find("scale") != opts.end()) {
766 istringstream iss(opts["scale"]);
770 os << "\tscale " << val << '\n';
772 if (opts.find("angle") != opts.end())
773 os << "\trotateAngle "
774 << opts["angle"] << '\n';
775 if (opts.find("origin") != opts.end()) {
777 string const opt = opts["origin"];
778 if (opt.find('l') != string::npos) ss << "left";
779 if (opt.find('r') != string::npos) ss << "right";
780 if (opt.find('c') != string::npos) ss << "center";
781 if (opt.find('t') != string::npos) ss << "Top";
782 if (opt.find('b') != string::npos) ss << "Bottom";
783 if (opt.find('B') != string::npos) ss << "Baseline";
785 os << "\trotateOrigin " << ss.str() << '\n';
787 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
789 if (opts.find("keepaspectratio") != opts.end())
790 os << "\tkeepAspectRatio\n";
791 if (opts.find("clip") != opts.end())
793 if (opts.find("draft") != opts.end())
795 if (opts.find("bb") != opts.end())
796 os << "\tBoundingBox "
797 << opts["bb"] << '\n';
798 int numberOfbbOptions = 0;
799 if (opts.find("bbllx") != opts.end())
801 if (opts.find("bblly") != opts.end())
803 if (opts.find("bburx") != opts.end())
805 if (opts.find("bbury") != opts.end())
807 if (numberOfbbOptions == 4)
808 os << "\tBoundingBox "
809 << opts["bbllx"] << opts["bblly"]
810 << opts["bburx"] << opts["bbury"] << '\n';
811 else if (numberOfbbOptions > 0)
812 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
813 numberOfbbOptions = 0;
814 if (opts.find("natwidth") != opts.end())
816 if (opts.find("natheight") != opts.end())
818 if (numberOfbbOptions == 2)
819 os << "\tBoundingBox 0bp 0bp "
820 << opts["natwidth"] << opts["natheight"] << '\n';
821 else if (numberOfbbOptions > 0)
822 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
823 ostringstream special;
824 if (opts.find("hiresbb") != opts.end())
825 special << "hiresbb,";
826 if (opts.find("trim") != opts.end())
828 if (opts.find("viewport") != opts.end())
829 special << "viewport=" << opts["viewport"] << ',';
830 if (opts.find("totalheight") != opts.end())
831 special << "totalheight=" << opts["totalheight"] << ',';
832 if (opts.find("type") != opts.end())
833 special << "type=" << opts["type"] << ',';
834 if (opts.find("ext") != opts.end())
835 special << "ext=" << opts["ext"] << ',';
836 if (opts.find("read") != opts.end())
837 special << "read=" << opts["read"] << ',';
838 if (opts.find("command") != opts.end())
839 special << "command=" << opts["command"] << ',';
840 string s_special = special.str();
841 if (s_special.size()) {
842 // We had special arguments. Remove the trailing ','.
843 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
845 // TODO: Handle the unknown settings better.
846 // Warn about invalid options.
847 // Check wether some option was given twice.
851 else if (t.cs() == "footnote") {
853 context.check_layout(os);
854 begin_inset(os, "Foot\n");
855 os << "collapsed true\n\n";
856 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
860 else if (t.cs() == "marginpar") {
862 context.check_layout(os);
863 begin_inset(os, "Marginal\n");
864 os << "collapsed true\n\n";
865 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
869 else if (t.cs() == "ensuremath") {
871 context.check_layout(os);
872 Context newcontext(false, context.textclass);
873 string s = parse_text(p, FLAG_ITEM, false, newcontext);
874 if (s == "±" || s == "³" || s == "²" || s == "µ")
877 handle_ert(os, "\\ensuremath{" + s + "}",
881 else if (t.cs() == "hfill") {
882 context.check_layout(os);
888 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
890 skip_braces(p); // swallow this
893 else if (t.cs() == "tableofcontents") {
895 context.check_layout(os);
896 begin_inset(os, "LatexCommand \\tableofcontents\n");
898 skip_braces(p); // swallow this
901 else if (t.cs() == "listoffigures") {
903 context.check_layout(os);
904 begin_inset(os, "FloatList figure\n");
906 skip_braces(p); // swallow this
909 else if (t.cs() == "listoftables") {
911 context.check_layout(os);
912 begin_inset(os, "FloatList table\n");
914 skip_braces(p); // swallow this
917 else if (t.cs() == "listof") {
919 string const name = p.get_token().asString();
920 if (context.textclass.floats().typeExist(name)) {
921 context.check_layout(os);
922 begin_inset(os, "FloatList ");
925 p.get_token(); // swallow second arg
927 handle_ert(os, "\\listof{" + name + "}", context);
930 else if (t.cs() == "textrm") {
931 context.check_layout(os);
932 os << "\n\\family roman \n";
933 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
934 os << "\n\\family default \n";
937 else if (t.cs() == "textsf") {
938 context.check_layout(os);
939 os << "\n\\family sans \n";
940 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
941 os << "\n\\family default \n";
944 else if (t.cs() == "textsl") {
945 context.check_layout(os);
946 os << "\n\\shape slanted \n";
947 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
948 os << "\n\\shape default \n";
951 else if (t.cs() == "texttt") {
952 context.check_layout(os);
953 os << "\n\\family typewriter \n";
954 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
955 os << "\n\\family default \n";
958 else if (t.cs() == "textit") {
959 context.check_layout(os);
960 os << "\n\\shape italic \n";
961 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
962 os << "\n\\shape default \n";
965 else if (t.cs() == "textsc") {
966 context.check_layout(os);
967 os << "\n\\noun on \n";
968 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
969 os << "\n\\noun default \n";
972 else if (t.cs() == "textbf") {
973 context.check_layout(os);
974 os << "\n\\series bold \n";
975 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
976 os << "\n\\series default \n";
979 else if (t.cs() == "underbar") {
980 context.check_layout(os);
981 os << "\n\\bar under \n";
982 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
983 os << "\n\\bar default \n";
986 else if (t.cs() == "emph" || t.cs() == "noun") {
987 context.check_layout(os);
988 os << "\n\\" << t.cs() << " on \n";
989 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
990 os << "\n\\" << t.cs() << " default \n";
993 else if (is_known(t.cs(), known_latex_commands)) {
994 context.check_layout(os);
995 begin_inset(os, "LatexCommand ");
996 os << '\\' << t.cs();
999 os << '{' << p.verbatim_item() << "}\n";
1003 else if (is_known(t.cs(), known_quotes)) {
1004 char const ** where = is_known(t.cs(), known_quotes);
1005 context.check_layout(os);
1006 begin_inset(os, "Quotes ");
1007 os << known_coded_quotes[where - known_quotes];
1012 else if (is_known(t.cs(), known_sizes)) {
1013 char const ** where = is_known(t.cs(), known_sizes);
1014 context.check_layout(os);
1015 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
1019 else if (t.cs() == "LyX" || t.cs() == "TeX"
1020 || t.cs() == "LaTeX") {
1021 context.check_layout(os);
1023 skip_braces(p); // eat {}
1026 else if (t.cs() == "LaTeXe") {
1027 context.check_layout(os);
1029 skip_braces(p); // eat {}
1032 else if (t.cs() == "ldots") {
1033 context.check_layout(os);
1035 os << "\\SpecialChar \\ldots{}\n";
1038 else if (t.cs() == "lyxarrow") {
1039 context.check_layout(os);
1040 os << "\\SpecialChar \\menuseparator\n";
1044 else if (t.cs() == "textcompwordmark") {
1045 context.check_layout(os);
1046 os << "\\SpecialChar \\textcompwordmark{}\n";
1050 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1051 context.check_layout(os);
1052 os << "\\SpecialChar \\@.\n";
1056 else if (t.cs() == "-") {
1057 context.check_layout(os);
1058 os << "\\SpecialChar \\-\n";
1061 else if (t.cs() == "textasciitilde") {
1062 context.check_layout(os);
1067 else if (t.cs() == "textasciicircum") {
1068 context.check_layout(os);
1073 else if (t.cs() == "textbackslash") {
1074 context.check_layout(os);
1075 os << "\n\\backslash \n";
1079 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1080 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1082 context.check_layout(os);
1086 else if (t.cs() == "char") {
1087 context.check_layout(os);
1088 if (p.next_token().character() == '`') {
1090 if (p.next_token().cs() == "\"") {
1095 handle_ert(os, "\\char`", context);
1098 handle_ert(os, "\\char", context);
1102 else if (t.cs() == "\"") {
1103 context.check_layout(os);
1104 string const name = p.verbatim_item();
1105 if (name == "a") os << 'ä';
1106 else if (name == "o") os << 'ö';
1107 else if (name == "u") os << 'ü';
1108 else if (name == "A") os << 'Ä';
1109 else if (name == "O") os << 'Ö';
1110 else if (name == "U") os << 'Ü';
1111 else handle_ert(os, "\"{" + name + "}", context);
1114 // Problem: \= creates a tabstop inside the tabbing environment
1115 // and else an accent. In the latter case we really would want
1116 // \={o} instead of \= o.
1117 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1118 || t.cs() == "~" || t.cs() == "." || (t.cs() == "=" && ! (flags & FLAG_TABBING))) {
1119 // we need the trim as the LyX parser chokes on such spaces
1120 context.check_layout(os);
1121 os << "\n\\i \\" << t.cs() << "{"
1122 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1125 else if (t.cs() == "ss") {
1126 context.check_layout(os);
1130 else if (t.cs() == "i" || t.cs() == "j") {
1131 context.check_layout(os);
1132 os << "\\" << t.cs() << ' ';
1135 else if (t.cs() == "\\") {
1136 context.check_layout(os);
1137 string const next = p.next_token().asInput();
1139 handle_ert(os, "\\\\" + p.getOpt(), context);
1140 else if (next == "*") {
1142 handle_ert(os, "\\\\*" + p.getOpt(), context);
1145 os << "\n\\newline \n";
1149 else if (t.cs() == "input" || t.cs() == "include"
1150 || t.cs() == "verbatiminput") {
1151 string name = '\\' + t.cs();
1152 if (t.cs() == "verbatiminput"
1153 && p.next_token().asInput() == "*")
1154 name += p.get_token().asInput();
1155 context.check_layout(os);
1156 begin_inset(os, "Include ");
1157 string filename(p.getArg('{', '}'));
1158 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1159 if (tex2lyx(filename, lyxname)) {
1160 os << name << '{' << lyxname << "}\n";
1162 os << name << '{' << filename << "}\n";
1164 os << "preview false\n";
1168 else if (t.cs() == "fancyhead") {
1169 context.check_layout(os);
1171 ss << "\\fancyhead";
1173 ss << '{' << p.verbatim_item() << "}\n";
1174 handle_ert(os, ss.str(), context);
1177 else if (t.cs() == "bibliographystyle") {
1178 // store new bibliographystyle
1179 bibliographystyle = p.verbatim_item();
1180 // output new bibliographystyle.
1181 // This is only necessary if used in some other macro than \bibliography.
1182 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1185 else if (t.cs() == "bibliography") {
1186 context.check_layout(os);
1187 begin_inset(os, "LatexCommand ");
1189 // Do we have a bibliographystyle set?
1190 if (bibliographystyle.size()) {
1191 os << '[' << bibliographystyle << ']';
1193 os << '{' << p.verbatim_item() << "}\n";
1197 else if ( t.cs() == "smallskip" ||
1198 t.cs() == "medskip" ||
1199 t.cs() == "bigskip" ||
1200 t.cs() == "vfill" ||
1201 (t.cs() == "vspace" && p.next_token().asInput() != "*")) {
1203 if (t.cs() == "vspace")
1204 arg = p.getArg('{', '}');
1207 // We may only add the vspace to the current context if the
1208 // current paragraph is not empty.
1209 if (context.atParagraphStart()
1210 && (p.next_token().cat() != catNewline || p.next_token().cs().size() == 1)
1211 && (! (p.next_token().cat() == catEscape && p.next_token().cs() == "end"))
1212 && (! (p.next_token().cat() == catEscape && p.next_token().cs() == "par"))) {
1213 context.extra_stuff += "\\added_space_top " + arg + " ";
1216 if (t.cs() == "vspace")
1217 handle_ert(os, t.asInput() + '{' + arg + '}', context);
1219 handle_ert(os, t.asInput(), context);
1221 // Would be nice to recognize added_space_bottom too...
1222 // At the moment this is parsed as added_space_top of the
1226 else if (t.cs() == "psfrag") {
1227 // psfrag{ps-text}[ps-pos][tex-pos]{tex-text}
1228 // TODO: Generalize this!
1229 string arguments = p.getArg('{', '}');
1231 arguments += p.getOpt();
1232 arguments += p.getOpt();
1233 handle_ert(os, "\\psfrag{" + arguments, context);
1237 //cerr << "#: " << t << " mode: " << mode << endl;
1238 // heuristic: read up to next non-nested space
1240 string s = t.asInput();
1241 string z = p.verbatim_item();
1242 while (p.good() && z != " " && z.size()) {
1243 //cerr << "read: " << z << endl;
1245 z = p.verbatim_item();
1247 cerr << "found ERT: " << s << endl;
1248 handle_ert(os, s + ' ', context);
1250 context.check_layout(os);
1251 string name = t.asInput();
1252 if (p.next_token().asInput() == "*") {
1253 // Starred commands like \vspace*{}
1254 p.get_token(); // Eat '*'
1257 handle_ert(os, name, context);
1260 if (flags & FLAG_LEAVE) {
1261 flags &= ~FLAG_LEAVE;