]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/table.C
fix tex2lyx table column specification parsing
[lyx.git] / src / tex2lyx / table.C
1 /**
2  * \file table.C
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 "support/convert.h"
20 #include "support/lstrings.h"
21
22 #include <cctype>
23 #include <fstream>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 #include <map>
28
29 using std::cerr;
30 using std::endl;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34 using std::string;
35 using std::vector;
36
37
38 // filled in preamble.C
39 std::map<char, int> special_columns;
40
41
42 namespace {
43
44 class ColInfo {
45 public:
46         ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
47         /// column alignment
48         char align;
49         /// vertical alignment
50         char valign;
51         /// column width
52         string width;
53         /// special column alignment
54         string special;
55         /// number of lines on the right
56         int rightlines;
57         /// number of lines on the left
58         int leftlines;
59 };
60
61
62 /// row type for longtables
63 enum LTRowType
64 {
65         /// normal row
66         LT_NORMAL,
67         /// part of head
68         LT_HEAD,
69         /// part of head on first page
70         LT_FIRSTHEAD,
71         /// part of foot
72         LT_FOOT,
73         /// part of foot on last page
74         LT_LASTFOOT
75 };
76
77
78 class RowInfo {
79 public:
80         RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
81                     newpage(false) {}
82         /// horizontal line above
83         bool topline;
84         /// horizontal line below
85         bool bottomline;
86         /// These are for longtabulars only
87         /// row type (head, foot, firsthead etc.)
88         LTRowType type;
89         /// row for a pagebreak
90         bool newpage;
91 };
92
93
94 enum Multicolumn {
95         /// A normal cell
96         CELL_NORMAL = 0,
97         /// A multicolumn cell. The number of columns is <tt>1 + number
98         /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
99         CELL_BEGIN_OF_MULTICOLUMN,
100         /// This is a dummy cell (part of a multicolumn cell)
101         CELL_PART_OF_MULTICOLUMN
102 };
103
104
105 class CellInfo {
106 public:
107         CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
108                      leftlines(0), rightlines(0), topline(false),
109                      bottomline(false), rotate(false) {}
110         /// cell content
111         string content;
112         /// multicolumn flag
113         Multicolumn multi;
114         /// cell alignment
115         char align;
116         /// vertical cell alignment
117         char valign;
118         /// number of lines on the left
119         int leftlines;
120         /// number of lines on the right
121         int rightlines;
122         /// do we have a line above?
123         bool topline;
124         /// do we have a line below?
125         bool bottomline;
126         /// is the cell rotated?
127         bool rotate;
128         /// width for multicolumn cells
129         string width;
130         /// special formatting for multicolumn cells
131         string special;
132 };
133
134
135 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
136 inline char const * verbose_align(char c)
137 {
138         switch (c) {
139         case 'c':
140                 return "center";
141         case 'r':
142                 return "right";
143         case 'l':
144                 return "left";
145         default:
146                 return "none";
147         }
148 }
149
150
151 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
152 inline char const * verbose_valign(char c)
153 {
154         // The default value for no special alignment is "top".
155         switch (c) {
156         case 'm':
157                 return "middle";
158         case 'b':
159                 return "bottom";
160         case 'p':
161         default:
162                 return "top";
163         }
164 }
165
166
167 // stripped down from tabluar.C. We use it currently only for bools and
168 // strings
169 string const write_attribute(string const & name, bool const & b)
170 {
171         // we write only true attribute values so we remove a bit of the
172         // file format bloat for tabulars.
173         return b ? ' ' + name + "=\"true\"" : string();
174 }
175
176
177 string const write_attribute(string const & name, string const & s)
178 {
179         return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
180 }
181
182
183 /*! rather brutish way to code table structure in a string:
184
185 \verbatim
186   \begin{tabular}{ccc}
187     1 & 2 & 3\\ \hline
188     \multicolumn{2}{c}{4} & 5 //
189     6 & 7 \\
190     8 \endhead
191   \end{tabular}
192 \endverbatim
193
194  gets "translated" to:
195
196 \verbatim
197          HLINE 1                     TAB 2 TAB 3 HLINE          HLINE LINE
198   \hline HLINE \multicolumn{2}{c}{4} TAB 5       HLINE          HLINE LINE
199          HLINE 6                     TAB 7       HLINE          HLINE LINE
200          HLINE 8                                 HLINE \endhead HLINE LINE
201 \endverbatim
202  */
203
204 char const TAB   = '\001';
205 char const LINE  = '\002';
206 char const HLINE = '\004';
207
208
209 /*!
210  * Move the information in leftlines, rightlines, align and valign to the
211  * special field. This is necessary if the special field is not empty,
212  * because LyX ignores leftlines, rightlines, align and valign in this case.
213  */
214 void ci2special(ColInfo & ci)
215 {
216         if (ci.width.empty() && ci.align == 'n')
217                 // The alignment setting is already in special, since
218                 // handle_colalign() never stores ci with these settings
219                 // and ensures that leftlines == 0 and rightlines == 0 in
220                 // this case.
221                 return;
222
223         if (!ci.width.empty()) {
224                 switch (ci.align) {
225                 case 'l':
226                         ci.special += ">{\\raggedright}";
227                         break;
228                 case 'r':
229                         ci.special += ">{\\raggedleft}";
230                         break;
231                 case 'c':
232                         ci.special += ">{\\centering}";
233                         break;
234                 }
235                 if (ci.valign == 'n')
236                         ci.special += 'p';
237                 else
238                         ci.special += ci.valign;
239                 ci.special += '{' + ci.width + '}';
240                 ci.width.erase();
241         } else
242                 ci.special += ci.align;
243
244         for (int i = 0; i < ci.leftlines; ++i)
245                 ci.special.insert(0, "|");
246         for (int i = 0; i < ci.rightlines; ++i)
247                 ci.special += '|';
248         ci.leftlines = 0;
249         ci.rightlines = 0;
250         ci.align = 'n';
251         ci.valign = 'n';
252 }
253
254
255 /*!
256  * Handle column specifications for tabulars and multicolumns.
257  * The next token of the parser \p p must be an opening brace, and we read
258  * everything until the matching closing brace.
259  * The resulting column specifications are filled into \p colinfo. This is
260  * in an intermediate form. fix_colalign() makes it suitable for LyX output.
261  */
262 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
263                      ColInfo const & start)
264 {
265         if (p.get_token().cat() != catBegin)
266                 cerr << "Wrong syntax for table column alignment.\n"
267                         "Expected '{', got '" << p.curr_token().asInput()
268                      << "'.\n";
269
270         ColInfo next = start;
271         for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
272              t = p.get_token()) {
273 #ifdef FILEDEBUG
274                 cerr << "t: " << t << "  c: '" << t.character() << "'\n";
275 #endif
276
277                 // We cannot handle comments here
278                 if (t.cat() == catComment) {
279                         if (t.cs().empty()) {
280                                 // "%\n" combination
281                                 p.skip_spaces();
282                         } else
283                                 cerr << "Ignoring comment: " << t.asInput();
284                         continue;
285                 }
286
287                 switch (t.character()) {
288                         case 'c':
289                         case 'l':
290                         case 'r':
291                                 // new column, horizontal aligned
292                                 next.align = t.character();
293                                 if (!next.special.empty())
294                                         ci2special(next);
295                                 colinfo.push_back(next);
296                                 next = ColInfo();
297                                 break;
298                         case 'p':
299                         case 'b':
300                         case 'm':
301                                 // new column, vertical aligned box
302                                 next.valign = t.character();
303                                 next.width = p.verbatim_item();
304                                 if (!next.special.empty())
305                                         ci2special(next);
306                                 colinfo.push_back(next);
307                                 next = ColInfo();
308                                 break;
309                         case '|':
310                                 // vertical rule
311                                 if (colinfo.empty()) {
312                                         if (next.special.empty())
313                                                 ++next.leftlines;
314                                         else
315                                                 next.special += '|';
316                                 } else if (colinfo.back().special.empty())
317                                         ++colinfo.back().rightlines;
318                                 else if (next.special.empty())
319                                         ++next.leftlines;
320                                 else
321                                         colinfo.back().special += '|';
322                                 break;
323                         case '>': {
324                                 // text before the next column
325                                 string const s = trim(p.verbatim_item());
326                                 if (next.special.empty() &&
327                                     next.align == 'n') {
328                                         // Maybe this can be converted to a
329                                         // horizontal alignment setting for
330                                         // fixed width columns
331                                         if (s == "\\raggedleft")
332                                                 next.align = 'r';
333                                         else if (s == "\\raggedright")
334                                                 next.align = 'l';
335                                         else if (s == "\\centering")
336                                                 next.align = 'c';
337                                         else
338                                                 next.special = ">{" + s + '}';
339                                 } else
340                                         next.special += ">{" + s + '}';
341                                 break;
342                         }
343                         case '<': {
344                                 // text after the last column
345                                 string const s = trim(p.verbatim_item());
346                                 if (colinfo.empty())
347                                         // This is not possible in LaTeX.
348                                         cerr << "Ignoring separator '<{"
349                                              << s << "}'." << endl;
350                                 else {
351                                         ColInfo & ci = colinfo.back();
352                                         ci2special(ci);
353                                         ci.special += "<{" + s + '}';
354                                 }
355                                 break;
356                         }
357                         case '*': {
358                                 // *{n}{arg} means 'n' columns of type 'arg'
359                                 string const num = p.verbatim_item();
360                                 string const arg = p.verbatim_item();
361                                 size_t const n = convert<unsigned int>(num);
362                                 if (!arg.empty() && n > 0) {
363                                         string s("{");
364                                         for (size_t i = 0; i < n; ++i)
365                                                 s += arg;
366                                         s += '}';
367                                         Parser p2(s);
368                                         handle_colalign(p2, colinfo, next);
369                                         next = ColInfo();
370                                 } else {
371                                         cerr << "Ignoring column specification"
372                                                 " '*{" << num << "}{"
373                                              << arg << "}'." << endl;
374                                 }
375                                 break;
376                         }
377                         case '@':
378                                 // text instead of the column spacing
379                         case '!':
380                                 // text in addition to the column spacing
381                                 next.special += t.character();
382                                 next.special += '{' + p.verbatim_item() + '}';
383                                 break;
384                         default:
385                                 // try user defined column types
386                                 if (special_columns.find(t.character()) !=
387                                     special_columns.end()) {
388                                         ci2special(next);
389                                         next.special += t.character();
390                                         int const nargs =
391                                                 special_columns[t.character()];
392                                         for (int i = 0; i < nargs; ++i)
393                                                 next.special += '{' +
394                                                         p.verbatim_item() +
395                                                         '}';
396                                         colinfo.push_back(next);
397                                         next = ColInfo();
398                                 } else
399                                         cerr << "Ignoring column specification"
400                                                 " '" << t << "'." << endl;
401                                 break;
402                         }
403         }
404
405         // Maybe we have some column separators that need to be added to the
406         // last column?
407         ci2special(next);
408         if (!next.special.empty()) {
409                 ColInfo & ci = colinfo.back();
410                 ci2special(ci);
411                 ci.special += next.special;
412                 next.special.erase();
413         }
414 }
415
416
417 /*!
418  * Move the left and right lines and alignment settings of the column \p ci
419  * to the special field if necessary.
420  */
421 void fix_colalign(ColInfo & ci)
422 {
423         if (ci.leftlines > 1 || ci.rightlines > 1)
424                 ci2special(ci);
425 }
426
427
428 /*!
429  * LyX can't handle more than one vertical line at the left or right side
430  * of a column.
431  * This function moves the left and right lines and alignment settings of all
432  * columns in \p colinfo to the special field if necessary.
433  */
434 void fix_colalign(vector<ColInfo> & colinfo)
435 {
436         // Try to move extra leftlines to the previous column.
437         // We do this only if both special fields are empty, otherwise we
438         // can't tell wether the result will be the same.
439         for (size_t col = 0; col < colinfo.size(); ++col) {
440                 if (colinfo[col].leftlines > 1 &&
441                     colinfo[col].special.empty() && col > 0 &&
442                     colinfo[col - 1].rightlines == 0 &&
443                     colinfo[col - 1].special.empty()) {
444                         ++colinfo[col - 1].rightlines;
445                         --colinfo[col].leftlines;
446                 }
447         }
448         // Try to move extra rightlines to the next column
449         for (size_t col = 0; col < colinfo.size(); ++col) {
450                 if (colinfo[col].rightlines > 1 &&
451                     colinfo[col].special.empty() &&
452                     col < colinfo.size() - 1 &&
453                     colinfo[col + 1].leftlines == 0 &&
454                     colinfo[col + 1].special.empty()) {
455                         ++colinfo[col + 1].leftlines;
456                         --colinfo[col].rightlines;
457                 }
458         }
459         // Move the lines and alignment settings to the special field if
460         // necessary
461         for (size_t col = 0; col < colinfo.size(); ++col)
462                 fix_colalign(colinfo[col]);
463 }
464
465
466 /*!
467  * Parse hlines and similar stuff.
468  * \returns wether the token \p t was parsed
469  */
470 bool parse_hlines(Parser & p, Token const & t, string & hlines,
471                   bool is_long_tabular)
472 {
473         BOOST_ASSERT(t.cat() == catEscape);
474
475         if (t.cs() == "hline")
476                 hlines += "\\hline";
477
478         else if (t.cs() == "cline")
479                 hlines += "\\cline{" + p.verbatim_item() + '}';
480
481         else if (is_long_tabular && t.cs() == "newpage")
482                 hlines += "\\newpage";
483
484         else
485                 return false;
486
487         return true;
488 }
489
490
491 /// Position in a row
492 enum RowPosition {
493         /// At the very beginning, before the first token
494         ROW_START,
495         /// After the first token and before any column token
496         IN_HLINES_START,
497         /// After the first column token. Comments and whitespace are only
498         /// treated as tokens in this position
499         IN_COLUMNS,
500         /// After the first non-column token at the end
501         IN_HLINES_END
502 };
503
504
505 /*!
506  * Parse table structure.
507  * We parse tables in a two-pass process: This function extracts the table
508  * structure (rows, columns, hlines etc.), but does not change the cell
509  * content. The cell content is parsed in a second step in handle_tabular().
510  */
511 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
512                  RowPosition & pos, unsigned flags)
513 {
514         // table structure commands such as \hline
515         string hlines;
516
517         // comments that occur at places where we can't handle them
518         string comments;
519
520         while (p.good()) {
521                 Token const & t = p.get_token();
522
523 #ifdef FILEDEBUG
524                 cerr << "t: " << t << " flags: " << flags << "\n";
525 #endif
526
527                 // comments and whitespace in hlines
528                 switch (pos) {
529                 case ROW_START:
530                 case IN_HLINES_START:
531                 case IN_HLINES_END:
532                         if (t.cat() == catComment) {
533                                 if (t.cs().empty())
534                                         // line continuation
535                                         p.skip_spaces();
536                                 else
537                                         // We can't handle comments here,
538                                         // store them for later use
539                                         comments += t.asInput();
540                                 continue;
541                         } else if (t.cat() == catSpace ||
542                                    t.cat() == catNewline) {
543                                 // whitespace is irrelevant here, we
544                                 // need to recognize hline stuff
545                                 p.skip_spaces();
546                                 continue;
547                         }
548                         break;
549                 case IN_COLUMNS:
550                         break;
551                 }
552
553                 // We need to handle structure stuff first in order to
554                 // determine wether we need to output a HLINE separator
555                 // before the row or not.
556                 if (t.cat() == catEscape) {
557                         if (parse_hlines(p, t, hlines, is_long_tabular)) {
558                                 switch (pos) {
559                                 case ROW_START:
560                                         pos = IN_HLINES_START;
561                                         break;
562                                 case IN_COLUMNS:
563                                         pos = IN_HLINES_END;
564                                         break;
565                                 case IN_HLINES_START:
566                                 case IN_HLINES_END:
567                                         break;
568                                 }
569                                 continue;
570                         }
571
572                         else if (t.cs() == "tabularnewline" ||
573                                  t.cs() == "\\" ||
574                                  t.cs() == "cr") {
575                                 if (t.cs() == "cr")
576                                         cerr << "Warning: Converting TeX "
577                                                 "'\\cr' to LaTeX '\\\\'."
578                                              << endl;
579                                 // stuff before the line break
580                                 os << comments << HLINE << hlines << HLINE
581                                    << LINE;
582                                 //cerr << "hlines: " << hlines << endl;
583                                 hlines.erase();
584                                 comments.erase();
585                                 pos = ROW_START;
586                                 continue;
587                         }
588
589                         else if (is_long_tabular &&
590                                  (t.cs() == "endhead" ||
591                                   t.cs() == "endfirsthead" ||
592                                   t.cs() == "endfoot" ||
593                                   t.cs() == "endlastfoot")) {
594                                 hlines += t.asInput();
595                                 switch (pos) {
596                                 case IN_COLUMNS:
597                                 case IN_HLINES_END:
598                                         // these commands are implicit line
599                                         // breaks
600                                         os << comments << HLINE << hlines
601                                            << HLINE << LINE;
602                                         hlines.erase();
603                                         comments.erase();
604                                         pos = ROW_START;
605                                         break;
606                                 case ROW_START:
607                                         pos = IN_HLINES_START;
608                                         break;
609                                 case IN_HLINES_START:
610                                         break;
611                                 }
612                                 continue;
613                         }
614
615                 }
616
617                 // We need a HLINE separator if we either have no hline
618                 // stuff at all and are just starting a row or if we just
619                 // got the first non-hline token.
620                 switch (pos) {
621                 case ROW_START:
622                         // no hline tokens exist, first token at row start
623                 case IN_HLINES_START:
624                         // hline tokens exist, first non-hline token at row
625                         // start
626                         os << hlines << HLINE << comments;
627                         hlines.erase();
628                         comments.erase();
629                         pos = IN_COLUMNS;
630                         break;
631                 case IN_HLINES_END:
632                         // Oops, there is still cell content after hline
633                         // stuff. This does not work in LaTeX, so we ignore
634                         // the hlines.
635                         cerr << "Ignoring '" << hlines << "' in a cell"
636                              << endl;
637                         os << comments;
638                         hlines.erase();
639                         comments.erase();
640                         pos = IN_COLUMNS;
641                         break;
642                 case IN_COLUMNS:
643                         break;
644                 }
645
646                 // If we come here we have normal cell content
647                 //
648                 // cat codes
649                 //
650                 if (t.cat() == catMath) {
651                         // we are inside some text mode thingy, so opening new math is allowed
652                         Token const & n = p.get_token();
653                         if (n.cat() == catMath) {
654                                 // TeX's $$...$$ syntax for displayed math
655                                 os << "\\[";
656                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
657                                 os << "\\]";
658                                 p.get_token(); // skip the second '$' token
659                         } else {
660                                 // simple $...$  stuff
661                                 p.putback();
662                                 os << '$';
663                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
664                                 os << '$';
665                         }
666                 }
667
668                 else if (t.cat() == catSpace || t.cat() == catNewline)
669                                 os << t.cs();
670
671                 else if (t.cat() == catLetter ||
672                                t.cat() == catSuper ||
673                                t.cat() == catSub ||
674                                t.cat() == catOther ||
675                                t.cat() == catActive ||
676                                t.cat() == catParameter)
677                         os << t.character();
678
679                 else if (t.cat() == catBegin) {
680                         os << '{';
681                         parse_table(p, os, is_long_tabular, pos,
682                                     FLAG_BRACE_LAST);
683                         os << '}';
684                 }
685
686                 else if (t.cat() == catEnd) {
687                         if (flags & FLAG_BRACE_LAST)
688                                 return;
689                         cerr << "unexpected '}'\n";
690                 }
691
692                 else if (t.cat() == catAlign) {
693                         os << TAB;
694                         p.skip_spaces();
695                 }
696
697                 else if (t.cat() == catComment)
698                         os << t.asInput();
699
700                 else if (t.cs() == "(") {
701                         os << "\\(";
702                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
703                         os << "\\)";
704                 }
705
706                 else if (t.cs() == "[") {
707                         os << "\\[";
708                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
709                         os << "\\]";
710                 }
711
712                 else if (t.cs() == "begin") {
713                         string const name = p.getArg('{', '}');
714                         active_environments.push_back(name);
715                         os << "\\begin{" << name << '}';
716                         if (is_math_env(name)) {
717                                 parse_math(p, os, FLAG_END, MATH_MODE);
718                         } else {
719                                 parse_table(p, os, is_long_tabular, pos,
720                                             FLAG_END);
721                         }
722                         os << "\\end{" << name << '}';
723                         active_environments.pop_back();
724                 }
725
726                 else if (t.cs() == "end") {
727                         if (flags & FLAG_END) {
728                                 // eat environment name
729                                 string const name = p.getArg('{', '}');
730                                 if (name != active_environment())
731                                         p.error("\\end{" + name + "} does not match \\begin{"
732                                                 + active_environment() + "}");
733                                 return;
734                         }
735                         p.error("found 'end' unexpectedly");
736                 }
737
738                 else
739                         os << t.asInput();
740         }
741
742         // We can have comments if the last line is incomplete
743         os << comments;
744
745         // We can have hline stuff if the last line is incomplete
746         if (!hlines.empty()) {
747                 // this does not work in LaTeX, so we ignore it
748                 cerr << "Ignoring '" << hlines << "' at end of tabular"
749                      << endl;
750         }
751 }
752
753
754 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
755 {
756         ri.topline = true;
757         for (size_t col = 0; col < ci.size(); ++col)
758                 ci[col].topline = true;
759 }
760
761
762 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
763 {
764         ri.bottomline = true;
765         for (size_t col = 0; col < ci.size(); ++col)
766                 ci[col].bottomline = true;
767 }
768
769
770 } // anonymous namespace
771
772
773 void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
774                     Context & context)
775 {
776         string posopts = p.getOpt();
777         if (!posopts.empty()) {
778                 // FIXME: Convert this to ERT
779                 if (is_long_tabular)
780                         cerr << "horizontal longtable";
781                 else
782                         cerr << "vertical tabular";
783                 cerr << " positioning '" << posopts << "' ignored\n";
784         }
785
786         vector<ColInfo> colinfo;
787
788         // handle column formatting
789         handle_colalign(p, colinfo, ColInfo());
790         fix_colalign(colinfo);
791
792         // first scan of cells
793         // use table mode to keep it minimal-invasive
794         // not exactly what's TeX doing...
795         vector<string> lines;
796         ostringstream ss;
797         RowPosition rowpos = ROW_START;
798         parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
799         split(ss.str(), lines, LINE);
800
801         vector< vector<CellInfo> > cellinfo(lines.size());
802         vector<RowInfo> rowinfo(lines.size());
803
804         // split into rows
805         //cerr << "// split into rows\n";
806         for (size_t row = 0; row < rowinfo.size(); ++row) {
807
808                 // init row
809                 cellinfo[row].resize(colinfo.size());
810
811                 // split row
812                 vector<string> dummy;
813                 //cerr << "\n########### LINE: " << lines[row] << "########\n";
814                 split(lines[row], dummy, HLINE);
815
816                 // handle horizontal line fragments
817                 // we do only expect this for a last line without '\\'
818                 if (dummy.size() != 3) {
819                         if ((dummy.size() != 1 && dummy.size() != 2) ||
820                             row != rowinfo.size() - 1)
821                                 cerr << "unexpected dummy size: " << dummy.size()
822                                         << " content: " << lines[row] << "\n";
823                         dummy.resize(3);
824                 }
825                 lines[row] = dummy[1];
826
827                 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
828                 //cerr << "line: " << row << " below 2: " << dummy[2] <<  "\n";
829                 //cerr << "line: " << row << " cells 1: " << dummy[1] <<  "\n";
830
831                 for (int i = 0; i <= 2; i += 2) {
832                         //cerr << "   reading from line string '" << dummy[i] << "'\n";
833                         Parser p1(dummy[i]);
834                         while (p1.good()) {
835                                 Token t = p1.get_token();
836                                 //cerr << "read token: " << t << "\n";
837                                 if (t.cs() == "hline") {
838                                         if (i == 0) {
839                                                 if (rowinfo[row].topline) {
840                                                         if (row > 0) // extra bottomline above
841                                                                 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
842                                                         else
843                                                                 cerr << "dropping extra hline\n";
844                                                         //cerr << "below row: " << row-1 << endl;
845                                                 } else {
846                                                         handle_hline_above(rowinfo[row], cellinfo[row]);
847                                                         //cerr << "above row: " << row << endl;
848                                                 }
849                                         } else {
850                                                 //cerr << "below row: " << row << endl;
851                                                 handle_hline_below(rowinfo[row], cellinfo[row]);
852                                         }
853                                 } else if (t.cs() == "cline") {
854                                         string arg = p1.verbatim_item();
855                                         //cerr << "read cline arg: '" << arg << "'\n";
856                                         vector<string> t;
857                                         split(arg, t, '-');
858                                         t.resize(2);
859                                         size_t from = convert<unsigned int>(t[0]);
860                                         if (from == 0)
861                                                 cerr << "Could not parse "
862                                                         "cline start column."
863                                                      << endl;
864                                         else
865                                                 // 1 based index -> 0 based
866                                                 --from;
867                                         if (from >= colinfo.size()) {
868                                                 cerr << "cline starts at non "
869                                                         "existing column "
870                                                      << (from + 1) << endl;
871                                                 from = colinfo.size() - 1;
872                                         }
873                                         size_t to = convert<unsigned int>(t[1]);
874                                         if (to == 0)
875                                                 cerr << "Could not parse "
876                                                         "cline end column."
877                                                      << endl;
878                                         else
879                                                 // 1 based index -> 0 based
880                                                 --to;
881                                         if (to >= colinfo.size()) {
882                                                 cerr << "cline ends at non "
883                                                         "existing column "
884                                                      << (to + 1) << endl;
885                                                 to = colinfo.size() - 1;
886                                         }
887                                         for (size_t col = from; col <= to; ++col) {
888                                                 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
889                                                 if (i == 0) {
890                                                         rowinfo[row].topline = true;
891                                                         cellinfo[row][col].topline = true;
892                                                 } else {
893                                                         rowinfo[row].bottomline = true;
894                                                         cellinfo[row][col].bottomline = true;
895                                                 }
896                                         }
897                                 } else if (t.cs() == "endhead") {
898                                         if (i > 0)
899                                                 rowinfo[row].type = LT_HEAD;
900                                         for (int r = row - 1; r >= 0; --r) {
901                                                 if (rowinfo[r].type != LT_NORMAL)
902                                                         break;
903                                                 rowinfo[r].type = LT_HEAD;
904                                         }
905                                 } else if (t.cs() == "endfirsthead") {
906                                         if (i > 0)
907                                                 rowinfo[row].type = LT_FIRSTHEAD;
908                                         for (int r = row - 1; r >= 0; --r) {
909                                                 if (rowinfo[r].type != LT_NORMAL)
910                                                         break;
911                                                 rowinfo[r].type = LT_FIRSTHEAD;
912                                         }
913                                 } else if (t.cs() == "endfoot") {
914                                         if (i > 0)
915                                                 rowinfo[row].type = LT_FOOT;
916                                         for (int r = row - 1; r >= 0; --r) {
917                                                 if (rowinfo[r].type != LT_NORMAL)
918                                                         break;
919                                                 rowinfo[r].type = LT_FOOT;
920                                         }
921                                 } else if (t.cs() == "endlastfoot") {
922                                         if (i > 0)
923                                                 rowinfo[row].type = LT_LASTFOOT;
924                                         for (int r = row - 1; r >= 0; --r) {
925                                                 if (rowinfo[r].type != LT_NORMAL)
926                                                         break;
927                                                 rowinfo[r].type = LT_LASTFOOT;
928                                         }
929                                 } else if (t.cs() == "newpage") {
930                                         if (i == 0) {
931                                                 if (row > 0)
932                                                         rowinfo[row - 1].newpage = true;
933                                                 else
934                                                         // This does not work in LaTeX
935                                                         cerr << "Ignoring "
936                                                                 "'\\newpage' "
937                                                                 "before rows."
938                                                              << endl;
939                                         } else
940                                                 rowinfo[row].newpage = true;
941                                 } else {
942                                         cerr << "unexpected line token: " << t << endl;
943                                 }
944                         }
945                 }
946
947                 // split into cells
948                 vector<string> cells;
949                 split(lines[row], cells, TAB);
950                 for (size_t col = 0, cell = 0; cell < cells.size();
951                      ++col, ++cell) {
952                         //cerr << "cell content: '" << cells[cell] << "'\n";
953                         if (col >= colinfo.size()) {
954                                 // This does not work in LaTeX
955                                 cerr << "Ignoring extra cell '"
956                                      << cells[cell] << "'." << endl;
957                                 continue;
958                         }
959                         Parser p(cells[cell]);
960                         p.skip_spaces();
961                         //cells[cell] << "'\n";
962                         if (p.next_token().cs() == "multicolumn") {
963                                 // how many cells?
964                                 p.get_token();
965                                 size_t const ncells =
966                                         convert<unsigned int>(p.verbatim_item());
967
968                                 // special cell properties alignment
969                                 vector<ColInfo> t;
970                                 handle_colalign(p, t, ColInfo());
971                                 ColInfo & ci = t.front();
972
973                                 // The logic of LyX for multicolumn vertical
974                                 // lines is too complicated to reproduce it
975                                 // here (see LyXTabular::TeXCellPreamble()).
976                                 // Therefore we simply put everything in the
977                                 // special field.
978                                 ci2special(ci);
979
980                                 cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
981                                 cellinfo[row][col].align      = ci.align;
982                                 cellinfo[row][col].special    = ci.special;
983                                 cellinfo[row][col].leftlines  = ci.leftlines;
984                                 cellinfo[row][col].rightlines = ci.rightlines;
985                                 ostringstream os;
986                                 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
987                                 if (!cellinfo[row][col].content.empty()) {
988                                         // This may or may not work in LaTeX,
989                                         // but it does not work in LyX.
990                                         // FIXME: Handle it correctly!
991                                         cerr << "Moving cell content '"
992                                              << cells[cell]
993                                              << "' into a multicolumn cell. "
994                                                 "This will probably not work."
995                                              << endl;
996                                 }
997                                 cellinfo[row][col].content += os.str();
998
999                                 // add dummy cells for multicol
1000                                 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
1001                                         ++col;
1002                                         cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1003                                         cellinfo[row][col].align = 'c';
1004                                 }
1005
1006                         } else {
1007                                 cellinfo[row][col].leftlines  = colinfo[col].leftlines;
1008                                 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1009                                 cellinfo[row][col].align      = colinfo[col].align;
1010                                 ostringstream os;
1011                                 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1012                                 cellinfo[row][col].content += os.str();
1013                         }
1014                 }
1015
1016                 //cerr << "//  handle almost empty last row what we have\n";
1017                 // handle almost empty last row
1018                 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1019                         //cerr << "remove empty last line\n";
1020                         if (rowinfo[row].topline)
1021                                 rowinfo[row - 1].bottomline = true;
1022                         for (size_t col = 0; col < colinfo.size(); ++col)
1023                                 if (cellinfo[row][col].topline)
1024                                         cellinfo[row - 1][col].bottomline = true;
1025                         rowinfo.pop_back();
1026                 }
1027         }
1028
1029         // Now we have the table structure and content in rowinfo, colinfo
1030         // and cellinfo.
1031         // Unfortunately LyX has some limitations that we need to work around.
1032
1033         // Convert cells with special content to multicolumn cells
1034         // (LyX ignores the special field for non-multicolumn cells).
1035         for (size_t row = 0; row < rowinfo.size(); ++row) {
1036                 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1037                         if (cellinfo[row][col].multi == CELL_NORMAL &&
1038                             !cellinfo[row][col].special.empty())
1039                                 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1040                 }
1041         }
1042
1043         //cerr << "// output what we have\n";
1044         // output what we have
1045         os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1046            << "\" columns=\"" << colinfo.size() << "\">\n";
1047         os << "<features"
1048            << write_attribute("rotate", false)
1049            << write_attribute("islongtable", is_long_tabular)
1050            << ">\n";
1051
1052         //cerr << "// after header\n";
1053         for (size_t col = 0; col < colinfo.size(); ++col) {
1054                 os << "<column alignment=\""
1055                    << verbose_align(colinfo[col].align) << "\""
1056                    << " valignment=\""
1057                    << verbose_valign(colinfo[col].valign) << "\""
1058                    << write_attribute("leftline", colinfo[col].leftlines > 0)
1059                    << write_attribute("rightline", colinfo[col].rightlines > 0)
1060                    << write_attribute("width", colinfo[col].width)
1061                    << write_attribute("special", colinfo[col].special)
1062                    << ">\n";
1063         }
1064         //cerr << "// after cols\n";
1065
1066         for (size_t row = 0; row < rowinfo.size(); ++row) {
1067                 os << "<row"
1068                    << write_attribute("topline", rowinfo[row].topline)
1069                    << write_attribute("bottomline", rowinfo[row].bottomline)
1070                    << write_attribute("endhead",
1071                                       rowinfo[row].type == LT_HEAD)
1072                    << write_attribute("endfirsthead",
1073                                       rowinfo[row].type == LT_FIRSTHEAD)
1074                    << write_attribute("endfoot",
1075                                       rowinfo[row].type == LT_FOOT)
1076                    << write_attribute("endlastfoot",
1077                                       rowinfo[row].type == LT_LASTFOOT)
1078                    << write_attribute("newpage", rowinfo[row].newpage)
1079                    << ">\n";
1080                 for (size_t col = 0; col < colinfo.size(); ++col) {
1081                         CellInfo const & cell = cellinfo[row][col];
1082                         os << "<cell";
1083                         if (cell.multi != CELL_NORMAL)
1084                                 os << " multicolumn=\"" << cell.multi << "\"";
1085                         os << " alignment=\"" << verbose_align(cell.align)
1086                            << "\""
1087                            << " valignment=\"" << verbose_valign(cell.valign)
1088                            << "\""
1089                            << write_attribute("topline", cell.topline)
1090                            << write_attribute("bottomline", cell.bottomline)
1091                            << write_attribute("leftline", cell.leftlines > 0)
1092                            << write_attribute("rightline", cell.rightlines > 0)
1093                            << write_attribute("rotate", cell.rotate);
1094                         //cerr << "\nrow: " << row << " col: " << col;
1095                         //if (cell.topline)
1096                         //      cerr << " topline=\"true\"";
1097                         //if (cell.bottomline)
1098                         //      cerr << " bottomline=\"true\"";
1099                         os << " usebox=\"none\""
1100                            << write_attribute("width", cell.width);
1101                         if (cell.multi != CELL_NORMAL)
1102                                 os << write_attribute("special", cell.special);
1103                         os << ">"
1104                            << "\n\\begin_inset Text\n"
1105                            << cell.content
1106                            << "\n\\end_inset\n"
1107                            << "</cell>\n";
1108                 }
1109                 os << "</row>\n";
1110         }
1111
1112         os << "</lyxtabular>\n";
1113 }
1114
1115
1116
1117
1118 // }])