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