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