2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "CutAndPaste.h"
14 #include "BufferView.h"
16 #include "errorlist.h"
17 #include "paragraph.h"
18 #include "ParagraphParameters.h"
20 #include "lyxcursor.h"
21 #include "iterators.h"
22 #include "lyxtextclasslist.h"
23 #include "undo_funcs.h"
25 #include "paragraph_funcs.h"
27 #include "insets/insetinclude.h"
28 #include "insets/insettabular.h"
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
32 #include "support/limited_stack.h"
41 using lyx::textclass_type;
44 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
53 CutAndPaste::availableSelections(Buffer const & buffer)
55 vector<string> selList;
57 CutStack::const_iterator cit = cuts.begin();
58 CutStack::const_iterator end = cuts.end();
59 for (; cit != end; ++cit) {
60 ParagraphList const & pars = cit->first;
61 string asciiPar(pars.front().asString(&buffer, false), 0, 25);
62 selList.push_back(asciiPar);
69 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
71 ParagraphList::iterator startpit,
72 ParagraphList::iterator endpit,
73 int startpos, int endpos,
74 textclass_type tc, bool doclear)
76 copySelection(startpit, endpit, startpos, endpos, tc);
77 return eraseSelection(params, pars, startpit, endpit, startpos,
82 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
84 ParagraphList::iterator startpit,
85 ParagraphList::iterator endpit,
86 int startpos, int endpos, bool doclear)
88 if (startpit == pars.end() || (startpos > startpit->size()))
89 return PitPosPair(endpit, endpos);
91 if (endpit == pars.end() || startpit == endpit) {
92 endpos -= startpit->erase(startpos, endpos);
93 return PitPosPair(endpit, endpos);
96 // clear end/begin fragments of the first/last par in selection
97 bool all_erased = true;
99 startpit->erase(startpos, startpit->size());
100 if (startpit->size() != startpos)
103 endpos -= endpit->erase(0, endpos);
107 // Loop through the deleted pars if any, erasing as needed
109 ParagraphList::iterator pit = boost::next(startpit);
111 while (pit != endpit && pit != pars.end()) {
112 ParagraphList::iterator const next = boost::next(pit);
113 // "erase" the contents of the par
114 pit->erase(0, pit->size());
116 // remove the par if it's now empty
123 #if 0 // FIXME: why for cut but not copy ?
124 // the cut selection should begin with standard layout
126 buf->params().clear();
128 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
132 if (boost::next(startpit) == pars.end())
133 return PitPosPair(endpit, endpos);
136 boost::next(startpit)->stripLeadingSpaces();
139 // paste the paragraphs again, if possible
141 (startpit->hasSameLayout(*boost::next(startpit)) ||
142 boost::next(startpit)->empty())) {
143 mergeParagraph(params, pars, startpit);
144 // this because endpar gets deleted here!
149 return PitPosPair(endpit, endpos);
156 struct resetOwnerAndChanges {
157 void operator()(Paragraph & p) {
165 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
166 ParagraphList::iterator endpit,
167 int start, int end, textclass_type tc)
169 lyx::Assert(&*startpit);
170 lyx::Assert(&*endpit);
171 lyx::Assert(0 <= start && start <= startpit->size());
172 lyx::Assert(0 <= end && end <= endpit->size());
173 lyx::Assert(startpit != endpit || start <= end);
175 ParagraphList paragraphs;
177 // Clone the paragraphs within the selection.
178 ParagraphList::iterator postend = boost::next(endpit);
180 paragraphs.assign(startpit, postend);
181 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
183 // Cut out the end of the last paragraph.
184 Paragraph & back = paragraphs.back();
185 back.erase(end, back.size());
187 // Cut out the begin of the first paragraph
188 Paragraph & front = paragraphs.front();
189 front.erase(0, start);
191 cuts.push(make_pair(paragraphs, tc));
197 pair<PitPosPair, ParagraphList::iterator>
198 CutAndPaste::pasteSelection(Buffer const & buffer,
199 ParagraphList & pars,
200 ParagraphList::iterator pit, int pos,
202 ErrorList & errorlist)
204 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
208 pair<PitPosPair, ParagraphList::iterator>
209 CutAndPaste::pasteSelection(Buffer const & buffer,
210 ParagraphList & pars,
211 ParagraphList::iterator pit, int pos,
212 textclass_type tc, size_t cut_index,
213 ErrorList & errorlist)
215 if (!checkPastePossible())
216 return make_pair(PitPosPair(pit, pos), pit);
218 lyx::Assert (pos <= pit->size());
220 // Make a copy of the CaP paragraphs.
221 ParagraphList simple_cut_clone = cuts[cut_index].first;
222 textclass_type const textclass = cuts[cut_index].second;
224 // Now remove all out of the pars which is NOT allowed in the
225 // new environment and set also another font if that is required.
227 // Make sure there is no class difference.
228 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
231 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
232 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
234 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
236 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
237 // If we have a negative jump so that the depth would
238 // go below 0 depth then we have to redo the delta to
239 // this new max depth level so that subsequent
240 // paragraphs are aligned correctly to this paragraph
242 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
245 // Set the right depth so that we are not too deep or shallow.
246 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
247 if (tmpbuf->params().depth() > max_depth)
248 tmpbuf->params().depth(max_depth);
250 // Only set this from the 2nd on as the 2nd depends
251 // for maxDepth still on pit.
252 if (tmpbuf != simple_cut_clone.begin())
253 max_depth = tmpbuf->getMaxDepthAfter();
255 // Set the inset owner of this paragraph.
256 tmpbuf->setInsetOwner(pit->inInset());
257 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
258 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
259 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
263 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
265 if (!pit->checkInsertChar(f1)) {
267 } else if (f1 != f2) {
268 tmpbuf->setFont(i, f1);
274 // Make the buf exactly the same layout than
275 // the cursor paragraph.
276 simple_cut_clone.begin()->makeSameLayout(*pit);
278 // Prepare the paragraphs and insets for insertion
279 // A couple of insets store buffer references so need
281 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
282 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
284 for (; fpit != fend; ++fpit) {
285 InsetList::iterator lit = fpit->insetlist.begin();
286 InsetList::iterator eit = fpit->insetlist.end();
288 for (; lit != eit; ++lit) {
289 switch (lit->inset->lyxCode()) {
290 case Inset::INCLUDE_CODE: {
291 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
292 InsetInclude::Params ip = ii->params();
293 ip.masterFilename_ = buffer.fileName();
298 case Inset::TABULAR_CODE: {
299 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
300 it->buffer(const_cast<Buffer*>(&buffer));
310 bool paste_the_end = false;
312 // Open the paragraph for inserting the buf
314 if (pit->size() > pos || boost::next(pit) == pars.end()) {
315 breakParagraphConservative(buffer.params,
317 paste_the_end = true;
320 // Set the end for redoing later.
321 ParagraphList::iterator endpit = boost::next(boost::next(pit));
325 ParagraphList::iterator past_pit = boost::next(pit);
326 pars.splice(past_pit, simple_cut_clone);
327 ParagraphList::iterator last_paste = boost::prior(past_pit);
329 // If we only inserted one paragraph.
330 if (boost::next(pit) == last_paste)
333 mergeParagraph(buffer.params, pars, pit);
335 // Store the new cursor position.
337 pos = last_paste->size();
339 // Maybe some pasting.
340 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
341 if (boost::next(last_paste) != pars.end() &&
343 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
344 mergeParagraph(buffer.params, pars,
346 } else if (boost::next(last_paste)->empty()) {
347 boost::next(last_paste)->makeSameLayout(*last_paste);
348 mergeParagraph(buffer.params, pars,
350 } else if (last_paste->empty()) {
351 last_paste->makeSameLayout(*boost::next(last_paste));
352 mergeParagraph(buffer.params, pars,
355 boost::next(last_paste)->stripLeadingSpaces();
358 return make_pair(PitPosPair(pit, pos), endpit);
362 int CutAndPaste::nrOfParagraphs()
364 return cuts.empty() ? 0 : cuts[0].first.size();
368 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
370 ParagraphList & pars,
371 ErrorList & errorlist)
373 lyx::Assert(!pars.empty());
379 LyXTextClass const & tclass1 = textclasslist[c1];
380 LyXTextClass const & tclass2 = textclasslist[c2];
381 ParIterator end = ParIterator(pars.end(), pars);
382 for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
383 string const name = it->layout()->name();
384 bool hasLayout = tclass2.hasLayout(name);
387 it->layout(tclass2[name]);
389 it->layout(tclass2.defaultLayout());
391 if (!hasLayout && name != tclass1.defaultLayoutName()) {
393 string const s = bformat(
394 _("Layout had to be changed from\n%1$s to %2$s\n"
395 "because of class conversion from\n%3$s to %4$s"),
396 name, it->layout()->name(), tclass1.name(), tclass2.name());
397 // To warn the user that something had to be done.
398 errorlist.push_back(ErrorItem("Changed Layout", s,
407 bool CutAndPaste::checkPastePossible()
409 return !cuts.empty() && !cuts[0].first.empty();