]> git.lyx.org Git - lyx.git/blobdiff - src/CutAndPaste.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / CutAndPaste.cpp
index 150fd8a6464879f517c69dcb2339b9f52b37bbe6..abed1567d7fa289520784322cba948e8f0367b88 100644 (file)
@@ -109,6 +109,21 @@ struct PasteReturnValue {
        bool needupdate;
 };
 
+
+/// return true if the whole ParagraphList is deleted
+static bool isFullyDeleted(ParagraphList const & pars)
+{
+       pit_type const pars_size = static_cast<pit_type>(pars.size());
+
+       // check all paragraphs
+       for (pit_type pit = 0; pit < pars_size; ++pit) {
+               if (!pars[pit].empty())   // prevent assertion failure
+                       if (!pars[pit].isDeleted(0, pars[pit].size()))
+                               return false;
+       }
+       return true;
+}
+
 PasteReturnValue
 pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
                      DocumentClassConstPtr oldDocClass, cap::BranchAction branchAction,
@@ -277,24 +292,27 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
                                if (!target_inset->insetAllowed(inset->lyxCode()))
                                        tmpbuf->eraseChar(i--, false);
                }
-
-               if (lyxrc.ct_markup_copied) {
-                       // Only change to inserted if ct is active,
-                       // otherwise leave markup as is
-                       if (buffer.params().track_changes)
-                               tmpbuf->setChange(Change(Change::INSERTED));
-               } else
-                       // Resolve all markup to inserted or unchanged
-                       tmpbuf->setChange(Change(buffer.params().track_changes ?
-                                                Change::INSERTED : Change::UNCHANGED));
        }
 
-       bool const empty = pars[pit].empty();
-       if (!empty) {
-               // Make the buf exactly the same layout as the cursor
-               // paragraph.
+       bool const target_empty = pars[pit].empty();
+       // Use the paste content's layout, if...
+       bool const paste_layout =
+               // if target paragraph is empty
+               (target_empty
+                // ... and its layout is default
+                && (pars[pit].layout() == defaultLayout
+                    // ... or plain
+                    || pars[pit].layout() == plainLayout
+                    // ... or the paste content spans several paragraphs
+                    || insertion.size() > 1))
+               // or if pasting is done at the beginning of paragraph
+                || (pos == 0
+                    // and the paste content spans several paragraphs
+                    && insertion.size() > 1);
+       if (!paste_layout)
+               // Give the first paragraph to insert the same layout as the
+               // target paragraph.
                insertion.begin()->makeSameLayout(pars[pit]);
-       }
 
        // Prepare the paragraphs and insets for insertion.
        insertion.swap(in.paragraphs());
@@ -457,12 +475,43 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
        }
        insertion.swap(in.paragraphs());
 
+       // We need to do this here, after the inset handling above,
+       // as acceptChanges() and rejectChanges() might access inset buffers.
+       tmpbuf = insertion.begin();
+       for (; tmpbuf != insertion.end(); ++tmpbuf) {
+               if (lyxrc.ct_markup_copied) {
+                       // Only remove deleted text and change
+                       // the rest to inserted if ct is active,
+                       // otherwise leave markup as is
+                       if (buffer.params().track_changes) {
+                               if (tmpbuf->size() > 0) {
+                                       if (!isFullyDeleted(insertion))
+                                               tmpbuf->acceptChanges(0, tmpbuf->size());
+                                       else
+                                               tmpbuf->rejectChanges(0, tmpbuf->size());
+                               }
+                               tmpbuf->setChange(Change(Change::INSERTED));
+                       }
+               } else
+                       // Resolve all markup to inserted or unchanged
+                       // Deleted text has already been removed on copy
+                       // (copySelectionHelper)
+                       tmpbuf->setChange(Change(buffer.params().track_changes ?
+                                                Change::INSERTED : Change::UNCHANGED));
+       }
+
        // Split the paragraph for inserting the buf if necessary.
-       if (!empty)
+       if (!target_empty)
                breakParagraphConservative(buffer.params(), pars, pit, pos);
 
+       // If multiple paragraphs are inserted before the target paragraph,
+       // the cursor is now in a new paragraph before it,
+       // so use layout from paste content in that case.
+       if (pos == 0 && insertion.size() > 1)
+               pars[pit].makeSameLayout(* insertion.begin());
+
        // Paste it!
-       if (empty) {
+       if (target_empty) {
                pars.insert(pars.iterator_at(pit),
                            insertion.begin(), insertion.end());
 
@@ -491,7 +540,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
 
        // Join (conditionally) last pasted paragraph with next one, i.e.,
        // the tail of the spliced document paragraph
-       if (!empty && last_paste + 1 != pit_type(pars.size())) {
+       if (!target_empty && last_paste + 1 != pit_type(pars.size())) {
                if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
                        mergeParagraph(buffer.params(), pars, last_paste);
                } else if (pars[last_paste + 1].empty()) {
@@ -535,10 +584,7 @@ PitPosPair eraseSelectionHelper(BufferParams const & params,
                pars[pit].eraseChars(left, right, params.track_changes);
 
                // Separate handling of paragraph break:
-               if (merge && pit != endpit &&
-                   (pit + 1 != endpit
-                    || pars[pit].hasSameLayout(pars[endpit])
-                    || pars[endpit].size() == endpos)) {
+               if (merge && pit != endpit) {
                        if (pit + 1 == endpit)
                                endpos += pars[pit].size();
                        mergeParagraph(params, pars, pit);
@@ -651,21 +697,6 @@ void putClipboard(ParagraphList const & paragraphs,
 }
 
 
-/// return true if the whole ParagraphList is deleted
-static bool isFullyDeleted(ParagraphList const & pars)
-{
-       pit_type const pars_size = static_cast<pit_type>(pars.size());
-
-       // check all paragraphs
-       for (pit_type pit = 0; pit < pars_size; ++pit) {
-               if (!pars[pit].empty())   // prevent assertion failure
-                       if (!pars[pit].isDeleted(0, pars[pit].size()))
-                               return false;
-       }
-       return true;
-}
-
-
 void copySelectionHelper(Buffer const & buf, Text const & text,
        pit_type startpit, pit_type endpit,
        int start, int end, DocumentClassConstPtr dc, CutStack & cutstack)
@@ -797,6 +828,13 @@ bool multipleCellsSelected(CursorData const & cur)
 }
 
 
+void switchBetweenClasses(DocumentClassConstPtr oldone,
+               DocumentClassConstPtr newone, InsetText & in) {
+       ErrorList el = {};
+       switchBetweenClasses(oldone, newone, in, el);
+}
+
+
 void switchBetweenClasses(DocumentClassConstPtr oldone,
                DocumentClassConstPtr newone, InsetText & in, ErrorList & errorlist)
 {
@@ -846,7 +884,10 @@ void switchBetweenClasses(DocumentClassConstPtr oldone,
                        docstring const & n = newone->insetLayout(layoutName).name();
                        bool const is_undefined = n.empty() ||
                                n == DocumentClass::plainInsetLayout().name();
-                       if (!is_undefined)
+                       docstring const & oldn = oldone->insetLayout(layoutName).name();
+                       bool const was_undefined = oldn.empty() ||
+                               oldn == DocumentClass::plainInsetLayout().name();
+                       if (!is_undefined || was_undefined)
                                continue;
 
                        // The flex inset is undefined in newtc
@@ -948,6 +989,8 @@ void cutSelectionHelper(Cursor & cur, CutStack & cuts, bool realcut, bool putcli
 
                if (begpit != endpit)
                        cur.screenUpdateFlags(Update::Force | Update::FitCursor);
+               else
+                       cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
 
                tie(endpit, endpos) =
                        eraseSelectionHelper(bp, text->paragraphs(), begpit, endpit,
@@ -1008,7 +1051,9 @@ void copySelection(Cursor const & cur)
 }
 
 
-void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext)
+namespace {
+
+void copyInsetToStack(Cursor const & cur, CutStack & cutstack, Inset * inset)
 {
        ParagraphList pars;
        Paragraph par;
@@ -1017,13 +1062,28 @@ void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext)
        Font font(inherit_font, bp.language);
        par.insertInset(0, inset, font, Change(Change::UNCHANGED));
        pars.push_back(par);
-       theCuts.push(make_pair(pars, make_pair(bp.documentClassPtr(), bp.authors())));
+       cutstack.push(make_pair(pars, make_pair(bp.documentClassPtr(), bp.authors())));
+}
+
+}
+
+
+void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext)
+{
+       copyInsetToStack(cur, theCuts, inset);
 
        // stuff the selection onto the X clipboard, from an explicit copy request
+       BufferParams const & bp = cur.buffer()->params();
        putClipboard(theCuts[0].first, theCuts[0].second, plaintext, bp);
 }
 
 
+void copyInsetToTemp(Cursor const & cur, Inset * inset)
+{
+       copyInsetToStack(cur, tempCut, inset);
+}
+
+
 namespace {
 
 void copySelectionToStack(CursorData const & cur, CutStack & cutstack)
@@ -1253,6 +1313,7 @@ bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
                string lyx = theClipboard().getAsLyX();
                if (!lyx.empty()) {
                        Buffer buffer(string(), false);
+                       buffer.setInternal(true);
                        buffer.setUnnamed(true);
                        if (buffer.readString(lyx)) {
                                cur.recordUndo();
@@ -1286,7 +1347,9 @@ bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
                        available = !text.empty();
                        if (available) {
                                Buffer buffer(string(), false);
+                               buffer.setInternal(true);
                                buffer.setUnnamed(true);
+                               buffer.params() = cur.buffer()->params();
                                available = buffer.importString(names[i], text, errorList);
                                if (available)
                                        available = !buffer.paragraphs().empty();