]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
b549d2013c59c6fd11bdb86e15ad346649c431b0
[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 "BufferView.h"
14 #include "buffer.h"
15 #include "lyxparagraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
19 #include "gettext.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 using std::pair;
26
27 extern BufferView * current_view;
28
29 // Jürgen, note that this means that you cannot currently have a list
30 // of selections cut/copied. So IMHO later we should have a
31 // list/vector/deque that we could store
32 // struct selection_item {
33 //       LyXParagraph * buf;
34 //       LyXTextClassList::size_type textclass;
35 // };
36 // in and some method of choosing beween them (based on the first few chars
37 // in the selection probably.) This would be a nice feature and quite
38 // easy to implement. (Lgb)
39 //
40 // Sure but I just cleaned up this code for now with the same functionality
41 // as before. I also want to add a XClipboard function so that we can copy
42 // text from LyX to some other X-application in the form of ASCII or in the
43 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
44 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
45 // then do a middle mouse button click in the application you want and have
46 // the whole formula there in LaTeX-Code. (Jug)
47
48 namespace {
49
50 LyXParagraph * buf = 0;
51 LyXTextClassList::size_type textclass = 0;
52
53 // for now here this should be in another Cut&Paste Class!
54 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
55 // member of the CutAndPaste class and in addition it was private.
56 // Perhaps it even should take a parameter? (Lgb)
57 void DeleteBuffer()
58 {
59     if (!buf)
60                 return;
61         
62     LyXParagraph * tmppar;
63         
64     while (buf) {
65                 tmppar =  buf;
66                 buf = buf->next();
67                 delete tmppar;
68     }
69     buf = 0;
70 }
71
72 } // namespace anon
73
74
75 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
76                                int start, int & end, char tc, bool doclear)
77 {
78         if (!startpar || (start > startpar->size()))
79                 return false;
80         
81         DeleteBuffer();
82         
83         textclass = tc;
84         
85         if (!(*endpar) ||
86             startpar == (*endpar)) {
87                 // only within one paragraph
88                 buf = new LyXParagraph;
89                 LyXParagraph::size_type i = start;
90                 if (end > startpar->size())
91                         end = startpar->size();
92                 for (; i < end; ++i) {
93                         startpar->CopyIntoMinibuffer(*current_view->buffer(),
94                                                      start);
95                         startpar->Erase(start);
96                         
97                         buf->InsertFromMinibuffer(buf->size());
98                 }
99                 end = start - 1;
100         } else {
101                 // more than one paragraph
102                 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
103                                                       end);
104                 *endpar = (*endpar)->next();
105                 end = 0;
106    
107                 startpar->BreakParagraphConservative(current_view->buffer()->params,
108                                                      start);
109                 
110                 // store the selection
111                 buf = startpar->next();
112                 
113                 buf->previous(0);
114                 (*endpar)->previous()->next(0);
115                 
116                 // cut the selection
117                 startpar->next(*endpar);
118                 
119                 (*endpar)->previous(startpar);
120                 
121                 // the cut selection should begin with standard layout
122                 buf->Clear(); 
123                 
124                 // paste the paragraphs again, if possible
125                 if (doclear)
126                         startpar->next()->StripLeadingSpaces(textclass);
127                 if (startpar->HasSameLayout(startpar->next()) ||
128                     !startpar->next()->size()) {
129                         startpar->PasteParagraph(current_view->buffer()->params);
130                         (*endpar) = startpar; // this because endpar gets deleted here!
131                 }
132         }
133         return true;
134 }
135
136
137 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
138                                 int start, int end, char tc)
139 {
140         if (!startpar || (start > startpar->size()))
141                 return false;
142         
143         DeleteBuffer();
144         
145         textclass = tc;
146         
147         if (!endpar ||
148             startpar == endpar) {
149                 // only within one paragraph
150                 buf = new LyXParagraph;
151                 LyXParagraph::size_type i = start;
152                 if (end > startpar->size())
153                         end = startpar->size();
154                 for (; i < end; ++i) {
155                         startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
156                         buf->InsertFromMinibuffer(buf->size());
157                 }
158         } else {
159                 // copy more than one paragraph
160                 // clone the paragraphs within the selection
161                 LyXParagraph * tmppar = startpar;
162 #if 0
163                 buf = tmppar->Clone();
164 #else
165                 buf = new LyXParagraph(*tmppar);
166 #endif
167                 LyXParagraph * tmppar2 = buf;
168                 
169                 while (tmppar != endpar
170                        && tmppar->next()) {
171                         tmppar = tmppar->next();
172 #if 0
173                         tmppar2->next(tmppar->Clone());
174 #else
175                         tmppar2->next(new LyXParagraph(*tmppar));
176 #endif
177                         tmppar2->next()->previous(tmppar2);
178                         tmppar2 = tmppar2->next();
179                 }
180                 tmppar2->next(0);
181                 
182                 // the buf paragraph is too big
183                 LyXParagraph::size_type tmpi2 = start;
184                 for (; tmpi2; --tmpi2)
185                         buf->Erase(0);
186                 
187                 // now tmppar 2 is too big, delete all after end
188                 tmpi2 = end;
189                 while (tmppar2->size() > tmpi2) {
190                         tmppar2->Erase(tmppar2->size() - 1);
191                 }
192         }
193         return true;
194 }
195
196
197 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
198                                  int & pos, char tc)
199 {
200         if (!checkPastePossible(*par))
201                 return false;
202         
203         if (pos > (*par)->size())
204                 pos = (*par)->size();
205         
206         // LyXParagraph * tmpbuf;
207         LyXParagraph * tmppar = *par;
208         int tmppos = pos;
209         
210         // There are two cases: cutbuffer only one paragraph or many
211         if (!buf->next()) {
212                 // only within a paragraph
213 #if 0
214                 LyXParagraph * tmpbuf = buf->Clone();
215 #else
216                 LyXParagraph * tmpbuf = new LyXParagraph(*buf);
217 #endif
218                 // Some provisions should be done here for checking
219                 // if we are inserting at the beginning of a
220                 // paragraph. If there are a space at the beginning
221                 // of the text to insert and we are inserting at
222                 // the beginning of the paragraph the space should
223                 // be removed.
224                 while (buf->size()) {
225                         // This is an attempt to fix the
226                         // "never insert a space at the
227                         // beginning of a paragraph" problem.
228                         if (!tmppos && buf->IsLineSeparator(0)) {
229                                 buf->Erase(0);
230                         } else {
231                                 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
232                                 buf->Erase(0);
233                                 if (tmppar->InsertFromMinibuffer(tmppos))
234                                         ++tmppos;
235                         }
236                 }
237                 delete buf;
238                 buf = tmpbuf;
239                 *endpar = tmppar->next();
240                 pos = tmppos;
241         } else {
242                 // many paragraphs
243                 
244                 // make a copy of the simple cut_buffer
245                 LyXParagraph * tmpbuf = buf;
246 #if 0
247                 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
248 #else
249                 LyXParagraph * simple_cut_clone = new LyXParagraph(*tmpbuf);
250 #endif
251                 LyXParagraph * tmpbuf2 = simple_cut_clone;
252                 while (tmpbuf->next()) {
253                         tmpbuf = tmpbuf->next();
254 #if 0
255                         tmpbuf2->next(tmpbuf->Clone());
256 #else
257                         tmpbuf2->next(new LyXParagraph(*tmpbuf));
258 #endif
259                         tmpbuf2->next()->previous(tmpbuf2);
260                         tmpbuf2 = tmpbuf2->next();
261                 }
262                 
263                 // make sure there is no class difference
264                 SwitchLayoutsBetweenClasses(textclass, tc, buf);
265                 
266                 // make the buf exactly the same layout than
267                 // the cursor paragraph
268                 buf->MakeSameLayout(*par);
269                 
270                 // find the end of the buffer
271                 LyXParagraph * lastbuffer = buf;
272                 while (lastbuffer->next())
273                         lastbuffer = lastbuffer->next();
274                 
275                 bool paste_the_end = false;
276                 
277                 // open the paragraph for inserting the buf
278                 // if necessary
279                 if (((*par)->size() > pos) || !(*par)->next()) {
280                         (*par)->BreakParagraphConservative(current_view->buffer()->params,
281                                                            pos);
282                         paste_the_end = true;
283                 }
284                 // set the end for redoing later
285                 *endpar = (*par)->next()->next();
286                 
287                 // paste it!
288                 lastbuffer->next((*par)->next());
289                 (*par)->next()->previous(lastbuffer);
290                 
291                 (*par)->next(buf);
292                 buf->previous(*par);
293                 
294                 if ((*par)->next() == lastbuffer)
295                         lastbuffer = *par;
296                 
297                 (*par)->PasteParagraph(current_view->buffer()->params);
298                 // store the new cursor position
299                 *par = lastbuffer;
300                 pos = lastbuffer->size();
301                 // maybe some pasting
302                 if (lastbuffer->next() && paste_the_end) {
303                         if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
304                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
305                         } else if (!lastbuffer->next()->size()) {
306                                 lastbuffer->next()->MakeSameLayout(lastbuffer);
307                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
308                         } else if (!lastbuffer->size()) {
309                                 lastbuffer->MakeSameLayout(lastbuffer->next());
310                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
311                         } else
312                                 lastbuffer->next()->StripLeadingSpaces(tc);
313                 }
314                 // restore the simple cut buffer
315                 buf = simple_cut_clone;
316         }
317         
318         return true;
319 }
320
321
322 int CutAndPaste::nrOfParagraphs()
323 {
324         if (!buf)
325                 return 0;
326
327         int n = 1;
328         LyXParagraph * tmppar = buf;
329         while(tmppar->next()) {
330                 ++n;
331                 tmppar = tmppar->next();
332         }
333         return n;
334 }
335
336
337 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
338                                              LyXTextClassList::size_type c2,
339                                              LyXParagraph * par)
340 {
341     int ret = 0;
342     if (!par || c1 == c2)
343                 return ret;
344
345     while (par) {
346                 string name = textclasslist.NameOfLayout(c1, par->layout);
347                 int lay = 0;
348                 pair<bool, LyXTextClass::LayoutList::size_type> pp =
349                         textclasslist.NumberOfLayout(c2, name);
350                 if (pp.first) {
351                         lay = pp.second;
352                 } else { // layout not found
353                         // use default layout "Standard" (0)
354                         lay = 0;
355                 }
356                 par->layout = lay;
357                 
358                 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
359                         ++ret;
360                         string s = _("Layout had to be changed from\n")
361                                 + name + _(" to ")
362                                 + textclasslist.NameOfLayout(c2, par->layout)
363                                 + _("\nbecause of class conversion from\n")
364                                 + textclasslist.NameOfClass(c1) + _(" to ")
365                                 + textclasslist.NameOfClass(c2);
366                         InsetError * new_inset = new InsetError(s);
367                         par->InsertInset(0, new_inset);
368                 }
369                 par = par->next();
370     }
371     return ret;
372 }
373
374
375 bool CutAndPaste::checkPastePossible(LyXParagraph *)
376 {
377     if (!buf) return false;
378
379     return true;
380 }