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