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