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