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