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