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