]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
1ac379811b908fb3ff874e84bc2718a897b00ac7
[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                 buf = tmppar->Clone();
163                 LyXParagraph * tmppar2 = buf;
164                 
165                 while (tmppar != endpar
166                        && tmppar->next()) {
167                         tmppar = tmppar->next();
168                         tmppar2->next(tmppar->Clone());
169                         tmppar2->next()->previous(tmppar2);
170                         tmppar2 = tmppar2->next();
171                 }
172                 tmppar2->next(0);
173                 
174                 // the buf paragraph is too big
175                 LyXParagraph::size_type tmpi2 = start;
176                 for (; tmpi2; --tmpi2)
177                         buf->Erase(0);
178                 
179                 // now tmppar 2 is too big, delete all after end
180                 tmpi2 = end;
181                 while (tmppar2->size() > tmpi2) {
182                         tmppar2->Erase(tmppar2->size() - 1);
183                 }
184         }
185         return true;
186 }
187
188
189 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
190                                  int & pos, char tc)
191 {
192         if (!checkPastePossible(*par))
193                 return false;
194         
195         if (pos > (*par)->size())
196                 pos = (*par)->size();
197         
198         LyXParagraph * tmpbuf;
199         LyXParagraph * tmppar = *par;
200         int tmppos = pos;
201         
202         // There are two cases: cutbuffer only one paragraph or many
203         if (!buf->next()) {
204                 // only within a paragraph
205                 tmpbuf = buf->Clone();
206                 // Some provisions should be done here for checking
207                 // if we are inserting at the beginning of a
208                 // paragraph. If there are a space at the beginning
209                 // of the text to insert and we are inserting at
210                 // the beginning of the paragraph the space should
211                 // be removed.
212                 while (buf->size()) {
213                         // This is an attempt to fix the
214                         // "never insert a space at the
215                         // beginning of a paragraph" problem.
216                         if (!tmppos && buf->IsLineSeparator(0)) {
217                                 buf->Erase(0);
218                         } else {
219                                 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
220                                 buf->Erase(0);
221                                 if (tmppar->InsertFromMinibuffer(tmppos))
222                                         ++tmppos;
223                         }
224                 }
225                 delete buf;
226                 buf = tmpbuf;
227                 *endpar = tmppar->next();
228                 pos = tmppos;
229         } else {
230                 // many paragraphs
231                 
232                 // make a copy of the simple cut_buffer
233                 tmpbuf = buf;
234                 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
235                 LyXParagraph * tmpbuf2 = simple_cut_clone;
236                 while (tmpbuf->next()) {
237                         tmpbuf = tmpbuf->next();
238                         tmpbuf2->next(tmpbuf->Clone());
239                         tmpbuf2->next()->previous(tmpbuf2);
240                         tmpbuf2 = tmpbuf2->next();
241                 }
242                 
243                 // make sure there is no class difference
244                 SwitchLayoutsBetweenClasses(textclass, tc, buf);
245                 
246                 // make the buf exactly the same layout than
247                 // the cursor paragraph
248                 buf->MakeSameLayout(*par);
249                 
250                 // find the end of the buffer
251                 LyXParagraph * lastbuffer = buf;
252                 while (lastbuffer->next())
253                         lastbuffer = lastbuffer->next();
254                 
255                 bool paste_the_end = false;
256                 
257                 // open the paragraph for inserting the buf
258                 // if necessary
259                 if (((*par)->size() > pos) || !(*par)->next()) {
260                         (*par)->BreakParagraphConservative(current_view->buffer()->params,
261                                                            pos);
262                         paste_the_end = true;
263                 }
264                 // set the end for redoing later
265                 *endpar = (*par)->next()->next();
266                 
267                 // paste it!
268                 lastbuffer->next((*par)->next());
269                 (*par)->next()->previous(lastbuffer);
270                 
271                 (*par)->next(buf);
272                 buf->previous(*par);
273                 
274                 if ((*par)->next() == lastbuffer)
275                         lastbuffer = *par;
276                 
277                 (*par)->PasteParagraph(current_view->buffer()->params);
278                 // store the new cursor position
279                 *par = lastbuffer;
280                 pos = lastbuffer->size();
281                 // maybe some pasting
282                 if (lastbuffer->next() && paste_the_end) {
283                         if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
284                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
285                         } else if (!lastbuffer->next()->size()) {
286                                 lastbuffer->next()->MakeSameLayout(lastbuffer);
287                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
288                         } else if (!lastbuffer->size()) {
289                                 lastbuffer->MakeSameLayout(lastbuffer->next());
290                                 lastbuffer->PasteParagraph(current_view->buffer()->params);
291                         } else
292                                 lastbuffer->next()->StripLeadingSpaces(tc);
293                 }
294                 // restore the simple cut buffer
295                 buf = simple_cut_clone;
296         }
297         
298         return true;
299 }
300
301
302 int CutAndPaste::nrOfParagraphs()
303 {
304         if (!buf)
305                 return 0;
306
307         int n = 1;
308         LyXParagraph * tmppar = buf;
309         while(tmppar->next()) {
310                 ++n;
311                 tmppar = tmppar->next();
312         }
313         return n;
314 }
315
316
317 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
318                                              LyXTextClassList::size_type c2,
319                                              LyXParagraph * par)
320 {
321     int ret = 0;
322     if (!par || c1 == c2)
323                 return ret;
324
325     while (par) {
326                 string name = textclasslist.NameOfLayout(c1, par->layout);
327                 int lay = 0;
328                 pair<bool, LyXTextClass::LayoutList::size_type> pp =
329                         textclasslist.NumberOfLayout(c2, name);
330                 if (pp.first) {
331                         lay = pp.second;
332                 } else { // layout not found
333                         // use default layout "Standard" (0)
334                         lay = 0;
335                 }
336                 par->layout = lay;
337                 
338                 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
339                         ++ret;
340                         string s = _("Layout had to be changed from\n")
341                                 + name + _(" to ")
342                                 + textclasslist.NameOfLayout(c2, par->layout)
343                                 + _("\nbecause of class conversion from\n")
344                                 + textclasslist.NameOfClass(c1) + _(" to ")
345                                 + textclasslist.NameOfClass(c2);
346                         InsetError * new_inset = new InsetError(s);
347                         par->InsertInset(0, new_inset);
348                 }
349                 par = par->next();
350     }
351     return ret;
352 }
353
354
355 bool CutAndPaste::checkPastePossible(LyXParagraph *)
356 {
357     if (!buf) return false;
358
359     return true;
360 }