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 plain layout if necessary
134 if (cur.inset().usePlainLayout()) {
135 bool forcePlainLayout = cur.inset().forcePlainLayout();
136 Layout const & plainLayout = newDocClass->plainLayout();
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(plainLayout);
145 } else { // check if we need to reset from plain layout
146 Layout const & defaultLayout = newDocClass->defaultLayout();
147 Layout const & plainLayout = newDocClass->plainLayout();
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 == plainLayout)
153 par->setLayout(defaultLayout);
157 InsetText in(buffer);
158 // Make sure there is no class difference.
159 in.paragraphs().clear();
160 // This works without copying any paragraph data because we have
161 // a specialized swap method for ParagraphList. This is important
162 // since we store pointers to insets at some places and we don't
163 // want to invalidate them.
164 insertion.swap(in.paragraphs());
165 cap::switchBetweenClasses(oldDocClass, newDocClass, in, errorlist);
166 insertion.swap(in.paragraphs());
168 ParagraphList::iterator tmpbuf = insertion.begin();
169 int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
171 depth_type max_depth = pars[pit].getMaxDepthAfter();
173 for (; tmpbuf != insertion.end(); ++tmpbuf) {
174 // If we have a negative jump so that the depth would
175 // go below 0 depth then we have to redo the delta to
176 // this new max depth level so that subsequent
177 // paragraphs are aligned correctly to this paragraph
179 if (int(tmpbuf->params().depth()) + depth_delta < 0)
182 // Set the right depth so that we are not too deep or shallow.
183 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
184 if (tmpbuf->params().depth() > max_depth)
185 tmpbuf->params().depth(max_depth);
187 // Only set this from the 2nd on as the 2nd depends
188 // for maxDepth still on pit.
189 if (tmpbuf != insertion.begin())
190 max_depth = tmpbuf->getMaxDepthAfter();
192 // Set the inset owner of this paragraph.
193 tmpbuf->setInsetOwner(pars[pit].inInset());
194 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
195 // do not track deletion of invalid insets
196 if (Inset * inset = tmpbuf->getInset(i))
197 if (!pars[pit].insetAllowed(inset->lyxCode()))
198 tmpbuf->eraseChar(i--, false);
201 tmpbuf->setChange(Change(buffer.params().trackChanges ?
202 Change::INSERTED : Change::UNCHANGED));
205 bool const empty = pars[pit].empty();
207 // Make the buf exactly the same layout as the cursor
209 insertion.begin()->makeSameLayout(pars[pit]);
212 // Prepare the paragraphs and insets for insertion.
213 // A couple of insets store buffer references so need updating.
214 insertion.swap(in.paragraphs());
216 InsetIterator const i_end = inset_iterator_end(in);
218 for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
220 it->setBuffer(const_cast<Buffer &>(buffer));
222 switch (it->lyxCode()) {
225 // check for duplicates
226 InsetCommand & lab = static_cast<InsetCommand &>(*it);
227 docstring const oldname = lab.getParam("name");
228 lab.updateCommand(oldname, false);
229 docstring const newname = lab.getParam("name");
230 if (oldname != newname) {
231 // adapt the references
232 for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
233 if (itt->lyxCode() == REF_CODE) {
234 InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
235 if (ref.getParam("reference") == oldname)
236 ref.setParam("reference", newname);
244 // check for duplicates
245 InsetCommand & bib = static_cast<InsetCommand &>(*it);
246 docstring const oldkey = bib.getParam("key");
247 bib.updateCommand(oldkey, false);
248 docstring const newkey = bib.getParam("key");
249 if (oldkey != newkey) {
250 // adapt the references
251 for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
252 if (itt->lyxCode() == CITE_CODE) {
253 InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
254 if (ref.getParam("key") == oldkey)
255 ref.setParam("key", newkey);
266 insertion.swap(in.paragraphs());
268 // Split the paragraph for inserting the buf if necessary.
270 breakParagraphConservative(buffer.params(), pars, pit, pos);
274 pars.insert(boost::next(pars.begin(), pit),
278 // merge the empty par with the last par of the insertion
279 mergeParagraph(buffer.params(), pars,
280 pit + insertion.size() - 1);
282 pars.insert(boost::next(pars.begin(), pit + 1),
286 // merge the first par of the insertion with the current par
287 mergeParagraph(buffer.params(), pars, pit);
289 //FIXME: We should call setBuffer() on each inserted paragraph.
290 // instead, we call setBuffer() for the main inset at the beginning
293 pit_type last_paste = pit + insertion.size() - 1;
295 // Store the new cursor position.
297 pos = pars[last_paste].size();
299 // Join (conditionally) last pasted paragraph with next one, i.e.,
300 // the tail of the spliced document paragraph
301 if (!empty && last_paste + 1 != pit_type(pars.size())) {
302 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
303 mergeParagraph(buffer.params(), pars, last_paste);
304 } else if (pars[last_paste + 1].empty()) {
305 pars[last_paste + 1].makeSameLayout(pars[last_paste]);
306 mergeParagraph(buffer.params(), pars, last_paste);
307 } else if (pars[last_paste].empty()) {
308 pars[last_paste].makeSameLayout(pars[last_paste + 1]);
309 mergeParagraph(buffer.params(), pars, last_paste);
311 pars[last_paste + 1].stripLeadingSpaces(buffer.params().trackChanges);
316 return make_pair(PitPosPair(pit, pos), last_paste + 1);
320 PitPosPair eraseSelectionHelper(BufferParams const & params,
321 ParagraphList & pars,
322 pit_type startpit, pit_type endpit,
323 int startpos, int endpos)
325 // Start of selection is really invalid.
326 if (startpit == pit_type(pars.size()) ||
327 (startpos > pars[startpit].size()))
328 return PitPosPair(endpit, endpos);
330 // Start and end is inside same paragraph
331 if (endpit == pit_type(pars.size()) || startpit == endpit) {
332 endpos -= pars[startpit].eraseChars(startpos, endpos, params.trackChanges);
333 return PitPosPair(endpit, endpos);
336 for (pit_type pit = startpit; pit != endpit + 1;) {
337 pos_type const left = (pit == startpit ? startpos : 0);
338 pos_type right = (pit == endpit ? endpos : pars[pit].size() + 1);
339 bool const merge = pars[pit].isMergedOnEndOfParDeletion(params.trackChanges);
341 // Logically erase only, including the end-of-paragraph character
342 pars[pit].eraseChars(left, right, params.trackChanges);
344 // Separate handling of paragraph break:
345 if (merge && pit != endpit &&
347 || pars[pit].hasSameLayout(pars[endpit])
348 || pars[endpit].size() == endpos)) {
349 if (pit + 1 == endpit)
350 endpos += pars[pit].size();
351 mergeParagraph(params, pars, pit);
357 // Ensure legal cursor pos:
360 return PitPosPair(endpit, endpos);
364 void putClipboard(ParagraphList const & paragraphs,
365 DocumentClass const * const docclass, docstring const & plaintext)
367 // For some strange reason gcc 3.2 and 3.3 do not accept
368 // Buffer buffer(string(), false);
369 // This needs to be static to avoid a memory leak. When a Buffer is
370 // constructed, it constructs a BufferParams, which in turn constructs
371 // a DocumentClass, via new, that is never deleted. If we were to go to
372 // some kind of garbage collection there, or a shared_ptr, then this
373 // would not be needed.
374 static Buffer * buffer = theBufferList().newBuffer(
375 FileName::tempName().absFilename() + "_clipboard.internal");
376 buffer->setUnnamed(true);
377 buffer->paragraphs() = paragraphs;
378 buffer->inset().setBuffer(*buffer);
379 buffer->params().setDocumentClass(docclass);
381 if (buffer->write(lyx))
382 theClipboard().put(lyx.str(), plaintext);
384 theClipboard().put(string(), plaintext);
386 buffer->paragraphs().clear();
390 void copySelectionHelper(Buffer const & buf, ParagraphList & pars,
391 pit_type startpit, pit_type endpit,
392 int start, int end, DocumentClass const * const dc, CutStack & cutstack)
394 LASSERT(0 <= start && start <= pars[startpit].size(), /**/);
395 LASSERT(0 <= end && end <= pars[endpit].size(), /**/);
396 LASSERT(startpit != endpit || start <= end, /**/);
398 // Clone the paragraphs within the selection.
399 ParagraphList copy_pars(boost::next(pars.begin(), startpit),
400 boost::next(pars.begin(), endpit + 1));
402 // Remove the end of the last paragraph; afterwards, remove the
403 // beginning of the first paragraph. Keep this order - there may only
404 // be one paragraph! Do not track deletions here; this is an internal
405 // action not visible to the user
407 Paragraph & back = copy_pars.back();
408 back.eraseChars(end, back.size(), false);
409 Paragraph & front = copy_pars.front();
410 front.eraseChars(0, start, false);
412 ParagraphList::iterator it = copy_pars.begin();
413 ParagraphList::iterator it_end = copy_pars.end();
415 for (; it != it_end; it++) {
416 // ERT paragraphs have the Language latex_language.
417 // This is invalid outside of ERT, so we need to change it
418 // to the buffer language.
419 if (it->ownerCode() == ERT_CODE || it->ownerCode() == LISTINGS_CODE) {
420 it->changeLanguage(buf.params(), latex_language, buf.language());
422 it->setInsetOwner(0);
425 // do not copy text (also nested in insets) which is marked as deleted
426 acceptChanges(copy_pars, buf.params());
428 DocumentClass * d = const_cast<DocumentClass *>(dc);
429 cutstack.push(make_pair(copy_pars, d));
439 void region(CursorSlice const & i1, CursorSlice const & i2,
440 Inset::row_type & r1, Inset::row_type & r2,
441 Inset::col_type & c1, Inset::col_type & c2)
443 Inset & p = i1.inset();
444 c1 = p.col(i1.idx());
445 c2 = p.col(i2.idx());
448 r1 = p.row(i1.idx());
449 r2 = p.row(i2.idx());
455 docstring grabAndEraseSelection(Cursor & cur)
457 if (!cur.selection())
459 docstring res = grabSelection(cur);
465 bool reduceSelectionToOneCell(Cursor & cur)
467 if (!cur.selection() || !cur.inMathed())
470 CursorSlice i1 = cur.selBegin();
471 CursorSlice i2 = cur.selEnd();
472 if (!i1.inset().asInsetMath())
475 // the easy case: do nothing if only one cell is selected
476 if (i1.idx() == i2.idx())
481 cur.top().pos() = cur.top().lastpos();
487 bool multipleCellsSelected(Cursor const & cur)
489 if (!cur.selection() || !cur.inMathed())
492 CursorSlice i1 = cur.selBegin();
493 CursorSlice i2 = cur.selEnd();
494 if (!i1.inset().asInsetMath())
497 if (i1.idx() == i2.idx())
504 void switchBetweenClasses(DocumentClass const * const oldone,
505 DocumentClass const * const newone, InsetText & in, ErrorList & errorlist)
509 LASSERT(!in.paragraphs().empty(), /**/);
510 if (oldone == newone)
513 DocumentClass const & oldtc = *oldone;
514 DocumentClass const & newtc = *newone;
517 ParIterator end = par_iterator_end(in);
518 for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
519 docstring const name = it->layout().name();
521 // the pasted text will keep their own layout name. If this layout does
522 // not exist in the new document, it will behave like a standard layout.
523 newtc.addLayoutIfNeeded(name);
525 if (in.usePlainLayout())
526 it->setLayout(newtc.plainLayout());
528 it->setLayout(newtc[name]);
532 InsetIterator const i_end = inset_iterator_end(in);
533 for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
534 InsetCollapsable * inset = it->asInsetCollapsable();
537 if (inset->lyxCode() != FLEX_CODE)
538 // FIXME: Should we verify all InsetCollapsable?
540 inset->setLayout(newone);
541 if (!inset->undefined())
543 // The flex inset is undefined in newtc
544 docstring const s = bformat(_(
545 "Flex inset %1$s is "
546 "undefined because of class "
547 "conversion from\n%2$s to %3$s"),
548 inset->name(), from_utf8(oldtc.name()),
549 from_utf8(newtc.name()));
550 // To warn the user that something had to be done.
551 errorlist.push_back(ErrorItem(
552 _("Undefined flex inset"),
553 s, it.paragraph().id(), it.pos(), it.pos() + 1));
558 vector<docstring> availableSelections(Buffer const * buf)
560 vector<docstring> selList;
564 CutStack::const_iterator cit = theCuts.begin();
565 CutStack::const_iterator end = theCuts.end();
566 for (; cit != end; ++cit) {
567 // we do not use cit-> here because gcc 2.9x does not
569 ParagraphList const & pars = (*cit).first;
571 ParagraphList::const_iterator pit = pars.begin();
572 ParagraphList::const_iterator pend = pars.end();
573 for (; pit != pend; ++pit) {
574 Paragraph par(*pit, 0, 26);
575 // adapt paragraph to current buffer.
576 par.setBuffer(const_cast<Buffer &>(*buf));
577 asciiSel += par.asString(AS_STR_INSETS);
578 if (asciiSel.size() > 25) {
579 asciiSel.replace(22, docstring::npos,
585 selList.push_back(asciiSel);
592 size_type numberOfSelections()
594 return theCuts.size();
598 void cutSelection(Cursor & cur, bool doclear, bool realcut)
600 // This doesn't make sense, if there is no selection
601 if (!cur.selection())
604 // OK, we have a selection. This is always between cur.selBegin()
607 if (cur.inTexted()) {
608 Text * text = cur.text();
613 // make sure that the depth behind the selection are restored, too
614 cur.recordUndoSelection();
615 pit_type begpit = cur.selBegin().pit();
616 pit_type endpit = cur.selEnd().pit();
618 int endpos = cur.selEnd().pos();
620 BufferParams const & bp = cur.buffer().params();
622 copySelectionHelper(cur.buffer(),
625 cur.selBegin().pos(), endpos,
626 bp.documentClassPtr(), theCuts);
627 // Stuff what we got on the clipboard.
628 // Even if there is no selection.
629 putClipboard(theCuts[0].first, theCuts[0].second,
630 cur.selectionAsString(true));
633 if (begpit != endpit)
634 cur.updateFlags(Update::Force | Update::FitCursor);
636 boost::tie(endpit, endpos) =
637 eraseSelectionHelper(bp,
640 cur.selBegin().pos(), endpos);
642 // cutSelection can invalidate the cursor so we need to set
644 // we prefer the end for when tracking changes
648 // sometimes necessary
650 && text->paragraphs()[begpit].stripLeadingSpaces(bp.trackChanges))
653 // need a valid cursor. (Lgb)
654 cur.clearSelection();
655 updateLabels(cur.buffer());
657 // tell tabular that a recent copy happened
658 dirtyTabularStack(false);
661 if (cur.inMathed()) {
662 if (cur.selBegin().idx() != cur.selEnd().idx()) {
663 // The current selection spans more than one cell.
665 cur.recordUndoInset();
667 // Record only the current cell to avoid a jumping
678 void copySelection(Cursor & cur)
680 copySelection(cur, cur.selectionAsString(true));
686 void copySelectionToStack(Cursor & cur, CutStack & cutstack)
688 // this doesn't make sense, if there is no selection
689 if (!cur.selection())
692 // copySelection can not yet handle the case of cross idx selection
693 if (cur.selBegin().idx() != cur.selEnd().idx())
696 if (cur.inTexted()) {
697 Text * text = cur.text();
699 // ok we have a selection. This is always between cur.selBegin()
700 // and sel_end cursor
702 // copy behind a space if there is one
703 ParagraphList & pars = text->paragraphs();
704 pos_type pos = cur.selBegin().pos();
705 pit_type par = cur.selBegin().pit();
706 while (pos < pars[par].size() &&
707 pars[par].isLineSeparator(pos) &&
708 (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
711 copySelectionHelper(cur.buffer(), pars, par, cur.selEnd().pit(),
712 pos, cur.selEnd().pos(),
713 cur.buffer().params().documentClassPtr(), cutstack);
714 dirtyTabularStack(false);
717 if (cur.inMathed()) {
718 //lyxerr << "copySelection in mathed" << endl;
721 BufferParams const & bp = cur.buffer().params();
722 // FIXME This should be the plain layout...right?
723 par.setLayout(bp.documentClass().plainLayout());
724 par.insert(0, grabSelection(cur), Font(), Change(Change::UNCHANGED));
726 cutstack.push(make_pair(pars, bp.documentClassPtr()));
733 void copySelectionToStack()
735 if (!selectionBuffer.empty())
736 theCuts.push(selectionBuffer[0]);
740 void copySelection(Cursor & cur, docstring const & plaintext)
742 // In tablemode, because copy and paste actually use special table stack
743 // we do not attempt to get selected paragraphs under cursor. Instead, a
744 // paragraph with the plain text version is generated so that table cells
745 // can be pasted as pure text somewhere else.
746 if (cur.selBegin().idx() != cur.selEnd().idx()) {
749 BufferParams const & bp = cur.buffer().params();
750 par.setLayout(bp.documentClass().plainLayout());
751 par.insert(0, plaintext, Font(), Change(Change::UNCHANGED));
753 theCuts.push(make_pair(pars, bp.documentClassPtr()));
755 copySelectionToStack(cur, theCuts);
758 // stuff the selection onto the X clipboard, from an explicit copy request
759 putClipboard(theCuts[0].first, theCuts[0].second, plaintext);
763 void saveSelection(Cursor & cur)
765 // This function is called, not when a selection is formed, but when
766 // a selection is cleared. Therefore, multiple keyboard selection
767 // will not repeatively trigger this function (bug 3877).
769 && cur.selBegin() == cur.bv().cursor().selBegin()
770 && cur.selEnd() == cur.bv().cursor().selEnd()) {
771 LYXERR(Debug::SELECTION, "saveSelection: '" << cur.selectionAsString(true) << "'");
772 copySelectionToStack(cur, selectionBuffer);
779 return !selectionBuffer.empty();
783 void clearSelection()
785 selectionBuffer.clear();
795 docstring selection(size_t sel_index)
797 return sel_index < theCuts.size()
798 ? theCuts[sel_index].first.back().asString(AS_STR_INSETS)
803 void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
804 DocumentClass const * const docclass, ErrorList & errorList)
806 if (cur.inTexted()) {
807 Text * text = cur.text();
813 boost::tie(ppp, endpit) =
814 pasteSelectionHelper(cur, parlist, docclass, errorList);
815 updateLabels(cur.buffer());
816 cur.clearSelection();
817 text->setCursor(cur, ppp.first, ppp.second);
820 // mathed is handled in InsetMathNest/InsetMathGrid
821 LASSERT(!cur.inMathed(), /**/);
825 void pasteFromStack(Cursor & cur, ErrorList & errorList, size_t sel_index)
827 // this does not make sense, if there is nothing to paste
828 if (!checkPastePossible(sel_index))
832 pasteParagraphList(cur, theCuts[sel_index].first,
833 theCuts[sel_index].second, errorList);
838 void pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs)
840 // Use internal clipboard if it is the most recent one
841 if (theClipboard().isInternal()) {
842 pasteFromStack(cur, errorList, 0);
846 // First try LyX format
847 if (theClipboard().hasLyXContents()) {
848 string lyx = theClipboard().getAsLyX();
850 // For some strange reason gcc 3.2 and 3.3 do not accept
851 // Buffer buffer(string(), false);
852 Buffer buffer("", false);
853 buffer.setUnnamed(true);
854 if (buffer.readString(lyx)) {
856 pasteParagraphList(cur, buffer.paragraphs(),
857 buffer.params().documentClassPtr(), errorList);
864 // Then try plain text
865 docstring const text = theClipboard().getAsText();
870 cur.text()->insertStringAsParagraphs(cur, text);
872 cur.text()->insertStringAsLines(cur, text);
876 void pasteClipboardGraphics(Cursor & cur, ErrorList & /* errorList */,
877 Clipboard::GraphicsType preferedType)
879 LASSERT(theClipboard().hasGraphicsContents(preferedType), /**/);
881 // get picture from clipboard
882 FileName filename = theClipboard().getAsGraphics(cur, preferedType);
883 if (filename.empty())
886 // create inset for graphic
887 InsetGraphics * inset = new InsetGraphics(cur.buffer());
888 InsetGraphicsParams params;
889 params.filename = support::DocFileName(filename.absFilename());
890 inset->setParams(params);
896 void pasteSelection(Cursor & cur, ErrorList & errorList)
898 if (selectionBuffer.empty())
901 pasteParagraphList(cur, selectionBuffer[0].first,
902 selectionBuffer[0].second, errorList);
906 void replaceSelectionWithString(Cursor & cur, docstring const & str, bool backwards)
909 DocIterator selbeg = cur.selectionBegin();
911 // Get font setting before we cut
913 selbeg.paragraph().getFontSettings(cur.buffer().params(), selbeg.pos());
915 // Insert the new string
916 pos_type pos = cur.selEnd().pos();
917 Paragraph & par = cur.selEnd().paragraph();
918 docstring::const_iterator cit = str.begin();
919 docstring::const_iterator end = str.end();
920 for (; cit != end; ++cit, ++pos)
921 par.insertChar(pos, *cit, font, cur.buffer().params().trackChanges);
924 cutSelection(cur, true, false);
926 // select the replacement
928 selbeg.pos() += str.length();
929 cur.setSelection(selbeg, -int(str.length()));
931 cur.setSelection(selbeg, str.length());
935 void replaceSelection(Cursor & cur)
938 cutSelection(cur, true, false);
942 void eraseSelection(Cursor & cur)
944 //lyxerr << "cap::eraseSelection begin: " << cur << endl;
945 CursorSlice const & i1 = cur.selBegin();
946 CursorSlice const & i2 = cur.selEnd();
947 if (i1.inset().asInsetMath()) {
950 if (i1.idx() == i2.idx()) {
951 i1.cell().erase(i1.pos(), i2.pos());
952 // We may have deleted i1.cell(cur.pos()).
953 // Make sure that pos is valid.
954 if (cur.pos() > cur.lastpos())
955 cur.pos() = cur.lastpos();
957 InsetMath * p = i1.asInsetMath();
958 Inset::row_type r1, r2;
959 Inset::col_type c1, c2;
960 region(i1, i2, r1, r2, c1, c2);
961 for (Inset::row_type row = r1; row <= r2; ++row)
962 for (Inset::col_type col = c1; col <= c2; ++col)
963 p->cell(p->index(row, col)).clear();
964 // We've deleted the whole cell. Only pos 0 is valid.
967 // need a valid cursor. (Lgb)
968 cur.clearSelection();
970 lyxerr << "can't erase this selection 1" << endl;
972 //lyxerr << "cap::eraseSelection end: " << cur << endl;
976 void selDel(Cursor & cur)
978 //lyxerr << "cap::selDel" << endl;
984 void selClearOrDel(Cursor & cur)
986 //lyxerr << "cap::selClearOrDel" << endl;
987 if (lyxrc.auto_region_delete)
990 cur.selection() = false;
994 docstring grabSelection(Cursor const & cur)
996 if (!cur.selection())
1000 // grab selection by glueing multiple cells together. This is not what
1001 // we want because selections spanning multiple cells will get "&" and "\\"
1004 for (DocIterator dit = cur.selectionBegin();
1005 dit != cur.selectionEnd(); dit.forwardPos())
1006 os << asString(dit.cell());
1010 CursorSlice i1 = cur.selBegin();
1011 CursorSlice i2 = cur.selEnd();
1013 if (i1.idx() == i2.idx()) {
1014 if (i1.inset().asInsetMath()) {
1015 MathData::const_iterator it = i1.cell().begin();
1016 return asString(MathData(it + i1.pos(), it + i2.pos()));
1018 return from_ascii("unknown selection 1");
1022 Inset::row_type r1, r2;
1023 Inset::col_type c1, c2;
1024 region(i1, i2, r1, r2, c1, c2);
1027 if (i1.inset().asInsetMath()) {
1028 for (Inset::row_type row = r1; row <= r2; ++row) {
1031 for (Inset::col_type col = c1; col <= c2; ++col) {
1034 data += asString(i1.asInsetMath()->
1035 cell(i1.asInsetMath()->index(row, col)));
1039 data = from_ascii("unknown selection 2");
1045 void dirtyTabularStack(bool b)
1047 dirty_tabular_stack_ = b;
1051 bool tabularStackDirty()
1053 return dirty_tabular_stack_;