]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
zlib stuff
[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(0 <= start && start <= startpit->size());
185         Assert(0 <= end && end <= endpit->size());
186         Assert(startpit != endpit || start <= end);
187
188         ParagraphList paragraphs;
189
190         // Clone the paragraphs within the selection.
191         ParagraphList::iterator postend = boost::next(endpit);
192
193         paragraphs.assign(startpit, postend);
194         for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
195
196         // Cut out the end of the last paragraph.
197         Paragraph & back = paragraphs.back();
198         back.erase(end, back.size());
199
200         // Cut out the begin of the first paragraph
201         Paragraph & front = paragraphs.front();
202         front.erase(0, start);
203
204         cuts.push(make_pair(paragraphs, tc));
205
206         return true;
207 }
208
209
210 pair<PitPosPair, ParagraphList::iterator>
211 CutAndPaste::pasteSelection(Buffer const & buffer,
212                             ParagraphList & pars,
213                             ParagraphList::iterator pit, int pos,
214                             textclass_type tc,
215                             ErrorList & errorlist)
216 {
217         return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
218 }
219
220
221 pair<PitPosPair, ParagraphList::iterator>
222 CutAndPaste::pasteSelection(Buffer const & buffer,
223                             ParagraphList & pars,
224                             ParagraphList::iterator pit, int pos,
225                             textclass_type tc, size_t cut_index,
226                             ErrorList & errorlist)
227 {
228         if (!checkPastePossible())
229                 return make_pair(PitPosPair(pit, pos), pit);
230
231         Assert (pos <= pit->size());
232
233         // Make a copy of the CaP paragraphs.
234         ParagraphList simple_cut_clone = cuts[cut_index].first;
235         textclass_type const textclass = cuts[cut_index].second;
236
237         // Now remove all out of the pars which is NOT allowed in the
238         // new environment and set also another font if that is required.
239
240         // Make sure there is no class difference.
241         SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
242                                     errorlist);
243
244         ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
245         int depth_delta = pit->params().depth() - tmpbuf->params().depth();
246
247         Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
248
249         for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
250                 // If we have a negative jump so that the depth would
251                 // go below 0 depth then we have to redo the delta to
252                 // this new max depth level so that subsequent
253                 // paragraphs are aligned correctly to this paragraph
254                 // at level 0.
255                 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
256                         depth_delta = 0;
257
258                 // Set the right depth so that we are not too deep or shallow.
259                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
260                 if (tmpbuf->params().depth() > max_depth)
261                         tmpbuf->params().depth(max_depth);
262
263                 // Only set this from the 2nd on as the 2nd depends
264                 // for maxDepth still on pit.
265                 if (tmpbuf != simple_cut_clone.begin())
266                         max_depth = tmpbuf->getMaxDepthAfter();
267
268                 // Set the inset owner of this paragraph.
269                 tmpbuf->setInsetOwner(pit->inInset());
270                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
271                         if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
272                                 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
273                                         tmpbuf->erase(i--);
274                                 }
275                         } else {
276                                 LyXFont f1 = tmpbuf->getFont(buffer.params, i, outerFont(pit, pars));
277                                 LyXFont f2 = f1;
278                                 if (!pit->checkInsertChar(f1)) {
279                                         tmpbuf->erase(i--);
280                                 } else if (f1 != f2) {
281                                         tmpbuf->setFont(i, f1);
282                                 }
283                         }
284                 }
285         }
286
287         // Make the buf exactly the same layout than
288         // the cursor paragraph.
289         simple_cut_clone.begin()->makeSameLayout(*pit);
290
291         // Prepare the paragraphs and insets for insertion
292         // A couple of insets store buffer references so need
293         // updating
294         ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
295         ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
296
297         for (; fpit != fend; ++fpit) {
298                 InsetList::iterator lit = fpit->insetlist.begin();
299                 InsetList::iterator eit = fpit->insetlist.end();
300
301                 for (; lit != eit; ++lit) {
302                         switch (lit->inset->lyxCode()) {
303                         case InsetOld::INCLUDE_CODE: {
304                                 InsetInclude * ii = static_cast<InsetInclude*>(lit->inset);
305                                 InsetInclude::Params ip = ii->params();
306                                 ip.masterFilename_ = buffer.fileName();
307                                 ii->set(ip);
308                                 break;
309                         }
310
311                         case InsetOld::TABULAR_CODE: {
312                                 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
313                                 it->buffer(const_cast<Buffer*>(&buffer));
314                                 break;
315                         }
316
317                         default:
318                                 break; // nothing
319                         }
320                 }
321         }
322
323         bool paste_the_end = false;
324
325         // Open the paragraph for inserting the buf
326         // if necessary.
327         if (pit->size() > pos || boost::next(pit) == pars.end()) {
328                 breakParagraphConservative(buffer.params,
329                                            pars, pit, pos);
330                 paste_the_end = true;
331         }
332
333         // Set the end for redoing later.
334         ParagraphList::iterator endpit = boost::next(boost::next(pit));
335
336         // Paste it!
337
338         ParagraphList::iterator past_pit = boost::next(pit);
339         pars.splice(past_pit, simple_cut_clone);
340         ParagraphList::iterator last_paste = boost::prior(past_pit);
341
342         // If we only inserted one paragraph.
343         if (boost::next(pit) == last_paste)
344                 last_paste = pit;
345
346         mergeParagraph(buffer.params, pars, pit);
347
348         // Store the new cursor position.
349         pit = last_paste;
350         pos = last_paste->size();
351
352         // Maybe some pasting.
353 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
354         if (boost::next(last_paste) != pars.end() &&
355             paste_the_end) {
356                 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
357                         mergeParagraph(buffer.params, pars,
358                                        last_paste);
359                 } else if (boost::next(last_paste)->empty()) {
360                         boost::next(last_paste)->makeSameLayout(*last_paste);
361                         mergeParagraph(buffer.params, pars,
362                                        last_paste);
363                 } else if (last_paste->empty()) {
364                         last_paste->makeSameLayout(*boost::next(last_paste));
365                         mergeParagraph(buffer.params, pars,
366                                        last_paste);
367                 } else
368                         boost::next(last_paste)->stripLeadingSpaces();
369         }
370
371         return make_pair(PitPosPair(pit, pos), endpit);
372 }
373
374
375 int CutAndPaste::nrOfParagraphs()
376 {
377         return cuts.empty() ? 0 : cuts[0].first.size();
378 }
379
380
381 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
382                                              textclass_type c2,
383                                              ParagraphList & pars,
384                                              ErrorList & errorlist)
385 {
386         Assert(!pars.empty());
387
388         int ret = 0;
389         if (c1 == c2)
390                 return ret;
391
392         LyXTextClass const & tclass1 = textclasslist[c1];
393         LyXTextClass const & tclass2 = textclasslist[c2];
394         ParIterator end = ParIterator(pars.end(), pars);
395         for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
396                 string const name = it->layout()->name();
397                 bool hasLayout = tclass2.hasLayout(name);
398
399                 if (hasLayout)
400                         it->layout(tclass2[name]);
401                 else
402                         it->layout(tclass2.defaultLayout());
403
404                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
405                         ++ret;
406                         string const s = bformat(
407                                 _("Layout had to be changed from\n%1$s to %2$s\n"
408                                 "because of class conversion from\n%3$s to %4$s"),
409                          name, it->layout()->name(), tclass1.name(), tclass2.name());
410                         // To warn the user that something had to be done.
411                         errorlist.push_back(ErrorItem("Changed Layout", s,
412                                                       it->id(), 0,
413                                                       it->size()));
414                 }
415         }
416         return ret;
417 }
418
419
420 bool CutAndPaste::checkPastePossible()
421 {
422         return !cuts.empty() && !cuts[0].first.empty();
423 }