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