]> git.lyx.org Git - lyx.git/blobdiff - src/CutAndPaste.cpp
We don't need to crash here, but can try to continue in release
[lyx.git] / src / CutAndPaste.cpp
index a521e1434ef32fa5506f457eb326b23756018cc5..4ec9811d90bb8fd086e2e30be20ecb958bf1b620 100644 (file)
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/TempFile.h"
+#include "support/unique_ptr.h"
 
 #include "frontends/alert.h"
 #include "frontends/Clipboard.h"
 #include "frontends/Selection.h"
 
-#include <boost/tuple/tuple.hpp>
-
 #include <string>
+#include <tuple>
 
 using namespace std;
 using namespace lyx::support;
@@ -100,11 +100,11 @@ bool checkPastePossible(int index)
 
 
 struct PasteReturnValue {
-       PasteReturnValue(pit_type r_par, pos_type r_pos, bool r_nu) :
-         par(r_par), pos(r_pos), needupdate(r_nu)
+       PasteReturnValue(pit_type r_pit, pos_type r_pos, bool r_nu) :
+         pit(r_pit), pos(r_pos), needupdate(r_nu)
        {}
 
-       pit_type par;
+       pit_type pit;
        pos_type pos;
        bool needupdate;
 };
@@ -160,7 +160,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
        // set the paragraphs to plain layout if necessary
        DocumentClassConstPtr newDocClass = buffer.params().documentClassPtr();
        if (cur.inset().usePlainLayout()) {
-               bool forcePlainLayout = cur.inset().forcePlainLayout();
+               bool forcePlainLayout = target_inset->forcePlainLayout();
                Layout const & plainLayout = newDocClass->plainLayout();
                Layout const & defaultLayout = newDocClass->defaultLayout();
                ParagraphList::iterator const end = insertion.end();
@@ -387,7 +387,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
 
        // Paste it!
        if (empty) {
-               pars.insert(next(pars.begin(), pit),
+               pars.insert(lyx::next(pars.begin(), pit),
                            insertion.begin(),
                            insertion.end());
 
@@ -395,7 +395,7 @@ pasteSelectionHelper(DocIterator const & cur, ParagraphList const & parlist,
                mergeParagraph(buffer.params(), pars,
                               pit + insertion.size() - 1);
        } else {
-               pars.insert(next(pars.begin(), pit + 1),
+               pars.insert(lyx::next(pars.begin(), pit + 1),
                            insertion.begin(),
                            insertion.end());
 
@@ -486,16 +486,18 @@ Buffer * copyToTempBuffer(ParagraphList const & paragraphs, DocumentClassConstPt
        // to be so, but the alternative is to construct a new one of these (with a
        // new temporary directory, etc) every time, and then to destroy it. So maybe
        // it's worth just keeping this one around.
-       // FIXME THREAD
        static TempFile tempfile("clipboard.internal");
        tempfile.setAutoRemove(false);
-       static Buffer * staticbuffer = theBufferList().newInternalBuffer(
-                       tempfile.name().absFileName());
-
-       // These two things only really need doing the first time.
-       staticbuffer->setUnnamed(true);
-       staticbuffer->inset().setBuffer(*staticbuffer);
-
+       // The initialization of staticbuffer is thread-safe. Using a lambda
+       // guarantees that the properties are set only once.
+       static Buffer * staticbuffer = [&](){
+               Buffer * b =
+                       theBufferList().newInternalBuffer(tempfile.name().absFileName());
+               b->setUnnamed(true);
+               b->inset().setBuffer(*b);
+               //initialize staticbuffer with b
+               return b;
+       }();
        // 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();
@@ -599,8 +601,8 @@ void copySelectionHelper(Buffer const & buf, Text const & text,
        LASSERT(startpit != endpit || start <= end, return);
 
        // Clone the paragraphs within the selection.
-       ParagraphList copy_pars(next(pars.begin(), startpit),
-                               next(pars.begin(), endpit + 1));
+       ParagraphList copy_pars(lyx::next(pars.begin(), startpit),
+                               lyx::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
@@ -875,11 +877,9 @@ void cutSelection(Cursor & cur, bool doclear, bool realcut)
                if (begpit != endpit)
                        cur.screenUpdateFlags(Update::Force | Update::FitCursor);
 
-               boost::tie(endpit, endpos) =
-                       eraseSelectionHelper(bp,
-                               text->paragraphs(),
-                               begpit, endpit,
-                               cur.selBegin().pos(), endpos);
+               tie(endpit, endpos) =
+                       eraseSelectionHelper(bp, text->paragraphs(), begpit, endpit,
+                                            cur.selBegin().pos(), endpos);
 
                // cutSelection can invalidate the cursor so we need to set
                // it anew. (Lgb)
@@ -961,20 +961,11 @@ void copySelectionToStack(Cursor const & cur, CutStack & cutstack)
                LBUFERR(text);
                // ok we have a selection. This is always between cur.selBegin()
                // and sel_end cursor
-
-               // copy behind a space if there is one
-               ParagraphList & pars = text->paragraphs();
-               pos_type pos = cur.selBegin().pos();
-               pit_type par = cur.selBegin().pit();
-               while (pos < pars[par].size() &&
-                      pars[par].isLineSeparator(pos) &&
-                      (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
-                       ++pos;
-
-               copySelectionHelper(*cur.buffer(), *text, par, cur.selEnd().pit(),
-                       pos, cur.selEnd().pos(), 
-                       cur.buffer()->params().documentClassPtr(), cutstack);
-
+               copySelectionHelper(*cur.buffer(), *text,
+                                   cur.selBegin().pit(), cur.selEnd().pit(),
+                                   cur.selBegin().pos(), cur.selEnd().pos(), 
+                                   cur.buffer()->params().documentClassPtr(),
+                                   cutstack);
                // Reset the dirty_tabular_stack_ flag only when something
                // is copied to the clipboard (not to the selectionBuffer).
                if (&cutstack == &theCuts)
@@ -1064,7 +1055,8 @@ docstring selection(size_t sel_index, DocumentClassConstPtr docclass)
        if (sel_index >= theCuts.size())
                return docstring();
 
-       boost::scoped_ptr<Buffer> buffer(copyToTempBuffer(theCuts[sel_index].first, docclass));
+       unique_ptr<Buffer> buffer(copyToTempBuffer(theCuts[sel_index].first,
+                                                  docclass));
        if (!buffer)
                return docstring();
 
@@ -1083,7 +1075,7 @@ void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
                        pasteSelectionHelper(cur, parlist, docclass, 0, errorList);
                cur.forceBufferUpdate();
                cur.clearSelection();
-               text->setCursor(cur, prv.par, prv.pos);
+               text->setCursor(cur, prv.pit, prv.pos);
        }
 
        // mathed is handled in InsetMathNest/InsetMathGrid
@@ -1178,6 +1170,7 @@ bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
                cur.text()->insertStringAsParagraphs(cur, text, cur.current_font);
        else
                cur.text()->insertStringAsLines(cur, text, cur.current_font);
+       cur.forceBufferUpdate();
        return true;
 }
 
@@ -1334,7 +1327,7 @@ void selClearOrDel(Cursor & cur)
        if (lyxrc.auto_region_delete)
                selDel(cur);
        else
-               cur.setSelection(false);
+               cur.selection(false);
 }