]> git.lyx.org Git - lyx.git/blob - src/insets/insettabular.C
51c4a7471da0b6192a6a810384fc54777f93a380
[lyx.git] / src / insets / insettabular.C
1 /**
2  * \file insettabular.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettabular.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "CutAndPaste.h"
20 #include "coordcache.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "language.h"
27 #include "LColor.h"
28 #include "lyx_cb.h"
29 #include "lyxlex.h"
30 #include "metricsinfo.h"
31 #include "outputparams.h"
32 #include "paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "ParagraphParameters.h"
35 #include "undo.h"
36
37 #include "support/convert.h"
38
39 #include "frontends/Alert.h"
40 #include "frontends/font_metrics.h"
41 #include "frontends/LyXView.h"
42 #include "frontends/Painter.h"
43 #include "frontends/nullpainter.h"
44
45 #include <sstream>
46 #include <iostream>
47 #include <limits>
48
49 using lyx::cap::dirtyTabularStack;
50 using lyx::cap::tabularStackDirty;
51
52 using lyx::graphics::PreviewLoader;
53
54 using lyx::support::ltrim;
55
56 using boost::shared_ptr;
57
58 using std::auto_ptr;
59 using std::endl;
60 using std::max;
61 using std::string;
62 using std::istringstream;
63 using std::ostream;
64 using std::ostringstream;
65 using std::swap;
66 using std::vector;
67
68
69 namespace {
70
71 int const ADD_TO_HEIGHT = 2;
72 int const ADD_TO_TABULAR_WIDTH = 2;
73
74 ///
75 boost::scoped_ptr<LyXTabular> paste_tabular;
76
77
78 struct TabularFeature {
79         LyXTabular::Feature action;
80         string feature;
81 };
82
83
84 TabularFeature tabularFeature[] =
85 {
86         { LyXTabular::APPEND_ROW, "append-row" },
87         { LyXTabular::APPEND_COLUMN, "append-column" },
88         { LyXTabular::DELETE_ROW, "delete-row" },
89         { LyXTabular::DELETE_COLUMN, "delete-column" },
90         { LyXTabular::TOGGLE_LINE_TOP, "toggle-line-top" },
91         { LyXTabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
92         { LyXTabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
93         { LyXTabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
94         { LyXTabular::ALIGN_LEFT, "align-left" },
95         { LyXTabular::ALIGN_RIGHT, "align-right" },
96         { LyXTabular::ALIGN_CENTER, "align-center" },
97         { LyXTabular::ALIGN_BLOCK, "align-block" },
98         { LyXTabular::VALIGN_TOP, "valign-top" },
99         { LyXTabular::VALIGN_BOTTOM, "valign-bottom" },
100         { LyXTabular::VALIGN_MIDDLE, "valign-middle" },
101         { LyXTabular::M_TOGGLE_LINE_TOP, "m-toggle-line-top" },
102         { LyXTabular::M_TOGGLE_LINE_BOTTOM, "m-toggle-line-bottom" },
103         { LyXTabular::M_TOGGLE_LINE_LEFT, "m-toggle-line-left" },
104         { LyXTabular::M_TOGGLE_LINE_RIGHT, "m-toggle-line-right" },
105         { LyXTabular::M_ALIGN_LEFT, "m-align-left" },
106         { LyXTabular::M_ALIGN_RIGHT, "m-align-right" },
107         { LyXTabular::M_ALIGN_CENTER, "m-align-center" },
108         { LyXTabular::M_VALIGN_TOP, "m-valign-top" },
109         { LyXTabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
110         { LyXTabular::M_VALIGN_MIDDLE, "m-valign-middle" },
111         { LyXTabular::MULTICOLUMN, "multicolumn" },
112         { LyXTabular::SET_ALL_LINES, "set-all-lines" },
113         { LyXTabular::UNSET_ALL_LINES, "unset-all-lines" },
114         { LyXTabular::SET_LONGTABULAR, "set-longtabular" },
115         { LyXTabular::UNSET_LONGTABULAR, "unset-longtabular" },
116         { LyXTabular::SET_PWIDTH, "set-pwidth" },
117         { LyXTabular::SET_MPWIDTH, "set-mpwidth" },
118         { LyXTabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
119         { LyXTabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
120         { LyXTabular::SET_ROTATE_CELL, "set-rotate-cell" },
121         { LyXTabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
122         { LyXTabular::SET_USEBOX, "set-usebox" },
123         { LyXTabular::SET_LTHEAD, "set-lthead" },
124         { LyXTabular::UNSET_LTHEAD, "unset-lthead" },
125         { LyXTabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
126         { LyXTabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
127         { LyXTabular::SET_LTFOOT, "set-ltfoot" },
128         { LyXTabular::UNSET_LTFOOT, "unset-ltfoot" },
129         { LyXTabular::SET_LTLASTFOOT, "set-ltlastfoot" },
130         { LyXTabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
131         { LyXTabular::SET_LTNEWPAGE, "set-ltnewpage" },
132         { LyXTabular::SET_SPECIAL_COLUMN, "set-special-column" },
133         { LyXTabular::SET_SPECIAL_MULTI, "set-special-multi" },
134         { LyXTabular::LAST_ACTION, "" }
135 };
136
137
138 class FeatureEqual : public std::unary_function<TabularFeature, bool> {
139 public:
140         FeatureEqual(LyXTabular::Feature feature)
141                 : feature_(feature) {}
142         bool operator()(TabularFeature const & tf) const {
143                 return tf.action == feature_;
144         }
145 private:
146         LyXTabular::Feature feature_;
147 };
148
149 } // namespace anon
150
151
152 string const featureAsString(LyXTabular::Feature feature)
153 {
154         TabularFeature * end = tabularFeature +
155                 sizeof(tabularFeature) / sizeof(TabularFeature);
156         TabularFeature * it = std::find_if(tabularFeature, end,
157                                            FeatureEqual(feature));
158         return (it == end) ? string() : it->feature;
159 }
160
161
162 bool InsetTabular::hasPasteBuffer() const
163 {
164         return (paste_tabular.get() != 0);
165 }
166
167
168 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
169                            col_type columns)
170         : tabular(buf.params(), max(rows, row_type(1)),
171           max(columns, col_type(1))), buffer_(&buf), scx_(0)
172 {}
173
174
175 InsetTabular::InsetTabular(InsetTabular const & tab)
176         : InsetOld(tab), tabular(tab.tabular),
177                 buffer_(tab.buffer_), scx_(0)
178 {}
179
180
181 InsetTabular::~InsetTabular()
182 {
183         InsetTabularMailer(*this).hideDialog();
184 }
185
186
187 auto_ptr<InsetBase> InsetTabular::doClone() const
188 {
189         return auto_ptr<InsetBase>(new InsetTabular(*this));
190 }
191
192
193 Buffer const & InsetTabular::buffer() const
194 {
195         return *buffer_;
196 }
197
198
199 void InsetTabular::buffer(Buffer const * b)
200 {
201         buffer_ = b;
202 }
203
204
205 void InsetTabular::write(Buffer const & buf, ostream & os) const
206 {
207         os << "Tabular" << endl;
208         tabular.write(buf, os);
209 }
210
211
212 void InsetTabular::read(Buffer const & buf, LyXLex & lex)
213 {
214         bool const old_format = (lex.getString() == "\\LyXTable");
215
216         tabular.read(buf, lex);
217
218         if (old_format)
219                 return;
220
221         lex.next();
222         string token = lex.getString();
223         while (lex.isOK() && (token != "\\end_inset")) {
224                 lex.next();
225                 token = lex.getString();
226         }
227         if (token != "\\end_inset") {
228                 lex.printError("Missing \\end_inset at this point. "
229                                "Read: `$$Token'");
230         }
231 }
232
233
234 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
235 {
236         //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
237         //      mi.base.textwidth << "\n";
238         if (!mi.base.bv) {
239                 lyxerr << "InsetTabular::metrics: need bv" << endl;
240                 BOOST_ASSERT(false);
241         }
242
243         row_type i = 0;
244         for (idx_type cell = 0; i < tabular.rows(); ++i) {
245                 int maxAsc = 0;
246                 int maxDesc = 0;
247                 for (col_type j = 0; j < tabular.columns(); ++j) {
248                         if (tabular.isPartOfMultiColumn(i, j))
249                                 // Multicolumn cell, but not first one
250                                 continue;
251                         Dimension dim;
252                         MetricsInfo m = mi;
253                         LyXLength p_width;
254                         if (tabular.cell_info[i][j].multicolumn ==
255                             LyXTabular::CELL_BEGIN_OF_MULTICOLUMN)
256                                 p_width = tabular.cellinfo_of_cell(cell).p_width;
257                         else
258                                 p_width = tabular.column_info[j].p_width;
259                         if (!p_width.zero())
260                                 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
261                         tabular.getCellInset(cell)->metrics(m, dim);
262                         if (!p_width.zero())
263                                 dim.wid = m.base.textwidth;
264                         maxAsc  = max(maxAsc, dim.asc);
265                         maxDesc = max(maxDesc, dim.des);
266                         tabular.setWidthOfCell(cell, dim.wid);
267                         ++cell;
268                 }
269                 tabular.setAscentOfRow(i, maxAsc + ADD_TO_HEIGHT);
270                 tabular.setDescentOfRow(i, maxDesc + ADD_TO_HEIGHT);
271         }
272
273         dim.asc = tabular.getAscentOfRow(0);
274         dim.des = tabular.getHeightOfTabular() - dim.asc;
275         dim.wid = tabular.getWidthOfTabular() + 2 * ADD_TO_TABULAR_WIDTH;
276         dim_ = dim;
277 }
278
279
280 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
281 {
282         setPosCache(pi, x, y);
283
284         //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
285         BufferView * bv = pi.base.bv;
286
287         static NullPainter nop;
288         static PainterInfo nullpi(bv, nop);
289
290         //resetPos(bv->cursor());
291
292         x += scx_;
293         x += ADD_TO_TABULAR_WIDTH;
294
295         idx_type idx = 0;
296         first_visible_cell = LyXTabular::npos;
297         for (row_type i = 0; i < tabular.rows(); ++i) {
298                 int nx = x;
299                 int const a = tabular.getAscentOfRow(i);
300                 int const d = tabular.getDescentOfRow(i);
301                 idx = tabular.getCellNumber(i, 0);
302                 for (col_type j = 0; j < tabular.columns(); ++j) {
303                         if (tabular.isPartOfMultiColumn(i, j))
304                                 continue;
305                         if (first_visible_cell == LyXTabular::npos)
306                                 first_visible_cell = idx;
307
308                         int const cx = nx + tabular.getBeginningOfTextInCell(idx);
309                         if (nx + tabular.getWidthOfColumn(idx) < 0
310                             || nx > bv->workWidth()
311                             || y + d < 0
312                             || y - a > bv->workHeight()) {
313                                 cell(idx)->draw(nullpi, cx, y);
314                                 drawCellLines(nop, nx, y, i, idx, pi.erased_);
315                         } else {
316                                 cell(idx)->draw(pi, cx, y);
317                                 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
318                         }
319                         nx += tabular.getWidthOfColumn(idx);
320                         ++idx;
321                 }
322
323                 if (i + 1 < tabular.rows())
324                         y += d + tabular.getAscentOfRow(i + 1) +
325                                 tabular.getAdditionalHeight(i + 1);
326         }
327 }
328
329
330 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
331 {
332         setPosCache(pi, x, y);
333
334         LCursor & cur = pi.base.bv->cursor();
335         if (!cur.selection())
336                 return;
337         if (!ptr_cmp(&cur.inset(), this))
338                 return;
339
340         //resetPos(cur);
341
342         x += scx_ + ADD_TO_TABULAR_WIDTH;
343
344         if (tablemode(cur)) {
345                 row_type rs, re;
346                 col_type cs, ce;
347                 getSelection(cur, rs, re, cs, ce);
348                 y -= tabular.getAscentOfRow(0);
349                 for (row_type j = 0; j < tabular.rows(); ++j) {
350                         int const a = tabular.getAscentOfRow(j);
351                         int const h = a + tabular.getDescentOfRow(j);
352                         int xx = x;
353                         y += tabular.getAdditionalHeight(j);
354                         for (col_type i = 0; i < tabular.columns(); ++i) {
355                                 if (tabular.isPartOfMultiColumn(j, i))
356                                         continue;
357                                 idx_type const cell =
358                                         tabular.getCellNumber(j, i);
359                                 int const w = tabular.getWidthOfColumn(cell);
360                                 if (i >= cs && i <= ce && j >= rs && j <= re)
361                                         pi.pain.fillRectangle(xx, y, w, h,
362                                                               LColor::selection);
363                                 xx += w;
364
365                         }
366                         y += h;
367                 }
368
369         } else {
370                 cur.text()->drawSelection(pi, x + getCellXPos(cur.idx()) + tabular.getBeginningOfTextInCell(cur.idx()), 0 /*this value is ignored */);
371         }
372 }
373
374
375 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
376                                  row_type row, idx_type cell, bool erased) const
377 {
378         int x2 = x + tabular.getWidthOfColumn(cell);
379         bool on_off = false;
380         LColor::color col = LColor::tabularline;
381         LColor::color onoffcol = LColor::tabularonoffline;
382
383         if (erased) {
384                 col = LColor::strikeout;
385                 onoffcol = LColor::strikeout;
386         }
387
388         if (!tabular.topAlreadyDrawn(cell)) {
389                 on_off = !tabular.topLine(cell);
390                 pain.line(x, y - tabular.getAscentOfRow(row),
391                           x2, y -  tabular.getAscentOfRow(row),
392                           on_off ? onoffcol : col,
393                           on_off ? Painter::line_onoffdash : Painter::line_solid);
394         }
395         on_off = !tabular.bottomLine(cell);
396         pain.line(x, y + tabular.getDescentOfRow(row),
397                   x2, y + tabular.getDescentOfRow(row),
398                   on_off ? onoffcol : col,
399                   on_off ? Painter::line_onoffdash : Painter::line_solid);
400         if (!tabular.leftAlreadyDrawn(cell)) {
401                 on_off = !tabular.leftLine(cell);
402                 pain.line(x, y -  tabular.getAscentOfRow(row),
403                           x, y +  tabular.getDescentOfRow(row),
404                           on_off ? onoffcol : col,
405                           on_off ? Painter::line_onoffdash : Painter::line_solid);
406         }
407         on_off = !tabular.rightLine(cell);
408         pain.line(x2 - tabular.getAdditionalWidth(cell),
409                   y -  tabular.getAscentOfRow(row),
410                   x2 - tabular.getAdditionalWidth(cell),
411                   y +  tabular.getDescentOfRow(row),
412                   on_off ? onoffcol : col,
413                   on_off ? Painter::line_onoffdash : Painter::line_solid);
414 }
415
416
417 string const InsetTabular::editMessage() const
418 {
419         return _("Opened table");
420 }
421
422
423 void InsetTabular::edit(LCursor & cur, bool left)
424 {
425         //lyxerr << "InsetTabular::edit: " << this << endl;
426         finishUndo();
427         cur.selection() = false;
428         cur.push(*this);
429         if (left) {
430                 if (isRightToLeft(cur))
431                         cur.idx() = tabular.getLastCellInRow(0);
432                 else
433                         cur.idx() = 0;
434                 cur.pit() = 0;
435                 cur.pos() = 0;
436         } else {
437                 if (isRightToLeft(cur))
438                         cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1);
439                 else
440                         cur.idx() = tabular.getNumberOfCells() - 1;
441                 cur.pit() = 0;
442                 cur.pos() = cur.lastpos(); // FIXME crude guess
443         }
444         // this accesses the position cache before it is initialized
445         //resetPos(cur);
446         //cur.bv().fitCursor();
447 }
448
449
450 void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd)
451 {
452         lyxerr[Debug::DEBUG] << "# InsetTabular::doDispatch: cmd: " << cmd
453                              << "\n  cur:" << cur << endl;
454         CursorSlice sl = cur.top();
455         LCursor & bvcur = cur.bv().cursor();
456
457         switch (cmd.action) {
458
459         case LFUN_MOUSE_PRESS:
460                 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
461
462                 if (cmd.button() == mouse_button::button1) {
463                         cur.selection() = false;
464                         setCursorFromCoordinates(cur, cmd.x, cmd.y);
465                         cur.resetAnchor();
466                         bvcur = cur;
467                         break;
468                 }
469
470                 if (cmd.button() == mouse_button::button2) {
471                         cmd = FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph");
472                         doDispatch(cur, cmd);
473                         break;
474                 }
475
476                 // we'll pop up the table dialog on release
477                 if (cmd.button() == mouse_button::button3)
478                         cur.undispatched();
479                 break;
480
481         case LFUN_MOUSE_MOTION:
482                 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
483                 if (cmd.button() == mouse_button::button1) {
484                         // only accept motions to places not deeper nested than the real anchor
485                         if (bvcur.anchor_.hasPart(cur)) {
486                                 setCursorFromCoordinates(cur, cmd.x, cmd.y);
487                                 bvcur.setCursor(cur);
488                                 bvcur.selection() = true;
489                         } else
490                                 cur.undispatched();
491                 }
492                 break;
493
494         case LFUN_MOUSE_RELEASE:
495                 //lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
496                 if (cmd.button() == mouse_button::button3)
497                         InsetTabularMailer(*this).showDialog(&cur.bv());
498                 break;
499
500         case LFUN_CELL_BACKWARD:
501                 movePrevCell(cur);
502                 cur.selection() = false;
503                 break;
504
505         case LFUN_CELL_FORWARD:
506                 moveNextCell(cur);
507                 cur.selection() = false;
508                 break;
509
510         case LFUN_CHAR_FORWARDSEL:
511         case LFUN_CHAR_FORWARD:
512                 cell(cur.idx())->dispatch(cur, cmd);
513                 if (!cur.result().dispatched()) {
514                         isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
515                         if (sl == cur.top())
516                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
517                         else
518                                 cur.dispatched();
519                 }
520                 break;
521
522         case LFUN_BACKWARD_SELECT:
523         case LFUN_CHAR_BACKWARD:
524                 cell(cur.idx())->dispatch(cur, cmd);
525                 if (!cur.result().dispatched()) {
526                         isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
527                         if (sl == cur.top())
528                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
529                         else
530                                 cur.dispatched();
531                 }
532                 break;
533
534         case LFUN_DOWN_SELECT:
535         case LFUN_DOWN:
536                 cell(cur.idx())->dispatch(cur, cmd);
537                 cur.dispatched(); // override the cell's decision
538                 if (sl == cur.top())
539                         // if our LyXText didn't do anything to the cursor
540                         // then we try to put the cursor into the cell below
541                         // setting also the right targetX.
542                         if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) {
543                                 cur.idx() = tabular.getCellBelow(cur.idx());
544                                 cur.pit() = 0;
545                                 cur.pos() = cell(cur.idx())->getText(0)->x2pos(
546                                         cur.pit(), 0, cur.targetX());
547                         }
548                 if (sl == cur.top()) {
549                         // we trick it to go to the RIGHT after leaving the
550                         // tabular.
551                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
552                         cur.undispatched();
553                 }
554                 break;
555
556         case LFUN_UP_SELECT:
557         case LFUN_UP:
558                 cell(cur.idx())->dispatch(cur, cmd);
559                 cur.dispatched(); // override the cell's decision
560                 if (sl == cur.top())
561                         // if our LyXText didn't do anything to the cursor
562                         // then we try to put the cursor into the cell above
563                         // setting also the right targetX.
564                         if (tabular.row_of_cell(cur.idx()) != 0) {
565                                 cur.idx() = tabular.getCellAbove(cur.idx());
566                                 cur.pit() = cur.lastpit();
567                                 LyXText const * text = cell(cur.idx())->getText(0);
568                                 cur.pos() = text->x2pos(
569                                         cur.pit(),
570                                         text->paragraphs().back().rows().size()-1,
571                                         cur.targetX());
572                         }
573                 if (sl == cur.top()) {
574                         cmd = FuncRequest(LFUN_FINISHED_UP);
575                         cur.undispatched();
576                 }
577                 break;
578
579 //      case LFUN_SCREEN_DOWN: {
580 //              //if (hasSelection())
581 //              //      cur.selection() = false;
582 //              col_type const col = tabular.column_of_cell(cur.idx());
583 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
584 //              if (t < yo() + tabular.getHeightOfTabular()) {
585 //                      cur.bv().scrollDocView(t);
586 //                      cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
587 //              } else {
588 //                      cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
589 //              }
590 //              cur.par() = 0;
591 //              cur.pos() = 0;
592 //              break;
593 //      }
594 //
595 //      case LFUN_SCREEN_UP: {
596 //              //if (hasSelection())
597 //              //      cur.selection() = false;
598 //              col_type const col = tabular.column_of_cell(cur.idx());
599 //              int const t =   cur.bv().top_y() + cur.bv().painter().paperHeight();
600 //              if (yo() < 0) {
601 //                      cur.bv().scrollDocView(t);
602 //                      if (yo() > 0)
603 //                              cur.idx() = col;
604 //                      else
605 //                              cur.idx() = tabular.getCellBelow(first_visible_cell) + col;
606 //              } else {
607 //                      cur.idx() = col;
608 //              }
609 //              cur.par() = cur.lastpar();
610 //              cur.pos() = cur.lastpos();
611 //              break;
612 //      }
613
614         case LFUN_LAYOUT_TABULAR:
615                 InsetTabularMailer(*this).showDialog(&cur.bv());
616                 break;
617
618         case LFUN_INSET_DIALOG_UPDATE:
619                 InsetTabularMailer(*this).updateDialog(&cur.bv());
620                 break;
621
622         case LFUN_TABULAR_FEATURE:
623                 if (!tabularFeatures(cur, cmd.argument))
624                         cur.undispatched();
625                 break;
626
627         // insert file functions
628         case LFUN_FILE_INSERT_ASCII_PARA:
629         case LFUN_FILE_INSERT_ASCII: {
630                 string const tmpstr = getContentsOfAsciiFile(&cur.bv(), cmd.argument, false);
631                 if (!tmpstr.empty() && !insertAsciiString(cur.bv(), tmpstr, false))
632                         cur.undispatched();
633                 break;
634         }
635
636         case LFUN_CUT:
637                 if (tablemode(cur)) {
638                         if (copySelection(cur)) {
639                                 recordUndoInset(cur, Undo::DELETE);
640                                 cutSelection(cur);
641                         }
642                 }
643                 else
644                         cell(cur.idx())->dispatch(cur, cmd);
645                 break;
646
647         case LFUN_DELETE_FORWARD_BACKWARD:
648         case LFUN_DELETE_FORWARD:
649                 if (tablemode(cur)) {
650                         recordUndoInset(cur, Undo::DELETE);
651                         cutSelection(cur);
652                 }
653                 else
654                         cell(cur.idx())->dispatch(cur, cmd);
655                 break;
656
657         case LFUN_COPY:
658                 if (!cur.selection())
659                         break;
660                 if (tablemode(cur)) {
661                         finishUndo();
662                         copySelection(cur);
663                 } else
664                         cell(cur.idx())->dispatch(cur, cmd);
665                 break;
666
667         case LFUN_PRIMARY_SELECTION_PASTE: {
668                 string const clip = cur.bv().getClipboard();
669                 if (clip.empty())
670                         break;
671                 if (insertAsciiString(cur.bv(), clip, false))
672                         break;
673                 else {
674                         // so that the clipboard is used and it goes on
675                         // to default
676                         // and executes LFUN_PRIMARY_SELECTION_PASTE in insettext!
677                         paste_tabular.reset();
678                         dirtyTabularStack(false);
679                 }
680                 // fall through
681         }
682
683         case LFUN_PASTE:
684                 if (hasPasteBuffer() && tabularStackDirty()) {
685                         recordUndoInset(cur, Undo::INSERT);
686                         pasteSelection(cur);
687                         break;
688                 }
689                 cell(cur.idx())->dispatch(cur, cmd);
690                 // Reset pasted paragraphs:
691                 if (tabular.getPWidth(cur.idx()).zero())
692                         cell(cur.idx())->forceParagraphsToDefault(cur);
693                 break;
694
695         case LFUN_FONT_EMPH:
696         case LFUN_FONT_BOLD:
697         case LFUN_FONT_ROMAN:
698         case LFUN_FONT_NOUN:
699         case LFUN_FONT_ITAL:
700         case LFUN_FONT_FRAK:
701         case LFUN_FONT_CODE:
702         case LFUN_FONT_SANS:
703         case LFUN_FONT_FREE_APPLY:
704         case LFUN_FONT_FREE_UPDATE:
705         case LFUN_FONT_SIZE:
706         case LFUN_FONT_UNDERLINE:
707         case LFUN_LANGUAGE:
708         case LFUN_WORD_CAPITALIZE:
709         case LFUN_WORD_UPCASE:
710         case LFUN_WORD_LOWCASE:
711         case LFUN_CHARS_TRANSPOSE:
712                 if (tablemode(cur)) {
713                         row_type rs, re;
714                         col_type cs, ce;
715                         getSelection(cur, rs, re, cs, ce);
716                         for (row_type i = rs; i <= re; ++i) {
717                                 for (col_type j = cs; j <= ce; ++j) {
718                                         // cursor follows cell:
719                                         cur.idx() = tabular.getCellNumber(i, j);
720                                         // select this cell only:
721                                         cur.pos() = 0;
722                                         cur.resetAnchor();
723                                         cur.pos() = cur.top().lastpos();
724                                         cur.setCursor(cur);
725                                         cur.setSelection();
726                                         cell(cur.idx())->dispatch(cur, cmd);
727                                 }
728                         }
729                         // Restore original selection
730                         cur.idx() = tabular.getCellNumber(rs, cs);
731                         cur.pos() = 0;
732                         cur.resetAnchor();
733                         cur.idx() = tabular.getCellNumber(re, ce);
734                         cur.pos() = cur.top().lastpos();
735                         cur.setCursor(cur);
736                         cur.setSelection();
737                         break;
738                 } else {
739                         cell(cur.idx())->dispatch(cur, cmd);
740                         break;
741                 }
742         default:
743                 // we try to handle this event in the insets dispatch function.
744                 cell(cur.idx())->dispatch(cur, cmd);
745                 break;
746         }
747
748         resetPos(cur);
749         InsetTabularMailer(*this).updateDialog(&cur.bv());
750 }
751
752
753 // function sets an object as defined in func_status.h:
754 // states OK, Unknown, Disabled, On, Off.
755 bool InsetTabular::getStatus(LCursor & cur, FuncRequest const & cmd,
756         FuncStatus & status) const
757 {
758         switch (cmd.action) {
759         case LFUN_TABULAR_FEATURE: {
760                 int action = LyXTabular::LAST_ACTION;
761                 int i = 0;
762                 for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
763                         string const tmp = tabularFeature[i].feature;
764                         if (tmp == cmd.argument.substr(0, tmp.length())) {
765                                 action = tabularFeature[i].action;
766                                 break;
767                         }
768                 }
769                 if (action == LyXTabular::LAST_ACTION) {
770                         status.clear();
771                         status.unknown(true);
772                         return true;
773                 }
774
775                 string const argument
776                         = ltrim(cmd.argument.substr(tabularFeature[i].feature.length()));
777
778                 row_type sel_row_start = 0;
779                 row_type sel_row_end = 0;
780                 col_type dummy;
781                 LyXTabular::ltType dummyltt;
782                 bool flag = true;
783
784                 getSelection(cur, sel_row_start, sel_row_end, dummy, dummy);
785
786                 switch (action) {
787                 case LyXTabular::SET_PWIDTH:
788                 case LyXTabular::SET_MPWIDTH:
789                 case LyXTabular::SET_SPECIAL_COLUMN:
790                 case LyXTabular::SET_SPECIAL_MULTI:
791                 case LyXTabular::APPEND_ROW:
792                 case LyXTabular::APPEND_COLUMN:
793                 case LyXTabular::DELETE_ROW:
794                 case LyXTabular::DELETE_COLUMN:
795                 case LyXTabular::SET_ALL_LINES:
796                 case LyXTabular::UNSET_ALL_LINES:
797                         status.clear();
798                         return true;
799
800                 case LyXTabular::MULTICOLUMN:
801                         status.setOnOff(tabular.isMultiColumn(cur.idx()));
802                         break;
803
804                 case LyXTabular::M_TOGGLE_LINE_TOP:
805                         flag = false;
806                 case LyXTabular::TOGGLE_LINE_TOP:
807                         status.setOnOff(tabular.topLine(cur.idx(), flag));
808                         break;
809
810                 case LyXTabular::M_TOGGLE_LINE_BOTTOM:
811                         flag = false;
812                 case LyXTabular::TOGGLE_LINE_BOTTOM:
813                         status.setOnOff(tabular.bottomLine(cur.idx(), flag));
814                         break;
815
816                 case LyXTabular::M_TOGGLE_LINE_LEFT:
817                         flag = false;
818                 case LyXTabular::TOGGLE_LINE_LEFT:
819                         status.setOnOff(tabular.leftLine(cur.idx(), flag));
820                         break;
821
822                 case LyXTabular::M_TOGGLE_LINE_RIGHT:
823                         flag = false;
824                 case LyXTabular::TOGGLE_LINE_RIGHT:
825                         status.setOnOff(tabular.rightLine(cur.idx(), flag));
826                         break;
827
828                 case LyXTabular::M_ALIGN_LEFT:
829                         flag = false;
830                 case LyXTabular::ALIGN_LEFT:
831                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
832                         break;
833
834                 case LyXTabular::M_ALIGN_RIGHT:
835                         flag = false;
836                 case LyXTabular::ALIGN_RIGHT:
837                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
838                         break;
839
840                 case LyXTabular::M_ALIGN_CENTER:
841                         flag = false;
842                 case LyXTabular::ALIGN_CENTER:
843                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
844                         break;
845
846                 case LyXTabular::ALIGN_BLOCK:
847                         status.enabled(!tabular.getPWidth(cur.idx()).zero());
848                         status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
849                         break;
850
851                 case LyXTabular::M_VALIGN_TOP:
852                         flag = false;
853                 case LyXTabular::VALIGN_TOP:
854                         status.setOnOff(
855                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_TOP);
856                         break;
857
858                 case LyXTabular::M_VALIGN_BOTTOM:
859                         flag = false;
860                 case LyXTabular::VALIGN_BOTTOM:
861                         status.setOnOff(
862                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_BOTTOM);
863                         break;
864
865                 case LyXTabular::M_VALIGN_MIDDLE:
866                         flag = false;
867                 case LyXTabular::VALIGN_MIDDLE:
868                         status.setOnOff(
869                                 tabular.getVAlignment(cur.idx(), flag) == LyXTabular::LYX_VALIGN_MIDDLE);
870                         break;
871
872                 case LyXTabular::SET_LONGTABULAR:
873                         status.setOnOff(tabular.isLongTabular());
874                         break;
875
876                 case LyXTabular::UNSET_LONGTABULAR:
877                         status.setOnOff(!tabular.isLongTabular());
878                         break;
879
880                 case LyXTabular::SET_ROTATE_TABULAR:
881                         status.setOnOff(tabular.getRotateTabular());
882                         break;
883
884                 case LyXTabular::UNSET_ROTATE_TABULAR:
885                         status.setOnOff(!tabular.getRotateTabular());
886                         break;
887
888                 case LyXTabular::SET_ROTATE_CELL:
889                         status.setOnOff(tabular.getRotateCell(cur.idx()));
890                         break;
891
892                 case LyXTabular::UNSET_ROTATE_CELL:
893                         status.setOnOff(!tabular.getRotateCell(cur.idx()));
894                         break;
895
896                 case LyXTabular::SET_USEBOX:
897                         status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
898                         break;
899
900                 case LyXTabular::SET_LTFIRSTHEAD:
901                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
902                         break;
903
904                 case LyXTabular::UNSET_LTFIRSTHEAD:
905                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
906                         break;
907
908                 case LyXTabular::SET_LTHEAD:
909                         status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
910                         break;
911
912                 case LyXTabular::UNSET_LTHEAD:
913                         status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
914                         break;
915
916                 case LyXTabular::SET_LTFOOT:
917                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
918                         break;
919
920                 case LyXTabular::UNSET_LTFOOT:
921                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
922                         break;
923
924                 case LyXTabular::SET_LTLASTFOOT:
925                         status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
926                         break;
927
928                 case LyXTabular::UNSET_LTLASTFOOT:
929                         status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
930                         break;
931
932                 case LyXTabular::SET_LTNEWPAGE:
933                         status.setOnOff(tabular.getLTNewPage(sel_row_start));
934                         break;
935
936                 default:
937                         status.clear();
938                         status.enabled(false);
939                         break;
940                 }
941                 return true;
942         }
943
944         // These are only enabled inside tabular
945         case LFUN_CELL_BACKWARD:
946         case LFUN_CELL_FORWARD:
947                 status.enabled(true);
948                 return true;
949
950         // disable these with multiple cells selected
951         case LFUN_INSET_INSERT:
952         case LFUN_TABULAR_INSERT:
953         case LFUN_CHARSTYLE_INSERT:
954         case LFUN_FLOAT_INSERT:
955         case LFUN_FLOAT_WIDE_INSERT:
956         case LFUN_FOOTNOTE_INSERT:
957         case LFUN_MARGINALNOTE_INSERT:
958         case LFUN_MATH_INSERT:
959         case LFUN_MATH_MODE:
960         case LFUN_MATH_MUTATE:
961         case LFUN_MATH_DISPLAY:
962         case LFUN_NOTE_INSERT:
963         case LFUN_OPTIONAL_INSERT:
964         case LFUN_BOX_INSERT:
965         case LFUN_BRANCH_INSERT:
966         case LFUN_WRAP_INSERT:
967         case LFUN_ERT_INSERT: {
968                 if (tablemode(cur)) {
969                         status.enabled(false);
970                         return true;
971                 } else
972                         return cell(cur.idx())->getStatus(cur, cmd, status);
973         }
974
975         // disable in non-fixed-width cells
976         case LFUN_BREAK_LINE:
977         case LFUN_BREAK_PARAGRAPH:
978         case LFUN_BREAK_PARAGRAPHKEEPLAYOUT:
979         case LFUN_BREAK_PARAGRAPH_SKIP: {
980                 if (tabular.getPWidth(cur.idx()).zero()) {
981                         status.enabled(false);
982                         return true;
983                 } else
984                         return cell(cur.idx())->getStatus(cur, cmd, status);
985         }
986
987         case LFUN_PASTE:
988                 if (tabularStackDirty()) {
989                         status.enabled(true);
990                         return true;
991                 }
992
993         case LFUN_INSET_MODIFY:
994                 if (translate(cmd.getArg(0)) == TABULAR_CODE) {
995                         status.enabled(true);
996                         return true;
997                 }
998                 // Fall through
999
1000         default:
1001                 // we try to handle this event in the insets dispatch function.
1002                 return cell(cur.idx())->getStatus(cur, cmd, status);
1003         }
1004 }
1005
1006
1007 int InsetTabular::latex(Buffer const & buf, ostream & os,
1008                         OutputParams const & runparams) const
1009 {
1010         return tabular.latex(buf, os, runparams);
1011 }
1012
1013
1014 int InsetTabular::plaintext(Buffer const & buf, ostream & os,
1015                         OutputParams const & runparams) const
1016 {
1017         int const dp = runparams.linelen ? runparams.depth : 0;
1018         return tabular.plaintext(buf, os, runparams, dp, false, 0);
1019 }
1020
1021
1022 int InsetTabular::linuxdoc(Buffer const & buf, ostream & os,
1023                            OutputParams const & runparams) const
1024 {
1025         return tabular.linuxdoc(buf,os, runparams);
1026 }
1027
1028
1029 int InsetTabular::docbook(Buffer const & buf, ostream & os,
1030                           OutputParams const & runparams) const
1031 {
1032         int ret = 0;
1033         InsetBase * master = 0;
1034
1035 #ifdef WITH_WARNINGS
1036 #warning Why not pass a proper DocIterator here?
1037 #endif
1038 #if 0
1039         // if the table is inside a float it doesn't need the informaltable
1040         // wrapper. Search for it.
1041         for (master = owner(); master; master = master->owner())
1042                 if (master->lyxCode() == InsetBase::FLOAT_CODE)
1043                         break;
1044 #endif
1045
1046         if (!master) {
1047                 os << "<informaltable>";
1048                 ++ret;
1049         }
1050         ret += tabular.docbook(buf, os, runparams);
1051         if (!master) {
1052                 os << "</informaltable>";
1053                 ++ret;
1054         }
1055         return ret;
1056 }
1057
1058
1059 void InsetTabular::validate(LaTeXFeatures & features) const
1060 {
1061         tabular.validate(features);
1062 }
1063
1064
1065 shared_ptr<InsetText const> InsetTabular::cell(idx_type idx) const
1066 {
1067         return tabular.getCellInset(idx);
1068 }
1069
1070
1071 shared_ptr<InsetText> InsetTabular::cell(idx_type idx)
1072 {
1073         return tabular.getCellInset(idx);
1074 }
1075
1076
1077 void InsetTabular::cursorPos
1078         (CursorSlice const & sl, bool boundary, int & x, int & y) const
1079 {
1080         cell(sl.idx())->cursorPos(sl, boundary, x, y);
1081
1082         // y offset     correction
1083         int const row = tabular.row_of_cell(sl.idx());
1084         for (int i = 0; i <= row; ++i) {
1085                 if (i != 0) {
1086                         y += tabular.getAscentOfRow(i);
1087                         y += tabular.getAdditionalHeight(i);
1088                 }
1089                 if (i != row)
1090                         y += tabular.getDescentOfRow(i);
1091         }
1092
1093         // x offset correction
1094         int const col = tabular.column_of_cell(sl.idx());
1095         int idx = tabular.getCellNumber(row, 0);
1096         for (int j = 0; j < col; ++j) {
1097                 if (tabular.isPartOfMultiColumn(row, j))
1098                         continue;
1099                 x += tabular.getWidthOfColumn(idx);
1100                 ++idx;
1101         }
1102         x += tabular.getBeginningOfTextInCell(idx);
1103         x += ADD_TO_TABULAR_WIDTH;
1104         x += scx_;
1105 }
1106
1107
1108 int InsetTabular::dist(idx_type const cell, int x, int y) const
1109 {
1110         int xx = 0;
1111         int yy = 0;
1112         InsetBase const & inset = *tabular.getCellInset(cell);
1113         Point o = theCoords.getInsets().xy(&inset);
1114         int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
1115         int const xend = xbeg + tabular.getWidthOfColumn(cell);
1116         int const ybeg = o.y_ - inset.ascent();
1117         row_type const row = tabular.row_of_cell(cell);
1118         int const rowheight = tabular.getAscentOfRow(row)
1119                         + tabular.getDescentOfRow(row);
1120         int const yend = ybeg + rowheight;
1121
1122         if (x < xbeg)
1123                 xx = xbeg - x;
1124         else if (x > xend)
1125                 xx = x - xend;
1126
1127         if (y < ybeg)
1128                 yy = ybeg - y;
1129         else if (y > yend)
1130                 yy = y - yend;
1131
1132         //lyxerr << " xbeg=" << xbeg << "  xend=" << xend
1133         //       << " ybeg=" << ybeg << " yend=" << yend
1134         //       << " xx=" << xx << " yy=" << yy
1135         //       << " dist=" << xx + yy << endl;
1136         return xx + yy;
1137 }
1138
1139
1140 InsetBase * InsetTabular::editXY(LCursor & cur, int x, int y)
1141 {
1142         //lyxerr << "InsetTabular::editXY: " << this << endl;
1143         cur.selection() = false;
1144         cur.push(*this);
1145         cur.idx() = getNearestCell(x, y);
1146         resetPos(cur);
1147         return cell(cur.idx())->text_.editXY(cur, x, y);
1148 }
1149
1150
1151 void InsetTabular::setCursorFromCoordinates(LCursor & cur, int x, int y) const
1152 {
1153         cur.idx() = getNearestCell(x, y);
1154         cell(cur.idx())->text_.setCursorFromCoordinates(cur, x, y);
1155 }
1156
1157
1158 InsetTabular::idx_type InsetTabular::getNearestCell(int x, int y) const
1159 {
1160         idx_type idx_min = 0;
1161         int dist_min = std::numeric_limits<int>::max();
1162         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1163                 if (theCoords.getInsets().has(tabular.getCellInset(i).get())) {
1164                         int const d = dist(i, x, y);
1165                         if (d < dist_min) {
1166                                 dist_min = d;
1167                                 idx_min = i;
1168                         }
1169                 }
1170         }
1171         return idx_min;
1172 }
1173
1174
1175 int InsetTabular::getCellXPos(idx_type const cell) const
1176 {
1177         idx_type c = cell;
1178
1179         for (; !tabular.isFirstCellInRow(c); --c)
1180                 ;
1181         int lx = tabular.getWidthOfColumn(cell);
1182         for (; c < cell; ++c)
1183                 lx += tabular.getWidthOfColumn(c);
1184
1185         return lx - tabular.getWidthOfColumn(cell);
1186 }
1187
1188
1189 void InsetTabular::resetPos(LCursor & cur) const
1190 {
1191         BufferView & bv = cur.bv();
1192         int const maxwidth = bv.workWidth();
1193
1194         if (&cur.inset() != this) {
1195                 scx_ = 0;
1196         } else {
1197                 int const X1 = 0;
1198                 int const X2 = maxwidth;
1199                 int const offset = ADD_TO_TABULAR_WIDTH + 2;
1200                 int const x1 = xo() + getCellXPos(cur.idx()) + offset;
1201                 int const x2 = x1 + tabular.getWidthOfColumn(cur.idx());
1202
1203                 if (x1 < X1)
1204                         scx_ = X1 + 20 - x1;
1205                 else if (x2 > X2)
1206                         scx_ = X2 - 20 - x2;
1207                 else
1208                         scx_ = 0;
1209         }
1210
1211         cur.needsUpdate();
1212
1213         InsetTabularMailer(*this).updateDialog(&bv);
1214 }
1215
1216
1217 void InsetTabular::moveNextCell(LCursor & cur)
1218 {
1219         if (isRightToLeft(cur)) {
1220                 if (tabular.isFirstCellInRow(cur.idx())) {
1221                         row_type const row = tabular.row_of_cell(cur.idx());
1222                         if (row == tabular.rows() - 1)
1223                                 return;
1224                         cur.idx() = tabular.getCellBelow(tabular.getLastCellInRow(row));
1225                 } else {
1226                         if (cur.idx() == 0)
1227                                 return;
1228                         --cur.idx();
1229                 }
1230         } else {
1231                 if (tabular.isLastCell(cur.idx()))
1232                         return;
1233                 ++cur.idx();
1234         }
1235         cur.pit() = 0;
1236         cur.pos() = 0;
1237         resetPos(cur);
1238 }
1239
1240
1241 void InsetTabular::movePrevCell(LCursor & cur)
1242 {
1243         if (isRightToLeft(cur)) {
1244                 if (tabular.isLastCellInRow(cur.idx())) {
1245                         row_type const row = tabular.row_of_cell(cur.idx());
1246                         if (row == 0)
1247                                 return;
1248                         cur.idx() = tabular.getFirstCellInRow(row);
1249                         cur.idx() = tabular.getCellAbove(cur.idx());
1250                 } else {
1251                         if (tabular.isLastCell(cur.idx()))
1252                                 return;
1253                         ++cur.idx();
1254                 }
1255         } else {
1256                 if (cur.idx() == 0) // first cell
1257                         return;
1258                 --cur.idx();
1259         }
1260         cur.pit() = cur.lastpit();
1261         cur.pos() = cur.lastpos();
1262         resetPos(cur);
1263 }
1264
1265
1266 bool InsetTabular::tabularFeatures(LCursor & cur, string const & what)
1267 {
1268         LyXTabular::Feature action = LyXTabular::LAST_ACTION;
1269
1270         int i = 0;
1271         for (; tabularFeature[i].action != LyXTabular::LAST_ACTION; ++i) {
1272                 string const tmp = tabularFeature[i].feature;
1273
1274                 if (tmp == what.substr(0, tmp.length())) {
1275                         //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
1276                         //tabularFeatures[i].feature.length())) {
1277                         action = tabularFeature[i].action;
1278                         break;
1279                 }
1280         }
1281         if (action == LyXTabular::LAST_ACTION)
1282                 return false;
1283
1284         string const val =
1285                 ltrim(what.substr(tabularFeature[i].feature.length()));
1286         tabularFeatures(cur, action, val);
1287         return true;
1288 }
1289
1290
1291 namespace {
1292
1293 void checkLongtableSpecial(LyXTabular::ltType & ltt,
1294                           string const & special, bool & flag)
1295 {
1296         if (special == "dl_above") {
1297                 ltt.topDL = flag;
1298                 ltt.set = false;
1299         } else if (special == "dl_below") {
1300                 ltt.bottomDL = flag;
1301                 ltt.set = false;
1302         } else if (special == "empty") {
1303                 ltt.empty = flag;
1304                 ltt.set = false;
1305         } else if (flag) {
1306                 ltt.empty = false;
1307                 ltt.set = true;
1308         }
1309 }
1310
1311 } // anon namespace
1312
1313
1314 void InsetTabular::tabularFeatures(LCursor & cur,
1315         LyXTabular::Feature feature, string const & value)
1316 {
1317         BufferView & bv = cur.bv();
1318         col_type sel_col_start;
1319         col_type sel_col_end;
1320         row_type sel_row_start;
1321         row_type sel_row_end;
1322         bool setLines = false;
1323         LyXAlignment setAlign = LYX_ALIGN_LEFT;
1324         LyXTabular::VAlignment setVAlign = LyXTabular::LYX_VALIGN_TOP;
1325
1326         switch (feature) {
1327
1328         case LyXTabular::M_ALIGN_LEFT:
1329         case LyXTabular::ALIGN_LEFT:
1330                 setAlign = LYX_ALIGN_LEFT;
1331                 break;
1332
1333         case LyXTabular::M_ALIGN_RIGHT:
1334         case LyXTabular::ALIGN_RIGHT:
1335                 setAlign = LYX_ALIGN_RIGHT;
1336                 break;
1337
1338         case LyXTabular::M_ALIGN_CENTER:
1339         case LyXTabular::ALIGN_CENTER:
1340                 setAlign = LYX_ALIGN_CENTER;
1341                 break;
1342
1343         case LyXTabular::ALIGN_BLOCK:
1344                 setAlign = LYX_ALIGN_BLOCK;
1345                 break;
1346
1347         case LyXTabular::M_VALIGN_TOP:
1348         case LyXTabular::VALIGN_TOP:
1349                 setVAlign = LyXTabular::LYX_VALIGN_TOP;
1350                 break;
1351
1352         case LyXTabular::M_VALIGN_BOTTOM:
1353         case LyXTabular::VALIGN_BOTTOM:
1354                 setVAlign = LyXTabular::LYX_VALIGN_BOTTOM;
1355                 break;
1356
1357         case LyXTabular::M_VALIGN_MIDDLE:
1358         case LyXTabular::VALIGN_MIDDLE:
1359                 setVAlign = LyXTabular::LYX_VALIGN_MIDDLE;
1360                 break;
1361
1362         default:
1363                 break;
1364         }
1365
1366         recordUndoInset(cur, Undo::ATOMIC);
1367
1368         getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
1369         row_type const row = tabular.row_of_cell(cur.idx());
1370         col_type const column = tabular.column_of_cell(cur.idx());
1371         bool flag = true;
1372         LyXTabular::ltType ltt;
1373
1374         switch (feature) {
1375
1376         case LyXTabular::SET_PWIDTH: {
1377                 LyXLength const len(value);
1378                 tabular.setColumnPWidth(cur, cur.idx(), len);
1379                 if (len.zero()
1380                     && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
1381                         tabularFeatures(cur, LyXTabular::ALIGN_CENTER, string());
1382                 break;
1383         }
1384
1385         case LyXTabular::SET_MPWIDTH:
1386                 tabular.setMColumnPWidth(cur, cur.idx(), LyXLength(value));
1387                 break;
1388
1389         case LyXTabular::SET_SPECIAL_COLUMN:
1390         case LyXTabular::SET_SPECIAL_MULTI:
1391                 tabular.setAlignSpecial(cur.idx(),value,feature);
1392                 break;
1393
1394         case LyXTabular::APPEND_ROW:
1395                 // append the row into the tabular
1396                 tabular.appendRow(bv.buffer()->params(), cur.idx());
1397                 break;
1398
1399         case LyXTabular::APPEND_COLUMN:
1400                 // append the column into the tabular
1401                 tabular.appendColumn(bv.buffer()->params(), cur.idx());
1402                 cur.idx() = tabular.getCellNumber(row, column);
1403                 break;
1404
1405         case LyXTabular::DELETE_ROW:
1406                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1407                         tabular.deleteRow(sel_row_start);
1408                 if (sel_row_start >= tabular.rows())
1409                         --sel_row_start;
1410                 cur.idx() = tabular.getCellNumber(sel_row_start, column);
1411                 cur.pit() = 0;
1412                 cur.pos() = 0;
1413                 cur.selection() = false;
1414                 break;
1415
1416         case LyXTabular::DELETE_COLUMN:
1417                 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
1418                         tabular.deleteColumn(sel_col_start);
1419                 if (sel_col_start >= tabular.columns())
1420                         --sel_col_start;
1421                 cur.idx() = tabular.getCellNumber(row, sel_col_start);
1422                 cur.pit() = 0;
1423                 cur.pos() = 0;
1424                 cur.selection() = false;
1425                 break;
1426
1427         case LyXTabular::M_TOGGLE_LINE_TOP:
1428                 flag = false;
1429         case LyXTabular::TOGGLE_LINE_TOP: {
1430                 bool lineSet = !tabular.topLine(cur.idx(), flag);
1431                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1432                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1433                                 tabular.setTopLine(
1434                                         tabular.getCellNumber(i, j),
1435                                         lineSet, flag);
1436                 break;
1437         }
1438
1439         case LyXTabular::M_TOGGLE_LINE_BOTTOM:
1440                 flag = false;
1441         case LyXTabular::TOGGLE_LINE_BOTTOM: {
1442                 bool lineSet = !tabular.bottomLine(cur.idx(), flag);
1443                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1444                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1445                                 tabular.setBottomLine(
1446                                         tabular.getCellNumber(i, j),
1447                                         lineSet,
1448                                         flag);
1449                 break;
1450         }
1451
1452         case LyXTabular::M_TOGGLE_LINE_LEFT:
1453                 flag = false;
1454         case LyXTabular::TOGGLE_LINE_LEFT: {
1455                 bool lineSet = !tabular.leftLine(cur.idx(), flag);
1456                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1457                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1458                                 tabular.setLeftLine(
1459                                         tabular.getCellNumber(i,j),
1460                                         lineSet,
1461                                         flag);
1462                 break;
1463         }
1464
1465         case LyXTabular::M_TOGGLE_LINE_RIGHT:
1466                 flag = false;
1467         case LyXTabular::TOGGLE_LINE_RIGHT: {
1468                 bool lineSet = !tabular.rightLine(cur.idx(), flag);
1469                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1470                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1471                                 tabular.setRightLine(
1472                                         tabular.getCellNumber(i,j),
1473                                         lineSet,
1474                                         flag);
1475                 break;
1476         }
1477
1478         case LyXTabular::M_ALIGN_LEFT:
1479         case LyXTabular::M_ALIGN_RIGHT:
1480         case LyXTabular::M_ALIGN_CENTER:
1481                 flag = false;
1482         case LyXTabular::ALIGN_LEFT:
1483         case LyXTabular::ALIGN_RIGHT:
1484         case LyXTabular::ALIGN_CENTER:
1485         case LyXTabular::ALIGN_BLOCK:
1486                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1487                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1488                                 tabular.setAlignment(
1489                                         tabular.getCellNumber(i, j),
1490                                         setAlign,
1491                                         flag);
1492                 break;
1493
1494         case LyXTabular::M_VALIGN_TOP:
1495         case LyXTabular::M_VALIGN_BOTTOM:
1496         case LyXTabular::M_VALIGN_MIDDLE:
1497                 flag = false;
1498         case LyXTabular::VALIGN_TOP:
1499         case LyXTabular::VALIGN_BOTTOM:
1500         case LyXTabular::VALIGN_MIDDLE:
1501                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1502                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1503                                 tabular.setVAlignment(
1504                                         tabular.getCellNumber(i, j),
1505                                         setVAlign, flag);
1506                 break;
1507
1508         case LyXTabular::MULTICOLUMN: {
1509                 if (sel_row_start != sel_row_end) {
1510 #ifdef WITH_WARNINGS
1511 #warning Need I say it ? This is horrible.
1512 #endif
1513                         Alert::error(_("Error setting multicolumn"),
1514                                    _("You cannot set multicolumn vertically."));
1515                         return;
1516                 }
1517                 if (!cur.selection()) {
1518                         // just multicol for one single cell
1519                         // check whether we are completely in a multicol
1520                         if (tabular.isMultiColumn(cur.idx()))
1521                                 tabular.unsetMultiColumn(cur.idx());
1522                         else
1523                                 tabular.setMultiColumn(bv.buffer(), cur.idx(), 1);
1524                         break;
1525                 }
1526                 // we have a selection so this means we just add all this
1527                 // cells to form a multicolumn cell
1528                 idx_type const s_start = cur.selBegin().idx();
1529                 idx_type const s_end = cur.selEnd().idx();
1530                 tabular.setMultiColumn(bv.buffer(), s_start, s_end - s_start + 1);
1531                 cur.idx() = s_start;
1532                 cur.pit() = 0;
1533                 cur.pos() = 0;
1534                 cur.selection() = false;
1535                 break;
1536         }
1537
1538         case LyXTabular::SET_ALL_LINES:
1539                 setLines = true;
1540         case LyXTabular::UNSET_ALL_LINES:
1541                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1542                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1543                                 tabular.setAllLines(
1544                                         tabular.getCellNumber(i,j), setLines);
1545                 break;
1546
1547         case LyXTabular::SET_LONGTABULAR:
1548                 tabular.setLongTabular(true);
1549                 break;
1550
1551         case LyXTabular::UNSET_LONGTABULAR:
1552                 tabular.setLongTabular(false);
1553                 break;
1554
1555         case LyXTabular::SET_ROTATE_TABULAR:
1556                 tabular.setRotateTabular(true);
1557                 break;
1558
1559         case LyXTabular::UNSET_ROTATE_TABULAR:
1560                 tabular.setRotateTabular(false);
1561                 break;
1562
1563         case LyXTabular::SET_ROTATE_CELL:
1564                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1565                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1566                                 tabular.setRotateCell(
1567                                         tabular.getCellNumber(i, j), true);
1568                 break;
1569
1570         case LyXTabular::UNSET_ROTATE_CELL:
1571                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1572                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1573                                 tabular.setRotateCell(
1574                                         tabular.getCellNumber(i, j), false);
1575                 break;
1576
1577         case LyXTabular::SET_USEBOX: {
1578                 LyXTabular::BoxType val = LyXTabular::BoxType(convert<int>(value));
1579                 if (val == tabular.getUsebox(cur.idx()))
1580                         val = LyXTabular::BOX_NONE;
1581                 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
1582                         for (col_type j = sel_col_start; j <= sel_col_end; ++j)
1583                                 tabular.setUsebox(tabular.getCellNumber(i, j), val);
1584                 break;
1585         }
1586
1587         case LyXTabular::UNSET_LTFIRSTHEAD:
1588                 flag = false;
1589         case LyXTabular::SET_LTFIRSTHEAD:
1590                 tabular.getRowOfLTFirstHead(row, ltt);
1591                 checkLongtableSpecial(ltt, value, flag);
1592                 tabular.setLTHead(row, flag, ltt, true);
1593                 break;
1594
1595         case LyXTabular::UNSET_LTHEAD:
1596                 flag = false;
1597         case LyXTabular::SET_LTHEAD:
1598                 tabular.getRowOfLTHead(row, ltt);
1599                 checkLongtableSpecial(ltt, value, flag);
1600                 tabular.setLTHead(row, flag, ltt, false);
1601                 break;
1602
1603         case LyXTabular::UNSET_LTFOOT:
1604                 flag = false;
1605         case LyXTabular::SET_LTFOOT:
1606                 tabular.getRowOfLTFoot(row, ltt);
1607                 checkLongtableSpecial(ltt, value, flag);
1608                 tabular.setLTFoot(row, flag, ltt, false);
1609                 break;
1610
1611         case LyXTabular::UNSET_LTLASTFOOT:
1612                 flag = false;
1613         case LyXTabular::SET_LTLASTFOOT:
1614                 tabular.getRowOfLTLastFoot(row, ltt);
1615                 checkLongtableSpecial(ltt, value, flag);
1616                 tabular.setLTFoot(row, flag, ltt, true);
1617                 break;
1618
1619         case LyXTabular::SET_LTNEWPAGE:
1620                 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
1621                 break;
1622
1623         // dummy stuff just to avoid warnings
1624         case LyXTabular::LAST_ACTION:
1625                 break;
1626         }
1627
1628         InsetTabularMailer(*this).updateDialog(&bv);
1629 }
1630
1631
1632 bool InsetTabular::showInsetDialog(BufferView * bv) const
1633 {
1634         InsetTabularMailer(*this).showDialog(bv);
1635         return true;
1636 }
1637
1638
1639 void InsetTabular::openLayoutDialog(BufferView * bv) const
1640 {
1641         InsetTabularMailer(*this).showDialog(bv);
1642 }
1643
1644
1645 bool InsetTabular::copySelection(LCursor & cur)
1646 {
1647         if (!cur.selection())
1648                 return false;
1649
1650         row_type rs, re;
1651         col_type cs, ce;
1652         getSelection(cur, rs, re, cs, ce);
1653
1654         paste_tabular.reset(new LyXTabular(tabular));
1655
1656         for (row_type i = 0; i < rs; ++i)
1657                 paste_tabular->deleteRow(0);
1658
1659         row_type const rows = re - rs + 1;
1660         while (paste_tabular->rows() > rows)
1661                 paste_tabular->deleteRow(rows);
1662
1663         paste_tabular->setTopLine(0, true, true);
1664         paste_tabular->setBottomLine(paste_tabular->getFirstCellInRow(rows - 1),
1665                                      true, true);
1666
1667         for (col_type i = 0; i < cs; ++i)
1668                 paste_tabular->deleteColumn(0);
1669
1670         col_type const columns = ce - cs + 1;
1671         while (paste_tabular->columns() > columns)
1672                 paste_tabular->deleteColumn(columns);
1673
1674         paste_tabular->setLeftLine(0, true, true);
1675         paste_tabular->setRightLine(paste_tabular->getLastCellInRow(0),
1676                                     true, true);
1677
1678         ostringstream os;
1679         OutputParams const runparams;
1680         paste_tabular->plaintext(cur.buffer(), os, runparams, 0, true, '\t');
1681         cur.bv().stuffClipboard(os.str());
1682         // mark tabular stack dirty
1683         // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
1684         // when we (hopefully) have a one-for-all paste mechanism.
1685         lyx::cap::dirtyTabularStack(true);
1686
1687         return true;
1688 }
1689
1690
1691 bool InsetTabular::pasteSelection(LCursor & cur)
1692 {
1693         if (!paste_tabular)
1694                 return false;
1695         col_type const actcol = tabular.column_of_cell(cur.idx());
1696         row_type const actrow = tabular.row_of_cell(cur.idx());
1697         for (row_type r1 = 0, r2 = actrow;
1698              r1 < paste_tabular->rows() && r2 < tabular.rows();
1699              ++r1, ++r2) {
1700                 for (col_type c1 = 0, c2 = actcol;
1701                     c1 < paste_tabular->columns() && c2 < tabular.columns();
1702                     ++c1, ++c2) {
1703                         if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
1704                             tabular.isPartOfMultiColumn(r2, c2))
1705                                 continue;
1706                         if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
1707                                 --c2;
1708                                 continue;
1709                         }
1710                         if (tabular.isPartOfMultiColumn(r2, c2)) {
1711                                 --c1;
1712                                 continue;
1713                         }
1714                         shared_ptr<InsetText> inset(
1715                                 new InsetText(*paste_tabular->getCellInset(r1, c1)));
1716                         tabular.setCellInset(r2, c2, inset);
1717                         inset->markNew();
1718                         cur.pos() = 0;
1719                 }
1720         }
1721         return true;
1722 }
1723
1724
1725 void InsetTabular::cutSelection(LCursor & cur)
1726 {
1727         if (!cur.selection())
1728                 return;
1729
1730         row_type rs, re;
1731         col_type cs, ce;
1732         getSelection(cur, rs, re, cs, ce);
1733         for (row_type i = rs; i <= re; ++i) {
1734                 for (col_type j = cs; j <= ce; ++j) {
1735                         shared_ptr<InsetText> t
1736                                 = cell(tabular.getCellNumber(i, j));
1737                         if (cur.buffer().params().tracking_changes)
1738                                 t->markErased(true);
1739                         else
1740                                 t->clear();
1741                 }
1742         }
1743
1744         // cursor position might be invalid now
1745         cur.pos() = cur.lastpos();
1746         cur.clearSelection();
1747 }
1748
1749
1750 bool InsetTabular::isRightToLeft(LCursor & cur) const
1751 {
1752         BOOST_ASSERT(cur.depth() > 1);
1753         Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
1754         LCursor::pos_type const parentpos = cur[cur.depth() - 2].pos();
1755         return parentpar.getFontSettings(cur.bv().buffer()->params(),
1756                                          parentpos).language()->rightToLeft();
1757 }
1758
1759
1760 void InsetTabular::getSelection(LCursor & cur,
1761         row_type & rs, row_type & re, col_type & cs, col_type & ce) const
1762 {
1763         CursorSlice const & beg = cur.selBegin();
1764         CursorSlice const & end = cur.selEnd();
1765         cs = tabular.column_of_cell(beg.idx());
1766         ce = tabular.column_of_cell(end.idx());
1767         if (cs > ce) {
1768                 ce = cs;
1769                 cs = tabular.column_of_cell(end.idx());
1770         } else {
1771                 ce = tabular.right_column_of_cell(end.idx());
1772         }
1773
1774         rs = tabular.row_of_cell(beg.idx());
1775         re = tabular.row_of_cell(end.idx());
1776         if (rs > re)
1777                 swap(rs, re);
1778 }
1779
1780
1781 LyXText * InsetTabular::getText(int idx) const
1782 {
1783         return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
1784 }
1785
1786
1787 void InsetTabular::markErased(bool erased)
1788 {
1789         for (idx_type idx = 0; idx < nargs(); ++idx)
1790                 cell(idx)->markErased(erased);
1791 }
1792
1793
1794 bool InsetTabular::forceDefaultParagraphs(idx_type cell) const
1795 {
1796         return tabular.getPWidth(cell).zero();
1797 }
1798
1799
1800 bool InsetTabular::insertAsciiString(BufferView & bv, string const & buf,
1801                                      bool usePaste)
1802 {
1803         if (buf.length() <= 0)
1804                 return true;
1805
1806         col_type cols = 1;
1807         row_type rows = 1;
1808         col_type maxCols = 1;
1809         string::size_type const len = buf.length();
1810         string::size_type p = 0;
1811
1812         while (p < len && (p = buf.find_first_of("\t\n", p)) != string::npos) {
1813                 switch (buf[p]) {
1814                 case '\t':
1815                         ++cols;
1816                         break;
1817                 case '\n':
1818                         if (p + 1 < len)
1819                                 ++rows;
1820                         maxCols = max(cols, maxCols);
1821                         cols = 1;
1822                         break;
1823                 }
1824                 ++p;
1825         }
1826         maxCols = max(cols, maxCols);
1827         LyXTabular * loctab;
1828         idx_type cell = 0;
1829         col_type ocol = 0;
1830         row_type row = 0;
1831         if (usePaste) {
1832                 paste_tabular.reset(
1833                         new LyXTabular(bv.buffer()->params(), rows, maxCols));
1834                 loctab = paste_tabular.get();
1835                 cols = 0;
1836                 dirtyTabularStack(true);
1837         } else {
1838                 loctab = &tabular;
1839                 cell = bv.cursor().idx();
1840                 ocol = tabular.column_of_cell(cell);
1841                 row = tabular.row_of_cell(cell);
1842         }
1843
1844         string::size_type op = 0;
1845         idx_type const cells = loctab->getNumberOfCells();
1846         p = 0;
1847         cols = ocol;
1848         rows = loctab->rows();
1849         col_type const columns = loctab->columns();
1850
1851         while (cell < cells && p < len && row < rows &&
1852                (p = buf.find_first_of("\t\n", p)) != string::npos)
1853         {
1854                 if (p >= len)
1855                         break;
1856                 switch (buf[p]) {
1857                 case '\t':
1858                         // we can only set this if we are not too far right
1859                         if (cols < columns) {
1860                                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1861                                 inset->setViewCache(&bv);
1862                                 Paragraph & par = inset->text_.getPar(0);
1863                                 LyXFont const font = inset->text_.getFont(par, 0);
1864                                 inset->setText(buf.substr(op, p - op), font);
1865                                 ++cols;
1866                                 ++cell;
1867                         }
1868                         break;
1869                 case '\n':
1870                         // we can only set this if we are not too far right
1871                         if (cols < columns) {
1872                                 shared_ptr<InsetText> inset = tabular.getCellInset(cell);
1873                                 inset->setViewCache(&bv);
1874                                 Paragraph & par = inset->text_.getPar(0);
1875                                 LyXFont const font = inset->text_.getFont(par, 0);
1876                                 inset->setText(buf.substr(op, p - op), font);
1877                         }
1878                         cols = ocol;
1879                         ++row;
1880                         if (row < rows)
1881                                 cell = loctab->getCellNumber(row, cols);
1882                         break;
1883                 }
1884                 ++p;
1885                 op = p;
1886         }
1887         // check for the last cell if there is no trailing '\n'
1888         if (cell < cells && op < len) {
1889                 shared_ptr<InsetText> inset = loctab->getCellInset(cell);
1890                 inset->setViewCache(&bv);
1891                 Paragraph & par = inset->text_.getPar(0);
1892                 LyXFont const font = inset->text_.getFont(par, 0);
1893                 inset->setText(buf.substr(op, len - op), font);
1894         }
1895         return true;
1896 }
1897
1898
1899 void InsetTabular::addPreview(PreviewLoader & loader) const
1900 {
1901         row_type const rows = tabular.rows();
1902         col_type const columns = tabular.columns();
1903         for (row_type i = 0; i < rows; ++i) {
1904                 for (col_type j = 0; j < columns; ++j)
1905                         tabular.getCellInset(i, j)->addPreview(loader);
1906         }
1907 }
1908
1909
1910 bool InsetTabular::tablemode(LCursor & cur) const
1911 {
1912         return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
1913 }
1914
1915
1916
1917
1918
1919 string const InsetTabularMailer::name_("tabular");
1920
1921 InsetTabularMailer::InsetTabularMailer(InsetTabular const & inset)
1922         : inset_(const_cast<InsetTabular &>(inset))
1923 {}
1924
1925
1926 string const InsetTabularMailer::inset2string(Buffer const &) const
1927 {
1928         return params2string(inset_);
1929 }
1930
1931
1932 void InsetTabularMailer::string2params(string const & in, InsetTabular & inset)
1933 {
1934         istringstream data(in);
1935         LyXLex lex(0,0);
1936         lex.setStream(data);
1937
1938         if (in.empty())
1939                 return;
1940
1941         string token;
1942         lex >> token;
1943         if (!lex || token != name_)
1944                 return print_mailer_error("InsetTabularMailer", in, 1,
1945                                           name_);
1946
1947         // This is part of the inset proper that is usually swallowed
1948         // by Buffer::readInset
1949         lex >> token;
1950         if (!lex || token != "Tabular")
1951                 return print_mailer_error("InsetTabularMailer", in, 2,
1952                                           "Tabular");
1953
1954         Buffer const & buffer = inset.buffer();
1955         inset.read(buffer, lex);
1956 }
1957
1958
1959 string const InsetTabularMailer::params2string(InsetTabular const & inset)
1960 {
1961         ostringstream data;
1962         data << name_ << ' ';
1963         inset.write(inset.buffer(), data);
1964         data << "\\end_inset\n";
1965         return data.str();
1966 }