]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
d2707ff01cee2d95c6c5436043a9941f0b5d13c0
[lyx.git] / src / CutAndPaste.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995-2000 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #include "CutAndPaste.h"
13 #include "lyxparagraph.h"
14 #include "insets/inseterror.h"
15 #include "lyx_gui_misc.h"
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 using std::pair;
22
23 static LyXParagraph * buf = 0;
24
25 // for now here this should be in another Cut&Paste Class!
26 //
27 void CutAndPaste::DeleteBuffer()
28 {
29         if (!buf) return;
30         
31         LyXParagraph * tmppar;
32         
33         while (buf) {
34                 tmppar =  buf;
35                 buf = buf->next;
36                 delete tmppar;
37         }
38         buf = 0;
39 }
40
41
42 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
43                                int start, int & end, char tc, bool doclear)
44 {
45     if (!startpar || (start > startpar->Last()))
46         return false;
47
48     DeleteBuffer();
49
50     textclass = tc;
51
52     if (!(*endpar) || (startpar->ParFromPos(start) ==
53                        (*endpar)->ParFromPos(end))) {
54         // only within one paragraph
55         buf = new LyXParagraph;
56         LyXParagraph::size_type i = start;
57         if (end > startpar->Last())
58             end = startpar->Last();
59         for (; i < end; ++i) {
60             startpar->CopyIntoMinibuffer(start);
61             /* table stuff -- begin */
62             if (startpar->table && startpar->IsNewline(start)) {
63                 ++start;
64             } else {
65                 /* table stuff -- end */
66                 startpar->Erase(start);
67             }
68             buf->InsertFromMinibuffer(buf->Last());
69         }
70     } else {
71         // more than one paragraph
72         (*endpar)->BreakParagraphConservative(end);
73         *endpar = (*endpar)->Next();
74         end = 0;
75    
76         startpar->BreakParagraphConservative(start);
77
78         // store the selection
79         buf = startpar->ParFromPos(start)->next;
80         buf->previous = 0;
81         (*endpar)->previous->next = 0;
82
83         // cut the selection
84         startpar->ParFromPos(start)->next = (*endpar);
85         
86         (*endpar)->previous = startpar->ParFromPos(start);
87
88         // care about footnotes
89         if (buf->footnoteflag) {
90             LyXParagraph * tmppar = buf;
91             while (tmppar){
92                 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
93                 tmppar = tmppar->next;
94             }
95         }
96
97         // the cut selection should begin with standard layout
98         buf->Clear(); 
99    
100         // paste the paragraphs again, if possible
101         if (doclear)
102             startpar->Next()->ClearParagraph();
103         if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) || 
104             !startpar->Next()->Last())
105             startpar->ParFromPos(start)->PasteParagraph();
106     }
107     return true;
108 }
109
110
111 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
112                                 int start, int end, char tc)
113 {
114     if (!startpar || (start > startpar->Last()))
115         return false;
116
117     DeleteBuffer();
118
119     textclass = tc;
120
121     if (!(endpar) || (startpar->ParFromPos(start) ==
122                        (endpar)->ParFromPos(end))) {
123         // only within one paragraph
124         buf = new LyXParagraph;
125         LyXParagraph::size_type i = start;
126         if (end > startpar->Last())
127             end = startpar->Last();
128         for (; i < end; ++i) {
129             startpar->CopyIntoMinibuffer(i);
130             buf->InsertFromMinibuffer(buf->Last());
131         }
132     } else {
133         // copy more than one paragraph
134         // clone the paragraphs within the selection
135         LyXParagraph *tmppar = startpar->ParFromPos(start);
136         buf = tmppar->Clone();
137         LyXParagraph *tmppar2 = buf;
138      
139         while (tmppar != endpar->ParFromPos(end)
140                && tmppar->next) {
141             tmppar = tmppar->next;
142             tmppar2->next = tmppar->Clone();
143             tmppar2->next->previous = tmppar2;
144             tmppar2 = tmppar2->next;
145         }
146         tmppar2->next = 0;
147
148         // care about footnotes
149         if (buf->footnoteflag) {
150             tmppar = buf;
151             while (tmppar) {
152                 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
153                 tmppar = tmppar->next;
154             }
155         }
156         
157         // the buf paragraph is too big
158         LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
159         for (; tmpi2; --tmpi2)
160             buf->Erase(0);
161         
162         // now tmppar 2 is too big, delete all after end
163         
164         tmpi2 = endpar->PositionInParFromPos(end);
165         while (tmppar2->size() > tmpi2) {
166             tmppar2->Erase(tmppar2->size() - 1);
167         }
168     }
169     return true;
170 }
171
172
173 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
174                                  int & pos, char tc)
175 {
176     if (!checkPastePossible(*par, pos))
177         return false;
178
179     if (pos > (*par)->Last())
180         pos = (*par)->Last();
181
182     LyXParagraph * tmpbuf;
183     LyXParagraph * tmppar = *par;
184     int tmppos = pos;
185
186     // There are two cases: cutbuffer only one paragraph or many
187     if (!buf->next) {
188         // only within a paragraph
189         tmpbuf = buf->Clone();
190         /* table stuff -- begin */
191         bool table_too_small = false;
192         if ((*par)->table) {
193             while (buf->size() && !table_too_small) {
194                 if (buf->IsNewline(0)){
195                     while((tmppos < tmppar->Last()) &&
196                           !tmppar->IsNewline(tmppos))
197                         ++tmppos;
198                     buf->Erase(0);
199                     if (tmppos < tmppar->Last())
200                         ++tmppos;
201                     else
202                         table_too_small = true;
203                 } else {
204                     // This is an attempt to fix the
205                     // "never insert a space at the
206                     // beginning of a paragraph" problem.
207                     if (!tmppos && buf->IsLineSeparator(0)) {
208                         buf->Erase(0);
209                     } else {
210                         buf->CutIntoMinibuffer(0);
211                         buf->Erase(0);
212                         if (tmppar->InsertFromMinibuffer(tmppos))
213                             ++tmppos;
214                     }
215                 }
216             }
217         } else {
218             /* table stuff -- end */
219             // Some provisions should be done here for checking
220             // if we are inserting at the beginning of a
221             // paragraph. If there are a space at the beginning
222             // of the text to insert and we are inserting at
223             // the beginning of the paragraph the space should
224             // be removed.
225             while (buf->size()) {
226                 // This is an attempt to fix the
227                 // "never insert a space at the
228                 // beginning of a paragraph" problem.
229                 if (!tmppos && buf->IsLineSeparator(0)) {
230                     buf->Erase(0);
231                 } else {
232                     buf->CutIntoMinibuffer(0);
233                     buf->Erase(0);
234                     if (tmppar->InsertFromMinibuffer(tmppos))
235                         ++tmppos;
236                 }
237             }
238         }
239         delete buf;
240         buf = tmpbuf;
241         *endpar = tmppar->Next();
242         pos = tmppos;
243     } else {
244         // many paragraphs
245
246         // make a copy of the simple cut_buffer
247         tmpbuf = buf;
248         LyXParagraph * simple_cut_clone = tmpbuf->Clone();
249         LyXParagraph * tmpbuf2 = simple_cut_clone;
250         if ((*par)->footnoteflag){
251             tmpbuf->footnoteflag = (*par)->footnoteflag;
252             tmpbuf->footnotekind = (*par)->footnotekind;
253         }
254         while (tmpbuf->next) {
255             tmpbuf = tmpbuf->next;
256             tmpbuf2->next = tmpbuf->Clone();
257             tmpbuf2->next->previous = tmpbuf2;
258             tmpbuf2 = tmpbuf2->next;
259             if ((*par)->footnoteflag){
260                 tmpbuf->footnoteflag = (*par)->footnoteflag;
261                 tmpbuf->footnotekind = (*par)->footnotekind;
262             }
263         }
264         
265         // make sure there is no class difference
266         SwitchLayoutsBetweenClasses(textclass, tc, buf);
267         
268         // make the buf exactly the same layout than
269         // the cursor paragraph
270         buf->MakeSameLayout(*par);
271         
272         // find the end of the buffer
273         LyXParagraph * lastbuffer = buf;
274         while (lastbuffer->Next())
275             lastbuffer = lastbuffer->Next();
276         
277         bool paste_the_end = false;
278         
279         // open the paragraph for inserting the buf
280         // if necessary
281         if (((*par)->Last() > pos) || !(*par)->Next()) {
282             (*par)->BreakParagraphConservative(pos);
283             paste_the_end = true;
284         }
285         
286         // set the end for redoing later
287         *endpar = (*par)->ParFromPos(pos)->next->Next();
288         
289         // paste it!
290         lastbuffer->ParFromPos(lastbuffer->Last())->next =
291             (*par)->ParFromPos(pos)->next;
292         (*par)->ParFromPos(pos)->next->previous =
293             lastbuffer->ParFromPos(lastbuffer->Last());
294         
295         (*par)->ParFromPos(pos)->next = buf;
296         buf->previous = (*par)->ParFromPos(pos);
297         
298         if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
299             lastbuffer = *par;
300         
301         (*par)->ParFromPos(pos)->PasteParagraph();
302         
303         // store the new cursor position
304         tmppar = lastbuffer;
305         tmppos = lastbuffer->Last();
306         
307         // maybe some pasting
308         if (lastbuffer->Next() && paste_the_end) {
309             if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
310                 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
311             } else if (!lastbuffer->Next()->Last()) {
312                 lastbuffer->Next()->MakeSameLayout(lastbuffer);
313                 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
314             } else if (!lastbuffer->Last()) {
315                 lastbuffer->MakeSameLayout(lastbuffer->next);
316                 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph();
317             } else
318                 lastbuffer->Next()->ClearParagraph();
319         }
320         // restore the simple cut buffer
321         buf = simple_cut_clone;
322         pos = tmppos;
323     }
324
325     return true;
326 }
327
328
329 int CutAndPaste::nrOfParagraphs() const
330 {
331         if (!buf) return 0;
332
333         int n = 1;
334         LyXParagraph *tmppar = buf;
335         while(tmppar->next) {
336                 ++n;
337                 tmppar = tmppar->next;
338         }
339         return n;
340 }
341
342
343 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
344                                              LyXTextClassList::size_type c2,
345                                              LyXParagraph * par)
346 {
347     int ret = 0;
348     if (!par || c1 == c2)
349         return ret;
350     par = par->FirstPhysicalPar();
351     while (par) {
352         string name = textclasslist.NameOfLayout(c1, par->layout);
353         int lay = 0;
354         pair<bool, LyXTextClass::LayoutList::size_type> pp =
355             textclasslist.NumberOfLayout(c2, name);
356         if (pp.first) {
357             lay = pp.second;
358         } else { // layout not found
359             // use default layout "Standard" (0)
360             lay = 0;
361         }
362         par->layout = lay;
363         
364         if (name != textclasslist.NameOfLayout(c2, par->layout)) {
365             ++ret;
366             string s = "Layout had to be changed from\n"
367                 + name + " to "
368                 + textclasslist.NameOfLayout(c2, par->layout)
369                 + "\nbecause of class conversion from\n"
370                 + textclasslist.NameOfClass(c1) + " to "
371                 + textclasslist.NameOfClass(c2);
372             InsetError * new_inset = new InsetError(s);
373             par->InsertChar(0, LyXParagraph::META_INSET);
374             par->InsertInset(0, new_inset);
375         }
376         
377         par = par->next;
378     }
379     return ret;
380 }
381
382
383 LyXTextClassList::size_type CutAndPaste::getBufferTextClass() const
384 {
385     return textclass;
386 }
387
388
389 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int) const
390 {
391     if (!buf) return false;
392
393     LyXParagraph * tmppar;
394
395     // be carefull with footnotes in footnotes
396     if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
397         // check whether the cut_buffer includes a footnote
398         tmppar = buf;
399         while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
400             tmppar = tmppar->next;
401       
402         if (tmppar) {
403             WriteAlert(_("Impossible operation"),
404                        _("Can't paste float into float!"),
405                        _("Sorry."));
406             return false;
407         }
408     }
409     /* table stuff -- begin */
410     if (par->table) {
411         if (buf->next) {
412             WriteAlert(_("Impossible operation"),
413                        _("Table cell cannot include more than one paragraph!"),
414                        _("Sorry."));
415             return false;
416         }
417     }
418     /* table stuff -- end */
419     return true;
420 }