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