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