]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
fea5382d1bd578993663c16238d068c9d899cefb
[lyx.git] / src / CutAndPaste.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
14 #include "buffer.h"
15 #include "paragraph.h"
16 #include "ParagraphParameters.h"
17 #include "lyxtext.h"
18 #include "lyxcursor.h"
19 #include "gettext.h"
20 #include "iterators.h"
21 #include "lyxtextclasslist.h"
22 #include "undo_funcs.h"
23 #include "paragraph_funcs.h"
24 #include "debug.h"
25
26 #include "insets/inseterror.h"
27
28 #include "support/BoostFormat.h"
29 #include "support/LAssert.h"
30
31 using std::endl;
32 using std::pair;
33 using std::make_pair;
34 using std::for_each;
35
36 using lyx::pos_type;
37 using lyx::textclass_type;
38
39 extern BufferView * current_view;
40
41 // Jürgen, note that this means that you cannot currently have a list
42 // of selections cut/copied. So IMHO later we should have a
43 // list/vector/deque that we could store
44 // struct selection_item {
45 //       Paragraph * buf;
46 //       LyXTextClassList::size_type textclass;
47 // };
48 // in and some method of choosing beween them (based on the first few chars
49 // in the selection probably.) This would be a nice feature and quite
50 // easy to implement. (Lgb)
51 //
52 // Sure but I just cleaned up this code for now with the same functionality
53 // as before. I also want to add a XClipboard function so that we can copy
54 // text from LyX to some other X-application in the form of ASCII or in the
55 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
56 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
57 // then do a middle mouse button click in the application you want and have
58 // the whole formula there in LaTeX-Code. (Jug)
59
60 namespace {
61
62 // FIXME: stupid name
63 ParagraphList paragraphs;
64 textclass_type textclass = 0;
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         textclass = tc;
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         return true;
192 }
193
194
195 pair<PitPosPair, ParagraphList::iterator>
196 CutAndPaste::pasteSelection(ParagraphList & pars,
197                             ParagraphList::iterator pit, int pos,
198                             textclass_type tc)
199 {
200         if (!checkPastePossible())
201                 return make_pair(PitPosPair(pit, pos), pit);
202
203         lyx::Assert (pos <= pit->size());
204
205         // make a copy of the simple cut_buffer
206 #if 1
207         ParagraphList simple_cut_clone;
208         ParagraphList::iterator it = paragraphs.begin();
209         ParagraphList::iterator end = paragraphs.end();
210         for (; it != end; ++it) {
211                 simple_cut_clone.push_back(new Paragraph(*it, false));
212         }
213 #else
214         // Later we want it done like this:
215         ParagraphList simple_cut_clone = paragraphs;
216 #endif
217         // now remove all out of the buffer which is NOT allowed in the
218         // new environment and set also another font if that is required
219         ParagraphList::iterator tmpbuf = paragraphs.begin();
220         int depth_delta = pit->params().depth() - tmpbuf->params().depth();
221         // Temporary set *par as previous of tmpbuf as we might have
222         // to realize the font.
223         tmpbuf->previous(&*pit);
224
225         // make sure there is no class difference
226 #warning current_view used here
227         SwitchLayoutsBetweenClasses(textclass, tc, &*tmpbuf,
228                                     current_view->buffer()->params);
229
230         Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
231
232         for (; tmpbuf != paragraphs.end(); ++tmpbuf) {
233                 // If we have a negative jump so that the depth would
234                 // go below 0 depth then we have to redo the delta to
235                 // this new max depth level so that subsequent
236                 // paragraphs are aligned correctly to this paragraph
237                 // at level 0.
238                 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
239                         depth_delta = 0;
240                 // set the right depth so that we are not too deep or shallow.
241                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
242                 if (tmpbuf->params().depth() > max_depth)
243                         tmpbuf->params().depth(max_depth);
244                 // only set this from the 2nd on as the 2nd depends for maxDepth
245                 // still on pit
246                 if (tmpbuf->previous() != pit)
247                         max_depth = tmpbuf->getMaxDepthAfter();
248                 // set the inset owner of this paragraph
249                 tmpbuf->setInsetOwner(pit->inInset());
250                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
251                         if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
252                                 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
253                                         tmpbuf->erase(i--);
254                                 }
255                         } else {
256                                 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params, i, outerFont(tmpbuf, pars));
257                                 LyXFont f2 = f1;
258                                 if (!pit->checkInsertChar(f1)) {
259                                         tmpbuf->erase(i--);
260                                 } else if (f1 != f2) {
261                                         tmpbuf->setFont(i, f1);
262                                 }
263                         }
264                 }
265         }
266
267         // now reset it to 0
268         paragraphs.begin()->previous(0);
269
270         // make the buf exactly the same layout than
271         // the cursor paragraph
272         paragraphs.begin()->makeSameLayout(*pit);
273
274         // find the end of the buffer
275         // FIXME: change this to end() - 1
276         ParagraphList::iterator lastbuffer = paragraphs.begin();
277         while (boost::next(lastbuffer) != paragraphs.end())
278                 ++lastbuffer;
279
280         bool paste_the_end = false;
281
282         // open the paragraph for inserting the buf
283         // if necessary
284         if (pit->size() > pos || !pit->next()) {
285                 breakParagraphConservative(current_view->buffer()->params,
286                                            pars, &*pit, pos);
287                 paste_the_end = true;
288         }
289         // set the end for redoing later
290         ParagraphList::iterator endpit = boost::next(boost::next(pit));
291
292         // paste it!
293         lastbuffer->next(pit->next());
294         pit->next()->previous(&*lastbuffer);
295
296         pit->next(&*paragraphs.begin());
297         paragraphs.begin()->previous(&*pit);
298
299         if (boost::next(pit) == lastbuffer)
300                 lastbuffer = pit;
301
302         mergeParagraph(current_view->buffer()->params, pars, pit);
303         // store the new cursor position
304         pit = lastbuffer;
305         pos = lastbuffer->size();
306         // maybe some pasting
307         if (boost::next(lastbuffer) != paragraphs.end() && paste_the_end) {
308                 if (boost::next(lastbuffer)->hasSameLayout(*lastbuffer)) {
309                         mergeParagraph(current_view->buffer()->params, pars,
310                                        lastbuffer);
311                 } else if (!boost::next(lastbuffer)->size()) {
312                         boost::next(lastbuffer)->makeSameLayout(*lastbuffer);
313                         mergeParagraph(current_view->buffer()->params, pars,
314                                        lastbuffer);
315                 } else if (!lastbuffer->size()) {
316                         lastbuffer->makeSameLayout(*boost::next(lastbuffer));
317                         mergeParagraph(current_view->buffer()->params, pars,
318                                        lastbuffer);
319                 } else
320                         boost::next(lastbuffer)->stripLeadingSpaces();
321         }
322         // restore the simple cut buffer
323         paragraphs = simple_cut_clone;
324
325         return make_pair(PitPosPair(pit, pos), endpit);
326 }
327
328
329 int CutAndPaste::nrOfParagraphs()
330 {
331         return paragraphs.size();
332 }
333
334
335 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
336                                              textclass_type c2,
337                                              Paragraph * par,
338                                              BufferParams const & /*bparams*/)
339 {
340         int ret = 0;
341         if (!par || c1 == c2)
342                 return ret;
343
344         LyXTextClass const & tclass1 = textclasslist[c1];
345         LyXTextClass const & tclass2 = textclasslist[c2];
346         ParIterator end = ParIterator();
347         for (ParIterator it = ParIterator(par); it != end; ++it) {
348                 par = *it;
349                 string const name = par->layout()->name();
350                 bool hasLayout = tclass2.hasLayout(name);
351
352                 if (hasLayout)
353                         par->layout(tclass2[name]);
354                 else
355                         par->layout(tclass2.defaultLayout());
356
357                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
358                         ++ret;
359 #if USE_BOOST_FORMAT
360                         boost::format fmt(_("Layout had to be changed from\n"
361                                             "%1$s to %2$s\n"
362                                             "because of class conversion from\n"
363                                             "%3$s to %4$s"));
364                         fmt     % name
365                                 % par->layout()->name()
366                                 % tclass1.name()
367                                 % tclass2.name();
368
369                         string const s = fmt.str();
370 #else
371                         string const s = _("Layout had to be changed from\n")
372                                 + name + _(" to ")
373                                 + par->layout()->name()
374                                 + _("\nbecause of class conversion from\n")
375                                 + tclass1.name() + _(" to ")
376                                 + tclass2.name();
377 #endif
378                         freezeUndo();
379                         InsetError * new_inset = new InsetError(s);
380                         LyXText * txt = current_view->getLyXText();
381                         LyXCursor cur = txt->cursor;
382                         txt->setCursorIntern(par, 0);
383                         txt->insertInset(new_inset);
384                         txt->fullRebreak();
385                         txt->setCursorIntern(cur.par(), cur.pos());
386                         unFreezeUndo();
387                 }
388         }
389         return ret;
390 }
391
392
393 bool CutAndPaste::checkPastePossible()
394 {
395         return !paragraphs.empty();
396 }