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)
186 Assert(0 <= start && start <= startpit->size());
187 Assert(0 <= end && end <= endpit->size());
188 Assert(startpit != endpit || start <= end);
190 ParagraphList paragraphs;
192 // Clone the paragraphs within the selection.
193 ParagraphList::iterator postend = boost::next(endpit);
195 paragraphs.assign(startpit, postend);
196 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
198 // Cut out the end of the last paragraph.
199 Paragraph & back = paragraphs.back();
200 back.erase(end, back.size());
202 // Cut out the begin of the first paragraph
203 Paragraph & front = paragraphs.front();
204 front.erase(0, start);
206 cuts.push(make_pair(paragraphs, tc));
212 pair<PitPosPair, ParagraphList::iterator>
213 CutAndPaste::pasteSelection(Buffer const & buffer,
214 ParagraphList & pars,
215 ParagraphList::iterator pit, int pos,
217 ErrorList & errorlist)
219 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
223 pair<PitPosPair, ParagraphList::iterator>
224 CutAndPaste::pasteSelection(Buffer const & buffer,
225 ParagraphList & pars,
226 ParagraphList::iterator pit, int pos,
227 textclass_type tc, size_t cut_index,
228 ErrorList & errorlist)
230 if (!checkPastePossible())
231 return make_pair(PitPosPair(pit, pos), pit);
233 Assert (pos <= pit->size());
235 // Make a copy of the CaP paragraphs.
236 ParagraphList simple_cut_clone = cuts[cut_index].first;
237 textclass_type const textclass = cuts[cut_index].second;
239 // Now remove all out of the pars which is NOT allowed in the
240 // new environment and set also another font if that is required.
242 // Make sure there is no class difference.
243 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
246 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
247 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
249 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
251 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
252 // If we have a negative jump so that the depth would
253 // go below 0 depth then we have to redo the delta to
254 // this new max depth level so that subsequent
255 // paragraphs are aligned correctly to this paragraph
257 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
260 // Set the right depth so that we are not too deep or shallow.
261 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
262 if (tmpbuf->params().depth() > max_depth)
263 tmpbuf->params().depth(max_depth);
265 // Only set this from the 2nd on as the 2nd depends
266 // for maxDepth still on pit.
267 if (tmpbuf != simple_cut_clone.begin())
268 max_depth = tmpbuf->getMaxDepthAfter();
270 // Set the inset owner of this paragraph.
271 tmpbuf->setInsetOwner(pit->inInset());
272 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
273 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
274 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
278 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
280 if (!pit->checkInsertChar(f1)) {
282 } else if (f1 != f2) {
283 tmpbuf->setFont(i, f1);
289 // Make the buf exactly the same layout than
290 // the cursor paragraph.
291 simple_cut_clone.begin()->makeSameLayout(*pit);
293 // Prepare the paragraphs and insets for insertion
294 // A couple of insets store buffer references so need
296 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
297 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
299 for (; fpit != fend; ++fpit) {
300 InsetList::iterator lit = fpit->insetlist.begin();
301 InsetList::iterator eit = fpit->insetlist.end();
303 for (; lit != eit; ++lit) {
304 switch (lit->inset->lyxCode()) {
305 case Inset::INCLUDE_CODE: {
306 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
307 InsetInclude::Params ip = ii->params();
308 ip.masterFilename_ = buffer.fileName();
313 case Inset::TABULAR_CODE: {
314 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
315 it->buffer(const_cast<Buffer*>(&buffer));
325 bool paste_the_end = false;
327 // Open the paragraph for inserting the buf
329 if (pit->size() > pos || boost::next(pit) == pars.end()) {
330 breakParagraphConservative(buffer.params,
332 paste_the_end = true;
335 // Set the end for redoing later.
336 ParagraphList::iterator endpit = boost::next(boost::next(pit));
340 ParagraphList::iterator past_pit = boost::next(pit);
341 pars.splice(past_pit, simple_cut_clone);
342 ParagraphList::iterator last_paste = boost::prior(past_pit);
344 // If we only inserted one paragraph.
345 if (boost::next(pit) == last_paste)
348 mergeParagraph(buffer.params, pars, pit);
350 // Store the new cursor position.
352 pos = last_paste->size();
354 // Maybe some pasting.
355 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
356 if (boost::next(last_paste) != pars.end() &&
358 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
359 mergeParagraph(buffer.params, pars,
361 } else if (boost::next(last_paste)->empty()) {
362 boost::next(last_paste)->makeSameLayout(*last_paste);
363 mergeParagraph(buffer.params, pars,
365 } else if (last_paste->empty()) {
366 last_paste->makeSameLayout(*boost::next(last_paste));
367 mergeParagraph(buffer.params, pars,
370 boost::next(last_paste)->stripLeadingSpaces();
373 return make_pair(PitPosPair(pit, pos), endpit);
377 int CutAndPaste::nrOfParagraphs()
379 return cuts.empty() ? 0 : cuts[0].first.size();
383 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
385 ParagraphList & pars,
386 ErrorList & errorlist)
388 Assert(!pars.empty());
394 LyXTextClass const & tclass1 = textclasslist[c1];
395 LyXTextClass const & tclass2 = textclasslist[c2];
396 ParIterator end = ParIterator(pars.end(), pars);
397 for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
398 string const name = it->layout()->name();
399 bool hasLayout = tclass2.hasLayout(name);
402 it->layout(tclass2[name]);
404 it->layout(tclass2.defaultLayout());
406 if (!hasLayout && name != tclass1.defaultLayoutName()) {
408 string const s = bformat(
409 _("Layout had to be changed from\n%1$s to %2$s\n"
410 "because of class conversion from\n%3$s to %4$s"),
411 name, it->layout()->name(), tclass1.name(), tclass2.name());
412 // To warn the user that something had to be done.
413 errorlist.push_back(ErrorItem("Changed Layout", s,
422 bool CutAndPaste::checkPastePossible()
424 return !cuts.empty() && !cuts[0].first.empty();