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