]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/table.C
add config.h
[lyx.git] / src / tex2lyx / table.C
index 10d0bf7eb211ab81c13f4ed83c4742d0d1105ef7..cff95f4b1f70104cd980489393d6b47c4cd4c861 100644 (file)
 #include "support/lstrings.h"
 
 #include <cctype>
-#include <fstream>
 #include <iostream>
 #include <sstream>
 #include <vector>
 #include <map>
 
+
+namespace lyx {
+
 using std::cerr;
 using std::endl;
 using std::istringstream;
@@ -78,7 +80,7 @@ enum LTRowType
 class RowInfo {
 public:
        RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
-                   newpage(false) {}
+                   newpage(false) {}
        /// horizontal line above
        bool topline;
        /// horizontal line below
@@ -105,8 +107,8 @@ enum Multicolumn {
 class CellInfo {
 public:
        CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
-                    leftlines(0), rightlines(0), topline(false),
-                    bottomline(false), rotate(false) {}
+                    leftlines(0), rightlines(0), topline(false),
+                    bottomline(false), rotate(false) {}
        /// cell content
        string content;
        /// multicolumn flag
@@ -194,10 +196,10 @@ string const write_attribute(string const & name, string const & s)
  gets "translated" to:
 
 \verbatim
-         HLINE 1                     TAB 2 TAB 3 HLINE          HLINE LINE
+        HLINE 1                     TAB 2 TAB 3 HLINE          HLINE LINE
   \hline HLINE \multicolumn{2}{c}{4} TAB 5       HLINE          HLINE LINE
-         HLINE 6                     TAB 7       HLINE          HLINE LINE
-         HLINE 8                                 HLINE \endhead HLINE LINE
+        HLINE 6                     TAB 7       HLINE          HLINE LINE
+        HLINE 8                                 HLINE \endhead HLINE LINE
 \endverbatim
  */
 
@@ -260,11 +262,11 @@ void ci2special(ColInfo & ci)
  * in an intermediate form. fix_colalign() makes it suitable for LyX output.
  */
 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
-                     ColInfo const & start)
+                    ColInfo const & start)
 {
        if (p.get_token().cat() != catBegin)
                cerr << "Wrong syntax for table column alignment.\n"
-                       "Expected '{', got '" << p.curr_token().asInput()
+                       "Expected '{', got '" << p.curr_token().asInput()
                     << "'.\n";
 
        ColInfo next = start;
@@ -369,7 +371,7 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                                        next = ColInfo();
                                } else {
                                        cerr << "Ignoring column specification"
-                                               " '*{" << num << "}{"
+                                               " '*{" << num << "}{"
                                             << arg << "}'." << endl;
                                }
                                break;
@@ -397,7 +399,7 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
                                        next = ColInfo();
                                } else
                                        cerr << "Ignoring column specification"
-                                               " '" << t << "'." << endl;
+                                               " '" << t << "'." << endl;
                                break;
                        }
        }
@@ -468,7 +470,7 @@ void fix_colalign(vector<ColInfo> & colinfo)
  * \returns wether the token \p t was parsed
  */
 bool parse_hlines(Parser & p, Token const & t, string & hlines,
-                  bool is_long_tabular)
+                 bool is_long_tabular)
 {
        BOOST_ASSERT(t.cat() == catEscape);
 
@@ -509,7 +511,7 @@ enum RowPosition {
  * content. The cell content is parsed in a second step in handle_tabular().
  */
 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
-                 RowPosition & pos, unsigned flags)
+                RowPosition & pos, unsigned flags)
 {
        // table structure commands such as \hline
        string hlines;
@@ -539,7 +541,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                                        comments += t.asInput();
                                continue;
                        } else if (t.cat() == catSpace ||
-                                  t.cat() == catNewline) {
+                                  t.cat() == catNewline) {
                                // whitespace is irrelevant here, we
                                // need to recognize hline stuff
                                p.skip_spaces();
@@ -570,11 +572,11 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        }
 
                        else if (t.cs() == "tabularnewline" ||
-                                t.cs() == "\\" ||
-                                t.cs() == "cr") {
+                                t.cs() == "\\" ||
+                                t.cs() == "cr") {
                                if (t.cs() == "cr")
                                        cerr << "Warning: Converting TeX "
-                                               "'\\cr' to LaTeX '\\\\'."
+                                               "'\\cr' to LaTeX '\\\\'."
                                             << endl;
                                // stuff before the line break
                                os << comments << HLINE << hlines << HLINE
@@ -587,10 +589,10 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        }
 
                        else if (is_long_tabular &&
-                                (t.cs() == "endhead" ||
-                                 t.cs() == "endfirsthead" ||
-                                 t.cs() == "endfoot" ||
-                                 t.cs() == "endlastfoot")) {
+                                (t.cs() == "endhead" ||
+                                 t.cs() == "endfirsthead" ||
+                                 t.cs() == "endfoot" ||
+                                 t.cs() == "endlastfoot")) {
                                hlines += t.asInput();
                                switch (pos) {
                                case IN_COLUMNS:
@@ -653,6 +655,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        if (n.cat() == catMath) {
                                // TeX's $$...$$ syntax for displayed math
                                os << "\\[";
+                               // This does only work because parse_math outputs TeX
                                parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
                                os << "\\]";
                                p.get_token(); // skip the second '$' token
@@ -660,6 +663,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                                // simple $...$  stuff
                                p.putback();
                                os << '$';
+                               // This does only work because parse_math outputs TeX
                                parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
                                os << '$';
                        }
@@ -679,7 +683,7 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                else if (t.cat() == catBegin) {
                        os << '{';
                        parse_table(p, os, is_long_tabular, pos,
-                                   FLAG_BRACE_LAST);
+                                   FLAG_BRACE_LAST);
                        os << '}';
                }
 
@@ -699,12 +703,14 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
 
                else if (t.cs() == "(") {
                        os << "\\(";
+                       // This does only work because parse_math outputs TeX
                        parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
                        os << "\\)";
                }
 
                else if (t.cs() == "[") {
                        os << "\\[";
+                       // This does only work because parse_math outputs TeX
                        parse_math(p, os, FLAG_EQUATION, MATH_MODE);
                        os << "\\]";
                }
@@ -713,12 +719,10 @@ void parse_table(Parser & p, ostream & os, bool is_long_tabular,
                        string const name = p.getArg('{', '}');
                        active_environments.push_back(name);
                        os << "\\begin{" << name << '}';
-                       if (is_math_env(name)) {
-                               parse_math(p, os, FLAG_END, MATH_MODE);
-                       } else {
-                               parse_table(p, os, is_long_tabular, pos,
-                                           FLAG_END);
-                       }
+                       // treat the nested environment as a block, don't
+                       // parse &, \\ etc, because they don't belong to our
+                       // table if they appear.
+                       os << p.verbatimEnvironment(name);
                        os << "\\end{" << name << '}';
                        active_environments.pop_back();
                }
@@ -859,28 +863,28 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                                        size_t from = convert<unsigned int>(t[0]);
                                        if (from == 0)
                                                cerr << "Could not parse "
-                                                       "cline start column."
+                                                       "cline start column."
                                                     << endl;
                                        else
                                                // 1 based index -> 0 based
                                                --from;
                                        if (from >= colinfo.size()) {
                                                cerr << "cline starts at non "
-                                                       "existing column "
+                                                       "existing column "
                                                     << (from + 1) << endl;
                                                from = colinfo.size() - 1;
                                        }
                                        size_t to = convert<unsigned int>(t[1]);
                                        if (to == 0)
                                                cerr << "Could not parse "
-                                                       "cline end column."
+                                                       "cline end column."
                                                     << endl;
                                        else
                                                // 1 based index -> 0 based
                                                --to;
                                        if (to >= colinfo.size()) {
                                                cerr << "cline ends at non "
-                                                       "existing column "
+                                                       "existing column "
                                                     << (to + 1) << endl;
                                                to = colinfo.size() - 1;
                                        }
@@ -933,8 +937,8 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                                                else
                                                        // This does not work in LaTeX
                                                        cerr << "Ignoring "
-                                                               "'\\newpage' "
-                                                               "before rows."
+                                                               "'\\newpage' "
+                                                               "before rows."
                                                             << endl;
                                        } else
                                                rowinfo[row].newpage = true;
@@ -991,7 +995,7 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                                        cerr << "Moving cell content '"
                                             << cells[cell]
                                             << "' into a multicolumn cell. "
-                                               "This will probably not work."
+                                               "This will probably not work."
                                             << endl;
                                }
                                cellinfo[row][col].content += os.str();
@@ -1057,7 +1061,7 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                   << verbose_valign(colinfo[col].valign) << "\""
                   << write_attribute("leftline", colinfo[col].leftlines > 0)
                   << write_attribute("rightline", colinfo[col].rightlines > 0)
-                  << write_attribute("width", colinfo[col].width)
+                  << write_attribute("width", translate_len(colinfo[col].width))
                   << write_attribute("special", colinfo[col].special)
                   << ">\n";
        }
@@ -1068,13 +1072,13 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                   << write_attribute("topline", rowinfo[row].topline)
                   << write_attribute("bottomline", rowinfo[row].bottomline)
                   << write_attribute("endhead",
-                                     rowinfo[row].type == LT_HEAD)
+                                     rowinfo[row].type == LT_HEAD)
                   << write_attribute("endfirsthead",
-                                     rowinfo[row].type == LT_FIRSTHEAD)
+                                     rowinfo[row].type == LT_FIRSTHEAD)
                   << write_attribute("endfoot",
-                                     rowinfo[row].type == LT_FOOT)
+                                     rowinfo[row].type == LT_FOOT)
                   << write_attribute("endlastfoot",
-                                     rowinfo[row].type == LT_LASTFOOT)
+                                     rowinfo[row].type == LT_LASTFOOT)
                   << write_attribute("newpage", rowinfo[row].newpage)
                   << ">\n";
                for (size_t col = 0; col < colinfo.size(); ++col) {
@@ -1097,7 +1101,7 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
                        //if (cell.bottomline)
                        //      cerr << " bottomline=\"true\"";
                        os << " usebox=\"none\""
-                          << write_attribute("width", cell.width);
+                          << write_attribute("width", translate_len(cell.width));
                        if (cell.multi != CELL_NORMAL)
                                os << write_attribute("special", cell.special);
                        os << ">"
@@ -1116,3 +1120,6 @@ void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
 
 
 // }])
+
+
+} // namespace lyx