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