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