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