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