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