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"
29 using lyx::support::rtrim;
30 using lyx::support::suffixIs;
31 using lyx::support::contains;
38 using std::ostringstream;
39 using std::istringstream;
44 // thin wrapper around parse_text using a string
45 string parse_text(Parser & p, unsigned flags, const bool outer,
49 parse_text(p, os, flags, outer, context);
53 // parses a subdocument, usually useful in insets (whence the name)
54 void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer,
57 Context newcontext(true, context.textclass);
58 parse_text(p, os, flags, outer, newcontext);
59 newcontext.check_end_layout(os);
63 // parses a paragraph snippet, useful for example for \emph{...}
64 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
67 Context newcontext(false, context.textclass);
68 parse_text(p, os, flags, outer, newcontext);
69 // should not be needed
70 newcontext.check_end_layout(os);
76 char const * known_latex_commands[] = { "ref", "cite", "label", "index",
77 "printindex", "pageref", "url", "vref", "vpageref", "prettyref", "eqref", 0 };
79 // LaTeX names for quotes
80 char const * known_quotes[] = { "glqq", "grqq", "quotedblbase",
81 "textquotedblleft", "quotesinglbase", "guilsinglleft", "guilsinglright", 0};
83 // the same as known_quotes with .lyx names
84 char const * known_coded_quotes[] = { "gld", "grd", "gld",
85 "grd", "gls", "fls", "frd", 0};
87 char const * known_sizes[] = { "tiny", "scriptsize", "footnotesize",
88 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
90 char const * known_coded_sizes[] = { "tiny", "scriptsize", "footnotesize",
91 "small", "normal", "large", "larger", "largest", "huge", "giant", 0};
93 // splits "x=z, y=b" into a map
94 map<string, string> split_map(string const & s)
96 map<string, string> res;
99 for (size_t i = 0; i < v.size(); ++i) {
100 size_t const pos = v[i].find('=');
101 string const index = v[i].substr(0, pos);
102 string const value = v[i].substr(pos + 1, string::npos);
103 res[trim(index)] = trim(value);
110 * Split a LaTeX length into value and unit.
111 * The latter can be a real unit like "pt", or a latex length variable
112 * like "\textwidth". The unit may contain additional stuff like glue
113 * lengths, but we don't care, because such lengths are ERT anyway.
114 * \return true if \param value and \param unit are valid.
116 bool splitLatexLength(string const & len, string & value, string & unit)
120 const string::size_type i = len.find_first_not_of(" -+0123456789.,");
121 //'4,5' is a valid LaTeX length number. Change it to '4.5'
122 string const length = lyx::support::subst(len, ',', '.');
123 if (i == string::npos)
126 if (len[0] == '\\') {
127 // We had something like \textwidth without a factor
133 value = trim(string(length, 0, i));
137 // 'cM' is a valid LaTeX length unit. Change it to 'cm'
138 if (contains(len, '\\'))
139 unit = trim(string(len, i));
141 unit = lyx::support::lowercase(trim(string(len, i)));
146 // A simple function to translate a latex length to something lyx can
147 // understand. Not perfect, but rather best-effort.
148 bool translate_len(string const & length, string & valstring, string & unit)
150 if (!splitLatexLength(length, valstring, unit))
152 // LyX uses percent values
154 istringstream iss(valstring);
159 string const percentval = oss.str();
161 if (unit.empty() || unit[0] != '\\')
163 string::size_type const i = unit.find(' ');
164 string const endlen = (i == string::npos) ? string() : string(unit, i);
165 if (unit == "\\textwidth") {
166 valstring = percentval;
167 unit = "text%" + endlen;
168 } else if (unit == "\\columnwidth") {
169 valstring = percentval;
170 unit = "col%" + endlen;
171 } else if (unit == "\\paperwidth") {
172 valstring = percentval;
173 unit = "page%" + endlen;
174 } else if (unit == "\\linewidth") {
175 valstring = percentval;
176 unit = "line%" + endlen;
177 } else if (unit == "\\paperheight") {
178 valstring = percentval;
179 unit = "pheight%" + endlen;
180 } else if (unit == "\\textheight") {
181 valstring = percentval;
182 unit = "theight%" + endlen;
188 string translate_len(string const & length)
192 if (translate_len(length, value, unit))
194 // If the input is invalid, return what we have.
200 * Translates a LaTeX length into \param value, \param unit and
201 * \param special parts suitable for a box inset.
202 * The difference from translate_len() is that a box inset knows about
203 * some special "units" that are stored in \param special.
205 void translate_box_len(string const & length, string & value, string & unit, string & special)
207 if (translate_len(length, value, unit)) {
208 if (unit == "\\height" || unit == "\\depth" ||
209 unit == "\\totalheight" || unit == "\\width") {
210 special = unit.substr(1);
211 // The unit is not used, but LyX requires a dummy setting
223 void begin_inset(ostream & os, string const & name)
225 os << "\n\\begin_inset " << name;
229 void end_inset(ostream & os)
231 os << "\n\\end_inset \n\n";
235 void skip_braces(Parser & p)
237 if (p.next_token().cat() != catBegin)
240 if (p.next_token().cat() == catEnd) {
248 void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true)
251 // We must have a valid layout before outputting the ERT inset.
252 context.check_layout(os);
254 Context newcontext(true, context.textclass);
255 begin_inset(os, "ERT");
256 os << "\nstatus collapsed\n";
257 newcontext.check_layout(os);
258 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
260 os << "\n\\backslash \n";
264 newcontext.check_end_layout(os);
269 void handle_comment(ostream & os, string const & s, Context & context)
271 // TODO: Handle this better
272 Context newcontext(true, context.textclass);
273 begin_inset(os, "ERT");
274 os << "\nstatus collapsed\n";
275 newcontext.check_layout(os);
276 for (string::const_iterator it = s.begin(), et = s.end(); it != et; ++it) {
278 os << "\n\\backslash \n";
282 // make sure that our comment is the last thing on the line
284 newcontext.check_end_layout(os);
289 class isLayout : public std::unary_function<LyXLayout_ptr, bool> {
291 isLayout(string const name) : name_(name) {}
292 bool operator()(LyXLayout_ptr const & ptr) const {
293 return ptr->latexname() == name_;
300 LyXLayout_ptr findLayout(LyXTextClass const & textclass,
303 LyXTextClass::const_iterator beg = textclass.begin();
304 LyXTextClass::const_iterator end = textclass.end();
306 LyXTextClass::const_iterator
307 it = std::find_if(beg, end, isLayout(name));
309 return (it == end) ? LyXLayout_ptr() : *it;
313 void output_command_layout(ostream & os, Parser & p, bool outer,
314 Context & parent_context,
315 LyXLayout_ptr newlayout)
317 parent_context.check_end_layout(os);
318 Context context(true, parent_context.textclass, newlayout,
319 parent_context.layout);
320 context.check_deeper(os);
321 context.check_layout(os);
322 if (context.layout->optionalargs > 0) {
324 if (p.next_token().character() == '[') {
325 p.get_token(); // eat '['
326 begin_inset(os, "OptArg\n");
327 os << "status collapsed\n\n";
328 parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
332 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
333 context.check_end_layout(os);
334 context.check_end_deeper(os);
335 // We don't need really a new paragraph, but
336 // we must make sure that the next item gets a \begin_layout.
337 parent_context.new_paragraph(os);
342 * Output a space if necessary.
343 * This function gets called for every whitespace token.
345 * We have three cases here:
346 * 1. A space must be suppressed. Example: The lyxcode case below
347 * 2. A space may be suppressed. Example: Spaces before "\par"
348 * 3. A space must not be suppressed. Example: A space between two words
350 * We currently handle only 1. and 3 and from 2. only the case of
351 * spaces before newlines as a side effect.
353 * 2. could be used to suppress as many spaces as possible. This has two effects:
354 * - Reimporting LyX generated LaTeX files changes almost no whitespace
355 * - Superflous whitespace from non LyX generated LaTeX files is removed.
356 * The drawback is that the logic inside the function becomes
357 * complicated, and that is the reason why it is not implemented.
359 void check_space(Parser const & p, ostream & os, Context & context)
361 Token const next = p.next_token();
362 Token const curr = p.curr_token();
363 // A space before a single newline and vice versa must be ignored
364 // LyX emits a newline before \end{lyxcode}.
365 // This newline must be ignored,
366 // otherwise LyX will add an additional protected space.
367 if (next.cat() == catSpace ||
368 next.cat() == catNewline ||
369 (next.cs() == "end" && context.layout->free_spacing && curr.cat() == catNewline)) {
372 context.check_layout(os);
378 * Check wether \param command is a known command. If yes,
379 * handle the command with all arguments.
380 * \return true if the command was parsed, false otherwise.
382 bool parse_command(string const & command, Parser & p, ostream & os,
383 bool outer, Context & context)
385 if (known_commands.find(command) != known_commands.end()) {
386 vector<ArgumentType> const & template_arguments = known_commands[command];
387 string ert = command;
388 size_t no_arguments = template_arguments.size();
389 for (size_t i = 0; i < no_arguments; ++i) {
390 switch (template_arguments[i]) {
392 // This argument contains regular LaTeX
393 handle_ert(os, ert + '{', context);
394 parse_text(p, os, FLAG_ITEM, outer, context);
398 // This argument may contain special characters
399 ert += '{' + p.verbatim_item() + '}';
406 handle_ert(os, ert, context);
413 /// Parses a minipage or parbox
414 void parse_box(Parser & p, ostream & os, unsigned flags, bool outer,
415 Context & parent_context, bool use_parbox)
419 string height_value = "0";
420 string height_unit = "pt";
421 string height_special = "none";
423 if (p.next_token().asInput() == "[") {
424 position = p.getArg('[', ']');
425 if (position != "t" && position != "c" && position != "b") {
427 cerr << "invalid position for minipage/parbox" << endl;
429 if (p.next_token().asInput() == "[") {
430 latex_height = p.getArg('[', ']');
431 translate_box_len(latex_height, height_value, height_unit, height_special);
433 if (p.next_token().asInput() == "[") {
434 inner_pos = p.getArg('[', ']');
435 if (inner_pos != "c" && inner_pos != "t" &&
436 inner_pos != "b" && inner_pos != "s") {
437 inner_pos = position;
438 cerr << "invalid inner_pos for minipage/parbox"
446 string const latex_width = p.verbatim_item();
447 translate_len(latex_width, width_value, width_unit);
448 if (contains(width_unit, "\\") || contains(height_unit, "\\")) {
449 // LyX can't handle length variables
454 ss << "\\begin{minipage}";
455 if (!position.empty())
456 ss << '[' << position << ']';
457 if (!latex_height.empty())
458 ss << '[' << latex_height << ']';
459 if (!inner_pos.empty())
460 ss << '[' << inner_pos << ']';
461 ss << "{" << latex_width << "}";
464 handle_ert(os, ss.str(), parent_context);
465 parent_context.new_paragraph(os);
466 parse_text_in_inset(p, os, flags, outer, parent_context);
468 handle_ert(os, "}", parent_context);
470 handle_ert(os, "\\end{minipage}", parent_context);
472 // LyX does not like empty positions, so we have
473 // to set them to the LaTeX default values here.
474 if (position.empty())
476 if (inner_pos.empty())
477 inner_pos = position;
478 parent_context.check_layout(os);
479 begin_inset(os, "Box Frameless\n");
480 os << "position \"" << position << "\"\n";
481 os << "hor_pos \"c\"\n";
482 os << "has_inner_box 1\n";
483 os << "inner_pos \"" << inner_pos << "\"\n";
484 os << "use_parbox " << use_parbox << "\n";
485 os << "width \"" << width_value << width_unit << "\"\n";
486 os << "special \"none\"\n";
487 os << "height \"" << height_value << height_unit << "\"\n";
488 os << "height_special \"" << height_special << "\"\n";
489 os << "status open\n\n";
490 parse_text_in_inset(p, os, flags, outer, parent_context);
492 #ifdef PRESERVE_LAYOUT
493 // lyx puts a % after the end of the minipage
494 if (p.next_token().cat() == catNewline && p.next_token().cs().size() > 1) {
496 //handle_comment(os, "%dummy", parent_context);
499 parent_context.new_paragraph(os);
501 else if (p.next_token().cat() == catSpace || p.next_token().cat() == catNewline) {
502 //handle_comment(os, "%dummy", parent_context);
505 // We add a protected space if something real follows
506 if (p.good() && p.next_token().cat() != catComment) {
507 os << "\\InsetSpace ~\n";
515 void parse_environment(Parser & p, ostream & os, bool outer,
516 Context & parent_context)
518 LyXLayout_ptr newlayout;
519 string const name = p.getArg('{', '}');
520 const bool is_starred = suffixIs(name, '*');
521 string const unstarred_name = rtrim(name, "*");
522 active_environments.push_back(name);
525 if (is_math_env(name)) {
526 parent_context.check_layout(os);
527 begin_inset(os, "Formula ");
528 os << "\\begin{" << name << "}";
529 parse_math(p, os, FLAG_END, MATH_MODE);
530 os << "\\end{" << name << "}";
534 else if (name == "tabular") {
535 parent_context.check_layout(os);
536 begin_inset(os, "Tabular ");
537 handle_tabular(p, os, parent_context);
541 else if (parent_context.textclass.floats().typeExist(unstarred_name)) {
542 parent_context.check_layout(os);
543 begin_inset(os, "Float " + unstarred_name + "\n");
544 if (p.next_token().asInput() == "[") {
545 os << "placement " << p.getArg('[', ']') << '\n';
547 os << "wide " << tostr(is_starred)
548 << "\nstatus open\n\n";
549 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
551 // We don't need really a new paragraph, but
552 // we must make sure that the next item gets a \begin_layout.
553 parent_context.new_paragraph(os);
556 else if (name == "minipage")
557 parse_box(p, os, FLAG_END, outer, parent_context, false);
559 // Alignment settings
560 else if (name == "center" || name == "flushleft" || name == "flushright" ||
561 name == "centering" || name == "raggedright" || name == "raggedleft") {
562 // We must begin a new paragraph if not already done
563 if (! parent_context.atParagraphStart()) {
564 parent_context.check_end_layout(os);
565 parent_context.new_paragraph(os);
567 if (name == "flushleft" || name == "raggedright")
568 parent_context.add_extra_stuff("\\align left ");
569 else if (name == "flushright" || name == "raggedleft")
570 parent_context.add_extra_stuff("\\align right ");
572 parent_context.add_extra_stuff("\\align center ");
573 parse_text(p, os, FLAG_END, outer, parent_context);
574 // Just in case the environment is empty ..
575 parent_context.extra_stuff.erase();
576 // We must begin a new paragraph to reset the alignment
577 parent_context.new_paragraph(os);
580 // The single '=' is meant here.
581 else if ((newlayout = findLayout(parent_context.textclass, name)).get() &&
582 newlayout->isEnvironment()) {
583 Context context(true, parent_context.textclass, newlayout,
584 parent_context.layout);
585 parent_context.check_end_layout(os);
586 switch (context.layout->latextype) {
587 case LATEX_LIST_ENVIRONMENT:
588 context.extra_stuff = "\\labelwidthstring "
589 + p.verbatim_item() + '\n';
592 case LATEX_BIB_ENVIRONMENT:
593 p.verbatim_item(); // swallow next arg
599 context.check_deeper(os);
600 parse_text(p, os, FLAG_END, outer, context);
601 context.check_end_layout(os);
602 context.check_end_deeper(os);
603 parent_context.new_paragraph(os);
606 else if (name == "appendix") {
607 // This is no good latex style, but it works and is used in some documents...
608 parent_context.check_end_layout(os);
609 Context context(true, parent_context.textclass, parent_context.layout,
610 parent_context.layout);
611 context.check_layout(os);
612 os << "\\start_of_appendix\n";
613 parse_text(p, os, FLAG_END, outer, context);
614 context.check_end_layout(os);
617 else if (name == "comment") {
618 parent_context.check_layout(os);
619 begin_inset(os, "Note Comment\n");
620 os << "status open\n";
621 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
625 else if (name == "lyxgreyedout") {
626 parent_context.check_layout(os);
627 begin_inset(os, "Note Greyedout\n");
628 os << "status open\n";
629 parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
633 else if (name == "tabbing") {
634 // We need to remember that we have to handle '\=' specially
635 handle_ert(os, "\\begin{" + name + "}", parent_context);
636 parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
637 handle_ert(os, "\\end{" + name + "}", parent_context);
641 handle_ert(os, "\\begin{" + name + "}", parent_context);
642 parse_text_snippet(p, os, FLAG_END, outer, parent_context);
643 handle_ert(os, "\\end{" + name + "}", parent_context);
646 active_environments.pop_back();
651 } // anonymous namespace
656 void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
659 LyXLayout_ptr newlayout;
660 // Store the latest bibliographystyle (needed for bibtex inset)
661 string bibliographystyle;
663 Token const & t = p.get_token();
666 cerr << "t: " << t << " flags: " << flags << "\n";
669 if (flags & FLAG_ITEM) {
670 if (t.cat() == catSpace)
674 if (t.cat() == catBegin) {
675 // skip the brace and collect everything to the next matching
677 flags |= FLAG_BRACE_LAST;
681 // handle only this single token, leave the loop if done
685 if (t.character() == ']' && (flags & FLAG_BRACK_LAST))
691 if (t.cat() == catMath) {
692 // we are inside some text mode thingy, so opening new math is allowed
693 context.check_layout(os);
694 begin_inset(os, "Formula ");
695 Token const & n = p.get_token();
696 if (n.cat() == catMath && outer) {
697 // TeX's $$...$$ syntax for displayed math
699 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
701 p.get_token(); // skip the second '$' token
703 // simple $...$ stuff
706 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
712 else if (t.cat() == catSuper || t.cat() == catSub)
713 cerr << "catcode " << t << " illegal in text mode\n";
715 // Basic support for english quotes. This should be
716 // extended to other quotes, but is not so easy (a
717 // left english quote is the same as a right german
719 else if (t.asInput() == "`"
720 && p.next_token().asInput() == "`") {
721 context.check_layout(os);
722 begin_inset(os, "Quotes ");
728 else if (t.asInput() == "'"
729 && p.next_token().asInput() == "'") {
730 context.check_layout(os);
731 begin_inset(os, "Quotes ");
738 else if (t.cat() == catSpace || (t.cat() == catNewline && t.cs().size() == 1))
739 check_space(p, os, context);
741 else if (t.cat() == catLetter ||
742 t.cat() == catOther ||
743 t.cat() == catAlign ||
744 t.cat() == catParameter) {
745 context.check_layout(os);
749 else if (t.cat() == catNewline || (t.cat() == catEscape && t.cs() == "par")) {
751 context.new_paragraph(os);
754 else if (t.cat() == catActive) {
755 context.check_layout(os);
756 if (t.character() == '~') {
757 if (context.layout->free_spacing)
760 os << "\\InsetSpace ~\n";
765 else if (t.cat() == catBegin) {
766 // special handling of size changes
767 context.check_layout(os);
768 bool const is_size = is_known(p.next_token().cs(), known_sizes);
769 Token const prev = p.prev_token();
770 string const s = parse_text(p, FLAG_BRACE_LAST, outer, context);
771 if (s.empty() && (p.next_token().character() == '`' ||
772 (prev.character() == '-' && p.next_token().character())))
773 ; // ignore it in {}`` or -{}-
774 else if (is_size || s == "[" || s == "]" || s == "*")
777 handle_ert(os, "{", context, false);
778 // s will end the current layout and begin a new one if necessary
780 handle_ert(os, "}", context);
784 else if (t.cat() == catEnd) {
785 if (flags & FLAG_BRACE_LAST) {
788 cerr << "stray '}' in text\n";
789 handle_ert(os, "}", context);
792 else if (t.cat() == catComment) {
793 context.check_layout(os);
794 if (!t.cs().empty()) {
795 handle_comment(os, '%' + t.cs(), context);
796 if (p.next_token().cat() == catNewline) {
797 // A newline after a comment line starts a new paragraph
798 context.new_paragraph(os);
811 else if (t.cs() == "(") {
812 context.check_layout(os);
813 begin_inset(os, "Formula");
815 parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
820 else if (t.cs() == "[") {
821 context.check_layout(os);
822 begin_inset(os, "Formula");
824 parse_math(p, os, FLAG_EQUATION, MATH_MODE);
829 else if (t.cs() == "begin")
830 parse_environment(p, os, outer, context);
832 else if (t.cs() == "end") {
833 if (flags & FLAG_END) {
834 // eat environment name
835 string const name = p.getArg('{', '}');
836 if (name != active_environment())
837 cerr << "\\end{" + name + "} does not match \\begin{"
838 + active_environment() + "}\n";
841 p.error("found 'end' unexpectedly");
844 else if (t.cs() == "item") {
848 if (p.next_token().character() == '[') {
849 p.get_token(); // eat '['
850 Context newcontext(false, context.textclass);
851 s = parse_text(p, FLAG_BRACK_LAST, outer, newcontext);
855 context.check_layout(os);
857 if (context.layout->labeltype != LABEL_MANUAL) {
858 // lyx does not support \item[\mybullet] in itemize environments
859 handle_ert(os, "[", context);
861 handle_ert(os, "]", context);
862 } else if (!s.empty()) {
863 // The space is needed to separate the item from the rest of the sentence.
870 else if (t.cs() == "bibitem") {
872 context.check_layout(os);
875 os << '{' << p.verbatim_item() << '}' << "\n";
878 else if (t.cs() == "def") {
880 context.check_layout(os);
881 string name = p.get_token().cs();
882 while (p.next_token().cat() != catBegin)
883 name += p.get_token().asString();
884 handle_ert(os, "\\def\\" + name + '{' + p.verbatim_item() + '}', context);
887 else if (t.cs() == "noindent") {
889 context.add_extra_stuff("\\noindent ");
892 else if (t.cs() == "appendix") {
894 context.add_extra_stuff("\\start_of_appendix ");
897 // Must attempt to parse "Section*" before "Section".
898 else if ((p.next_token().asInput() == "*") &&
899 // The single '=' is meant here.
900 (newlayout = findLayout(context.textclass,
901 t.cs() + '*')).get() &&
902 newlayout->isCommand()) {
904 output_command_layout(os, p, outer, context, newlayout);
908 // The single '=' is meant here.
909 else if ((newlayout = findLayout(context.textclass, t.cs())).get() &&
910 newlayout->isCommand()) {
911 output_command_layout(os, p, outer, context, newlayout);
915 else if (t.cs() == "includegraphics") {
916 map<string, string> opts = split_map(p.getArg('[', ']'));
917 string name = p.verbatim_item();
919 context.check_layout(os);
920 begin_inset(os, "Graphics ");
921 os << "\n\tfilename " << name << '\n';
922 if (opts.find("width") != opts.end())
924 << translate_len(opts["width"]) << '\n';
925 if (opts.find("height") != opts.end())
927 << translate_len(opts["height"]) << '\n';
928 if (opts.find("scale") != opts.end()) {
929 istringstream iss(opts["scale"]);
933 os << "\tscale " << val << '\n';
935 if (opts.find("angle") != opts.end())
936 os << "\trotateAngle "
937 << opts["angle"] << '\n';
938 if (opts.find("origin") != opts.end()) {
940 string const opt = opts["origin"];
941 if (opt.find('l') != string::npos) ss << "left";
942 if (opt.find('r') != string::npos) ss << "right";
943 if (opt.find('c') != string::npos) ss << "center";
944 if (opt.find('t') != string::npos) ss << "Top";
945 if (opt.find('b') != string::npos) ss << "Bottom";
946 if (opt.find('B') != string::npos) ss << "Baseline";
947 if (!ss.str().empty())
948 os << "\trotateOrigin " << ss.str() << '\n';
950 cerr << "Warning: Ignoring unknown includegraphics origin argument '" << opt << "'\n";
952 if (opts.find("keepaspectratio") != opts.end())
953 os << "\tkeepAspectRatio\n";
954 if (opts.find("clip") != opts.end())
956 if (opts.find("draft") != opts.end())
958 if (opts.find("bb") != opts.end())
959 os << "\tBoundingBox "
960 << opts["bb"] << '\n';
961 int numberOfbbOptions = 0;
962 if (opts.find("bbllx") != opts.end())
964 if (opts.find("bblly") != opts.end())
966 if (opts.find("bburx") != opts.end())
968 if (opts.find("bbury") != opts.end())
970 if (numberOfbbOptions == 4)
971 os << "\tBoundingBox "
972 << opts["bbllx"] << opts["bblly"]
973 << opts["bburx"] << opts["bbury"] << '\n';
974 else if (numberOfbbOptions > 0)
975 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
976 numberOfbbOptions = 0;
977 if (opts.find("natwidth") != opts.end())
979 if (opts.find("natheight") != opts.end())
981 if (numberOfbbOptions == 2)
982 os << "\tBoundingBox 0bp 0bp "
983 << opts["natwidth"] << opts["natheight"] << '\n';
984 else if (numberOfbbOptions > 0)
985 cerr << "Warning: Ignoring incomplete includegraphics boundingbox arguments.\n";
986 ostringstream special;
987 if (opts.find("hiresbb") != opts.end())
988 special << "hiresbb,";
989 if (opts.find("trim") != opts.end())
991 if (opts.find("viewport") != opts.end())
992 special << "viewport=" << opts["viewport"] << ',';
993 if (opts.find("totalheight") != opts.end())
994 special << "totalheight=" << opts["totalheight"] << ',';
995 if (opts.find("type") != opts.end())
996 special << "type=" << opts["type"] << ',';
997 if (opts.find("ext") != opts.end())
998 special << "ext=" << opts["ext"] << ',';
999 if (opts.find("read") != opts.end())
1000 special << "read=" << opts["read"] << ',';
1001 if (opts.find("command") != opts.end())
1002 special << "command=" << opts["command"] << ',';
1003 string s_special = special.str();
1004 if (!s_special.empty()) {
1005 // We had special arguments. Remove the trailing ','.
1006 os << "\tspecial " << s_special.substr(0, s_special.size() - 1) << '\n';
1008 // TODO: Handle the unknown settings better.
1009 // Warn about invalid options.
1010 // Check wether some option was given twice.
1014 else if (t.cs() == "footnote") {
1016 context.check_layout(os);
1017 begin_inset(os, "Foot\n");
1018 os << "status collapsed\n\n";
1019 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1023 else if (t.cs() == "marginpar") {
1025 context.check_layout(os);
1026 begin_inset(os, "Marginal\n");
1027 os << "status collapsed\n\n";
1028 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
1032 else if (t.cs() == "ensuremath") {
1034 context.check_layout(os);
1035 Context newcontext(false, context.textclass);
1036 string s = parse_text(p, FLAG_ITEM, false, newcontext);
1037 if (s == "±" || s == "³" || s == "²" || s == "µ")
1040 handle_ert(os, "\\ensuremath{" + s + "}",
1044 else if (t.cs() == "hfill") {
1045 context.check_layout(os);
1046 os << "\n\\hfill\n";
1051 else if (t.cs() == "makeindex" || t.cs() == "maketitle") {
1053 skip_braces(p); // swallow this
1056 else if (t.cs() == "tableofcontents") {
1058 context.check_layout(os);
1059 begin_inset(os, "LatexCommand \\tableofcontents\n");
1061 skip_braces(p); // swallow this
1064 else if (t.cs() == "listoffigures") {
1066 context.check_layout(os);
1067 begin_inset(os, "FloatList figure\n");
1069 skip_braces(p); // swallow this
1072 else if (t.cs() == "listoftables") {
1074 context.check_layout(os);
1075 begin_inset(os, "FloatList table\n");
1077 skip_braces(p); // swallow this
1080 else if (t.cs() == "listof") {
1081 p.skip_spaces(true);
1082 string const name = p.get_token().asString();
1083 if (context.textclass.floats().typeExist(name)) {
1084 context.check_layout(os);
1085 begin_inset(os, "FloatList ");
1088 p.get_token(); // swallow second arg
1090 handle_ert(os, "\\listof{" + name + "}", context);
1093 else if (t.cs() == "textrm") {
1094 context.check_layout(os);
1095 os << "\n\\family roman \n";
1096 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1097 os << "\n\\family default \n";
1100 else if (t.cs() == "textsf") {
1101 context.check_layout(os);
1102 os << "\n\\family sans \n";
1103 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1104 os << "\n\\family default \n";
1107 else if (t.cs() == "textsl") {
1108 context.check_layout(os);
1109 os << "\n\\shape slanted \n";
1110 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1111 os << "\n\\shape default \n";
1114 else if (t.cs() == "texttt") {
1115 context.check_layout(os);
1116 os << "\n\\family typewriter \n";
1117 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1118 os << "\n\\family default \n";
1121 else if (t.cs() == "textit") {
1122 context.check_layout(os);
1123 os << "\n\\shape italic \n";
1124 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1125 os << "\n\\shape default \n";
1128 else if (t.cs() == "textsc") {
1129 context.check_layout(os);
1130 os << "\n\\noun on \n";
1131 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1132 os << "\n\\noun default \n";
1135 else if (t.cs() == "textbf") {
1136 context.check_layout(os);
1137 os << "\n\\series bold \n";
1138 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1139 os << "\n\\series default \n";
1142 else if (t.cs() == "underbar") {
1143 context.check_layout(os);
1144 os << "\n\\bar under \n";
1145 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1146 os << "\n\\bar default \n";
1149 else if (t.cs() == "emph" || t.cs() == "noun") {
1150 context.check_layout(os);
1151 os << "\n\\" << t.cs() << " on \n";
1152 parse_text_snippet(p, os, FLAG_ITEM, outer, context);
1153 os << "\n\\" << t.cs() << " default \n";
1156 else if (is_known(t.cs(), known_latex_commands)) {
1157 context.check_layout(os);
1158 begin_inset(os, "LatexCommand ");
1159 os << '\\' << t.cs();
1162 os << '{' << p.verbatim_item() << "}\n";
1166 else if (is_known(t.cs(), known_quotes)) {
1167 char const ** where = is_known(t.cs(), known_quotes);
1168 context.check_layout(os);
1169 begin_inset(os, "Quotes ");
1170 os << known_coded_quotes[where - known_quotes];
1175 else if (is_known(t.cs(), known_sizes)) {
1176 char const ** where = is_known(t.cs(), known_sizes);
1177 context.check_layout(os);
1178 os << "\n\\size " << known_coded_sizes[where - known_sizes] << "\n";
1182 else if (t.cs() == "LyX" || t.cs() == "TeX"
1183 || t.cs() == "LaTeX") {
1184 context.check_layout(os);
1186 skip_braces(p); // eat {}
1189 else if (t.cs() == "LaTeXe") {
1190 context.check_layout(os);
1192 skip_braces(p); // eat {}
1195 else if (t.cs() == "ldots") {
1196 context.check_layout(os);
1198 os << "\\SpecialChar \\ldots{}\n";
1201 else if (t.cs() == "lyxarrow") {
1202 context.check_layout(os);
1203 os << "\\SpecialChar \\menuseparator\n";
1207 else if (t.cs() == "textcompwordmark") {
1208 context.check_layout(os);
1209 os << "\\SpecialChar \\textcompwordmark{}\n";
1213 else if (t.cs() == "@" && p.next_token().asInput() == ".") {
1214 context.check_layout(os);
1215 os << "\\SpecialChar \\@.\n";
1219 else if (t.cs() == "-") {
1220 context.check_layout(os);
1221 os << "\\SpecialChar \\-\n";
1224 else if (t.cs() == "textasciitilde") {
1225 context.check_layout(os);
1230 else if (t.cs() == "textasciicircum") {
1231 context.check_layout(os);
1236 else if (t.cs() == "textbackslash") {
1237 context.check_layout(os);
1238 os << "\n\\backslash \n";
1242 else if (t.cs() == "_" || t.cs() == "&" || t.cs() == "#"
1243 || t.cs() == "$" || t.cs() == "{" || t.cs() == "}"
1245 context.check_layout(os);
1249 else if (t.cs() == "char") {
1250 context.check_layout(os);
1251 if (p.next_token().character() == '`') {
1253 if (p.next_token().cs() == "\"") {
1258 handle_ert(os, "\\char`", context);
1261 handle_ert(os, "\\char", context);
1265 else if (t.cs() == "\"") {
1266 context.check_layout(os);
1267 string const name = p.verbatim_item();
1268 if (name == "a") os << 'ä';
1269 else if (name == "o") os << 'ö';
1270 else if (name == "u") os << 'ü';
1271 else if (name == "A") os << 'Ä';
1272 else if (name == "O") os << 'Ö';
1273 else if (name == "U") os << 'Ü';
1274 else handle_ert(os, "\"{" + name + "}", context);
1277 // Problem: \= creates a tabstop inside the tabbing environment
1278 // and else an accent. In the latter case we really would want
1279 // \={o} instead of \= o.
1280 else if (t.cs() == "=" && (flags & FLAG_TABBING))
1281 handle_ert(os, t.asInput(), context);
1283 else if (t.cs() == "H" || t.cs() == "c" || t.cs() == "^" || t.cs() == "'"
1284 || t.cs() == "~" || t.cs() == "." || t.cs() == "=") {
1285 // we need the trim as the LyX parser chokes on such spaces
1286 context.check_layout(os);
1287 os << "\n\\i \\" << t.cs() << "{"
1288 << trim(parse_text(p, FLAG_ITEM, outer, context), " ") << "}\n";
1291 else if (t.cs() == "ss") {
1292 context.check_layout(os);
1296 else if (t.cs() == "i" || t.cs() == "j") {
1297 context.check_layout(os);
1298 os << "\\" << t.cs() << ' ';
1301 else if (t.cs() == "\\") {
1302 context.check_layout(os);
1303 string const next = p.next_token().asInput();
1305 handle_ert(os, "\\\\" + p.getOpt(), context);
1306 else if (next == "*") {
1308 handle_ert(os, "\\\\*" + p.getOpt(), context);
1311 os << "\n\\newline \n";
1315 else if (t.cs() == "input" || t.cs() == "include"
1316 || t.cs() == "verbatiminput") {
1317 string name = '\\' + t.cs();
1318 if (t.cs() == "verbatiminput"
1319 && p.next_token().asInput() == "*")
1320 name += p.get_token().asInput();
1321 context.check_layout(os);
1322 begin_inset(os, "Include ");
1323 string filename(p.getArg('{', '}'));
1324 string lyxname(lyx::support::ChangeExtension(filename, ".lyx"));
1325 if (tex2lyx(filename, lyxname)) {
1326 os << name << '{' << lyxname << "}\n";
1328 os << name << '{' << filename << "}\n";
1330 os << "preview false\n";
1334 else if (t.cs() == "fancyhead") {
1335 context.check_layout(os);
1337 ss << "\\fancyhead";
1339 ss << '{' << p.verbatim_item() << "}\n";
1340 handle_ert(os, ss.str(), context);
1343 else if (t.cs() == "bibliographystyle") {
1344 // store new bibliographystyle
1345 bibliographystyle = p.verbatim_item();
1346 // output new bibliographystyle.
1347 // This is only necessary if used in some other macro than \bibliography.
1348 handle_ert(os, "\\bibliographystyle{" + bibliographystyle + "}", context);
1351 else if (t.cs() == "bibliography") {
1352 context.check_layout(os);
1353 begin_inset(os, "LatexCommand ");
1355 // Do we have a bibliographystyle set?
1356 if (!bibliographystyle.empty()) {
1357 os << '[' << bibliographystyle << ']';
1359 os << '{' << p.verbatim_item() << "}\n";
1363 else if (t.cs() == "parbox")
1364 parse_box(p, os, FLAG_ITEM, outer, context, true);
1366 else if (t.cs() == "smallskip" ||
1367 t.cs() == "medskip" ||
1368 t.cs() == "bigskip" ||
1369 t.cs() == "vfill") {
1370 context.check_layout(os);
1371 begin_inset(os, "VSpace ");
1376 else if (t.cs() == "vspace") {
1377 bool starred = false;
1378 if (p.next_token().asInput() == "*") {
1382 string const length = p.verbatim_item();
1385 bool valid = splitLatexLength(length, valstring, unit);
1386 bool known_vspace = false;
1387 bool known_unit = false;
1390 istringstream iss(valstring);
1393 if (unit == "\\smallskipamount") {
1395 known_vspace = true;
1396 } else if (unit == "\\medskipamount") {
1398 known_vspace = true;
1399 } else if (unit == "\\bigskipamount") {
1401 known_vspace = true;
1402 } else if (unit == "\\fill") {
1404 known_vspace = true;
1407 switch (unitFromString(unit)) {
1428 if (known_unit || known_vspace) {
1429 // Literal length or known variable
1430 context.check_layout(os);
1431 begin_inset(os, "VSpace ");
1439 // LyX can't handle other length variables in Inset VSpace
1440 string name = t.asInput();
1445 handle_ert(os, name + '{' + unit + '}', context);
1446 else if (value == -1.0)
1447 handle_ert(os, name + "{-" + unit + '}', context);
1449 handle_ert(os, name + '{' + valstring + unit + '}', context);
1451 handle_ert(os, name + '{' + length + '}', context);
1456 //cerr << "#: " << t << " mode: " << mode << endl;
1457 // heuristic: read up to next non-nested space
1459 string s = t.asInput();
1460 string z = p.verbatim_item();
1461 while (p.good() && z != " " && z.size()) {
1462 //cerr << "read: " << z << endl;
1464 z = p.verbatim_item();
1466 cerr << "found ERT: " << s << endl;
1467 handle_ert(os, s + ' ', context);
1469 string name = t.asInput();
1470 if (p.next_token().asInput() == "*") {
1471 // Starred commands like \vspace*{}
1472 p.get_token(); // Eat '*'
1475 if (! parse_command(name, p, os, outer, context))
1476 handle_ert(os, name, context);
1479 if (flags & FLAG_LEAVE) {
1480 flags &= ~FLAG_LEAVE;