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