2 * \file CutAndPaste.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
11 * Full author contact details are available in file CREDITS.
16 #include "CutAndPaste.h"
19 #include "buffer_funcs.h"
20 #include "BufferList.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
25 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "InsetIterator.h"
29 #include "InsetList.h"
34 #include "Paragraph.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphParameters.h"
37 #include "ParIterator.h"
40 #include "insets/InsetFlex.h"
41 #include "insets/InsetCommand.h"
42 #include "insets/InsetGraphics.h"
43 #include "insets/InsetGraphicsParams.h"
44 #include "insets/InsetTabular.h"
46 #include "mathed/MathData.h"
47 #include "mathed/InsetMath.h"
48 #include "mathed/MathSupport.h"
50 #include "support/debug.h"
51 #include "support/docstream.h"
52 #include "support/gettext.h"
53 #include "support/limited_stack.h"
54 #include "support/lstrings.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Selection.h"
59 #include <boost/tuple/tuple.hpp>
60 #include <boost/next_prior.hpp>
65 using namespace lyx::support;
66 using lyx::frontend::Clipboard;
72 typedef pair<pit_type, int> PitPosPair;
74 typedef limited_stack<pair<ParagraphList, DocumentClass const *> > CutStack;
77 // persistent selection, cleared until the next selection
78 CutStack selectionBuffer(1);
80 // store whether the tabular stack is newer than the normal copy stack
81 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
82 // when we (hopefully) have a one-for-all paste mechanism.
83 bool dirty_tabular_stack_ = false;
86 bool checkPastePossible(int index)
88 return size_t(index) < theCuts.size() && !theCuts[index].first.empty();
92 pair<PitPosPair, pit_type>
93 pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
94 DocumentClass const * const oldDocClass, ErrorList & errorlist)
96 Buffer const & buffer = cur.buffer();
97 pit_type pit = cur.pit();
98 pos_type pos = cur.pos();
99 ParagraphList & pars = cur.text()->paragraphs();
102 return make_pair(PitPosPair(pit, pos), pit);
104 BOOST_ASSERT (pos <= pars[pit].size());
106 // Make a copy of the CaP paragraphs.
107 ParagraphList insertion = parlist;
108 DocumentClass const * const newDocClass =
109 buffer.params().documentClassPtr();
111 // Now remove all out of the pars which is NOT allowed in the
112 // new environment and set also another font if that is required.
114 // Convert newline to paragraph break in ERT inset.
115 // This should not be here!
116 Inset * inset = pars[pit].inInset();
117 if (inset && (inset->lyxCode() == ERT_CODE ||
118 inset->lyxCode() == LISTINGS_CODE)) {
119 for (size_t i = 0; i != insertion.size(); ++i) {
120 for (pos_type j = 0; j != insertion[i].size(); ++j) {
121 if (insertion[i].isNewline(j)) {
122 // do not track deletion of newline
123 insertion[i].eraseChar(j, false);
124 breakParagraphConservative(
133 // set the paragraphs to empty layout if necessary
134 if (cur.inset().usePlainLayout()) {
135 bool forcePlainLayout = cur.inset().forcePlainLayout();
136 Layout const & emptyLayout = newDocClass->emptyLayout();
137 Layout const & defaultLayout = newDocClass->defaultLayout();
138 ParagraphList::iterator const end = insertion.end();
139 ParagraphList::iterator par = insertion.begin();
140 for (; par != end; ++par) {
141 Layout const & parLayout = par->layout();
142 if (forcePlainLayout || parLayout == defaultLayout)
143 par->setLayout(emptyLayout);
145 } else { // check if we need to reset from empty layout
146 Layout const & defaultLayout = newDocClass->defaultLayout();
147 Layout const & emptyLayout = newDocClass->emptyLayout();
148 ParagraphList::iterator const end = insertion.end();
149 ParagraphList::iterator par = insertion.begin();
150 for (; par != end; ++par) {
151 Layout const & parLayout = par->layout();
152 if (parLayout == emptyLayout)
153 par->setLayout(defaultLayout);
157 // Make sure there is no class difference.
159 // This works without copying any paragraph data because we have
160 // a specialized swap method for ParagraphList. This is important
161 // since we store pointers to insets at some places and we don't
162 // want to invalidate them.
163 insertion.swap(in.paragraphs());
164 cap::switchBetweenClasses(oldDocClass, newDocClass, in, errorlist);
165 insertion.swap(in.paragraphs());
167 ParagraphList::iterator tmpbuf = insertion.begin();
168 int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
170 depth_type max_depth = pars[pit].getMaxDepthAfter();
172 for (; tmpbuf != insertion.end(); ++tmpbuf) {
173 // If we have a negative jump so that the depth would
174 // go below 0 depth then we have to redo the delta to
175 // this new max depth level so that subsequent
176 // paragraphs are aligned correctly to this paragraph
178 if (int(tmpbuf->params().depth()) + depth_delta < 0)
181 // Set the right depth so that we are not too deep or shallow.
182 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
183 if (tmpbuf->params().depth() > max_depth)
184 tmpbuf->params().depth(max_depth);
186 // Only set this from the 2nd on as the 2nd depends
187 // for maxDepth still on pit.
188 if (tmpbuf != insertion.begin())
189 max_depth = tmpbuf->getMaxDepthAfter();
191 // Set the inset owner of this paragraph.
192 tmpbuf->setInsetOwner(pars[pit].inInset());
193 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
194 // do not track deletion of invalid insets
195 if (Inset * inset = tmpbuf->getInset(i))
196 if (!pars[pit].insetAllowed(inset->lyxCode()))
197 tmpbuf->eraseChar(i--, false);
200 tmpbuf->setChange(Change(buffer.params().trackChanges ?
201 Change::INSERTED : Change::UNCHANGED));
204 bool const empty = pars[pit].empty();
206 // Make the buf exactly the same layout as the cursor
208 insertion.begin()->makeSameLayout(pars[pit]);
211 // Prepare the paragraphs and insets for insertion.
212 // A couple of insets store buffer references so need updating.
213 insertion.swap(in.paragraphs());
215 InsetIterator const i_end = inset_iterator_end(in);
217 for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
219 it->setBuffer(const_cast<Buffer &>(buffer));
221 switch (it->lyxCode()) {
224 // check for duplicates
225 InsetCommand & lab = static_cast<InsetCommand &>(*it);
226 docstring const oldname = lab.getParam("name");
227 lab.updateCommand(oldname, false);
228 docstring const newname = lab.getParam("name");
229 if (oldname != newname) {
230 // adapt the references
231 for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
232 if (itt->lyxCode() == REF_CODE) {
233 InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
234 if (ref.getParam("reference") == oldname)
235 ref.setParam("reference", newname);
243 // check for duplicates
244 InsetCommand & bib = static_cast<InsetCommand &>(*it);
245 docstring const oldkey = bib.getParam("key");
246 bib.updateCommand(oldkey, false);
247 docstring const newkey = bib.getParam("key");
248 if (oldkey != newkey) {
249 // adapt the references
250 for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
251 if (itt->lyxCode() == CITE_CODE) {
252 InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
253 if (ref.getParam("key") == oldkey)
254 ref.setParam("key", newkey);
265 insertion.swap(in.paragraphs());
267 // Split the paragraph for inserting the buf if necessary.
269 breakParagraphConservative(buffer.params(), pars, pit, pos);
273 pars.insert(boost::next(pars.begin(), pit),
277 // merge the empty par with the last par of the insertion
278 mergeParagraph(buffer.params(), pars,
279 pit + insertion.size() - 1);
281 pars.insert(boost::next(pars.begin(), pit + 1),
285 // merge the first par of the insertion with the current par
286 mergeParagraph(buffer.params(), pars, pit);
289 pit_type last_paste = pit + insertion.size() - 1;
291 // Store the new cursor position.
293 pos = pars[last_paste].size();
295 // Join (conditionally) last pasted paragraph with next one, i.e.,
296 // the tail of the spliced document paragraph
297 if (!empty && last_paste + 1 != pit_type(pars.size())) {
298 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
299 mergeParagraph(buffer.params(), pars, last_paste);
300 } else if (pars[last_paste + 1].empty()) {
301 pars[last_paste + 1].makeSameLayout(pars[last_paste]);
302 mergeParagraph(buffer.params(), pars, last_paste);
303 } else if (pars[last_paste].empty()) {
304 pars[last_paste].makeSameLayout(pars[last_paste + 1]);
305 mergeParagraph(buffer.params(), pars, last_paste);
307 pars[last_paste + 1].stripLeadingSpaces(buffer.params().trackChanges);
312 return make_pair(PitPosPair(pit, pos), last_paste + 1);
316 PitPosPair eraseSelectionHelper(BufferParams const & params,
317 ParagraphList & pars,
318 pit_type startpit, pit_type endpit,
319 int startpos, int endpos)
321 // Start of selection is really invalid.
322 if (startpit == pit_type(pars.size()) ||
323 (startpos > pars[startpit].size()))
324 return PitPosPair(endpit, endpos);
326 // Start and end is inside same paragraph
327 if (endpit == pit_type(pars.size()) || startpit == endpit) {
328 endpos -= pars[startpit].eraseChars(startpos, endpos, params.trackChanges);
329 return PitPosPair(endpit, endpos);
332 for (pit_type pit = startpit; pit != endpit + 1;) {
333 pos_type const left = (pit == startpit ? startpos : 0);
334 pos_type right = (pit == endpit ? endpos : pars[pit].size() + 1);
335 bool const merge = pars[pit].isMergedOnEndOfParDeletion(params.trackChanges);
337 // Logically erase only, including the end-of-paragraph character
338 pars[pit].eraseChars(left, right, params.trackChanges);
340 // Separate handling of paragraph break:
341 if (merge && pit != endpit &&
343 || pars[pit].hasSameLayout(pars[endpit])
344 || pars[endpit].size() == endpos)) {
345 if (pit + 1 == endpit)
346 endpos += pars[pit].size();
347 mergeParagraph(params, pars, pit);
353 // Ensure legal cursor pos:
356 return PitPosPair(endpit, endpos);
360 void putClipboard(ParagraphList const & paragraphs,
361 DocumentClass const * const docclass, docstring const & plaintext)
363 // For some strange reason gcc 3.2 and 3.3 do not accept
364 // Buffer buffer(string(), false);
365 // This needs to be static to avoid a memory leak. When a Buffer is
366 // constructed, it constructs a BufferParams, which in turn constructs
367 // a DocumentClass, via new, that is never deleted. If we were to go to
368 // some kind of garbage collection there, or a shared_ptr, then this
369 // would not be needed.
370 static Buffer * buffer = theBufferList().newBuffer(
371 FileName::tempName().absFilename() + "_clipboard.internal");
372 buffer->setUnnamed(true);
373 buffer->paragraphs() = paragraphs;
374 buffer->inset().setBuffer(*buffer);
375 buffer->params().setDocumentClass(docclass);
377 if (buffer->write(lyx))
378 theClipboard().put(lyx.str(), plaintext);
380 theClipboard().put(string(), plaintext);
382 buffer->paragraphs().clear();
386 void copySelectionHelper(Buffer const & buf, ParagraphList & pars,
387 pit_type startpit, pit_type endpit,
388 int start, int end, DocumentClass const * const dc, CutStack & cutstack)
390 LASSERT(0 <= start && start <= pars[startpit].size(), /**/);
391 LASSERT(0 <= end && end <= pars[endpit].size(), /**/);
392 LASSERT(startpit != endpit || start <= end, /**/);
394 // Clone the paragraphs within the selection.
395 ParagraphList copy_pars(boost::next(pars.begin(), startpit),
396 boost::next(pars.begin(), endpit + 1));
398 // Remove the end of the last paragraph; afterwards, remove the
399 // beginning of the first paragraph. Keep this order - there may only
400 // be one paragraph! Do not track deletions here; this is an internal
401 // action not visible to the user
403 Paragraph & back = copy_pars.back();
404 back.eraseChars(end, back.size(), false);
405 Paragraph & front = copy_pars.front();
406 front.eraseChars(0, start, false);
408 ParagraphList::iterator it = copy_pars.begin();
409 ParagraphList::iterator it_end = copy_pars.end();
411 for (; it != it_end; it++) {
412 // ERT paragraphs have the Language latex_language.
413 // This is invalid outside of ERT, so we need to change it
414 // to the buffer language.
415 if (it->ownerCode() == ERT_CODE || it->ownerCode() == LISTINGS_CODE) {
416 it->changeLanguage(buf.params(), latex_language, buf.language());
418 it->setInsetOwner(0);
421 // do not copy text (also nested in insets) which is marked as deleted
422 acceptChanges(copy_pars, buf.params());
424 DocumentClass * d = const_cast<DocumentClass *>(dc);
425 cutstack.push(make_pair(copy_pars, d));
435 void region(CursorSlice const & i1, CursorSlice const & i2,
436 Inset::row_type & r1, Inset::row_type & r2,
437 Inset::col_type & c1, Inset::col_type & c2)
439 Inset & p = i1.inset();
440 c1 = p.col(i1.idx());
441 c2 = p.col(i2.idx());
444 r1 = p.row(i1.idx());
445 r2 = p.row(i2.idx());
451 docstring grabAndEraseSelection(Cursor & cur)
453 if (!cur.selection())
455 docstring res = grabSelection(cur);
461 bool reduceSelectionToOneCell(Cursor & cur)
463 if (!cur.selection() || !cur.inMathed())
466 CursorSlice i1 = cur.selBegin();
467 CursorSlice i2 = cur.selEnd();
468 if (!i1.inset().asInsetMath())
471 // the easy case: do nothing if only one cell is selected
472 if (i1.idx() == i2.idx())
477 cur.top().pos() = cur.top().lastpos();
483 bool multipleCellsSelected(Cursor const & cur)
485 if (!cur.selection() || !cur.inMathed())
488 CursorSlice i1 = cur.selBegin();
489 CursorSlice i2 = cur.selEnd();
490 if (!i1.inset().asInsetMath())
493 if (i1.idx() == i2.idx())
500 void switchBetweenClasses(DocumentClass const * const oldone,
501 DocumentClass const * const newone, InsetText & in, ErrorList & errorlist)
505 LASSERT(!in.paragraphs().empty(), /**/);
506 if (oldone == newone)
509 DocumentClass const & oldtc = *oldone;
510 DocumentClass const & newtc = *newone;
513 ParIterator end = par_iterator_end(in);
514 for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
515 docstring const name = it->layout().name();
517 // the pasted text will keep their own layout name. If this layout does
518 // not exist in the new document, it will behave like a standard layout.
519 newtc.addLayoutIfNeeded(name);
521 if (in.usePlainLayout())
522 it->setLayout(newtc.emptyLayout());
524 it->setLayout(newtc[name]);
528 InsetIterator const i_end = inset_iterator_end(in);
529 for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
530 InsetCollapsable * inset = it->asInsetCollapsable();
533 if (inset->lyxCode() != FLEX_CODE)
534 // FIXME: Should we verify all InsetCollapsable?
536 inset->setLayout(newone);
537 if (!inset->undefined())
539 // The flex inset is undefined in newtc
540 docstring const s = bformat(_(
541 "Flex inset %1$s is "
542 "undefined because of class "
543 "conversion from\n%2$s to %3$s"),
544 inset->name(), from_utf8(oldtc.name()),
545 from_utf8(newtc.name()));
546 // To warn the user that something had to be done.
547 errorlist.push_back(ErrorItem(
548 _("Undefined flex inset"),
549 s, it.paragraph().id(), it.pos(), it.pos() + 1));
554 vector<docstring> availableSelections()
556 vector<docstring> selList;
558 CutStack::const_iterator cit = theCuts.begin();
559 CutStack::const_iterator end = theCuts.end();
560 for (; cit != end; ++cit) {
561 // we do not use cit-> here because gcc 2.9x does not
563 ParagraphList const & pars = (*cit).first;
565 ParagraphList::const_iterator pit = pars.begin();
566 ParagraphList::const_iterator pend = pars.end();
567 for (; pit != pend; ++pit) {
568 asciiSel += pit->asString(AS_STR_INSETS);
569 if (asciiSel.size() > 25) {
570 asciiSel.replace(22, docstring::npos,
576 selList.push_back(asciiSel);
583 size_type numberOfSelections()
585 return theCuts.size();
589 void cutSelection(Cursor & cur, bool doclear, bool realcut)
591 // This doesn't make sense, if there is no selection
592 if (!cur.selection())
595 // OK, we have a selection. This is always between cur.selBegin()
598 if (cur.inTexted()) {
599 Text * text = cur.text();
604 // make sure that the depth behind the selection are restored, too
605 cur.recordUndoSelection();
606 pit_type begpit = cur.selBegin().pit();
607 pit_type endpit = cur.selEnd().pit();
609 int endpos = cur.selEnd().pos();
611 BufferParams const & bp = cur.buffer().params();
613 copySelectionHelper(cur.buffer(),
616 cur.selBegin().pos(), endpos,
617 bp.documentClassPtr(), theCuts);
618 // Stuff what we got on the clipboard.
619 // Even if there is no selection.
620 putClipboard(theCuts[0].first, theCuts[0].second,
621 cur.selectionAsString(true));
624 if (begpit != endpit)
625 cur.updateFlags(Update::Force | Update::FitCursor);
627 boost::tie(endpit, endpos) =
628 eraseSelectionHelper(bp,
631 cur.selBegin().pos(), endpos);
633 // cutSelection can invalidate the cursor so we need to set
635 // we prefer the end for when tracking changes
639 // sometimes necessary
641 && text->paragraphs()[begpit].stripLeadingSpaces(bp.trackChanges))
644 // need a valid cursor. (Lgb)
645 cur.clearSelection();
646 updateLabels(cur.buffer());
648 // tell tabular that a recent copy happened
649 dirtyTabularStack(false);
652 if (cur.inMathed()) {
653 if (cur.selBegin().idx() != cur.selEnd().idx()) {
654 // The current selection spans more than one cell.
656 cur.recordUndoInset();
658 // Record only the current cell to avoid a jumping
669 void copySelection(Cursor & cur)
671 copySelection(cur, cur.selectionAsString(true));
677 void copySelectionToStack(Cursor & cur, CutStack & cutstack)
679 // this doesn't make sense, if there is no selection
680 if (!cur.selection())
683 // copySelection can not yet handle the case of cross idx selection
684 if (cur.selBegin().idx() != cur.selEnd().idx())
687 if (cur.inTexted()) {
688 Text * text = cur.text();
690 // ok we have a selection. This is always between cur.selBegin()
691 // and sel_end cursor
693 // copy behind a space if there is one
694 ParagraphList & pars = text->paragraphs();
695 pos_type pos = cur.selBegin().pos();
696 pit_type par = cur.selBegin().pit();
697 while (pos < pars[par].size() &&
698 pars[par].isLineSeparator(pos) &&
699 (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
702 copySelectionHelper(cur.buffer(), pars, par, cur.selEnd().pit(),
703 pos, cur.selEnd().pos(),
704 cur.buffer().params().documentClassPtr(), cutstack);
705 dirtyTabularStack(false);
708 if (cur.inMathed()) {
709 //lyxerr << "copySelection in mathed" << endl;
712 BufferParams const & bp = cur.buffer().params();
713 // FIXME This should be the empty layout...right?
714 par.setLayout(bp.documentClass().emptyLayout());
715 par.insert(0, grabSelection(cur), Font(), Change(Change::UNCHANGED));
717 cutstack.push(make_pair(pars, bp.documentClassPtr()));
724 void copySelectionToStack()
726 if (!selectionBuffer.empty())
727 theCuts.push(selectionBuffer[0]);
731 void copySelection(Cursor & cur, docstring const & plaintext)
733 // In tablemode, because copy and paste actually use special table stack
734 // we do not attempt to get selected paragraphs under cursor. Instead, a
735 // paragraph with the plain text version is generated so that table cells
736 // can be pasted as pure text somewhere else.
737 if (cur.selBegin().idx() != cur.selEnd().idx()) {
740 BufferParams const & bp = cur.buffer().params();
741 par.setLayout(bp.documentClass().emptyLayout());
742 par.insert(0, plaintext, Font(), Change(Change::UNCHANGED));
744 theCuts.push(make_pair(pars, bp.documentClassPtr()));
746 copySelectionToStack(cur, theCuts);
749 // stuff the selection onto the X clipboard, from an explicit copy request
750 putClipboard(theCuts[0].first, theCuts[0].second, plaintext);
754 void saveSelection(Cursor & cur)
756 // This function is called, not when a selection is formed, but when
757 // a selection is cleared. Therefore, multiple keyboard selection
758 // will not repeatively trigger this function (bug 3877).
760 && cur.selBegin() == cur.bv().cursor().selBegin()
761 && cur.selEnd() == cur.bv().cursor().selEnd()) {
762 LYXERR(Debug::SELECTION, "saveSelection: '" << cur.selectionAsString(true) << "'");
763 copySelectionToStack(cur, selectionBuffer);
770 return !selectionBuffer.empty();
774 void clearSelection()
776 selectionBuffer.clear();
786 docstring selection(size_t sel_index)
788 return sel_index < theCuts.size()
789 ? theCuts[sel_index].first.back().asString(AS_STR_INSETS)
794 void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
795 DocumentClass const * const docclass, ErrorList & errorList)
797 if (cur.inTexted()) {
798 Text * text = cur.text();
804 boost::tie(ppp, endpit) =
805 pasteSelectionHelper(cur, parlist, docclass, errorList);
806 updateLabels(cur.buffer());
807 cur.clearSelection();
808 text->setCursor(cur, ppp.first, ppp.second);
811 // mathed is handled in InsetMathNest/InsetMathGrid
812 LASSERT(!cur.inMathed(), /**/);
816 void pasteFromStack(Cursor & cur, ErrorList & errorList, size_t sel_index)
818 // this does not make sense, if there is nothing to paste
819 if (!checkPastePossible(sel_index))
823 pasteParagraphList(cur, theCuts[sel_index].first,
824 theCuts[sel_index].second, errorList);
829 void pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs)
831 // Use internal clipboard if it is the most recent one
832 if (theClipboard().isInternal()) {
833 pasteFromStack(cur, errorList, 0);
837 // First try LyX format
838 if (theClipboard().hasLyXContents()) {
839 string lyx = theClipboard().getAsLyX();
841 // For some strange reason gcc 3.2 and 3.3 do not accept
842 // Buffer buffer(string(), false);
843 Buffer buffer("", false);
844 buffer.setUnnamed(true);
845 if (buffer.readString(lyx)) {
847 pasteParagraphList(cur, buffer.paragraphs(),
848 buffer.params().documentClassPtr(), errorList);
855 // Then try plain text
856 docstring const text = theClipboard().getAsText();
861 cur.text()->insertStringAsParagraphs(cur, text);
863 cur.text()->insertStringAsLines(cur, text);
867 void pasteClipboardGraphics(Cursor & cur, ErrorList & /* errorList */,
868 Clipboard::GraphicsType preferedType)
870 LASSERT(theClipboard().hasGraphicsContents(preferedType), /**/);
872 // get picture from clipboard
873 FileName filename = theClipboard().getAsGraphics(cur, preferedType);
874 if (filename.empty())
877 // create inset for graphic
878 InsetGraphics * inset = new InsetGraphics(cur.buffer());
879 InsetGraphicsParams params;
880 params.filename = support::DocFileName(filename.absFilename());
881 inset->setParams(params);
887 void pasteSelection(Cursor & cur, ErrorList & errorList)
889 if (selectionBuffer.empty())
892 pasteParagraphList(cur, selectionBuffer[0].first,
893 selectionBuffer[0].second, errorList);
897 void replaceSelectionWithString(Cursor & cur, docstring const & str, bool backwards)
900 DocIterator selbeg = cur.selectionBegin();
902 // Get font setting before we cut
904 selbeg.paragraph().getFontSettings(cur.buffer().params(), selbeg.pos());
906 // Insert the new string
907 pos_type pos = cur.selEnd().pos();
908 Paragraph & par = cur.selEnd().paragraph();
909 docstring::const_iterator cit = str.begin();
910 docstring::const_iterator end = str.end();
911 for (; cit != end; ++cit, ++pos)
912 par.insertChar(pos, *cit, font, cur.buffer().params().trackChanges);
915 cutSelection(cur, true, false);
917 // select the replacement
919 selbeg.pos() += str.length();
920 cur.setSelection(selbeg, -int(str.length()));
922 cur.setSelection(selbeg, str.length());
926 void replaceSelection(Cursor & cur)
929 cutSelection(cur, true, false);
933 void eraseSelection(Cursor & cur)
935 //lyxerr << "cap::eraseSelection begin: " << cur << endl;
936 CursorSlice const & i1 = cur.selBegin();
937 CursorSlice const & i2 = cur.selEnd();
938 if (i1.inset().asInsetMath()) {
941 if (i1.idx() == i2.idx()) {
942 i1.cell().erase(i1.pos(), i2.pos());
943 // We may have deleted i1.cell(cur.pos()).
944 // Make sure that pos is valid.
945 if (cur.pos() > cur.lastpos())
946 cur.pos() = cur.lastpos();
948 InsetMath * p = i1.asInsetMath();
949 Inset::row_type r1, r2;
950 Inset::col_type c1, c2;
951 region(i1, i2, r1, r2, c1, c2);
952 for (Inset::row_type row = r1; row <= r2; ++row)
953 for (Inset::col_type col = c1; col <= c2; ++col)
954 p->cell(p->index(row, col)).clear();
955 // We've deleted the whole cell. Only pos 0 is valid.
958 // need a valid cursor. (Lgb)
959 cur.clearSelection();
961 lyxerr << "can't erase this selection 1" << endl;
963 //lyxerr << "cap::eraseSelection end: " << cur << endl;
967 void selDel(Cursor & cur)
969 //lyxerr << "cap::selDel" << endl;
975 void selClearOrDel(Cursor & cur)
977 //lyxerr << "cap::selClearOrDel" << endl;
978 if (lyxrc.auto_region_delete)
981 cur.selection() = false;
985 docstring grabSelection(Cursor const & cur)
987 if (!cur.selection())
991 // grab selection by glueing multiple cells together. This is not what
992 // we want because selections spanning multiple cells will get "&" and "\\"
995 for (DocIterator dit = cur.selectionBegin();
996 dit != cur.selectionEnd(); dit.forwardPos())
997 os << asString(dit.cell());
1001 CursorSlice i1 = cur.selBegin();
1002 CursorSlice i2 = cur.selEnd();
1004 if (i1.idx() == i2.idx()) {
1005 if (i1.inset().asInsetMath()) {
1006 MathData::const_iterator it = i1.cell().begin();
1007 return asString(MathData(it + i1.pos(), it + i2.pos()));
1009 return from_ascii("unknown selection 1");
1013 Inset::row_type r1, r2;
1014 Inset::col_type c1, c2;
1015 region(i1, i2, r1, r2, c1, c2);
1018 if (i1.inset().asInsetMath()) {
1019 for (Inset::row_type row = r1; row <= r2; ++row) {
1022 for (Inset::col_type col = c1; col <= c2; ++col) {
1025 data += asString(i1.asInsetMath()->
1026 cell(i1.asInsetMath()->index(row, col)));
1030 data = from_ascii("unknown selection 2");
1036 void dirtyTabularStack(bool b)
1038 dirty_tabular_stack_ = b;
1042 bool tabularStackDirty()
1044 return dirty_tabular_stack_;