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"
40 using lyx::textclass_type;
42 // Jürgen, note that this means that you cannot currently have a list
43 // of selections cut/copied. So IMHO later we should have a
44 // list/vector/deque that we could store
45 // struct selection_item {
46 // ParagraphList copy_pars;
47 // LyXTextClassList::size_type textclass;
49 // in and some method of choosing beween them (based on the first few chars
50 // in the selection probably.) This would be a nice feature and quite
51 // easy to implement. (Lgb)
53 // Sure but I just cleaned up this code for now with the same functionality
54 // as before. I also want to add a XClipboard function so that we can copy
55 // text from LyX to some other X-application in the form of ASCII or in the
56 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
57 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
58 // then do a middle mouse button click in the application you want and have
59 // the whole formula there in LaTeX-Code. (Jug)
63 limited_stack<pair<ParagraphList, textclass_type> > cuts(10);
67 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
69 ParagraphList::iterator startpit,
70 ParagraphList::iterator endpit,
71 int startpos, int endpos,
72 textclass_type tc, bool doclear)
74 copySelection(startpit, endpit, startpos, endpos, tc);
75 return eraseSelection(params, pars, startpit, endpit, startpos,
80 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
82 ParagraphList::iterator startpit,
83 ParagraphList::iterator endpit,
84 int startpos, int endpos, bool doclear)
86 if (startpit == pars.end() || (startpos > startpit->size()))
87 return PitPosPair(endpit, endpos);
89 if (endpit == pars.end() || startpit == endpit) {
90 endpos -= startpit->erase(startpos, endpos);
91 return PitPosPair(endpit, endpos);
94 // clear end/begin fragments of the first/last par in selection
95 bool all_erased = true;
97 startpit->erase(startpos, startpit->size());
98 if (startpit->size() != startpos)
101 endpos -= endpit->erase(0, endpos);
105 // Loop through the deleted pars if any, erasing as needed
107 ParagraphList::iterator pit = boost::next(startpit);
109 while (pit != endpit && pit != pars.end()) {
110 ParagraphList::iterator const next = boost::next(pit);
111 // "erase" the contents of the par
112 pit->erase(0, pit->size());
114 // remove the par if it's now empty
121 #if 0 // FIXME: why for cut but not copy ?
122 // the cut selection should begin with standard layout
124 buf->params().clear();
126 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
130 if (boost::next(startpit) == pars.end())
131 return PitPosPair(endpit, endpos);
134 boost::next(startpit)->stripLeadingSpaces();
137 // paste the paragraphs again, if possible
139 (startpit->hasSameLayout(*boost::next(startpit)) ||
140 boost::next(startpit)->empty())) {
141 mergeParagraph(params, pars, startpit);
142 // this because endpar gets deleted here!
147 return PitPosPair(endpit, endpos);
154 struct resetOwnerAndChanges {
155 void operator()(Paragraph & p) {
163 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
164 ParagraphList::iterator endpit,
165 int start, int end, textclass_type tc)
167 lyx::Assert(&*startpit);
168 lyx::Assert(&*endpit);
169 lyx::Assert(0 <= start && start <= startpit->size());
170 lyx::Assert(0 <= end && end <= endpit->size());
171 lyx::Assert(startpit != endpit || start <= end);
173 ParagraphList paragraphs;
175 // Clone the paragraphs within the selection.
176 ParagraphList::iterator postend = boost::next(endpit);
178 paragraphs.assign(startpit, postend);
179 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
181 // Cut out the end of the last paragraph.
182 Paragraph & back = paragraphs.back();
183 back.erase(end, back.size());
185 // Cut out the begin of the first paragraph
186 Paragraph & front = paragraphs.front();
187 front.erase(0, start);
189 cuts.push(make_pair(paragraphs, tc));
195 pair<PitPosPair, ParagraphList::iterator>
196 CutAndPaste::pasteSelection(Buffer const & buffer,
197 ParagraphList & pars,
198 ParagraphList::iterator pit, int pos,
200 ErrorList & errorlist)
202 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
205 pair<PitPosPair, ParagraphList::iterator>
206 CutAndPaste::pasteSelection(Buffer const & buffer,
207 ParagraphList & pars,
208 ParagraphList::iterator pit, int pos,
209 textclass_type tc, size_t cut_index,
210 ErrorList & errorlist)
212 if (!checkPastePossible())
213 return make_pair(PitPosPair(pit, pos), pit);
215 lyx::Assert (pos <= pit->size());
217 // Make a copy of the CaP paragraphs.
218 ParagraphList simple_cut_clone = cuts[cut_index].first;
219 textclass_type const textclass = cuts[cut_index].second;
221 // Now remove all out of the pars which is NOT allowed in the
222 // new environment and set also another font if that is required.
224 // Make sure there is no class difference.
225 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
228 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
229 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
231 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
233 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
234 // If we have a negative jump so that the depth would
235 // go below 0 depth then we have to redo the delta to
236 // this new max depth level so that subsequent
237 // paragraphs are aligned correctly to this paragraph
239 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
242 // Set the right depth so that we are not too deep or shallow.
243 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
244 if (tmpbuf->params().depth() > max_depth)
245 tmpbuf->params().depth(max_depth);
247 // Only set this from the 2nd on as the 2nd depends
248 // for maxDepth still on pit.
249 if (tmpbuf != simple_cut_clone.begin())
250 max_depth = tmpbuf->getMaxDepthAfter();
252 // Set the inset owner of this paragraph.
253 tmpbuf->setInsetOwner(pit->inInset());
254 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
255 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
256 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
260 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
262 if (!pit->checkInsertChar(f1)) {
264 } else if (f1 != f2) {
265 tmpbuf->setFont(i, f1);
271 // Make the buf exactly the same layout than
272 // the cursor paragraph.
273 simple_cut_clone.begin()->makeSameLayout(*pit);
275 // Prepare the paragraphs and insets for insertion
276 // A couple of insets store buffer references so need
278 ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
279 ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
281 for (; fpit != fend; ++fpit) {
282 InsetList::iterator lit = fpit->insetlist.begin();
283 InsetList::iterator eit = fpit->insetlist.end();
285 for (; lit != eit; ++lit) {
286 switch (lit->inset->lyxCode()) {
287 case Inset::INCLUDE_CODE: {
288 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
289 InsetInclude::Params ip = ii->params();
290 ip.masterFilename_ = buffer.fileName();
295 case Inset::TABULAR_CODE: {
296 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
297 it->buffer(const_cast<Buffer*>(&buffer));
307 bool paste_the_end = false;
309 // Open the paragraph for inserting the buf
311 if (pit->size() > pos || boost::next(pit) == pars.end()) {
312 breakParagraphConservative(buffer.params,
314 paste_the_end = true;
317 // Set the end for redoing later.
318 ParagraphList::iterator endpit = boost::next(boost::next(pit));
322 ParagraphList::iterator past_pit = boost::next(pit);
323 pars.splice(past_pit, simple_cut_clone);
324 ParagraphList::iterator last_paste = boost::prior(past_pit);
326 // If we only inserted one paragraph.
327 if (boost::next(pit) == last_paste)
330 mergeParagraph(buffer.params, pars, pit);
332 // Store the new cursor position.
334 pos = last_paste->size();
336 // Maybe some pasting.
337 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
338 if (boost::next(last_paste) != pars.end() &&
340 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
341 mergeParagraph(buffer.params, pars,
343 } else if (boost::next(last_paste)->empty()) {
344 boost::next(last_paste)->makeSameLayout(*last_paste);
345 mergeParagraph(buffer.params, pars,
347 } else if (last_paste->empty()) {
348 last_paste->makeSameLayout(*boost::next(last_paste));
349 mergeParagraph(buffer.params, pars,
352 boost::next(last_paste)->stripLeadingSpaces();
355 return make_pair(PitPosPair(pit, pos), endpit);
359 int CutAndPaste::nrOfParagraphs()
361 return cuts.empty() ? 0 : cuts[0].first.size();
365 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
367 ParagraphList & pars,
368 ErrorList & errorlist)
370 lyx::Assert(!pars.empty());
376 LyXTextClass const & tclass1 = textclasslist[c1];
377 LyXTextClass const & tclass2 = textclasslist[c2];
378 ParIterator end = ParIterator(pars.end(), pars);
379 for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
380 Paragraph * par = &*(*it);
381 string const name = par->layout()->name();
382 bool hasLayout = tclass2.hasLayout(name);
385 par->layout(tclass2[name]);
387 par->layout(tclass2.defaultLayout());
389 if (!hasLayout && name != tclass1.defaultLayoutName()) {
391 string const s = bformat(
392 _("Layout had to be changed from\n%1$s to %2$s\n"
393 "because of class conversion from\n%3$s to %4$s"),
394 name, par->layout()->name(), tclass1.name(), tclass2.name());
395 // To warn the user that something had to be done.
396 errorlist.push_back(ErrorItem("Changed Layout", s,
405 bool CutAndPaste::checkPastePossible()
407 return !cuts.empty() && !cuts[0].first.empty();