]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/text.cpp
tex2lyx/text.cpp: only whitespace
[lyx.git] / src / tex2lyx / text.cpp
index 6b3d724408de83c00b940c923c619223f9f4735d..3e1594595deff7d4bf58e042528c8cb3745bc301 100644 (file)
@@ -21,7 +21,7 @@
 #include "Layout.h"
 #include "Length.h"
 
-#include "support/assert.h"
+#include "support/lassert.h"
 #include "support/convert.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
@@ -56,8 +56,8 @@ void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
                Context & context)
 {
        Context newcontext(context);
-       // Don't inherit the extra stuff
-       newcontext.extra_stuff.clear();
+       // Don't inherit the paragraph-level extra stuff
+       newcontext.par_extra_stuff.clear();
        parse_text(p, os, flags, outer, newcontext);
        // Make sure that we don't create invalid .lyx files
        context.need_layout = newcontext.need_layout;
@@ -82,14 +82,14 @@ string parse_text_snippet(Parser & p, unsigned flags, const bool outer,
        newcontext.need_end_layout = false;
        newcontext.new_layout_allowed = false;
        // Avoid warning by Context::~Context()
-       newcontext.extra_stuff.clear();
+       newcontext.par_extra_stuff.clear();
        ostringstream os;
        parse_text_snippet(p, os, flags, outer, newcontext);
        return os.str();
 }
 
 
-char const * const known_latex_commands[] = { "ref", "cite", "nocite", "label",
+char const * const known_latex_commands[] = { "ref", "cite", "label",
  "index", "printindex", "pageref", "url", "vref", "vpageref", "prettyref",
  "eqref", 0 };
 
@@ -133,7 +133,7 @@ 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};
 
@@ -809,7 +809,10 @@ void parse_environment(Parser & p, ostream & os, bool outer,
        // 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") {
+       else if (name == "center" || name == "centering" ||
+                name == "flushleft" || name == "flushright" ||
+                name == "singlespace" || name == "onehalfspace" ||
+                name == "doublespace" || name == "spacing") {
                eat_whitespace(p, os, parent_context, false);
                // We must begin a new paragraph if not already done
                if (! parent_context.atParagraphStart()) {
@@ -820,8 +823,16 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                        parent_context.add_extra_stuff("\\align left\n");
                else if (name == "flushright")
                        parent_context.add_extra_stuff("\\align right\n");
-               else if (name == "center")
+               else if (name == "center" || name == "centering")
                        parent_context.add_extra_stuff("\\align center\n");
+               else if (name == "singlespace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing single\n");
+               else if (name == "onehalfspace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing onehalf\n");
+               else if (name == "doublespace")
+                       parent_context.add_extra_stuff("\\paragraph_spacing double\n");
+               else if (name == "spacing")
+                       parent_context.add_extra_stuff("\\paragraph_spacing other " + p.verbatim_item() + "\n");
                parse_text(p, os, FLAG_END, outer, parent_context);
                // Just in case the environment is empty
                parent_context.extra_stuff.erase();
@@ -845,8 +856,8 @@ void parse_environment(Parser & p, ostream & os, bool outer,
                parent_context.check_end_layout(os);
                switch (context.layout->latextype) {
                case  LATEX_LIST_ENVIRONMENT:
-                       context.extra_stuff = "\\labelwidthstring "
-                               + p.verbatim_item() + '\n';
+                       context.add_par_extra_stuff("\\labelwidthstring "
+                                                   + p.verbatim_item() + '\n');
                        p.skip_spaces();
                        break;
                case  LATEX_BIB_ENVIRONMENT:
@@ -1458,12 +1469,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        os << "\\bibitem ";
                        os << p.getOpt();
                        os << '{' << p.verbatim_item() << '}' << "\n";
-               } 
-               
-               else if(t.cs() == "global") {
-                       // skip global which can appear in front of e.g. "def"
                }
-               
+
                else if (t.cs() == "def") {
                        context.check_layout(os);
                        eat_whitespace(p, os, context, false);
@@ -1510,11 +1517,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                else if (t.cs() == "noindent") {
                        p.skip_spaces();
-                       context.add_extra_stuff("\\noindent\n");
+                       context.add_par_extra_stuff("\\noindent\n");
                }
 
                else if (t.cs() == "appendix") {
-                       context.add_extra_stuff("\\start_of_appendix\n");
+                       context.add_par_extra_stuff("\\start_of_appendix\n");
                        // We need to start a new paragraph. Otherwise the
                        // appendix in 'bla\appendix\chapter{' would start
                        // too late.
@@ -1536,22 +1543,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();
                }
 
@@ -2278,32 +2307,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        skip_braces(p); // eat {}
                }
 
-               else if (t.cs() == "href") {
-                       context.check_layout(os);
-                       begin_inset(os, "CommandInset ");
-                       os << t.cs() << "\n";
-                       os << "LatexCommand " << t.cs() << "\n";
-                       bool erase = false;
-                       size_t pos;
-                       // the first argument is "type:target", "type:" is optional
-                       // the second argument the name
-                       string href_target = subst(p.verbatim_item(), "\n", " ");
-                       string href_name = subst(p.verbatim_item(), "\n", " ");
-                       string href_type;
-                       // serach for the ":" to divide type from target
-                       if ((pos = href_target.find(":", 0)) != string::npos){
-                               href_type = href_target;
-                               href_type.erase(pos + 1, href_type.length());
-                               href_target.erase(0, pos + 1);
-                           erase = true;                                                                                       
-                       }
-                       os << "name " << '"' << href_name << '"' << "\n";
-                       os << "target " << '"' << href_target << '"' << "\n";
-                       if(erase)
-                               os << "type " << '"' << href_type << '"' << "\n";
-                       end_inset(os);
-               }
-
                else if (t.cs() == "input" || t.cs() == "include"
                         || t.cs() == "verbatiminput") {
                        string name = '\\' + t.cs();
@@ -2430,8 +2433,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 
                else if (t.cs() == "newcommand" ||
                         t.cs() == "providecommand" ||
-                        t.cs() == "renewcommand" ||
-                        t.cs() == "newlyxcommand") {
+                        t.cs() == "renewcommand") {
                        // these could be handled by parse_command(), but
                        // we need to call add_known_command() here.
                        string name = t.asInput();
@@ -2442,18 +2444,11 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        }
                        string const command = p.verbatim_item();
                        string const opt1 = p.getOpt();
-                       string optionals;
-                       unsigned optionalsNum = 0;
-                       while (true) {
-                               string const opt = p.getFullOpt();
-                               if (opt.empty())
-                                       break;
-                               optionalsNum++;
-                               optionals += opt;
-                       }
-                       add_known_command(command, opt1, optionalsNum);
-                       string const ert = name + '{' + command + '}' + opt1
-                               + optionals + '{' + p.verbatim_item() + '}';
+                       string const opt2 = p.getFullOpt();
+                       add_known_command(command, opt1, !opt2.empty());
+                       string const ert = name + '{' + command + '}' +
+                                          opt1 + opt2 +
+                                          '{' + p.verbatim_item() + '}';
 
                        context.check_layout(os);
                        begin_inset(os, "FormulaMacro");
@@ -2461,103 +2456,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                        end_inset(os);
                }
                
-               else if (t.cs() == "newcommandx" ||
-                        t.cs() == "renewcommandx") {
-                       // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
-
-                       // get command name
-                       string command;
-                       if (p.next_token().cat() == catBegin)
-                               command = p.verbatim_item();
-                       else 
-                               command = "\\" + p.get_token().cs();
-                       
-                       // get arity, we do not check that it fits to the given
-                       // optional parameters here.
-                       string const opt1 = p.getOpt();
-                       
-                       // get options and default values for optional parameters
-                       std::vector<string> optionalValues;
-                       int optionalsNum = 0;
-                       if (p.next_token().character() == '[') {
-                               // skip '['
-                               p.get_token();
-                               
-                               // handle 'opt=value' options, separated by ','.
-                               eat_whitespace(p, os, context, false);
-                               while (p.next_token().character() != ']' && p.good()) {
-                                       char_type nextc = p.next_token().character();
-                                       if (nextc >= '1' && nextc <= '9') {
-                                               // optional value -> get parameter number
-                                               int n = p.getChar() - '0';
-
-                                               // skip '='
-                                               if (p.next_token().character() != '=') {
-                                                       cerr << "'=' expected after numeral option of \\newcommandx" << std::endl;
-                                                       // try to find ] or ,
-                                                       while (p.next_token().character() != ','
-                                                              && p.next_token().character() != ']')
-                                                               p.get_token();
-                                                       continue;
-                                               } else
-                                                       p.get_token();
-                                               
-                                               // get value
-                                               optionalValues.resize(max(size_t(n), optionalValues.size()));
-                                               optionalValues[n - 1].clear();
-                                               while (p.next_token().character() != ']'
-                                                      && p.next_token().character() != ',')
-                                                       optionalValues[n - 1] += p.verbatim_item();
-                                               optionalsNum = max(n, optionalsNum);
-                                       } else if (p.next_token().cat() == catLetter) {
-                                               // we in fact ignore every non-optional
-                                               // parameters
-                                               
-                                               // get option name
-                                               docstring opt;
-                                               while (p.next_token().cat() == catLetter)
-                                                       opt += p.getChar();
-                                               
-                                               // value?
-                                               eat_whitespace(p, os, context, false);
-                                               if (p.next_token().character() == '=') {
-                                                       p.get_token();
-                                                       while (p.next_token().character() != ']'
-                                                              && p.next_token().character() != ',')
-                                                               p.verbatim_item();
-                                               }
-                                       } else
-                                               return;
-                                       
-                                       // skip komma
-                                       eat_whitespace(p, os, context, false);
-                                       if (p.next_token().character() == ',') {
-                                               p.getChar();
-                                               eat_whitespace(p, os, context, false);
-                                       } else if (p.next_token().character() != ']')
-                                               continue;
-                               }
-                               
-                               // skip ']'
-                               p.get_token();
-                       }
-                       
-                       // concat the default values to the optionals string
-                       string optionals;
-                       for (unsigned i = 0; i < optionalValues.size(); ++i)
-                               optionals += "[" + optionalValues[i] + "]";
-                       
-                       // register and output command
-                       add_known_command(command, opt1, optionalsNum);
-                       string const ert = "\\newcommand{" + command + '}' + opt1
-                       + optionals + '{' + p.verbatim_item() + '}';
-                       
-                       context.check_layout(os);
-                       begin_inset(os, "FormulaMacro");
-                       os << "\n" << ert;
-                       end_inset(os);
-               }
-
                else if (t.cs() == "vspace") {
                        bool starred = false;
                        if (p.next_token().asInput() == "*") {
@@ -2610,6 +2508,7 @@ 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);
@@ -2637,79 +2536,6 @@ 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