-/* This file is part of
- * ======================================================
+/* \file CutAndPaste.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * LyX, The Document Processor
+ * \author Jurgen Vigna
+ * \author Lars Gullik Bjønnes
*
- * Copyright 1995-2001 The LyX Team.
- *
- * ====================================================== */
+ * Full author contact details are available in file CREDITS
+ */
#include <config.h>
#include "support/BoostFormat.h"
#include "support/LAssert.h"
+#include "support/limited_stack.h"
using std::endl;
using std::pair;
// of selections cut/copied. So IMHO later we should have a
// list/vector/deque that we could store
// struct selection_item {
-// Paragraph * buf;
+// ParagraphList copy_pars;
// LyXTextClassList::size_type textclass;
// };
// in and some method of choosing beween them (based on the first few chars
namespace {
-// FIXME: stupid name
-ParagraphList paragraphs;
-textclass_type textclass = 0;
+limited_stack<pair<ParagraphList, textclass_type> > cuts(10);
} // namespace anon
#warning current_view used here.
// should we pass buffer or buffer->params around?
Buffer * buffer = current_view->buffer();
- mergeParagraph(buffer->params, pars, &*startpit);
+ mergeParagraph(buffer->params, pars, startpit);
// this because endpar gets deleted here!
endpit = startpit;
endpos = startpos;
lyx::Assert(0 <= end && end <= endpit->size());
lyx::Assert(startpit != endpit || start <= end);
- textclass = tc;
+ ParagraphList paragraphs;
// Clone the paragraphs within the selection.
ParagraphList::iterator postend = boost::next(endpit);
Paragraph & front = paragraphs.front();
front.erase(0, start);
+ cuts.push(make_pair(paragraphs, tc));
+
return true;
}
CutAndPaste::pasteSelection(ParagraphList & pars,
ParagraphList::iterator pit, int pos,
textclass_type tc)
+{
+ return pasteSelection(pars, pit, pos, tc, 0);
+}
+
+pair<PitPosPair, ParagraphList::iterator>
+CutAndPaste::pasteSelection(ParagraphList & pars,
+ ParagraphList::iterator pit, int pos,
+ textclass_type tc, size_t cut_index)
{
if (!checkPastePossible())
return make_pair(PitPosPair(pit, pos), pit);
lyx::Assert (pos <= pit->size());
- // Make a copy of the simple cut_buffer.
- ParagraphList simple_cut_clone = paragraphs;
+ // Make a copy of the CaP paragraphs.
+ ParagraphList simple_cut_clone = cuts[cut_index].first;
+ textclass_type const textclass = cuts[cut_index].second;
- // Now remove all out of the buffer which is NOT allowed in the
+ // Now remove all out of the pars which is NOT allowed in the
// new environment and set also another font if that is required.
- ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
- int depth_delta = pit->params().depth() - tmpbuf->params().depth();
// Make sure there is no class difference.
-#warning current_view used here
- SwitchLayoutsBetweenClasses(textclass, tc, &*tmpbuf,
- current_view->buffer()->params);
+ SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone);
+
+ ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
+ int depth_delta = pit->params().depth() - tmpbuf->params().depth();
Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
// at level 0.
if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
depth_delta = 0;
+
// Set the right depth so that we are not too deep or shallow.
tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
if (tmpbuf->params().depth() > max_depth)
// Only set this from the 2nd on as the 2nd depends
// for maxDepth still on pit.
- if (tmpbuf->previous() != pit)
+ if (tmpbuf != simple_cut_clone.begin())
max_depth = tmpbuf->getMaxDepthAfter();
// Set the inset owner of this paragraph.
// Open the paragraph for inserting the buf
// if necessary.
- if (pit->size() > pos || !pit->next()) {
+ if (pit->size() > pos || boost::next(pit) == pars.end()) {
breakParagraphConservative(current_view->buffer()->params,
pars, pit, pos);
paste_the_end = true;
// Paste it!
ParagraphList::iterator past_pit = boost::next(pit);
- // It is possible that std::list::splice also could be used here.
- pars.insert(past_pit,
- simple_cut_clone.begin(), simple_cut_clone.end());
+ pars.splice(past_pit, simple_cut_clone);
ParagraphList::iterator last_paste = boost::prior(past_pit);
// If we only inserted one paragraph.
last_paste = pit;
mergeParagraph(current_view->buffer()->params, pars, pit);
+
// Store the new cursor position.
pit = last_paste;
pos = last_paste->size();
// Maybe some pasting.
- if (boost::next(last_paste) != simple_cut_clone.end() &&
+#warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
+ if (boost::next(last_paste) != pars.end() &&
paste_the_end) {
if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
mergeParagraph(current_view->buffer()->params, pars,
int CutAndPaste::nrOfParagraphs()
{
- return paragraphs.size();
+ return cuts.empty() ? 0 : cuts[0].first.size();
}
int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
textclass_type c2,
- Paragraph * par,
- BufferParams const & /*bparams*/)
+ ParagraphList & pars)
{
+ lyx::Assert(!pars.empty());
+
+ Paragraph * par = &*pars.begin();
+
int ret = 0;
- if (!par || c1 == c2)
+ if (c1 == c2)
return ret;
LyXTextClass const & tclass1 = textclasslist[c1];
+ tclass1.name() + _(" to ")
+ tclass2.name();
#endif
- freezeUndo();
+ // To warn the user that something had to be done.
InsetError * new_inset = new InsetError(s);
- LyXText * txt = current_view->getLyXText();
- LyXCursor cur = txt->cursor;
- txt->setCursorIntern(par, 0);
- txt->insertInset(new_inset);
- txt->fullRebreak();
- txt->setCursorIntern(cur.par(), cur.pos());
- unFreezeUndo();
+ par->insertInset(0, new_inset);
}
}
return ret;
bool CutAndPaste::checkPastePossible()
{
- return !paragraphs.empty();
+ return !cuts.empty() && !cuts[0].first.empty();
}