]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
More ascii-export fixes and when making copy of single tabular cells now the
[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                                             current_view->buffer()->params);
293                 
294                 // make the buf exactly the same layout than
295                 // the cursor paragraph
296                 buf->makeSameLayout(*par);
297                 
298                 // find the end of the buffer
299                 Paragraph * lastbuffer = buf;
300                 while (lastbuffer->next())
301                         lastbuffer = lastbuffer->next();
302                 
303                 bool paste_the_end = false;
304                 
305                 // open the paragraph for inserting the buf
306                 // if necessary
307                 if (((*par)->size() > pos) || !(*par)->next()) {
308                         (*par)->breakParagraphConservative(current_view->buffer()->params,
309                                                            pos);
310                         paste_the_end = true;
311                 }
312                 // set the end for redoing later
313                 *endpar = (*par)->next()->next();
314                 
315                 // paste it!
316                 lastbuffer->next((*par)->next());
317                 (*par)->next()->previous(lastbuffer);
318                 
319                 (*par)->next(buf);
320                 buf->previous(*par);
321                 
322                 if ((*par)->next() == lastbuffer)
323                         lastbuffer = *par;
324                 
325                 (*par)->pasteParagraph(current_view->buffer()->params);
326                 // store the new cursor position
327                 *par = lastbuffer;
328                 pos = lastbuffer->size();
329                 // maybe some pasting
330                 if (lastbuffer->next() && paste_the_end) {
331                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
332                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
333                         } else if (!lastbuffer->next()->size()) {
334                                 lastbuffer->next()->makeSameLayout(lastbuffer);
335                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
336                         } else if (!lastbuffer->size()) {
337                                 lastbuffer->makeSameLayout(lastbuffer->next());
338                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
339                         } else
340                                 lastbuffer->next()->stripLeadingSpaces(tc);
341                 }
342                 // restore the simple cut buffer
343                 buf = simple_cut_clone;
344         }
345         
346         return true;
347 }
348
349
350 int CutAndPaste::nrOfParagraphs()
351 {
352         if (!buf)
353                 return 0;
354         
355         int n = 1;
356         Paragraph * tmppar = buf;
357         while (tmppar->next()) {
358                 ++n;
359                 tmppar = tmppar->next();
360         }
361         return n;
362 }
363
364
365 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
366                                              textclass_type c2,
367                                              Paragraph * par,
368                                              BufferParams const & bparams)
369 {
370         int ret = 0;
371         if (!par || c1 == c2)
372                 return ret;
373
374         ParIterator end = ParIterator();
375         for (ParIterator it = ParIterator(par); it != end; ++it) {
376                 par = *it;
377                 string const name = textclasslist.NameOfLayout(c1, par->layout);
378                 int lay = 0;
379                 pair<bool, layout_type> pp =
380                         textclasslist.NumberOfLayout(c2, name);
381                 if (pp.first) {
382                         lay = pp.second;
383                 } else {
384                         // not found: use default layout "Standard" (0)
385                         lay = 0;
386                 }
387                 par->layout = lay;
388                 
389                 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
390                         ++ret;
391                         string const s = _("Layout had to be changed from\n")
392                                 + name + _(" to ")
393                                 + textclasslist.NameOfLayout(c2, par->layout)
394                                 + _("\nbecause of class conversion from\n")
395                                 + textclasslist.NameOfClass(c1) + _(" to ")
396                                 + textclasslist.NameOfClass(c2);
397                         InsetError * new_inset = new InsetError(s);
398                         par->insertInset(0, new_inset,
399                                          LyXFont(LyXFont::ALL_INHERIT,
400                                                  bparams.language));
401                 }
402         }
403         return ret;
404 }
405
406
407 bool CutAndPaste::checkPastePossible(Paragraph *)
408 {
409         if (!buf) return false;
410         
411         return true;
412 }