2003-08-23 00:17:00 +00:00
|
|
|
|
/*
|
|
|
|
|
* \file CutAndPaste.C
|
2003-05-06 09:34:56 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-09-07 01:45:40 +00:00
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
2003-05-06 09:34:56 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
2003-06-28 01:23:11 +00:00
|
|
|
|
* \author Alfredo Braunstein
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-05-06 09:34:56 +00:00
|
|
|
|
*/
|
2000-04-10 14:29:05 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "CutAndPaste.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2000-06-08 23:16:16 +00:00
|
|
|
|
#include "buffer.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "buffer_funcs.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
#include "bufferparams.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
#include "cursor.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "errorlist.h"
|
|
|
|
|
#include "gettext.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "lyxtext.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include "lyxtextclasslist.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
|
#include "paragraph.h"
|
2002-08-20 19:41:13 +00:00
|
|
|
|
#include "paragraph_funcs.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "ParagraphParameters.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "ParagraphList_fwd.h"
|
2004-03-28 22:00:22 +00:00
|
|
|
|
#include "pariterator.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include "undo.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2003-06-03 15:10:14 +00:00
|
|
|
|
#include "insets/insettabular.h"
|
2000-04-10 14:29:05 +00:00
|
|
|
|
|
2003-05-13 09:48:57 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2002-12-01 21:10:37 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::pos_type;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
using lyx::par_type;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::textclass_type;
|
|
|
|
|
|
|
|
|
|
using lyx::support::bformat;
|
2003-09-09 17:25:35 +00:00
|
|
|
|
|
2003-05-01 12:53:22 +00:00
|
|
|
|
using std::for_each;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::make_pair;
|
|
|
|
|
using std::pair;
|
2003-06-17 15:33:49 +00:00
|
|
|
|
using std::vector;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-05-01 12:53:22 +00:00
|
|
|
|
|
2003-06-17 15:33:49 +00:00
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
namespace {
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
typedef std::pair<lyx::par_type, int> PitPosPair;
|
|
|
|
|
|
|
|
|
|
typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
|
|
|
|
|
|
2003-06-17 15:33:49 +00:00
|
|
|
|
CutStack cuts(10);
|
2000-04-10 14:29:05 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
struct resetOwnerAndChanges : public std::unary_function<Paragraph, void> {
|
|
|
|
|
void operator()(Paragraph & p) const {
|
|
|
|
|
p.cleanChanges();
|
|
|
|
|
p.setInsetOwner(0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
|
} // namespace anon
|
2000-04-11 22:55:29 +00:00
|
|
|
|
|
2003-06-28 01:23:11 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace cap {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2,
|
|
|
|
|
ParagraphList & pars, ErrorList & errorlist)
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(!pars.empty());
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (c1 == c2)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
LyXTextClass const & tclass1 = textclasslist[c1];
|
|
|
|
|
LyXTextClass const & tclass2 = textclasslist[c2];
|
2004-03-28 22:00:22 +00:00
|
|
|
|
|
|
|
|
|
InsetText in;
|
|
|
|
|
std::swap(in.paragraphs(), pars);
|
|
|
|
|
|
|
|
|
|
ParIterator end = ParIterator(in, pars.size());
|
|
|
|
|
for (ParIterator it = ParIterator(in, 0); it != end; ++it) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
string const name = it->layout()->name();
|
|
|
|
|
bool hasLayout = tclass2.hasLayout(name);
|
|
|
|
|
|
|
|
|
|
if (hasLayout)
|
|
|
|
|
it->layout(tclass2[name]);
|
|
|
|
|
else
|
|
|
|
|
it->layout(tclass2.defaultLayout());
|
|
|
|
|
|
|
|
|
|
if (!hasLayout && name != tclass1.defaultLayoutName()) {
|
|
|
|
|
++ret;
|
|
|
|
|
string const s = bformat(
|
|
|
|
|
_("Layout had to be changed from\n%1$s to %2$s\n"
|
|
|
|
|
"because of class conversion from\n%3$s to %4$s"),
|
|
|
|
|
name, it->layout()->name(), tclass1.name(), tclass2.name());
|
|
|
|
|
// To warn the user that something had to be done.
|
|
|
|
|
errorlist.push_back(ErrorItem("Changed Layout", s,
|
|
|
|
|
it->id(), 0,
|
|
|
|
|
it->size()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-28 22:00:22 +00:00
|
|
|
|
std::swap(in.paragraphs(), pars);
|
2004-03-25 09:16:36 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<string> const availableSelections(Buffer const & buffer)
|
2003-06-17 15:33:49 +00:00
|
|
|
|
{
|
|
|
|
|
vector<string> selList;
|
|
|
|
|
|
|
|
|
|
CutStack::const_iterator cit = cuts.begin();
|
|
|
|
|
CutStack::const_iterator end = cuts.end();
|
|
|
|
|
for (; cit != end; ++cit) {
|
2003-06-19 13:55:31 +00:00
|
|
|
|
// we do not use cit-> here because gcc 2.9x does not
|
|
|
|
|
// like it (JMarc)
|
|
|
|
|
ParagraphList const & pars = (*cit).first;
|
2003-06-18 13:53:11 +00:00
|
|
|
|
string asciiSel;
|
|
|
|
|
ParagraphList::const_iterator pit = pars.begin();
|
|
|
|
|
ParagraphList::const_iterator pend = pars.end();
|
|
|
|
|
for (; pit != pend; ++pit) {
|
2003-08-28 07:41:31 +00:00
|
|
|
|
asciiSel += pit->asString(buffer, false);
|
2003-06-18 13:53:11 +00:00
|
|
|
|
if (asciiSel.size() > 25) {
|
|
|
|
|
asciiSel.replace(22, string::npos, "...");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selList.push_back(asciiSel);
|
2003-06-17 15:33:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return selList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
PitPosPair eraseSelection(BufferParams const & params, ParagraphList & pars,
|
|
|
|
|
par_type startpit, par_type endpit,
|
|
|
|
|
int startpos, int endpos, bool doclear)
|
2001-04-17 13:48:09 +00:00
|
|
|
|
{
|
2004-03-27 01:18:51 +00:00
|
|
|
|
if (startpit == par_type(pars.size()) ||
|
|
|
|
|
(startpos > pars[startpit].size()))
|
2003-04-30 10:32:01 +00:00
|
|
|
|
return PitPosPair(endpit, endpos);
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2004-03-27 01:18:51 +00:00
|
|
|
|
if (endpit == par_type(pars.size()) ||
|
|
|
|
|
startpit == endpit) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
endpos -= pars[startpit].erase(startpos, endpos);
|
2003-04-30 10:32:01 +00:00
|
|
|
|
return PitPosPair(endpit, endpos);
|
2003-04-29 09:40:49 +00:00
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// clear end/begin fragments of the first/last par in selection
|
2003-04-29 09:40:49 +00:00
|
|
|
|
bool all_erased = true;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars[startpit].erase(startpos, pars[startpit].size());
|
|
|
|
|
if (pars[startpit].size() != startpos)
|
2003-04-29 09:40:49 +00:00
|
|
|
|
all_erased = false;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
endpos -= pars[endpit].erase(0, endpos);
|
2003-04-29 09:40:49 +00:00
|
|
|
|
if (endpos != 0)
|
|
|
|
|
all_erased = false;
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2003-04-29 09:40:49 +00:00
|
|
|
|
// Loop through the deleted pars if any, erasing as needed
|
2004-03-28 19:13:11 +00:00
|
|
|
|
for (par_type pit = startpit + 1; pit != endpit;) {
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// "erase" the contents of the par
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars[pit].erase(0, pars[pit].size());
|
|
|
|
|
if (!pars[pit].size()) {
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// remove the par if it's now empty
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars.erase(pars.begin() + pit);
|
2004-03-28 19:13:11 +00:00
|
|
|
|
--endpit;
|
|
|
|
|
} else {
|
|
|
|
|
++pit;
|
2003-04-29 09:40:49 +00:00
|
|
|
|
all_erased = false;
|
2004-03-28 19:13:11 +00:00
|
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
#if 0 // FIXME: why for cut but not copy ?
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// the cut selection should begin with standard layout
|
|
|
|
|
if (realcut) {
|
|
|
|
|
buf->params().clear();
|
|
|
|
|
buf->bibkey = 0;
|
|
|
|
|
buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
|
|
|
|
|
}
|
2003-03-04 09:27:27 +00:00
|
|
|
|
#endif
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2004-03-27 01:18:51 +00:00
|
|
|
|
if (startpit + 1 == par_type(pars.size()))
|
2003-04-30 10:32:01 +00:00
|
|
|
|
return PitPosPair(endpit, endpos);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
if (doclear) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars[startpit + 1].stripLeadingSpaces();
|
2003-02-08 19:18:01 +00:00
|
|
|
|
}
|
2002-03-05 23:02:37 +00:00
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// paste the paragraphs again, if possible
|
2003-05-01 12:53:22 +00:00
|
|
|
|
if (all_erased &&
|
2004-03-25 09:16:36 +00:00
|
|
|
|
(pars[startpit].hasSameLayout(pars[startpit + 1]) ||
|
|
|
|
|
pars[startpit + 1].empty())) {
|
2003-06-10 14:39:45 +00:00
|
|
|
|
mergeParagraph(params, pars, startpit);
|
2003-02-08 19:18:01 +00:00
|
|
|
|
// this because endpar gets deleted here!
|
2003-04-29 09:40:49 +00:00
|
|
|
|
endpit = startpit;
|
|
|
|
|
endpos = startpos;
|
2000-05-22 11:08:25 +00:00
|
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
2003-04-30 10:32:01 +00:00
|
|
|
|
return PitPosPair(endpit, endpos);
|
2003-04-29 09:40:49 +00:00
|
|
|
|
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-03-04 09:27:27 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
bool copySelection(ParagraphList & pars,
|
|
|
|
|
par_type startpit, par_type endpit,
|
|
|
|
|
int start, int end, textclass_type tc)
|
2001-04-17 13:48:09 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
|
|
|
|
|
BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
|
2003-09-09 17:25:35 +00:00
|
|
|
|
BOOST_ASSERT(startpit != endpit || start <= end);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-05-01 12:53:22 +00:00
|
|
|
|
// Clone the paragraphs within the selection.
|
2004-03-25 09:16:36 +00:00
|
|
|
|
ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
|
2003-05-01 12:53:22 +00:00
|
|
|
|
for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-04-30 10:32:01 +00:00
|
|
|
|
// Cut out the end of the last paragraph.
|
|
|
|
|
Paragraph & back = paragraphs.back();
|
2003-05-01 12:53:22 +00:00
|
|
|
|
back.erase(end, back.size());
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-04-30 10:32:01 +00:00
|
|
|
|
// Cut out the begin of the first paragraph
|
|
|
|
|
Paragraph & front = paragraphs.front();
|
2003-05-01 12:53:22 +00:00
|
|
|
|
front.erase(0, start);
|
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
|
cuts.push(make_pair(paragraphs, tc));
|
|
|
|
|
|
2001-04-17 13:48:09 +00:00
|
|
|
|
return true;
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
PitPosPair cutSelection(BufferParams const & params, ParagraphList & pars,
|
|
|
|
|
par_type startpit, par_type endpit,
|
|
|
|
|
int startpos, int endpos, textclass_type tc, bool doclear)
|
2003-05-06 09:34:56 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
copySelection(pars, startpit, endpit, startpos, endpos, tc);
|
|
|
|
|
return eraseSelection(params, pars, startpit, endpit, startpos,
|
|
|
|
|
endpos, doclear);
|
2003-05-06 09:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-06-17 15:33:49 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pair<PitPosPair, par_type>
|
|
|
|
|
pasteSelection(Buffer const & buffer, ParagraphList & pars,
|
|
|
|
|
par_type pit, int pos,
|
|
|
|
|
textclass_type tc, size_t cut_index, ErrorList & errorlist)
|
2001-04-17 13:48:09 +00:00
|
|
|
|
{
|
2003-03-06 21:01:04 +00:00
|
|
|
|
if (!checkPastePossible())
|
2003-05-01 12:53:22 +00:00
|
|
|
|
return make_pair(PitPosPair(pit, pos), pit);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
BOOST_ASSERT (pos <= pars[pit].size());
|
2003-03-17 16:25:00 +00:00
|
|
|
|
|
2003-05-01 15:43:58 +00:00
|
|
|
|
// Make a copy of the CaP paragraphs.
|
2004-03-25 09:16:36 +00:00
|
|
|
|
ParagraphList insertion = cuts[cut_index].first;
|
2003-05-06 09:34:56 +00:00
|
|
|
|
textclass_type const textclass = cuts[cut_index].second;
|
2003-05-01 13:12:43 +00:00
|
|
|
|
|
2003-05-01 15:43:58 +00:00
|
|
|
|
// Now remove all out of the pars which is NOT allowed in the
|
2003-05-01 13:12:43 +00:00
|
|
|
|
// new environment and set also another font if that is required.
|
2003-05-01 15:43:58 +00:00
|
|
|
|
|
2003-05-06 09:34:56 +00:00
|
|
|
|
// Make sure there is no class difference.
|
2004-03-25 09:16:36 +00:00
|
|
|
|
SwitchLayoutsBetweenClasses(textclass, tc, insertion,
|
2003-05-20 16:51:31 +00:00
|
|
|
|
errorlist);
|
2003-05-06 09:34:56 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
ParagraphList::iterator tmpbuf = insertion.begin();
|
|
|
|
|
int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
|
2003-03-17 16:25:00 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
Paragraph::depth_type max_depth = pars[pit].getMaxDepthAfter();
|
2003-03-17 16:25:00 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
for (; tmpbuf != insertion.end(); ++tmpbuf) {
|
2003-03-08 15:58:28 +00:00
|
|
|
|
// If we have a negative jump so that the depth would
|
|
|
|
|
// go below 0 depth then we have to redo the delta to
|
|
|
|
|
// this new max depth level so that subsequent
|
|
|
|
|
// paragraphs are aligned correctly to this paragraph
|
|
|
|
|
// at level 0.
|
2003-12-10 09:45:32 +00:00
|
|
|
|
if (int(tmpbuf->params().depth()) + depth_delta < 0)
|
2003-03-08 15:58:28 +00:00
|
|
|
|
depth_delta = 0;
|
2003-05-01 15:43:58 +00:00
|
|
|
|
|
2003-05-01 14:45:04 +00:00
|
|
|
|
// Set the right depth so that we are not too deep or shallow.
|
2003-03-08 15:58:28 +00:00
|
|
|
|
tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
|
|
|
|
|
if (tmpbuf->params().depth() > max_depth)
|
|
|
|
|
tmpbuf->params().depth(max_depth);
|
2003-05-01 13:12:43 +00:00
|
|
|
|
|
|
|
|
|
// Only set this from the 2nd on as the 2nd depends
|
|
|
|
|
// for maxDepth still on pit.
|
2004-03-25 09:16:36 +00:00
|
|
|
|
if (tmpbuf != insertion.begin())
|
2003-03-08 15:58:28 +00:00
|
|
|
|
max_depth = tmpbuf->getMaxDepthAfter();
|
2003-05-01 13:12:43 +00:00
|
|
|
|
|
|
|
|
|
// Set the inset owner of this paragraph.
|
2004-03-25 09:16:36 +00:00
|
|
|
|
tmpbuf->setInsetOwner(pars[pit].inInset());
|
2003-03-08 15:58:28 +00:00
|
|
|
|
for (pos_type i = 0; i < tmpbuf->size(); ++i) {
|
|
|
|
|
if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
if (!pars[pit].insetAllowed(tmpbuf->getInset(i)->lyxCode()))
|
2003-03-08 15:58:28 +00:00
|
|
|
|
tmpbuf->erase(i--);
|
2001-12-05 15:34:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-04-17 13:48:09 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
// Make the buf exactly the same layout as the cursor paragraph.
|
|
|
|
|
insertion.begin()->makeSameLayout(pars[pit]);
|
2003-03-17 16:25:00 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
// Prepare the paragraphs and insets for insertion.
|
|
|
|
|
// A couple of insets store buffer references so need updating.
|
2004-03-28 22:00:22 +00:00
|
|
|
|
InsetText in;
|
|
|
|
|
std::swap(in.paragraphs(), insertion);
|
|
|
|
|
|
|
|
|
|
ParIterator fpit(in, 0);
|
|
|
|
|
ParIterator fend(in, insertion.size());
|
2003-06-03 15:10:14 +00:00
|
|
|
|
|
|
|
|
|
for (; fpit != fend; ++fpit) {
|
|
|
|
|
InsetList::iterator lit = fpit->insetlist.begin();
|
|
|
|
|
InsetList::iterator eit = fpit->insetlist.end();
|
|
|
|
|
|
|
|
|
|
for (; lit != eit; ++lit) {
|
|
|
|
|
switch (lit->inset->lyxCode()) {
|
2003-07-25 21:20:24 +00:00
|
|
|
|
case InsetOld::TABULAR_CODE: {
|
2003-06-03 15:10:14 +00:00
|
|
|
|
InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
|
2003-06-10 14:39:45 +00:00
|
|
|
|
it->buffer(const_cast<Buffer*>(&buffer));
|
2003-06-03 15:10:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2003-06-10 14:39:45 +00:00
|
|
|
|
|
2003-06-03 15:10:14 +00:00
|
|
|
|
default:
|
|
|
|
|
break; // nothing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-03-28 22:00:22 +00:00
|
|
|
|
std::swap(in.paragraphs(), insertion);
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
// Split the paragraph for inserting the buf if necessary.
|
|
|
|
|
bool did_split = false;
|
2004-03-27 01:18:51 +00:00
|
|
|
|
if (pars[pit].size() || pit + 1 == par_type(pars.size())) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
breakParagraphConservative(buffer.params(), pars, pit, pos);
|
|
|
|
|
did_split = true;
|
2003-03-08 15:58:28 +00:00
|
|
|
|
}
|
2003-05-01 13:22:07 +00:00
|
|
|
|
|
2003-05-01 13:12:43 +00:00
|
|
|
|
// Paste it!
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars.insert(pars.begin() + pit + 1, insertion.begin(), insertion.end());
|
2003-09-09 09:47:59 +00:00
|
|
|
|
mergeParagraph(buffer.params(), pars, pit);
|
2003-05-01 15:43:58 +00:00
|
|
|
|
|
2004-03-28 19:13:11 +00:00
|
|
|
|
par_type last_paste = pit + insertion.size() - 1;
|
|
|
|
|
|
2003-05-01 13:12:43 +00:00
|
|
|
|
// Store the new cursor position.
|
2003-05-01 14:45:04 +00:00
|
|
|
|
pit = last_paste;
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pos = pars[last_paste].size();
|
2003-05-01 14:45:04 +00:00
|
|
|
|
|
2003-05-01 13:12:43 +00:00
|
|
|
|
// Maybe some pasting.
|
2004-03-27 01:18:51 +00:00
|
|
|
|
if (did_split && last_paste + 1 != par_type(pars.size())) {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
|
|
|
|
|
mergeParagraph(buffer.params(), pars, last_paste);
|
|
|
|
|
} else if (pars[last_paste + 1].empty()) {
|
|
|
|
|
pars[last_paste + 1].makeSameLayout(pars[last_paste]);
|
|
|
|
|
mergeParagraph(buffer.params(), pars, last_paste);
|
|
|
|
|
} else if (pars[last_paste].empty()) {
|
2004-03-28 19:13:11 +00:00
|
|
|
|
pars[last_paste].makeSameLayout(pars[last_paste + 1]);
|
2004-03-25 09:16:36 +00:00
|
|
|
|
mergeParagraph(buffer.params(), pars, last_paste);
|
2004-03-28 19:13:11 +00:00
|
|
|
|
} else {
|
2004-03-25 09:16:36 +00:00
|
|
|
|
pars[last_paste + 1].stripLeadingSpaces();
|
2004-03-28 19:13:11 +00:00
|
|
|
|
++last_paste;
|
|
|
|
|
}
|
2003-03-08 15:58:28 +00:00
|
|
|
|
}
|
2003-03-17 16:25:00 +00:00
|
|
|
|
|
2004-03-28 19:13:11 +00:00
|
|
|
|
return make_pair(PitPosPair(pit, pos), last_paste + 1);
|
2004-03-25 09:16:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pair<PitPosPair, par_type>
|
|
|
|
|
pasteSelection(Buffer const & buffer, ParagraphList & pars,
|
|
|
|
|
par_type pit, int pos, textclass_type tc, ErrorList & errorlist)
|
|
|
|
|
{
|
|
|
|
|
return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
int nrOfParagraphs()
|
2000-04-10 14:29:05 +00:00
|
|
|
|
{
|
2003-05-06 09:34:56 +00:00
|
|
|
|
return cuts.empty() ? 0 : cuts[0].first.size();
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
bool checkPastePossible()
|
2000-04-10 14:29:05 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
return !cuts.empty() && !cuts[0].first.empty();
|
|
|
|
|
}
|
2003-05-06 09:34:56 +00:00
|
|
|
|
|
2001-12-07 18:40:24 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
void cutSelection(LCursor & cur, bool doclear, bool realcut)
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
// Stuff what we got on the clipboard. Even if there is no selection.
|
|
|
|
|
|
|
|
|
|
// There is a problem with having the stuffing here in that the
|
|
|
|
|
// larger the selection the slower LyX will get. This can be
|
|
|
|
|
// solved by running the line below only when the selection has
|
|
|
|
|
// finished. The solution used currently just works, to make it
|
|
|
|
|
// faster we need to be more clever and probably also have more
|
|
|
|
|
// calls to stuffClipboard. (Lgb)
|
|
|
|
|
cur.bv().stuffClipboard(cur.selectionAsString(true));
|
|
|
|
|
|
|
|
|
|
// This doesn't make sense, if there is no selection
|
|
|
|
|
if (!cur.selection())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// OK, we have a selection. This is always between cur.selBegin()
|
|
|
|
|
// and cur.selEnd()
|
|
|
|
|
|
|
|
|
|
// make sure that the depth behind the selection are restored, too
|
|
|
|
|
recordUndoSelection(cur);
|
|
|
|
|
par_type begpit = cur.selBegin().par();
|
|
|
|
|
par_type endpit = cur.selEnd().par();
|
|
|
|
|
|
|
|
|
|
int endpos = cur.selEnd().pos();
|
|
|
|
|
|
|
|
|
|
BufferParams const & bufparams = cur.bv().buffer()->params();
|
|
|
|
|
boost::tie(endpit, endpos) = realcut ?
|
|
|
|
|
cutSelection(bufparams,
|
|
|
|
|
text->paragraphs(),
|
|
|
|
|
begpit, endpit,
|
|
|
|
|
cur.selBegin().pos(), endpos,
|
|
|
|
|
bufparams.textclass,
|
|
|
|
|
doclear)
|
|
|
|
|
: eraseSelection(bufparams,
|
|
|
|
|
text->paragraphs(),
|
|
|
|
|
begpit, endpit,
|
|
|
|
|
cur.selBegin().pos(), endpos,
|
|
|
|
|
doclear);
|
|
|
|
|
// sometimes necessary
|
|
|
|
|
if (doclear)
|
|
|
|
|
text->paragraphs()[begpit].stripLeadingSpaces();
|
|
|
|
|
|
|
|
|
|
text->redoParagraphs(begpit, begpit + 1);
|
|
|
|
|
// cutSelection can invalidate the cursor so we need to set
|
|
|
|
|
// it anew. (Lgb)
|
|
|
|
|
// we prefer the end for when tracking changes
|
|
|
|
|
cur.pos() = endpos;
|
|
|
|
|
cur.par() = endpit;
|
|
|
|
|
|
|
|
|
|
// need a valid cursor. (Lgb)
|
|
|
|
|
cur.clearSelection();
|
|
|
|
|
text->updateCounters();
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
void copySelection(LCursor & cur)
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
// stuff the selection onto the X clipboard, from an explicit copy request
|
|
|
|
|
cur.bv().stuffClipboard(cur.selectionAsString(true));
|
|
|
|
|
|
|
|
|
|
// this doesn't make sense, if there is no selection
|
|
|
|
|
if (!cur.selection())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
par_type par = cur.selBegin().par();
|
|
|
|
|
while (pos < pars[par].size()
|
|
|
|
|
&& pars[par].isLineSeparator(pos)
|
|
|
|
|
&& (par != cur.selEnd().par() || pos < cur.selEnd().pos()))
|
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
|
|
copySelection(pars, par, cur.selEnd().par(),
|
|
|
|
|
pos, cur.selEnd().pos(), cur.bv().buffer()->params().textclass);
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
|
2004-03-25 09:16:36 +00:00
|
|
|
|
void pasteSelection(LCursor & cur, size_t sel_index)
|
2000-04-10 14:29:05 +00:00
|
|
|
|
{
|
2004-03-25 09:16:36 +00:00
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
// this does not make sense, if there is nothing to paste
|
|
|
|
|
if (!checkPastePossible())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
recordUndo(cur);
|
|
|
|
|
|
|
|
|
|
par_type endpit;
|
|
|
|
|
PitPosPair ppp;
|
|
|
|
|
|
|
|
|
|
ErrorList el;
|
|
|
|
|
|
|
|
|
|
boost::tie(ppp, endpit) =
|
|
|
|
|
pasteSelection(*cur.bv().buffer(),
|
|
|
|
|
text->paragraphs(),
|
|
|
|
|
cur.par(), cur.pos(),
|
|
|
|
|
cur.bv().buffer()->params().textclass,
|
|
|
|
|
sel_index, el);
|
|
|
|
|
bufferErrors(*cur.bv().buffer(), el);
|
|
|
|
|
text->bv()->showErrorList(_("Paste"));
|
|
|
|
|
|
|
|
|
|
text->redoParagraphs(cur.par(), endpit);
|
|
|
|
|
|
|
|
|
|
cur.clearSelection();
|
|
|
|
|
cur.resetAnchor();
|
|
|
|
|
text->setCursor(cur, ppp.first, ppp.second);
|
|
|
|
|
cur.setSelection();
|
|
|
|
|
text->updateCounters();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setSelectionRange(LCursor & cur, pos_type length)
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
if (!length)
|
|
|
|
|
return;
|
|
|
|
|
cur.resetAnchor();
|
|
|
|
|
while (length--)
|
|
|
|
|
text->cursorRight(cur);
|
|
|
|
|
cur.setSelection();
|
2000-04-10 14:29:05 +00:00
|
|
|
|
}
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// simple replacing. The font of the first selected character is used
|
|
|
|
|
void replaceSelectionWithString(LCursor & cur, string const & str)
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
recordUndo(cur);
|
|
|
|
|
|
|
|
|
|
// Get font setting before we cut
|
|
|
|
|
pos_type pos = cur.selEnd().pos();
|
|
|
|
|
LyXFont const font = text->getPar(cur.selBegin().par()).
|
|
|
|
|
getFontSettings(cur.bv().buffer()->params(), cur.selBegin().pos());
|
|
|
|
|
|
|
|
|
|
// Insert the new string
|
|
|
|
|
string::const_iterator cit = str.begin();
|
|
|
|
|
string::const_iterator end = str.end();
|
|
|
|
|
for (; cit != end; ++cit, ++pos)
|
|
|
|
|
text->getPar(cur.selEnd().par()).insertChar(pos, (*cit), font);
|
|
|
|
|
|
|
|
|
|
// Cut the selection
|
|
|
|
|
cutSelection(cur, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void replaceSelection(LCursor & cur)
|
|
|
|
|
{
|
|
|
|
|
if (cur.selection())
|
|
|
|
|
cutSelection(cur, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// only used by the spellchecker
|
|
|
|
|
void replaceWord(LCursor & cur, string const & replacestring)
|
|
|
|
|
{
|
|
|
|
|
LyXText * text = cur.text();
|
|
|
|
|
BOOST_ASSERT(text);
|
|
|
|
|
|
|
|
|
|
replaceSelectionWithString(cur, replacestring);
|
|
|
|
|
setSelectionRange(cur, replacestring.length());
|
|
|
|
|
|
|
|
|
|
// Go back so that replacement string is also spellchecked
|
|
|
|
|
for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
|
|
|
|
|
text->cursorLeft(cur);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace cap
|
|
|
|
|
} // namespace lyx
|