3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
15 #include "CutAndPaste.h"
18 #include "bufferparams.h"
19 #include "errorlist.h"
21 #include "iterators.h"
22 #include "lyxtextclasslist.h"
23 #include "paragraph.h"
24 #include "paragraph_funcs.h"
25 #include "ParagraphParameters.h"
27 #include "insets/insettabular.h"
29 #include "support/lstrings.h"
32 using lyx::textclass_type;
34 using lyx::support::bformat;
43 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
52 std::vector<string> const
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 // we do not use cit-> here because gcc 2.9x does not
62 ParagraphList const & pars = (*cit).first;
64 ParagraphList::const_iterator pit = pars.begin();
65 ParagraphList::const_iterator pend = pars.end();
66 for (; pit != pend; ++pit) {
67 asciiSel += pit->asString(buffer, false);
68 if (asciiSel.size() > 25) {
69 asciiSel.replace(22, string::npos, "...");
74 selList.push_back(asciiSel);
81 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
83 ParagraphList::iterator startpit,
84 ParagraphList::iterator endpit,
85 int startpos, int endpos,
86 textclass_type tc, bool doclear)
88 copySelection(startpit, endpit, startpos, endpos, tc);
89 return eraseSelection(params, pars, startpit, endpit, startpos,
94 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
96 ParagraphList::iterator startpit,
97 ParagraphList::iterator endpit,
98 int startpos, int endpos, bool doclear)
100 if (startpit == pars.end() || (startpos > startpit->size()))
101 return PitPosPair(endpit, endpos);
103 if (endpit == pars.end() || startpit == endpit) {
104 endpos -= startpit->erase(startpos, endpos);
105 return PitPosPair(endpit, endpos);
108 // clear end/begin fragments of the first/last par in selection
109 bool all_erased = true;
111 startpit->erase(startpos, startpit->size());
112 if (startpit->size() != startpos)
115 endpos -= endpit->erase(0, endpos);
119 // Loop through the deleted pars if any, erasing as needed
121 ParagraphList::iterator pit = boost::next(startpit);
123 while (pit != endpit && pit != pars.end()) {
124 ParagraphList::iterator const next = boost::next(pit);
125 // "erase" the contents of the par
126 pit->erase(0, pit->size());
128 // remove the par if it's now empty
135 #if 0 // FIXME: why for cut but not copy ?
136 // the cut selection should begin with standard layout
138 buf->params().clear();
140 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
144 if (boost::next(startpit) == pars.end())
145 return PitPosPair(endpit, endpos);
148 boost::next(startpit)->stripLeadingSpaces();
151 // paste the paragraphs again, if possible
153 (startpit->hasSameLayout(*boost::next(startpit)) ||
154 boost::next(startpit)->empty())) {
155 mergeParagraph(params, pars, startpit);
156 // this because endpar gets deleted here!
161 return PitPosPair(endpit, endpos);
168 struct resetOwnerAndChanges {
169 void operator()(Paragraph & p) {
177 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
178 ParagraphList::iterator endpit,
179 int start, int end, textclass_type tc)
181 BOOST_ASSERT(0 <= start && start <= startpit->size());
182 BOOST_ASSERT(0 <= end && end <= endpit->size());
183 BOOST_ASSERT(startpit != endpit || start <= end);
186 // Clone the paragraphs within the selection.
187 ParagraphList::iterator postend = boost::next(endpit);
189 ParagraphList paragraphs(startpit, postend);
190 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
192 // Cut out the end of the last paragraph.
193 Paragraph & back = paragraphs.back();
194 back.erase(end, back.size());
196 // Cut out the begin of the first paragraph
197 Paragraph & front = paragraphs.front();
198 front.erase(0, start);
200 cuts.push(make_pair(paragraphs, tc));
206 pair<PitPosPair, ParagraphList::iterator>
207 CutAndPaste::pasteSelection(Buffer const & buffer,
208 ParagraphList & pars,
209 ParagraphList::iterator pit, int pos,
211 ErrorList & errorlist)
213 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
217 pair<PitPosPair, ParagraphList::iterator>
218 CutAndPaste::pasteSelection(Buffer const & buffer,
219 ParagraphList & pars,
220 ParagraphList::iterator pit, int pos,
221 textclass_type tc, size_t cut_index,
222 ErrorList & errorlist)
224 if (!checkPastePossible())
225 return make_pair(PitPosPair(pit, pos), pit);
227 BOOST_ASSERT (pos <= pit->size());
229 // Make a copy of the CaP paragraphs.
230 ParagraphList simple_cut_clone = cuts[cut_index].first;
231 textclass_type const textclass = cuts[cut_index].second;
233 // Now remove all out of the pars which is NOT allowed in the
234 // new environment and set also another font if that is required.
236 // Make sure there is no class difference.
237 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
240 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
241 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
243 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
245 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
246 // If we have a negative jump so that the depth would
247 // go below 0 depth then we have to redo the delta to
248 // this new max depth level so that subsequent
249 // paragraphs are aligned correctly to this paragraph
251 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
254 // Set the right depth so that we are not too deep or shallow.
255 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
256 if (tmpbuf->params().depth() > max_depth)
257 tmpbuf->params().depth(max_depth);
259 // Only set this from the 2nd on as the 2nd depends
260 // for maxDepth still on pit.
261 if (tmpbuf != simple_cut_clone.begin())
262 max_depth = tmpbuf->getMaxDepthAfter();
264 // Set the inset owner of this paragraph.
265 tmpbuf->setInsetOwner(pit->inInset());
266 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
267 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
268 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
272 LyXFont f1 = tmpbuf->getFont(buffer.params(), i, outerFont(pit, pars));
274 if (!pit->checkInsertChar(f1)) {
276 } else if (f1 != f2) {
277 tmpbuf->setFont(i, f1);
283 // Make the buf exactly the same layout than
284 // the cursor paragraph.
285 simple_cut_clone.begin()->makeSameLayout(*pit);
287 // Prepare the paragraphs and insets for insertion
288 // A couple of insets store buffer references so need
290 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
291 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
293 for (; fpit != fend; ++fpit) {
294 InsetList::iterator lit = fpit->insetlist.begin();
295 InsetList::iterator eit = fpit->insetlist.end();
297 for (; lit != eit; ++lit) {
298 switch (lit->inset->lyxCode()) {
299 case InsetOld::TABULAR_CODE: {
300 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
301 it->buffer(const_cast<Buffer*>(&buffer));
311 bool paste_the_end = false;
313 // Open the paragraph for inserting the buf
315 if (pit->size() > pos || boost::next(pit) == pars.end()) {
316 breakParagraphConservative(buffer.params(),
318 paste_the_end = true;
321 // Set the end for redoing later.
322 ParagraphList::iterator endpit = boost::next(boost::next(pit));
326 ParagraphList::iterator past_pit = boost::next(pit);
327 pars.splice(past_pit, simple_cut_clone);
328 ParagraphList::iterator last_paste = boost::prior(past_pit);
330 // If we only inserted one paragraph.
331 if (boost::next(pit) == last_paste)
334 mergeParagraph(buffer.params(), pars, pit);
336 // Store the new cursor position.
338 pos = last_paste->size();
340 // Maybe some pasting.
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 BOOST_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();