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/tostr.h"
22 #include "support/filetools.h"
34 using std::ostringstream;
35 using std::istringstream;
39 using lyx::support::rtrim;
40 using lyx::support::suffixIs;
43 // thin wrapper around parse_text using a string
44 string parse_text(Parser & p, unsigned flags, const bool outer,
48 parse_text(p, os, flags, outer, context);
52 // parses a subdocument, usually useful in insets (whence the name)
53 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
56 Context newcontext(true, context.textclass);
57 parse_text(p, os, flags, outer, newcontext);
58 newcontext.check_end_layout(os);
62 // parses a paragraph snippet, useful for example for \emph{...}
63 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
66 Context newcontext(false, context.textclass);
67 parse_text(p, os, flags, outer, newcontext);
68 // should not be needed
69 newcontext.check_end_layout(os);
75 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
76 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
78 // LaTeX names for quotes
79 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
80 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
82 // the same as known_quotes with .lyx names
83 char const * known_coded_quotes[] = { "gld", "grd", "gld",
84 "grd", "gls", "fls", "frd", 0};
86 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
87 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
89 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
90 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
92 // splits "x=z, y=b" into a map
93 map<string, string> split_map(string const & s)
95 map<string, string> res;
98 for (size_t i = 0; i < v.size(); ++i) {
99 size_t const pos = v[i].find('=');
100 string const index = v[i].substr(0, pos);
101 string const value = v[i].substr(pos + 1, string::npos);
102 res[trim(index)] = trim(value);
109 * Split a LaTeX length into value and unit.
110 * The latter can be a real unit like "pt", or a latex length variable
111 * like "\textwidth". The unit may contain additional stuff like glue
112 * lengths, but we don't care, because such lengths are ERT anyway.
113 * \return true if \param value and \param unit are valid.
115 bool splitLatexLength(string const & len, string & value, string & unit)
119 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
120 //'4,5' is a valid LaTeX length number. Change it to '4.5'
121 string const length = lyx::support::subst(len, ',', '.');
122 if (i == string::npos)
125 if (len[0] == '\\') {
126 // We had something like \textwidth without a factor
132 value = trim(string(length, 0, i));
136 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
137 if (lyx::support::contains(len, '\\'))
138 unit = trim(string(len, i));
140 unit = lyx::support::lowercase(trim(string(len, i)));
145 // A simple function to translate a latex length to something lyx can
146 // understand. Not perfect, but rather best-effort.
147 string translate_len(string const & length)
151 // If the input is invalid, return what we have.
152 if (!splitLatexLength(length, valstring, unit))
154 // LyX uses percent values
156 istringstream iss(valstring);
161 string const percentval = oss.str();
163 if (unit.empty() || unit[0] != '\\')
164 return valstring + unit;
165 const string::size_type i = unit.find(" ", i);
166 string const endlen = (i == string::npos) ? string() : string(unit, i);
167 if (unit == "\\textwidth")
168 return percentval + "text%" + endlen;
169 else if (unit == "\\columnwidth")
170 return percentval + "col%" + endlen;
171 else if (unit == "\\paperwidth")
172 return percentval + "page%" + endlen;
173 else if (unit == "\\linewidth")
174 return percentval + "line%" + endlen;
175 else if (unit == "\\paperheight")
176 return percentval + "pheight%" + endlen;
177 else if (unit == "\\textheight")
178 return percentval + "theight%" + endlen;
180 return valstring + unit;
184 void begin_inset(ostream & os, string const & name)
186 os << "\n\\begin_inset " << name;
190 void end_inset(ostream & os)
192 os << "\n\\end_inset \n\n";
196 void skip_braces(Parser & p)
198 if (p.next_token().cat() != catBegin)
201 if (p.next_token().cat() == catEnd) {
209 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
212 // We must have a valid layout before outputting the ERT inset.
213 context.check_layout(os);
215 Context newcontext(true, context.textclass);
216 begin_inset(os, "ERT");
217 os << "\nstatus Collapsed\n";
218 newcontext.check_layout(os);
219 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
221 os << "\n\\backslash \n";
225 newcontext.check_end_layout(os);
230 void handle_comment(ostream & os, string const & s, Context & context)
232 // TODO: Handle this better
233 Context newcontext(true, context.textclass);
234 begin_inset(os, "ERT");
235 os << "\nstatus Collapsed\n";
236 newcontext.check_layout(os);
237 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
239 os << "\n\\backslash \n";
243 // make sure that our comment is the last thing on the line
245 newcontext.check_end_layout(os);
251 isLayout(string const name) : name_(name) {}
252 bool operator()(LyXLayout_ptr const & ptr) {
253 return ptr.get() && ptr->latexname() == name_;
260 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
263 LyXTextClass::const_iterator it = textclass.begin();
264 LyXTextClass::const_iterator end = textclass.end();
265 it = std::find_if(it, end, isLayout(name));
266 return (it == end) ? LyXLayout_ptr() : *it;
270 void output_command_layout(ostream & os, Parser & p, bool outer,
271 Context & parent_context,
272 LyXLayout_ptr newlayout)
274 parent_context.check_end_layout(os);
275 Context context(true, parent_context.textclass, newlayout,
276 parent_context.layout);
277 context.check_deeper(os);
278 context.check_layout(os);
279 if (context.layout->optionalargs > 0) {
281 if (p.next_token().character() == '[') {
282 p.get_token(); // eat '['
283 begin_inset(os, "OptArg\n");
284 os << "collapsed true\n\n";
285 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
289 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
290 context.check_end_layout(os);
291 context.check_end_deeper(os);
292 // We don't need really a new paragraph, but
293 // we must make sure that the next item gets a \begin_layout.
294 parent_context.new_paragraph(os);
299 * Output a space if necessary.
300 * This function gets called for every whitespace token.
302 * We have three cases here:
303 * 1. A space must be suppressed. Example: The lyxcode case below
304 * 2. A space may be suppressed. Example: Spaces before "\par"
305 * 3. A space must not be suppressed. Example: A space between two words
307 * We currently handle only 1. and 3 and from 2. only the case of
308 * spaces before newlines as a side effect.
310 * 2. could be used to suppress as many spaces as possible. This has two effects:
311 * - Reimporting LyX generated LaTeX files changes almost no whitespace
312 * - Superflous whitespace from non LyX generated LaTeX files is removed.
313 * The drawback is that the logic inside the function becomes
314 * complicated, and that is the reason why it is not implemented.
316 void check_space(Parser const & p, ostream & os, Context & context)
318 Token const next = p.next_token();
319 Token const curr = p.curr_token();
320 // A space before a single newline and vice versa must be ignored
321 // LyX emits a newline before \end{lyxcode}.
322 // This newline must be ignored,
323 // otherwise LyX will add an additional protected space.
324 if (next.cat() == catSpace ||
325 next.cat() == catNewline ||
326 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
329 context.check_layout(os);
335 * Check wether \param command is a known command. If yes,
336 * handle the command with all arguments.
337 * \return true if the command was parsed, false otherwise.
339 bool parse_command(string const & command, Parser & p, ostream & os,
340 bool outer, Context & context)
342 if (known_commands.find(command) != known_commands.end()) {
343 vector<ArgumentType> const & template_arguments = known_commands[command];
344 string ert = command;
345 size_t no_arguments = template_arguments.size();
346 for (size_t i = 0; i < no_arguments; ++i) {
347 switch (template_arguments[i]) {
349 // This argument contains regular LaTeX
350 handle_ert(os, ert + '{', context);
351 parse_text(p, os, FLAG_ITEM, outer, context);
355 // This argument may contain special characters
356 ert += '{' + p.verbatim_item() + '}';
363 handle_ert(os, ert, context);
370 void parse_environment(Parser & p, ostream & os, bool outer,
371 Context & parent_context)
373 LyXLayout_ptr newlayout;
374 string const name = p.getArg('{', '}');
375 const bool is_starred = suffixIs(name, '*');
376 string const unstarred_name = rtrim(name, "*");
377 active_environments.push_back(name);
380 if (is_math_env(name)) {
381 parent_context.check_layout(os);
382 begin_inset(os, "Formula ");
383 os << "\\begin{" << name << "}";
384 parse_math(p, os, FLAG_END, MATH_MODE);
385 os << "\\end{" << name << "}";
389 else if (name == "tabular") {
390 parent_context.check_layout(os);
391 begin_inset(os, "Tabular ");
392 handle_tabular(p, os, parent_context);
396 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
397 parent_context.check_layout(os);
398 begin_inset(os, "Float " + unstarred_name + "\n");
399 if (p.next_token().asInput() == "[") {
400 os << "placement " << p.getArg('[', ']') << '\n';
402 os << "wide " << tostr(is_starred)
403 << "\ncollapsed false\n\n";
404 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
406 // We don't need really a new paragraph, but
407 // we must make sure that the next item gets a \begin_layout.
408 parent_context.new_paragraph(os);
411 else if (name == "minipage") {
412 string position = "1";
413 string inner_pos = "0";
414 string height = "0pt";
415 string latex_position;
416 string latex_inner_pos;
418 if (p.next_token().asInput() == "[") {
419 latex_position = p.getArg('[', ']');
420 switch(latex_position[0]) {
421 case 't': position = "0"; break;
422 case 'c': position = "1"; break;
423 case 'b': position = "2"; break;
425 cerr << "invalid position for minipage"
429 if (p.next_token().asInput() == "[") {
430 latex_height = p.getArg('[', ']');
431 height = translate_len(latex_height);
433 if (p.next_token().asInput() == "[") {
434 latex_inner_pos = p.getArg('[', ']');
435 switch(latex_inner_pos[0]) {
436 case 'c': inner_pos = "0"; break;
437 case 't': inner_pos = "1"; break;
438 case 'b': inner_pos = "2"; break;
439 case 's': inner_pos = "3"; break;
441 cerr << "invalid inner_pos for minipage"
448 string width = translate_len(p.verbatim_item());
449 if (width[0] == '\\') {
450 // lyx can't handle length variables
452 ss << "\\begin{minipage}";
453 if (!latex_position.empty())
454 ss << '[' << latex_position << ']';
455 if (!latex_height.empty())
456 ss << '[' << latex_height << ']';
457 if (!latex_inner_pos.empty())
458 ss << '[' << latex_inner_pos << ']';
459 ss << "{" << width << "}";
460 handle_ert(os, ss.str(), parent_context);
461 parent_context.new_paragraph(os);
462 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
463 handle_ert(os, "\\end{minipage}", parent_context);
465 parent_context.check_layout(os);
466 begin_inset(os, "Minipage\n");
467 os << "position " << position << '\n';
468 os << "inner_position " << inner_pos << '\n';
469 os << "height \"" << height << "\"\n";
470 os << "width \"" << width << "\"\n";
471 os << "collapsed false\n\n";
472 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
477 // Alignment settings
478 else if (name == "center" || name == "flushleft" || name == "flushright" ||
479 name == "centering" || name == "raggedright" || name == "raggedleft") {
480 // We must begin a new paragraph if not already done
481 if (! parent_context.atParagraphStart()) {
482 parent_context.check_end_layout(os);
483 parent_context.new_paragraph(os);
485 if (name == "flushleft" || name == "raggedright")
486 parent_context.extra_stuff += "\\align left ";
487 else if (name == "flushright" || name == "raggedleft")
488 parent_context.extra_stuff += "\\align right ";
490 parent_context.extra_stuff += "\\align center ";
491 parse_text(p, os, FLAG_END, outer, parent_context);
492 // Just in case the environment is empty ..
493 parent_context.extra_stuff.erase();
494 // We must begin a new paragraph to reset the alignment
495 parent_context.new_paragraph(os);
498 // The single '=' is meant here.
499 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
500 newlayout->isEnvironment()) {
501 Context context(true, parent_context.textclass, newlayout,
502 parent_context.layout);
503 parent_context.check_end_layout(os);
504 switch (context.layout->latextype) {
505 case LATEX_LIST_ENVIRONMENT:
506 context.extra_stuff = "\\labelwidthstring "
507 + p.verbatim_item() + '\n';
510 case LATEX_BIB_ENVIRONMENT:
511 p.verbatim_item(); // swallow next arg
517 context.check_deeper(os);
518 parse_text(p, os, FLAG_END, outer, context);
519 context.check_end_layout(os);
520 context.check_end_deeper(os);
521 parent_context.new_paragraph(os);
524 else if (name == "appendix") {
525 // This is no good latex style, but it works and is used in some documents...
526 parent_context.check_end_layout(os);
527 Context context(true, parent_context.textclass, parent_context.layout,
528 parent_context.layout);
529 context.check_layout(os);
530 os << "\\start_of_appendix\n";
531 parse_text(p, os, FLAG_END, outer, context);
532 context.check_end_layout(os);
535 else if (name == "comment") {
536 parent_context.check_layout(os);
537 begin_inset(os, "Comment\n");
538 os << "collapsed false\n";
539 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
543 else if (name == "tabbing") {
544 // We need to remember that we have to handle '\=' specially
545 handle_ert(os, "\\begin{" + name + "}", parent_context);
546 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
547 handle_ert(os, "\\end{" + name + "}", parent_context);
551 handle_ert(os, "\\begin{" + name + "}", parent_context);
552 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
553 handle_ert(os, "\\end{" + name + "}", parent_context);
556 active_environments.pop_back();
561 } // anonymous namespace
566 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
569 LyXLayout_ptr newlayout;
570 // Store the latest bibliographystyle (needed for bibtex inset)
571 string bibliographystyle;
573 Token const & t = p.get_token();
576 cerr << "t: " << t << " flags: " << flags << "\n";
579 if (flags & FLAG_ITEM) {
580 if (t.cat() == catSpace)
584 if (t.cat() == catBegin) {
585 // skip the brace and collect everything to the next matching
587 flags |= FLAG_BRACE_LAST;
591 // handle only this single token, leave the loop if done
595 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
601 if (t.cat() == catMath) {
602 // we are inside some text mode thingy, so opening new math is allowed
603 context.check_layout(os);
604 begin_inset(os, "Formula ");
605 Token const & n = p.get_token();
606 if (n.cat() == catMath && outer) {
607 // TeX's $$...$$ syntax for displayed math
609 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
611 p.get_token(); // skip the second '$' token
613 // simple $...$ stuff
616 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
622 else if (t.cat() == catSuper || t.cat() == catSub)
623 cerr << "catcode " << t << " illegal in text mode\n";
625 // Basic support for english quotes. This should be
626 // extended to other quotes, but is not so easy (a
627 // left english quote is the same as a right german
629 else if (t.asInput() == "`"
630 && p.next_token().asInput() == "`") {
631 context.check_layout(os);
632 begin_inset(os, "Quotes ");
638 else if (t.asInput() == "'"
639 && p.next_token().asInput() == "'") {
640 context.check_layout(os);
641 begin_inset(os, "Quotes ");
648 else if (t.cat() == catSpace || (t.cat() == catNewline && t.cs().size() == 1))
649 check_space(p, os, context);
651 else if (t.cat() == catLetter ||
652 t.cat() == catOther ||
653 t.cat() == catAlign ||
654 t.cat() == catParameter) {
655 context.check_layout(os);
659 else if (t.cat() == catNewline || (t.cat() == catEscape && t.cs() == "par")) {
661 context.new_paragraph(os);
664 else if (t.cat() == catActive) {
665 context.check_layout(os);
666 if (t.character() == '~') {
667 if (context.layout->free_spacing)
670 os << "\\InsetSpace ~\n";
675 else if (t.cat() == catBegin) {
676 // special handling of size changes
677 context.check_layout(os);
678 bool const is_size = is_known(p.next_token().cs(), known_sizes);
679 Token const prev = p.prev_token();
680 string const s = parse_text(p, FLAG_BRACE_LAST, outer, context);
681 if (s.empty() && (p.next_token().character() == '`' ||
682 (prev.character() == '-' && p.next_token().character())))
683 ; // ignore it in {}`` or -{}-
684 else if (is_size || s == "[" || s == "]" || s == "*")
687 handle_ert(os, "{", context, false);
688 // s will end the current layout and begin a new one if necessary
690 handle_ert(os, "}", context);
694 else if (t.cat() == catEnd) {
695 if (flags & FLAG_BRACE_LAST) {
698 cerr << "stray '}' in text\n";
699 handle_ert(os, "}", context);
702 else if (t.cat() == catComment) {
703 context.check_layout(os);
704 if (!t.cs().empty()) {
705 handle_comment(os, '%' + t.cs(), context);
706 if (p.next_token().cat() == catNewline) {
707 // A newline after a comment line starts a new paragraph
708 context.new_paragraph(os);
721 else if (t.cs() == "(") {
722 context.check_layout(os);
723 begin_inset(os, "Formula");
725 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
730 else if (t.cs() == "[") {
731 context.check_layout(os);
732 begin_inset(os, "Formula");
734 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
739 else if (t.cs() == "begin")
740 parse_environment(p, os, outer, context);
742 else if (t.cs() == "end") {
743 if (flags & FLAG_END) {
744 // eat environment name
745 string const name = p.getArg('{', '}');
746 if (name != active_environment())
747 cerr << "\\end{" + name + "} does not match \\begin{"
748 + active_environment() + "}\n";
751 p.error("found 'end' unexpectedly");
754 else if (t.cs() == "item") {
758 if (p.next_token().character() == '[') {
759 p.get_token(); // eat '['
760 Context newcontext(false, context.textclass);
761 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
765 context.check_layout(os);
767 if (context.layout->labeltype != LABEL_MANUAL) {
768 // lyx does not support \item[\mybullet] in itemize environments
769 handle_ert(os, "[", context);
771 handle_ert(os, "]", context);
772 } else if (!s.empty()) {
773 // The space is needed to separate the item from the rest of the sentence.
780 else if (t.cs() == "bibitem") {
782 context.check_layout(os);
785 os << '{' << p.verbatim_item() << '}' << "\n";
788 else if (t.cs() == "def") {
790 context.check_layout(os);
791 string name = p.get_token().cs();
792 while (p.next_token().cat() != catBegin)
793 name += p.get_token().asString();
794 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
797 else if (t.cs() == "noindent") {
799 context.extra_stuff += "\\noindent ";
802 else if (t.cs() == "appendix") {
804 context.extra_stuff += "\\start_of_appendix ";
807 // Must attempt to parse "Section*" before "Section".
808 else if ((p.next_token().asInput() == "*") &&
809 // The single '=' is meant here.
810 (newlayout = findLayout(context.textclass,
811 t.cs() + '*')).get() &&
812 newlayout->isCommand()) {
814 output_command_layout(os, p, outer, context, newlayout);
818 // The single '=' is meant here.
819 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
820 newlayout->isCommand()) {
821 output_command_layout(os, p, outer, context, newlayout);
825 else if (t.cs() == "includegraphics") {
826 map<string, string> opts = split_map(p.getArg('[', ']'));
827 string name = p.verbatim_item();
829 context.check_layout(os);
830 begin_inset(os, "Graphics ");
831 os << "\n\tfilename " << name << '\n';
832 if (opts.find("width") != opts.end())
834 << translate_len(opts["width"]) << '\n';
835 if (opts.find("height") != opts.end())
837 << translate_len(opts["height"]) << '\n';
838 if (opts.find("scale") != opts.end()) {
839 istringstream iss(opts["scale"]);
843 os << "\tscale " << val << '\n';
845 if (opts.find("angle") != opts.end())
846 os << "\trotateAngle "
847 << opts["angle"] << '\n';
848 if (opts.find("origin") != opts.end()) {
850 string const opt = opts["origin"];
851 if (opt.find('l') != string::npos) ss << "left";
852 if (opt.find('r') != string::npos) ss << "right";
853 if (opt.find('c') != string::npos) ss << "center";
854 if (opt.find('t') != string::npos) ss << "Top";
855 if (opt.find('b') != string::npos) ss << "Bottom";
856 if (opt.find('B') != string::npos) ss << "Baseline";
857 if (!ss.str().empty())
858 os << "\trotateOrigin " << ss.str() << '\n';
860 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
862 if (opts.find("keepaspectratio") != opts.end())
863 os << "\tkeepAspectRatio\n";
864 if (opts.find("clip") != opts.end())
866 if (opts.find("draft") != opts.end())
868 if (opts.find("bb") != opts.end())
869 os << "\tBoundingBox "
870 << opts["bb"] << '\n';
871 int numberOfbbOptions = 0;
872 if (opts.find("bbllx") != opts.end())
874 if (opts.find("bblly") != opts.end())
876 if (opts.find("bburx") != opts.end())
878 if (opts.find("bbury") != opts.end())
880 if (numberOfbbOptions == 4)
881 os << "\tBoundingBox "
882 << opts["bbllx"] << opts["bblly"]
883 << opts["bburx"] << opts["bbury"] << '\n';
884 else if (numberOfbbOptions > 0)
885 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
886 numberOfbbOptions = 0;
887 if (opts.find("natwidth") != opts.end())
889 if (opts.find("natheight") != opts.end())
891 if (numberOfbbOptions == 2)
892 os << "\tBoundingBox 0bp 0bp "
893 << opts["natwidth"] << opts["natheight"] << '\n';
894 else if (numberOfbbOptions > 0)
895 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
896 ostringstream special;
897 if (opts.find("hiresbb") != opts.end())
898 special << "hiresbb,";
899 if (opts.find("trim") != opts.end())
901 if (opts.find("viewport") != opts.end())
902 special << "viewport=" << opts["viewport"] << ',';
903 if (opts.find("totalheight") != opts.end())
904 special << "totalheight=" << opts["totalheight"] << ',';
905 if (opts.find("type") != opts.end())
906 special << "type=" << opts["type"] << ',';
907 if (opts.find("ext") != opts.end())
908 special << "ext=" << opts["ext"] << ',';
909 if (opts.find("read") != opts.end())
910 special << "read=" << opts["read"] << ',';
911 if (opts.find("command") != opts.end())
912 special << "command=" << opts["command"] << ',';
913 string s_special = special.str();
914 if (!s_special.empty()) {
915 // We had special arguments. Remove the trailing ','.
916 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
918 // TODO: Handle the unknown settings better.
919 // Warn about invalid options.
920 // Check wether some option was given twice.
924 else if (t.cs() == "footnote") {
926 context.check_layout(os);
927 begin_inset(os, "Foot\n");
928 os << "collapsed true\n\n";
929 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
933 else if (t.cs() == "marginpar") {
935 context.check_layout(os);
936 begin_inset(os, "Marginal\n");
937 os << "collapsed true\n\n";
938 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
942 else if (t.cs() == "ensuremath") {
944 context.check_layout(os);
945 Context newcontext(false, context.textclass);
946 string s = parse_text(p, FLAG_ITEM, false, newcontext);
947 if (s == "±" || s == "³" || s == "²" || s == "µ")
950 handle_ert(os, "\\ensuremath{" + s + "}",
954 else if (t.cs() == "hfill") {
955 context.check_layout(os);
961 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
963 skip_braces(p); // swallow this
966 else if (t.cs() == "tableofcontents") {
968 context.check_layout(os);
969 begin_inset(os, "LatexCommand \\tableofcontents\n");
971 skip_braces(p); // swallow this
974 else if (t.cs() == "listoffigures") {
976 context.check_layout(os);
977 begin_inset(os, "FloatList figure\n");
979 skip_braces(p); // swallow this
982 else if (t.cs() == "listoftables") {
984 context.check_layout(os);
985 begin_inset(os, "FloatList table\n");
987 skip_braces(p); // swallow this
990 else if (t.cs() == "listof") {
992 string const name = p.get_token().asString();
993 if (context.textclass.floats().typeExist(name)) {
994 context.check_layout(os);
995 begin_inset(os, "FloatList ");
998 p.get_token(); // swallow second arg
1000 handle_ert(os, "\\listof{" + name + "}", context);
1003 else if (t.cs() == "textrm") {
1004 context.check_layout(os);
1005 os << "\n\\family roman \n";
1006 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1007 os << "\n\\family default \n";
1010 else if (t.cs() == "textsf") {
1011 context.check_layout(os);
1012 os << "\n\\family sans \n";
1013 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1014 os << "\n\\family default \n";
1017 else if (t.cs() == "textsl") {
1018 context.check_layout(os);
1019 os << "\n\\shape slanted \n";
1020 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1021 os << "\n\\shape default \n";
1024 else if (t.cs() == "texttt") {
1025 context.check_layout(os);
1026 os << "\n\\family typewriter \n";
1027 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1028 os << "\n\\family default \n";
1031 else if (t.cs() == "textit") {
1032 context.check_layout(os);
1033 os << "\n\\shape italic \n";
1034 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1035 os << "\n\\shape default \n";
1038 else if (t.cs() == "textsc") {
1039 context.check_layout(os);
1040 os << "\n\\noun on \n";
1041 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1042 os << "\n\\noun default \n";
1045 else if (t.cs() == "textbf") {
1046 context.check_layout(os);
1047 os << "\n\\series bold \n";
1048 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1049 os << "\n\\series default \n";
1052 else if (t.cs() == "underbar") {
1053 context.check_layout(os);
1054 os << "\n\\bar under \n";
1055 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1056 os << "\n\\bar default \n";
1059 else if (t.cs() == "emph" || t.cs() == "noun") {
1060 context.check_layout(os);
1061 os << "\n\\" << t.cs() << " on \n";
1062 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1063 os << "\n\\" << t.cs() << " default \n";
1066 else if (is_known(t.cs(), known_latex_commands)) {
1067 context.check_layout(os);
1068 begin_inset(os, "LatexCommand ");
1069 os << '\\' << t.cs();
1072 os << '{' << p.verbatim_item() << "}\n";
1076 else if (is_known(t.cs(), known_quotes)) {
1077 char const ** where = is_known(t.cs(), known_quotes);
1078 context.check_layout(os);
1079 begin_inset(os, "Quotes ");
1080 os << known_coded_quotes[where - known_quotes];
1085 else if (is_known(t.cs(), known_sizes)) {
1086 char const ** where = is_known(t.cs(), known_sizes);
1087 context.check_layout(os);
1088 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
1092 else if (t.cs() == "LyX" || t.cs() == "TeX"
1093 || t.cs() == "LaTeX") {
1094 context.check_layout(os);
1096 skip_braces(p); // eat {}
1099 else if (t.cs() == "LaTeXe") {
1100 context.check_layout(os);
1102 skip_braces(p); // eat {}
1105 else if (t.cs() == "ldots") {
1106 context.check_layout(os);
1108 os << "\\SpecialChar \\ldots{}\n";
1111 else if (t.cs() == "lyxarrow") {
1112 context.check_layout(os);
1113 os << "\\SpecialChar \\menuseparator\n";
1117 else if (t.cs() == "textcompwordmark") {
1118 context.check_layout(os);
1119 os << "\\SpecialChar \\textcompwordmark{}\n";
1123 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1124 context.check_layout(os);
1125 os << "\\SpecialChar \\@.\n";
1129 else if (t.cs() == "-") {
1130 context.check_layout(os);
1131 os << "\\SpecialChar \\-\n";
1134 else if (t.cs() == "textasciitilde") {
1135 context.check_layout(os);
1140 else if (t.cs() == "textasciicircum") {
1141 context.check_layout(os);
1146 else if (t.cs() == "textbackslash") {
1147 context.check_layout(os);
1148 os << "\n\\backslash \n";
1152 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1153 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1155 context.check_layout(os);
1159 else if (t.cs() == "char") {
1160 context.check_layout(os);
1161 if (p.next_token().character() == '`') {
1163 if (p.next_token().cs() == "\"") {
1168 handle_ert(os, "\\char`", context);
1171 handle_ert(os, "\\char", context);
1175 else if (t.cs() == "\"") {
1176 context.check_layout(os);
1177 string const name = p.verbatim_item();
1178 if (name == "a") os << 'ä';
1179 else if (name == "o") os << 'ö';
1180 else if (name == "u") os << 'ü';
1181 else if (name == "A") os << 'Ä';
1182 else if (name == "O") os << 'Ö';
1183 else if (name == "U") os << 'Ü';
1184 else handle_ert(os, "\"{" + name + "}", context);
1187 // Problem: \= creates a tabstop inside the tabbing environment
1188 // and else an accent. In the latter case we really would want
1189 // \={o} instead of \= o.
1190 else if (t.cs() == "=" && (flags & FLAG_TABBING))
1191 handle_ert(os, t.asInput(), context);
1193 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1194 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
1195 // we need the trim as the LyX parser chokes on such spaces
1196 context.check_layout(os);
1197 os << "\n\\i \\" << t.cs() << "{"
1198 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1201 else if (t.cs() == "ss") {
1202 context.check_layout(os);
1206 else if (t.cs() == "i" || t.cs() == "j") {
1207 context.check_layout(os);
1208 os << "\\" << t.cs() << ' ';
1211 else if (t.cs() == "\\") {
1212 context.check_layout(os);
1213 string const next = p.next_token().asInput();
1215 handle_ert(os, "\\\\" + p.getOpt(), context);
1216 else if (next == "*") {
1218 handle_ert(os, "\\\\*" + p.getOpt(), context);
1221 os << "\n\\newline \n";
1225 else if (t.cs() == "input" || t.cs() == "include"
1226 || t.cs() == "verbatiminput") {
1227 string name = '\\' + t.cs();
1228 if (t.cs() == "verbatiminput"
1229 && p.next_token().asInput() == "*")
1230 name += p.get_token().asInput();
1231 context.check_layout(os);
1232 begin_inset(os, "Include ");
1233 string filename(p.getArg('{', '}'));
1234 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1235 if (tex2lyx(filename, lyxname)) {
1236 os << name << '{' << lyxname << "}\n";
1238 os << name << '{' << filename << "}\n";
1240 os << "preview false\n";
1244 else if (t.cs() == "fancyhead") {
1245 context.check_layout(os);
1247 ss << "\\fancyhead";
1249 ss << '{' << p.verbatim_item() << "}\n";
1250 handle_ert(os, ss.str(), context);
1253 else if (t.cs() == "bibliographystyle") {
1254 // store new bibliographystyle
1255 bibliographystyle = p.verbatim_item();
1256 // output new bibliographystyle.
1257 // This is only necessary if used in some other macro than \bibliography.
1258 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1261 else if (t.cs() == "bibliography") {
1262 context.check_layout(os);
1263 begin_inset(os, "LatexCommand ");
1265 // Do we have a bibliographystyle set?
1266 if (!bibliographystyle.empty()) {
1267 os << '[' << bibliographystyle << ']';
1269 os << '{' << p.verbatim_item() << "}\n";
1273 else if (t.cs() == "smallskip" ||
1274 t.cs() == "medskip" ||
1275 t.cs() == "bigskip" ||
1276 t.cs() == "vfill") {
1277 context.check_layout(os);
1278 begin_inset(os, "VSpace ");
1283 else if (t.cs() == "vspace") {
1284 bool starred = false;
1285 if (p.next_token().asInput() == "*") {
1289 string const length = p.verbatim_item();
1292 bool valid = splitLatexLength(length, valstring, unit);
1293 bool known_vspace = false;
1294 bool known_unit = false;
1297 istringstream iss(valstring);
1300 if (unit == "\\smallskipamount") {
1302 known_vspace = true;
1303 } else if (unit == "\\medskipamount") {
1305 known_vspace = true;
1306 } else if (unit == "\\bigskipamount") {
1308 known_vspace = true;
1309 } else if (unit == "\\fill") {
1311 known_vspace = true;
1314 switch (unitFromString(unit)) {
1335 if (known_unit || known_vspace) {
1336 // Literal length or known variable
1337 context.check_layout(os);
1338 begin_inset(os, "VSpace ");
1346 // LyX can't handle other length variables in Inset VSpace
1347 string name = t.asInput();
1352 handle_ert(os, name + '{' + unit + '}', context);
1353 else if (value == -1.0)
1354 handle_ert(os, name + "{-" + unit + '}', context);
1356 handle_ert(os, name + '{' + valstring + unit + '}', context);
1358 handle_ert(os, name + '{' + length + '}', context);
1363 //cerr << "#: " << t << " mode: " << mode << endl;
1364 // heuristic: read up to next non-nested space
1366 string s = t.asInput();
1367 string z = p.verbatim_item();
1368 while (p.good() && z != " " && z.size()) {
1369 //cerr << "read: " << z << endl;
1371 z = p.verbatim_item();
1373 cerr << "found ERT: " << s << endl;
1374 handle_ert(os, s + ' ', context);
1376 string name = t.asInput();
1377 if (p.next_token().asInput() == "*") {
1378 // Starred commands like \vspace*{}
1379 p.get_token(); // Eat '*'
1382 if (! parse_command(name, p, os, outer, context))
1383 handle_ert(os, name, context);
1386 if (flags & FLAG_LEAVE) {
1387 flags &= ~FLAG_LEAVE;