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