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