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