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