3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Juergen Vigna
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
15 #include "CutAndPaste.h"
16 #include "BufferView.h"
18 #include "errorlist.h"
19 #include "paragraph.h"
20 #include "ParagraphParameters.h"
22 #include "lyxcursor.h"
23 #include "iterators.h"
24 #include "lyxtextclasslist.h"
25 #include "undo_funcs.h"
27 #include "paragraph_funcs.h"
29 #include "insets/insetinclude.h"
30 #include "insets/insettabular.h"
32 #include "support/LAssert.h"
33 #include "support/lstrings.h"
34 #include "support/limited_stack.h"
42 using namespace lyx::support;
44 using lyx::textclass_type;
47 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
57 CutAndPaste::availableSelections(Buffer const & buffer)
59 vector<string> selList;
61 CutStack::const_iterator cit = cuts.begin();
62 CutStack::const_iterator end = cuts.end();
63 for (; cit != end; ++cit) {
64 // we do not use cit-> here because gcc 2.9x does not
66 ParagraphList const & pars = (*cit).first;
68 ParagraphList::const_iterator pit = pars.begin();
69 ParagraphList::const_iterator pend = pars.end();
70 for (; pit != pend; ++pit) {
71 asciiSel += pit->asString(&buffer, false);
72 if (asciiSel.size() > 25) {
73 asciiSel.replace(22, string::npos, "...");
78 selList.push_back(asciiSel);
85 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
87 ParagraphList::iterator startpit,
88 ParagraphList::iterator endpit,
89 int startpos, int endpos,
90 textclass_type tc, bool doclear)
92 copySelection(startpit, endpit, startpos, endpos, tc);
93 return eraseSelection(params, pars, startpit, endpit, startpos,
98 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
100 ParagraphList::iterator startpit,
101 ParagraphList::iterator endpit,
102 int startpos, int endpos, bool doclear)
104 if (startpit == pars.end() || (startpos > startpit->size()))
105 return PitPosPair(endpit, endpos);
107 if (endpit == pars.end() || startpit == endpit) {
108 endpos -= startpit->erase(startpos, endpos);
109 return PitPosPair(endpit, endpos);
112 // clear end/begin fragments of the first/last par in selection
113 bool all_erased = true;
115 startpit->erase(startpos, startpit->size());
116 if (startpit->size() != startpos)
119 endpos -= endpit->erase(0, endpos);
123 // Loop through the deleted pars if any, erasing as needed
125 ParagraphList::iterator pit = boost::next(startpit);
127 while (pit != endpit && pit != pars.end()) {
128 ParagraphList::iterator const next = boost::next(pit);
129 // "erase" the contents of the par
130 pit->erase(0, pit->size());
132 // remove the par if it's now empty
139 #if 0 // FIXME: why for cut but not copy ?
140 // the cut selection should begin with standard layout
142 buf->params().clear();
144 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
148 if (boost::next(startpit) == pars.end())
149 return PitPosPair(endpit, endpos);
152 boost::next(startpit)->stripLeadingSpaces();
155 // paste the paragraphs again, if possible
157 (startpit->hasSameLayout(*boost::next(startpit)) ||
158 boost::next(startpit)->empty())) {
159 mergeParagraph(params, pars, startpit);
160 // this because endpar gets deleted here!
165 return PitPosPair(endpit, endpos);
172 struct resetOwnerAndChanges {
173 void operator()(Paragraph & p) {
181 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
182 ParagraphList::iterator endpit,
183 int start, int end, textclass_type tc)
185 Assert(0 <= start && start <= startpit->size());
186 Assert(0 <= end && end <= endpit->size());
187 Assert(startpit != endpit || start <= end);
189 ParagraphList paragraphs;
191 // Clone the paragraphs within the selection.
192 ParagraphList::iterator postend = boost::next(endpit);
194 paragraphs.assign(startpit, postend);
195 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
197 // Cut out the end of the last paragraph.
198 Paragraph & back = paragraphs.back();
199 back.erase(end, back.size());
201 // Cut out the begin of the first paragraph
202 Paragraph & front = paragraphs.front();
203 front.erase(0, start);
205 cuts.push(make_pair(paragraphs, tc));
211 pair<PitPosPair, ParagraphList::iterator>
212 CutAndPaste::pasteSelection(Buffer const & buffer,
213 ParagraphList & pars,
214 ParagraphList::iterator pit, int pos,
216 ErrorList & errorlist)
218 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
222 pair<PitPosPair, ParagraphList::iterator>
223 CutAndPaste::pasteSelection(Buffer const & buffer,
224 ParagraphList & pars,
225 ParagraphList::iterator pit, int pos,
226 textclass_type tc, size_t cut_index,
227 ErrorList & errorlist)
229 if (!checkPastePossible())
230 return make_pair(PitPosPair(pit, pos), pit);
232 Assert (pos <= pit->size());
234 // Make a copy of the CaP paragraphs.
235 ParagraphList simple_cut_clone = cuts[cut_index].first;
236 textclass_type const textclass = cuts[cut_index].second;
238 // Now remove all out of the pars which is NOT allowed in the
239 // new environment and set also another font if that is required.
241 // Make sure there is no class difference.
242 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
245 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
246 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
248 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
250 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
251 // If we have a negative jump so that the depth would
252 // go below 0 depth then we have to redo the delta to
253 // this new max depth level so that subsequent
254 // paragraphs are aligned correctly to this paragraph
256 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
259 // Set the right depth so that we are not too deep or shallow.
260 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
261 if (tmpbuf->params().depth() > max_depth)
262 tmpbuf->params().depth(max_depth);
264 // Only set this from the 2nd on as the 2nd depends
265 // for maxDepth still on pit.
266 if (tmpbuf != simple_cut_clone.begin())
267 max_depth = tmpbuf->getMaxDepthAfter();
269 // Set the inset owner of this paragraph.
270 tmpbuf->setInsetOwner(pit->inInset());
271 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
272 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
273 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
277 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
279 if (!pit->checkInsertChar(f1)) {
281 } else if (f1 != f2) {
282 tmpbuf->setFont(i, f1);
288 // Make the buf exactly the same layout than
289 // the cursor paragraph.
290 simple_cut_clone.begin()->makeSameLayout(*pit);
292 // Prepare the paragraphs and insets for insertion
293 // A couple of insets store buffer references so need
295 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
296 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
298 for (; fpit != fend; ++fpit) {
299 InsetList::iterator lit = fpit->insetlist.begin();
300 InsetList::iterator eit = fpit->insetlist.end();
302 for (; lit != eit; ++lit) {
303 switch (lit->inset->lyxCode()) {
304 case InsetOld::INCLUDE_CODE: {
305 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
306 InsetInclude::Params ip = ii->params();
307 ip.masterFilename_ = buffer.fileName();
312 case InsetOld::TABULAR_CODE: {
313 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
314 it->buffer(const_cast<Buffer*>(&buffer));
324 bool paste_the_end = false;
326 // Open the paragraph for inserting the buf
328 if (pit->size() > pos || boost::next(pit) == pars.end()) {
329 breakParagraphConservative(buffer.params,
331 paste_the_end = true;
334 // Set the end for redoing later.
335 ParagraphList::iterator endpit = boost::next(boost::next(pit));
339 ParagraphList::iterator past_pit = boost::next(pit);
340 pars.splice(past_pit, simple_cut_clone);
341 ParagraphList::iterator last_paste = boost::prior(past_pit);
343 // If we only inserted one paragraph.
344 if (boost::next(pit) == last_paste)
347 mergeParagraph(buffer.params, pars, pit);
349 // Store the new cursor position.
351 pos = last_paste->size();
353 // Maybe some pasting.
354 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
355 if (boost::next(last_paste) != pars.end() &&
357 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
358 mergeParagraph(buffer.params, pars,
360 } else if (boost::next(last_paste)->empty()) {
361 boost::next(last_paste)->makeSameLayout(*last_paste);
362 mergeParagraph(buffer.params, pars,
364 } else if (last_paste->empty()) {
365 last_paste->makeSameLayout(*boost::next(last_paste));
366 mergeParagraph(buffer.params, pars,
369 boost::next(last_paste)->stripLeadingSpaces();
372 return make_pair(PitPosPair(pit, pos), endpit);
376 int CutAndPaste::nrOfParagraphs()
378 return cuts.empty() ? 0 : cuts[0].first.size();
382 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
384 ParagraphList & pars,
385 ErrorList & errorlist)
387 Assert(!pars.empty());
393 LyXTextClass const & tclass1 = textclasslist[c1];
394 LyXTextClass const & tclass2 = textclasslist[c2];
395 ParIterator end = ParIterator(pars.end(), pars);
396 for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
397 string const name = it->layout()->name();
398 bool hasLayout = tclass2.hasLayout(name);
401 it->layout(tclass2[name]);
403 it->layout(tclass2.defaultLayout());
405 if (!hasLayout && name != tclass1.defaultLayoutName()) {
407 string const s = bformat(
408 _("Layout had to be changed from\n%1$s to %2$s\n"
409 "because of class conversion from\n%3$s to %4$s"),
410 name, it->layout()->name(), tclass1.name(), tclass2.name());
411 // To warn the user that something had to be done.
412 errorlist.push_back(ErrorItem("Changed Layout", s,
421 bool CutAndPaste::checkPastePossible()
423 return !cuts.empty() && !cuts[0].first.empty();