]> git.lyx.org Git - lyx.git/blob - src/CutAndPaste.C
remove commented code and reindent
[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 "ParagraphParameters.h"
17 #include "lyxtext.h"
18 #include "lyxcursor.h"
19 #include "gettext.h"
20 #include "iterators.h"
21 #include "lyxtextclasslist.h"
22 #include "undo_funcs.h"
23 #include "paragraph_funcs.h"
24 #include "debug.h"
25
26 #include "insets/inseterror.h"
27
28 #include "BoostFormat.h"
29
30 using std::endl;
31 using std::pair;
32 using lyx::pos_type;
33 using lyx::textclass_type;
34
35 extern BufferView * current_view;
36
37 // Jürgen, note that this means that you cannot currently have a list
38 // of selections cut/copied. So IMHO later we should have a
39 // list/vector/deque that we could store
40 // struct selection_item {
41 //       Paragraph * buf;
42 //       LyXTextClassList::size_type textclass;
43 // };
44 // in and some method of choosing beween them (based on the first few chars
45 // in the selection probably.) This would be a nice feature and quite
46 // easy to implement. (Lgb)
47 //
48 // Sure but I just cleaned up this code for now with the same functionality
49 // as before. I also want to add a XClipboard function so that we can copy
50 // text from LyX to some other X-application in the form of ASCII or in the
51 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
52 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
53 // then do a middle mouse button click in the application you want and have
54 // the whole formula there in LaTeX-Code. (Jug)
55
56 namespace {
57
58 // FIXME: stupid name
59 ParagraphList paragraphs;
60 textclass_type textclass = 0;
61
62 } // namespace anon
63
64
65 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
66                                int start, int & end, textclass_type tc,
67                                bool doclear, bool realcut)
68 {
69         if (!startpar || (start > startpar->size()))
70                 return false;
71
72         if (realcut) {
73                 copySelection(startpar, *endpar, start, end, tc);
74         }
75
76         if (!endpar || startpar == *endpar) {
77                 if (startpar->erase(start, end)) {
78                         // Some chars were erased, go to start to be safe
79                         end = start;
80                 }
81                 return true;
82         }
83
84         bool actually_erased = false;
85
86         // clear end/begin fragments of the first/last par in selection
87         actually_erased |= (startpar)->erase(start, startpar->size());
88         if ((*endpar)->erase(0, end)) {
89                 actually_erased = true;
90                 end = 0;
91         }
92
93         // Loop through the deleted pars if any, erasing as needed
94
95         Paragraph * pit = startpar->next();
96
97         while (true) {
98                 // *endpar can be 0
99                 if (!pit)
100                         break;
101
102                 Paragraph * next = pit->next();
103
104                 // "erase" the contents of the par
105                 if (pit != *endpar) {
106                         actually_erased |= pit->erase(0, pit->size());
107
108                         // remove the par if it's now empty
109                         if (actually_erased) {
110                                 pit->previous()->next(pit->next());
111                                 if (next) {
112                                         next->previous(pit->previous());
113                                 }
114
115                                 delete pit;
116                         }
117                 }
118
119                 if (pit == *endpar)
120                         break;
121
122                 pit = next;
123         }
124
125 #if 0 // FIXME: why for cut but not copy ?
126         // the cut selection should begin with standard layout
127         if (realcut) {
128                 buf->params().clear();
129                 buf->bibkey = 0;
130                 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
131         }
132 #endif
133
134         if (!startpar->next())
135                 return true;
136
137         Buffer * buffer = current_view->buffer();
138
139         if (doclear) {
140                 startpar->next()->stripLeadingSpaces();
141         }
142
143         if (!actually_erased)
144                 return true;
145
146         // paste the paragraphs again, if possible
147         if (startpar->hasSameLayout(startpar->next()) ||
148             startpar->next()->empty()) {
149 #warning This is suspect. (Lgb)
150                 // When doing this merge we must know if the par really
151                 // belongs to an inset, and if it does then we have to use
152                 // the insets paragraphs, and not the buffers. (Lgb)
153                 mergeParagraph(buffer->params, buffer->paragraphs, startpar);
154                 // this because endpar gets deleted here!
155                 (*endpar) = startpar;
156         }
157
158         return true;
159 }
160
161
162 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
163                                 int start, int end, textclass_type tc)
164 {
165         if (!startpar || (start > startpar->size()))
166                 return false;
167
168         paragraphs.clear();
169
170         textclass = tc;
171
172         if (!endpar || startpar == endpar) {
173                 // only within one paragraph
174                 ParagraphList::iterator buf =
175                         paragraphs.insert(paragraphs.begin(), new Paragraph);
176
177                 buf->layout(startpar->layout());
178                 pos_type i = start;
179                 if (end > startpar->size())
180                         end = startpar->size();
181                 for (; i < end; ++i) {
182                         startpar->copyIntoMinibuffer(*current_view->buffer(), i);
183                         buf->insertFromMinibuffer(buf->size());
184                 }
185         } else {
186                 // copy more than one paragraph
187                 // clone the paragraphs within the selection
188                 Paragraph * tmppar = startpar;
189
190                 while (tmppar != endpar) {
191                         Paragraph * newpar = new Paragraph(*tmppar, false);
192                         // reset change info
193                         newpar->cleanChanges();
194                         newpar->setInsetOwner(0);
195
196                         paragraphs.push_back(newpar);
197                         tmppar = tmppar->next();
198                 }
199
200                 // The first paragraph is too big.
201                 Paragraph & front = paragraphs.front();
202                 pos_type tmpi2 = start;
203                 for (; tmpi2; --tmpi2)
204                         front.erase(0);
205
206                 // Now last paragraph is too big, delete all after end.
207                 Paragraph & back = paragraphs.back();
208                 tmpi2 = end;
209                 while (back.size() > tmpi2) {
210                         back.erase(back.size() - 1);
211                 }
212         }
213         return true;
214 }
215
216
217 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
218                                  int & pos, textclass_type tc)
219 {
220         if (!checkPastePossible())
221                 return false;
222
223         if (pos > (*par)->size())
224                 pos = (*par)->size();
225
226         // many paragraphs
227         
228         // make a copy of the simple cut_buffer
229 #if 1
230         ParagraphList::iterator it = paragraphs.begin();
231         
232         ParagraphList simple_cut_clone;
233         simple_cut_clone.insert(simple_cut_clone.begin(),
234                                 new Paragraph(*it, false));
235
236         ParagraphList::iterator end = paragraphs.end();
237         while (boost::next(it) != end) {
238                 ++it;
239                 simple_cut_clone.insert(simple_cut_clone.end(),
240                                         new Paragraph(*it, false));
241         }
242 #else
243         // Later we want it done like this:
244         ParagraphList simple_cut_clone(paragraphs.begin(),
245                                        paragraphs.end());
246 #endif
247         // now remove all out of the buffer which is NOT allowed in the
248         // new environment and set also another font if that is required
249         ParagraphList::iterator tmpbuf = paragraphs.begin();
250         int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
251         // Temporary set *par as previous of tmpbuf as we might have
252         // to realize the font.
253         tmpbuf->previous(*par);
254         
255         // make sure there is no class difference
256         SwitchLayoutsBetweenClasses(textclass, tc, &*tmpbuf,
257                                     current_view->buffer()->params);
258         
259         Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
260         
261         while (tmpbuf != paragraphs.end()) {
262                 // If we have a negative jump so that the depth would
263                 // go below 0 depth then we have to redo the delta to
264                 // this new max depth level so that subsequent
265                 // paragraphs are aligned correctly to this paragraph
266                 // at level 0.
267                 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
268                         depth_delta = 0;
269                 // set the right depth so that we are not too deep or shallow.
270                 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
271                 if (tmpbuf->params().depth() > max_depth)
272                         tmpbuf->params().depth(max_depth);
273                 // only set this from the 2nd on as the 2nd depends for maxDepth
274                 // still on *par
275                 if (tmpbuf->previous() != (*par))
276                         max_depth = tmpbuf->getMaxDepthAfter();
277                 // set the inset owner of this paragraph
278                 tmpbuf->setInsetOwner((*par)->inInset());
279                 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
280                         if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
281                                 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
282                                         tmpbuf->erase(i--);
283                                 }
284                         } else {
285                                 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
286                                 LyXFont f2 = f1;
287                                 if (!(*par)->checkInsertChar(f1)) {
288                                         tmpbuf->erase(i--);
289                                 } else if (f1 != f2) {
290                                         tmpbuf->setFont(i, f1);
291                                 }
292                         }
293                 }
294                 tmpbuf = tmpbuf->next();
295         }
296
297         // now reset it to 0
298         paragraphs.begin()->previous(0);
299
300         // make the buf exactly the same layout than
301         // the cursor paragraph
302         paragraphs.begin()->makeSameLayout(*par);
303         
304         // find the end of the buffer
305         ParagraphList::iterator lastbuffer = paragraphs.begin();
306         while (boost::next(lastbuffer) != paragraphs.end())
307                 ++lastbuffer;
308         
309         bool paste_the_end = false;
310         
311         // open the paragraph for inserting the buf
312         // if necessary
313         if (((*par)->size() > pos) || !(*par)->next()) {
314                 breakParagraphConservative(
315                         current_view->buffer()->params, current_view->buffer()->paragraphs, *par, pos);
316                 paste_the_end = true;
317         }
318         // set the end for redoing later
319         *endpar = (*par)->next()->next();
320         
321         // paste it!
322         lastbuffer->next((*par)->next());
323         (*par)->next()->previous(&*lastbuffer);
324         
325         (*par)->next(&*paragraphs.begin());
326         paragraphs.begin()->previous(*par);
327         
328         if ((*par)->next() == lastbuffer)
329                 lastbuffer = *par;
330         
331         mergeParagraph(current_view->buffer()->params,
332                        current_view->buffer()->paragraphs, *par);
333         // store the new cursor position
334         *par = &*lastbuffer;
335         pos = lastbuffer->size();
336         // maybe some pasting
337         if (lastbuffer->next() && paste_the_end) {
338                 if (lastbuffer->next()->hasSameLayout(&*lastbuffer)) {
339                         mergeParagraph(current_view->buffer()->params,
340                                        current_view->buffer()->paragraphs, lastbuffer);
341                 } else if (!lastbuffer->next()->size()) {
342                         lastbuffer->next()->makeSameLayout(&*lastbuffer);
343                         mergeParagraph(current_view->buffer()->params, current_view->buffer()->paragraphs, lastbuffer);
344                 } else if (!lastbuffer->size()) {
345                         lastbuffer->makeSameLayout(lastbuffer->next());
346                         mergeParagraph(current_view->buffer()->params,
347                                        current_view->buffer()->paragraphs, lastbuffer);
348                 } else
349                         lastbuffer->next()->stripLeadingSpaces();
350         }
351         // restore the simple cut buffer
352         paragraphs = simple_cut_clone;
353         
354         return true;
355 }
356
357
358 int CutAndPaste::nrOfParagraphs()
359 {
360         return paragraphs.size();
361 }
362
363
364 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
365                                              textclass_type c2,
366                                              Paragraph * par,
367                                              BufferParams const & /*bparams*/)
368 {
369         int ret = 0;
370         if (!par || c1 == c2)
371                 return ret;
372
373         LyXTextClass const & tclass1 = textclasslist[c1];
374         LyXTextClass const & tclass2 = textclasslist[c2];
375         ParIterator end = ParIterator();
376         for (ParIterator it = ParIterator(par); it != end; ++it) {
377                 par = *it;
378                 string const name = par->layout()->name();
379                 bool hasLayout = tclass2.hasLayout(name);
380
381                 if (hasLayout)
382                         par->layout(tclass2[name]);
383                 else
384                         par->layout(tclass2.defaultLayout());
385
386                 if (!hasLayout && name != tclass1.defaultLayoutName()) {
387                         ++ret;
388 #if USE_BOOST_FORMAT
389                         boost::format fmt(_("Layout had to be changed from\n"
390                                             "%1$s to %2$s\n"
391                                             "because of class conversion from\n"
392                                             "%3$s to %4$s"));
393                         fmt     % name
394                                 % par->layout()->name()
395                                 % tclass1.name()
396                                 % tclass2.name();
397
398                         string const s = fmt.str();
399 #else
400                         string const s = _("Layout had to be changed from\n")
401                                 + name + _(" to ")
402                                 + par->layout()->name()
403                                 + _("\nbecause of class conversion from\n")
404                                 + tclass1.name() + _(" to ")
405                                 + tclass2.name();
406 #endif
407                         freezeUndo();
408                         InsetError * new_inset = new InsetError(s);
409                         LyXText * txt = current_view->getLyXText();
410                         LyXCursor cur = txt->cursor;
411                         txt->setCursorIntern(current_view, par, 0);
412                         txt->insertInset(current_view, new_inset);
413                         txt->fullRebreak(current_view);
414                         txt->setCursorIntern(current_view, cur.par(), cur.pos());
415                         unFreezeUndo();
416                 }
417         }
418         return ret;
419 }
420
421
422 bool CutAndPaste::checkPastePossible()
423 {
424         return !paragraphs.empty();
425 }