X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FCutAndPaste.cpp;h=a521e1434ef32fa5506f457eb326b23756018cc5;hb=44cdffa39e9160bde46d824f1915f9ef3084b53e;hp=7329c248918f95b0e60b446626c4e0a6dcd38a52;hpb=f58d9f27a239db757efbab54524bd5a6bb6a1532;p=lyx.git diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 7329c24891..a521e1434e 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -60,6 +60,7 @@ #include "support/lassert.h" #include "support/limited_stack.h" #include "support/lstrings.h" +#include "support/lyxalgo.h" #include "support/TempFile.h" #include "frontends/alert.h" @@ -67,7 +68,6 @@ #include "frontends/Selection.h" #include -#include #include @@ -125,7 +125,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, InsetText * target_inset = cur.inset().asInsetText(); if (!target_inset) { InsetTabular * it = cur.inset().asInsetTabular(); - target_inset = it? it->cell(cur.idx())->asInsetText() : 0; + target_inset = it ? it->cell(cur.idx())->asInsetText() : 0; } LASSERT(target_inset, return PasteReturnValue(pit, pos, need_update)); @@ -170,7 +170,8 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, if (forcePlainLayout || parLayout == defaultLayout) par->setLayout(plainLayout); } - } else { // check if we need to reset from plain layout + } else { + // check if we need to reset from plain layout Layout const & defaultLayout = newDocClass->defaultLayout(); Layout const & plainLayout = newDocClass->plainLayout(); ParagraphList::iterator const end = insertion.end(); @@ -386,7 +387,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, // Paste it! if (empty) { - pars.insert(boost::next(pars.begin(), pit), + pars.insert(next(pars.begin(), pit), insertion.begin(), insertion.end()); @@ -394,7 +395,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist, mergeParagraph(buffer.params(), pars, pit + insertion.size() - 1); } else { - pars.insert(boost::next(pars.begin(), pit + 1), + pars.insert(next(pars.begin(), pit + 1), insertion.begin(), insertion.end()); @@ -479,8 +480,7 @@ PitPosPair eraseSelectionHelper(BufferParams const & params, } -void putClipboard(ParagraphList const & paragraphs, - DocumentClassConstPtr docclass, docstring const & plaintext) +Buffer * copyToTempBuffer(ParagraphList const & paragraphs, DocumentClassConstPtr docclass) { // This used to need to be static to avoid a memory leak. It no longer needs // to be so, but the alternative is to construct a new one of these (with a @@ -499,9 +499,20 @@ void putClipboard(ParagraphList const & paragraphs, // Use a clone for the complicated stuff so that we do not need to clean // up in order to avoid a crash. Buffer * buffer = staticbuffer->cloneBufferOnly(); - LASSERT(buffer, return); + LASSERT(buffer, return 0); // This needs doing every time. + // Since setDocumentClass() causes deletion of the old document class + // we need to reset all layout pointers in paragraphs (otherwise they + // would be dangling). + ParIterator const end = buffer->par_iterator_end(); + for (ParIterator it = buffer->par_iterator_begin(); it != end; ++it) { + docstring const name = it->layout().name(); + if (docclass->hasLayout(name)) + it->setLayout((*docclass)[name]); + else + it->setPlainOrDefaultLayout(*docclass); + } buffer->params().setDocumentClass(docclass); // we will use pasteSelectionHelper to copy the paragraphs into the @@ -510,6 +521,17 @@ void putClipboard(ParagraphList const & paragraphs, ErrorList el; pasteSelectionHelper(dit, paragraphs, docclass, buffer, el); + return buffer; +} + + +void putClipboard(ParagraphList const & paragraphs, + DocumentClassConstPtr docclass, docstring const & plaintext) +{ + Buffer * buffer = copyToTempBuffer(paragraphs, docclass); + if (!buffer) // already asserted in copyToTempBuffer() + return; + // We don't want to produce images that are not used. Therefore, // output formulas as MathML. Even if this is not understood by all // applications, the number that can parse it should go up in the future. @@ -577,8 +599,8 @@ void copySelectionHelper(Buffer const & buf, Text const & text, LASSERT(startpit != endpit || start <= end, return); // Clone the paragraphs within the selection. - ParagraphList copy_pars(boost::next(pars.begin(), startpit), - boost::next(pars.begin(), endpit + 1)); + ParagraphList copy_pars(next(pars.begin(), startpit), + next(pars.begin(), endpit + 1)); // Remove the end of the last paragraph; afterwards, remove the // beginning of the first paragraph. Keep this order - there may only @@ -789,22 +811,20 @@ vector availableSelections(Buffer const * buf) // we do not use cit-> here because gcc 2.9x does not // like it (JMarc) ParagraphList const & pars = (*cit).first; - docstring asciiSel; + docstring textSel; ParagraphList::const_iterator pit = pars.begin(); ParagraphList::const_iterator pend = pars.end(); for (; pit != pend; ++pit) { - Paragraph par(*pit, 0, 26); + Paragraph par(*pit, 0, 46); // adapt paragraph to current buffer. par.setBuffer(const_cast(*buf)); - asciiSel += par.asString(AS_STR_INSETS); - if (asciiSel.size() > 25) { - asciiSel.replace(22, docstring::npos, - from_ascii("...")); + textSel += par.asString(AS_STR_INSETS); + if (textSel.size() > 45) { + support::truncateWithEllipsis(textSel,45); break; } } - - selList.push_back(asciiSel); + selList.push_back(textSel); } return selList; @@ -1039,11 +1059,16 @@ void clearCutStack() } -docstring selection(size_t sel_index) +docstring selection(size_t sel_index, DocumentClassConstPtr docclass) { - return sel_index < theCuts.size() - ? theCuts[sel_index].first.back().asString(AS_STR_INSETS | AS_STR_NEWLINES) - : docstring(); + if (sel_index >= theCuts.size()) + return docstring(); + + boost::scoped_ptr buffer(copyToTempBuffer(theCuts[sel_index].first, docclass)); + if (!buffer) + return docstring(); + + return buffer->paragraphs().back().asString(AS_STR_INSETS | AS_STR_NEWLINES); }