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