]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
34dbf7ed11d38377d672d47928aa8fc543963c1a
[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 "debug.h"
18 #include "BufferView.h"
19 #include "buffer.h"
20 #include "paragraph.h"
21 #include "ParagraphParameters.h"
22 #include "lyxcursor.h"
23 #include "gettext.h"
24 #include "iterators.h"
25 #include "lyxtextclasslist.h"
26
27 #include "insets/inseterror.h"
28
29 using std::pair;
30 using lyx::pos_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 #if 0
223         // Paragraph * tmpbuf;
224         Paragraph * tmppar = *par;
225         int tmppos = pos;
226
227         // There are two cases: cutbuffer only one paragraph or many
228         if (!buf->next()) {
229                 // only within a paragraph
230                 Paragraph * tmpbuf = new Paragraph(*buf, false);
231                 
232                 // Some provisions should be done here for checking
233                 // if we are inserting at the beginning of a
234                 // paragraph. If there are a space at the beginning
235                 // of the text to insert and we are inserting at
236                 // the beginning of the paragraph the space should
237                 // be removed.
238                 while (buf->size()) {
239                         // This is an attempt to fix the
240                         // "never insert a space at the
241                         // beginning of a paragraph" problem.
242                         if (!tmppos && buf->isLineSeparator(0)) {
243                                 buf->erase(0);
244                         } else {
245                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
246                                 buf->erase(0);
247                                 if (tmppar->insertFromMinibuffer(tmppos))
248                                         ++tmppos;
249                         }
250                 }
251                 delete buf;
252                 buf = tmpbuf;
253                 *endpar = tmppar->next();
254                 pos = tmppos;
255         } else
256 #endif
257         {
258                 // many paragraphs
259                 
260                 // make a copy of the simple cut_buffer
261                 Paragraph * tmpbuf = buf;
262                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
263                 Paragraph * tmpbuf2 = simple_cut_clone;
264                 
265                 while (tmpbuf->next()) {
266                         tmpbuf = tmpbuf->next();
267                         tmpbuf2->next(new Paragraph(*tmpbuf, false));
268                         tmpbuf2->next()->previous(tmpbuf2);
269                         tmpbuf2 = tmpbuf2->next();
270                 }
271
272                 // now remove all out of the buffer which is NOT allowed in the
273                 // new environment and set also another font if that is required
274                 tmpbuf = buf;
275                 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
276                 // temporary set *par as previous of tmpbuf as we might have to realize
277                 // the font.
278                 tmpbuf->previous(*par);
279                 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter(current_view->buffer());
280                 while(tmpbuf) {
281                         // if we have a negative jump so that the depth would go below
282                         // 0 depth then we have to redo the delta to this new max depth
283                         // level so that subsequent paragraphs are aligned correctly to
284                         // this paragraph at level 0.
285                         if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
286                                 depth_delta = 0;
287                         // set the right depth so that we are not too deep or shallow.
288                         tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
289                         if (tmpbuf->params().depth() > max_depth)
290                                 tmpbuf->params().depth(max_depth);
291                         // only set this from the 2nd on as the 2nd depends for maxDepth
292                         // still on *par
293                         if (tmpbuf->previous() != (*par))
294                                 max_depth = tmpbuf->getMaxDepthAfter(current_view->buffer());
295                         // set the inset owner of this paragraph
296                         tmpbuf->setInsetOwner((*par)->inInset());
297                         for(pos_type i = 0; i < tmpbuf->size(); ++i) {
298                                 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
299                                         if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
300                                         {
301                                                 tmpbuf->erase(i--);
302                                         }
303                                 } else {
304                                         LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
305                                         LyXFont f2 = f1;
306                                         if (!(*par)->checkInsertChar(f1)) {
307                                                 tmpbuf->erase(i--);
308                                         } else if (f1 != f2) {
309                                                 tmpbuf->setFont(i, f1);
310                                         }
311                                 }
312                         }
313                         tmpbuf = tmpbuf->next();
314                 }
315                 // now reset it to 0
316                 buf->previous(0);
317                 
318                 // make sure there is no class difference
319                 SwitchLayoutsBetweenClasses(textclass, tc, buf,
320                                             current_view->buffer()->params);
321                 
322                 // make the buf exactly the same layout than
323                 // the cursor paragraph
324                 buf->makeSameLayout(*par);
325                 
326                 // find the end of the buffer
327                 Paragraph * lastbuffer = buf;
328                 while (lastbuffer->next())
329                         lastbuffer = lastbuffer->next();
330                 
331                 bool paste_the_end = false;
332                 
333                 // open the paragraph for inserting the buf
334                 // if necessary
335                 if (((*par)->size() > pos) || !(*par)->next()) {
336                         (*par)->breakParagraphConservative(current_view->buffer()->params,
337                                                            pos);
338                         paste_the_end = true;
339                 }
340                 // set the end for redoing later
341                 *endpar = (*par)->next()->next();
342                 
343                 // paste it!
344                 lastbuffer->next((*par)->next());
345                 (*par)->next()->previous(lastbuffer);
346                 
347                 (*par)->next(buf);
348                 buf->previous(*par);
349                 
350                 if ((*par)->next() == lastbuffer)
351                         lastbuffer = *par;
352                 
353                 (*par)->pasteParagraph(current_view->buffer()->params);
354                 // store the new cursor position
355                 *par = lastbuffer;
356                 pos = lastbuffer->size();
357                 // maybe some pasting
358                 if (lastbuffer->next() && paste_the_end) {
359                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
360                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
361                         } else if (!lastbuffer->next()->size()) {
362                                 lastbuffer->next()->makeSameLayout(lastbuffer);
363                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
364                         } else if (!lastbuffer->size()) {
365                                 lastbuffer->makeSameLayout(lastbuffer->next());
366                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
367                         } else
368                                 lastbuffer->next()->stripLeadingSpaces(tc);
369                 }
370                 // restore the simple cut buffer
371                 buf = simple_cut_clone;
372         }
373         
374         return true;
375 }
376
377
378 int CutAndPaste::nrOfParagraphs()
379 {
380         if (!buf)
381                 return 0;
382         
383         int n = 1;
384         Paragraph * tmppar = buf;
385         while (tmppar->next()) {
386                 ++n;
387                 tmppar = tmppar->next();
388         }
389         return n;
390 }
391
392
393 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
394                                              textclass_type c2,
395                                              Paragraph * par,
396                                              BufferParams const & bparams)
397 {
398         int ret = 0;
399         if (!par || c1 == c2)
400                 return ret;
401
402         ParIterator end = ParIterator();
403         for (ParIterator it = ParIterator(par); it != end; ++it) {
404                 par = *it;
405                 string const name = par->layout();
406                 LyXTextClass const & tclass = textclasslist[c2];
407                 
408                 bool hasLayout = tclass.hasLayout(name);
409                 
410                 string lay = tclass.defaultLayoutName();
411                 if (hasLayout) {
412                         lay = name;
413                 } else {
414                         // not found: use default layout
415                         lay = tclass.defaultLayoutName();
416                 }
417                 par->layout(lay);
418                 
419                 if (name != par->layout()) {
420                         ++ret;
421                         string const s = _("Layout had to be changed from\n")
422                                 + name + _(" to ")
423                                 + par->layout()
424                                 + _("\nbecause of class conversion from\n")
425                                 + textclasslist[c1].name() + _(" to ")
426                                 + textclasslist[c2].name();
427                         InsetError * new_inset = new InsetError(s);
428                         par->insertInset(0, new_inset,
429                                          LyXFont(LyXFont::ALL_INHERIT,
430                                                  bparams.language));
431                 }
432         }
433         return ret;
434 }
435
436
437 bool CutAndPaste::checkPastePossible(Paragraph *)
438 {
439         if (!buf) return false;
440         
441         return true;
442 }