]> git.lyx.org Git - features.git/blob - src/CutAndPaste.C
54ae59110bd5089a809690c9e2c16ca81bd908f8
[features.git] / src / CutAndPaste.C
1 /* \file CutAndPaste.C
2  * This file is part of LyX, the document processor.
3  * Licence details can be found in the file COPYING.
4  *
5  * \author Juergen Vigna
6  * \author Lars Gullik Bjønnes
7  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "CutAndPaste.h"
15 #include "BufferView.h"
16 #include "buffer.h"
17 #include "errorlist.h"
18 #include "paragraph.h"
19 #include "ParagraphParameters.h"
20 #include "lyxtext.h"
21 #include "lyxcursor.h"
22 #include "iterators.h"
23 #include "lyxtextclasslist.h"
24 #include "undo_funcs.h"
25 #include "gettext.h"
26 #include "paragraph_funcs.h"
27 #include "debug.h"
28 #include "insets/insetinclude.h"
29 #include "insets/insettabular.h"
30
31 #include "support/LAssert.h"
32 #include "support/lstrings.h"
33 #include "support/limited_stack.h"
34
35 using std::endl;
36 using std::pair;
37 using std::make_pair;
38 using std::for_each;
39 using std::vector;
40
41 using lyx::pos_type;
42 using lyx::textclass_type;
43
44
45 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
46
47 namespace {
48
49 CutStack cuts(10);
50
51 } // namespace anon
52
53
54 std::vector<string>
55 CutAndPaste::availableSelections(Buffer const & buffer)
56 {
57         vector<string> selList;
58
59         CutStack::const_iterator cit = cuts.begin();
60         CutStack::const_iterator end = cuts.end();
61         for (; cit != end; ++cit) {
62                 // we do not use cit-> here because gcc 2.9x does not
63                 // like it (JMarc)
64                 ParagraphList const & pars = (*cit).first;
65                 string asciiSel;
66                 ParagraphList::const_iterator pit = pars.begin();
67                 ParagraphList::const_iterator pend = pars.end();
68                 for (; pit != pend; ++pit) {
69                         asciiSel += pit->asString(&buffer, false);
70                         if (asciiSel.size() > 25) {
71                                 asciiSel.replace(22, string::npos, "...");
72                                 break;
73                         }
74                 }
75
76                 selList.push_back(asciiSel);
77         }
78
79         return selList;
80 }
81
82
83 PitPosPair CutAndPaste::cutSelection(BufferParams const & params,
84                                      ParagraphList & pars,
85                                      ParagraphList::iterator startpit,
86                                      ParagraphList::iterator endpit,
87                                      int startpos, int endpos,
88                                      textclass_type tc, bool doclear)
89 {
90         copySelection(startpit, endpit, startpos, endpos, tc);
91         return eraseSelection(params, pars, startpit, endpit, startpos,
92                               endpos, doclear);
93 }
94
95
96 PitPosPair CutAndPaste::eraseSelection(BufferParams const & params,
97                                        ParagraphList & pars,
98                                        ParagraphList::iterator startpit,
99                                        ParagraphList::iterator endpit,
100                                        int startpos, int endpos, bool doclear)
101 {
102         if (startpit == pars.end() || (startpos > startpit->size()))
103                 return PitPosPair(endpit, endpos);
104
105         if (endpit == pars.end() || startpit == endpit) {
106                 endpos -= startpit->erase(startpos, endpos);
107                 return PitPosPair(endpit, endpos);
108         }
109
110         // clear end/begin fragments of the first/last par in selection
111         bool all_erased = true;
112
113         startpit->erase(startpos, startpit->size());
114         if (startpit->size() != startpos)
115                 all_erased = false;
116
117         endpos -= endpit->erase(0, endpos);
118         if (endpos != 0)
119                 all_erased = false;
120
121         // Loop through the deleted pars if any, erasing as needed
122
123         ParagraphList::iterator pit = boost::next(startpit);
124
125         while (pit != endpit && pit != pars.end()) {
126                 ParagraphList::iterator const next = boost::next(pit);
127                 // "erase" the contents of the par
128                 pit->erase(0, pit->size());
129                 if (!pit->size()) {
130                         // remove the par if it's now empty
131                         pars.erase(pit);
132                 } else
133                         all_erased = false;
134                 pit = next;
135         }
136
137 #if 0 // FIXME: why for cut but not copy ?
138         // the cut selection should begin with standard layout
139         if (realcut) {
140                 buf->params().clear();
141                 buf->bibkey = 0;
142                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
143         }
144 #endif
145
146         if (boost::next(startpit) == pars.end())
147                 return PitPosPair(endpit, endpos);
148
149         if (doclear) {
150                 boost::next(startpit)->stripLeadingSpaces();
151         }
152
153         // paste the paragraphs again, if possible
154         if (all_erased &&
155             (startpit->hasSameLayout(*boost::next(startpit)) ||
156              boost::next(startpit)->empty())) {
157                 mergeParagraph(params, pars, startpit);
158                 // this because endpar gets deleted here!
159                 endpit = startpit;
160                 endpos = startpos;
161         }
162
163         return PitPosPair(endpit, endpos);
164
165 }
166
167
168 namespace {
169
170 struct resetOwnerAndChanges {
171         void operator()(Paragraph & p) {
172                 p.cleanChanges();
173                 p.setInsetOwner(0);
174         }
175 };
176
177 } // anon namespace
178
179 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
180                                 ParagraphList::iterator endpit,
181                                 int start, int end, textclass_type tc)
182 {
183         lyx::Assert(&*startpit);
184         lyx::Assert(&*endpit);
185         lyx::Assert(0 <= start && start <= startpit->size());
186         lyx::Assert(0 <= end && end <= endpit->size());
187         lyx::Assert(startpit != endpit || start <= end);
188
189         ParagraphList paragraphs;
190
191         // Clone the paragraphs within the selection.
192         ParagraphList::iterator postend = boost::next(endpit);
193
194         paragraphs.assign(startpit, postend);
195         for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
196
197         // Cut out the end of the last paragraph.
198         Paragraph & back = paragraphs.back();
199         back.erase(end, back.size());
200
201         // Cut out the begin of the first paragraph
202         Paragraph & front = paragraphs.front();
203         front.erase(0, start);
204
205         cuts.push(make_pair(paragraphs, tc));
206
207         return true;
208 }
209
210
211 pair<PitPosPair, ParagraphList::iterator>
212 CutAndPaste::pasteSelection(Buffer const & buffer,
213                             ParagraphList & pars,
214                             ParagraphList::iterator pit, int pos,
215                             textclass_type tc,
216                             ErrorList & errorlist)
217 {
218         return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
219 }
220
221
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)
228 {
229         if (!checkPastePossible())
230                 return make_pair(PitPosPair(pit, pos), pit);
231
232         lyx::Assert (pos <= pit->size());
233
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;
237
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.
240
241         // Make sure there is no class difference.
242         SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
243                                     errorlist);
244
245         ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
246         int depth_delta = pit->params().depth() - tmpbuf->params().depth();
247
248         Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
249
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
255                 // at level 0.
256                 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
257                         depth_delta = 0;
258
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);
263
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();
268
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())) {
274                                         tmpbuf->erase(i--);
275                                 }
276                         } else {
277                                 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
278                                 LyXFont f2 = f1;
279                                 if (!pit->checkInsertChar(f1)) {
280                                         tmpbuf->erase(i--);
281                                 } else if (f1 != f2) {
282                                         tmpbuf->setFont(i, f1);
283                                 }
284                         }
285                 }
286         }
287
288         // Make the buf exactly the same layout than
289         // the cursor paragraph.
290         simple_cut_clone.begin()->makeSameLayout(*pit);
291
292         // Prepare the paragraphs and insets for insertion
293         // A couple of insets store buffer references so need
294         // updating
295         ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
296         ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
297
298         for (; fpit != fend; ++fpit) {
299                 InsetList::iterator lit = fpit->insetlist.begin();
300                 InsetList::iterator eit = fpit->insetlist.end();
301
302                 for (; lit != eit; ++lit) {
303                         switch (lit->inset->lyxCode()) {
304                         case Inset::INCLUDE_CODE: {
305                                 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
306                                 InsetInclude::Params ip = ii->params();
307                                 ip.masterFilename_ = buffer.fileName();
308                                 ii->set(ip);
309                                 break;
310                         }
311
312                         case Inset::TABULAR_CODE: {
313                                 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
314                                 it->buffer(const_cast<Buffer*>(&buffer));
315                                 break;
316                         }
317
318                         default:
319                                 break; // nothing
320                         }
321                 }
322         }
323
324         bool paste_the_end = false;
325
326         // Open the paragraph for inserting the buf
327         // if necessary.
328         if (pit->size() > pos || boost::next(pit) == pars.end()) {
329                 breakParagraphConservative(buffer.params,
330                                            pars, pit, pos);
331                 paste_the_end = true;
332         }
333
334         // Set the end for redoing later.
335         ParagraphList::iterator endpit = boost::next(boost::next(pit));
336
337         // Paste it!
338
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);
342
343         // If we only inserted one paragraph.
344         if (boost::next(pit) == last_paste)
345                 last_paste = pit;
346
347         mergeParagraph(buffer.params, pars, pit);
348
349         // Store the new cursor position.
350         pit = last_paste;
351         pos = last_paste->size();
352
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() &&
356             paste_the_end) {
357                 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
358                         mergeParagraph(buffer.params, pars,
359                                        last_paste);
360                 } else if (boost::next(last_paste)->empty()) {
361                         boost::next(last_paste)->makeSameLayout(*last_paste);
362                         mergeParagraph(buffer.params, pars,
363                                        last_paste);
364                 } else if (last_paste->empty()) {
365                         last_paste->makeSameLayout(*boost::next(last_paste));
366                         mergeParagraph(buffer.params, pars,
367                                        last_paste);
368                 } else
369                         boost::next(last_paste)->stripLeadingSpaces();
370         }
371
372         return make_pair(PitPosPair(pit, pos), endpit);
373 }
374
375
376 int CutAndPaste::nrOfParagraphs()
377 {
378         return cuts.empty() ? 0 : cuts[0].first.size();
379 }
380
381
382 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
383                                              textclass_type c2,
384                                              ParagraphList & pars,
385                                              ErrorList & errorlist)
386 {
387         lyx::Assert(!pars.empty());
388
389         int ret = 0;
390         if (c1 == c2)
391                 return ret;
392
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);
399
400                 if (hasLayout)
401                         it->layout(tclass2[name]);
402                 else
403                         it->layout(tclass2.defaultLayout());
404
405                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
406                         ++ret;
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,
413                                                       it->id(), 0,
414                                                       it->size()));
415                 }
416         }
417         return ret;
418 }
419
420
421 bool CutAndPaste::checkPastePossible()
422 {
423         return !cuts.empty() && !cuts[0].first.empty();
424 }