]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: cosmetic
[lyx.git] / src / tex2lyx / text.cpp
index 5a22ecf614b2a473c58ea5e6d48ed124564769c6..88a08d7e043e0bf536cff627028d3c3ae29d7294 100644 (file)
@@ -133,9 +133,9 @@ char const * const known_coded_quotes[] = { "prd", "ard", "ard", "ard",
 char const * const known_sizes[] = { "tiny", "scriptsize", "footnotesize",
 "small", "normalsize", "large", "Large", "LARGE", "huge", "Huge", 0};
 
-/// the same as known_sizes with .lyx names
+/// the same as known_sizes with .lyx names plus a default entry
 char const * const known_coded_sizes[] = { "default", "tiny", "scriptsize", "footnotesize",
-"small", "normal", "large", "larger", "largest",  "huge", "giant", 0};
+"small", "normal", "large", "larger", "largest", "huge", "giant", 0};
 
 /// LaTeX 2.09 names for font families
 char const * const known_old_font_families[] = { "rm", "sf", "tt", 0};
@@ -765,6 +765,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
                end_inset(os);
                p.skip_spaces();
+               skip_braces(p); // eat {} that might by set by LyX behind comments
        }
 
        else if (name == "lyxgreyedout") {
@@ -801,23 +802,28 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parse_unknown_environment(p, name, os, FLAG_END, outer,
                                          parent_context);
 
-       // Alignment settings
-       else if (name == "center" || name == "flushleft" || name == "flushright" ||
-                name == "centering" || name == "raggedright" || name == "raggedleft") {
+       // Alignment and spacing settings
+       // FIXME (bug xxxx): These settings can span multiple paragraphs and
+       //                                       therefore are totally broken!
+       // Note that \centering, raggedright, and raggedleft cannot be handled, as
+       // they are commands not environments. They are furthermore switches that
+       // can be ended by another switches, but also by commands like \footnote or
+       // \parbox. So the only safe way is to leave them untouched.
+       else if (name == "center" || name == "flushleft" || name == "flushright") {
                eat_whitespace(p, os, parent_context, false);
                // We must begin a new paragraph if not already done
                if (! parent_context.atParagraphStart()) {
                        parent_context.check_end_layout(os);
                        parent_context.new_paragraph(os);
                }
-               if (name == "flushleft" || name == "raggedright")
+               if (name == "flushleft")
                        parent_context.add_extra_stuff("\\align left\n");
-               else if (name == "flushright" || name == "raggedleft")
+               else if (name == "flushright")
                        parent_context.add_extra_stuff("\\align right\n");
-               else
+               else if (name == "center")
                        parent_context.add_extra_stuff("\\align center\n");
                parse_text(p, os, FLAG_END, outer, parent_context);
-               // Just in case the environment is empty ..
+               // Just in case the environment is empty
                parent_context.extra_stuff.erase();
                // We must begin a new paragraph to reset the alignment
                parent_context.new_paragraph(os);
@@ -1530,22 +1536,44 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        eat_whitespace(p, os, context, true);
                }
 
+               // Starred section headings
                // Must attempt to parse "Section*" before "Section".
                else if ((p.next_token().asInput() == "*") &&
                         context.new_layout_allowed &&
-                        // The single '=' is meant here.
                         (newlayout = findLayout(context.textclass, t.cs() + '*')) &&
-                         newlayout->isCommand()) {
+                        newlayout->isCommand()) {
+                       TeXFont const oldFont = context.font;
+                       // save the current font size
+                       string const size = oldFont.size;
+                       // reset the font size to default, because the font size switches don't
+                       // affect section headings and the like
+                       context.font.size = known_coded_sizes[0];
+                       output_font_change(os, oldFont, context.font);
+                       // write the layout
                        p.get_token();
                        output_command_layout(os, p, outer, context, newlayout);
+                       // set the font size to the original value
+                       context.font.size = size;
+                       output_font_change(os, oldFont, context.font);
                        p.skip_spaces();
                }
 
-               // The single '=' is meant here.
+               // Section headings and the like
                else if (context.new_layout_allowed &&
                         (newlayout = findLayout(context.textclass, t.cs())) &&
                         newlayout->isCommand()) {
+                       TeXFont const oldFont = context.font;
+                       // save the current font size
+                       string const size = oldFont.size;
+                       // reset the font size to default, because the font size switches don't
+                       // affect section headings and the like
+                       context.font.size = known_coded_sizes[0];
+                       output_font_change(os, oldFont, context.font);
+                       // write the layout
                        output_command_layout(os, p, outer, context, newlayout);
+                       // set the font size to the original value
+                       context.font.size = size;
+                       output_font_change(os, oldFont, context.font);
                        p.skip_spaces();
                }
 
@@ -1847,6 +1875,23 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                eat_whitespace(p, os, context, false);
                }
 
+               else if (t.cs() == "textcolor") {
+                       // scheme is \textcolor{color name}{text}
+                       string const color = p.verbatim_item();
+                       // we only support the predefined colors of the color package
+                       if (color == "black" || color == "blue" || color == "cyan"
+                               || color == "green" || color == "magenta" || color == "red"
+                               || color == "white" || color == "yellow") {
+                                       context.check_layout(os);
+                                       os << "\n\\color " << color << "\n";
+                                       parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+                                       context.check_layout(os);
+                                       os << "\n\\color inherit\n";
+                       } else
+                               // for custom defined colors
+                               handle_ert(os, t.asInput() + "{" + color + "}", context);
+               }
+
                else if (t.cs() == "underbar") {
                        // Do NOT handle \underline.
                        // \underbar cuts through y, g, q, p etc.,
@@ -1866,6 +1911,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n\\" << t.cs() << " default\n";
                }
 
+               else if (t.cs() == "lyxline") {
+                       context.check_layout(os);
+                       os << "\\lyxline";
+               }
+
                else if (use_natbib &&
                         is_known(t.cs(), known_natbib_commands) &&
                         ((t.cs() != "citefullauthor" &&
@@ -1984,7 +2034,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        char const * const * where = is_known(t.cs(), known_sizes);
                        context.check_layout(os);
                        TeXFont const oldFont = context.font;
-                       context.font.size = known_coded_sizes[where - known_sizes];
+                       // the font size index differs by 1, because the known_coded_sizes
+                       // has additionally a "default" entry
+                       context.font.size = known_coded_sizes[where - known_sizes + 1];
                        output_font_change(os, oldFont, context.font);
                        eat_whitespace(p, os, context, false);
                }
@@ -2373,7 +2425,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                else if (is_known(t.cs(), known_spaces)) {
                        char const * const * where = is_known(t.cs(), known_spaces);
                        context.check_layout(os);
-                       begin_inset(os, "InsetSpace ");
+                       os << "\\InsetSpace ";
                        os << '\\' << known_coded_spaces[where - known_spaces]
                           << '\n';
                        // LaTeX swallows whitespace after all spaces except
@@ -2580,7 +2632,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                        }
                                }
                        }
-
                        if (known_unit || known_vspace) {
                                // Literal length or known variable
                                context.check_layout(os);
@@ -2608,6 +2659,79 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                }
 
+               else if (t.cs() == "hspace") {
+                       bool starred = false;
+                       if (p.next_token().asInput() == "*") {
+                               p.get_token();
+                               starred = true;
+                       }
+                       string const length = p.verbatim_item();
+                       string unit;
+                       string valstring;
+                       bool valid = splitLatexLength(length, valstring, unit);
+                       bool known_unit = false;
+                       bool fill = false;
+                       double value;
+                       if (valid) {
+                               istringstream iss(valstring);
+                               iss >> value;
+                               if (value == 1.0)
+                                       if (unit == "\\fill") {
+                                               known_unit = true;
+                                               fill = true;
+                                       }
+                               switch (unitFromString(unit)) {
+                               case Length::SP:
+                               case Length::PT:
+                               case Length::BP:
+                               case Length::DD:
+                               case Length::MM:
+                               case Length::PC:
+                               case Length::CC:
+                               case Length::CM:
+                               case Length::IN:
+                               case Length::EX:
+                               case Length::EM:
+                               case Length::MU:
+                                       known_unit = true;
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+                       if (known_unit) {
+                               // Literal length or known variable
+                               context.check_layout(os);
+                               begin_inset(os, "Space ");
+                               if (known_unit) {
+                                       os << "\\hspace";
+                                       if (starred)
+                                               os << '*';
+                                       if (fill)
+                                               os << "{" + unit + "}";
+                                       else {
+                                               os << "{}\n";
+                                               os << "\\length " << value << unit;
+                                       }
+                               }                               
+                               end_inset(os);
+                       } else {
+                               // LyX can't handle other length variables in Inset HSpace
+                               string name = t.asInput();
+                               if (starred)
+                                       name += '*';
+                               if (valid) {
+                                       if (value == 1.0)
+                                               handle_ert(os, name + '{' + unit + '}', context);
+                                       else if (value == -1.0)
+                                               handle_ert(os, name + "{-" + unit + '}', context);
+                                       else
+                                               handle_ert(os, name + '{' + valstring + unit + '}', context);
+                               } else
+                                       handle_ert(os, name + '{' + length + '}', context);
+                       }
+               }
+
                else {
                        //cerr << "#: " << t << " mode: " << mode << endl;
                        // heuristic: read up to next non-nested space