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