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