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