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