]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/table.cpp
Correct indentation
[lyx.git] / src / tex2lyx / table.cpp
1 /**
2  * \file table.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Jean-Marc Lasgouttes
8  * \author Georg Baum
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 // {[(
14
15 #include <config.h>
16
17 #include "tex2lyx.h"
18
19 #include "Context.h"
20 #include "Preamble.h"
21
22 #include "support/lassert.h"
23 #include "support/convert.h"
24 #include "support/lstrings.h"
25
26 #include <algorithm>
27 #include <iostream>
28 #include <sstream>
29 #include <vector>
30 #include <map>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 namespace {
38
39 class ColInfo {
40 public:
41         ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0),
42                 varwidth(false), decimal_point('\0'), vcolumn(false) {}
43         /// column alignment
44         char align;
45         /// vertical alignment
46         char valign;
47         /// column width
48         string width;
49         /// special column alignment
50         string special;
51         /// number of lines on the right
52         int rightlines;
53         /// number of lines on the left
54         int leftlines;
55         /// varwidth column
56         bool varwidth;
57         /// decimal separator
58         char decimal_point;
59         /// V column type
60         bool vcolumn;
61 };
62
63
64 /// row type for longtables
65 enum LTRowType
66 {
67         /// normal row
68         LT_NORMAL,
69         /// part of head
70         LT_HEAD,
71         /// part of head on first page
72         LT_FIRSTHEAD,
73         /// part of foot
74         LT_FOOT,
75         /// part of foot on last page
76         LT_LASTFOOT
77 };
78
79
80 class RowInfo {
81 public:
82         RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
83                     caption(false), newpage(false) {}
84         /// Does this row have any special setting?
85         bool special() const
86         {
87                 return topline || bottomline || !top_space.empty() ||
88                         !bottom_space.empty() || !interline_space.empty() ||
89                         type != LT_NORMAL || caption || newpage;
90         }
91         /// horizontal line above
92         bool topline;
93         /// horizontal line below
94         bool bottomline;
95         /// Extra space between the top line and this row
96         string top_space;
97         /// Extra space between this row and the bottom line
98         string bottom_space;
99         /// Extra space between the bottom line and the next top line
100         string interline_space;
101         /// These are for longtabulars only
102         /// row type (head, foot, firsthead etc.)
103         LTRowType type;
104         /// row for a caption
105         bool caption;
106         /// row for a newpage
107         bool newpage;
108 };
109
110
111 /// the numeric values are part of the file format!
112 enum Multi {
113         /// A normal cell
114         CELL_NORMAL = 0,
115         /// A multicolumn cell. The number of columns is <tt>1 + number
116         /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
117         CELL_BEGIN_OF_MULTICOLUMN,
118         /// This is a dummy cell (part of a multicolumn cell)
119         CELL_PART_OF_MULTICOLUMN,
120         ///
121         CELL_BEGIN_OF_MULTIROW,
122         ///
123         CELL_PART_OF_MULTIROW
124 };
125
126
127 class CellInfo {
128 public:
129         CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
130                      leftlines(0), rightlines(0), topline(false),
131                      bottomline(false), rotate(0), mrxnum(0) {}
132         /// cell content
133         string content;
134         /// multicolumn flag
135         Multi multi;
136         /// cell alignment
137         char align;
138         /// vertical cell alignment
139         char valign;
140         /// number of lines on the left
141         int leftlines;
142         /// number of lines on the right
143         int rightlines;
144         /// do we have a line above?
145         bool topline;
146         /// do we have a line below?
147         bool bottomline;
148         /// how is the cell rotated?
149         int rotate;
150         /// width for multicolumn cells
151         string width;
152         /// special formatting for multicolumn cells
153         string special;
154         /// multirow offset
155         string mroffset;
156         /// number of further multirows
157         int mrxnum;
158 };
159
160
161 class ltType {
162 public:
163         // constructor
164         ltType() : set(false), topDL(false), bottomDL(false), empty(false) {}
165         // we have this header type (is set in the getLT... functions)
166         bool set;
167         // double borders on top
168         bool topDL;
169         // double borders on bottom
170         bool bottomDL;
171         // used for FirstHeader & LastFooter and if this is true
172         // all the rows marked as FirstHeader or LastFooter are
173         // ignored in the output and it is set to be empty!
174         bool empty;
175 };
176
177
178 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
179 inline char const * verbose_align(char c)
180 {
181         switch (c) {
182         case 'c':
183                 return "center";
184         case 'r':
185                 return "right";
186         case 'l':
187                 return "left";
188         case 'd':
189                 return "decimal";
190         default:
191                 return "none";
192         }
193 }
194
195
196 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
197 inline char const * verbose_valign(char c)
198 {
199         // The default value for no special alignment is "top".
200         switch (c) {
201         case 'm':
202                 return "middle";
203         case 'b':
204                 return "bottom";
205         case 'p':
206         default:
207                 return "top";
208         }
209 }
210
211
212 // stripped down from tabluar.C. We use it currently only for bools and
213 // strings
214 string const write_attribute(string const & name, bool const & b)
215 {
216         // we write only true attribute values so we remove a bit of the
217         // file format bloat for tabulars.
218         return b ? ' ' + name + "=\"true\"" : string();
219 }
220
221
222 string const write_attribute(string const & name, string const & s)
223 {
224         return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
225 }
226
227
228 string const write_attribute(string const & name, int const & i)
229 {
230         // we write only true attribute values so we remove a bit of the
231         // file format bloat for tabulars.
232         return i ? write_attribute(name, convert<string>(i)) : string();
233 }
234
235
236 /*! rather brutish way to code table structure in a string:
237
238 \verbatim
239   \begin{tabular}{ccc}
240     1 & 2 & 3\\ \hline
241     \multicolumn{2}{c}{4} & 5 //
242     6 & 7 \\
243     8 \endhead
244   \end{tabular}
245 \endverbatim
246
247  gets "translated" to:
248
249 \verbatim
250          HLINE 1                     TAB 2 TAB 3 HLINE          HLINE LINE
251   \hline HLINE \multicolumn{2}{c}{4} TAB 5       HLINE          HLINE LINE
252          HLINE 6                     TAB 7       HLINE          HLINE LINE
253          HLINE 8                                 HLINE \endhead HLINE LINE
254 \endverbatim
255  */
256
257 char const TAB   = '\001';
258 char const LINE  = '\002';
259 char const HLINE = '\004';
260
261
262 /*!
263  * Move the information in leftlines, rightlines, align and valign to the
264  * special field. This is necessary if the special field is not empty,
265  * because LyX ignores leftlines > 1, rightlines > 1, align and valign in
266  * this case.
267  */
268 void ci2special(ColInfo & ci)
269 {
270         if (ci.width.empty() && ci.align == 'n')
271                 // The alignment setting is already in special, since
272                 // handle_colalign() never stores ci with these settings
273                 // and ensures that leftlines == 0 and rightlines == 0 in
274                 // this case.
275                 return;
276
277         if (ci.decimal_point != '\0') {
278                 // we only support decimal point natively
279                 // with 'l' alignment in or 'n' alignment
280                 // with width in second row
281                 if (ci.align != 'l' && ci.align != 'n') {
282                         ci.decimal_point = '\0';
283                         return;
284                 } else
285                         ci.special.clear();
286         }
287
288         if (!ci.width.empty()) {
289                 string arraybackslash;
290                 if (ci.varwidth)
291                         arraybackslash = "\\arraybackslash";
292                 switch (ci.align) {
293                 case 'l':
294                         ci.special += ">{\\raggedright" + arraybackslash + "}";
295                         break;
296                 case 'r':
297                         ci.special += ">{\\raggedleft" + arraybackslash + "}";
298                         break;
299                 case 'c':
300                         ci.special += ">{\\centering" + arraybackslash + "}";
301                         break;
302                 }
303                 if (ci.vcolumn)
304                         ci.special += 'V';
305                 else if (ci.varwidth)
306                         ci.special += 'X';
307                 else if (ci.valign == 'n')
308                         ci.special += 'p';
309                 else
310                         ci.special += ci.valign;
311                 ci.special += '{' + ci.width + '}';
312                 ci.width.erase();
313         } else
314                 ci.special += ci.align;
315
316         // LyX can only have one left and one right line.
317         for (int i = 1; i < ci.leftlines; ++i)
318                 ci.special.insert(0, "|");
319         for (int i = 1; i < ci.rightlines; ++i)
320                 ci.special += '|';
321         ci.leftlines = min(ci.leftlines, 1);
322         ci.rightlines = min(ci.rightlines, 1);
323         ci.align = 'n';
324         ci.valign = 'n';
325 }
326
327
328 /*!
329  * Handle column specifications for tabulars and multicolumns.
330  * The next token of the parser \p p must be an opening brace, and we read
331  * everything until the matching closing brace.
332  * The resulting column specifications are filled into \p colinfo. This is
333  * in an intermediate form. fix_colalign() makes it suitable for LyX output.
334  */
335 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
336                      ColInfo const & start)
337 {
338         if (p.get_token().cat() != catBegin)
339                 cerr << "Wrong syntax for table column alignment.\n"
340                         "Expected '{', got '" << p.curr_token().asInput()
341                      << "'.\n";
342
343         ColInfo next = start;
344         for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
345              t = p.get_token()) {
346 #ifdef FILEDEBUG
347                 cerr << "t: " << t << "  c: '" << t.character() << "'\n";
348 #endif
349
350                 // We cannot handle comments here
351                 if (t.cat() == catComment) {
352                         if (t.cs().empty()) {
353                                 // "%\n" combination
354                                 p.skip_spaces();
355                         } else
356                                 cerr << "Ignoring comment: " << t.asInput();
357                         continue;
358                 }
359
360                 switch (t.character()) {
361                         case ' ':
362                                 // whitespace, ignore.
363                                 break;
364                         case 'c':
365                         case 'l':
366                         case 'r':
367                                 // new column, horizontal aligned
368                                 next.align = t.character();
369                                 if (!next.special.empty()) {
370                                         ci2special(next);
371                                         // handle decimal separator
372                                         if (next.decimal_point != '\0') {
373                                                 if (!colinfo.empty() && colinfo.back().align == 'r') {
374                                                         colinfo.back().align = 'd';
375                                                         colinfo.back().decimal_point = next.decimal_point;
376                                                 } else
377                                                         next.decimal_point = '\0';
378                                         }
379                                 }
380                                 colinfo.push_back(next);
381                                 next = ColInfo();
382                                 break;
383                         case 'X':
384                                 // varwidth column
385                                 next.varwidth = true;
386                                 if (!next.special.empty())
387                                         ci2special(next);
388                                 colinfo.push_back(next);
389                                 next = ColInfo();
390                                 break;
391                         case 'V': {
392                                 // V column type (varwidth package)
393                                 string const s = trimSpaceAndEol(p.verbatim_item());
394                                 // V{\linewidth} is treated as a normal column
395                                 // (which allows for line breaks). The V type is
396                                 // automatically set by LyX in this case
397                                 if (s != "\\linewidth" || !next.special.empty()) {
398                                         next.vcolumn = true;
399                                         next.width = s;
400                                         ci2special(next);
401                                 }
402                                 colinfo.push_back(next);
403                                 next = ColInfo();
404                                 break;
405                         }
406                         case 'p':
407                         case 'b':
408                         case 'm':
409                                 // new column, vertical aligned box
410                                 next.valign = t.character();
411                                 next.width = p.verbatim_item();
412                                 if (!next.special.empty()) {
413                                         ci2special(next);
414                                         // handle decimal separator
415                                         if (next.decimal_point != '\0') {
416                                                 if (!colinfo.empty() && colinfo.back().align == 'r') {
417                                                         colinfo.back().align = 'd';
418                                                         colinfo.back().decimal_point = next.decimal_point;
419                                                 } else
420                                                         next.decimal_point = '\0';
421                                         }
422                                 }
423                                 colinfo.push_back(next);
424                                 next = ColInfo();
425                                 break;
426                         case '|':
427                                 // vertical rule
428                                 if (colinfo.empty()) {
429                                         if (next.special.empty())
430                                                 ++next.leftlines;
431                                         else
432                                                 next.special += '|';
433                                 } else if (colinfo.back().special.empty())
434                                         ++colinfo.back().rightlines;
435                                 else if (next.special.empty() && p.next_token().cat() != catEnd)
436                                         ++next.leftlines;
437                                 else
438                                         colinfo.back().special += '|';
439                                 break;
440                         case '>': {
441                                 // text before the next column
442                                 string const s = trimSpaceAndEol(p.verbatim_item());
443                                 if (next.special.empty() &&
444                                     next.align == 'n') {
445                                         // Maybe this can be converted to a
446                                         // horizontal alignment setting for
447                                         // fixed width columns
448                                         if (s == "\\raggedleft" || s == "\\raggedleft\\arraybackslash")
449                                                 next.align = 'r';
450                                         else if (s == "\\raggedright" || s == "\\raggedright\\arraybackslash")
451                                                 next.align = 'l';
452                                         else if (s == "\\centering" || s == "\\centering\\arraybackslash")
453                                                 next.align = 'c';
454                                         else
455                                                 next.special = ">{" + s + '}';
456                                 } else
457                                         next.special += ">{" + s + '}';
458                                 break;
459                         }
460                         case '<': {
461                                 // text after the last column
462                                 string const s = trimSpaceAndEol(p.verbatim_item());
463                                 if (colinfo.empty())
464                                         // This is not possible in LaTeX.
465                                         cerr << "Ignoring separator '<{"
466                                              << s << "}'." << endl;
467                                 else {
468                                         ColInfo & ci = colinfo.back();
469                                         ci2special(ci);
470                                         ci.special += "<{" + s + '}';
471                                 }
472                                 break;
473                         }
474                         case '*': {
475                                 if (p.next_token().character() != '{')
476                                         continue;
477                                 // *{n}{arg} means 'n' columns of type 'arg'
478                                 string const num = p.verbatim_item();
479                                 string const arg = p.verbatim_item();
480                                 size_t const n = convert<unsigned int>(num);
481                                 if (!arg.empty() && n > 0) {
482                                         string s("{");
483                                         for (size_t i = 0; i < n; ++i)
484                                                 s += arg;
485                                         s += '}';
486                                         Parser p2(s);
487                                         handle_colalign(p2, colinfo, next);
488                                         next = ColInfo();
489                                 } else {
490                                         cerr << "Ignoring column specification"
491                                                 " '*{" << num << "}{"
492                                              << arg << "}'." << endl;
493                                 }
494                                 break;
495                         }
496                         case '@':
497                                 // text instead of the column spacing
498                         case '!': {
499                                 // text in addition to the column spacing
500                                 string const arg =  p.verbatim_item();
501                                 next.special += t.character();
502                                 next.special += '{' + arg + '}';
503                                 string const sarg = arg.size() > 2 ? arg.substr(0, arg.size() - 1) : string();
504                                 if (t.character() == '@' && sarg == "\\extracolsep{0pt}")
505                                         next.decimal_point = arg.back();
506                                 break;
507                         }
508                         default: {
509                                 // try user defined column types
510                                 // unknown column types (nargs == -1) are
511                                 // assumed to consume no arguments
512                                 ci2special(next);
513                                 next.special += t.character();
514                                 int const nargs =
515                                         preamble.getSpecialTableColumnArguments(t.character());
516                                 for (int i = 0; i < nargs; ++i)
517                                         next.special += '{' +
518                                                 p.verbatim_item() + '}';
519                                 colinfo.push_back(next);
520                                 next = ColInfo();
521                                 break;
522                         }
523                         }
524         }
525
526         // Maybe we have some column separators that need to be added to the
527         // last column?
528         ci2special(next);
529         if (!next.special.empty()) {
530                 ColInfo & ci = colinfo.back();
531                 ci2special(ci);
532                 ci.special += next.special;
533                 next.special.erase();
534         }
535 }
536
537
538 /*!
539  * Move the left and right lines and alignment settings of the column \p ci
540  * to the special field if necessary.
541  */
542 void fix_colalign(ColInfo & ci)
543 {
544         if (ci.leftlines > 1 || ci.rightlines > 1)
545                 ci2special(ci);
546 }
547
548
549 /*!
550  * LyX can't handle more than one vertical line at the left or right side
551  * of a column.
552  * This function moves the left and right lines and alignment settings of all
553  * columns in \p colinfo to the special field if necessary.
554  */
555 void fix_colalign(vector<ColInfo> & colinfo)
556 {
557         // Try to move extra leftlines to the previous column.
558         // We do this only if both special fields are empty, otherwise we
559         // can't tell wether the result will be the same.
560         for (size_t col = 0; col < colinfo.size(); ++col) {
561                 if (colinfo[col].leftlines > 1 &&
562                     colinfo[col].special.empty() && col > 0 &&
563                     colinfo[col - 1].rightlines == 0 &&
564                     colinfo[col - 1].special.empty()) {
565                         ++colinfo[col - 1].rightlines;
566                         --colinfo[col].leftlines;
567                 }
568         }
569         // Try to move extra rightlines to the next column
570         for (size_t col = 0; col < colinfo.size(); ++col) {
571                 if (colinfo[col].rightlines > 1 &&
572                     colinfo[col].special.empty() &&
573                     col < colinfo.size() - 1 &&
574                     colinfo[col + 1].leftlines == 0 &&
575                     colinfo[col + 1].special.empty()) {
576                         ++colinfo[col + 1].leftlines;
577                         --colinfo[col].rightlines;
578                 }
579         }
580         // Move the lines and alignment settings to the special field if
581         // necessary
582         for (size_t col = 0; col < colinfo.size(); ++col)
583                 fix_colalign(colinfo[col]);
584 }
585
586
587 /*!
588  * Parse hlines and similar stuff.
589  * \returns wether the token \p t was parsed
590  */
591 bool parse_hlines(Parser & p, Token const & t, string & hlines,
592                   bool is_long_tabular)
593 {
594         LASSERT(t.cat() == catEscape, return false);
595
596         if (t.cs() == "hline" || t.cs() == "toprule" || t.cs() == "midrule" ||
597             t.cs() == "bottomrule")
598                 hlines += '\\' + t.cs();
599
600         else if (t.cs() == "cline")
601                 hlines += "\\cline{" + p.verbatim_item() + '}';
602
603         else if (t.cs() == "cmidrule") {
604                 // We cannot handle the \cmidrule(l){3-4} form
605                 p.pushPosition();
606                 p.skip_spaces(true);
607                 bool const hasParentheses(p.getFullArg('(', ')').first);
608                 p.popPosition();
609                 if (hasParentheses)
610                         return false;
611                 hlines += "\\cmidrule{" + p.verbatim_item() + '}';
612         }
613
614         else if (t.cs() == "addlinespace") {
615                 p.pushPosition();
616                 p.skip_spaces(true);
617                 bool const hasArgument(p.getFullArg('{', '}').first);
618                 p.popPosition();
619                 if (hasArgument)
620                         hlines += "\\addlinespace{" + p.verbatim_item() + '}';
621                 else
622                         hlines += "\\addlinespace";
623         }
624
625         else if (is_long_tabular && t.cs() == "newpage")
626                 hlines += "\\newpage";
627
628         else
629                 return false;
630
631         return true;
632 }
633
634
635 /// Position in a row
636 enum RowPosition {
637         /// At the very beginning, before the first token
638         ROW_START,
639         /// After the first token and before any column token
640         IN_HLINES_START,
641         /// After the first column token. Comments and whitespace are only
642         /// treated as tokens in this position
643         IN_COLUMNS,
644         /// After the first non-column token at the end
645         IN_HLINES_END
646 };
647
648
649 /*!
650  * Parse table structure.
651  * We parse tables in a two-pass process: This function extracts the table
652  * structure (rows, columns, hlines etc.), but does not change the cell
653  * content. The cell content is parsed in a second step in handle_tabular().
654  */
655 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
656                  RowPosition & pos, unsigned flags)
657 {
658         // table structure commands such as \hline
659         string hlines;
660
661         // comments that occur at places where we can't handle them
662         string comments;
663
664         while (p.good()) {
665                 Token const & t = p.get_token();
666
667 #ifdef FILEDEBUG
668                 debugToken(cerr, t, flags);
669 #endif
670
671                 // comments and whitespace in hlines
672                 switch (pos) {
673                 case ROW_START:
674                 case IN_HLINES_START:
675                 case IN_HLINES_END:
676                         if (t.cat() == catComment) {
677                                 if (t.cs().empty())
678                                         // line continuation
679                                         p.skip_spaces();
680                                 else
681                                         // We can't handle comments here,
682                                         // store them for later use
683                                         comments += t.asInput();
684                                 continue;
685                         } else if (t.cat() == catSpace ||
686                                    t.cat() == catNewline) {
687                                 // whitespace is irrelevant here, we
688                                 // need to recognize hline stuff
689                                 p.skip_spaces();
690                                 continue;
691                         }
692                         break;
693                 case IN_COLUMNS:
694                         break;
695                 }
696
697                 // We need to handle structure stuff first in order to
698                 // determine wether we need to output a HLINE separator
699                 // before the row or not.
700                 if (t.cat() == catEscape) {
701                         if (parse_hlines(p, t, hlines, is_long_tabular)) {
702                                 switch (pos) {
703                                 case ROW_START:
704                                         pos = IN_HLINES_START;
705                                         break;
706                                 case IN_COLUMNS:
707                                         pos = IN_HLINES_END;
708                                         break;
709                                 case IN_HLINES_START:
710                                 case IN_HLINES_END:
711                                         break;
712                                 }
713                                 continue;
714                         }
715
716                         else if (t.cs() == "tabularnewline" ||
717                                  t.cs() == "\\" ||
718                                  t.cs() == "cr") {
719                                 if (t.cs() == "cr")
720                                         cerr << "Warning: Converting TeX "
721                                                 "'\\cr' to LaTeX '\\\\'."
722                                              << endl;
723                                 // stuff before the line break
724                                 os << comments << HLINE << hlines << HLINE
725                                    << LINE;
726                                 //cerr << "hlines: " << hlines << endl;
727                                 hlines.erase();
728                                 comments.erase();
729                                 pos = ROW_START;
730                                 continue;
731                         }
732
733                         else if (is_long_tabular &&
734                                  (t.cs() == "endhead" ||
735                                   t.cs() == "endfirsthead" ||
736                                   t.cs() == "endfoot" ||
737                                   t.cs() == "endlastfoot")) {
738                                 hlines += t.asInput();
739                                 switch (pos) {
740                                 case IN_COLUMNS:
741                                 case IN_HLINES_END:
742                                         // these commands are implicit line
743                                         // breaks
744                                         os << comments << HLINE << hlines
745                                            << HLINE << LINE;
746                                         hlines.erase();
747                                         comments.erase();
748                                         pos = ROW_START;
749                                         break;
750                                 case ROW_START:
751                                         pos = IN_HLINES_START;
752                                         break;
753                                 case IN_HLINES_START:
754                                         break;
755                                 }
756                                 continue;
757                         }
758                 }
759
760                 // We need a HLINE separator if we either have no hline
761                 // stuff at all and are just starting a row or if we just
762                 // got the first non-hline token.
763                 switch (pos) {
764                 case ROW_START:
765                         // no hline tokens exist, first token at row start
766                 case IN_HLINES_START:
767                         // hline tokens exist, first non-hline token at row
768                         // start
769                         os << hlines << HLINE << comments;
770                         hlines.erase();
771                         comments.erase();
772                         pos = IN_COLUMNS;
773                         break;
774                 case IN_HLINES_END:
775                         // Oops, there is still cell content or unsupported
776                         // booktabs commands after hline stuff. The latter are
777                         // moved to the cell, and the first does not work in
778                         // LaTeX, so we ignore the hlines.
779                         os << comments;
780                         comments.erase();
781                         if (support::contains(hlines, "\\hline") ||
782                             support::contains(hlines, "\\cline") ||
783                             support::contains(hlines, "\\newpage"))
784                                 cerr << "Ignoring '" << hlines
785                                      << "' in a cell" << endl;
786                         else
787                                 os << hlines;
788                         hlines.erase();
789                         pos = IN_COLUMNS;
790                         break;
791                 case IN_COLUMNS:
792                         break;
793                 }
794
795                 // If we come here we have normal cell content
796                 //
797                 // cat codes
798                 //
799                 if (t.cat() == catMath) {
800                         // we are inside some text mode thingy, so opening new math is allowed
801                         Token const & n = p.get_token();
802                         if (n.cat() == catMath) {
803                                 // TeX's $$...$$ syntax for displayed math
804                                 os << "\\[";
805                                 // This does only work because parse_math outputs TeX
806                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
807                                 os << "\\]";
808                                 p.get_token(); // skip the second '$' token
809                         } else {
810                                 // simple $...$  stuff
811                                 p.putback();
812                                 os << '$';
813                                 // This does only work because parse_math outputs TeX
814                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
815                                 os << '$';
816                         }
817                 }
818
819                 else if (t.cat() == catSpace
820                          || t.cat() == catNewline
821                          || t.cat() == catLetter
822                          || t.cat() == catSuper
823                          || t.cat() == catSub
824                          || t.cat() == catOther
825                          || t.cat() == catActive
826                          || t.cat() == catParameter)
827                         os << t.cs();
828
829                 else if (t.cat() == catBegin) {
830                         os << '{';
831                         parse_table(p, os, is_long_tabular, pos,
832                                     FLAG_BRACE_LAST);
833                         os << '}';
834                 }
835
836                 else if (t.cat() == catEnd) {
837                         if (flags & FLAG_BRACE_LAST)
838                                 return;
839                         cerr << "unexpected '}'\n";
840                 }
841
842                 else if (t.cat() == catAlign) {
843                         os << TAB;
844                         p.skip_spaces();
845                 }
846
847                 else if (t.cat() == catComment)
848                         os << t.asInput();
849
850                 else if (t.cs() == "(") {
851                         os << "\\(";
852                         // This does only work because parse_math outputs TeX
853                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
854                         os << "\\)";
855                 }
856
857                 else if (t.cs() == "[") {
858                         os << "\\[";
859                         // This does only work because parse_math outputs TeX
860                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
861                         os << "\\]";
862                 }
863
864                 else if (t.cs() == "begin") {
865                         string const name = p.getArg('{', '}');
866                         active_environments.push_back(name);
867                         os << "\\begin{" << name << '}';
868                         // treat the nested environment as a block, don't
869                         // parse &, \\ etc, because they don't belong to our
870                         // table if they appear.
871                         os << p.ertEnvironment(name);
872                         os << "\\end{" << name << '}';
873                         active_environments.pop_back();
874                 }
875
876                 else if (t.cs() == "end") {
877                         if (flags & FLAG_END) {
878                                 // eat environment name
879                                 string const name = p.getArg('{', '}');
880                                 if (name != active_environment())
881                                         p.error("\\end{" + name + "} does not match \\begin{"
882                                                 + active_environment() + "}");
883                                 return;
884                         }
885                         p.error("found 'end' unexpectedly");
886                 }
887
888                 else
889                         os << t.asInput();
890         }
891
892         // We can have comments if the last line is incomplete
893         os << comments;
894
895         // We can have hline stuff if the last line is incomplete
896         if (!hlines.empty()) {
897                 // this does not work in LaTeX, so we ignore it
898                 cerr << "Ignoring '" << hlines << "' at end of tabular"
899                      << endl;
900         }
901 }
902
903
904 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
905 {
906         ri.topline = true;
907         for (size_t col = 0; col < ci.size(); ++col)
908                 ci[col].topline = true;
909 }
910
911
912 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
913 {
914         ri.bottomline = true;
915         for (size_t col = 0; col < ci.size(); ++col)
916                 ci[col].bottomline = true;
917 }
918
919
920 } // anonymous namespace
921
922
923 void handle_tabular(Parser & p, ostream & os, string const & name,
924                     string const & tabularwidth, string const & halign,
925                     Context & context)
926 {
927         bool const is_long_tabular(name == "longtable" || name == "xltabular");
928         bool booktabs = false;
929         string tabularvalignment("middle");
930         string posopts = p.getOpt();
931         if (!posopts.empty()) {
932                 if (posopts == "[t]")
933                         tabularvalignment = "top";
934                 else if (posopts == "[b]")
935                         tabularvalignment = "bottom";
936                 else
937                         cerr << "vertical tabular positioning '"
938                              << posopts << "' ignored\n";
939         }
940
941         vector<ColInfo> colinfo;
942
943         // handle column formatting
944         handle_colalign(p, colinfo, ColInfo());
945         fix_colalign(colinfo);
946
947         // first scan of cells
948         // use table mode to keep it minimal-invasive
949         // not exactly what's TeX doing...
950         vector<string> lines;
951         ostringstream ss;
952         RowPosition rowpos = ROW_START;
953         parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
954         split(ss.str(), lines, LINE);
955
956         vector< vector<CellInfo> > cellinfo(lines.size());
957         vector<RowInfo> rowinfo(lines.size());
958         ltType endfirsthead;
959         ltType endhead;
960         ltType endfoot;
961         ltType endlastfoot;
962
963         // split into rows
964         //cerr << "// split into rows\n";
965         for (size_t row = 0; row < rowinfo.size();) {
966
967                 // init row
968                 cellinfo[row].resize(colinfo.size());
969                 bool deletelastrow = false;
970
971                 // split row
972                 vector<string> dummy;
973                 //cerr << "\n########### LINE: " << lines[row] << "########\n";
974                 split(lines[row], dummy, HLINE);
975
976                 // handle horizontal line fragments
977                 // we do only expect this for a last line without '\\'
978                 if (dummy.size() != 3) {
979                         if ((dummy.size() != 1 && dummy.size() != 2) ||
980                             row != rowinfo.size() - 1)
981                                 cerr << "unexpected dummy size: " << dummy.size()
982                                         << " content: " << lines[row] << "\n";
983                         dummy.resize(3);
984                 }
985                 lines[row] = dummy[1];
986
987                 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
988                 //cerr << "line: " << row << " below 2: " << dummy[2] <<  "\n";
989                 //cerr << "line: " << row << " cells 1: " << dummy[1] <<  "\n";
990
991                 for (int i = 0; i <= 2; i += 2) {
992                         //cerr << "   reading from line string '" << dummy[i] << "'\n";
993                         Parser p1(dummy[i]);
994                         while (p1.good()) {
995                                 Token t = p1.get_token();
996                                 //cerr << "read token: " << t << "\n";
997                                 if (t.cs() == "hline" || t.cs() == "toprule" ||
998                                     t.cs() == "midrule" ||
999                                     t.cs() == "bottomrule") {
1000                                         if (t.cs() != "hline")
1001                                                 booktabs = true;
1002                                         if (i == 0) {
1003                                                 if (rowinfo[row].topline) {
1004                                                         if (row > 0) // extra bottomline above
1005                                                                 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
1006                                                         else
1007                                                                 cerr << "dropping extra "
1008                                                                      << t.cs() << '\n';
1009                                                         //cerr << "below row: " << row-1 << endl;
1010                                                 } else {
1011                                                         handle_hline_above(rowinfo[row], cellinfo[row]);
1012                                                         //cerr << "above row: " << row << endl;
1013                                                 }
1014                                         } else {
1015                                                 //cerr << "below row: " << row << endl;
1016                                                 handle_hline_below(rowinfo[row], cellinfo[row]);
1017                                         }
1018                                 } else if (t.cs() == "cline" || t.cs() == "cmidrule") {
1019                                         if (t.cs() == "cmidrule")
1020                                                 booktabs = true;
1021                                         string arg = p1.verbatim_item();
1022                                         //cerr << "read " << t.cs() << " arg: '" << arg << "'\n";
1023                                         vector<string> cols;
1024                                         split(arg, cols, '-');
1025                                         cols.resize(2);
1026                                         size_t from = convert<unsigned int>(cols[0]);
1027                                         if (from == 0)
1028                                                 cerr << "Could not parse "
1029                                                      << t.cs() << " start column."
1030                                                      << endl;
1031                                         else
1032                                                 // 1 based index -> 0 based
1033                                                 --from;
1034                                         if (from >= colinfo.size()) {
1035                                                 cerr << t.cs() << " starts at "
1036                                                         "non existing column "
1037                                                      << (from + 1) << endl;
1038                                                 from = colinfo.size() - 1;
1039                                         }
1040                                         size_t to = convert<unsigned int>(cols[1]);
1041                                         if (to == 0)
1042                                                 cerr << "Could not parse "
1043                                                      << t.cs() << " end column."
1044                                                      << endl;
1045                                         else
1046                                                 // 1 based index -> 0 based
1047                                                 --to;
1048                                         if (to >= colinfo.size()) {
1049                                                 cerr << t.cs() << " ends at "
1050                                                         "non existing column "
1051                                                      << (to + 1) << endl;
1052                                                 to = colinfo.size() - 1;
1053                                         }
1054                                         for (size_t col = from; col <= to; ++col) {
1055                                                 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
1056                                                 if (i == 0) {
1057                                                         rowinfo[row].topline = true;
1058                                                         cellinfo[row][col].topline = true;
1059                                                 } else {
1060                                                         rowinfo[row].bottomline = true;
1061                                                         cellinfo[row][col].bottomline = true;
1062                                                 }
1063                                         }
1064                                 } else if (t.cs() == "addlinespace") {
1065                                         booktabs = true;
1066                                         string const opt = p.next_token().cat() == catBegin ?
1067                                                         p.verbatim_item() : string();
1068                                         if (i == 0) {
1069                                                 if (opt.empty())
1070                                                         rowinfo[row].top_space = "default";
1071                                                 else
1072                                                         rowinfo[row].top_space = translate_len(opt);
1073                                         } else if (rowinfo[row].bottomline) {
1074                                                 if (opt.empty())
1075                                                         rowinfo[row].bottom_space = "default";
1076                                                 else
1077                                                         rowinfo[row].bottom_space = translate_len(opt);
1078                                         } else {
1079                                                 if (opt.empty())
1080                                                         rowinfo[row].interline_space = "default";
1081                                                 else
1082                                                         rowinfo[row].interline_space = translate_len(opt);
1083                                         }
1084                                 } else if (t.cs() == "endhead") {
1085                                         if (i == 0)
1086                                                 endhead.empty = true;
1087                                         else
1088                                                 rowinfo[row].type = LT_HEAD;
1089                                         for (int r = row - 1; r >= 0; --r) {
1090                                                 if (rowinfo[r].type != LT_NORMAL)
1091                                                         break;
1092                                                 rowinfo[r].type = LT_HEAD;
1093                                                 endhead.empty = false;
1094                                         }
1095                                         endhead.set = true;
1096                                 } else if (t.cs() == "endfirsthead") {
1097                                         if (i == 0)
1098                                                 endfirsthead.empty = true;
1099                                         else
1100                                                 rowinfo[row].type = LT_FIRSTHEAD;
1101                                         for (int r = row - 1; r >= 0; --r) {
1102                                                 if (rowinfo[r].type != LT_NORMAL)
1103                                                         break;
1104                                                 rowinfo[r].type = LT_FIRSTHEAD;
1105                                                 endfirsthead.empty = false;
1106                                         }
1107                                         endfirsthead.set = true;
1108                                 } else if (t.cs() == "endfoot") {
1109                                         if (i == 0)
1110                                                 endfoot.empty = true;
1111                                         else
1112                                                 rowinfo[row].type = LT_FOOT;
1113                                         for (int r = row - 1; r >= 0; --r) {
1114                                                 if (rowinfo[r].type != LT_NORMAL)
1115                                                         break;
1116                                                 rowinfo[r].type = LT_FOOT;
1117                                                 endfoot.empty = false;
1118                                         }
1119                                         endfoot.set = true;
1120                                 } else if (t.cs() == "endlastfoot") {
1121                                         if (i == 0)
1122                                                 endlastfoot.empty = true;
1123                                         else
1124                                                 rowinfo[row].type = LT_LASTFOOT;
1125                                         for (int r = row - 1; r >= 0; --r) {
1126                                                 if (rowinfo[r].type != LT_NORMAL)
1127                                                         break;
1128                                                 rowinfo[r].type = LT_LASTFOOT;
1129                                                 endlastfoot.empty = false;
1130                                         }
1131                                         endlastfoot.set = true;
1132                                 } else if (t.cs() == "newpage") {
1133                                         if (i == 0) {
1134                                                 if (row > 0)
1135                                                         rowinfo[row - 1].newpage = true;
1136                                                 else
1137                                                         // This does not work in LaTeX
1138                                                         cerr << "Ignoring "
1139                                                                 "'\\newpage' "
1140                                                                 "before rows."
1141                                                              << endl;
1142                                         } else
1143                                                 rowinfo[row].newpage = true;
1144                                 } else {
1145                                         cerr << "unexpected line token: " << t << endl;
1146                                 }
1147                         }
1148                 }
1149
1150                 // LyX ends headers and footers always with \tabularnewline.
1151                 // This causes one additional row in the output.
1152                 // If the last row of a header/footer is empty, we can work
1153                 // around that by removing it.
1154                 if (row > 1) {
1155                         RowInfo test = rowinfo[row-1];
1156                         test.type = LT_NORMAL;
1157                         if (lines[row-1].empty() && !test.special()) {
1158                                 switch (rowinfo[row-1].type) {
1159                                 case LT_FIRSTHEAD:
1160                                         if (rowinfo[row].type != LT_FIRSTHEAD &&
1161                                             rowinfo[row-2].type == LT_FIRSTHEAD)
1162                                                 deletelastrow = true;
1163                                         break;
1164                                 case LT_HEAD:
1165                                         if (rowinfo[row].type != LT_HEAD &&
1166                                             rowinfo[row-2].type == LT_HEAD)
1167                                                 deletelastrow = true;
1168                                         break;
1169                                 case LT_FOOT:
1170                                         if (rowinfo[row].type != LT_FOOT &&
1171                                             rowinfo[row-2].type == LT_FOOT)
1172                                                 deletelastrow = true;
1173                                         break;
1174                                 case LT_LASTFOOT:
1175                                         if (rowinfo[row].type != LT_LASTFOOT &&
1176                                             rowinfo[row-2].type == LT_LASTFOOT)
1177                                                 deletelastrow = true;
1178                                         break;
1179                                 case LT_NORMAL:
1180                                         break;
1181                                 }
1182                         }
1183                 }
1184
1185                 if (deletelastrow) {
1186                         lines.erase(lines.begin() + (row - 1));
1187                         rowinfo.erase(rowinfo.begin() + (row - 1));
1188                         cellinfo.erase(cellinfo.begin() + (row - 1));
1189                         continue;
1190                 }
1191
1192                 // split into cells
1193                 vector<string> cells;
1194                 split(lines[row], cells, TAB);
1195                 for (size_t col = 0, cell = 0; cell < cells.size();
1196                      ++col, ++cell) {
1197                         //cerr << "cell content: '" << cells[cell] << "'\n";
1198                         if (col >= colinfo.size()) {
1199                                 // This does not work in LaTeX
1200                                 cerr << "Ignoring extra cell '"
1201                                      << cells[cell] << "'." << endl;
1202                                 continue;
1203                         }
1204                         string cellcont = cells[cell];
1205                         // For decimal cells, ass the content of the second one to the first one
1206                         // of a pair.
1207                         if (colinfo[col].decimal_point != '\0' && colinfo[col].align == 'd' && cell < cells.size() - 1)
1208                                 cellcont += colinfo[col].decimal_point + cells[cell + 1];
1209                         Parser parse(cellcont);
1210                         parse.skip_spaces();
1211                         //cells[cell] << "'\n";
1212                         if (parse.next_token().cs() == "multirow") {
1213                                 // We do not support the vpos arg yet.
1214                                 if (parse.hasOpt()) {
1215                                         string const vpos = parse.getArg('[', ']');
1216                                         parse.skip_spaces(true);
1217                                         cerr << "Ignoring multirow's vpos arg '"
1218                                              << vpos << "'!" << endl;
1219                                 }
1220                                 // how many cells?
1221                                 parse.get_token();
1222                                 size_t const ncells =
1223                                         convert<unsigned int>(parse.verbatim_item());
1224                                 // We do not support the bigstrut arg yet.
1225                                 if (parse.hasOpt()) {
1226                                         string const bs = parse.getArg('[', ']');
1227                                         parse.skip_spaces(true);
1228                                         cerr << "Ignoring multirow's bigstrut arg '"
1229                                              << bs << "'!" << endl;
1230                                 }
1231                                 // the width argument
1232                                 string const width = parse.getArg('{', '}');
1233                                 // the vmove arg
1234                                 string vmove;
1235                                 if (parse.hasOpt()) {
1236                                         vmove = parse.getArg('[', ']');
1237                                         parse.skip_spaces(true);
1238                                 }
1239
1240                                 if (width != "*")
1241                                         colinfo[col].width = width;
1242                                 if (!vmove.empty())
1243                                         cellinfo[row][col].mroffset = vmove;
1244                                 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTIROW;
1245                                 cellinfo[row][col].leftlines  = colinfo[col].leftlines;
1246                                 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1247                                 cellinfo[row][col].mrxnum = ncells - 1;
1248
1249                                 ostringstream os2;
1250                                 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1251                                 if (!cellinfo[row][col].content.empty()) {
1252                                         // This may or may not work in LaTeX,
1253                                         // but it does not work in LyX.
1254                                         // FIXME: Handle it correctly!
1255                                         cerr << "Moving cell content '"
1256                                              << cells[cell]
1257                                              << "' into a multirow cell. "
1258                                                 "This will probably not work."
1259                                              << endl;
1260                                 }
1261                                 cellinfo[row][col].content += os2.str();
1262                         } else if (parse.next_token().cs() == "multicolumn") {
1263                                 // how many cells?
1264                                 parse.get_token();
1265                                 size_t const ncells =
1266                                         convert<unsigned int>(parse.verbatim_item());
1267
1268                                 // special cell properties alignment
1269                                 vector<ColInfo> t;
1270                                 handle_colalign(parse, t, ColInfo());
1271                                 parse.skip_spaces(true);
1272                                 ColInfo & ci = t.front();
1273
1274                                 // The logic of LyX for multicolumn vertical
1275                                 // lines is too complicated to reproduce it
1276                                 // here (see LyXTabular::TeXCellPreamble()).
1277                                 // Therefore we simply put everything in the
1278                                 // special field.
1279                                 ci2special(ci);
1280
1281                                 cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
1282                                 cellinfo[row][col].align      = ci.align;
1283                                 cellinfo[row][col].special    = ci.special;
1284                                 cellinfo[row][col].leftlines  = ci.leftlines;
1285                                 cellinfo[row][col].rightlines = ci.rightlines;
1286                                 ostringstream os2;
1287                                 parse_text_in_inset(parse, os2, FLAG_ITEM, false, context);
1288                                 if (!cellinfo[row][col].content.empty()) {
1289                                         // This may or may not work in LaTeX,
1290                                         // but it does not work in LyX.
1291                                         // FIXME: Handle it correctly!
1292                                         cerr << "Moving cell content '"
1293                                              << cells[cell]
1294                                              << "' into a multicolumn cell. "
1295                                                 "This will probably not work."
1296                                              << endl;
1297                                 }
1298                                 cellinfo[row][col].content += os2.str();
1299
1300                                 // add dummy cells for multicol
1301                                 for (size_t i = 0; i < ncells - 1; ++i) {
1302                                         ++col;
1303                                         if (col >= colinfo.size()) {
1304                                                 cerr << "The cell '"
1305                                                         << cells[cell]
1306                                                         << "' specifies "
1307                                                         << convert<string>(ncells)
1308                                                         << " columns while the table has only "
1309                                                         << convert<string>(colinfo.size())
1310                                                         << " columns!"
1311                                                         << " Therefore the surplus columns will be ignored."
1312                                                         << endl;
1313                                                 break;
1314                                         }
1315                                         cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1316                                         cellinfo[row][col].align = 'c';
1317                                 }
1318
1319                         } else if (col == 0 && colinfo.size() > 1 &&
1320                                    is_long_tabular &&
1321                                    parse.next_token().cs() == "caption") {
1322                                 // longtable caption support in LyX is a hack:
1323                                 // Captions require a row of their own with
1324                                 // the caption flag set to true, having only
1325                                 // one multicolumn cell. The contents of that
1326                                 // cell must contain exactly one caption inset
1327                                 // and nothing else.
1328                                 // Fortunately, the caption flag is only needed
1329                                 // for tables with more than one column.
1330                                 rowinfo[row].caption = true;
1331                                 for (size_t c = 1; c < cells.size(); ++c) {
1332                                         if (!cells[c].empty()) {
1333                                                 cerr << "Moving cell content '"
1334                                                      << cells[c]
1335                                                      << "' into the caption cell. "
1336                                                         "This will probably not work."
1337                                                      << endl;
1338                                                 cells[0] += cells[c];
1339                                         }
1340                                 }
1341                                 cells.resize(1);
1342                                 cellinfo[row][col].align      = colinfo[col].align;
1343                                 cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
1344                                 ostringstream os2;
1345                                 parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1346                                 cellinfo[row][col].content += os2.str();
1347                                 // add dummy multicolumn cells
1348                                 for (size_t c = 1; c < colinfo.size(); ++c)
1349                                         cellinfo[row][c].multi = CELL_PART_OF_MULTICOLUMN;
1350                         } else {
1351                                 bool turn = false;
1352                                 int rotate = 0;
1353                                 if (parse.next_token().cs() == "begin") {
1354                                         parse.pushPosition();
1355                                         parse.get_token();
1356                                         string const env = parse.getArg('{', '}');
1357                                         if (env == "sideways" || env == "turn") {
1358                                                 string angle = "90";
1359                                                 if (env == "turn") {
1360                                                         turn = true;
1361                                                         angle = parse.getArg('{', '}');
1362                                                 }
1363                                                 active_environments.push_back(env);
1364                                                 parse.ertEnvironment(env);
1365                                                 active_environments.pop_back();
1366                                                 parse.skip_spaces();
1367                                                 if (!parse.good() && support::isStrInt(angle))
1368                                                         rotate = convert<int>(angle);
1369                                         }
1370                                         parse.popPosition();
1371                                 }
1372                                 cellinfo[row][col].leftlines  = colinfo[col].leftlines;
1373                                 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1374                                 cellinfo[row][col].align      = colinfo[col].align;
1375                                 ostringstream os2;
1376                                 if (rotate != 0) {
1377                                         cellinfo[row][col].rotate = rotate;
1378                                         parse.get_token();
1379                                         active_environments.push_back(parse.getArg('{', '}'));
1380                                         if (turn)
1381                                                 parse.getArg('{', '}');
1382                                         parse_text_in_inset(parse, os2, FLAG_END, false, context);
1383                                         active_environments.pop_back();
1384                                         preamble.registerAutomaticallyLoadedPackage("rotating");
1385                                 } else {
1386                                         parse_text_in_inset(parse, os2, FLAG_CELL, false, context);
1387                                 }
1388                                 cellinfo[row][col].content += os2.str();
1389                         }
1390                 }
1391
1392                 //cerr << "//  handle almost empty last row what we have\n";
1393                 // handle almost empty last row
1394                 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1395                         //cerr << "remove empty last line\n";
1396                         if (rowinfo[row].topline)
1397                                 rowinfo[row - 1].bottomline = true;
1398                         for (size_t col = 0; col < colinfo.size(); ++col)
1399                                 if (cellinfo[row][col].topline)
1400                                         cellinfo[row - 1][col].bottomline = true;
1401                         rowinfo.pop_back();
1402                 }
1403
1404                 ++row;
1405         }
1406
1407         // Now we have the table structure and content in rowinfo, colinfo
1408         // and cellinfo.
1409         // Unfortunately LyX has some limitations that we need to work around.
1410
1411         // Some post work
1412         for (size_t row = 0; row < rowinfo.size(); ++row) {
1413                 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1414                         // Convert cells with special content to multicolumn cells
1415                         // (LyX ignores the special field for non-multicolumn cells).
1416                         if (cellinfo[row][col].multi == CELL_NORMAL &&
1417                             !cellinfo[row][col].special.empty())
1418                                 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1419                         // Add multirow dummy cells
1420                         if (row > 1 && (cellinfo[row - 1][col].multi == CELL_PART_OF_MULTIROW
1421                                         || cellinfo[row - 1][col].multi == CELL_BEGIN_OF_MULTIROW)
1422                                     && cellinfo[row - 1][col].mrxnum > 0) {
1423                                 // add dummy cells for multirow
1424                                 cellinfo[row][col].multi = CELL_PART_OF_MULTIROW;
1425                                 cellinfo[row][col].align = 'c';
1426                                 cellinfo[row][col].mrxnum = cellinfo[row - 1][col].mrxnum - 1;
1427                         }
1428                 }
1429         }
1430
1431         // Distribute lines from rows/columns to cells
1432         // The code was stolen from convert_tablines() in lyx2lyx/lyx_1_6.py.
1433         // Each standard cell inherits the settings of the corresponding
1434         // rowinfo/colinfo. This works because all cells with individual
1435         // settings were converted to multicolumn cells above.
1436         // Each multicolumn cell inherits the settings of the rowinfo/colinfo
1437         // corresponding to the first column of the multicolumn cell (default
1438         // of the multicol package). This works because the special field
1439         // overrides the line fields.
1440         for (size_t row = 0; row < rowinfo.size(); ++row) {
1441                 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1442                         if (cellinfo[row][col].multi == CELL_NORMAL) {
1443                                 cellinfo[row][col].topline = rowinfo[row].topline;
1444                                 cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1445                                 cellinfo[row][col].leftlines = colinfo[col].leftlines;
1446                                 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1447                         } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTICOLUMN) {
1448                                 size_t s = col + 1;
1449                                 while (s < cellinfo[row].size() &&
1450                                        cellinfo[row][s].multi == CELL_PART_OF_MULTICOLUMN)
1451                                         s++;
1452                                 if (s < cellinfo[row].size() &&
1453                                     cellinfo[row][s].multi != CELL_BEGIN_OF_MULTICOLUMN)
1454                                         cellinfo[row][col].rightlines = colinfo[col].rightlines;
1455                                 if (col > 0 && cellinfo[row][col-1].multi == CELL_NORMAL)
1456                                         cellinfo[row][col].leftlines = colinfo[col].leftlines;
1457                         } else if (cellinfo[row][col].multi == CELL_BEGIN_OF_MULTIROW) {
1458                                 size_t s = row + 1;
1459                                 while (s < rowinfo.size() &&
1460                                        cellinfo[s][col].multi == CELL_PART_OF_MULTIROW)
1461                                         s++;
1462                                 if (s < cellinfo[row].size() &&
1463                                     cellinfo[s][col].multi != CELL_BEGIN_OF_MULTIROW)
1464                                         cellinfo[row][col].bottomline = rowinfo[row].bottomline;
1465                                 if (row > 0 && cellinfo[row - 1][col].multi == CELL_NORMAL)
1466                                         cellinfo[row][col].topline = rowinfo[row].topline;
1467                         }
1468                 }
1469         }
1470
1471         if (booktabs)
1472                 preamble.registerAutomaticallyLoadedPackage("booktabs");
1473         if (name == "longtable")
1474                 preamble.registerAutomaticallyLoadedPackage("longtable");
1475         else if (name == "xltabular")
1476                 preamble.registerAutomaticallyLoadedPackage("xltabular");
1477         else if (name == "tabularx")
1478                 preamble.registerAutomaticallyLoadedPackage("tabularx");
1479
1480         //cerr << "// output what we have\n";
1481         // output what we have
1482         size_type cols = colinfo.size();
1483         for (size_t col = 0; col < colinfo.size(); ++col) {
1484                 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1485                         --cols;
1486         }
1487         os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1488            << "\" columns=\"" << cols << "\">\n";
1489         os << "<features"
1490            << write_attribute("rotate", context.tablerotation)
1491            << write_attribute("booktabs", booktabs)
1492            << write_attribute("islongtable", is_long_tabular);
1493         if (is_long_tabular) {
1494                 os << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1495                    << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1496                    << write_attribute("firstHeadEmpty", endfirsthead.empty)
1497                    << write_attribute("headTopDL", endhead.topDL)
1498                    << write_attribute("headBottomDL", endhead.bottomDL)
1499                    << write_attribute("footTopDL", endfoot.topDL)
1500                    << write_attribute("footBottomDL", endfoot.bottomDL)
1501                    << write_attribute("lastFootTopDL", endlastfoot.topDL)
1502                    << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1503                    << write_attribute("lastFootEmpty", endlastfoot.empty);
1504                 if (!halign.empty())
1505                         os << write_attribute("longtabularalignment", halign);
1506         } else
1507                 os << write_attribute("tabularvalignment", tabularvalignment);
1508                    
1509         os << write_attribute("tabularwidth", tabularwidth) << ">\n";
1510
1511         //cerr << "// after header\n";
1512         for (size_t col = 0; col < colinfo.size(); ++col) {
1513                 if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1514                         continue;
1515                 os << "<column alignment=\""
1516                            << verbose_align(colinfo[col].align) << "\"";
1517                 if (colinfo[col].decimal_point != '\0')
1518                         os << " decimal_point=\"" << colinfo[col].decimal_point << "\"";
1519                 os << " valignment=\""
1520                    << verbose_valign(colinfo[col].valign) << "\""
1521                    << write_attribute("width", translate_len(colinfo[col].width))
1522                    << write_attribute("special", colinfo[col].special)
1523                    << write_attribute("varwidth", colinfo[col].varwidth)
1524                    << ">\n";
1525         }
1526         //cerr << "// after cols\n";
1527
1528         for (size_t row = 0; row < rowinfo.size(); ++row) {
1529                 os << "<row"
1530                    << write_attribute("topspace", rowinfo[row].top_space)
1531                    << write_attribute("bottomspace", rowinfo[row].bottom_space)
1532                    << write_attribute("interlinespace", rowinfo[row].interline_space)
1533                    << write_attribute("endhead",
1534                                       rowinfo[row].type == LT_HEAD)
1535                    << write_attribute("endfirsthead",
1536                                       rowinfo[row].type == LT_FIRSTHEAD)
1537                    << write_attribute("endfoot",
1538                                       rowinfo[row].type == LT_FOOT)
1539                    << write_attribute("endlastfoot",
1540                                       rowinfo[row].type == LT_LASTFOOT)
1541                    << write_attribute("newpage", rowinfo[row].newpage)
1542                    << write_attribute("caption", rowinfo[row].caption)
1543                    << ">\n";
1544                 for (size_t col = 0; col < colinfo.size(); ++col) {
1545                         CellInfo const & cell = cellinfo[row][col];
1546                         if (colinfo[col].decimal_point != '\0' && colinfo[col].align != 'd')
1547                                 // These are the second columns in a salign pair. Skip.
1548                                 continue;
1549                         os << "<cell";
1550                         if ((cell.multi == CELL_BEGIN_OF_MULTICOLUMN
1551                                     || cell.multi == CELL_PART_OF_MULTICOLUMN)
1552                                    && colinfo[col].align != 'd')
1553                                 os << " multicolumn=\"" << cell.multi << "\"";
1554                         if (cell.multi == CELL_BEGIN_OF_MULTIROW
1555                             || cell.multi == CELL_PART_OF_MULTIROW)
1556                                 os << " multirow=\"" << cell.multi << "\"";
1557                         os << " alignment=\"" << verbose_align(cell.align)
1558                            << "\""
1559                            << " valignment=\"" << verbose_valign(cell.valign)
1560                            << "\""
1561                            << write_attribute("topline", cell.topline)
1562                            << write_attribute("bottomline", cell.bottomline)
1563                            << write_attribute("leftline", cell.leftlines > 0)
1564                            << write_attribute("rightline", cell.rightlines > 0)
1565                            << write_attribute("rotate", cell.rotate)
1566                            << write_attribute("mroffset", cell.mroffset);
1567                         //cerr << "\nrow: " << row << " col: " << col;
1568                         //if (cell.topline)
1569                         //      cerr << " topline=\"true\"";
1570                         //if (cell.bottomline)
1571                         //      cerr << " bottomline=\"true\"";
1572                         os << " usebox=\"none\""
1573                            << write_attribute("width", translate_len(cell.width));
1574                         if (cell.multi != CELL_NORMAL)
1575                                 os << write_attribute("special", cell.special);
1576                         os << ">"
1577                            << "\n\\begin_inset Text\n"
1578                            << cell.content
1579                            << "\n\\end_inset\n"
1580                            << "</cell>\n";
1581                 }
1582                 os << "</row>\n";
1583         }
1584
1585         os << "</lyxtabular>\n";
1586 }
1587
1588
1589
1590
1591 // }])
1592
1593
1594 } // namespace lyx