]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/table.C
Whitespace, only whitespace. s/ +$//
[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                                 // This does only work because parse_math outputs TeX
657                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
658                                 os << "\\]";
659                                 p.get_token(); // skip the second '$' token
660                         } else {
661                                 // simple $...$  stuff
662                                 p.putback();
663                                 os << '$';
664                                 // This does only work because parse_math outputs TeX
665                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
666                                 os << '$';
667                         }
668                 }
669
670                 else if (t.cat() == catSpace || t.cat() == catNewline)
671                                 os << t.cs();
672
673                 else if (t.cat() == catLetter ||
674                                t.cat() == catSuper ||
675                                t.cat() == catSub ||
676                                t.cat() == catOther ||
677                                t.cat() == catActive ||
678                                t.cat() == catParameter)
679                         os << t.character();
680
681                 else if (t.cat() == catBegin) {
682                         os << '{';
683                         parse_table(p, os, is_long_tabular, pos,
684                                     FLAG_BRACE_LAST);
685                         os << '}';
686                 }
687
688                 else if (t.cat() == catEnd) {
689                         if (flags & FLAG_BRACE_LAST)
690                                 return;
691                         cerr << "unexpected '}'\n";
692                 }
693
694                 else if (t.cat() == catAlign) {
695                         os << TAB;
696                         p.skip_spaces();
697                 }
698
699                 else if (t.cat() == catComment)
700                         os << t.asInput();
701
702                 else if (t.cs() == "(") {
703                         os << "\\(";
704                         // This does only work because parse_math outputs TeX
705                         parse_math(p, os, FLAG_SIMPLE2, MATH_MODE);
706                         os << "\\)";
707                 }
708
709                 else if (t.cs() == "[") {
710                         os << "\\[";
711                         // This does only work because parse_math outputs TeX
712                         parse_math(p, os, FLAG_EQUATION, MATH_MODE);
713                         os << "\\]";
714                 }
715
716                 else if (t.cs() == "begin") {
717                         string const name = p.getArg('{', '}');
718                         active_environments.push_back(name);
719                         os << "\\begin{" << name << '}';
720                         // treat the nested environment as a block, don't
721                         // parse &, \\ etc, because they don't belong to our
722                         // table if they appear.
723                         os << p.verbatimEnvironment(name);
724                         os << "\\end{" << name << '}';
725                         active_environments.pop_back();
726                 }
727
728                 else if (t.cs() == "end") {
729                         if (flags & FLAG_END) {
730                                 // eat environment name
731                                 string const name = p.getArg('{', '}');
732                                 if (name != active_environment())
733                                         p.error("\\end{" + name + "} does not match \\begin{"
734                                                 + active_environment() + "}");
735                                 return;
736                         }
737                         p.error("found 'end' unexpectedly");
738                 }
739
740                 else
741                         os << t.asInput();
742         }
743
744         // We can have comments if the last line is incomplete
745         os << comments;
746
747         // We can have hline stuff if the last line is incomplete
748         if (!hlines.empty()) {
749                 // this does not work in LaTeX, so we ignore it
750                 cerr << "Ignoring '" << hlines << "' at end of tabular"
751                      << endl;
752         }
753 }
754
755
756 void handle_hline_above(RowInfo & ri, vector<CellInfo> & ci)
757 {
758         ri.topline = true;
759         for (size_t col = 0; col < ci.size(); ++col)
760                 ci[col].topline = true;
761 }
762
763
764 void handle_hline_below(RowInfo & ri, vector<CellInfo> & ci)
765 {
766         ri.bottomline = true;
767         for (size_t col = 0; col < ci.size(); ++col)
768                 ci[col].bottomline = true;
769 }
770
771
772 } // anonymous namespace
773
774
775 void handle_tabular(Parser & p, ostream & os, bool is_long_tabular,
776                     Context & context)
777 {
778         string posopts = p.getOpt();
779         if (!posopts.empty()) {
780                 // FIXME: Convert this to ERT
781                 if (is_long_tabular)
782                         cerr << "horizontal longtable";
783                 else
784                         cerr << "vertical tabular";
785                 cerr << " positioning '" << posopts << "' ignored\n";
786         }
787
788         vector<ColInfo> colinfo;
789
790         // handle column formatting
791         handle_colalign(p, colinfo, ColInfo());
792         fix_colalign(colinfo);
793
794         // first scan of cells
795         // use table mode to keep it minimal-invasive
796         // not exactly what's TeX doing...
797         vector<string> lines;
798         ostringstream ss;
799         RowPosition rowpos = ROW_START;
800         parse_table(p, ss, is_long_tabular, rowpos, FLAG_END);
801         split(ss.str(), lines, LINE);
802
803         vector< vector<CellInfo> > cellinfo(lines.size());
804         vector<RowInfo> rowinfo(lines.size());
805
806         // split into rows
807         //cerr << "// split into rows\n";
808         for (size_t row = 0; row < rowinfo.size(); ++row) {
809
810                 // init row
811                 cellinfo[row].resize(colinfo.size());
812
813                 // split row
814                 vector<string> dummy;
815                 //cerr << "\n########### LINE: " << lines[row] << "########\n";
816                 split(lines[row], dummy, HLINE);
817
818                 // handle horizontal line fragments
819                 // we do only expect this for a last line without '\\'
820                 if (dummy.size() != 3) {
821                         if ((dummy.size() != 1 && dummy.size() != 2) ||
822                             row != rowinfo.size() - 1)
823                                 cerr << "unexpected dummy size: " << dummy.size()
824                                         << " content: " << lines[row] << "\n";
825                         dummy.resize(3);
826                 }
827                 lines[row] = dummy[1];
828
829                 //cerr << "line: " << row << " above 0: " << dummy[0] << "\n";
830                 //cerr << "line: " << row << " below 2: " << dummy[2] <<  "\n";
831                 //cerr << "line: " << row << " cells 1: " << dummy[1] <<  "\n";
832
833                 for (int i = 0; i <= 2; i += 2) {
834                         //cerr << "   reading from line string '" << dummy[i] << "'\n";
835                         Parser p1(dummy[i]);
836                         while (p1.good()) {
837                                 Token t = p1.get_token();
838                                 //cerr << "read token: " << t << "\n";
839                                 if (t.cs() == "hline") {
840                                         if (i == 0) {
841                                                 if (rowinfo[row].topline) {
842                                                         if (row > 0) // extra bottomline above
843                                                                 handle_hline_below(rowinfo[row - 1], cellinfo[row - 1]);
844                                                         else
845                                                                 cerr << "dropping extra hline\n";
846                                                         //cerr << "below row: " << row-1 << endl;
847                                                 } else {
848                                                         handle_hline_above(rowinfo[row], cellinfo[row]);
849                                                         //cerr << "above row: " << row << endl;
850                                                 }
851                                         } else {
852                                                 //cerr << "below row: " << row << endl;
853                                                 handle_hline_below(rowinfo[row], cellinfo[row]);
854                                         }
855                                 } else if (t.cs() == "cline") {
856                                         string arg = p1.verbatim_item();
857                                         //cerr << "read cline arg: '" << arg << "'\n";
858                                         vector<string> t;
859                                         split(arg, t, '-');
860                                         t.resize(2);
861                                         size_t from = convert<unsigned int>(t[0]);
862                                         if (from == 0)
863                                                 cerr << "Could not parse "
864                                                         "cline start column."
865                                                      << endl;
866                                         else
867                                                 // 1 based index -> 0 based
868                                                 --from;
869                                         if (from >= colinfo.size()) {
870                                                 cerr << "cline starts at non "
871                                                         "existing column "
872                                                      << (from + 1) << endl;
873                                                 from = colinfo.size() - 1;
874                                         }
875                                         size_t to = convert<unsigned int>(t[1]);
876                                         if (to == 0)
877                                                 cerr << "Could not parse "
878                                                         "cline end column."
879                                                      << endl;
880                                         else
881                                                 // 1 based index -> 0 based
882                                                 --to;
883                                         if (to >= colinfo.size()) {
884                                                 cerr << "cline ends at non "
885                                                         "existing column "
886                                                      << (to + 1) << endl;
887                                                 to = colinfo.size() - 1;
888                                         }
889                                         for (size_t col = from; col <= to; ++col) {
890                                                 //cerr << "row: " << row << " col: " << col << " i: " << i << endl;
891                                                 if (i == 0) {
892                                                         rowinfo[row].topline = true;
893                                                         cellinfo[row][col].topline = true;
894                                                 } else {
895                                                         rowinfo[row].bottomline = true;
896                                                         cellinfo[row][col].bottomline = true;
897                                                 }
898                                         }
899                                 } else if (t.cs() == "endhead") {
900                                         if (i > 0)
901                                                 rowinfo[row].type = LT_HEAD;
902                                         for (int r = row - 1; r >= 0; --r) {
903                                                 if (rowinfo[r].type != LT_NORMAL)
904                                                         break;
905                                                 rowinfo[r].type = LT_HEAD;
906                                         }
907                                 } else if (t.cs() == "endfirsthead") {
908                                         if (i > 0)
909                                                 rowinfo[row].type = LT_FIRSTHEAD;
910                                         for (int r = row - 1; r >= 0; --r) {
911                                                 if (rowinfo[r].type != LT_NORMAL)
912                                                         break;
913                                                 rowinfo[r].type = LT_FIRSTHEAD;
914                                         }
915                                 } else if (t.cs() == "endfoot") {
916                                         if (i > 0)
917                                                 rowinfo[row].type = LT_FOOT;
918                                         for (int r = row - 1; r >= 0; --r) {
919                                                 if (rowinfo[r].type != LT_NORMAL)
920                                                         break;
921                                                 rowinfo[r].type = LT_FOOT;
922                                         }
923                                 } else if (t.cs() == "endlastfoot") {
924                                         if (i > 0)
925                                                 rowinfo[row].type = LT_LASTFOOT;
926                                         for (int r = row - 1; r >= 0; --r) {
927                                                 if (rowinfo[r].type != LT_NORMAL)
928                                                         break;
929                                                 rowinfo[r].type = LT_LASTFOOT;
930                                         }
931                                 } else if (t.cs() == "newpage") {
932                                         if (i == 0) {
933                                                 if (row > 0)
934                                                         rowinfo[row - 1].newpage = true;
935                                                 else
936                                                         // This does not work in LaTeX
937                                                         cerr << "Ignoring "
938                                                                 "'\\newpage' "
939                                                                 "before rows."
940                                                              << endl;
941                                         } else
942                                                 rowinfo[row].newpage = true;
943                                 } else {
944                                         cerr << "unexpected line token: " << t << endl;
945                                 }
946                         }
947                 }
948
949                 // split into cells
950                 vector<string> cells;
951                 split(lines[row], cells, TAB);
952                 for (size_t col = 0, cell = 0; cell < cells.size();
953                      ++col, ++cell) {
954                         //cerr << "cell content: '" << cells[cell] << "'\n";
955                         if (col >= colinfo.size()) {
956                                 // This does not work in LaTeX
957                                 cerr << "Ignoring extra cell '"
958                                      << cells[cell] << "'." << endl;
959                                 continue;
960                         }
961                         Parser p(cells[cell]);
962                         p.skip_spaces();
963                         //cells[cell] << "'\n";
964                         if (p.next_token().cs() == "multicolumn") {
965                                 // how many cells?
966                                 p.get_token();
967                                 size_t const ncells =
968                                         convert<unsigned int>(p.verbatim_item());
969
970                                 // special cell properties alignment
971                                 vector<ColInfo> t;
972                                 handle_colalign(p, t, ColInfo());
973                                 ColInfo & ci = t.front();
974
975                                 // The logic of LyX for multicolumn vertical
976                                 // lines is too complicated to reproduce it
977                                 // here (see LyXTabular::TeXCellPreamble()).
978                                 // Therefore we simply put everything in the
979                                 // special field.
980                                 ci2special(ci);
981
982                                 cellinfo[row][col].multi      = CELL_BEGIN_OF_MULTICOLUMN;
983                                 cellinfo[row][col].align      = ci.align;
984                                 cellinfo[row][col].special    = ci.special;
985                                 cellinfo[row][col].leftlines  = ci.leftlines;
986                                 cellinfo[row][col].rightlines = ci.rightlines;
987                                 ostringstream os;
988                                 parse_text_in_inset(p, os, FLAG_ITEM, false, context);
989                                 if (!cellinfo[row][col].content.empty()) {
990                                         // This may or may not work in LaTeX,
991                                         // but it does not work in LyX.
992                                         // FIXME: Handle it correctly!
993                                         cerr << "Moving cell content '"
994                                              << cells[cell]
995                                              << "' into a multicolumn cell. "
996                                                 "This will probably not work."
997                                              << endl;
998                                 }
999                                 cellinfo[row][col].content += os.str();
1000
1001                                 // add dummy cells for multicol
1002                                 for (size_t i = 0; i < ncells - 1 && col < colinfo.size(); ++i) {
1003                                         ++col;
1004                                         cellinfo[row][col].multi = CELL_PART_OF_MULTICOLUMN;
1005                                         cellinfo[row][col].align = 'c';
1006                                 }
1007
1008                         } else {
1009                                 cellinfo[row][col].leftlines  = colinfo[col].leftlines;
1010                                 cellinfo[row][col].rightlines = colinfo[col].rightlines;
1011                                 cellinfo[row][col].align      = colinfo[col].align;
1012                                 ostringstream os;
1013                                 parse_text_in_inset(p, os, FLAG_CELL, false, context);
1014                                 cellinfo[row][col].content += os.str();
1015                         }
1016                 }
1017
1018                 //cerr << "//  handle almost empty last row what we have\n";
1019                 // handle almost empty last row
1020                 if (row && lines[row].empty() && row + 1 == rowinfo.size()) {
1021                         //cerr << "remove empty last line\n";
1022                         if (rowinfo[row].topline)
1023                                 rowinfo[row - 1].bottomline = true;
1024                         for (size_t col = 0; col < colinfo.size(); ++col)
1025                                 if (cellinfo[row][col].topline)
1026                                         cellinfo[row - 1][col].bottomline = true;
1027                         rowinfo.pop_back();
1028                 }
1029         }
1030
1031         // Now we have the table structure and content in rowinfo, colinfo
1032         // and cellinfo.
1033         // Unfortunately LyX has some limitations that we need to work around.
1034
1035         // Convert cells with special content to multicolumn cells
1036         // (LyX ignores the special field for non-multicolumn cells).
1037         for (size_t row = 0; row < rowinfo.size(); ++row) {
1038                 for (size_t col = 0; col < cellinfo[row].size(); ++col) {
1039                         if (cellinfo[row][col].multi == CELL_NORMAL &&
1040                             !cellinfo[row][col].special.empty())
1041                                 cellinfo[row][col].multi = CELL_BEGIN_OF_MULTICOLUMN;
1042                 }
1043         }
1044
1045         //cerr << "// output what we have\n";
1046         // output what we have
1047         os << "\n<lyxtabular version=\"3\" rows=\"" << rowinfo.size()
1048            << "\" columns=\"" << colinfo.size() << "\">\n";
1049         os << "<features"
1050            << write_attribute("rotate", false)
1051            << write_attribute("islongtable", is_long_tabular)
1052            << ">\n";
1053
1054         //cerr << "// after header\n";
1055         for (size_t col = 0; col < colinfo.size(); ++col) {
1056                 os << "<column alignment=\""
1057                    << verbose_align(colinfo[col].align) << "\""
1058                    << " valignment=\""
1059                    << verbose_valign(colinfo[col].valign) << "\""
1060                    << write_attribute("leftline", colinfo[col].leftlines > 0)
1061                    << write_attribute("rightline", colinfo[col].rightlines > 0)
1062                    << write_attribute("width", colinfo[col].width)
1063                    << write_attribute("special", colinfo[col].special)
1064                    << ">\n";
1065         }
1066         //cerr << "// after cols\n";
1067
1068         for (size_t row = 0; row < rowinfo.size(); ++row) {
1069                 os << "<row"
1070                    << write_attribute("topline", rowinfo[row].topline)
1071                    << write_attribute("bottomline", rowinfo[row].bottomline)
1072                    << write_attribute("endhead",
1073                                       rowinfo[row].type == LT_HEAD)
1074                    << write_attribute("endfirsthead",
1075                                       rowinfo[row].type == LT_FIRSTHEAD)
1076                    << write_attribute("endfoot",
1077                                       rowinfo[row].type == LT_FOOT)
1078                    << write_attribute("endlastfoot",
1079                                       rowinfo[row].type == LT_LASTFOOT)
1080                    << write_attribute("newpage", rowinfo[row].newpage)
1081                    << ">\n";
1082                 for (size_t col = 0; col < colinfo.size(); ++col) {
1083                         CellInfo const & cell = cellinfo[row][col];
1084                         os << "<cell";
1085                         if (cell.multi != CELL_NORMAL)
1086                                 os << " multicolumn=\"" << cell.multi << "\"";
1087                         os << " alignment=\"" << verbose_align(cell.align)
1088                            << "\""
1089                            << " valignment=\"" << verbose_valign(cell.valign)
1090                            << "\""
1091                            << write_attribute("topline", cell.topline)
1092                            << write_attribute("bottomline", cell.bottomline)
1093                            << write_attribute("leftline", cell.leftlines > 0)
1094                            << write_attribute("rightline", cell.rightlines > 0)
1095                            << write_attribute("rotate", cell.rotate);
1096                         //cerr << "\nrow: " << row << " col: " << col;
1097                         //if (cell.topline)
1098                         //      cerr << " topline=\"true\"";
1099                         //if (cell.bottomline)
1100                         //      cerr << " bottomline=\"true\"";
1101                         os << " usebox=\"none\""
1102                            << write_attribute("width", cell.width);
1103                         if (cell.multi != CELL_NORMAL)
1104                                 os << write_attribute("special", cell.special);
1105                         os << ">"
1106                            << "\n\\begin_inset Text\n"
1107                            << cell.content
1108                            << "\n\\end_inset\n"
1109                            << "</cell>\n";
1110                 }
1111                 os << "</row>\n";
1112         }
1113
1114         os << "</lyxtabular>\n";
1115 }
1116
1117
1118
1119
1120 // }])