Store the AuthorList in the cut stack

Otherwise we crash when we paste text with markup into a different buffer
that has change tracking disabled (in this case, markup is now kept).
This commit is contained in:
Juergen Spitzmueller 2021-02-09 16:54:21 +01:00
parent ce49e2cd8b
commit e35574b3ce
7 changed files with 47 additions and 26 deletions

View File

@ -2943,7 +2943,8 @@ void BufferView::insertLyXFile(FileName const & fname, bool const ignorelang)
buf.changeLanguage(buf.language(), d->cursor_.getFont().language()); buf.changeLanguage(buf.language(), d->cursor_.getFont().language());
buffer_.undo().recordUndo(d->cursor_); buffer_.undo().recordUndo(d->cursor_);
cap::pasteParagraphList(d->cursor_, pars, cap::pasteParagraphList(d->cursor_, pars,
buf.params().documentClassPtr(), el); buf.params().documentClassPtr(),
buf.params().authors(), el);
res = _("Document %1$s inserted."); res = _("Document %1$s inserted.");
} else { } else {
res = _("Could not insert document %1$s"); res = _("Could not insert document %1$s");

View File

@ -77,8 +77,9 @@ namespace lyx {
namespace { namespace {
typedef pair<pit_type, int> PitPosPair; typedef pair<pit_type, int> PitPosPair;
typedef pair<DocumentClassConstPtr, AuthorList > DocInfoPair;
typedef limited_stack<pair<ParagraphList, DocumentClassConstPtr> > CutStack; typedef limited_stack<pair<ParagraphList, DocInfoPair > > CutStack;
CutStack theCuts(10); CutStack theCuts(10);
// persistent selection, cleared until the next selection // persistent selection, cleared until the next selection
@ -601,10 +602,10 @@ Buffer * copyToTempBuffer(ParagraphList const & paragraphs, DocumentClassConstPt
void putClipboard(ParagraphList const & paragraphs, void putClipboard(ParagraphList const & paragraphs,
DocumentClassConstPtr docclass, docstring const & plaintext, DocInfoPair docinfo, docstring const & plaintext,
BufferParams const & bp) BufferParams const & bp)
{ {
Buffer * buffer = copyToTempBuffer(paragraphs, docclass); Buffer * buffer = copyToTempBuffer(paragraphs, docinfo.first);
if (!buffer) // already asserted in copyToTempBuffer() if (!buffer) // already asserted in copyToTempBuffer()
return; return;
@ -613,11 +614,9 @@ void putClipboard(ParagraphList const & paragraphs,
// applications, the number that can parse it should go up in the future. // applications, the number that can parse it should go up in the future.
buffer->params().html_math_output = BufferParams::MathML; buffer->params().html_math_output = BufferParams::MathML;
if (lyxrc.ct_markup_copied) { // Copy authors to the params. We need those pointers.
// Copy authors to the params. We need those pointers. for (Author const & a : bp.authors())
for (Author const & a : bp.authors()) buffer->params().authors().record(a);
buffer->params().authors().record(a);
}
// Make sure MarkAsExporting is deleted before buffer is // Make sure MarkAsExporting is deleted before buffer is
{ {
@ -725,7 +724,7 @@ void copySelectionHelper(Buffer const & buf, Text const & text,
par.setInsetOwner(nullptr); par.setInsetOwner(nullptr);
} }
cutstack.push(make_pair(copy_pars, dc)); cutstack.push(make_pair(copy_pars, make_pair(dc, buf.params().authors())));
} }
} // namespace } // namespace
@ -1018,7 +1017,7 @@ void copyInset(Cursor const & cur, Inset * inset, docstring const & plaintext)
Font font(inherit_font, bp.language); Font font(inherit_font, bp.language);
par.insertInset(0, inset, font, Change(Change::UNCHANGED)); par.insertInset(0, inset, font, Change(Change::UNCHANGED));
pars.push_back(par); pars.push_back(par);
theCuts.push(make_pair(pars, bp.documentClassPtr())); theCuts.push(make_pair(pars, make_pair(bp.documentClassPtr(), bp.authors())));
// stuff the selection onto the X clipboard, from an explicit copy request // stuff the selection onto the X clipboard, from an explicit copy request
putClipboard(theCuts[0].first, theCuts[0].second, plaintext, bp); putClipboard(theCuts[0].first, theCuts[0].second, plaintext, bp);
@ -1065,7 +1064,7 @@ void copySelectionToStack(CursorData const & cur, CutStack & cutstack)
par.insert(0, grabSelection(cur), Font(sane_font, par.getParLanguage(bp)), par.insert(0, grabSelection(cur), Font(sane_font, par.getParLanguage(bp)),
Change(Change::UNCHANGED)); Change(Change::UNCHANGED));
pars.push_back(par); pars.push_back(par);
cutstack.push(make_pair(pars, bp.documentClassPtr())); cutstack.push(make_pair(pars, make_pair(bp.documentClassPtr(), bp.authors())));
} }
} }
@ -1123,7 +1122,7 @@ void copySelection(Cursor const & cur, docstring const & plaintext)
while (pars.size() > 1) while (pars.size() > 1)
mergeParagraph(bp, pars, 0); mergeParagraph(bp, pars, 0);
} }
theCuts.push(make_pair(pars, bp.documentClassPtr())); theCuts.push(make_pair(pars, make_pair(bp.documentClassPtr(), bp.authors())));
} else { } else {
copySelectionToStack(cur, theCuts); copySelectionToStack(cur, theCuts);
} }
@ -1167,13 +1166,13 @@ void clearCutStack()
} }
docstring selection(size_t sel_index, DocumentClassConstPtr docclass, bool for_math) docstring selection(size_t sel_index, DocInfoPair docinfo, bool for_math)
{ {
if (sel_index >= theCuts.size()) if (sel_index >= theCuts.size())
return docstring(); return docstring();
unique_ptr<Buffer> buffer(copyToTempBuffer(theCuts[sel_index].first, unique_ptr<Buffer> buffer(copyToTempBuffer(theCuts[sel_index].first,
docclass)); docinfo.first));
if (!buffer) if (!buffer)
return docstring(); return docstring();
@ -1186,9 +1185,14 @@ docstring selection(size_t sel_index, DocumentClassConstPtr docclass, bool for_m
void pasteParagraphList(Cursor & cur, ParagraphList const & parlist, void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
DocumentClassConstPtr docclass, ErrorList & errorList, DocumentClassConstPtr docclass, AuthorList const & authors,
ErrorList & errorList,
cap::BranchAction branchAction) cap::BranchAction branchAction)
{ {
// Copy authors to the params. We need those pointers.
for (Author const & a : authors)
cur.buffer()->params().authors().record(a);
if (cur.inTexted()) { if (cur.inTexted()) {
Text * text = cur.text(); Text * text = cur.text();
LBUFERR(text); LBUFERR(text);
@ -1213,7 +1217,8 @@ bool pasteFromStack(Cursor & cur, ErrorList & errorList, size_t sel_index)
cur.recordUndo(); cur.recordUndo();
pasteParagraphList(cur, theCuts[sel_index].first, pasteParagraphList(cur, theCuts[sel_index].first,
theCuts[sel_index].second, errorList, BRANCH_ASK); theCuts[sel_index].second.first, theCuts[sel_index].second.second,
errorList, BRANCH_ASK);
return true; return true;
} }
@ -1226,7 +1231,8 @@ bool pasteFromTemp(Cursor & cur, ErrorList & errorList)
cur.recordUndo(); cur.recordUndo();
pasteParagraphList(cur, tempCut[0].first, pasteParagraphList(cur, tempCut[0].first,
tempCut[0].second, errorList, BRANCH_IGNORE); tempCut[0].second.first, tempCut[0].second.second,
errorList, BRANCH_IGNORE);
return true; return true;
} }
@ -1251,7 +1257,9 @@ bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
if (buffer.readString(lyx)) { if (buffer.readString(lyx)) {
cur.recordUndo(); cur.recordUndo();
pasteParagraphList(cur, buffer.paragraphs(), pasteParagraphList(cur, buffer.paragraphs(),
buffer.params().documentClassPtr(), errorList); buffer.params().documentClassPtr(),
buffer.params().authors(),
errorList);
return true; return true;
} }
} }
@ -1289,7 +1297,9 @@ bool pasteClipboardText(Cursor & cur, ErrorList & errorList, bool asParagraphs,
buffer.changeLanguage(buffer.language(), cur.getFont().language()); buffer.changeLanguage(buffer.language(), cur.getFont().language());
cur.recordUndo(); cur.recordUndo();
pasteParagraphList(cur, buffer.paragraphs(), pasteParagraphList(cur, buffer.paragraphs(),
buffer.params().documentClassPtr(), errorList); buffer.params().documentClassPtr(),
buffer.params().authors(),
errorList);
return true; return true;
} }
} }
@ -1369,7 +1379,9 @@ void pasteSelection(Cursor & cur, ErrorList & errorList)
return; return;
cur.recordUndo(); cur.recordUndo();
pasteParagraphList(cur, selectionBuffer[0].first, pasteParagraphList(cur, selectionBuffer[0].first,
selectionBuffer[0].second, errorList); selectionBuffer[0].second.first,
selectionBuffer[0].second.second,
errorList);
} }

View File

@ -14,6 +14,7 @@
#ifndef CUTANDPASTE_H #ifndef CUTANDPASTE_H
#define CUTANDPASTE_H #define CUTANDPASTE_H
#include "Author.h"
#include "DocumentClassPtr.h" #include "DocumentClassPtr.h"
#include "support/strfwd.h" #include "support/strfwd.h"
@ -41,11 +42,13 @@ namespace cap {
std::vector<docstring> availableSelections(Buffer const *); std::vector<docstring> availableSelections(Buffer const *);
/// Get the number of available elements in the cut buffer. /// Get the number of available elements in the cut buffer.
size_type numberOfSelections(); size_type numberOfSelections();
///
typedef std::pair<DocumentClassConstPtr, AuthorList > DocInfoPair;
/** /**
* Get the sel_index-th element of the cut buffer in plain text format * Get the sel_index-th element of the cut buffer in plain text format
* or, if \param for_math is true, in a format suitable for mathed. * or, if \param for_math is true, in a format suitable for mathed.
*/ */
docstring selection(size_t sel_index, DocumentClassConstPtr docclass, bool for_math = false); docstring selection(size_t sel_index, DocInfoPair docinfo, bool for_math = false);
/** /**
* Replace using the font of the first selected character and select * Replace using the font of the first selected character and select
@ -123,8 +126,9 @@ enum BranchAction {
/// Paste the paragraph list \p parlist at the position given by \p cur. /// Paste the paragraph list \p parlist at the position given by \p cur.
/// Does not handle undo. Does only work in text, not mathed. /// Does not handle undo. Does only work in text, not mathed.
void pasteParagraphList(Cursor & cur, ParagraphList const & parlist, void pasteParagraphList(Cursor & cur, ParagraphList const & parlist,
DocumentClassConstPtr textclass, ErrorList & errorList, DocumentClassConstPtr textclass, AuthorList const & authors,
BranchAction branchAction = BRANCH_ASK); ErrorList & errorList,
BranchAction branchAction = BRANCH_ASK);
/** Needed to switch between different classes. This works /** Needed to switch between different classes. This works

View File

@ -1881,6 +1881,7 @@ bool Text::dissolveInset(Cursor & cur)
} }
pasteParagraphList(cur, plist, b.params().documentClassPtr(), pasteParagraphList(cur, plist, b.params().documentClassPtr(),
b.params().authors(),
b.errorList("Paste")); b.errorList("Paste"));
} }

View File

@ -3999,6 +3999,7 @@ static int findAdvReplace(BufferView * bv, FindAndReplaceOptions const & opt, Ma
LYXERR(Debug::FIND, "Before pasteParagraphList() cur=" << cur << endl); LYXERR(Debug::FIND, "Before pasteParagraphList() cur=" << cur << endl);
cap::pasteParagraphList(cur, repl_buffer.paragraphs(), cap::pasteParagraphList(cur, repl_buffer.paragraphs(),
repl_buffer.params().documentClassPtr(), repl_buffer.params().documentClassPtr(),
repl_buffer.params().authors(),
bv->buffer().errorList("Paste")); bv->buffer().errorList("Paste"));
LYXERR(Debug::FIND, "After pasteParagraphList() cur=" << cur << endl); LYXERR(Debug::FIND, "After pasteParagraphList() cur=" << cur << endl);
sel_len = repl_buffer.paragraphs().begin()->size(); sel_len = repl_buffer.paragraphs().begin()->size();

View File

@ -1566,7 +1566,8 @@ void InsetMathGrid::doDispatch(Cursor & cur, FuncRequest & cmd)
idocstringstream is(cmd.argument()); idocstringstream is(cmd.argument());
int n = 0; int n = 0;
is >> n; is >> n;
topaste = cap::selection(n, buffer().params().documentClassPtr(), true); topaste = cap::selection(n, make_pair(buffer().params().documentClassPtr(),
buffer().params().authors()), true);
} }
InsetMathGrid grid(buffer_, 1, 1); InsetMathGrid grid(buffer_, 1, 1);
if (!topaste.empty()) if (!topaste.empty())

View File

@ -539,7 +539,8 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
size_t n = 0; size_t n = 0;
idocstringstream is(cmd.argument()); idocstringstream is(cmd.argument());
is >> n; is >> n;
topaste = cap::selection(n, buffer().params().documentClassPtr()); topaste = cap::selection(n, make_pair(buffer().params().documentClassPtr(),
buffer().params().authors()));
} }
cur.niceInsert(topaste, parseflg, false); cur.niceInsert(topaste, parseflg, false);
cur.clearSelection(); // bug 393 cur.clearSelection(); // bug 393