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