]> git.lyx.org Git - lyx.git/blobdiff - src/tabular.C
mathed31.diff
[lyx.git] / src / tabular.C
index cacf6279912d1b12cbd4ed4935e3190245e8a582..dd2b168ee997b566d19ca9153a50984a6c1fd13e 100644 (file)
@@ -92,7 +92,9 @@ LyXTabular::LyXTabular(InsetTabular * inset, LyXTabular const & lt)
 {
     owner_ = inset;
     Init(lt.rows_, lt.columns_);
+#ifdef WITH_WARNINGS
 #warning Jürgen, can you make it the other way round. So that copy assignment depends on the copy constructor and not the other way. (Lgb)
+#endif
     operator=(lt);
 }
 
@@ -135,8 +137,8 @@ LyXTabular * LyXTabular::Clone(InsetTabular * inset)
     LyXTabular * result = new LyXTabular(inset, *this);
     // don't know if this is good but I need to Clone also
     // the text-insets here, this is for the Undo-facility!
-    for(int i = 0; i < rows_; ++i) {
-       for(int j = 0; j < columns_; ++j) {
+    for (int i = 0; i < rows_; ++i) {
+       for (int j = 0; j < columns_; ++j) {
            result->cell_info[i][j].inset = cell_info[i][j].inset;
            result->cell_info[i][j].inset.setOwner(inset);
        }
@@ -193,6 +195,8 @@ void LyXTabular::AppendRow(int cell)
 
     row_vector::iterator rit = row_info.begin() + row;
     row_info.insert(rit, rowstruct());
+    // now set the values of the row before
+    row_info[row] = row_info[row+1];
 
 #if 0
     cell_vvector::iterator cit = cell_info.begin() + row;
@@ -201,13 +205,13 @@ void LyXTabular::AppendRow(int cell)
     cell_vvector c_info = cell_vvector(rows_, cell_vector(columns_,
                                                          cellstruct()));
 
-    for(int i = 0; i <= row; ++i) {
-       for(int j = 0; j < columns_; ++j) {
+    for (int i = 0; i <= row; ++i) {
+       for (int j = 0; j < columns_; ++j) {
            c_info[i][j] = cell_info[i][j];
        }
     }
-    for(int i = row + 1; i < rows_; ++i) {
-       for(int j = 0; j < columns_; ++j) {
+    for (int i = row + 1; i < rows_; ++i) {
+       for (int j = 0; j < columns_; ++j) {
            c_info[i][j] = cell_info[i-1][j];
        }
     }
@@ -244,6 +248,8 @@ void LyXTabular::AppendColumn(int cell)
     int const column = column_of_cell(cell);
     column_vector::iterator cit = column_info.begin() + column + 1;
     column_info.insert(cit, columnstruct());
+    // set the column values of the column before
+    column_info[column+1] = column_info[column];
 
     for (int i = 0; i < rows_; ++i) {
         for (int j = 0; j <= column; ++j) {
@@ -307,10 +313,7 @@ void LyXTabular::Reinit()
 
 void LyXTabular::set_row_column_number_info(bool oldformat)
 {
-    //int c = 0;
-    //int column = 0;
     numberofcells = -1;
-    //int row = 0;
     for (int row = 0; row < rows_; ++row) {
        for (int column = 0; column<columns_; ++column) {
            if (cell_info[row][column].multicolumn
@@ -324,27 +327,6 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
     rowofcell.resize(numberofcells);
     columnofcell.resize(numberofcells);
 
-#if 0
-    int row = 0;
-    int column = 0;
-    int c = 0;
-    
-    while (c < numberofcells && row < rows_ && column < columns_) {
-       rowofcell[c] = row;
-       columnofcell[c] = column;
-       ++c;
-       do {
-           ++column;
-       } while (column < columns_ &&
-                cell_info[row][column].multicolumn
-                == LyXTabular::CELL_PART_OF_MULTICOLUMN);
-       if (column == columns_) {
-           column = 0;
-           ++row;
-       }
-    }
-#else
-    // Isn't this the same as the while above? (Lgb)
     for (int row = 0, column = 0, c = 0;
         c < numberofcells && row < rows_ && column < columns_;) {
        rowofcell[c] = row;
@@ -360,7 +342,6 @@ void LyXTabular::set_row_column_number_info(bool oldformat)
            ++row;
        }
     }
-#endif
 
     for (int row = 0; row < rows_; ++row) {
        for (int column = 0; column < columns_; ++column) {
@@ -415,7 +396,7 @@ bool LyXTabular::BottomLine(int cell, bool onlycolumn) const
     // no bottom line underneath non-existent cells if you please
     // Isn't that a programming error? Is so this should
     // be an Assert instead. (Lgb)
-    if(cell >= numberofcells)
+    if (cell >= numberofcells)
        return false;
 
     if (!onlycolumn && IsMultiColumn(cell))
@@ -586,7 +567,7 @@ void LyXTabular::recalculateMulticolCells(int cell, int new_width)
     int i = column1;
     for (; i <= column2; ++i)
         cell_info[row][i].width_of_cell = 0;
-    for(i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
+    for (i = cell + 1; (i < numberofcells) && (!IsMultiColumn(i)); ++i)
         ;
     if (i < numberofcells)
         recalculateMulticolCells(i, GetWidthOfCell(i) - (2 * WIDTH_OF_LINE));
@@ -616,13 +597,13 @@ bool LyXTabular::SetWidthOfCell(int cell, int new_width)
     }
     if (tmp) {
         int i = 0;
-        for(; i<columns_; ++i)
+        for (; i<columns_; ++i)
             calculate_width_of_column_NMC(i);
-        for(i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
+        for (i = 0; (i < numberofcells) && !IsMultiColumn(i); ++i)
             ;
         if (i < numberofcells)
             recalculateMulticolCells(i, GetWidthOfCell(i)-(2 * WIDTH_OF_LINE));
-        for(i = 0; i < columns_; ++i)
+        for (i = 0; i < columns_; ++i)
             calculate_width_of_column(i);
         calculate_width_of_tabular();
         return true;
@@ -659,7 +640,7 @@ bool LyXTabular::SetColumnPWidth(int cell, string const & width)
     column_info[j].p_width = width;
     if (flag) // do this only if there is a width
        SetAlignment(cell, LYX_ALIGN_LEFT);
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        int c = GetCellNumber(i, j);
        flag = !GetPWidth(c).empty(); // because of multicolumns!
        GetCellInset(c)->SetAutoBreakRows(flag);
@@ -960,86 +941,223 @@ string const write_attribute(string const & name, string const & value)
 
 string const write_attribute(string const & name, bool value)
 {
-    string str = " " + name + "=\"" + tostr((int)value) + "\"";
+    string str = " " + name + "=\"" + tostr(static_cast<int>(value)) + "\"";
     return str;
 }
 #endif
 
 
+template<>
+inline
+string const tostr(LyXAlignment const & num)
+{
+    switch(num) {
+    case LYX_ALIGN_NONE:
+       return "none";
+    case LYX_ALIGN_BLOCK:
+       return "block";
+    case LYX_ALIGN_LEFT:
+       return "left";
+    case LYX_ALIGN_CENTER:
+       return "center";
+    case LYX_ALIGN_RIGHT:
+       return "right";
+    case LYX_ALIGN_LAYOUT:
+       return "layout";
+    case LYX_ALIGN_SPECIAL:
+       return "special";
+    }
+    return string();
+}
+
+
+template<>
+inline
+string const tostr(LyXTabular::VAlignment const & num)
+{
+    switch(num) {
+    case LyXTabular::LYX_VALIGN_TOP:
+       return "top";
+    case LyXTabular::LYX_VALIGN_CENTER:
+       return "center";
+    case LyXTabular::LYX_VALIGN_BOTTOM:
+       return "bottom";
+    }
+    return string();
+}
+
+
+template<>
+inline
+string const tostr(LyXTabular::BoxType const & num)
+{
+    switch(num) {
+    case LyXTabular::BOX_NONE:
+       return "none";
+    case LyXTabular::BOX_PARBOX:
+       return "parbox";
+    case LyXTabular::BOX_MINIPAGE:
+       return "minipage";
+    }
+    return string();
+}
+
+
 void LyXTabular::Write(Buffer const * buf, ostream & os) const
 {
     // header line
-    os << "<LyXTabular"
-       << write_attribute("version", 1)
+    os << "<lyxtabular"
+       << write_attribute("version", 2)
        << write_attribute("rows", rows_)
        << write_attribute("columns", columns_)
        << ">\n";
     // global longtable options
-    os << "<Features"
-       << write_attribute("rotate", rotate)
-       << write_attribute("islongtable", is_long_tabular)
+    os << "<features"
+       << write_attribute("rotate", tostr(rotate))
+       << write_attribute("islongtable", tostr(is_long_tabular))
        << write_attribute("endhead", endhead)
        << write_attribute("endfirsthead", endfirsthead)
        << write_attribute("endfoot", endfoot)
        << write_attribute("endlastfoot", endlastfoot)
-       << ">\n\n";
+       << ">\n";
+    for (int j = 0; j < columns_; ++j) {
+       os << "<column"
+          << write_attribute("alignment", tostr(column_info[j].alignment))
+          << write_attribute("valignment", tostr(column_info[j].valignment))
+          << write_attribute("leftline", tostr(column_info[j].left_line))
+          << write_attribute("rightline", tostr(column_info[j].right_line))
+          << write_attribute("width",
+                             VSpace(column_info[j].p_width)
+                             .asLyXCommand())
+          << write_attribute("special", column_info[j].align_special)
+          << ">\n";
+    }
     for (int i = 0; i < rows_; ++i) {
-       os << "<Row"
-          << write_attribute("topline", row_info[i].top_line)
-          << write_attribute("bottomline", row_info[i].bottom_line)
-          << write_attribute("newpage", row_info[i].newpage)
+       os << "<row"
+          << write_attribute("topline", tostr(row_info[i].top_line))
+          << write_attribute("bottomline", tostr(row_info[i].bottom_line))
+          << write_attribute("newpage", tostr(row_info[i].newpage))
           << ">\n";
        for (int j = 0; j < columns_; ++j) {
+#if 0
            if (!i) {
-               os << "<Column"
-                  << write_attribute("alignment", column_info[j].alignment)
-                  << write_attribute("valignment", column_info[j].valignment)
-                  << write_attribute("leftline", column_info[j].left_line)
-                  << write_attribute("rightline", column_info[j].right_line)
+               os << "<column"
+                  << write_attribute("alignment", tostr(column_info[j].alignment))
+                  << write_attribute("valignment", tostr(column_info[j].valignment))
+                  << write_attribute("leftline", tostr(column_info[j].left_line))
+                  << write_attribute("rightline", tostr(column_info[j].right_line))
                   << write_attribute("width",
                                      VSpace(column_info[j].p_width)
                                      .asLyXCommand())
                   << write_attribute("special", column_info[j].align_special)
                   << ">\n";
            } else {
-               os << "<Column>\n";
+               os << "<column>\n";
            }
-           os << "<Cell"
+#endif
+           os << "<cell"
               << write_attribute("multicolumn", cell_info[i][j].multicolumn)
-              << write_attribute("alignment", cell_info[i][j].alignment)
-              << write_attribute("valignment", cell_info[i][j].valignment)
-              << write_attribute("topline", cell_info[i][j].top_line)
-              << write_attribute("bottomline", cell_info[i][j].bottom_line)
-              << write_attribute("leftline", cell_info[i][j].left_line)
-              << write_attribute("rightline", cell_info[i][j].right_line)
-              << write_attribute("rotate", cell_info[i][j].rotate)
-              << write_attribute("usebox", cell_info[i][j].usebox)
+              << write_attribute("alignment", tostr(cell_info[i][j].alignment))
+              << write_attribute("valignment", tostr(cell_info[i][j].valignment))
+              << write_attribute("topline", tostr(cell_info[i][j].top_line))
+              << write_attribute("bottomline", tostr(cell_info[i][j].bottom_line))
+              << write_attribute("leftline", tostr(cell_info[i][j].left_line))
+              << write_attribute("rightline", tostr(cell_info[i][j].right_line))
+              << write_attribute("rotate", tostr(cell_info[i][j].rotate))
+              << write_attribute("usebox", tostr(cell_info[i][j].usebox))
               << write_attribute("width", cell_info[i][j].p_width)
               << write_attribute("special", cell_info[i][j].align_special)
               << ">\n";
            os << "\\begin_inset ";
            cell_info[i][j].inset.Write(buf, os);
            os << "\n\\end_inset \n"
-              << "</Cell>\n"
-              << "</Column>\n";
+              << "</cell>\n";
+#if 0
+              << "</column>\n";
+#endif
        }
-       os << "</Row>\n";
+       os << "</row>\n";
     }
-    os << "</LyXTabular>\n";
+    os << "</lyxtabular>\n";
+}
+
+
+// I would have liked a fromstr template a lot better. (Lgb)
+
+static inline
+bool string2type(string const str, LyXAlignment & num)
+{
+    if (str == "none")
+       num = LYX_ALIGN_NONE;
+    else if (str == "block")
+       num = LYX_ALIGN_BLOCK;
+    else if (str == "left")
+       num = LYX_ALIGN_LEFT;
+    else if (str == "center")
+       num = LYX_ALIGN_CENTER;
+    else if (str == "right")
+       num = LYX_ALIGN_RIGHT;
+    else
+       return false;
+    return true;
+}
+
+
+static inline
+bool string2type(string const str, LyXTabular::VAlignment & num)
+{
+    if (str == "top")
+       num = LyXTabular::LYX_VALIGN_TOP;
+    else if (str == "center")
+       num = LyXTabular::LYX_VALIGN_CENTER;
+    else if (str == "bottom")
+       num = LyXTabular::LYX_VALIGN_BOTTOM;
+    else
+       return false;
+    return true;
+}
+
+
+static inline
+bool string2type(string const str, LyXTabular::BoxType & num)
+{
+    if (str == "none")
+       num = LyXTabular::BOX_NONE;
+    else if (str == "parbox")
+       num = LyXTabular::BOX_PARBOX;
+    else if (str == "minipage")
+       num = LyXTabular::BOX_MINIPAGE;
+    else
+       return false;
+    return true;
+}
+
+
+static inline
+bool string2type(string const str, bool & num)
+{
+    if (str == "true")
+       num = true;
+    else if (str == "false")
+       num = false;
+    else
+       return false;
+    return true;
 }
 
 
 static
 bool getTokenValue(string const & str, const char * token, string & ret)
 {
+    size_t token_length = strlen(token);
     string::size_type pos = str.find(token);
-    char ch = str[pos + strlen(token)];
 
-    if ((pos == string::npos) || (ch != '='))
+    if (pos == string::npos || pos+token_length+1 >= str.length()
+       || str[pos+token_length] != '=')
        return false;
     ret.erase();
-    pos += strlen(token) + 1;
-    ch = str[pos];
+    pos += token_length + 1;
+    char ch = str[pos];
     if ((ch != '"') && (ch != '\'')) { // only read till next space
        ret += ch;
        ch = ' ';
@@ -1054,24 +1172,10 @@ bool getTokenValue(string const & str, const char * token, string & ret)
 static
 bool getTokenValue(string const & str, const char * token, int & num)
 {
-    string::size_type pos = str.find(token);
-    char ch = str[pos + strlen(token)];
-
-    if ((pos == string::npos) || (ch != '='))
+    string tmp;
+    if (!getTokenValue(str, token, tmp))
        return false;
-    string ret;
-    pos += strlen(token) + 1;
-    ch = str[pos];
-    if ((ch != '"') && (ch != '\'')) { // only read till next space
-       if (!isdigit(ch))
-           return false;
-       ret += ch;
-    }
-    ++pos;
-    while((pos < str.length() - 1) && isdigit(str[pos]))
-       ret += str[pos++];
-
-    num = strToInt(ret);
+    num = strToInt(tmp);
     return true;
 }
 
@@ -1079,10 +1183,10 @@ bool getTokenValue(string const & str, const char * token, int & num)
 static
 bool getTokenValue(string const & str, const char * token, LyXAlignment & num)
 {
-    int tmp;
-    bool const ret = getTokenValue(str, token, tmp);
-    num = static_cast<LyXAlignment>(tmp);
-    return ret;
+    string tmp;
+    if (!getTokenValue(str, token, tmp))
+       return false;
+    return string2type(tmp, num);
 }
 
 
@@ -1090,10 +1194,10 @@ static
 bool getTokenValue(string const & str, const char * token,
                   LyXTabular::VAlignment & num)
 {
-    int tmp;
-    bool const ret = getTokenValue(str, token, tmp);
-    num = static_cast<LyXTabular::VAlignment>(tmp);
-    return ret;
+    string tmp;
+    if (!getTokenValue(str, token, tmp))
+       return false;
+    return string2type(tmp, num);
 }
 
 
@@ -1101,36 +1205,21 @@ static
 bool getTokenValue(string const & str, const char * token,
                   LyXTabular::BoxType & num)
 {
-    int tmp;
-    bool ret = getTokenValue(str, token, tmp);
-    num = static_cast<LyXTabular::BoxType>(tmp);
-    return ret;
+    string tmp;
+    if (!getTokenValue(str, token, tmp))
+       return false;
+    return string2type(tmp, num);
 }
 
 
 static
 bool getTokenValue(string const & str, const char * token, bool & flag)
 {
-    string::size_type pos = str.find(token);
-    char ch = str[pos + strlen(token)];
-
-    if ((pos == string::npos) || (ch != '='))
+    string tmp;
+    if (!getTokenValue(str, token, tmp))
        return false;
-    string ret;
-    pos += strlen(token) + 1;
-    ch = str[pos];
-    if ((ch != '"') && (ch != '\'')) { // only read till next space
-       if (!isdigit(ch))
-           return false;
-       ret += ch;
-    }
-    ++pos;
-    while((pos < str.length() - 1) && isdigit(str[pos]))
-       ret += str[pos++];
-
-    flag = strToInt(ret);
-    return true;
-}
+    return string2type(tmp, flag);
+}    
 
 
 static inline
@@ -1144,41 +1233,69 @@ void l_getline(istream & is, string & str)
 
 void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
 {
-    string line;
-    istream & is = lex.getStream();
+       string line;
+       istream & is = lex.getStream();
 
-    l_getline(is, line);
-    if (!prefixIs(line, "<LyXTabular ")) {
-       OldFormatRead(lex, line);
-       return;
-    }
+       l_getline(is, line);
+       if (!prefixIs(line, "<lyxtabular ")
+           && !prefixIs(line, "<LyXTabular ")) {
+               OldFormatRead(lex, line);
+               return;
+       }
 
-    int version;
+       int version;
+       if (!getTokenValue(line, "version", version))
+               return;
+       if (version == 1)
+               ReadOld(buf, is, lex, line);
+       else if (version == 2)
+               ReadNew(buf, is, lex, line);
+}
+
+
+void LyXTabular::ReadNew(Buffer const * buf, istream & is,
+                        LyXLex & lex, string const & l)
+{
+    string line(l);
     int rows_arg;
-    int columns_arg;
-    if (!getTokenValue(line, "version", version))
-       return;
     if (!getTokenValue(line, "rows", rows_arg))
        return;
+    int columns_arg;
     if (!getTokenValue(line, "columns", columns_arg))
        return;
     Init(rows_arg, columns_arg);
     l_getline(is, line);
-    if (!prefixIs(line, "<Features ")) {
-       lyxerr << "Wrong tabular format (expected <Feture ...> got" <<
+    if (!prefixIs(line, "<features")) {
+       lyxerr << "Wrong tabular format (expected <features ...> got" <<
            line << ")" << endl;
        return;
     }
+    getTokenValue(line, "rotate", rotate);
     getTokenValue(line, "islongtable", is_long_tabular);
     getTokenValue(line, "endhead", endhead);
     getTokenValue(line, "endfirsthead", endfirsthead);
     getTokenValue(line, "endfoot", endfoot);
     getTokenValue(line, "endlastfoot", endlastfoot);
 
-    for(int i = 0; i < rows_; ++i) {
+    for (int j = 0; j < columns_; ++j) {
+       l_getline(is,line);
+       if (!prefixIs(line,"<column")) {
+           lyxerr << "Wrong tabular format (expected <column ...> got" <<
+               line << ")" << endl;
+           return;
+       }
+       getTokenValue(line, "alignment", column_info[j].alignment);
+       getTokenValue(line, "valignment", column_info[j].valignment);
+       getTokenValue(line, "leftline", column_info[j].left_line);
+       getTokenValue(line, "rightline", column_info[j].right_line);
+       getTokenValue(line, "width", column_info[j].p_width);
+       getTokenValue(line, "special", column_info[j].align_special);
+    }
+
+    for (int i = 0; i < rows_; ++i) {
        l_getline(is, line);
-       if (!prefixIs(line, "<Row ")) {
-           lyxerr << "Wrong tabular format (expected <Row ...> got" <<
+       if (!prefixIs(line, "<row")) {
+           lyxerr << "Wrong tabular format (expected <row ...> got" <<
                line << ")" << endl;
            return;
        }
@@ -1186,23 +1303,9 @@ void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
        getTokenValue(line, "bottomline", row_info[i].bottom_line);
        getTokenValue(line, "newpage", row_info[i].newpage);
        for (int j = 0; j < columns_; ++j) {
-           l_getline(is,line);
-           if (!prefixIs(line,"<Column")) {
-               lyxerr << "Wrong tabular format (expected <Column ...> got" <<
-                   line << ")" << endl;
-               return;
-           }
-           if (!i) {
-               getTokenValue(line, "alignment", column_info[j].alignment);
-               getTokenValue(line, "valignment", column_info[j].valignment);
-               getTokenValue(line, "leftline", column_info[j].left_line);
-               getTokenValue(line, "rightline", column_info[j].right_line);
-               getTokenValue(line, "width", column_info[j].p_width);
-               getTokenValue(line, "special", column_info[j].align_special);
-           }
            l_getline(is, line);
-           if (!prefixIs(line, "<Cell")) {
-               lyxerr << "Wrong tabular format (expected <Cell ...> got" <<
+           if (!prefixIs(line, "<cell")) {
+               lyxerr << "Wrong tabular format (expected <cell ...> got" <<
                    line << ")" << endl;
                return;
            }
@@ -1222,26 +1325,20 @@ void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
                cell_info[i][j].inset.Read(buf, lex);
                l_getline(is, line);
            }
-           if (line != "</Cell>") {
-               lyxerr << "Wrong tabular format (expected </Cell> got" <<
-                   line << ")" << endl;
-               return;
-           }
-           l_getline(is, line);
-           if (line != "</Column>") {
-               lyxerr << "Wrong tabular format (expected </Column> got" <<
+           if (line != "</cell>") {
+               lyxerr << "Wrong tabular format (expected </cell> got" <<
                    line << ")" << endl;
                return;
            }
        }
        l_getline(is, line);
-       if (line != "</Row>") {
-           lyxerr << "Wrong tabular format (expected </Row> got" <<
+       if (line != "</row>") {
+           lyxerr << "Wrong tabular format (expected </row> got" <<
                line << ")" << endl;
            return;
        }
     }
-    while (line != "</LyXTabular>") {
+    while (line != "</lyxtabular>") {
        l_getline(is, line);
     }
     set_row_column_number_info();
@@ -1251,7 +1348,8 @@ void LyXTabular::Read(Buffer const * buf, LyXLex & lex)
 void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
 {
     int version;
-    int i, j;
+    int i;
+    int j;
     int rows_arg = 0;
     int columns_arg = 0;
     int is_long_tabular_arg = false;
@@ -1263,7 +1361,6 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     int e = 0;
     int f = 0;
     int g = 0;
-    int h = 0;
        
     istream & is = lex.getStream();
     string s(fl);
@@ -1310,12 +1407,12 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
        cont_row_info = vector<int>(rows_arg);
        SetLongTabular(is_long_tabular_arg);
        SetRotateTabular(rotate_arg);
-       endhead = a;
-       endfirsthead = b;
-       endfoot = c;
-       endlastfoot = d;
+       endhead = a + 1;
+       endfirsthead = b + 1;
+       endfoot = c + 1;
+       endlastfoot = d + 1;
        for (i = 0; i < rows_; ++i) {
-           a = b = c = d = e = f = g = h = 0;
+           a = b = c = d = e = f = g = 0;
            is >> a >> b >> c >> d;
            row_info[i].top_line = a;
            row_info[i].bottom_line = b;
@@ -1391,7 +1488,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     string tmptok;
     int pos = 0;
     char depth = 0;
-    LyXFont font(LyXFont::ALL_SANE);
+    LyXFont font(LyXFont::ALL_INHERIT);
     font.setLanguage(owner_->BufferOwner()->GetLanguage());
 
     while (lex.IsOK()) {
@@ -1433,7 +1530,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
     InsetText * inset = GetCellInset(cell);
     int row;
 
-    for(int i = 0; i < par->Last(); ++i) {
+    for (int i = 0; i < par->Last(); ++i) {
        if (par->IsNewline(i)) {
            ++cell;
            if (cell > GetNumberOfCells()) {
@@ -1461,7 +1558,7 @@ void LyXTabular::OldFormatRead(LyXLex & lex, string const & fl)
                par->InsertChar(i, ' ');
            }
        }
-       par->CopyIntoMinibuffer(owner_->BufferOwner()->params, i);
+       par->CopyIntoMinibuffer(*owner_->BufferOwner(), i);
        inset->par->InsertFromMinibuffer(inset->par->Last());
     }
     delete par;
@@ -1947,7 +2044,7 @@ int LyXTabular::GetHeightOfTabular() const
 {
     int height = 0;
 
-    for(int row = 0; row < rows_; ++row)
+    for (int row = 0; row < rows_; ++row)
         height += GetAscentOfRow(row) + GetDescentOfRow(row) +
            GetAdditionalHeight(GetCellNumber(row, 0));
     return height;
@@ -1964,6 +2061,9 @@ bool LyXTabular::IsPartOfMultiColumn(int row, int column) const
 
 int LyXTabular::TeXTopHLine(ostream & os, int row) const
 {
+    if ((row < 0) || (row >= rows_))
+       return 0;
+
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
     int tmp = 0;
@@ -1994,6 +2094,9 @@ int LyXTabular::TeXTopHLine(ostream & os, int row) const
 
 int LyXTabular::TeXBottomHLine(ostream & os, int row) const
 {
+    if ((row < 0) || (row >= rows_))
+       return 0;
+
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
     int tmp = 0;
@@ -2032,13 +2135,13 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
     }
     if (IsMultiColumn(cell)) {
        os << "\\multicolumn{" << cells_in_multicolumn(cell) << "}{";
-       if (!cellinfo_of_cell(cell+1)->align_special.empty()) {
-           os << cellinfo_of_cell(cell+1)->align_special << "}{";
+       if (!cellinfo_of_cell(cell)->align_special.empty()) {
+           os << cellinfo_of_cell(cell)->align_special << "}{";
        } else {
            if (LeftLine(cell))
                os << '|';
            if (!GetPWidth(cell).empty()) {
-               switch(GetVAlignment(cell)) {
+               switch (GetVAlignment(cell)) {
                case LYX_VALIGN_TOP:
                    os << "p";
                    break;
@@ -2073,7 +2176,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
     }
     if (GetUsebox(cell) == BOX_PARBOX) {
        os << "\\parbox[";
-       switch(GetVAlignment(cell)) {
+       switch (GetVAlignment(cell)) {
        case LYX_VALIGN_TOP:
            os << "t";
            break;
@@ -2087,7 +2190,7 @@ int LyXTabular::TeXCellPreamble(ostream & os, int cell) const
        os << "]{" << GetPWidth(cell) << "}{";
     } else if (GetUsebox(cell) == BOX_MINIPAGE) {
        os << "\\begin{minipage}[";
-       switch(GetVAlignment(cell)) {
+       switch (GetVAlignment(cell)) {
        case LYX_VALIGN_TOP:
            os << "t";
            break;
@@ -2151,7 +2254,7 @@ int LyXTabular::Latex(Buffer const * buf,
        if (!column_info[i].align_special.empty()) {
            os << column_info[i].align_special;
        } else if (!column_info[i].p_width.empty()) {
-           switch(column_info[i].valignment) {
+           switch (column_info[i].valignment) {
            case LYX_VALIGN_TOP:
                os << "p";
                break;
@@ -2188,8 +2291,7 @@ int LyXTabular::Latex(Buffer const * buf,
     //+                      the single row and columns (cells)            +
     //+---------------------------------------------------------------------
 
-    //int bret;
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        ret += TeXTopHLine(os, i);
        int bret = ret;
        if (IsLongTabular()) {
@@ -2211,15 +2313,23 @@ int LyXTabular::Latex(Buffer const * buf,
            }
        }
        if (ret > bret) {
-           if (i > 0)
-               ret += TeXBottomHLine(os, i-1);
+           ret += TeXBottomHLine(os, i-1);
            ret += TeXTopHLine(os, i);
        }
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i,j))
                continue;
            ret += TeXCellPreamble(os, cell);
-           ret += GetCellInset(cell)->Latex(buf, os, fragile, fp);
+           InsetText * inset = GetCellInset(cell);
+
+           bool rtl = inset->par->isRightToLeftPar(buf->params) &&
+                   inset->par->Last() > 0 && GetPWidth(cell).empty();
+           if (rtl)
+               os << "\\R{";
+           ret += inset->Latex(buf, os, fragile, fp);
+           if (rtl)
+               os << "}";
+
            ret += TeXCellPostamble(os, cell);
            if (!IsLastCellInRow(cell)) { // not last cell in row
                os << "&\n";
@@ -2247,8 +2357,8 @@ int LyXTabular::Latex(Buffer const * buf,
                os << "\\endlastfoot\n";
                ++ret;
            }
-           if (ret > bret)
-               ret += TeXBottomHLine(os, i);
+//         if (ret > bret)
+//             ret += TeXBottomHLine(os, i);
            if (row_info[i].newpage) {
                os << "\\newpage\n";
                ++ret;
@@ -2307,14 +2417,14 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
 
     int cell = 0;
     os << "<tbody>\n";
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
         os << "<row>\n";
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i, j))
                continue;
            
            os << "<entry align=\"";
-           switch(GetAlignment(cell)) {
+           switch (GetAlignment(cell)) {
            case LYX_ALIGN_LEFT:
                os << "left";
                break;
@@ -2327,7 +2437,7 @@ int LyXTabular::DocBook(Buffer const * buf, ostream & os) const
            }
            
            os << "\" valign=\"";
-           switch(GetVAlignment(cell)) {
+           switch (GetVAlignment(cell)) {
            case LYX_VALIGN_TOP:
                os << "top";
                break;
@@ -2367,12 +2477,7 @@ static
 inline
 void print_n_chars(ostream & os, unsigned char ch, int n)
 {
-#if 0
-       for(int i = 0; i < n; ++i)
-               os << ch;
-#else
        os << string(n, ch);
-#endif
 }
 
 
@@ -2381,26 +2486,16 @@ int LyXTabular::AsciiTopHLine(ostream & os, int row,
 {
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
-    //int len;
-    //int column = 0;
-    //unsigned char ch;
-
-#if 0
     int tmp = 0;
 
     for (int i = fcell; i < n; ++i) {
-       if (TopLine(i))
+       if (TopLine(i)) {
            ++tmp;
+           break;
+       }
     }
     if (!tmp)
        return 0;
-#else
-    // Isn't this equivalent? (Lgb)
-    for (int i = fcell; i < n; ++i) {
-           if (TopLine(i))
-                   return 0;
-    }
-#endif
 
     unsigned char ch;
     for (int i = fcell; i < n; ++i) {
@@ -2438,25 +2533,17 @@ int LyXTabular::AsciiBottomHLine(ostream & os, int row,
 {
     int const fcell = GetFirstCellInRow(row);
     int const n = NumberOfCellsInRow(fcell) + fcell;
-    //int len;
-    //int column = 0;
-    //unsigned char ch;
-#if 0
     int tmp = 0;
 
     for (int i = fcell; i < n; ++i) {
-       if (BottomLine(i))
+       if (BottomLine(i)) {
            ++tmp;
+           break;
+       }
     }
     if (!tmp)
        return 0;
-#else
-    // Isn't this equivalent? (Lgb)
-    for (int i = fcell; i < n; ++i) {
-       if (BottomLine(i))
-           return 0;
-    }
-#endif
+
     unsigned char ch;
     for (int i = fcell; i < n; ++i) {
        if (BottomLine(i)) {
@@ -2506,7 +2593,7 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
        len2 += clen[column] + 4;
     len2 -= len1;
 
-    switch(GetAlignment(cell)) {
+    switch (GetAlignment(cell)) {
     default:
     case LYX_ALIGN_LEFT:
        len1 = 0;
@@ -2521,17 +2608,17 @@ int LyXTabular::AsciiPrintCell(Buffer const * buf, ostream & os,
        break;
     }
 
-    for(unsigned int i = 0; i < len1; ++i)
+    for (unsigned int i = 0; i < len1; ++i)
        os << " ";
     os << sstr.str();
-    for(unsigned int i = 0; i < len2; ++i)
+    for (unsigned int i = 0; i < len2; ++i)
        os << " ";
     if (RightLine(cell))
        os << " |";
     else
        os << "  ";
 
-    return ret * 0; // eh? (Lgb)
+    return ret;
 }
 
 
@@ -2545,9 +2632,9 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
     vector<unsigned int> clen(columns_);
 
     // first all non (real) multicolumn cells!
-    for(int j = 0; j < columns_; ++j) {
+    for (int j = 0; j < columns_; ++j) {
        clen[j] = 0;
-       for(int i = 0; i < rows_; ++i) {
+       for (int i = 0; i < rows_; ++i) {
            int cell = GetCellNumber(i, j);
            if (IsMultiColumn(cell, true))
                continue;
@@ -2558,8 +2645,8 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
        }
     }
     // then all (real) multicolumn cells!
-    for(int j = 0; j < columns_; ++j) {
-       for(int i = 0; i < rows_; ++i) {
+    for (int j = 0; j < columns_; ++j) {
+       for (int i = 0; i < rows_; ++i) {
            int cell = GetCellNumber(i, j);
            if (!IsMultiColumn(cell, true) || IsPartOfMultiColumn(i, j))
                continue;
@@ -2574,9 +2661,9 @@ int LyXTabular::Ascii(Buffer const * buf, ostream & os) const
        }
     }
     int cell = 0;
-    for(int i = 0; i < rows_; ++i) {
+    for (int i = 0; i < rows_; ++i) {
        AsciiTopHLine(os, i, clen);
-       for(int j = 0; j < columns_; ++j) {
+       for (int j = 0; j < columns_; ++j) {
            if (IsPartOfMultiColumn(i,j))
                continue;
            ret += AsciiPrintCell(buf, os, cell, i, j, clen);
@@ -2595,13 +2682,19 @@ InsetText * LyXTabular::GetCellInset(int cell) const
 }
 
 
+InsetText * LyXTabular::GetCellInset(int row, int column) const
+{
+    return GetCellInset(GetCellNumber(row, column));
+}
+
+
 void LyXTabular::Validate(LaTeXFeatures & features) const
 {
     if (IsLongTabular())
         features.longtable = true;
     if (NeedRotating())
        features.rotating = true;
-    for(int cell = 0; cell < numberofcells; ++cell) {
+    for (int cell = 0; cell < numberofcells; ++cell) {
        if (GetVAlignment(cell) != LYX_VALIGN_TOP)
            features.array = true;
        GetCellInset(cell)->Validate(features);
@@ -2613,8 +2706,8 @@ LyXTabular::BoxType LyXTabular::UseParbox(int cell) const
 {
     LyXParagraph * par = GetCellInset(cell)->par;
 
-    for(; par; par = par->next) {
-       for(int i = 0; i < par->Last(); ++i) {
+    for (; par; par = par->next) {
+       for (int i = 0; i < par->Last(); ++i) {
            if (par->GetChar(i) == LyXParagraph::META_NEWLINE)
                return BOX_PARBOX;
        }