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