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