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