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