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