]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
Two fixes involving RtL text drawing
[lyx.git] / src / CutAndPaste.C
1 /*
2  * \file CutAndPaste.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  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "CutAndPaste.h"
16
17 #include "buffer.h"
18 #include "buffer_funcs.h"
19 #include "bufferparams.h"
20 #include "BufferView.h"
21 #include "cursor.h"
22 #include "debug.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "insetiterator.h"
27 #include "lfuns.h"
28 #include "lyxrc.h"
29 #include "lyxtext.h"
30 #include "lyxtextclasslist.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "ParagraphParameters.h"
34 #include "ParagraphList_fwd.h"
35 #include "pariterator.h"
36 #include "undo.h"
37
38 #include "insets/insetcharstyle.h"
39 #include "insets/insettabular.h"
40
41 #include "mathed/math_data.h"
42 #include "mathed/math_inset.h"
43 #include "mathed/math_support.h"
44
45 #include "support/lstrings.h"
46
47 #include <boost/tuple/tuple.hpp>
48
49 using lyx::pos_type;
50 using lyx::pit_type;
51 using lyx::textclass_type;
52
53 using lyx::support::bformat;
54
55 using std::endl;
56 using std::for_each;
57 using std::make_pair;
58 using std::pair;
59 using std::vector;
60 using std::string;
61
62
63 namespace {
64
65 typedef std::pair<lyx::pit_type, int> PitPosPair;
66
67 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
68
69 CutStack theCuts(10);
70
71 // store whether the tabular stack is newer than the normal copy stack
72 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5, 
73 // when we (hopefully) have a one-for-all paste mechanism.
74 bool dirty_tabular_stack_;
75
76 class resetOwnerAndChanges : public std::unary_function<Paragraph, void> {
77 public:
78         void operator()(Paragraph & p) const {
79                 p.cleanChanges();
80                 p.setInsetOwner(0);
81         }
82 };
83
84
85 void region(CursorSlice const & i1, CursorSlice const & i2,
86         InsetBase::row_type & r1, InsetBase::row_type & r2,
87         InsetBase::col_type & c1, InsetBase::col_type & c2)
88 {
89         InsetBase & p = i1.inset();
90         c1 = p.col(i1.idx());
91         c2 = p.col(i2.idx());
92         if (c1 > c2)
93                 std::swap(c1, c2);
94         r1 = p.row(i1.idx());
95         r2 = p.row(i2.idx());
96         if (r1 > r2)
97                 std::swap(r1, r2);
98 }
99
100
101 bool checkPastePossible(int index)
102 {
103         return size_t(index) < theCuts.size() && !theCuts[index].first.empty();
104 }
105
106
107 pair<PitPosPair, pit_type>
108 pasteSelectionHelper(Buffer const & buffer, ParagraphList & pars,
109         pit_type pit, int pos,
110         textclass_type tc, size_t cut_index, ErrorList & errorlist)
111 {
112         if (!checkPastePossible(cut_index))
113                 return make_pair(PitPosPair(pit, pos), pit);
114
115         BOOST_ASSERT (pos <= pars[pit].size());
116
117         // Make a copy of the CaP paragraphs.
118         ParagraphList insertion = theCuts[cut_index].first;
119         textclass_type const textclass = theCuts[cut_index].second;
120
121         // Now remove all out of the pars which is NOT allowed in the
122         // new environment and set also another font if that is required.
123
124         // Convert newline to paragraph break in ERT inset.
125         // This should not be here!
126         if (pars[pit].inInset() &&
127             pars[pit].inInset()->lyxCode() == InsetBase::ERT_CODE) {
128                 for (ParagraphList::size_type i = 0; i < insertion.size(); ++i) {
129                         for (pos_type j = 0; j < insertion[i].size(); ++j) {
130                                 if (insertion[i].isNewline(j)) {
131                                         insertion[i].erase(j);
132                                         breakParagraphConservative(
133                                                         buffer.params(),
134                                                         insertion, i, j);
135                                 }
136                         }
137                 }
138         }
139
140         // Make sure there is no class difference.
141         lyx::cap::SwitchBetweenClasses(textclass, tc, insertion, errorlist);
142
143         ParagraphList::iterator tmpbuf = insertion.begin();
144         int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
145
146         Paragraph::depth_type max_depth = pars[pit].getMaxDepthAfter();
147
148         for (; tmpbuf != insertion.end(); ++tmpbuf) {
149                 // If we have a negative jump so that the depth would
150                 // go below 0 depth then we have to redo the delta to
151                 // this new max depth level so that subsequent
152                 // paragraphs are aligned correctly to this paragraph
153                 // at level 0.
154                 if (int(tmpbuf->params().depth()) + depth_delta < 0)
155                         depth_delta = 0;
156
157                 // Set the right depth so that we are not too deep or shallow.
158                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
159                 if (tmpbuf->params().depth() > max_depth)
160                         tmpbuf->params().depth(max_depth);
161
162                 // Only set this from the 2nd on as the 2nd depends
163                 // for maxDepth still on pit.
164                 if (tmpbuf != insertion.begin())
165                         max_depth = tmpbuf->getMaxDepthAfter();
166
167                 // Set the inset owner of this paragraph.
168                 tmpbuf->setInsetOwner(pars[pit].inInset());
169                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
170                         if (tmpbuf->getChar(i) == Paragraph::META_INSET &&
171                             !pars[pit].insetAllowed(tmpbuf->getInset(i)->lyxCode()))
172                                 tmpbuf->erase(i--);
173                 }
174         }
175
176         bool const empty = pars[pit].empty();
177         if (!empty) {
178                 // Make the buf exactly the same layout as the cursor
179                 // paragraph.
180                 insertion.begin()->makeSameLayout(pars[pit]);
181         }
182
183         // Prepare the paragraphs and insets for insertion.
184         // A couple of insets store buffer references so need updating.
185         InsetText in;
186         std::swap(in.paragraphs(), insertion);
187
188         ParIterator fpit = par_iterator_begin(in);
189         ParIterator fend = par_iterator_end(in);
190
191         for (; fpit != fend; ++fpit) {
192                 InsetList::iterator lit = fpit->insetlist.begin();
193                 InsetList::iterator eit = fpit->insetlist.end();
194
195                 for (; lit != eit; ++lit) {
196                         switch (lit->inset->lyxCode()) {
197                         case InsetBase::TABULAR_CODE: {
198                                 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
199                                 it->buffer(&buffer);
200                                 break;
201                         }
202
203                         default:
204                                 break; // nothing
205                         }
206                 }
207         }
208         std::swap(in.paragraphs(), insertion);
209
210         // Split the paragraph for inserting the buf if necessary.
211         if (!empty)
212                 breakParagraphConservative(buffer.params(), pars, pit, pos);
213
214         // Paste it!
215         if (empty) {
216                 pars.insert(pars.begin() + pit, insertion.begin(),
217                             insertion.end());
218
219                 // merge the empty par with the last par of the insertion
220                 mergeParagraph(buffer.params(), pars,
221                                pit + insertion.size() - 1);
222         } else {
223                 pars.insert(pars.begin() + pit + 1, insertion.begin(),
224                             insertion.end());
225
226                 // merge the first par of the insertion with the current par
227                 mergeParagraph(buffer.params(), pars, pit);
228         }
229
230         pit_type last_paste = pit + insertion.size() - 1;
231
232         // Store the new cursor position.
233         pit = last_paste;
234         pos = pars[last_paste].size();
235
236         // Maybe some pasting.
237         if (!empty && last_paste + 1 != pit_type(pars.size())) {
238                 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
239                         mergeParagraph(buffer.params(), pars, last_paste);
240                 } else if (pars[last_paste + 1].empty()) {
241                         pars[last_paste + 1].makeSameLayout(pars[last_paste]);
242                         mergeParagraph(buffer.params(), pars, last_paste);
243                 } else if (pars[last_paste].empty()) {
244                         pars[last_paste].makeSameLayout(pars[last_paste + 1]);
245                         mergeParagraph(buffer.params(), pars, last_paste);
246                 } else {
247                         pars[last_paste + 1].stripLeadingSpaces();
248                         ++last_paste;
249                 }
250         }
251
252         return make_pair(PitPosPair(pit, pos), last_paste + 1);
253 }
254
255
256 PitPosPair eraseSelectionHelper(BufferParams const & params,
257         ParagraphList & pars,
258         pit_type startpit, pit_type endpit,
259         int startpos, int endpos, bool doclear)
260 {
261         // Start of selection is really invalid.
262         if (startpit == pit_type(pars.size()) ||
263             (startpos > pars[startpit].size()))
264                 return PitPosPair(endpit, endpos);
265
266         // Start and end is inside same paragraph
267         if (endpit == pit_type(pars.size()) ||
268             startpit == endpit) {
269                 endpos -= pars[startpit].erase(startpos, endpos);
270                 return PitPosPair(endpit, endpos);
271         }
272
273         bool all_erased = true;
274
275         // Clear fragments of the first par in selection
276         pars[startpit].erase(startpos, pars[startpit].size());
277         if (pars[startpit].size() != startpos)
278                 all_erased = false;
279
280         // Clear fragments of the last par in selection
281         endpos -= pars[endpit].erase(0, endpos);
282         if (endpos != 0)
283                 all_erased = false;
284
285         // Erase all the "middle" paragraphs.
286         if (params.tracking_changes) {
287                 // Look through the deleted pars if any, erasing as needed
288                 for (pit_type pit = startpit + 1; pit != endpit;) {
289                         // "erase" the contents of the par
290                         pars[pit].erase(0, pars[pit].size());
291                         if (pars[pit].empty()) {
292                                 // remove the par if it's now empty
293                                 pars.erase(pars.begin() + pit);
294                                 --endpit;
295                         } else {
296                                 ++pit;
297                                 all_erased = false;
298                         }
299                 }
300         } else {
301                 pars.erase(pars.begin() + startpit + 1, pars.begin() + endpit);
302                 endpit = startpit + 1;
303         }
304         
305 #if 0 // FIXME: why for cut but not copy ?
306         // the cut selection should begin with standard layout
307         if (realcut) {
308                 buf->params().clear();
309                 buf->bibkey = 0;
310                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
311         }
312 #endif
313
314         if (startpit + 1 == pit_type(pars.size()))
315                 return PitPosPair(endpit, endpos);
316
317         if (doclear) {
318                 pars[startpit + 1].stripLeadingSpaces();
319         }
320
321         // Merge first and last paragraph, if possible
322         if (all_erased &&
323             (pars[startpit].hasSameLayout(pars[startpit + 1]) ||
324              pars[startpit + 1].empty())) {
325                 mergeParagraph(params, pars, startpit);
326                 // This because endpar gets deleted here!
327                 endpit = startpit;
328                 endpos = startpos;
329         }
330
331         return PitPosPair(endpit, endpos);
332 }
333
334
335 void copySelectionHelper(ParagraphList & pars,
336         pit_type startpit, pit_type endpit,
337         int start, int end, textclass_type tc)
338 {
339         BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
340         BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
341         BOOST_ASSERT(startpit != endpit || start <= end);
342
343         // Clone the paragraphs within the selection.
344         ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
345
346         for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
347
348         // Cut out the end of the last paragraph.
349         Paragraph & back = paragraphs.back();
350         back.erase(end, back.size());
351
352         // Cut out the begin of the first paragraph
353         Paragraph & front = paragraphs.front();
354         front.erase(0, start);
355
356         theCuts.push(make_pair(paragraphs, tc));
357 }
358
359 } // namespace anon
360
361
362
363
364 namespace lyx {
365 namespace cap {
366
367 string grabAndEraseSelection(LCursor & cur)
368 {
369         if (!cur.selection())
370                 return string();
371         string res = grabSelection(cur);
372         eraseSelection(cur);
373         cur.selection() = false;
374         return res;
375 }
376
377
378 void SwitchBetweenClasses(textclass_type c1, textclass_type c2,
379         ParagraphList & pars, ErrorList & errorlist)
380 {
381         BOOST_ASSERT(!pars.empty());
382         if (c1 == c2)
383                 return;
384
385         LyXTextClass const & tclass1 = textclasslist[c1];
386         LyXTextClass const & tclass2 = textclasslist[c2];
387
388         InsetText in;
389         std::swap(in.paragraphs(), pars);
390
391         // layouts
392         ParIterator end = par_iterator_end(in);
393         for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
394                 string const name = it->layout()->name();
395                 bool hasLayout = tclass2.hasLayout(name);
396
397                 if (hasLayout)
398                         it->layout(tclass2[name]);
399                 else
400                         it->layout(tclass2.defaultLayout());
401
402                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
403                         string const s = bformat(
404                                 _("Layout had to be changed from\n%1$s to %2$s\n"
405                                 "because of class conversion from\n%3$s to %4$s"),
406                          name, it->layout()->name(), tclass1.name(), tclass2.name());
407                         // To warn the user that something had to be done.
408                         errorlist.push_back(ErrorItem(_("Changed Layout"), s,
409                                                       it->id(), 0,
410                                                       it->size()));
411                 }
412         }
413
414         // character styles
415         InsetIterator const i_end = inset_iterator_end(in);
416         for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
417                 if (it->lyxCode() == InsetBase::CHARSTYLE_CODE) {
418                         InsetCharStyle & inset =
419                                 static_cast<InsetCharStyle &>(*it);
420                         string const name = inset.params().type;
421                         CharStyles::iterator const found_cs =
422                                 tclass2.charstyle(name);
423                         if (found_cs == tclass2.charstyles().end()) {
424                                 // The character style is undefined in tclass2
425                                 inset.setUndefined();
426                                 string const s = bformat(_(
427                                         "Character style %1$s is "
428                                         "undefined because of class "
429                                         "conversion from\n%2$s to %3$s"),
430                                          name, tclass1.name(), tclass2.name());
431                                 // To warn the user that something had to be done.
432                                 errorlist.push_back(ErrorItem(
433                                                 _("Undefined character style"),
434                                                 s, it.paragraph().id(),
435                                                 it.pos(), it.pos() + 1));
436                         } else if (inset.undefined()) {
437                                 // The character style is undefined in
438                                 // tclass1 and is defined in tclass2
439                                 inset.setDefined(found_cs);
440                         }
441                 }
442         }
443
444         std::swap(in.paragraphs(), pars);
445 }
446
447
448 std::vector<string> const availableSelections(Buffer const & buffer)
449 {
450         vector<string> selList;
451
452         CutStack::const_iterator cit = theCuts.begin();
453         CutStack::const_iterator end = theCuts.end();
454         for (; cit != end; ++cit) {
455                 // we do not use cit-> here because gcc 2.9x does not
456                 // like it (JMarc)
457                 ParagraphList const & pars = (*cit).first;
458                 string asciiSel;
459                 ParagraphList::const_iterator pit = pars.begin();
460                 ParagraphList::const_iterator pend = pars.end();
461                 for (; pit != pend; ++pit) {
462                         asciiSel += pit->asString(buffer, false);
463                         if (asciiSel.size() > 25) {
464                                 asciiSel.replace(22, string::npos, "...");
465                                 break;
466                         }
467                 }
468
469                 selList.push_back(asciiSel);
470         }
471
472         return selList;
473 }
474
475
476 int nrOfParagraphs()
477 {
478         return theCuts.empty() ? 0 : theCuts[0].first.size();
479 }
480
481
482 void cutSelection(LCursor & cur, bool doclear, bool realcut)
483 {
484         if (cur.inTexted()) {
485                 LyXText * text = cur.text();
486                 BOOST_ASSERT(text);
487                 // Stuff what we got on the clipboard. Even if there is no selection.
488
489                 // There is a problem with having the stuffing here in that the
490                 // larger the selection the slower LyX will get. This can be
491                 // solved by running the line below only when the selection has
492                 // finished. The solution used currently just works, to make it
493                 // faster we need to be more clever and probably also have more
494                 // calls to stuffClipboard. (Lgb)
495 //              cur.bv().stuffClipboard(cur.selectionAsString(true));
496
497                 // This doesn't make sense, if there is no selection
498                 if (!cur.selection())
499                         return;
500
501                 // OK, we have a selection. This is always between cur.selBegin()
502                 // and cur.selEnd()
503
504                 // make sure that the depth behind the selection are restored, too
505                 recordUndoSelection(cur);
506                 pit_type begpit = cur.selBegin().pit();
507                 pit_type endpit = cur.selEnd().pit();
508
509                 int endpos = cur.selEnd().pos();
510
511                 BufferParams const & bp = cur.buffer().params();
512                 if (realcut) {
513                         copySelectionHelper(text->paragraphs(),
514                                 begpit, endpit,
515                                 cur.selBegin().pos(), endpos,
516                                 bp.textclass);
517                 }
518
519                 boost::tie(endpit, endpos) =
520                         eraseSelectionHelper(bp,
521                                 text->paragraphs(),
522                                 begpit, endpit,
523                                 cur.selBegin().pos(), endpos,
524                                 doclear);
525
526                 // sometimes necessary
527                 if (doclear)
528                         text->paragraphs()[begpit].stripLeadingSpaces();
529
530                 // cutSelection can invalidate the cursor so we need to set
531                 // it anew. (Lgb)
532                 // we prefer the end for when tracking changes
533                 cur.pos() = endpos;
534                 cur.pit() = endpit;
535
536                 // need a valid cursor. (Lgb)
537                 cur.clearSelection();
538                 updateCounters(cur.buffer());
539
540                 // tell tabular that a recent copy happened
541                 dirtyTabularStack(false);
542         }
543
544         if (cur.inMathed()) {
545                 lyxerr << "cutSelection in mathed" << endl;
546                 LCursor tmp = cur;
547                 copySelection(cur);
548                 cur.selection() = false;
549                 eraseSelection(tmp);
550         }
551 }
552
553
554 void copySelection(LCursor & cur)
555 {
556         // stuff the selection onto the X clipboard, from an explicit copy request
557         cur.bv().stuffClipboard(cur.selectionAsString(true));
558
559         // this doesn't make sense, if there is no selection
560         if (!cur.selection())
561                 return;
562
563         if (cur.inTexted()) {
564                 LyXText * text = cur.text();
565                 BOOST_ASSERT(text);
566                 // ok we have a selection. This is always between cur.selBegin()
567                 // and sel_end cursor
568
569                 // copy behind a space if there is one
570                 ParagraphList & pars = text->paragraphs();
571                 pos_type pos = cur.selBegin().pos();
572                 pit_type par = cur.selBegin().pit();
573                 while (pos < pars[par].size()
574                                          && pars[par].isLineSeparator(pos)
575                                          && (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
576                         ++pos;
577
578                 copySelectionHelper(pars, par, cur.selEnd().pit(),
579                         pos, cur.selEnd().pos(), cur.buffer().params().textclass);
580         }
581
582         if (cur.inMathed()) {
583                 lyxerr << "copySelection in mathed" << endl;
584                 ParagraphList pars;
585                 pars.push_back(Paragraph());
586                 BufferParams const & bp = cur.buffer().params();
587                 pars.back().layout(bp.getLyXTextClass().defaultLayout());
588                 for_each(pars.begin(), pars.end(), resetOwnerAndChanges());
589                 pars.back().insert(0, grabSelection(cur), LyXFont());
590                 theCuts.push(make_pair(pars, bp.textclass));
591         }
592         // tell tabular that a recent copy happened
593         dirtyTabularStack(false);
594 }
595
596
597 std::string getSelection(Buffer const & buf, size_t sel_index)
598 {
599         return sel_index < theCuts.size()
600                 ? theCuts[sel_index].first.back().asString(buf, false)
601                 : string();
602 }
603
604
605 void pasteSelection(LCursor & cur, size_t sel_index)
606 {
607         // this does not make sense, if there is nothing to paste
608         lyxerr << "#### pasteSelection " << sel_index << endl;
609         if (!checkPastePossible(sel_index))
610                 return;
611
612         if (cur.inTexted()) {
613                 LyXText * text = cur.text();
614                 BOOST_ASSERT(text);
615
616                 recordUndo(cur);
617
618                 pit_type endpit;
619                 PitPosPair ppp;
620
621                 ErrorList el;
622
623                 boost::tie(ppp, endpit) =
624                         pasteSelectionHelper(cur.buffer(),
625                                              text->paragraphs(),
626                                              cur.pit(), cur.pos(),
627                                              cur.buffer().params().textclass,
628                                              sel_index, el);
629                 bufferErrors(cur.buffer(), el);
630                 cur.bv().showErrorList(_("Paste"));
631
632                 cur.clearSelection();
633                 cur.resetAnchor();
634                 text->setCursor(cur, ppp.first, ppp.second);
635                 cur.setSelection();
636                 updateCounters(cur.buffer());
637         }
638
639         if (cur.inMathed()) {
640                 lyxerr << "### should be handled in MathNest/GridInset" << endl;
641         }
642 }
643
644
645 void setSelectionRange(LCursor & cur, pos_type length)
646 {
647         LyXText * text = cur.text();
648         BOOST_ASSERT(text);
649         if (!length)
650                 return;
651         cur.resetAnchor();
652         while (length--)
653                 text->cursorRight(cur);
654         cur.setSelection();
655 }
656
657
658 // simple replacing. The font of the first selected character is used
659 void replaceSelectionWithString(LCursor & cur, string const & str)
660 {
661         LyXText * text = cur.text();
662         BOOST_ASSERT(text);
663         recordUndo(cur);
664
665         // Get font setting before we cut
666         pos_type pos = cur.selEnd().pos();
667         Paragraph & par = text->getPar(cur.selEnd().pit());
668         LyXFont const font =
669                 par.getFontSettings(cur.buffer().params(), cur.selBegin().pos());
670
671         // Insert the new string
672         string::const_iterator cit = str.begin();
673         string::const_iterator end = str.end();
674         for (; cit != end; ++cit, ++pos)
675                 par.insertChar(pos, (*cit), font);
676
677         // Cut the selection
678         cutSelection(cur, true, false);
679 }
680
681
682 void replaceSelection(LCursor & cur)
683 {
684         if (cur.selection())
685                 cutSelection(cur, true, false);
686 }
687
688
689 // only used by the spellchecker
690 void replaceWord(LCursor & cur, string const & replacestring)
691 {
692         LyXText * text = cur.text();
693         BOOST_ASSERT(text);
694
695         replaceSelectionWithString(cur, replacestring);
696         setSelectionRange(cur, replacestring.length());
697
698         // Go back so that replacement string is also spellchecked
699         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
700                 text->cursorLeft(cur);
701 }
702
703
704 void eraseSelection(LCursor & cur)
705 {
706         //lyxerr << "LCursor::eraseSelection begin: " << cur << endl;
707         CursorSlice const & i1 = cur.selBegin();
708         CursorSlice const & i2 = cur.selEnd();
709         if (i1.inset().asMathInset()) {
710                 cur.top() = i1;
711                 if (i1.idx() == i2.idx()) {
712                         i1.cell().erase(i1.pos(), i2.pos());
713                 } else {
714                         MathInset * p = i1.asMathInset();
715                         InsetBase::row_type r1, r2;
716                         InsetBase::col_type c1, c2;
717                         region(i1, i2, r1, r2, c1, c2);
718                         for (InsetBase::row_type row = r1; row <= r2; ++row)
719                                 for (InsetBase::col_type col = c1; col <= c2; ++col)
720                                         p->cell(p->index(row, col)).clear();
721                         // We've deleted the whole cell. Only pos 0 is valid.
722                         cur.pos() = 0;
723                 }
724                 cur.resetAnchor();
725         } else {
726                 lyxerr << "can't erase this selection 1" << endl;
727         }
728         //lyxerr << "LCursor::eraseSelection end: " << cur << endl;
729 }
730
731
732 void selDel(LCursor & cur)
733 {
734         //lyxerr << "LCursor::selDel" << endl;
735         if (cur.selection()) {
736                 eraseSelection(cur);
737                 cur.selection() = false;
738         }
739 }
740
741
742 void selClearOrDel(LCursor & cur)
743 {
744         //lyxerr << "LCursor::selClearOrDel" << endl;
745         if (lyxrc.auto_region_delete)
746                 selDel(cur);
747         else
748                 cur.selection() = false;
749 }
750
751
752 string grabSelection(LCursor & cur)
753 {
754         if (!cur.selection())
755                 return string();
756
757         CursorSlice i1 = cur.selBegin();
758         CursorSlice i2 = cur.selEnd();
759
760         if (i1.idx() == i2.idx()) {
761                 if (i1.inset().asMathInset()) {
762                         MathArray::const_iterator it = i1.cell().begin();
763                         return asString(MathArray(it + i1.pos(), it + i2.pos()));
764                 } else {
765                         return "unknown selection 1";
766                 }
767         }
768
769         InsetBase::row_type r1, r2;
770         InsetBase::col_type c1, c2;
771         region(i1, i2, r1, r2, c1, c2);
772
773         string data;
774         if (i1.inset().asMathInset()) {
775                 for (InsetBase::row_type row = r1; row <= r2; ++row) {
776                         if (row > r1)
777                                 data += "\\\\";
778                         for (InsetBase::col_type col = c1; col <= c2; ++col) {
779                                 if (col > c1)
780                                         data += '&';
781                                 data += asString(i1.asMathInset()->
782                                         cell(i1.asMathInset()->index(row, col)));
783                         }
784                 }
785         } else {
786                 data = "unknown selection 2";
787         }
788         return data;
789 }
790
791
792 void dirtyTabularStack(bool b)
793 {
794         dirty_tabular_stack_ = b;
795 }
796
797
798 bool tabularStackDirty()
799 {
800         return dirty_tabular_stack_;
801 }
802
803
804 } // namespace cap
805 } // namespace lyx