]> git.lyx.org Git - features.git/blob - src/CutAndPaste.C
remoev debug
[features.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          
138                                 delete pit;
139                         }
140                 }
141  
142                 if (pit == *endpar)
143                         break;
144  
145                 pit = next;
146         }
147
148 #if 0 // FIXME: why for cut but not copy ? 
149         // the cut selection should begin with standard layout
150         if (realcut) {
151                 buf->params().clear();
152                 buf->bibkey = 0;
153                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
154         }
155 #endif 
156
157         if (!startpar->next())
158                 return true;
159  
160         Buffer * buffer = current_view->buffer();
161
162         if (doclear) {
163                 startpar->next()->stripLeadingSpaces();
164         }
165
166         if (!actually_erased)
167                 return true;
168  
169         // paste the paragraphs again, if possible
170         if (startpar->hasSameLayout(startpar->next()) ||
171             startpar->next()->empty()) {
172                 mergeParagraph(buffer->params, 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, *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, *par);
385                 // store the new cursor position
386                 *par = lastbuffer;
387                 pos = lastbuffer->size();
388                 // maybe some pasting
389                 if (lastbuffer->next() && paste_the_end) {
390                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
391                                 mergeParagraph(current_view->buffer()->params, lastbuffer);
392                         } else if (!lastbuffer->next()->size()) {
393                                 lastbuffer->next()->makeSameLayout(lastbuffer);
394                                 mergeParagraph(current_view->buffer()->params, lastbuffer);
395                         } else if (!lastbuffer->size()) {
396                                 lastbuffer->makeSameLayout(lastbuffer->next());
397                                 mergeParagraph(current_view->buffer()->params, lastbuffer);
398                         } else
399                                 lastbuffer->next()->stripLeadingSpaces();
400                 }
401                 // restore the simple cut buffer
402                 buf = simple_cut_clone;
403         }
404
405         return true;
406 }
407
408
409 int CutAndPaste::nrOfParagraphs()
410 {
411         if (!buf)
412                 return 0;
413
414         int n = 1;
415         Paragraph * tmppar = buf;
416         while (tmppar->next()) {
417                 ++n;
418                 tmppar = tmppar->next();
419         }
420         return n;
421 }
422
423
424 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
425                                              textclass_type c2,
426                                              Paragraph * par,
427                                              BufferParams const & /*bparams*/)
428 {
429         int ret = 0;
430         if (!par || c1 == c2)
431                 return ret;
432
433         LyXTextClass const & tclass1 = textclasslist[c1];
434         LyXTextClass const & tclass2 = textclasslist[c2];
435         ParIterator end = ParIterator();
436         for (ParIterator it = ParIterator(par); it != end; ++it) {
437                 par = *it;
438                 string const name = par->layout()->name();
439                 bool hasLayout = tclass2.hasLayout(name);
440
441                 if (hasLayout)
442                         par->layout(tclass2[name]);
443                 else
444                         par->layout(tclass2.defaultLayout());
445
446                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
447                         ++ret;
448 #if USE_BOOST_FORMAT
449                         boost::format fmt(_("Layout had to be changed from\n"
450                                             "%1$s to %2$s\n"
451                                             "because of class conversion from\n"
452                                             "%3$s to %4$s"));
453                         fmt     % name
454                                 % par->layout()->name()
455                                 % tclass1.name()
456                                 % tclass2.name();
457
458                         string const s = fmt.str();
459 #else
460                         string const s = _("Layout had to be changed from\n")
461                                 + name + _(" to ")
462                                 + par->layout()->name()
463                                 + _("\nbecause of class conversion from\n")
464                                 + tclass1.name() + _(" to ")
465                                 + tclass2.name();
466 #endif
467                         freezeUndo();
468                         InsetError * new_inset = new InsetError(s);
469                         LyXText * txt = current_view->getLyXText();
470                         LyXCursor cur = txt->cursor;
471                         txt->setCursorIntern(current_view, par, 0);
472                         txt->insertInset(current_view, new_inset);
473                         txt->fullRebreak(current_view);
474                         txt->setCursorIntern(current_view, cur.par(), cur.pos());
475                         unFreezeUndo();
476                 }
477         }
478         return ret;
479 }
480
481
482 bool CutAndPaste::checkPastePossible(Paragraph *)
483 {
484         if (!buf) return false;
485
486         return true;
487 }