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