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