]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
* CutAndPaste.C (cutSelection): another layout settings forgotten.
[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                         buf->layout(startpar->layout());
98                 }
99                 pos_type i = start;
100                 if (end > startpar->size())
101                         end = startpar->size();
102                 for (; i < end; ++i) {
103                         if (realcut)
104                                 startpar->copyIntoMinibuffer(*current_view->buffer(),
105                                                              start);
106                         startpar->erase(start);
107                         if (realcut)
108                                 buf->insertFromMinibuffer(buf->size());
109                 }
110                 end = start - 1;
111         } else {
112                 // more than one paragraph
113                 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
114                                                       end);
115                 *endpar = (*endpar)->next();
116                 end = 0;
117                 
118                 startpar->breakParagraphConservative(current_view->buffer()->params,
119                                                      start);
120                 
121                 // store the selection
122                 if (realcut) {
123                         buf = startpar->next();
124                         buf->previous(0);
125                 } else {
126                         startpar->next()->previous(0);
127                 }
128                 (*endpar)->previous()->next(0);
129                 
130                 // cut the selection
131                 startpar->next(*endpar);
132                 
133                 (*endpar)->previous(startpar);
134                 
135                 // the cut selection should begin with standard layout
136                 if (realcut) {
137                         buf->params().clear();
138                         buf->bibkey = 0;
139                         buf->layout(textclasslist[current_view->buffer()->params.textclass].defaultLayoutName());
140                 }
141
142                 // paste the paragraphs again, if possible
143                 if (doclear)
144                         startpar->next()->stripLeadingSpaces(textclass);
145                 if (startpar->hasSameLayout(startpar->next()) ||
146                     !startpar->next()->size()) {
147                         startpar->pasteParagraph(current_view->buffer()->params);
148                         (*endpar) = startpar; // this because endpar gets deleted here!
149                 }
150                 // this paragraph's are of noone's owner!
151                 Paragraph * p = buf;
152                 while (p) {
153                         p->setInsetOwner(0);
154                         p = p->next();
155                 }
156         }
157         return true;
158 }
159
160
161 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
162                                 int start, int end, char tc)
163 {
164         if (!startpar || (start > startpar->size()))
165                 return false;
166         
167         DeleteBuffer();
168         
169         textclass = tc;
170         
171         if (!endpar || startpar == endpar) {
172                 // only within one paragraph
173                 buf = new Paragraph;
174                 buf->layout(startpar->layout());
175                 pos_type i = start;
176                 if (end > startpar->size())
177                         end = startpar->size();
178                 for (; i < end; ++i) {
179                         startpar->copyIntoMinibuffer(*current_view->buffer(), i);
180                         buf->insertFromMinibuffer(buf->size());
181                 }
182         } else {
183                 // copy more than one paragraph
184                 // clone the paragraphs within the selection
185                 Paragraph * tmppar = startpar;
186                 buf = new Paragraph(*tmppar, false);
187                 Paragraph * tmppar2 = buf;
188                 
189                 while (tmppar != endpar
190                        && tmppar->next()) {
191                         tmppar = tmppar->next();
192                         tmppar2->next(new Paragraph(*tmppar, false));
193                         tmppar2->next()->previous(tmppar2);
194                         tmppar2 = tmppar2->next();
195                 }
196                 tmppar2->next(0);
197                 
198                 // the buf paragraph is too big
199                 pos_type tmpi2 = start;
200                 for (; tmpi2; --tmpi2)
201                         buf->erase(0);
202                 
203                 // now tmppar 2 is too big, delete all after end
204                 tmpi2 = end;
205                 while (tmppar2->size() > tmpi2) {
206                         tmppar2->erase(tmppar2->size() - 1);
207                 }
208                 // this paragraph's are of noone's owner!
209                 tmppar = buf;
210                 while (tmppar) {
211                         tmppar->setInsetOwner(0);
212                         tmppar = tmppar->next();
213                 }
214         }
215         return true;
216 }
217
218
219 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
220                                  int & pos, char tc)
221 {
222         if (!checkPastePossible(*par))
223                 return false;
224         
225         if (pos > (*par)->size())
226                 pos = (*par)->size();
227         
228 #if 0
229         // Paragraph * tmpbuf;
230         Paragraph * tmppar = *par;
231         int tmppos = pos;
232
233         // There are two cases: cutbuffer only one paragraph or many
234         if (!buf->next()) {
235                 // only within a paragraph
236                 Paragraph * tmpbuf = new Paragraph(*buf, false);
237                 
238                 // Some provisions should be done here for checking
239                 // if we are inserting at the beginning of a
240                 // paragraph. If there are a space at the beginning
241                 // of the text to insert and we are inserting at
242                 // the beginning of the paragraph the space should
243                 // be removed.
244                 while (buf->size()) {
245                         // This is an attempt to fix the
246                         // "never insert a space at the
247                         // beginning of a paragraph" problem.
248                         if (!tmppos && buf->isLineSeparator(0)) {
249                                 buf->erase(0);
250                         } else {
251                                 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
252                                 buf->erase(0);
253                                 if (tmppar->insertFromMinibuffer(tmppos))
254                                         ++tmppos;
255                         }
256                 }
257                 delete buf;
258                 buf = tmpbuf;
259                 *endpar = tmppar->next();
260                 pos = tmppos;
261         } else
262 #endif
263         {
264                 // many paragraphs
265                 
266                 // make a copy of the simple cut_buffer
267                 Paragraph * tmpbuf = buf;
268                 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
269                 Paragraph * tmpbuf2 = simple_cut_clone;
270                 
271                 while (tmpbuf->next()) {
272                         tmpbuf = tmpbuf->next();
273                         tmpbuf2->next(new Paragraph(*tmpbuf, false));
274                         tmpbuf2->next()->previous(tmpbuf2);
275                         tmpbuf2 = tmpbuf2->next();
276                 }
277
278                 // now remove all out of the buffer which is NOT allowed in the
279                 // new environment and set also another font if that is required
280                 tmpbuf = buf;
281                 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
282                 // temporary set *par as previous of tmpbuf as we might have to realize
283                 // the font.
284                 tmpbuf->previous(*par);
285                 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter(current_view->buffer());
286                 while(tmpbuf) {
287                         // if we have a negative jump so that the depth would go below
288                         // 0 depth then we have to redo the delta to this new max depth
289                         // level so that subsequent paragraphs are aligned correctly to
290                         // this paragraph at level 0.
291                         if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
292                                 depth_delta = 0;
293                         // set the right depth so that we are not too deep or shallow.
294                         tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
295                         if (tmpbuf->params().depth() > max_depth)
296                                 tmpbuf->params().depth(max_depth);
297                         // only set this from the 2nd on as the 2nd depends for maxDepth
298                         // still on *par
299                         if (tmpbuf->previous() != (*par))
300                                 max_depth = tmpbuf->getMaxDepthAfter(current_view->buffer());
301                         // set the inset owner of this paragraph
302                         tmpbuf->setInsetOwner((*par)->inInset());
303                         for(pos_type i = 0; i < tmpbuf->size(); ++i) {
304                                 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
305                                         if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
306                                         {
307                                                 tmpbuf->erase(i--);
308                                         }
309                                 } else {
310                                         LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
311                                         LyXFont f2 = f1;
312                                         if (!(*par)->checkInsertChar(f1)) {
313                                                 tmpbuf->erase(i--);
314                                         } else if (f1 != f2) {
315                                                 tmpbuf->setFont(i, f1);
316                                         }
317                                 }
318                         }
319                         tmpbuf = tmpbuf->next();
320                 }
321                 // now reset it to 0
322                 buf->previous(0);
323                 
324                 // make sure there is no class difference
325                 SwitchLayoutsBetweenClasses(textclass, tc, buf,
326                                             current_view->buffer()->params);
327                 
328                 // make the buf exactly the same layout than
329                 // the cursor paragraph
330                 buf->makeSameLayout(*par);
331                 
332                 // find the end of the buffer
333                 Paragraph * lastbuffer = buf;
334                 while (lastbuffer->next())
335                         lastbuffer = lastbuffer->next();
336                 
337                 bool paste_the_end = false;
338                 
339                 // open the paragraph for inserting the buf
340                 // if necessary
341                 if (((*par)->size() > pos) || !(*par)->next()) {
342                         (*par)->breakParagraphConservative(current_view->buffer()->params,
343                                                            pos);
344                         paste_the_end = true;
345                 }
346                 // set the end for redoing later
347                 *endpar = (*par)->next()->next();
348                 
349                 // paste it!
350                 lastbuffer->next((*par)->next());
351                 (*par)->next()->previous(lastbuffer);
352                 
353                 (*par)->next(buf);
354                 buf->previous(*par);
355                 
356                 if ((*par)->next() == lastbuffer)
357                         lastbuffer = *par;
358                 
359                 (*par)->pasteParagraph(current_view->buffer()->params);
360                 // store the new cursor position
361                 *par = lastbuffer;
362                 pos = lastbuffer->size();
363                 // maybe some pasting
364                 if (lastbuffer->next() && paste_the_end) {
365                         if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
366                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
367                         } else if (!lastbuffer->next()->size()) {
368                                 lastbuffer->next()->makeSameLayout(lastbuffer);
369                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
370                         } else if (!lastbuffer->size()) {
371                                 lastbuffer->makeSameLayout(lastbuffer->next());
372                                 lastbuffer->pasteParagraph(current_view->buffer()->params);
373                         } else
374                                 lastbuffer->next()->stripLeadingSpaces(tc);
375                 }
376                 // restore the simple cut buffer
377                 buf = simple_cut_clone;
378         }
379         
380         return true;
381 }
382
383
384 int CutAndPaste::nrOfParagraphs()
385 {
386         if (!buf)
387                 return 0;
388         
389         int n = 1;
390         Paragraph * tmppar = buf;
391         while (tmppar->next()) {
392                 ++n;
393                 tmppar = tmppar->next();
394         }
395         return n;
396 }
397
398
399 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
400                                              textclass_type c2,
401                                              Paragraph * par,
402                                              BufferParams const & bparams)
403 {
404         int ret = 0;
405         if (!par || c1 == c2)
406                 return ret;
407
408         ParIterator end = ParIterator();
409         for (ParIterator it = ParIterator(par); it != end; ++it) {
410                 par = *it;
411                 string const name = par->layout();
412                 LyXTextClass const & tclass = textclasslist[c2];
413                 
414                 bool hasLayout = tclass.hasLayout(name);
415                 
416                 string lay = tclass.defaultLayoutName();
417                 if (hasLayout) {
418                         lay = name;
419                 } else {
420                         // not found: use default layout
421                         lay = tclass.defaultLayoutName();
422                 }
423                 par->layout(lay);
424                 
425                 if (name != par->layout()) {
426                         ++ret;
427                         string const s = _("Layout had to be changed from\n")
428                                 + name + _(" to ")
429                                 + par->layout()
430                                 + _("\nbecause of class conversion from\n")
431                                 + textclasslist[c1].name() + _(" to ")
432                                 + textclasslist[c2].name();
433                         InsetError * new_inset = new InsetError(s);
434                         par->insertInset(0, new_inset,
435                                          LyXFont(LyXFont::ALL_INHERIT,
436                                                  bparams.language));
437                 }
438         }
439         return ret;
440 }
441
442
443 bool CutAndPaste::checkPastePossible(Paragraph *)
444 {
445         if (!buf) return false;
446         
447         return true;
448 }