]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
ws changes only
[lyx.git] / src / CutAndPaste.C
1 /*
2  * \file CutAndPaste.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "CutAndPaste.h"
16
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "errorlist.h"
20 #include "gettext.h"
21 #include "iterators.h"
22 #include "lyxtextclasslist.h"
23 #include "paragraph.h"
24 #include "paragraph_funcs.h"
25 #include "ParagraphParameters.h"
26
27 #include "insets/insettabular.h"
28
29 #include "support/lstrings.h"
30
31 using lyx::pos_type;
32 using lyx::textclass_type;
33
34 using lyx::support::bformat;
35
36 using std::for_each;
37 using std::make_pair;
38 using std::pair;
39 using std::vector;
40 using std::string;
41
42
43 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
44
45 namespace {
46
47 CutStack cuts(10);
48
49 } // namespace anon
50
51
52 std::vector<string> const
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         BOOST_ASSERT(0 <= start && start <= startpit->size());
182         BOOST_ASSERT(0 <= end && end <= endpit->size());
183         BOOST_ASSERT(startpit != endpit || start <= end);
184
185         ParagraphList paragraphs;
186
187         // Clone the paragraphs within the selection.
188         ParagraphList::iterator postend = boost::next(endpit);
189
190         paragraphs.assign(startpit, postend);
191         for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
192
193         // Cut out the end of the last paragraph.
194         Paragraph & back = paragraphs.back();
195         back.erase(end, back.size());
196
197         // Cut out the begin of the first paragraph
198         Paragraph & front = paragraphs.front();
199         front.erase(0, start);
200
201         cuts.push(make_pair(paragraphs, tc));
202
203         return true;
204 }
205
206
207 pair<PitPosPair, ParagraphList::iterator>
208 CutAndPaste::pasteSelection(Buffer const & buffer,
209                             ParagraphList & pars,
210                             ParagraphList::iterator pit, int pos,
211                             textclass_type tc,
212                             ErrorList & errorlist)
213 {
214         return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
215 }
216
217
218 pair<PitPosPair, ParagraphList::iterator>
219 CutAndPaste::pasteSelection(Buffer const & buffer,
220                             ParagraphList & pars,
221                             ParagraphList::iterator pit, int pos,
222                             textclass_type tc, size_t cut_index,
223                             ErrorList & errorlist)
224 {
225         if (!checkPastePossible())
226                 return make_pair(PitPosPair(pit, pos), pit);
227
228         BOOST_ASSERT (pos <= pit->size());
229
230         // Make a copy of the CaP paragraphs.
231         ParagraphList simple_cut_clone = cuts[cut_index].first;
232         textclass_type const textclass = cuts[cut_index].second;
233
234         // Now remove all out of the pars which is NOT allowed in the
235         // new environment and set also another font if that is required.
236
237         // Make sure there is no class difference.
238         SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
239                                     errorlist);
240
241         ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
242         int depth_delta = pit->params().depth() - tmpbuf->params().depth();
243
244         Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
245
246         for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
247                 // If we have a negative jump so that the depth would
248                 // go below 0 depth then we have to redo the delta to
249                 // this new max depth level so that subsequent
250                 // paragraphs are aligned correctly to this paragraph
251                 // at level 0.
252                 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
253                         depth_delta = 0;
254
255                 // Set the right depth so that we are not too deep or shallow.
256                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
257                 if (tmpbuf->params().depth() > max_depth)
258                         tmpbuf->params().depth(max_depth);
259
260                 // Only set this from the 2nd on as the 2nd depends
261                 // for maxDepth still on pit.
262                 if (tmpbuf != simple_cut_clone.begin())
263                         max_depth = tmpbuf->getMaxDepthAfter();
264
265                 // Set the inset owner of this paragraph.
266                 tmpbuf->setInsetOwner(pit->inInset());
267                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
268                         if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
269                                 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
270                                         tmpbuf->erase(i--);
271                                 }
272                         } else {
273                                 LyXFont f1 = tmpbuf->getFont(buffer.params(), i, outerFont(pit, pars));
274                                 LyXFont f2 = f1;
275                                 if (!pit->checkInsertChar(f1)) {
276                                         tmpbuf->erase(i--);
277                                 } else if (f1 != f2) {
278                                         tmpbuf->setFont(i, f1);
279                                 }
280                         }
281                 }
282         }
283
284         // Make the buf exactly the same layout than
285         // the cursor paragraph.
286         simple_cut_clone.begin()->makeSameLayout(*pit);
287
288         // Prepare the paragraphs and insets for insertion
289         // A couple of insets store buffer references so need
290         // updating
291         ParIterator fpit(simple_cut_clone.begin(), simple_cut_clone);
292         ParIterator fend(simple_cut_clone.end(), simple_cut_clone);
293
294         for (; fpit != fend; ++fpit) {
295                 InsetList::iterator lit = fpit->insetlist.begin();
296                 InsetList::iterator eit = fpit->insetlist.end();
297
298                 for (; lit != eit; ++lit) {
299                         switch (lit->inset->lyxCode()) {
300                         case InsetOld::TABULAR_CODE: {
301                                 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
302                                 it->buffer(const_cast<Buffer*>(&buffer));
303                                 break;
304                         }
305
306                         default:
307                                 break; // nothing
308                         }
309                 }
310         }
311
312         bool paste_the_end = false;
313
314         // Open the paragraph for inserting the buf
315         // if necessary.
316         if (pit->size() > pos || boost::next(pit) == pars.end()) {
317                 breakParagraphConservative(buffer.params(),
318                                            pars, pit, pos);
319                 paste_the_end = true;
320         }
321
322         // Set the end for redoing later.
323         ParagraphList::iterator endpit = boost::next(boost::next(pit));
324
325         // Paste it!
326
327         ParagraphList::iterator past_pit = boost::next(pit);
328         pars.splice(past_pit, simple_cut_clone);
329         ParagraphList::iterator last_paste = boost::prior(past_pit);
330
331         // If we only inserted one paragraph.
332         if (boost::next(pit) == last_paste)
333                 last_paste = pit;
334
335         mergeParagraph(buffer.params(), pars, pit);
336
337         // Store the new cursor position.
338         pit = last_paste;
339         pos = last_paste->size();
340
341         // Maybe some pasting.
342         if (boost::next(last_paste) != pars.end() &&
343             paste_the_end) {
344                 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
345                         mergeParagraph(buffer.params(), pars,
346                                        last_paste);
347                 } else if (boost::next(last_paste)->empty()) {
348                         boost::next(last_paste)->makeSameLayout(*last_paste);
349                         mergeParagraph(buffer.params(), pars,
350                                        last_paste);
351                 } else if (last_paste->empty()) {
352                         last_paste->makeSameLayout(*boost::next(last_paste));
353                         mergeParagraph(buffer.params(), pars,
354                                        last_paste);
355                 } else
356                         boost::next(last_paste)->stripLeadingSpaces();
357         }
358
359         return make_pair(PitPosPair(pit, pos), endpit);
360 }
361
362
363 int CutAndPaste::nrOfParagraphs()
364 {
365         return cuts.empty() ? 0 : cuts[0].first.size();
366 }
367
368
369 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
370                                              textclass_type c2,
371                                              ParagraphList & pars,
372                                              ErrorList & errorlist)
373 {
374         BOOST_ASSERT(!pars.empty());
375
376         int ret = 0;
377         if (c1 == c2)
378                 return ret;
379
380         LyXTextClass const & tclass1 = textclasslist[c1];
381         LyXTextClass const & tclass2 = textclasslist[c2];
382         ParIterator end = ParIterator(pars.end(), pars);
383         for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
384                 string const name = it->layout()->name();
385                 bool hasLayout = tclass2.hasLayout(name);
386
387                 if (hasLayout)
388                         it->layout(tclass2[name]);
389                 else
390                         it->layout(tclass2.defaultLayout());
391
392                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
393                         ++ret;
394                         string const s = bformat(
395                                 _("Layout had to be changed from\n%1$s to %2$s\n"
396                                 "because of class conversion from\n%3$s to %4$s"),
397                          name, it->layout()->name(), tclass1.name(), tclass2.name());
398                         // To warn the user that something had to be done.
399                         errorlist.push_back(ErrorItem("Changed Layout", s,
400                                                       it->id(), 0,
401                                                       it->size()));
402                 }
403         }
404         return ret;
405 }
406
407
408 bool CutAndPaste::checkPastePossible()
409 {
410         return !cuts.empty() && !cuts[0].first.empty();
411 }