]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
532e12c40d07c1c1d1ee09f42a57ad777b5118b6
[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 "BoostFormat.h"
29
30 using std::endl;
31 using std::pair;
32 using lyx::pos_type;
33 using lyx::textclass_type;
34
35 extern BufferView * current_view;
36
37 // Jürgen, note that this means that you cannot currently have a list
38 // of selections cut/copied. So IMHO later we should have a
39 // list/vector/deque that we could store
40 // struct selection_item {
41 //       Paragraph * buf;
42 //       LyXTextClassList::size_type textclass;
43 // };
44 // in and some method of choosing beween them (based on the first few chars
45 // in the selection probably.) This would be a nice feature and quite
46 // easy to implement. (Lgb)
47 //
48 // Sure but I just cleaned up this code for now with the same functionality
49 // as before. I also want to add a XClipboard function so that we can copy
50 // text from LyX to some other X-application in the form of ASCII or in the
51 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
52 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
53 // then do a middle mouse button click in the application you want and have
54 // the whole formula there in LaTeX-Code. (Jug)
55
56 namespace {
57
58 // FIXME: stupid name
59 Paragraph * buf = 0;
60 textclass_type textclass = 0;
61
62 // for now here this should be in another Cut&Paste Class!
63 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
64 // member of the CutAndPaste class and in addition it was private.
65 // Perhaps it even should take a parameter? (Lgb)
66 void DeleteBuffer()
67 {
68         if (!buf)
69                 return;
70
71         Paragraph * tmppar;
72
73         while (buf) {
74                 tmppar =  buf;
75                 buf = buf->next();
76                 delete tmppar;
77         }
78         buf = 0;
79 }
80
81 } // namespace anon
82
83
84 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
85                                int start, int & end, char tc, bool doclear,
86                                                            bool realcut)
87 {
88         if (!startpar || (start > startpar->size()))
89                 return false;
90
91         if (realcut) {
92                 copySelection(startpar, *endpar, start, end, tc);
93         }
94
95         if (!endpar || startpar == *endpar) {
96                 if (startpar->erase(start, end)) {
97                         // Some chars were erased, go to start to be safe
98                         end = start;
99                 }
100                 return true;
101         }
102
103         bool actually_erased = false;
104
105         // clear end/begin fragments of the first/last par in selection
106         actually_erased |= (startpar)->erase(start, startpar->size());
107         if ((*endpar)->erase(0, end)) {
108                 actually_erased = true;
109                 end = 0;
110         }
111
112         // Loop through the deleted pars if any, erasing as needed
113
114         Paragraph * pit = startpar->next();
115
116         while (true) {
117                 // *endpar can be 0
118                 if (!pit)
119                         break;
120
121                 Paragraph * next = pit->next();
122
123                 // "erase" the contents of the par
124                 if (pit != *endpar) {
125                         actually_erased |= pit->erase(0, pit->size());
126
127                         // remove the par if it's now empty
128                         if (actually_erased) {
129                                 pit->previous()->next(pit->next());
130                                 if (next) {
131                                         next->previous(pit->previous());
132                                 }
133
134                                 delete pit;
135                         }
136                 }
137
138                 if (pit == *endpar)
139                         break;
140
141                 pit = next;
142         }
143
144 #if 0 // FIXME: why for cut but not copy ?
145         // the cut selection should begin with standard layout
146         if (realcut) {
147                 buf->params().clear();
148                 buf->bibkey = 0;
149                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
150         }
151 #endif
152
153         if (!startpar->next())
154                 return true;
155
156         Buffer * buffer = current_view->buffer();
157
158         if (doclear) {
159                 startpar->next()->stripLeadingSpaces();
160         }
161
162         if (!actually_erased)
163                 return true;
164
165         // paste the paragraphs again, if possible
166         if (startpar->hasSameLayout(startpar->next()) ||
167             startpar->next()->empty()) {
168 #warning This is suspect. (Lgb)
169                 // When doing this merge we must know if the par really
170                 // belongs to an inset, and if it does then we have to use
171                 // the insets paragraphs, and not the buffers. (Lgb)
172                 mergeParagraph(buffer->params, buffer->paragraphs, startpar);
173                 // this because endpar gets deleted here!
174                 (*endpar) = startpar;
175         }
176
177         return true;
178 }
179
180
181 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
182                                 int start, int end, char tc)
183 {
184         if (!startpar || (start > startpar->size()))
185                 return false;
186
187         DeleteBuffer();
188
189         textclass = tc;
190
191         if (!endpar || startpar == endpar) {
192                 // only within one paragraph
193                 buf = new Paragraph;
194                 buf->layout(startpar->layout());
195                 pos_type i = start;
196                 if (end > startpar->size())
197                         end = startpar->size();
198                 for (; i < end; ++i) {
199                         startpar->copyIntoMinibuffer(*current_view->buffer(), i);
200                         buf->insertFromMinibuffer(buf->size());
201                 }
202         } else {
203                 // copy more than one paragraph
204                 // clone the paragraphs within the selection
205                 Paragraph * tmppar = startpar;
206                 buf = new Paragraph(*tmppar, false);
207                 Paragraph * tmppar2 = buf;
208                 tmppar2->cleanChanges();
209
210                 while (tmppar != endpar
211                        && tmppar->next()) {
212                         tmppar = tmppar->next();
213                         tmppar2->next(new Paragraph(*tmppar, false));
214                         tmppar2->next()->previous(tmppar2);
215                         tmppar2 = tmppar2->next();
216                         // reset change info
217                         tmppar2->cleanChanges();
218                 }
219                 tmppar2->next(0);
220
221                 // the buf paragraph is too big
222                 pos_type tmpi2 = start;
223                 for (; tmpi2; --tmpi2)
224                         buf->erase(0);
225
226                 // now tmppar 2 is too big, delete all after end
227                 tmpi2 = end;
228                 while (tmppar2->size() > tmpi2) {
229                         tmppar2->erase(tmppar2->size() - 1);
230                 }
231                 // this paragraph's are of noone's owner!
232                 tmppar = buf;
233                 while (tmppar) {
234                         tmppar->setInsetOwner(0);
235                         tmppar = tmppar->next();
236                 }
237         }
238         return true;
239 }
240
241
242 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
243                                  int & pos, char tc)
244 {
245         if (!checkPastePossible(*par))
246                 return false;
247
248         if (pos > (*par)->size())
249                 pos = (*par)->size();
250
251 #if 0
252         // Paragraph * tmpbuf;
253         Paragraph * tmppar = *par;
254         int tmppos = pos;
255
256         // There are two cases: cutbuffer only one paragraph or many
257         if (!buf->next()) {
258                 // only within a paragraph
259                 Paragraph * tmpbuf = new Paragraph(*buf, false);
260
261                 // Some provisions should be done here for checking
262                 // if we are inserting at the beginning of a
263                 // paragraph. If there are a space at the beginning
264                 // of the text to insert and we are inserting at
265                 // the beginning of the paragraph the space should
266                 // be removed.
267                 while (buf->size()) {
268                         // This is an attempt to fix the
269                         // "never insert a space at the
270                         // beginning of a paragraph" problem.
271                         if (!tmppos && buf->isLineSeparator(0)) {
272                                 buf->erase(0);
273                         } else {
274                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
275                                 buf->erase(0);
276                                 if (tmppar->insertFromMinibuffer(tmppos))
277                                         ++tmppos;
278                         }
279                 }
280                 delete buf;
281                 buf = tmpbuf;
282                 *endpar = tmppar->next();
283                 pos = tmppos;
284         } else
285 #endif
286         {
287                 // many paragraphs
288
289                 // make a copy of the simple cut_buffer
290                 Paragraph * tmpbuf = buf;
291                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
292                 Paragraph * tmpbuf2 = simple_cut_clone;
293
294                 while (tmpbuf->next()) {
295                         tmpbuf = tmpbuf->next();
296                         tmpbuf2->next(new Paragraph(*tmpbuf, false));
297                         tmpbuf2->next()->previous(tmpbuf2);
298                         tmpbuf2 = tmpbuf2->next();
299                 }
300
301                 // now remove all out of the buffer which is NOT allowed in the
302                 // new environment and set also another font if that is required
303                 tmpbuf = buf;
304                 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
305                 // temporary set *par as previous of tmpbuf as we might have to realize
306                 // the font.
307                 tmpbuf->previous(*par);
308
309                 // make sure there is no class difference
310                 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
311                                             current_view->buffer()->params);
312
313                 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
314
315                 while(tmpbuf) {
316                         // if we have a negative jump so that the depth would go below
317                         // 0 depth then we have to redo the delta to this new max depth
318                         // level so that subsequent paragraphs are aligned correctly to
319                         // this paragraph at level 0.
320                         if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
321                                 depth_delta = 0;
322                         // set the right depth so that we are not too deep or shallow.
323                         tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
324                         if (tmpbuf->params().depth() > max_depth)
325                                 tmpbuf->params().depth(max_depth);
326                         // only set this from the 2nd on as the 2nd depends for maxDepth
327                         // still on *par
328                         if (tmpbuf->previous() != (*par))
329                                 max_depth = tmpbuf->getMaxDepthAfter();
330                         // set the inset owner of this paragraph
331                         tmpbuf->setInsetOwner((*par)->inInset());
332                         for(pos_type i = 0; i < tmpbuf->size(); ++i) {
333                                 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
334                                         if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
335                                         {
336                                                 tmpbuf->erase(i--);
337                                         }
338                                 } else {
339                                         LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
340                                         LyXFont f2 = f1;
341                                         if (!(*par)->checkInsertChar(f1)) {
342                                                 tmpbuf->erase(i--);
343                                         } else if (f1 != f2) {
344                                                 tmpbuf->setFont(i, f1);
345                                         }
346                                 }
347                         }
348                         tmpbuf = tmpbuf->next();
349                 }
350                 // now reset it to 0
351                 buf->previous(0);
352
353                 // make the buf exactly the same layout than
354                 // the cursor paragraph
355                 buf->makeSameLayout(*par);
356
357                 // find the end of the buffer
358                 Paragraph * lastbuffer = buf;
359                 while (lastbuffer->next())
360                         lastbuffer = lastbuffer->next();
361
362                 bool paste_the_end = false;
363
364                 // open the paragraph for inserting the buf
365                 // if necessary
366                 if (((*par)->size() > pos) || !(*par)->next()) {
367                         breakParagraphConservative(
368                                 current_view->buffer()->params, current_view->buffer()->paragraphs, *par, pos);
369                         paste_the_end = true;
370                 }
371                 // set the end for redoing later
372                 *endpar = (*par)->next()->next();
373
374                 // paste it!
375                 lastbuffer->next((*par)->next());
376                 (*par)->next()->previous(lastbuffer);
377
378                 (*par)->next(buf);
379                 buf->previous(*par);
380
381                 if ((*par)->next() == lastbuffer)
382                         lastbuffer = *par;
383
384                 mergeParagraph(current_view->buffer()->params,
385                                current_view->buffer()->paragraphs, *par);
386                 // store the new cursor position
387                 *par = lastbuffer;
388                 pos = lastbuffer->size();
389                 // maybe some pasting
390                 if (lastbuffer->next() && paste_the_end) {
391                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
392                                 mergeParagraph(current_view->buffer()->params,
393                                                current_view->buffer()->paragraphs, lastbuffer);
394                         } else if (!lastbuffer->next()->size()) {
395                                 lastbuffer->next()->makeSameLayout(lastbuffer);
396                                 mergeParagraph(current_view->buffer()->params, current_view->buffer()->paragraphs, lastbuffer);
397                         } else if (!lastbuffer->size()) {
398                                 lastbuffer->makeSameLayout(lastbuffer->next());
399                                 mergeParagraph(current_view->buffer()->params,
400                                                current_view->buffer()->paragraphs, lastbuffer);
401                         } else
402                                 lastbuffer->next()->stripLeadingSpaces();
403                 }
404                 // restore the simple cut buffer
405                 buf = simple_cut_clone;
406         }
407
408         return true;
409 }
410
411
412 int CutAndPaste::nrOfParagraphs()
413 {
414         if (!buf)
415                 return 0;
416
417         int n = 1;
418         Paragraph * tmppar = buf;
419         while (tmppar->next()) {
420                 ++n;
421                 tmppar = tmppar->next();
422         }
423         return n;
424 }
425
426
427 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
428                                              textclass_type c2,
429                                              Paragraph * par,
430                                              BufferParams const & /*bparams*/)
431 {
432         int ret = 0;
433         if (!par || c1 == c2)
434                 return ret;
435
436         LyXTextClass const & tclass1 = textclasslist[c1];
437         LyXTextClass const & tclass2 = textclasslist[c2];
438         ParIterator end = ParIterator();
439         for (ParIterator it = ParIterator(par); it != end; ++it) {
440                 par = *it;
441                 string const name = par->layout()->name();
442                 bool hasLayout = tclass2.hasLayout(name);
443
444                 if (hasLayout)
445                         par->layout(tclass2[name]);
446                 else
447                         par->layout(tclass2.defaultLayout());
448
449                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
450                         ++ret;
451 #if USE_BOOST_FORMAT
452                         boost::format fmt(_("Layout had to be changed from\n"
453                                             "%1$s to %2$s\n"
454                                             "because of class conversion from\n"
455                                             "%3$s to %4$s"));
456                         fmt     % name
457                                 % par->layout()->name()
458                                 % tclass1.name()
459                                 % tclass2.name();
460
461                         string const s = fmt.str();
462 #else
463                         string const s = _("Layout had to be changed from\n")
464                                 + name + _(" to ")
465                                 + par->layout()->name()
466                                 + _("\nbecause of class conversion from\n")
467                                 + tclass1.name() + _(" to ")
468                                 + tclass2.name();
469 #endif
470                         freezeUndo();
471                         InsetError * new_inset = new InsetError(s);
472                         LyXText * txt = current_view->getLyXText();
473                         LyXCursor cur = txt->cursor;
474                         txt->setCursorIntern(current_view, par, 0);
475                         txt->insertInset(current_view, new_inset);
476                         txt->fullRebreak(current_view);
477                         txt->setCursorIntern(current_view, cur.par(), cur.pos());
478                         unFreezeUndo();
479                 }
480         }
481         return ret;
482 }
483
484
485 bool CutAndPaste::checkPastePossible(Paragraph *)
486 {
487         if (!buf) return false;
488
489         return true;
490 }