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