]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
drop reLyX like it is hot, and setup to use tex2lyx instead (and remerged po files...
[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 void pasteSelectionHelper(LCursor & cur, size_t cut_index)
103 {
104         recordUndo(cur);
105         Buffer const & buffer = cur.buffer();
106         InsetText & inset = static_cast<InsetText &>(cur.inset());
107         pit_type pit = cur.pit();
108         pos_type pos = cur.pos();
109         pit_type endpit = pit;
110         textclass_type const & tc = buffer.params().textclass;
111         ErrorList errorlist;
112
113         if (checkPastePossible(cut_index)) {
114                 ParagraphList & pars = inset.paragraphs();
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                                                 !inset.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                 endpit = last_paste + 1;
253         }
254
255         bufferErrors(cur.buffer(), errorlist);
256         cur.bv().showErrorList(_("Paste"));
257         cur.clearSelection();
258         cur.resetAnchor();
259         BOOST_ASSERT(cur.text());
260         cur.text()->setCursor(cur, pit, pos);
261         cur.setSelection();
262         updateCounters(cur.buffer());
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         if (startpit == pit_type(pars.size()) ||
272             (startpos > pars[startpit].size()))
273                 return PitPosPair(endpit, endpos);
274
275         if (endpit == pit_type(pars.size()) ||
276             startpit == endpit) {
277                 endpos -= pars[startpit].erase(startpos, endpos);
278                 return PitPosPair(endpit, endpos);
279         }
280
281         // clear end/begin fragments of the first/last par in selection
282         bool all_erased = true;
283
284         pars[startpit].erase(startpos, pars[startpit].size());
285         if (pars[startpit].size() != startpos)
286                 all_erased = false;
287
288         endpos -= pars[endpit].erase(0, endpos);
289         if (endpos != 0)
290                 all_erased = false;
291
292         // Loop through the deleted pars if any, erasing as needed
293         for (pit_type pit = startpit + 1; pit != endpit;) {
294                 // "erase" the contents of the par
295                 pars[pit].erase(0, pars[pit].size());
296                 if (!pars[pit].size()) {
297                         // remove the par if it's now empty
298                         pars.erase(pars.begin() + pit);
299                         --endpit;
300                 } else {
301                         ++pit;
302                         all_erased = false;
303                 }
304         }
305
306 #if 0 // FIXME: why for cut but not copy ?
307         // the cut selection should begin with standard layout
308         if (realcut) {
309                 buf->params().clear();
310                 buf->bibkey = 0;
311                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
312         }
313 #endif
314
315         if (startpit + 1 == pit_type(pars.size()))
316                 return PitPosPair(endpit, endpos);
317
318         if (doclear) {
319                 pars[startpit + 1].stripLeadingSpaces();
320         }
321
322         // paste the paragraphs again, if possible
323         if (all_erased &&
324             (pars[startpit].hasSameLayout(pars[startpit + 1]) ||
325              pars[startpit + 1].empty())) {
326                 mergeParagraph(params, pars, startpit);
327                 // this because endpar gets deleted here!
328                 endpit = startpit;
329                 endpos = startpos;
330         }
331
332         return PitPosPair(endpit, endpos);
333
334 }
335
336
337 void copySelectionHelper(ParagraphList & pars,
338         pit_type startpit, pit_type endpit,
339         int start, int end, textclass_type tc)
340 {
341         BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
342         BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
343         BOOST_ASSERT(startpit != endpit || start <= end);
344
345         // Clone the paragraphs within the selection.
346         ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
347         for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
348
349         // Cut out the end of the last paragraph.
350         Paragraph & back = paragraphs.back();
351         back.erase(end, back.size());
352
353         // Cut out the begin of the first paragraph
354         Paragraph & front = paragraphs.front();
355         front.erase(0, start);
356
357         theCuts.push(make_pair(paragraphs, tc));
358 }
359
360 } // namespace anon
361
362
363 namespace lyx {
364 namespace cap {
365
366 string grabAndEraseSelection(LCursor & cur)
367 {
368         if (!cur.selection())
369                 return string();
370         string res = grabSelection(cur);
371         eraseSelection(cur);
372         cur.selection() = false;
373         return res;
374 }
375
376
377 void SwitchBetweenClasses(textclass_type c1, textclass_type c2,
378         ParagraphList & pars, ErrorList & errorlist)
379 {
380         BOOST_ASSERT(!pars.empty());
381         if (c1 == c2)
382                 return;
383
384         LyXTextClass const & tclass1 = textclasslist[c1];
385         LyXTextClass const & tclass2 = textclasslist[c2];
386
387         InsetText in;
388         std::swap(in.paragraphs(), pars);
389
390         // layouts
391         ParIterator end = par_iterator_end(in);
392         for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
393                 string const name = it->layout()->name();
394                 bool hasLayout = tclass2.hasLayout(name);
395
396                 if (hasLayout)
397                         it->layout(tclass2[name]);
398                 else
399                         it->layout(tclass2.defaultLayout());
400
401                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
402                         string const s = bformat(
403                                 _("Layout had to be changed from\n%1$s to %2$s\n"
404                                 "because of class conversion from\n%3$s to %4$s"),
405                          name, it->layout()->name(), tclass1.name(), tclass2.name());
406                         // To warn the user that something had to be done.
407                         errorlist.push_back(ErrorItem(_("Changed Layout"), s,
408                                                       it->id(), 0,
409                                                       it->size()));
410                 }
411         }
412
413         // character styles
414         InsetIterator const i_end = inset_iterator_end(in);
415         for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
416                 if (it->lyxCode() == InsetBase::CHARSTYLE_CODE) {
417                         InsetCharStyle & inset =
418                                 static_cast<InsetCharStyle &>(*it);
419                         string const name = inset.params().type;
420                         CharStyles::iterator const found_cs =
421                                 tclass2.charstyle(name);
422                         if (found_cs == tclass2.charstyles().end()) {
423                                 // The character style is undefined in tclass2
424                                 inset.setUndefined();
425                                 string const s = bformat(_(
426                                         "Character style %1$s is "
427                                         "undefined because of class "
428                                         "conversion from\n%2$s to %3$s"),
429                                          name, tclass1.name(), tclass2.name());
430                                 // To warn the user that something had to be done.
431                                 errorlist.push_back(ErrorItem(
432                                                 _("Undefined character style"),
433                                                 s, it.paragraph().id(),
434                                                 it.pos(), it.pos() + 1));
435                         } else if (inset.undefined()) {
436                                 // The character style is undefined in
437                                 // tclass1 and is defined in tclass2
438                                 inset.setDefined(found_cs);
439                         }
440                 }
441         }
442
443         std::swap(in.paragraphs(), pars);
444 }
445
446
447 std::vector<string> const availableSelections(Buffer const & buffer)
448 {
449         vector<string> selList;
450
451         CutStack::const_iterator cit = theCuts.begin();
452         CutStack::const_iterator end = theCuts.end();
453         for (; cit != end; ++cit) {
454                 // we do not use cit-> here because gcc 2.9x does not
455                 // like it (JMarc)
456                 ParagraphList const & pars = (*cit).first;
457                 string asciiSel;
458                 ParagraphList::const_iterator pit = pars.begin();
459                 ParagraphList::const_iterator pend = pars.end();
460                 for (; pit != pend; ++pit) {
461                         asciiSel += pit->asString(buffer, false);
462                         if (asciiSel.size() > 25) {
463                                 asciiSel.replace(22, string::npos, "...");
464                                 break;
465                         }
466                 }
467
468                 selList.push_back(asciiSel);
469         }
470
471         return selList;
472 }
473
474
475 int nrOfParagraphs()
476 {
477         return theCuts.empty() ? 0 : theCuts[0].first.size();
478 }
479
480
481 void cutSelection(LCursor & cur, bool doclear, bool realcut)
482 {
483         if (cur.inTexted()) {
484                 LyXText * text = cur.text();
485                 BOOST_ASSERT(text);
486                 // Stuff what we got on the clipboard. Even if there is no selection.
487
488                 // There is a problem with having the stuffing here in that the
489                 // larger the selection the slower LyX will get. This can be
490                 // solved by running the line below only when the selection has
491                 // finished. The solution used currently just works, to make it
492                 // faster we need to be more clever and probably also have more
493                 // calls to stuffClipboard. (Lgb)
494                 cur.bv().stuffClipboard(cur.selectionAsString(true));
495
496                 // This doesn't make sense, if there is no selection
497                 if (!cur.selection())
498                         return;
499
500                 // OK, we have a selection. This is always between cur.selBegin()
501                 // and cur.selEnd()
502
503                 // make sure that the depth behind the selection are restored, too
504                 recordUndoSelection(cur);
505                 pit_type begpit = cur.selBegin().pit();
506                 pit_type endpit = cur.selEnd().pit();
507
508                 int endpos = cur.selEnd().pos();
509
510                 BufferParams const & bp = cur.buffer().params();
511                 if (realcut) {
512                         copySelectionHelper(text->paragraphs(),
513                                 begpit, endpit,
514                                 cur.selBegin().pos(), endpos,
515                                 bp.textclass);
516                 }
517
518                 boost::tie(endpit, endpos) =
519                         eraseSelectionHelper(bp,
520                                 text->paragraphs(),
521                                 begpit, endpit,
522                                 cur.selBegin().pos(), endpos,
523                                 doclear);
524
525                 // sometimes necessary
526                 if (doclear)
527                         text->paragraphs()[begpit].stripLeadingSpaces();
528
529                 // cutSelection can invalidate the cursor so we need to set
530                 // it anew. (Lgb)
531                 // we prefer the end for when tracking changes
532                 cur.pos() = endpos;
533                 cur.pit() = endpit;
534
535                 // need a valid cursor. (Lgb)
536                 cur.clearSelection();
537                 updateCounters(cur.buffer());
538         }
539
540         if (cur.inMathed()) {
541                 lyxerr << "cutSelection in mathed" << endl;
542                 LCursor tmp = cur;
543                 copySelection(cur);
544                 cur.selection() = false;
545                 eraseSelection(tmp);
546         }
547 }
548
549
550 void copySelection(LCursor & cur)
551 {
552         // stuff the selection onto the X clipboard, from an explicit copy request
553         cur.bv().stuffClipboard(cur.selectionAsString(true));
554
555         // this doesn't make sense, if there is no selection
556         if (!cur.selection())
557                 return;
558
559         if (cur.inTexted()) {
560                 LyXText * text = cur.text();
561                 BOOST_ASSERT(text);
562                 // ok we have a selection. This is always between cur.selBegin()
563                 // and sel_end cursor
564
565                 // copy behind a space if there is one
566                 ParagraphList & pars = text->paragraphs();
567                 pos_type pos = cur.selBegin().pos();
568                 pit_type par = cur.selBegin().pit();
569                 while (pos < pars[par].size()
570                                          && pars[par].isLineSeparator(pos)
571                                          && (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
572                         ++pos;
573
574                 copySelectionHelper(pars, par, cur.selEnd().pit(),
575                         pos, cur.selEnd().pos(), cur.buffer().params().textclass);
576         }
577
578         if (cur.inMathed()) {
579                 //lyxerr << "copySelection in mathed" << endl;
580                 ParagraphList pars;
581                 pars.push_back(Paragraph());
582                 BufferParams const & bp = cur.buffer().params();
583                 pars.back().layout(bp.getLyXTextClass().defaultLayout());
584                 for_each(pars.begin(), pars.end(), resetOwnerAndChanges());
585                 pars.back().insert(0, grabSelection(cur), LyXFont());
586                 theCuts.push(make_pair(pars, bp.textclass));
587         }
588 }
589
590
591 std::string getSelection(Buffer const & buf, size_t sel_index)
592 {
593         return sel_index < theCuts.size()
594                 ? theCuts[sel_index].first.back().asString(buf, false)
595                 : string();
596 }
597
598
599 void pasteSelection(LCursor & cur, size_t sel_index)
600 {
601         // this does not make sense, if there is nothing to paste
602         if (!checkPastePossible(sel_index))
603                 return;
604         if (cur.inTexted())
605                 pasteSelectionHelper(cur, sel_index);
606         if (cur.inMathed()) 
607                 lyxerr << "### should be handled in MathNest/GridInset" << endl;
608 }
609
610
611 void setSelectionRange(LCursor & cur, pos_type length)
612 {
613         LyXText * text = cur.text();
614         BOOST_ASSERT(text);
615         if (!length)
616                 return;
617         cur.resetAnchor();
618         while (length--)
619                 text->cursorRight(cur);
620         cur.setSelection();
621 }
622
623
624 // simple replacing. The font of the first selected character is used
625 void replaceSelectionWithString(LCursor & cur, string const & str)
626 {
627         LyXText * text = cur.text();
628         BOOST_ASSERT(text);
629         recordUndo(cur);
630
631         // Get font setting before we cut
632         pos_type pos = cur.selEnd().pos();
633         Paragraph & par = text->getPar(cur.selEnd().pit());
634         LyXFont const font =
635                 par.getFontSettings(cur.buffer().params(), cur.selBegin().pos());
636
637         // Insert the new string
638         string::const_iterator cit = str.begin();
639         string::const_iterator end = str.end();
640         for (; cit != end; ++cit, ++pos)
641                 par.insertChar(pos, (*cit), font);
642
643         // Cut the selection
644         cutSelection(cur, true, false);
645 }
646
647
648 void replaceSelection(LCursor & cur)
649 {
650         if (cur.selection())
651                 cutSelection(cur, true, false);
652 }
653
654
655 // only used by the spellchecker
656 void replaceWord(LCursor & cur, string const & replacestring)
657 {
658         LyXText * text = cur.text();
659         BOOST_ASSERT(text);
660
661         replaceSelectionWithString(cur, replacestring);
662         setSelectionRange(cur, replacestring.length());
663
664         // Go back so that replacement string is also spellchecked
665         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
666                 text->cursorLeft(cur);
667 }
668
669
670 void eraseSelection(LCursor & cur)
671 {
672         //lyxerr << "LCursor::eraseSelection begin: " << cur << endl;
673         CursorSlice const & i1 = cur.selBegin();
674         CursorSlice const & i2 = cur.selEnd();
675         if (i1.inset().asMathInset()) {
676                 cur.top() = i1;
677                 if (i1.idx() == i2.idx()) {
678                         i1.cell().erase(i1.pos(), i2.pos());
679                 } else {
680                         MathInset * p = i1.asMathInset();
681                         InsetBase::row_type r1, r2;
682                         InsetBase::col_type c1, c2;
683                         region(i1, i2, r1, r2, c1, c2);
684                         for (InsetBase::row_type row = r1; row <= r2; ++row)
685                                 for (InsetBase::col_type col = c1; col <= c2; ++col)
686                                         p->cell(p->index(row, col)).clear();
687                         // We've deleted the whole cell. Only pos 0 is valid.
688                         cur.pos() = 0;
689                 }
690                 cur.resetAnchor();
691         } else {
692                 lyxerr << "can't erase this selection 1" << endl;
693         }
694         //lyxerr << "LCursor::eraseSelection end: " << cur << endl;
695 }
696
697
698 void selDel(LCursor & cur)
699 {
700         //lyxerr << "LCursor::selDel" << endl;
701         if (cur.selection()) {
702                 eraseSelection(cur);
703                 cur.selection() = false;
704         }
705 }
706
707
708 void selClearOrDel(LCursor & cur)
709 {
710         //lyxerr << "LCursor::selClearOrDel" << endl;
711         if (lyxrc.auto_region_delete)
712                 selDel(cur);
713         else
714                 cur.selection() = false;
715 }
716
717
718 string grabSelection(LCursor & cur)
719 {
720         if (!cur.selection())
721                 return string();
722
723         CursorSlice i1 = cur.selBegin();
724         CursorSlice i2 = cur.selEnd();
725
726         if (i1.idx() == i2.idx()) {
727                 if (i1.inset().asMathInset()) {
728                         MathArray::const_iterator it = i1.cell().begin();
729                         return asString(MathArray(it + i1.pos(), it + i2.pos()));
730                 } else {
731                         return "unknown selection 1";
732                 }
733         }
734
735         InsetBase::row_type r1, r2;
736         InsetBase::col_type c1, c2;
737         region(i1, i2, r1, r2, c1, c2);
738
739         string data;
740         if (i1.inset().asMathInset()) {
741                 for (InsetBase::row_type row = r1; row <= r2; ++row) {
742                         if (row > r1)
743                                 data += "\\\\";
744                         for (InsetBase::col_type col = c1; col <= c2; ++col) {
745                                 if (col > c1)
746                                         data += '&';
747                                 data += asString(i1.asMathInset()->
748                                         cell(i1.asMathInset()->index(row, col)));
749                         }
750                 }
751         } else {
752                 data = "unknown selection 2";
753         }
754         return data;
755 }
756
757
758 } // namespace cap
759 } // namespace lyx