]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
"Inter-word Space"
[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
28 #include "insets/inseterror.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         bool paste_the_end = false;
277
278         // Open the paragraph for inserting the buf
279         // if necessary.
280         if (pit->size() > pos || boost::next(pit) == pars.end()) {
281                 breakParagraphConservative(current_view->buffer()->params,
282                                            pars, pit, pos);
283                 paste_the_end = true;
284         }
285
286         // Set the end for redoing later.
287         ParagraphList::iterator endpit = boost::next(boost::next(pit));
288
289         // Paste it!
290
291         ParagraphList::iterator past_pit = boost::next(pit);
292         pars.splice(past_pit, simple_cut_clone);
293         ParagraphList::iterator last_paste = boost::prior(past_pit);
294
295         // If we only inserted one paragraph.
296         if (boost::next(pit) == last_paste)
297                 last_paste = pit;
298
299         mergeParagraph(current_view->buffer()->params, pars, pit);
300
301         // Store the new cursor position.
302         pit = last_paste;
303         pos = last_paste->size();
304
305         // Maybe some pasting.
306 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
307         if (boost::next(last_paste) != pars.end() &&
308             paste_the_end) {
309                 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
310                         mergeParagraph(current_view->buffer()->params, pars,
311                                        last_paste);
312                 } else if (boost::next(last_paste)->empty()) {
313                         boost::next(last_paste)->makeSameLayout(*last_paste);
314                         mergeParagraph(current_view->buffer()->params, pars,
315                                        last_paste);
316                 } else if (last_paste->empty()) {
317                         last_paste->makeSameLayout(*boost::next(last_paste));
318                         mergeParagraph(current_view->buffer()->params, pars,
319                                        last_paste);
320                 } else
321                         boost::next(last_paste)->stripLeadingSpaces();
322         }
323
324         return make_pair(PitPosPair(pit, pos), endpit);
325 }
326
327
328 int CutAndPaste::nrOfParagraphs()
329 {
330         return cuts.empty() ? 0 : cuts[0].first.size();
331 }
332
333
334 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
335                                              textclass_type c2,
336                                              ParagraphList & pars,
337                                              ErrorList & errorlist)
338 {
339         lyx::Assert(!pars.empty());
340
341         int ret = 0;
342         if (c1 == c2)
343                 return ret;
344
345         LyXTextClass const & tclass1 = textclasslist[c1];
346         LyXTextClass const & tclass2 = textclasslist[c2];
347         ParIterator end = ParIterator(pars.end(), pars);
348         for (ParIterator it = ParIterator(pars.begin(), pars); it != end; ++it) {
349                 Paragraph * par = &*(*it);
350                 string const name = par->layout()->name();
351                 bool hasLayout = tclass2.hasLayout(name);
352
353                 if (hasLayout)
354                         par->layout(tclass2[name]);
355                 else
356                         par->layout(tclass2.defaultLayout());
357
358                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
359                         ++ret;
360                         string const s = bformat(
361                                 _("Layout had to be changed from\n%1$s to %2$s\n"
362                                 "because of class conversion from\n%3$s to %4$s"),
363                          name, par->layout()->name(), tclass1.name(), tclass2.name());
364                         // To warn the user that something had to be done.
365                         errorlist.push_back(ErrorItem("Changed Layout", s,
366                                                       par->id(), 0,
367                                                       par->size()));
368                 }
369         }
370         return ret;
371 }
372
373
374 bool CutAndPaste::checkPastePossible()
375 {
376         return !cuts.empty() && !cuts[0].first.empty();
377 }