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