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