]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: code simplification
[lyx.git] / src / tex2lyx / text.cpp
index 086bbbef8b975a2212f4dc3d7dd4effe5b059364..8a9951d3c713acad8f59ff05cb7c5987de10deee 100644 (file)
@@ -109,6 +109,9 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
 char const * const known_ref_commands[] = { "ref", "pageref", "vref",
  "vpageref", "prettyref", "eqref", 0 };
 
+char const * const known_coded_ref_commands[] = { "ref", "pageref", "vref",
+ "vpageref", "formatted", "eqref", 0 };
+
 /*!
  * natbib commands.
  * The starred forms are also known except for "citefullauthor",
@@ -207,15 +210,18 @@ char const * const known_pdftex_graphics_formats[] = {"png", "pdf", "jpg",
 char const * const known_tex_extensions[] = {"tex", 0};
 
 /// spaces known by InsetSpace
-char const * const known_spaces[] = { " ", "space", ",", "thinspace", "quad",
-"qquad", "enspace", "enskip", "negthinspace", "hfill", "dotfill", "hrulefill",
-"leftarrowfill", "rightarrowfill", "upbracefill", "downbracefill", 0};
+char const * const known_spaces[] = { " ", "space", ",",
+"thinspace", "quad", "qquad", "enspace", "enskip",
+"negthinspace", "negmedspace", "negthickspace", "textvisiblespace",
+"hfill", "dotfill", "hrulefill", "leftarrowfill", "rightarrowfill",
+"upbracefill", "downbracefill", 0};
 
 /// the same as known_spaces with .lyx names
 char const * const known_coded_spaces[] = { "space{}", "space{}",
 "thinspace{}", "thinspace{}", "quad{}", "qquad{}", "enspace{}", "enskip{}",
-"negthinspace{}", "hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}",
-"rightarrowfill{}", "upbracefill{}", "downbracefill{}", 0};
+"negthinspace{}", "negmedspace{}", "negthickspace{}", "textvisiblespace{}",
+"hfill{}", "dotfill{}", "hrulefill{}", "leftarrowfill{}", "rightarrowfill{}",
+"upbracefill{}", "downbracefill{}", 0};
 
 /// These are translated by LyX to commands like "\\LyX{}", so we have to put
 /// them in ERT. "LaTeXe" must come before "LaTeX"!
@@ -577,7 +583,7 @@ void output_command_layout(ostream & os, Parser & p, bool outer,
                    p.next_token().character() != '[') 
                        break;
                p.get_token(); // eat '['
-               begin_inset(os, "OptArg\n");
+               begin_inset(os, "Argument\n");
                os << "status collapsed\n\n";
                parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
                end_inset(os);
@@ -585,12 +591,12 @@ void output_command_layout(ostream & os, Parser & p, bool outer,
                ++optargs;
        }
        unsigned int reqargs = 0;
-       while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
+       while (reqargs < context.layout->reqargs) {
                eat_whitespace(p, os, context, false);
                if (p.next_token().cat() != catBegin)
                        break;
                p.get_token(); // eat '{'
-               begin_inset(os, "OptArg\n");
+               begin_inset(os, "Argument\n");
                os << "status collapsed\n\n";
                parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
                end_inset(os);
@@ -866,6 +872,7 @@ void parse_box(Parser & p, ostream & os, unsigned outer_flags,
                os << "has_inner_box " << !inner_type.empty() << "\n";
                os << "inner_pos \"" << inner_pos << "\"\n";
                os << "use_parbox " << (inner_type == "parbox") << '\n';
+               os << "use_makebox 0\n";
                os << "width \"" << width_value << width_unit << "\"\n";
                os << "special \"none\"\n";
                os << "height \"" << height_value << height_unit << "\"\n";
@@ -874,6 +881,8 @@ void parse_box(Parser & p, ostream & os, unsigned outer_flags,
                Context context(true, parent_context.textclass);
                context.font = parent_context.font;
 
+               // FIXME, the inset layout should be plain, not standard, see bug #7846
+
                // If we have no inner box the contens will be read with the outer box
                if (!inner_type.empty())
                        parse_text(p, os, inner_flags, outer, context);
@@ -1066,6 +1075,25 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                p.skip_spaces();
        }
 
+       else if (unstarred_name == "sidewaysfigure"
+               || unstarred_name == "sidewaystable") {
+               eat_whitespace(p, os, parent_context, false);
+               parent_context.check_layout(os);
+               if (unstarred_name == "sidewaysfigure")
+                       begin_inset(os, "Float figure\n");
+               else
+                       begin_inset(os, "Float table\n");
+               os << "wide " << convert<string>(is_starred)
+                  << "\nsideways true"
+                  << "\nstatus open\n\n";
+               parse_text_in_inset(p, os, FLAG_END, outer, parent_context);
+               end_inset(os);
+               // We don't need really a new paragraph, but
+               // we must make sure that the next item gets a \begin_layout.
+               parent_context.new_paragraph(os);
+               p.skip_spaces();
+       }
+
        else if (name == "minipage") {
                eat_whitespace(p, os, parent_context, false);
                parse_box(p, os, 0, FLAG_END, outer, parent_context, "", "", name);
@@ -1219,7 +1247,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                                        context.check_layout(os);
                                        need_layout = false;
                                }
-                               begin_inset(os, "OptArg\n");
+                               begin_inset(os, "Argument\n");
                                os << "status collapsed\n\n";
                                parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
                                end_inset(os);
@@ -1227,7 +1255,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                                ++optargs;
                        }
                        unsigned int reqargs = 0;
-                       while (LYX_FORMAT >= 392 && reqargs < context.layout->reqargs) {
+                       while (reqargs < context.layout->reqargs) {
                                eat_whitespace(p, os, context, false);
                                if (p.next_token().cat() != catBegin)
                                        break;
@@ -1236,7 +1264,7 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                                        context.check_layout(os);
                                        need_layout = false;
                                }
-                               begin_inset(os, "OptArg\n");
+                               begin_inset(os, "Argument\n");
                                os << "status collapsed\n\n";
                                parse_text_in_inset(p, os, FLAG_BRACE_LAST, outer, context);
                                end_inset(os);
@@ -1491,8 +1519,10 @@ void parse_noweb(Parser & p, ostream & os, Context & context)
                        os << subst(t.asInput(), "\\", "\n\\backslash\n");
                else {
                        ostringstream oss;
-                       begin_inset(oss, "Newline newline");
-                       end_inset(oss);
+                       Context tmp(false, context.textclass,
+                                   &context.textclass[from_ascii("Scrap")]);
+                       tmp.need_end_layout = true;
+                       tmp.check_layout(oss);
                        os << subst(t.asInput(), "\n", oss.str());
                }
                // The scrap chunk is ended by an @ at the beginning of a line.
@@ -2078,7 +2108,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        if (p.next_token().cat() != catEscape &&
                            p.next_token().character() == '[') {
                                p.get_token(); // eat '['
-                               begin_inset(os, "OptArg\n");
+                               begin_inset(os, "Argument\n");
                                os << "status collapsed\n";
                                parse_text_in_inset(p, os, FLAG_BRACK_LAST, outer, context);
                                end_inset(os);
@@ -2404,6 +2434,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                }
 
                else if (t.cs() == "underbar" || t.cs() == "uline") {
+                       // \underbar is not 100% correct (LyX outputs \uline
+                       // of ulem.sty). The difference is that \ulem allows
+                       // line breaks, and \underbar does not.
                        // Do NOT handle \underline.
                        // \underbar cuts through y, g, q, p etc.,
                        // \underline does not.
@@ -2414,7 +2447,16 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n\\bar default\n";
                }
 
-               else if (t.cs() == "emph" || t.cs() == "noun") {
+               else if (t.cs() == "sout") {
+                       context.check_layout(os);
+                       os << "\n\\strikeout on\n";
+                       parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+                       context.check_layout(os);
+                       os << "\n\\strikeout default\n";
+               }
+
+               else if (t.cs() == "uuline" || t.cs() == "uwave" ||
+                        t.cs() == "emph" || t.cs() == "noun") {
                        context.check_layout(os);
                        os << "\n\\" << t.cs() << " on\n";
                        parse_text_snippet(p, os, FLAG_ITEM, outer, context);
@@ -2422,9 +2464,64 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\n\\" << t.cs() << " default\n";
                }
 
+               // FIXME, the inset layout should be plain, not standard, see bug #7846
+               else if (t.cs() == "phantom" || t.cs() == "hphantom" ||
+                            t.cs() == "vphantom") {
+                       context.check_layout(os);
+                       if (t.cs() == "phantom")
+                               begin_inset(os, "Phantom Phantom\n");
+                       if (t.cs() == "hphantom")
+                               begin_inset(os, "Phantom Hhantom\n");
+                       if (t.cs() == "vphantom")
+                               begin_inset(os, "Phantom Vhantom\n");
+                       os << "status open\n";
+                       parse_text_in_inset(p, os, FLAG_ITEM, outer, context);
+                       end_inset(os);
+               }
+               
                else if (t.cs() == "lyxline") {
+                       // swallow size argument (it is not used anyway)
+                       p.getArg('{', '}');
+                       if (!context.atParagraphStart()) {
+                               // so our line is in the middle of a paragraph
+                               // we need to add a new line, lest this line
+                               // follow the other content on that line and
+                               // run off the side of the page
+                               // FIXME: This may create an empty paragraph,
+                               //        but without that it would not be
+                               //        possible to set noindent below.
+                               //        Fortunately LaTeX does not care
+                               //        about the empty paragraph.
+                               context.new_paragraph(os);
+                       }
+                       if (h_paragraph_separation == "indent") {
+                               // we need to unindent, lest the line be too long
+                               context.add_par_extra_stuff("\\noindent\n");
+                       }
                        context.check_layout(os);
-                       os << "\\lyxline";
+                       begin_command_inset(os, "line", "rule");
+                       os << "offset \"0.5ex\"\n"
+                             "width \"100line%\"\n"
+                             "height \"1pt\"\n";
+                       end_inset(os);
+               }
+
+               else if (t.cs() == "rule") {
+                       string offset = "";
+                       string width;
+                       string thickness;
+                       if (p.hasOpt())
+                               offset = p.getArg('[', ']');
+                       width = p.getArg('{', '}');
+                       thickness = p.getArg('{', '}');
+                                       
+                       context.check_layout(os);
+                       begin_command_inset(os, "line", "rule");
+                       if (!offset.empty())
+                               os << "offset \"" << translate_len(offset) << "\"\n";
+                       os << "width \"" << translate_len(width) << "\"\n"
+                                 "height \"" << translate_len(thickness) << "\"\n";
+                       end_inset(os);
                }
 
                else if (is_known(t.cs(), known_phrases) ||
@@ -2445,7 +2542,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        string const opt = p.getOpt();
                        if (opt.empty()) {
                                context.check_layout(os);
-                               begin_command_inset(os, "ref", t.cs());
+                               char const * const * where = is_known(t.cs(),
+                                       known_ref_commands);
+                               begin_command_inset(os, "ref",
+                                       known_coded_ref_commands[where - known_ref_commands]);
                                os << "reference \""
                                   << convert_command_inset_arg(p.verbatim_item())
                                   << "\"\n";
@@ -2621,14 +2721,32 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                }
 
                else if (t.cs() == "printnomenclature") {
+                       string width = "";
+                       string width_type = "";
                        context.check_layout(os);
                        begin_command_inset(os, "nomencl_print", "printnomenclature");
+                       // case of a custom width
+                       if (p.hasOpt()) {
+                               width = p.getArg('[', ']');
+                               width = translate_len(width);
+                               width_type = "custom";
+                       }
+                       // case of no custom width
+                       // the case of no custom width but the width set
+                       // via \settowidth{\nomlabelwidth}{***} cannot be supported
+                       // because the user could have set anything, not only the width
+                       // of the longest label (which would be width_type = "auto")
+                       string label = convert_command_inset_arg(p.getArg('{', '}'));
+                       if (label.empty() && width_type.empty())
+                               width_type = "none";
+                       os << "set_width \"" << width_type << "\"\n";
+                       if (width_type == "custom")
+                               os << "width \"" << width << '\"';
                        end_inset(os);
                        skip_spaces_braces(p);
                }
 
-               else if (LYX_FORMAT >= 408 &&
-                        (t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
+               else if ((t.cs() == "textsuperscript" || t.cs() == "textsubscript")) {
                        context.check_layout(os);
                        begin_inset(os, "script ");
                        os << t.cs().substr(4) << '\n';