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