]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/table.cpp
* Doxy: polish html output.
[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 <iostream>
23 #include <sstream>
24 #include <vector>
25 #include <map>
26
27 using namespace std;
28
29 namespace lyx {
30
31 // filled in preamble.cpp
32 map<char, int> special_columns;
33
34
35 namespace {
36
37 class ColInfo {
38 public:
39         ColInfo() : align('n'), valign('n'), rightlines(0), leftlines(0) {}
40         /// column alignment
41         char align;
42         /// vertical alignment
43         char valign;
44         /// column width
45         string width;
46         /// special column alignment
47         string special;
48         /// number of lines on the right
49         int rightlines;
50         /// number of lines on the left
51         int leftlines;
52 };
53
54
55 /// row type for longtables
56 enum LTRowType
57 {
58         /// normal row
59         LT_NORMAL,
60         /// part of head
61         LT_HEAD,
62         /// part of head on first page
63         LT_FIRSTHEAD,
64         /// part of foot
65         LT_FOOT,
66         /// part of foot on last page
67         LT_LASTFOOT
68 };
69
70
71 class RowInfo {
72 public:
73         RowInfo() : topline(false), bottomline(false), type(LT_NORMAL),
74                     newpage(false) {}
75         /// horizontal line above
76         bool topline;
77         /// horizontal line below
78         bool bottomline;
79         /// These are for longtabulars only
80         /// row type (head, foot, firsthead etc.)
81         LTRowType type;
82         /// row for a newpage
83         bool newpage;
84 };
85
86
87 enum Multicolumn {
88         /// A normal cell
89         CELL_NORMAL = 0,
90         /// A multicolumn cell. The number of columns is <tt>1 + number
91         /// of CELL_PART_OF_MULTICOLUMN cells</tt> that follow directly
92         CELL_BEGIN_OF_MULTICOLUMN,
93         /// This is a dummy cell (part of a multicolumn cell)
94         CELL_PART_OF_MULTICOLUMN
95 };
96
97
98 class CellInfo {
99 public:
100         CellInfo() : multi(CELL_NORMAL), align('n'), valign('n'),
101                      leftlines(0), rightlines(0), topline(false),
102                      bottomline(false), rotate(false) {}
103         /// cell content
104         string content;
105         /// multicolumn flag
106         Multicolumn multi;
107         /// cell alignment
108         char align;
109         /// vertical cell alignment
110         char valign;
111         /// number of lines on the left
112         int leftlines;
113         /// number of lines on the right
114         int rightlines;
115         /// do we have a line above?
116         bool topline;
117         /// do we have a line below?
118         bool bottomline;
119         /// is the cell rotated?
120         bool rotate;
121         /// width for multicolumn cells
122         string width;
123         /// special formatting for multicolumn cells
124         string special;
125 };
126
127
128 /// translate a horizontal alignment (as stored in ColInfo and CellInfo) to LyX
129 inline char const * verbose_align(char c)
130 {
131         switch (c) {
132         case 'c':
133                 return "center";
134         case 'r':
135                 return "right";
136         case 'l':
137                 return "left";
138         default:
139                 return "none";
140         }
141 }
142
143
144 /// translate a vertical alignment (as stored in ColInfo and CellInfo) to LyX
145 inline char const * verbose_valign(char c)
146 {
147         // The default value for no special alignment is "top".
148         switch (c) {
149         case 'm':
150                 return "middle";
151         case 'b':
152                 return "bottom";
153         case 'p':
154         default:
155                 return "top";
156         }
157 }
158
159
160 // stripped down from tabluar.C. We use it currently only for bools and
161 // strings
162 string const write_attribute(string const & name, bool const & b)
163 {
164         // we write only true attribute values so we remove a bit of the
165         // file format bloat for tabulars.
166         return b ? ' ' + name + "=\"true\"" : string();
167 }
168
169
170 string const write_attribute(string const & name, string const & s)
171 {
172         return s.empty() ? string() : ' ' + name + "=\"" + s + '"';
173 }
174
175
176 /*! rather brutish way to code table structure in a string:
177
178 \verbatim
179   \begin{tabular}{ccc}
180     1 & 2 & 3\\ \hline
181     \multicolumn{2}{c}{4} & 5 //
182     6 & 7 \\
183     8 \endhead
184   \end{tabular}
185 \endverbatim
186
187  gets "translated" to:
188
189 \verbatim
190          HLINE 1                     TAB 2 TAB 3 HLINE          HLINE LINE
191   \hline HLINE \multicolumn{2}{c}{4} TAB 5       HLINE          HLINE LINE
192          HLINE 6                     TAB 7       HLINE          HLINE LINE
193          HLINE 8                                 HLINE \endhead HLINE LINE
194 \endverbatim
195  */
196
197 char const TAB   = '\001';
198 char const LINE  = '\002';
199 char const HLINE = '\004';
200
201
202 /*!
203  * Move the information in leftlines, rightlines, align and valign to the
204  * special field. This is necessary if the special field is not empty,
205  * because LyX ignores leftlines, rightlines, align and valign in this case.
206  */
207 void ci2special(ColInfo & ci)
208 {
209         if (ci.width.empty() && ci.align == 'n')
210                 // The alignment setting is already in special, since
211                 // handle_colalign() never stores ci with these settings
212                 // and ensures that leftlines == 0 and rightlines == 0 in
213                 // this case.
214                 return;
215
216         if (!ci.width.empty()) {
217                 switch (ci.align) {
218                 case 'l':
219                         ci.special += ">{\\raggedright}";
220                         break;
221                 case 'r':
222                         ci.special += ">{\\raggedleft}";
223                         break;
224                 case 'c':
225                         ci.special += ">{\\centering}";
226                         break;
227                 }
228                 if (ci.valign == 'n')
229                         ci.special += 'p';
230                 else
231                         ci.special += ci.valign;
232                 ci.special += '{' + ci.width + '}';
233                 ci.width.erase();
234         } else
235                 ci.special += ci.align;
236
237         for (int i = 0; i < ci.leftlines; ++i)
238                 ci.special.insert(0, "|");
239         for (int i = 0; i < ci.rightlines; ++i)
240                 ci.special += '|';
241         ci.leftlines = 0;
242         ci.rightlines = 0;
243         ci.align = 'n';
244         ci.valign = 'n';
245 }
246
247
248 /*!
249  * Handle column specifications for tabulars and multicolumns.
250  * The next token of the parser \p p must be an opening brace, and we read
251  * everything until the matching closing brace.
252  * The resulting column specifications are filled into \p colinfo. This is
253  * in an intermediate form. fix_colalign() makes it suitable for LyX output.
254  */
255 void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
256                      ColInfo const & start)
257 {
258         if (p.get_token().cat() != catBegin)
259                 cerr << "Wrong syntax for table column alignment.\n"
260                         "Expected '{', got '" << p.curr_token().asInput()
261                      << "'.\n";
262
263         ColInfo next = start;
264         for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
265              t = p.get_token()) {
266 #ifdef FILEDEBUG
267                 cerr << "t: " << t << "  c: '" << t.character() << "'\n";
268 #endif
269
270                 // We cannot handle comments here
271                 if (t.cat() == catComment) {
272                         if (t.cs().empty()) {
273                                 // "%\n" combination
274                                 p.skip_spaces();
275                         } else
276                                 cerr << "Ignoring comment: " << t.asInput();
277                         continue;
278                 }
279
280                 switch (t.character()) {
281                         case 'c':
282                         case 'l':
283                         case 'r':
284                                 // new column, horizontal aligned
285                                 next.align = t.character();
286                                 if (!next.special.empty())
287                                         ci2special(next);
288                                 colinfo.push_back(next);
289                                 next = ColInfo();
290                                 break;
291                         case 'p':
292                         case 'b':
293                         case 'm':
294                                 // new column, vertical aligned box
295                                 next.valign = t.character();
296                                 next.width = p.verbatim_item();
297                                 if (!next.special.empty())
298                                         ci2special(next);
299                                 colinfo.push_back(next);
300                                 next = ColInfo();
301                                 break;
302                         case '|':
303                                 // vertical rule
304                                 if (colinfo.empty()) {
305                                         if (next.special.empty())
306                                                 ++next.leftlines;
307                                         else
308                                                 next.special += '|';
309                                 } else if (colinfo.back().special.empty())
310                                         ++colinfo.back().rightlines;
311                                 else if (next.special.empty())
312                                         ++next.leftlines;
313                                 else
314                                         colinfo.back().special += '|';
315                                 break;
316                         case '>': {
317                                 // text before the next column
318                                 string const s = trim(p.verbatim_item());
319                                 if (next.special.empty() &&
320                                     next.align == 'n') {
321                                         // Maybe this can be converted to a
322                                         // horizontal alignment setting for
323                                         // fixed width columns
324                                         if (s == "\\raggedleft")
325                                                 next.align = 'r';
326                                         else if (s == "\\raggedright")
327                                                 next.align = 'l';
328                                         else if (s == "\\centering")
329                                                 next.align = 'c';
330                                         else
331                                                 next.special = ">{" + s + '}';
332                                 } else
333                                         next.special += ">{" + s + '}';
334                                 break;
335                         }
336                         case '<': {
337                                 // text after the last column
338                                 string const s = trim(p.verbatim_item());
339                                 if (colinfo.empty())
340                                         // This is not possible in LaTeX.
341                                         cerr << "Ignoring separator '<{"
342                                              << s << "}'." << endl;
343                                 else {
344                                         ColInfo & ci = colinfo.back();
345                                         ci2special(ci);
346                                         ci.special += "<{" + s + '}';
347                                 }
348                                 break;
349                         }
350                         case '*': {
351                                 // *{n}{arg} means 'n' columns of type 'arg'
352                                 string const num = p.verbatim_item();
353                                 string const arg = p.verbatim_item();
354                                 size_t const n = convert<unsigned int>(num);
355                                 if (!arg.empty() && n > 0) {
356                                         string s("{");
357                                         for (size_t i = 0; i < n; ++i)
358                                                 s += arg;
359                                         s += '}';
360                                         Parser p2(s);
361                                         handle_colalign(p2, colinfo, next);
362                                         next = ColInfo();
363                                 } else {
364                                         cerr << "Ignoring column specification"
365                                                 " '*{" << num << "}{"
366                                              << arg << "}'." << endl;
367                                 }
368                                 break;
369                         }
370                         case '@':
371                                 // text instead of the column spacing
372                         case '!':
373                                 // text in addition to the column spacing
374                                 next.special += t.character();
375                                 next.special += '{' + p.verbatim_item() + '}';
376                                 break;
377                         default:
378                                 // try user defined column types
379                                 if (special_columns.find(t.character()) !=
380                                     special_columns.end()) {
381                                         ci2special(next);
382                                         next.special += t.character();
383                                         int const nargs =
384                                                 special_columns[t.character()];
385                                         for (int i = 0; i < nargs; ++i)
386                                                 next.special += '{' +
387                                                         p.verbatim_item() +
388                                                         '}';
389                                         colinfo.push_back(next);
390                                         next = ColInfo();
391                                 } else
392                                         cerr << "Ignoring column specification"
393                                                 " '" << t << "'." << endl;
394                                 break;
395                         }
396         }
397
398         // Maybe we have some column separators that need to be added to the
399         // last column?
400         ci2special(next);
401         if (!next.special.empty()) {
402                 ColInfo & ci = colinfo.back();
403                 ci2special(ci);
404                 ci.special += next.special;
405                 next.special.erase();
406         }
407 }
408
409
410 /*!
411  * Move the left and right lines and alignment settings of the column \p ci
412  * to the special field if necessary.
413  */
414 void fix_colalign(ColInfo & ci)
415 {
416         if (ci.leftlines > 1 || ci.rightlines > 1)
417                 ci2special(ci);
418 }
419
420
421 /*!
422  * LyX can't handle more than one vertical line at the left or right side
423  * of a column.
424  * This function moves the left and right lines and alignment settings of all
425  * columns in \p colinfo to the special field if necessary.
426  */
427 void fix_colalign(vector<ColInfo> & colinfo)
428 {
429         // Try to move extra leftlines to the previous column.
430         // We do this only if both special fields are empty, otherwise we
431         // can't tell wether the result will be the same.
432         for (size_t col = 0; col < colinfo.size(); ++col) {
433                 if (colinfo[col].leftlines > 1 &&
434                     colinfo[col].special.empty() && col > 0 &&
435                     colinfo[col - 1].rightlines == 0 &&
436                     colinfo[col - 1].special.empty()) {
437                         ++colinfo[col - 1].rightlines;
438                         --colinfo[col].leftlines;
439                 }
440         }
441         // Try to move extra rightlines to the next column
442         for (size_t col = 0; col < colinfo.size(); ++col) {
443                 if (colinfo[col].rightlines > 1 &&
444                     colinfo[col].special.empty() &&
445                     col < colinfo.size() - 1 &&
446                     colinfo[col + 1].leftlines == 0 &&
447                     colinfo[col + 1].special.empty()) {
448                         ++colinfo[col + 1].leftlines;
449                         --colinfo[col].rightlines;
450                 }
451         }
452         // Move the lines and alignment settings to the special field if
453         // necessary
454         for (size_t col = 0; col < colinfo.size(); ++col)
455                 fix_colalign(colinfo[col]);
456 }
457
458
459 /*!
460  * Parse hlines and similar stuff.
461  * \returns wether the token \p t was parsed
462  */
463 bool parse_hlines(Parser & p, Token const & t, string & hlines,
464                   bool is_long_tabular)
465 {
466         BOOST_ASSERT(t.cat() == catEscape);
467
468         if (t.cs() == "hline")
469                 hlines += "\\hline";
470
471         else if (t.cs() == "cline")
472                 hlines += "\\cline{" + p.verbatim_item() + '}';
473
474         else if (is_long_tabular && t.cs() == "newpage")
475                 hlines += "\\newpage";
476
477         else
478                 return false;
479
480         return true;
481 }
482
483
484 /// Position in a row
485 enum RowPosition {
486         /// At the very beginning, before the first token
487         ROW_START,
488         /// After the first token and before any column token
489         IN_HLINES_START,
490         /// After the first column token. Comments and whitespace are only
491         /// treated as tokens in this position
492         IN_COLUMNS,
493         /// After the first non-column token at the end
494         IN_HLINES_END
495 };
496
497
498 /*!
499  * Parse table structure.
500  * We parse tables in a two-pass process: This function extracts the table
501  * structure (rows, columns, hlines etc.), but does not change the cell
502  * content. The cell content is parsed in a second step in handle_tabular().
503  */
504 void parse_table(Parser & p, ostream & os, bool is_long_tabular,
505                  RowPosition & pos, unsigned flags)
506 {
507         // table structure commands such as \hline
508         string hlines;
509
510         // comments that occur at places where we can't handle them
511         string comments;
512
513         while (p.good()) {
514                 Token const & t = p.get_token();
515
516 #ifdef FILEDEBUG
517                 cerr << "t: " << t << " flags: " << flags << "\n";
518 #endif
519
520                 // comments and whitespace in hlines
521                 switch (pos) {
522                 case ROW_START:
523                 case IN_HLINES_START:
524                 case IN_HLINES_END:
525                         if (t.cat() == catComment) {
526                                 if (t.cs().empty())
527                                         // line continuation
528                                         p.skip_spaces();
529                                 else
530                                         // We can't handle comments here,
531                                         // store them for later use
532                                         comments += t.asInput();
533                                 continue;
534                         } else if (t.cat() == catSpace ||
535                                    t.cat() == catNewline) {
536                                 // whitespace is irrelevant here, we
537                                 // need to recognize hline stuff
538                                 p.skip_spaces();
539                                 continue;
540                         }
541                         break;
542                 case IN_COLUMNS:
543                         break;
544                 }
545
546                 // We need to handle structure stuff first in order to
547                 // determine wether we need to output a HLINE separator
548                 // before the row or not.
549                 if (t.cat() == catEscape) {
550                         if (parse_hlines(p, t, hlines, is_long_tabular)) {
551                                 switch (pos) {
552                                 case ROW_START:
553                                         pos = IN_HLINES_START;
554                                         break;
555                                 case IN_COLUMNS:
556                                         pos = IN_HLINES_END;
557                                         break;
558                                 case IN_HLINES_START:
559                                 case IN_HLINES_END:
560                                         break;
561                                 }
562                                 continue;
563                         }
564
565                         else if (t.cs() == "tabularnewline" ||
566                                  t.cs() == "\\" ||
567                                  t.cs() == "cr") {
568                                 if (t.cs() == "cr")
569                                         cerr << "Warning: Converting TeX "
570                                                 "'\\cr' to LaTeX '\\\\'."
571                                              << endl;
572                                 // stuff before the line break
573                                 os << comments << HLINE << hlines << HLINE
574                                    << LINE;
575                                 //cerr << "hlines: " << hlines << endl;
576                                 hlines.erase();
577                                 comments.erase();
578                                 pos = ROW_START;
579                                 continue;
580                         }
581
582                         else if (is_long_tabular &&
583                                  (t.cs() == "endhead" ||
584                                   t.cs() == "endfirsthead" ||
585                                   t.cs() == "endfoot" ||
586                                   t.cs() == "endlastfoot")) {
587                                 hlines += t.asInput();
588                                 switch (pos) {
589                                 case IN_COLUMNS:
590                                 case IN_HLINES_END:
591                                         // these commands are implicit line
592                                         // breaks
593                                         os << comments << HLINE << hlines
594                                            << HLINE << LINE;
595                                         hlines.erase();
596                                         comments.erase();
597                                         pos = ROW_START;
598                                         break;
599                                 case ROW_START:
600                                         pos = IN_HLINES_START;
601                                         break;
602                                 case IN_HLINES_START:
603                                         break;
604                                 }
605                                 continue;
606                         }
607
608                 }
609
610                 // We need a HLINE separator if we either have no hline
611                 // stuff at all and are just starting a row or if we just
612                 // got the first non-hline token.
613                 switch (pos) {
614                 case ROW_START:
615                         // no hline tokens exist, first token at row start
616                 case IN_HLINES_START:
617                         // hline tokens exist, first non-hline token at row
618                         // start
619                         os << hlines << HLINE << comments;
620                         hlines.erase();
621                         comments.erase();
622                         pos = IN_COLUMNS;
623                         break;
624                 case IN_HLINES_END:
625                         // Oops, there is still cell content after hline
626                         // stuff. This does not work in LaTeX, so we ignore
627                         // the hlines.
628                         cerr << "Ignoring '" << hlines << "' in a cell"
629                              << endl;
630                         os << comments;
631                         hlines.erase();
632                         comments.erase();
633                         pos = IN_COLUMNS;
634                         break;
635                 case IN_COLUMNS:
636                         break;
637                 }
638
639                 // If we come here we have normal cell content
640                 //
641                 // cat codes
642                 //
643                 if (t.cat() == catMath) {
644                         // we are inside some text mode thingy, so opening new math is allowed
645                         Token const & n = p.get_token();
646                         if (n.cat() == catMath) {
647                                 // TeX's $$...$$ syntax for displayed math
648                                 os << "\\[";
649                                 // This does only work because parse_math outputs TeX
650                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
651                                 os << "\\]";
652                                 p.get_token(); // skip the second '$' token
653                         } else {
654                                 // simple $...$  stuff
655                                 p.putback();
656                                 os << '$';
657                                 // This does only work because parse_math outputs TeX
658                                 parse_math(p, os, FLAG_SIMPLE, MATH_MODE);
659                                 os << '$';
660                         }
661                 }
662
663                 else if (t.cat() == catSpace || t.cat() == catNewline)
664                                 os << t.cs();
665
666                 else if (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.character();
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