2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
5 * \author Juergen Vigna
6 * \author Lars Gullik Bjønnes
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS
14 #include "CutAndPaste.h"
15 #include "BufferView.h"
17 #include "errorlist.h"
18 #include "paragraph.h"
19 #include "ParagraphParameters.h"
21 #include "lyxcursor.h"
22 #include "iterators.h"
23 #include "lyxtextclasslist.h"
24 #include "undo_funcs.h"
26 #include "paragraph_funcs.h"
28 #include "insets/insetinclude.h"
29 #include "insets/insettabular.h"
31 #include "support/LAssert.h"
32 #include "support/lstrings.h"
33 #include "support/limited_stack.h"
41 using namespace lyx::support;
43 using lyx::textclass_type;
46 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
56 CutAndPaste::availableSelections(Buffer const & buffer)
58 vector<string> selList;
60 CutStack::const_iterator cit = cuts.begin();
61 CutStack::const_iterator end = cuts.end();
62 for (; cit != end; ++cit) {
63 // we do not use cit-> here because gcc 2.9x does not
65 ParagraphList const & pars = (*cit).first;
67 ParagraphList::const_iterator pit = pars.begin();
68 ParagraphList::const_iterator pend = pars.end();
69 for (; pit != pend; ++pit) {
70 asciiSel += pit->asString(&buffer, false);
71 if (asciiSel.size() > 25) {
72 asciiSel.replace(22, string::npos, "...");
77 selList.push_back(asciiSel);
84 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
86 ParagraphList::iterator startpit,
87 ParagraphList::iterator endpit,
88 int startpos, int endpos,
89 textclass_type tc, bool doclear)
91 copySelection(startpit, endpit, startpos, endpos, tc);
92 return eraseSelection(params, pars, startpit, endpit, startpos,
97 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
99 ParagraphList::iterator startpit,
100 ParagraphList::iterator endpit,
101 int startpos, int endpos, bool doclear)
103 if (startpit == pars.end() || (startpos > startpit->size()))
104 return PitPosPair(endpit, endpos);
106 if (endpit == pars.end() || startpit == endpit) {
107 endpos -= startpit->erase(startpos, endpos);
108 return PitPosPair(endpit, endpos);
111 // clear end/begin fragments of the first/last par in selection
112 bool all_erased = true;
114 startpit->erase(startpos, startpit->size());
115 if (startpit->size() != startpos)
118 endpos -= endpit->erase(0, endpos);
122 // Loop through the deleted pars if any, erasing as needed
124 ParagraphList::iterator pit = boost::next(startpit);
126 while (pit != endpit && pit != pars.end()) {
127 ParagraphList::iterator const next = boost::next(pit);
128 // "erase" the contents of the par
129 pit->erase(0, pit->size());
131 // remove the par if it's now empty
138 #if 0 // FIXME: why for cut but not copy ?
139 // the cut selection should begin with standard layout
141 buf->params().clear();
143 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
147 if (boost::next(startpit) == pars.end())
148 return PitPosPair(endpit, endpos);
151 boost::next(startpit)->stripLeadingSpaces();
154 // paste the paragraphs again, if possible
156 (startpit->hasSameLayout(*boost::next(startpit)) ||
157 boost::next(startpit)->empty())) {
158 mergeParagraph(params, pars, startpit);
159 // this because endpar gets deleted here!
164 return PitPosPair(endpit, endpos);
171 struct resetOwnerAndChanges {
172 void operator()(Paragraph & p) {
180 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
181 ParagraphList::iterator endpit,
182 int start, int end, textclass_type tc)
184 Assert(0 <= start && start <= startpit->size());
185 Assert(0 <= end && end <= endpit->size());
186 Assert(startpit != endpit || start <= end);
188 ParagraphList paragraphs;
190 // Clone the paragraphs within the selection.
191 ParagraphList::iterator postend = boost::next(endpit);
193 paragraphs.assign(startpit, postend);
194 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
196 // Cut out the end of the last paragraph.
197 Paragraph & back = paragraphs.back();
198 back.erase(end, back.size());
200 // Cut out the begin of the first paragraph
201 Paragraph & front = paragraphs.front();
202 front.erase(0, start);
204 cuts.push(make_pair(paragraphs, tc));
210 pair<PitPosPair, ParagraphList::iterator>
211 CutAndPaste::pasteSelection(Buffer const & buffer,
212 ParagraphList & pars,
213 ParagraphList::iterator pit, int pos,
215 ErrorList & errorlist)
217 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
221 pair<PitPosPair, ParagraphList::iterator>
222 CutAndPaste::pasteSelection(Buffer const & buffer,
223 ParagraphList & pars,
224 ParagraphList::iterator pit, int pos,
225 textclass_type tc, size_t cut_index,
226 ErrorList & errorlist)
228 if (!checkPastePossible())
229 return make_pair(PitPosPair(pit, pos), pit);
231 Assert (pos <= pit->size());
233 // Make a copy of the CaP paragraphs.
234 ParagraphList simple_cut_clone = cuts[cut_index].first;
235 textclass_type const textclass = cuts[cut_index].second;
237 // Now remove all out of the pars which is NOT allowed in the
238 // new environment and set also another font if that is required.
240 // Make sure there is no class difference.
241 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
244 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
245 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
247 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
249 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
250 // If we have a negative jump so that the depth would
251 // go below 0 depth then we have to redo the delta to
252 // this new max depth level so that subsequent
253 // paragraphs are aligned correctly to this paragraph
255 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
258 // Set the right depth so that we are not too deep or shallow.
259 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
260 if (tmpbuf->params().depth() > max_depth)
261 tmpbuf->params().depth(max_depth);
263 // Only set this from the 2nd on as the 2nd depends
264 // for maxDepth still on pit.
265 if (tmpbuf != simple_cut_clone.begin())
266 max_depth = tmpbuf->getMaxDepthAfter();
268 // Set the inset owner of this paragraph.
269 tmpbuf->setInsetOwner(pit->inInset());
270 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
271 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
272 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
276 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
278 if (!pit->checkInsertChar(f1)) {
280 } else if (f1 != f2) {
281 tmpbuf->setFont(i, f1);
287 // Make the buf exactly the same layout than
288 // the cursor paragraph.
289 simple_cut_clone.begin()->makeSameLayout(*pit);
291 // Prepare the paragraphs and insets for insertion
292 // A couple of insets store buffer references so need
294 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
295 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
297 for (; fpit != fend; ++fpit) {
298 InsetList::iterator lit = fpit->insetlist.begin();
299 InsetList::iterator eit = fpit->insetlist.end();
301 for (; lit != eit; ++lit) {
302 switch (lit->inset->lyxCode()) {
303 case InsetOld::INCLUDE_CODE: {
304 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
305 InsetInclude::Params ip = ii->params();
306 ip.masterFilename_ = buffer.fileName();
311 case InsetOld::TABULAR_CODE: {
312 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
313 it->buffer(const_cast<Buffer*>(&buffer));
323 bool paste_the_end = false;
325 // Open the paragraph for inserting the buf
327 if (pit->size() > pos || boost::next(pit) == pars.end()) {
328 breakParagraphConservative(buffer.params,
330 paste_the_end = true;
333 // Set the end for redoing later.
334 ParagraphList::iterator endpit = boost::next(boost::next(pit));
338 ParagraphList::iterator past_pit = boost::next(pit);
339 pars.splice(past_pit, simple_cut_clone);
340 ParagraphList::iterator last_paste = boost::prior(past_pit);
342 // If we only inserted one paragraph.
343 if (boost::next(pit) == last_paste)
346 mergeParagraph(buffer.params, pars, pit);
348 // Store the new cursor position.
350 pos = last_paste->size();
352 // Maybe some pasting.
353 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
354 if (boost::next(last_paste) != pars.end() &&
356 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
357 mergeParagraph(buffer.params, pars,
359 } else if (boost::next(last_paste)->empty()) {
360 boost::next(last_paste)->makeSameLayout(*last_paste);
361 mergeParagraph(buffer.params, pars,
363 } else if (last_paste->empty()) {
364 last_paste->makeSameLayout(*boost::next(last_paste));
365 mergeParagraph(buffer.params, pars,
368 boost::next(last_paste)->stripLeadingSpaces();
371 return make_pair(PitPosPair(pit, pos), endpit);
375 int CutAndPaste::nrOfParagraphs()
377 return cuts.empty() ? 0 : cuts[0].first.size();
381 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
383 ParagraphList & pars,
384 ErrorList & errorlist)
386 Assert(!pars.empty());
392 LyXTextClass const & tclass1 = textclasslist[c1];
393 LyXTextClass const & tclass2 = textclasslist[c2];
394 ParIterator end = ParIterator(pars.end(), pars);
395 for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
396 string const name = it->layout()->name();
397 bool hasLayout = tclass2.hasLayout(name);
400 it->layout(tclass2[name]);
402 it->layout(tclass2.defaultLayout());
404 if (!hasLayout && name != tclass1.defaultLayoutName()) {
406 string const s = bformat(
407 _("Layout had to be changed from\n%1$s to %2$s\n"
408 "because of class conversion from\n%3$s to %4$s"),
409 name, it->layout()->name(), tclass1.name(), tclass2.name());
410 // To warn the user that something had to be done.
411 errorlist.push_back(ErrorItem("Changed Layout", s,
420 bool CutAndPaste::checkPastePossible()
422 return !cuts.empty() && !cuts[0].first.empty();