]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
add ParagraphList::erase, make mergeParagraph take a Buffer* arg, adjust other funcs...
[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 (1) {
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                 mergeParagraph(buffer, startpar);
169                 // this because endpar gets deleted here!
170                 (*endpar) = startpar;
171         }
172
173         return true;
174 }
175
176
177 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
178                                 int start, int end, char tc)
179 {
180         if (!startpar || (start > startpar->size()))
181                 return false;
182
183         DeleteBuffer();
184
185         textclass = tc;
186
187         if (!endpar || startpar == endpar) {
188                 // only within one paragraph
189                 buf = new Paragraph;
190                 buf->layout(startpar->layout());
191                 pos_type i = start;
192                 if (end > startpar->size())
193                         end = startpar->size();
194                 for (; i < end; ++i) {
195                         startpar->copyIntoMinibuffer(*current_view->buffer(), i);
196                         buf->insertFromMinibuffer(buf->size());
197                 }
198         } else {
199                 // copy more than one paragraph
200                 // clone the paragraphs within the selection
201                 Paragraph * tmppar = startpar;
202                 buf = new Paragraph(*tmppar, false);
203                 Paragraph * tmppar2 = buf;
204                 tmppar2->cleanChanges();
205
206                 while (tmppar != endpar
207                        && tmppar->next()) {
208                         tmppar = tmppar->next();
209                         tmppar2->next(new Paragraph(*tmppar, false));
210                         tmppar2->next()->previous(tmppar2);
211                         tmppar2 = tmppar2->next();
212                         // reset change info
213                         tmppar2->cleanChanges();
214                 }
215                 tmppar2->next(0);
216
217                 // the buf paragraph is too big
218                 pos_type tmpi2 = start;
219                 for (; tmpi2; --tmpi2)
220                         buf->erase(0);
221
222                 // now tmppar 2 is too big, delete all after end
223                 tmpi2 = end;
224                 while (tmppar2->size() > tmpi2) {
225                         tmppar2->erase(tmppar2->size() - 1);
226                 }
227                 // this paragraph's are of noone's owner!
228                 tmppar = buf;
229                 while (tmppar) {
230                         tmppar->setInsetOwner(0);
231                         tmppar = tmppar->next();
232                 }
233         }
234         return true;
235 }
236
237
238 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
239                                  int & pos, char tc)
240 {
241         if (!checkPastePossible(*par))
242                 return false;
243
244         if (pos > (*par)->size())
245                 pos = (*par)->size();
246
247 #if 0
248         // Paragraph * tmpbuf;
249         Paragraph * tmppar = *par;
250         int tmppos = pos;
251
252         // There are two cases: cutbuffer only one paragraph or many
253         if (!buf->next()) {
254                 // only within a paragraph
255                 Paragraph * tmpbuf = new Paragraph(*buf, false);
256
257                 // Some provisions should be done here for checking
258                 // if we are inserting at the beginning of a
259                 // paragraph. If there are a space at the beginning
260                 // of the text to insert and we are inserting at
261                 // the beginning of the paragraph the space should
262                 // be removed.
263                 while (buf->size()) {
264                         // This is an attempt to fix the
265                         // "never insert a space at the
266                         // beginning of a paragraph" problem.
267                         if (!tmppos && buf->isLineSeparator(0)) {
268                                 buf->erase(0);
269                         } else {
270                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
271                                 buf->erase(0);
272                                 if (tmppar->insertFromMinibuffer(tmppos))
273                                         ++tmppos;
274                         }
275                 }
276                 delete buf;
277                 buf = tmpbuf;
278                 *endpar = tmppar->next();
279                 pos = tmppos;
280         } else
281 #endif
282         {
283                 // many paragraphs
284
285                 // make a copy of the simple cut_buffer
286                 Paragraph * tmpbuf = buf;
287                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
288                 Paragraph * tmpbuf2 = simple_cut_clone;
289
290                 while (tmpbuf->next()) {
291                         tmpbuf = tmpbuf->next();
292                         tmpbuf2->next(new Paragraph(*tmpbuf, false));
293                         tmpbuf2->next()->previous(tmpbuf2);
294                         tmpbuf2 = tmpbuf2->next();
295                 }
296
297                 // now remove all out of the buffer which is NOT allowed in the
298                 // new environment and set also another font if that is required
299                 tmpbuf = buf;
300                 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
301                 // temporary set *par as previous of tmpbuf as we might have to realize
302                 // the font.
303                 tmpbuf->previous(*par);
304
305                 // make sure there is no class difference
306                 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
307                                             current_view->buffer()->params);
308
309                 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
310
311                 while(tmpbuf) {
312                         // if we have a negative jump so that the depth would go below
313                         // 0 depth then we have to redo the delta to this new max depth
314                         // level so that subsequent paragraphs are aligned correctly to
315                         // this paragraph at level 0.
316                         if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
317                                 depth_delta = 0;
318                         // set the right depth so that we are not too deep or shallow.
319                         tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
320                         if (tmpbuf->params().depth() > max_depth)
321                                 tmpbuf->params().depth(max_depth);
322                         // only set this from the 2nd on as the 2nd depends for maxDepth
323                         // still on *par
324                         if (tmpbuf->previous() != (*par))
325                                 max_depth = tmpbuf->getMaxDepthAfter();
326                         // set the inset owner of this paragraph
327                         tmpbuf->setInsetOwner((*par)->inInset());
328                         for(pos_type i = 0; i < tmpbuf->size(); ++i) {
329                                 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
330                                         if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
331                                         {
332                                                 tmpbuf->erase(i--);
333                                         }
334                                 } else {
335                                         LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
336                                         LyXFont f2 = f1;
337                                         if (!(*par)->checkInsertChar(f1)) {
338                                                 tmpbuf->erase(i--);
339                                         } else if (f1 != f2) {
340                                                 tmpbuf->setFont(i, f1);
341                                         }
342                                 }
343                         }
344                         tmpbuf = tmpbuf->next();
345                 }
346                 // now reset it to 0
347                 buf->previous(0);
348
349                 // make the buf exactly the same layout than
350                 // the cursor paragraph
351                 buf->makeSameLayout(*par);
352
353                 // find the end of the buffer
354                 Paragraph * lastbuffer = buf;
355                 while (lastbuffer->next())
356                         lastbuffer = lastbuffer->next();
357
358                 bool paste_the_end = false;
359
360                 // open the paragraph for inserting the buf
361                 // if necessary
362                 if (((*par)->size() > pos) || !(*par)->next()) {
363                         breakParagraphConservative(
364                                 current_view->buffer()->params, *par, pos);
365                         paste_the_end = true;
366                 }
367                 // set the end for redoing later
368                 *endpar = (*par)->next()->next();
369
370                 // paste it!
371                 lastbuffer->next((*par)->next());
372                 (*par)->next()->previous(lastbuffer);
373
374                 (*par)->next(buf);
375                 buf->previous(*par);
376
377                 if ((*par)->next() == lastbuffer)
378                         lastbuffer = *par;
379
380                 mergeParagraph(current_view->buffer(), *par);
381                 // store the new cursor position
382                 *par = lastbuffer;
383                 pos = lastbuffer->size();
384                 // maybe some pasting
385                 if (lastbuffer->next() && paste_the_end) {
386                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
387                                 mergeParagraph(current_view->buffer(), lastbuffer);
388                         } else if (!lastbuffer->next()->size()) {
389                                 lastbuffer->next()->makeSameLayout(lastbuffer);
390                                 mergeParagraph(current_view->buffer(), lastbuffer);
391                         } else if (!lastbuffer->size()) {
392                                 lastbuffer->makeSameLayout(lastbuffer->next());
393                                 mergeParagraph(current_view->buffer(), lastbuffer);
394                         } else
395                                 lastbuffer->next()->stripLeadingSpaces();
396                 }
397                 // restore the simple cut buffer
398                 buf = simple_cut_clone;
399         }
400
401         return true;
402 }
403
404
405 int CutAndPaste::nrOfParagraphs()
406 {
407         if (!buf)
408                 return 0;
409
410         int n = 1;
411         Paragraph * tmppar = buf;
412         while (tmppar->next()) {
413                 ++n;
414                 tmppar = tmppar->next();
415         }
416         return n;
417 }
418
419
420 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
421                                              textclass_type c2,
422                                              Paragraph * par,
423                                              BufferParams const & /*bparams*/)
424 {
425         int ret = 0;
426         if (!par || c1 == c2)
427                 return ret;
428
429         LyXTextClass const & tclass1 = textclasslist[c1];
430         LyXTextClass const & tclass2 = textclasslist[c2];
431         ParIterator end = ParIterator();
432         for (ParIterator it = ParIterator(par); it != end; ++it) {
433                 par = *it;
434                 string const name = par->layout()->name();
435                 bool hasLayout = tclass2.hasLayout(name);
436
437                 if (hasLayout)
438                         par->layout(tclass2[name]);
439                 else
440                         par->layout(tclass2.defaultLayout());
441
442                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
443                         ++ret;
444 #if USE_BOOST_FORMAT
445                         boost::format fmt(_("Layout had to be changed from\n"
446                                             "%1$s to %2$s\n"
447                                             "because of class conversion from\n"
448                                             "%3$s to %4$s"));
449                         fmt     % name
450                                 % par->layout()->name()
451                                 % tclass1.name()
452                                 % tclass2.name();
453
454                         string const s = fmt.str();
455 #else
456                         string const s = _("Layout had to be changed from\n")
457                                 + name + _(" to ")
458                                 + par->layout()->name()
459                                 + _("\nbecause of class conversion from\n")
460                                 + tclass1.name() + _(" to ")
461                                 + tclass2.name();
462 #endif
463                         freezeUndo();
464                         InsetError * new_inset = new InsetError(s);
465                         LyXText * txt = current_view->getLyXText();
466                         LyXCursor cur = txt->cursor;
467                         txt->setCursorIntern(current_view, par, 0);
468                         txt->insertInset(current_view, new_inset);
469                         txt->fullRebreak(current_view);
470                         txt->setCursorIntern(current_view, cur.par(), cur.pos());
471                         unFreezeUndo();
472                 }
473         }
474         return ret;
475 }
476
477
478 bool CutAndPaste::checkPastePossible(Paragraph *)
479 {
480         if (!buf) return false;
481
482         return true;
483 }