]> git.lyx.org Git - features.git/blob - src/CutAndPaste.cpp
c3367215e3b947f47ad6266de3dbf635dbc1f63e
[features.git] / src / CutAndPaste.cpp
1 /**
2  * \file CutAndPaste.cpp
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  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Michael Gerz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "CutAndPaste.h"
17
18 #include "BranchList.h"
19 #include "Buffer.h"
20 #include "buffer_funcs.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "Changes.h"
25 #include "Cursor.h"
26 #include "Encoding.h"
27 #include "ErrorList.h"
28 #include "FuncCode.h"
29 #include "FuncRequest.h"
30 #include "InsetIterator.h"
31 #include "InsetList.h"
32 #include "Language.h"
33 #include "LyX.h"
34 #include "LyXRC.h"
35 #include "Text.h"
36 #include "Paragraph.h"
37 #include "ParagraphParameters.h"
38 #include "ParIterator.h"
39 #include "TextClass.h"
40
41 #include "insets/InsetBibitem.h"
42 #include "insets/InsetBranch.h"
43 #include "insets/InsetCommand.h"
44 #include "insets/InsetFlex.h"
45 #include "insets/InsetGraphics.h"
46 #include "insets/InsetGraphicsParams.h"
47 #include "insets/InsetInclude.h"
48 #include "insets/InsetLabel.h"
49 #include "insets/InsetTabular.h"
50
51 #include "mathed/MathData.h"
52 #include "mathed/InsetMath.h"
53 #include "mathed/InsetMathHull.h"
54 #include "mathed/InsetMathRef.h"
55 #include "mathed/MathSupport.h"
56
57 #include "support/debug.h"
58 #include "support/docstream.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/limited_stack.h"
62 #include "support/lstrings.h"
63 #include "support/TempFile.h"
64
65 #include "frontends/alert.h"
66 #include "frontends/Clipboard.h"
67 #include "frontends/Selection.h"
68
69 #include <boost/tuple/tuple.hpp>
70 #include <boost/next_prior.hpp>
71
72 #include <string>
73
74 using namespace std;
75 using namespace lyx::support;
76 using lyx::frontend::Clipboard;
77
78 namespace lyx {
79
80 namespace {
81
82 typedef pair<pit_type, int> PitPosPair;
83
84 typedef limited_stack<pair<ParagraphList, DocumentClassConstPtr> > CutStack;
85
86 CutStack theCuts(10);
87 // persistent selection, cleared until the next selection
88 CutStack selectionBuffer(1);
89
90 // store whether the tabular stack is newer than the normal copy stack
91 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
92 // when we (hopefully) have a one-for-all paste mechanism.
93 bool dirty_tabular_stack_ = false;
94
95
96 bool checkPastePossible(int index)
97 {
98         return size_t(index) < theCuts.size() && !theCuts[index].first.empty();
99 }
100
101
102 struct PasteReturnValue {
103         PasteReturnValue(pit_type r_par, pos_type r_pos, bool r_nu) :
104           par(r_par), pos(r_pos), needupdate(r_nu)
105         {}
106
107         pit_type par;
108         pos_type pos;
109         bool needupdate;
110 };
111
112 PasteReturnValue
113 pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
114                      DocumentClassConstPtr oldDocClass, Buffer * tmpbuffer,
115                      ErrorList & errorlist)
116 {
117         Buffer const & buffer = *cur.buffer();
118         pit_type pit = cur.pit();
119         pos_type pos = cur.pos();
120         bool need_update = false;
121
122         if (parlist.empty())
123                 return PasteReturnValue(pit, pos, need_update);
124
125         InsetText * target_inset = cur.inset().asInsetText();
126         if (!target_inset) {
127                 InsetTabular * it = cur.inset().asInsetTabular();
128                 target_inset = it? it->cell(cur.idx())->asInsetText() : 0;
129         }
130         LASSERT(target_inset, return PasteReturnValue(pit, pos, need_update));
131
132         ParagraphList & pars = target_inset->paragraphs();
133         LASSERT(pos <= pars[pit].size(),
134                         return PasteReturnValue(pit, pos, need_update));
135
136         // Make a copy of the CaP paragraphs.
137         ParagraphList insertion = parlist;
138
139         // Now remove all out of the pars which is NOT allowed in the
140         // new environment and set also another font if that is required.
141
142         // Convert newline to paragraph break in ParbreakIsNewline
143         if (target_inset->getLayout().parbreakIsNewline()
144             || pars[pit].layout().parbreak_is_newline) {
145                 for (size_t i = 0; i != insertion.size(); ++i) {
146                         for (pos_type j = 0; j != insertion[i].size(); ++j) {
147                                 if (insertion[i].isNewline(j)) {
148                                         // do not track deletion of newline
149                                         insertion[i].eraseChar(j, false);
150                                         insertion[i].setInsetOwner(target_inset);
151                                         breakParagraphConservative(
152                                                         buffer.params(),
153                                                         insertion, i, j);
154                                         break;
155                                 }
156                         }
157                 }
158         }
159
160         // set the paragraphs to plain layout if necessary
161         DocumentClassConstPtr newDocClass = buffer.params().documentClassPtr();
162         if (cur.inset().usePlainLayout()) {
163                 bool forcePlainLayout = cur.inset().forcePlainLayout();
164                 Layout const & plainLayout = newDocClass->plainLayout();
165                 Layout const & defaultLayout = newDocClass->defaultLayout();
166                 ParagraphList::iterator const end = insertion.end();
167                 ParagraphList::iterator par = insertion.begin();
168                 for (; par != end; ++par) {
169                         Layout const & parLayout = par->layout();
170                         if (forcePlainLayout || parLayout == defaultLayout)
171                                 par->setLayout(plainLayout);
172                 }
173         }       else { // check if we need to reset from plain layout
174                 Layout const & defaultLayout = newDocClass->defaultLayout();
175                 Layout const & plainLayout = newDocClass->plainLayout();
176                 ParagraphList::iterator const end = insertion.end();
177                 ParagraphList::iterator par = insertion.begin();
178                 for (; par != end; ++par) {
179                         Layout const & parLayout = par->layout();
180                         if (parLayout == plainLayout)
181                                 par->setLayout(defaultLayout);
182                 }
183         }
184
185         InsetText in(cur.buffer());
186         // Make sure there is no class difference.
187         in.paragraphs().clear();
188         // This works without copying any paragraph data because we have
189         // a specialized swap method for ParagraphList. This is important
190         // since we store pointers to insets at some places and we don't
191         // want to invalidate them.
192         insertion.swap(in.paragraphs());
193         cap::switchBetweenClasses(oldDocClass, newDocClass, in, errorlist);
194         insertion.swap(in.paragraphs());
195
196         ParagraphList::iterator tmpbuf = insertion.begin();
197         int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
198
199         depth_type max_depth = pars[pit].getMaxDepthAfter();
200
201         for (; tmpbuf != insertion.end(); ++tmpbuf) {
202                 // If we have a negative jump so that the depth would
203                 // go below 0 depth then we have to redo the delta to
204                 // this new max depth level so that subsequent
205                 // paragraphs are aligned correctly to this paragraph
206                 // at level 0.
207                 if (int(tmpbuf->params().depth()) + depth_delta < 0)
208                         depth_delta = 0;
209
210                 // Set the right depth so that we are not too deep or shallow.
211                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
212                 if (tmpbuf->params().depth() > max_depth)
213                         tmpbuf->params().depth(max_depth);
214
215                 // Set max_depth for the next paragraph
216                 max_depth = tmpbuf->getMaxDepthAfter();
217
218                 // Set the inset owner of this paragraph.
219                 tmpbuf->setInsetOwner(target_inset);
220                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
221                         // do not track deletion of invalid insets
222                         if (Inset * inset = tmpbuf->getInset(i))
223                                 if (!target_inset->insetAllowed(inset->lyxCode()))
224                                         tmpbuf->eraseChar(i--, false);
225                 }
226
227                 tmpbuf->setChange(Change(buffer.params().track_changes ?
228                                          Change::INSERTED : Change::UNCHANGED));
229         }
230
231         bool const empty = pars[pit].empty();
232         if (!empty) {
233                 // Make the buf exactly the same layout as the cursor
234                 // paragraph.
235                 insertion.begin()->makeSameLayout(pars[pit]);
236         }
237
238         // Prepare the paragraphs and insets for insertion.
239         insertion.swap(in.paragraphs());
240
241         InsetIterator const i_end = inset_iterator_end(in);
242         for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
243                 // Even though this will also be done later, it has to be done here 
244                 // since some inset might going to try to access
245                 // the buffer() member.
246                 it->setBuffer(const_cast<Buffer &>(buffer));
247                 switch (it->lyxCode()) {
248
249                 case MATH_HULL_CODE: {
250                         // check for equation labels and resolve duplicates
251                         InsetMathHull * ins = it->asInsetMath()->asHullInset();
252                         std::vector<InsetLabel *> labels = ins->getLabels();
253                         for (size_t i = 0; i != labels.size(); ++i) {
254                                 if (!labels[i])
255                                         continue;
256                                 InsetLabel * lab = labels[i];
257                                 docstring const oldname = lab->getParam("name");
258                                 lab->updateLabel(oldname);
259                                 // We need to update the buffer reference cache.
260                                 need_update = true;
261                                 docstring const newname = lab->getParam("name");
262                                 if (oldname == newname)
263                                         continue;
264                                 // adapt the references
265                                 for (InsetIterator itt = inset_iterator_begin(in);
266                                       itt != i_end; ++itt) {
267                                         if (itt->lyxCode() == REF_CODE) {
268                                                 InsetCommand * ref = itt->asInsetCommand();
269                                                 if (ref->getParam("reference") == oldname)
270                                                         ref->setParam("reference", newname);
271                                         } else if (itt->lyxCode() == MATH_REF_CODE) {
272                                                 InsetMathRef * mi = itt->asInsetMath()->asRefInset();
273                                                 // this is necessary to prevent an uninitialized
274                                                 // buffer when the RefInset is in a MathBox.
275                                                 // FIXME audit setBuffer calls
276                                                 mi->setBuffer(const_cast<Buffer &>(buffer));
277                                                 if (mi->getTarget() == oldname)
278                                                         mi->changeTarget(newname);
279                                         }
280                                 }
281                         }
282                         break;
283                 }
284
285                 case LABEL_CODE: {
286                         // check for duplicates
287                         InsetLabel & lab = static_cast<InsetLabel &>(*it);
288                         docstring const oldname = lab.getParam("name");
289                         lab.updateLabel(oldname);
290                         // We need to update the buffer reference cache.
291                         need_update = true;
292                         docstring const newname = lab.getParam("name");
293                         if (oldname == newname)
294                                 break;
295                         // adapt the references
296                         for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
297                                 if (itt->lyxCode() == REF_CODE) {
298                                         InsetCommand & ref = static_cast<InsetCommand &>(*itt);
299                                         if (ref.getParam("reference") == oldname)
300                                                 ref.setParam("reference", newname);
301                                 } else if (itt->lyxCode() == MATH_REF_CODE) {
302                                         InsetMathRef * mi = itt->asInsetMath()->asRefInset();
303                                         // this is necessary to prevent an uninitialized
304                                         // buffer when the RefInset is in a MathBox.
305                                         // FIXME audit setBuffer calls
306                                         mi->setBuffer(const_cast<Buffer &>(buffer));
307                                         if (mi->getTarget() == oldname)
308                                                 mi->changeTarget(newname);
309                                 }
310                         }
311                         break;
312                 }
313
314                 case INCLUDE_CODE: {
315                         InsetInclude & inc = static_cast<InsetInclude &>(*it);
316                         inc.updateCommand();
317                         // We need to update the list of included files.
318                         need_update = true;
319                         break;
320                 }
321
322                 case BIBITEM_CODE: {
323                         // check for duplicates
324                         InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
325                         docstring const oldkey = bib.getParam("key");
326                         bib.updateCommand(oldkey, false);
327                         // We need to update the buffer reference cache.
328                         need_update = true;
329                         docstring const newkey = bib.getParam("key");
330                         if (oldkey == newkey)
331                                 break;
332                         // adapt the references
333                         for (InsetIterator itt = inset_iterator_begin(in);
334                              itt != i_end; ++itt) {
335                                 if (itt->lyxCode() == CITE_CODE) {
336                                         InsetCommand * ref = itt->asInsetCommand();
337                                         if (ref->getParam("key") == oldkey)
338                                                 ref->setParam("key", newkey);
339                                 }
340                         }
341                         break;
342                 }
343
344                 case BRANCH_CODE: {
345                         // check if branch is known to target buffer
346                         // or its master
347                         InsetBranch & br = static_cast<InsetBranch &>(*it);
348                         docstring const name = br.branch();
349                         if (name.empty())
350                                 break;
351                         bool const is_child = (&buffer != buffer.masterBuffer());
352                         BranchList branchlist = buffer.params().branchlist();
353                         if ((!is_child && branchlist.find(name))
354                             || (is_child && (branchlist.find(name)
355                                 || buffer.masterBuffer()->params().branchlist().find(name))))
356                                 break;
357                         if (tmpbuffer) {
358                                 // This is for a temporary buffer, so simply create the branch.
359                                 // Must not use lyx::dispatch(), since tmpbuffer has no view.
360                                 DispatchResult dr;
361                                 tmpbuffer->dispatch(FuncRequest(LFUN_BRANCH_ADD, name), dr);
362                         } else {
363                                 docstring text = bformat(
364                                         _("The pasted branch \"%1$s\" is undefined.\n"
365                                           "Do you want to add it to the document's branch list?"),
366                                         name);
367                                 if (frontend::Alert::prompt(_("Unknown branch"),
368                                           text, 0, 1, _("&Add"), _("&Don't Add")) != 0)
369                                         break;
370                                 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, name));
371                         }
372                         // We need to update the list of branches.
373                         need_update = true;
374                         break;
375                 }
376
377                 default:
378                         break; // nothing
379                 }
380         }
381         insertion.swap(in.paragraphs());
382
383         // Split the paragraph for inserting the buf if necessary.
384         if (!empty)
385                 breakParagraphConservative(buffer.params(), pars, pit, pos);
386
387         // Paste it!
388         if (empty) {
389                 pars.insert(boost::next(pars.begin(), pit),
390                             insertion.begin(),
391                             insertion.end());
392
393                 // merge the empty par with the last par of the insertion
394                 mergeParagraph(buffer.params(), pars,
395                                pit + insertion.size() - 1);
396         } else {
397                 pars.insert(boost::next(pars.begin(), pit + 1),
398                             insertion.begin(),
399                             insertion.end());
400
401                 // merge the first par of the insertion with the current par
402                 mergeParagraph(buffer.params(), pars, pit);
403         }
404
405         // Store the new cursor position.
406         pit_type last_paste = pit + insertion.size() - 1;
407         pit_type startpit = pit;
408         pit = last_paste;
409         pos = pars[last_paste].size();
410
411         // FIXME Should we do it here, or should we let updateBuffer() do it?
412         // Set paragraph buffers. It's important to do this right away
413         // before something calls Inset::buffer() and causes a crash.
414         for (pit_type p = startpit; p <= pit; ++p)
415                 pars[p].setBuffer(const_cast<Buffer &>(buffer));
416
417         // Join (conditionally) last pasted paragraph with next one, i.e.,
418         // the tail of the spliced document paragraph
419         if (!empty && last_paste + 1 != pit_type(pars.size())) {
420                 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
421                         mergeParagraph(buffer.params(), pars, last_paste);
422                 } else if (pars[last_paste + 1].empty()) {
423                         pars[last_paste + 1].makeSameLayout(pars[last_paste]);
424                         mergeParagraph(buffer.params(), pars, last_paste);
425                 } else if (pars[last_paste].empty()) {
426                         pars[last_paste].makeSameLayout(pars[last_paste + 1]);
427                         mergeParagraph(buffer.params(), pars, last_paste);
428                 } else {
429                         pars[last_paste + 1].stripLeadingSpaces(buffer.params().track_changes);
430                         ++last_paste;
431                 }
432         }
433
434         return PasteReturnValue(pit, pos, need_update);
435 }
436
437
438 PitPosPair eraseSelectionHelper(BufferParams const & params,
439         ParagraphList & pars,
440         pit_type startpit, pit_type endpit,
441         int startpos, int endpos)
442 {
443         // Start of selection is really invalid.
444         if (startpit == pit_type(pars.size()) ||
445             (startpos > pars[startpit].size()))
446                 return PitPosPair(endpit, endpos);
447
448         // Start and end is inside same paragraph
449         if (endpit == pit_type(pars.size()) || startpit == endpit) {
450                 endpos -= pars[startpit].eraseChars(startpos, endpos, params.track_changes);
451                 return PitPosPair(endpit, endpos);
452         }
453
454         for (pit_type pit = startpit; pit != endpit + 1;) {
455                 pos_type const left  = (pit == startpit ? startpos : 0);
456                 pos_type right = (pit == endpit ? endpos : pars[pit].size() + 1);
457                 bool const merge = pars[pit].isMergedOnEndOfParDeletion(params.track_changes);
458
459                 // Logically erase only, including the end-of-paragraph character
460                 pars[pit].eraseChars(left, right, params.track_changes);
461
462                 // Separate handling of paragraph break:
463                 if (merge && pit != endpit &&
464                     (pit + 1 != endpit 
465                      || pars[pit].hasSameLayout(pars[endpit])
466                      || pars[endpit].size() == endpos)) {
467                         if (pit + 1 == endpit)
468                                 endpos += pars[pit].size();
469                         mergeParagraph(params, pars, pit);
470                         --endpit;
471                 } else
472                         ++pit;
473         }
474
475         // Ensure legal cursor pos:
476         endpit = startpit;
477         endpos = startpos;
478         return PitPosPair(endpit, endpos);
479 }
480
481
482 void putClipboard(ParagraphList const & paragraphs, 
483         DocumentClassConstPtr docclass, docstring const & plaintext)
484 {
485         // This used to need to be static to avoid a memory leak. It no longer needs
486         // to be so, but the alternative is to construct a new one of these (with a
487         // new temporary directory, etc) every time, and then to destroy it. So maybe
488         // it's worth just keeping this one around.
489         // FIXME THREAD
490         static TempFile tempfile("clipboard.internal");
491         tempfile.setAutoRemove(false);
492         static Buffer * staticbuffer = theBufferList().newInternalBuffer(
493                         tempfile.name().absFileName());
494
495         // These two things only really need doing the first time.
496         staticbuffer->setUnnamed(true);
497         staticbuffer->inset().setBuffer(*staticbuffer);
498
499         // Use a clone for the complicated stuff so that we do not need to clean
500         // up in order to avoid a crash.
501         Buffer * buffer = staticbuffer->cloneBufferOnly();
502         LASSERT(buffer, return);
503
504         // This needs doing every time.
505         buffer->params().setDocumentClass(docclass);
506
507         // we will use pasteSelectionHelper to copy the paragraphs into the
508         // temporary Buffer, since it does a lot of things to fix them up.
509         DocIterator dit = doc_iterator_begin(buffer, &buffer->inset());
510         ErrorList el;
511         pasteSelectionHelper(dit, paragraphs, docclass, buffer, el);
512
513         // We don't want to produce images that are not used. Therefore,
514         // output formulas as MathML. Even if this is not understood by all
515         // applications, the number that can parse it should go up in the future.
516         buffer->params().html_math_output = BufferParams::MathML;
517
518         // Make sure MarkAsExporting is deleted before buffer is
519         {
520                 // The Buffer is being used to export. This is necessary so that the
521                 // updateMacros call will record the needed information.
522                 MarkAsExporting mex(buffer);
523
524                 buffer->updateBuffer(Buffer::UpdateMaster, OutputUpdate);
525                 buffer->updateMacros();
526                 buffer->updateMacroInstances(OutputUpdate);
527
528                 // LyX's own format
529                 string lyx;
530                 ostringstream oslyx;
531                 if (buffer->write(oslyx))
532                         lyx = oslyx.str();
533
534                 // XHTML format
535                 odocstringstream oshtml;
536                 OutputParams runparams(encodings.fromLyXName("utf8"));
537                 // We do not need to produce images, etc.
538                 runparams.dryrun = true;
539                 // We are not interested in errors (bug 8866)
540                 runparams.silent = true;
541                 buffer->writeLyXHTMLSource(oshtml, runparams, Buffer::FullSource);
542
543                 theClipboard().put(lyx, oshtml.str(), plaintext);
544         }
545
546         // Save that memory
547         delete buffer;
548 }
549
550
551 /// return true if the whole ParagraphList is deleted
552 static bool isFullyDeleted(ParagraphList const & pars)
553 {
554         pit_type const pars_size = static_cast<pit_type>(pars.size());
555
556         // check all paragraphs
557         for (pit_type pit = 0; pit < pars_size; ++pit) {
558                 if (!pars[pit].empty())   // prevent assertion failure
559                         if (!pars[pit].isDeleted(0, pars[pit].size()))
560                                 return false;
561         }
562         return true;
563 }
564
565
566 void copySelectionHelper(Buffer const & buf, Text const & text,
567         pit_type startpit, pit_type endpit,
568         int start, int end, DocumentClassConstPtr dc, CutStack & cutstack)
569 {
570         ParagraphList const & pars = text.paragraphs();
571
572         // In most of these cases, we can try to recover.
573         LASSERT(0 <= start, start = 0);
574         LASSERT(start <= pars[startpit].size(), start = pars[startpit].size());
575         LASSERT(0 <= end, end = 0);
576         LASSERT(end <= pars[endpit].size(), end = pars[endpit].size());
577         LASSERT(startpit != endpit || start <= end, return);
578
579         // Clone the paragraphs within the selection.
580         ParagraphList copy_pars(boost::next(pars.begin(), startpit),
581                                 boost::next(pars.begin(), endpit + 1));
582
583         // Remove the end of the last paragraph; afterwards, remove the
584         // beginning of the first paragraph. Keep this order - there may only
585         // be one paragraph!  Do not track deletions here; this is an internal
586         // action not visible to the user
587
588         Paragraph & back = copy_pars.back();
589         back.eraseChars(end, back.size(), false);
590         Paragraph & front = copy_pars.front();
591         front.eraseChars(0, start, false);
592
593         ParagraphList::iterator it = copy_pars.begin();
594         ParagraphList::iterator it_end = copy_pars.end();
595
596         for (; it != it_end; ++it) {
597                 // Since we have a copy of the paragraphs, the insets
598                 // do not have a proper buffer reference. It makes
599                 // sense to add them temporarily, because the
600                 // operations below depend on that (acceptChanges included).
601                 it->setBuffer(const_cast<Buffer &>(buf));
602                 // PassThru paragraphs have the Language
603                 // latex_language. This is invalid for others, so we
604                 // need to change it to the buffer language.
605                 if (it->isPassThru())
606                         it->changeLanguage(buf.params(), 
607                                            latex_language, buf.language());
608         }
609
610         // do not copy text (also nested in insets) which is marked as
611         // deleted, unless the whole selection was deleted
612         if (!isFullyDeleted(copy_pars))
613                 acceptChanges(copy_pars, buf.params());
614         else
615                 rejectChanges(copy_pars, buf.params());
616
617
618         // do some final cleanup now, to make sure that the paragraphs
619         // are not linked to something else.
620         it = copy_pars.begin();
621         for (; it != it_end; ++it) {
622                 it->setBuffer(*static_cast<Buffer *>(0));
623                 it->setInsetOwner(0);
624         }
625
626         cutstack.push(make_pair(copy_pars, dc));
627 }
628
629 } // namespace anon
630
631
632
633
634 namespace cap {
635
636 void region(CursorSlice const & i1, CursorSlice const & i2,
637             Inset::row_type & r1, Inset::row_type & r2,
638             Inset::col_type & c1, Inset::col_type & c2)
639 {
640         Inset & p = i1.inset();
641         c1 = p.col(i1.idx());
642         c2 = p.col(i2.idx());
643         if (c1 > c2)
644                 swap(c1, c2);
645         r1 = p.row(i1.idx());
646         r2 = p.row(i2.idx());
647         if (r1 > r2)
648                 swap(r1, r2);
649 }
650
651
652 docstring grabAndEraseSelection(Cursor & cur)
653 {
654         if (!cur.selection())
655                 return docstring();
656         docstring res = grabSelection(cur);
657         eraseSelection(cur);
658         return res;
659 }
660
661
662 bool reduceSelectionToOneCell(Cursor & cur)
663 {
664         if (!cur.selection() || !cur.inMathed())
665                 return false;
666
667         CursorSlice i1 = cur.selBegin();
668         CursorSlice i2 = cur.selEnd();
669         if (!i1.inset().asInsetMath())
670                 return false;
671
672         // the easy case: do nothing if only one cell is selected
673         if (i1.idx() == i2.idx())
674                 return true;
675         
676         cur.top().pos() = 0;
677         cur.resetAnchor();
678         cur.top().pos() = cur.top().lastpos();
679         
680         return true;
681 }
682
683
684 bool multipleCellsSelected(Cursor const & cur)
685 {
686         if (!cur.selection() || !cur.inMathed())
687                 return false;
688         
689         CursorSlice i1 = cur.selBegin();
690         CursorSlice i2 = cur.selEnd();
691         if (!i1.inset().asInsetMath())
692                 return false;
693         
694         if (i1.idx() == i2.idx())
695                 return false;
696         
697         return true;
698 }
699
700
701 void switchBetweenClasses(DocumentClassConstPtr oldone,
702                 DocumentClassConstPtr newone, InsetText & in, ErrorList & errorlist)
703 {
704         errorlist.clear();
705
706         LBUFERR(!in.paragraphs().empty());
707         if (oldone == newone)
708                 return;
709         
710         DocumentClass const & oldtc = *oldone;
711         DocumentClass const & newtc = *newone;
712
713         // layouts
714         ParIterator it = par_iterator_begin(in);
715         ParIterator end = par_iterator_end(in);
716         // for remembering which layouts we've had to add
717         set<docstring> newlayouts;
718         for (; it != end; ++it) {
719                 docstring const name = it->layout().name();
720
721                 // the pasted text will keep their own layout name. If this layout does
722                 // not exist in the new document, it will behave like a standard layout.
723                 bool const added_one = newtc.addLayoutIfNeeded(name);
724                 if (added_one)
725                         newlayouts.insert(name);
726
727                 if (added_one || newlayouts.find(name) != newlayouts.end()) {
728                         // Warn the user.
729                         docstring const s = bformat(_("Layout `%1$s' was not found."), name);
730                         errorlist.push_back(
731                                 ErrorItem(_("Layout Not Found"), s, it->id(), 0, it->size()));
732                 }
733
734                 if (in.usePlainLayout())
735                         it->setLayout(newtc.plainLayout());
736                 else
737                         it->setLayout(newtc[name]);
738         }
739
740         // character styles
741         InsetIterator const i_end = inset_iterator_end(in);
742         for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
743                 if (it->lyxCode() != FLEX_CODE)
744                         // FIXME: Should we verify all InsetCollapsable?
745                         continue;
746
747                 docstring const layoutName = it->layoutName();
748                 docstring const & n = newone->insetLayout(layoutName).name();
749                 bool const is_undefined = n.empty() ||
750                         n == DocumentClass::plainInsetLayout().name();
751                 if (!is_undefined)
752                         continue;
753
754                 // The flex inset is undefined in newtc
755                 docstring const oldname = from_utf8(oldtc.name());
756                 docstring const newname = from_utf8(newtc.name());
757                 docstring s;
758                 if (oldname == newname)
759                         s = bformat(_("Flex inset %1$s is undefined after "
760                                 "reloading `%2$s' layout."), layoutName, oldname);
761                 else
762                         s = bformat(_("Flex inset %1$s is undefined because of "
763                                 "conversion from `%2$s' layout to `%3$s'."),
764                                 layoutName, oldname, newname);
765                 // To warn the user that something had to be done.
766                 errorlist.push_back(ErrorItem(
767                                 _("Undefined flex inset"),
768                                 s, it.paragraph().id(), it.pos(), it.pos() + 1));
769         }
770 }
771
772
773 vector<docstring> availableSelections(Buffer const * buf)
774 {
775         vector<docstring> selList;
776         if (!buf)
777                 return selList;
778
779         CutStack::const_iterator cit = theCuts.begin();
780         CutStack::const_iterator end = theCuts.end();
781         for (; cit != end; ++cit) {
782                 // we do not use cit-> here because gcc 2.9x does not
783                 // like it (JMarc)
784                 ParagraphList const & pars = (*cit).first;
785                 docstring asciiSel;
786                 ParagraphList::const_iterator pit = pars.begin();
787                 ParagraphList::const_iterator pend = pars.end();
788                 for (; pit != pend; ++pit) {
789                         Paragraph par(*pit, 0, 26);
790                         // adapt paragraph to current buffer.
791                         par.setBuffer(const_cast<Buffer &>(*buf));
792                         asciiSel += par.asString(AS_STR_INSETS);
793                         if (asciiSel.size() > 25) {
794                                 asciiSel.replace(22, docstring::npos,
795                                                  from_ascii("..."));
796                                 break;
797                         }
798                 }
799
800                 selList.push_back(asciiSel);
801         }
802
803         return selList;
804 }
805
806
807 size_type numberOfSelections()
808 {
809         return theCuts.size();
810 }
811
812
813 void cutSelection(Cursor & cur, bool doclear, bool realcut)
814 {
815         // This doesn't make sense, if there is no selection
816         if (!cur.selection())
817                 return;
818
819         // OK, we have a selection. This is always between cur.selBegin()
820         // and cur.selEnd()
821
822         if (cur.inTexted()) {
823                 Text * text = cur.text();
824                 LBUFERR(text);
825
826                 saveSelection(cur);
827
828                 // make sure that the depth behind the selection are restored, too
829                 cur.recordUndoSelection();
830                 pit_type begpit = cur.selBegin().pit();
831                 pit_type endpit = cur.selEnd().pit();
832
833                 int endpos = cur.selEnd().pos();
834
835                 BufferParams const & bp = cur.buffer()->params();
836                 if (realcut) {
837                         copySelectionHelper(*cur.buffer(),
838                                 *text,
839                                 begpit, endpit,
840                                 cur.selBegin().pos(), endpos,
841                                 bp.documentClassPtr(), theCuts);
842                         // Stuff what we got on the clipboard.
843                         // Even if there is no selection.
844                         putClipboard(theCuts[0].first, theCuts[0].second,
845                                 cur.selectionAsString(true));
846                 }
847
848                 if (begpit != endpit)
849                         cur.screenUpdateFlags(Update::Force | Update::FitCursor);
850
851                 boost::tie(endpit, endpos) =
852                         eraseSelectionHelper(bp,
853                                 text->paragraphs(),
854                                 begpit, endpit,
855                                 cur.selBegin().pos(), endpos);
856
857                 // cutSelection can invalidate the cursor so we need to set
858                 // it anew. (Lgb)
859                 // we prefer the end for when tracking changes
860                 cur.pos() = endpos;
861                 cur.pit() = endpit;
862
863                 // sometimes necessary
864                 if (doclear
865                         && text->paragraphs()[begpit].stripLeadingSpaces(bp.track_changes))
866                         cur.fixIfBroken();
867
868                 // need a valid cursor. (Lgb)
869                 cur.clearSelection();
870
871                 // After a cut operation, we must make sure that the Buffer is updated
872                 // because some further operation might need updated label information for
873                 // example. So we cannot just use "cur.forceBufferUpdate()" here.
874                 // This fixes #7071.
875                 cur.buffer()->updateBuffer();
876
877                 // tell tabular that a recent copy happened
878                 dirtyTabularStack(false);
879         }
880
881         if (cur.inMathed()) {
882                 if (cur.selBegin().idx() != cur.selEnd().idx()) {
883                         // The current selection spans more than one cell.
884                         // Record all cells
885                         cur.recordUndoInset();
886                 } else {
887                         // Record only the current cell to avoid a jumping
888                         // cursor after undo
889                         cur.recordUndo();
890                 }
891                 if (realcut)
892                         copySelection(cur);
893                 eraseSelection(cur);
894         }
895 }
896
897
898 void copySelection(Cursor const & cur)
899 {
900         copySelection(cur, cur.selectionAsString(true));
901 }
902
903
904 void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext)
905 {
906         ParagraphList pars;
907         Paragraph par;
908         BufferParams const & bp = cur.buffer()->params();
909         par.setLayout(bp.documentClass().plainLayout());
910         Font font(inherit_font, bp.language);
911         par.insertInset(0, inset, font, Change(Change::UNCHANGED));
912         pars.push_back(par);
913         theCuts.push(make_pair(pars, bp.documentClassPtr()));
914
915         // stuff the selection onto the X clipboard, from an explicit copy request
916         putClipboard(theCuts[0].first, theCuts[0].second, plaintext);
917 }
918
919
920 namespace {
921
922 void copySelectionToStack(Cursor const & cur, CutStack & cutstack)
923 {
924         // this doesn't make sense, if there is no selection
925         if (!cur.selection())
926                 return;
927
928         // copySelection can not yet handle the case of cross idx selection
929         if (cur.selBegin().idx() != cur.selEnd().idx())
930                 return;
931
932         if (cur.inTexted()) {
933                 Text * text = cur.text();
934                 LBUFERR(text);
935                 // ok we have a selection. This is always between cur.selBegin()
936                 // and sel_end cursor
937
938                 // copy behind a space if there is one
939                 ParagraphList & pars = text->paragraphs();
940                 pos_type pos = cur.selBegin().pos();
941                 pit_type par = cur.selBegin().pit();
942                 while (pos < pars[par].size() &&
943                        pars[par].isLineSeparator(pos) &&
944                        (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
945                         ++pos;
946
947                 copySelectionHelper(*cur.buffer(), *text, par, cur.selEnd().pit(),
948                         pos, cur.selEnd().pos(), 
949                         cur.buffer()->params().documentClassPtr(), cutstack);
950
951                 // Reset the dirty_tabular_stack_ flag only when something
952                 // is copied to the clipboard (not to the selectionBuffer).
953                 if (&cutstack == &theCuts)
954                         dirtyTabularStack(false);
955         }
956
957         if (cur.inMathed()) {
958                 //lyxerr << "copySelection in mathed" << endl;
959                 ParagraphList pars;
960                 Paragraph par;
961                 BufferParams const & bp = cur.buffer()->params();
962                 // FIXME This should be the plain layout...right?
963                 par.setLayout(bp.documentClass().plainLayout());
964                 par.insert(0, grabSelection(cur), Font(), Change(Change::UNCHANGED));
965                 pars.push_back(par);
966                 cutstack.push(make_pair(pars, bp.documentClassPtr()));
967         }
968 }
969
970 }
971
972
973 void copySelectionToStack()
974 {
975         if (!selectionBuffer.empty())
976                 theCuts.push(selectionBuffer[0]);
977 }
978
979
980 void copySelection(Cursor const & cur, docstring const & plaintext)
981 {
982         // In tablemode, because copy and paste actually use special table stack
983         // we do not attempt to get selected paragraphs under cursor. Instead, a
984         // paragraph with the plain text version is generated so that table cells
985         // can be pasted as pure text somewhere else.
986         if (cur.selBegin().idx() != cur.selEnd().idx()) {
987                 ParagraphList pars;
988                 Paragraph par;
989                 BufferParams const & bp = cur.buffer()->params();
990                 par.setLayout(bp.documentClass().plainLayout());
991                 par.insert(0, plaintext, Font(), Change(Change::UNCHANGED));
992                 pars.push_back(par);
993                 theCuts.push(make_pair(pars, bp.documentClassPtr()));
994         } else {
995                 copySelectionToStack(cur, theCuts);
996         }
997
998         // stuff the selection onto the X clipboard, from an explicit copy request
999         putClipboard(theCuts[0].first, theCuts[0].second, plaintext);
1000 }
1001
1002
1003 void saveSelection(Cursor const & cur)
1004 {
1005         // This function is called, not when a selection is formed, but when
1006         // a selection is cleared. Therefore, multiple keyboard selection
1007         // will not repeatively trigger this function (bug 3877).
1008         if (cur.selection() 
1009             && cur.selBegin() == cur.bv().cursor().selBegin()
1010             && cur.selEnd() == cur.bv().cursor().selEnd()) {
1011                 LYXERR(Debug::SELECTION, "saveSelection: '" << cur.selectionAsString(true) << "'");
1012                 copySelectionToStack(cur, selectionBuffer);
1013         }
1014 }
1015
1016
1017 bool selection()
1018 {
1019         return !selectionBuffer.empty();
1020 }
1021
1022
1023 void clearSelection()
1024 {
1025         selectionBuffer.clear();
1026 }
1027
1028
1029 void clearCutStack()
1030 {
1031         theCuts.clear();
1032 }
1033
1034
1035 docstring selection(size_t sel_index)
1036 {
1037         return sel_index < theCuts.size()
1038                 ? theCuts[sel_index].first.back().asString(AS_STR_INSETS | AS_STR_NEWLINES)
1039                 : docstring();
1040 }
1041
1042
1043 void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
1044                         DocumentClassConstPtr docclass, ErrorList & errorList)
1045 {
1046         if (cur.inTexted()) {
1047                 Text * text = cur.text();
1048                 LBUFERR(text);
1049
1050                 PasteReturnValue prv =
1051                         pasteSelectionHelper(cur, parlist, docclass, 0, errorList);
1052                 cur.forceBufferUpdate();
1053                 cur.clearSelection();
1054                 text->setCursor(cur, prv.par, prv.pos);
1055         }
1056
1057         // mathed is handled in InsetMathNest/InsetMathGrid
1058         LATTEST(!cur.inMathed());
1059 }
1060
1061
1062 bool pasteFromStack(Cursor & cur, ErrorList & errorList, size_t sel_index)
1063 {
1064         // this does not make sense, if there is nothing to paste
1065         if (!checkPastePossible(sel_index))
1066                 return false;
1067
1068         cur.recordUndo();
1069         pasteParagraphList(cur, theCuts[sel_index].first,
1070                            theCuts[sel_index].second, errorList);
1071         return true;
1072 }
1073
1074
1075 bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
1076                         Clipboard::TextType type)
1077 {
1078         // Use internal clipboard if it is the most recent one
1079         // This overrides asParagraphs and type on purpose!
1080         if (theClipboard().isInternal())
1081                 return pasteFromStack(cur, errorList, 0);
1082
1083         // First try LyX format
1084         if ((type == Clipboard::LyXTextType ||
1085              type == Clipboard::LyXOrPlainTextType ||
1086              type == Clipboard::AnyTextType) &&
1087             theClipboard().hasTextContents(Clipboard::LyXTextType)) {
1088                 string lyx = theClipboard().getAsLyX();
1089                 if (!lyx.empty()) {
1090                         // For some strange reason gcc 3.2 and 3.3 do not accept
1091                         // Buffer buffer(string(), false);
1092                         Buffer buffer("", false);
1093                         buffer.setUnnamed(true);
1094                         if (buffer.readString(lyx)) {
1095                                 cur.recordUndo();
1096                                 pasteParagraphList(cur, buffer.paragraphs(),
1097                                         buffer.params().documentClassPtr(), errorList);
1098                                 return true;
1099                         }
1100                 }
1101         }
1102
1103         // Then try TeX and HTML
1104         Clipboard::TextType types[2] = {Clipboard::HtmlTextType, Clipboard::LaTeXTextType};
1105         string names[2] = {"html", "latexclipboard"};
1106         for (int i = 0; i < 2; ++i) {
1107                 if (type != types[i] && type != Clipboard::AnyTextType)
1108                         continue;
1109                 bool available = theClipboard().hasTextContents(types[i]);
1110
1111                 // If a specific type was explicitly requested, try to
1112                 // interpret plain text: The user told us that the clipboard
1113                 // contents is in the desired format
1114                 if (!available && type == types[i]) {
1115                         types[i] = Clipboard::PlainTextType;
1116                         available = theClipboard().hasTextContents(types[i]);
1117                 }
1118
1119                 if (available) {
1120                         docstring text = theClipboard().getAsText(types[i]);
1121                         available = !text.empty();
1122                         if (available) {
1123                                 // For some strange reason gcc 3.2 and 3.3 do not accept
1124                                 // Buffer buffer(string(), false);
1125                                 Buffer buffer("", false);
1126                                 buffer.setUnnamed(true);
1127                                 available = buffer.importString(names[i], text, errorList);
1128                                 if (available)
1129                                         available = !buffer.paragraphs().empty();
1130                                 if (available && !buffer.paragraphs()[0].empty()) {
1131                                         cur.recordUndo();
1132                                         pasteParagraphList(cur, buffer.paragraphs(),
1133                                                 buffer.params().documentClassPtr(), errorList);
1134                                         return true;
1135                                 }
1136                         }
1137                 }
1138         }
1139
1140         // Then try plain text
1141         docstring const text = theClipboard().getAsText(Clipboard::PlainTextType);
1142         if (text.empty())
1143                 return false;
1144         cur.recordUndo();
1145         if (asParagraphs)
1146                 cur.text()->insertStringAsParagraphs(cur, text, cur.current_font);
1147         else
1148                 cur.text()->insertStringAsLines(cur, text, cur.current_font);
1149         return true;
1150 }
1151
1152
1153 void pasteSimpleText(Cursor & cur, bool asParagraphs)
1154 {
1155         docstring text;
1156         // Use internal clipboard if it is the most recent one
1157         if (theClipboard().isInternal()) {
1158                 if (!checkPastePossible(0))
1159                         return;
1160
1161                 ParagraphList const & pars = theCuts[0].first;
1162                 ParagraphList::const_iterator it = pars.begin();
1163                 for (; it != pars.end(); ++it) {
1164                         if (it != pars.begin())
1165                                 text += "\n";
1166                         text += (*it).asString();
1167                 }
1168                 asParagraphs = false;
1169         } else {
1170                 // Then try plain text
1171                 text = theClipboard().getAsText(Clipboard::PlainTextType);
1172         }
1173
1174         if (text.empty())
1175                 return;
1176
1177         cur.recordUndo();
1178         cutSelection(cur, true, false);
1179         if (asParagraphs)
1180                 cur.text()->insertStringAsParagraphs(cur, text, cur.current_font);
1181         else
1182                 cur.text()->insertStringAsLines(cur, text, cur.current_font);
1183 }
1184
1185
1186 void pasteClipboardGraphics(Cursor & cur, ErrorList & /* errorList */,
1187                             Clipboard::GraphicsType preferedType)
1188 {
1189         LASSERT(theClipboard().hasGraphicsContents(preferedType), return);
1190
1191         // get picture from clipboard
1192         FileName filename = theClipboard().getAsGraphics(cur, preferedType);
1193         if (filename.empty())
1194                 return;
1195
1196         // create inset for graphic
1197         InsetGraphics * inset = new InsetGraphics(cur.buffer());
1198         InsetGraphicsParams params;
1199         params.filename = support::DocFileName(filename.absFileName(), false);
1200         inset->setParams(params);
1201         cur.recordUndo();
1202         cur.insert(inset);
1203 }
1204
1205
1206 void pasteSelection(Cursor & cur, ErrorList & errorList)
1207 {
1208         if (selectionBuffer.empty())
1209                 return;
1210         cur.recordUndo();
1211         pasteParagraphList(cur, selectionBuffer[0].first,
1212                            selectionBuffer[0].second, errorList);
1213 }
1214
1215
1216 void replaceSelectionWithString(Cursor & cur, docstring const & str)
1217 {
1218         cur.recordUndo();
1219         DocIterator selbeg = cur.selectionBegin();
1220
1221         // Get font setting before we cut, we need a copy here, not a bare reference.
1222         Font const font =
1223                 selbeg.paragraph().getFontSettings(cur.buffer()->params(), selbeg.pos());
1224
1225         // Insert the new string
1226         pos_type pos = cur.selEnd().pos();
1227         Paragraph & par = cur.selEnd().paragraph();
1228         docstring::const_iterator cit = str.begin();
1229         docstring::const_iterator end = str.end();
1230         for (; cit != end; ++cit, ++pos)
1231                 par.insertChar(pos, *cit, font, cur.buffer()->params().track_changes);
1232
1233         // Cut the selection
1234         cutSelection(cur, true, false);
1235 }
1236
1237
1238 void replaceSelection(Cursor & cur)
1239 {
1240         if (cur.selection())
1241                 cutSelection(cur, true, false);
1242 }
1243
1244
1245 void eraseSelection(Cursor & cur)
1246 {
1247         //lyxerr << "cap::eraseSelection begin: " << cur << endl;
1248         CursorSlice const & i1 = cur.selBegin();
1249         CursorSlice const & i2 = cur.selEnd();
1250         if (i1.inset().asInsetMath()) {
1251                 saveSelection(cur);
1252                 cur.top() = i1;
1253                 if (i1.idx() == i2.idx()) {
1254                         i1.cell().erase(i1.pos(), i2.pos());
1255                         // We may have deleted i1.cell(cur.pos()).
1256                         // Make sure that pos is valid.
1257                         if (cur.pos() > cur.lastpos())
1258                                 cur.pos() = cur.lastpos();
1259                 } else {
1260                         InsetMath * p = i1.asInsetMath();
1261                         Inset::row_type r1, r2;
1262                         Inset::col_type c1, c2;
1263                         region(i1, i2, r1, r2, c1, c2);
1264                         for (Inset::row_type row = r1; row <= r2; ++row)
1265                                 for (Inset::col_type col = c1; col <= c2; ++col)
1266                                         p->cell(p->index(row, col)).clear();
1267                         // We've deleted the whole cell. Only pos 0 is valid.
1268                         cur.pos() = 0;
1269                 }
1270                 // need a valid cursor. (Lgb)
1271                 cur.clearSelection();
1272         } else {
1273                 lyxerr << "can't erase this selection 1" << endl;
1274         }
1275         //lyxerr << "cap::eraseSelection end: " << cur << endl;
1276 }
1277
1278
1279 void selDel(Cursor & cur)
1280 {
1281         //lyxerr << "cap::selDel" << endl;
1282         if (cur.selection())
1283                 eraseSelection(cur);
1284 }
1285
1286
1287 void selClearOrDel(Cursor & cur)
1288 {
1289         //lyxerr << "cap::selClearOrDel" << endl;
1290         if (lyxrc.auto_region_delete)
1291                 selDel(cur);
1292         else
1293                 cur.setSelection(false);
1294 }
1295
1296
1297 docstring grabSelection(Cursor const & cur)
1298 {
1299         if (!cur.selection())
1300                 return docstring();
1301
1302 #if 0
1303         // grab selection by glueing multiple cells together. This is not what
1304         // we want because selections spanning multiple cells will get "&" and "\\"
1305         // seperators.
1306         ostringstream os;
1307         for (DocIterator dit = cur.selectionBegin();
1308              dit != cur.selectionEnd(); dit.forwardPos())
1309                 os << asString(dit.cell());
1310         return os.str();
1311 #endif
1312
1313         CursorSlice i1 = cur.selBegin();
1314         CursorSlice i2 = cur.selEnd();
1315
1316         if (i1.idx() == i2.idx()) {
1317                 if (i1.inset().asInsetMath()) {
1318                         MathData::const_iterator it = i1.cell().begin();
1319                         Buffer * buf = cur.buffer();
1320                         return asString(MathData(buf, it + i1.pos(), it + i2.pos()));
1321                 } else {
1322                         return from_ascii("unknown selection 1");
1323                 }
1324         }
1325
1326         Inset::row_type r1, r2;
1327         Inset::col_type c1, c2;
1328         region(i1, i2, r1, r2, c1, c2);
1329
1330         docstring data;
1331         if (i1.inset().asInsetMath()) {
1332                 for (Inset::row_type row = r1; row <= r2; ++row) {
1333                         if (row > r1)
1334                                 data += "\\\\";
1335                         for (Inset::col_type col = c1; col <= c2; ++col) {
1336                                 if (col > c1)
1337                                         data += '&';
1338                                 data += asString(i1.asInsetMath()->
1339                                         cell(i1.asInsetMath()->index(row, col)));
1340                         }
1341                 }
1342         } else {
1343                 data = from_ascii("unknown selection 2");
1344         }
1345         return data;
1346 }
1347
1348
1349 void dirtyTabularStack(bool b)
1350 {
1351         dirty_tabular_stack_ = b;
1352 }
1353
1354
1355 bool tabularStackDirty()
1356 {
1357         return dirty_tabular_stack_;
1358 }
1359
1360
1361 } // namespace cap
1362 } // namespace lyx