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