1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2000 The LyX Team.
8 * ====================================================== */
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
15 #include "lyxparagraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
21 #pragma implementation
26 extern BufferView * current_view;
28 // Jürgen, note that this means that you cannot currently have a list
29 // of selections cut/copied. So IMHO later we should have a
30 // list/vector/deque that we could store
31 // struct selection_item {
32 // LyXParagraph * buf;
33 // LyXTextClassList::size_type textclass;
35 // in and some method of choosing beween them (based on the first few chars
36 // in the selection probably.) This would be a nice feature and quite
37 // easy to implement. (Lgb)
39 // Sure but I just cleaned up this code for now with the same functionality
40 // as before. I also want to add a XClipboard function so that we can copy
41 // text from LyX to some other X-application in the form of ASCII or in the
42 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
43 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
44 // then do a middle mouse button click in the application you want and have
45 // the whole formula there in LaTeX-Code. (Jug)
49 LyXParagraph * buf = 0;
50 LyXTextClassList::size_type textclass = 0;
52 // for now here this should be in another Cut&Paste Class!
53 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
54 // member of the CutAndPaste class and in addition it was private.
55 // Perhaps it even should take a parameter? (Lgb)
61 LyXParagraph * tmppar;
77 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
78 int start, int & end, char tc, bool doclear)
81 if (!startpar || (start > startpar->Last()))
84 if (!startpar || (start > startpar->size()))
94 (startpar->ParFromPos(start) ==
95 (*endpar)->ParFromPos(end))
97 (startpar == (*endpar))
100 // only within one paragraph
101 buf = new LyXParagraph;
102 LyXParagraph::size_type i = start;
104 if (end > startpar->Last())
105 end = startpar->Last();
107 if (end > startpar->size())
108 end = startpar->size();
110 for (; i < end; ++i) {
111 startpar->CopyIntoMinibuffer(*current_view->buffer(), start);
112 startpar->Erase(start);
115 buf->InsertFromMinibuffer(buf->Last());
117 buf->InsertFromMinibuffer(buf->size());
122 // more than one paragraph
123 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
125 *endpar = (*endpar)->next();
128 startpar->BreakParagraphConservative(current_view->buffer()->params,
131 // store the selection
133 buf = startpar->ParFromPos(start)->next_;
135 buf = startpar->next();
138 (*endpar)->previous()->next(0);
142 startpar->ParFromPos(start)->next(*endpar);
144 (*endpar)->previous(startpar->ParFromPos(start));
146 startpar->next(*endpar);
148 (*endpar)->previous(startpar);
152 // care about footnotes
153 if (buf->footnoteflag) {
154 LyXParagraph * tmppar = buf;
156 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
157 tmppar = tmppar->next_;
162 // the cut selection should begin with standard layout
165 // paste the paragraphs again, if possible
167 startpar->next()->StripLeadingSpaces(textclass);
169 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->next()) ||
170 !startpar->next()->Last())
172 if (startpar->HasSameLayout(startpar->next()) ||
173 !startpar->next()->size())
177 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
179 startpar->PasteParagraph(current_view->buffer()->params);
181 (*endpar) = startpar; // this because endpar gets deleted here!
188 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
189 int start, int end, char tc)
192 if (!startpar || (start > startpar->Last()))
195 if (!startpar || (start > startpar->size()))
205 (startpar->ParFromPos(start) ==
206 (endpar)->ParFromPos(end))
211 // only within one paragraph
212 buf = new LyXParagraph;
213 LyXParagraph::size_type i = start;
215 if (end > startpar->Last())
216 end = startpar->Last();
218 if (end > startpar->size())
219 end = startpar->size();
221 for (; i < end; ++i) {
222 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
224 buf->InsertFromMinibuffer(buf->Last());
226 buf->InsertFromMinibuffer(buf->size());
230 // copy more than one paragraph
231 // clone the paragraphs within the selection
233 LyXParagraph * tmppar = startpar->ParFromPos(start);
235 LyXParagraph * tmppar = startpar;
237 buf = tmppar->Clone();
238 LyXParagraph * tmppar2 = buf;
241 while (tmppar != endpar->ParFromPos(end)
243 tmppar = tmppar->next_;
244 tmppar2->next(tmppar->Clone());
245 tmppar2->next_->previous(tmppar2);
246 tmppar2 = tmppar2->next_;
250 while (tmppar != endpar
252 tmppar = tmppar->next();
253 tmppar2->next(tmppar->Clone());
254 tmppar2->next()->previous(tmppar2);
255 tmppar2 = tmppar2->next();
261 // care about footnotes
262 if (buf->footnoteflag) {
265 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
266 tmppar = tmppar->next_;
271 // the buf paragraph is too big
273 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
275 LyXParagraph::size_type tmpi2 = start;
277 for (; tmpi2; --tmpi2)
280 // now tmppar 2 is too big, delete all after end
282 tmpi2 = endpar->PositionInParFromPos(end);
286 while (tmppar2->size() > tmpi2) {
287 tmppar2->Erase(tmppar2->size() - 1);
294 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
297 if (!checkPastePossible(*par))
301 if (pos > (*par)->Last())
302 pos = (*par)->Last();
304 if (pos > (*par)->size())
305 pos = (*par)->size();
308 LyXParagraph * tmpbuf;
309 LyXParagraph * tmppar = *par;
312 // There are two cases: cutbuffer only one paragraph or many
318 // only within a paragraph
319 tmpbuf = buf->Clone();
320 // Some provisions should be done here for checking
321 // if we are inserting at the beginning of a
322 // paragraph. If there are a space at the beginning
323 // of the text to insert and we are inserting at
324 // the beginning of the paragraph the space should
326 while (buf->size()) {
327 // This is an attempt to fix the
328 // "never insert a space at the
329 // beginning of a paragraph" problem.
330 if (!tmppos && buf->IsLineSeparator(0)) {
333 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
335 if (tmppar->InsertFromMinibuffer(tmppos))
341 *endpar = tmppar->next();
346 // make a copy of the simple cut_buffer
348 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
349 LyXParagraph * tmpbuf2 = simple_cut_clone;
351 if ((*par)->footnoteflag) {
352 tmpbuf->footnoteflag = (*par)->footnoteflag;
353 tmpbuf->footnotekind = (*par)->footnotekind;
355 while (tmpbuf->next_) {
356 tmpbuf = tmpbuf->next_;
357 tmpbuf2->next(tmpbuf->Clone());
358 tmpbuf2->next_->previous(tmpbuf2);
359 tmpbuf2 = tmpbuf2->next_;
360 if ((*par)->footnoteflag){
361 tmpbuf->footnoteflag = (*par)->footnoteflag;
362 tmpbuf->footnotekind = (*par)->footnotekind;
366 while (tmpbuf->next()) {
367 tmpbuf = tmpbuf->next();
368 tmpbuf2->next(tmpbuf->Clone());
369 tmpbuf2->next()->previous(tmpbuf2);
370 tmpbuf2 = tmpbuf2->next();
374 // make sure there is no class difference
375 SwitchLayoutsBetweenClasses(textclass, tc, buf);
377 // make the buf exactly the same layout than
378 // the cursor paragraph
379 buf->MakeSameLayout(*par);
381 // find the end of the buffer
382 LyXParagraph * lastbuffer = buf;
383 while (lastbuffer->next())
384 lastbuffer = lastbuffer->next();
386 bool paste_the_end = false;
389 // open the paragraph for inserting the buf
391 if (((*par)->Last() > pos) || !(*par)->next()) {
392 (*par)->BreakParagraphConservative(current_view->buffer()->params,
394 paste_the_end = true;
396 // set the end for redoing later
397 *endpar = (*par)->ParFromPos(pos)->next_->next();
400 lastbuffer->ParFromPos(lastbuffer->Last())->next(
401 (*par)->ParFromPos(pos)->next_);
402 (*par)->ParFromPos(pos)->next()->previous(
403 lastbuffer->ParFromPos(lastbuffer->Last()));
405 (*par)->ParFromPos(pos)->next(buf);
406 buf->previous((*par)->ParFromPos(pos));
408 if ((*par)->ParFromPos(pos)->next() == lastbuffer)
411 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
412 // store the new cursor position
414 pos = lastbuffer->Last();
415 // maybe some pasting
416 if (lastbuffer->next() && paste_the_end) {
417 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
418 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
419 } else if (!lastbuffer->next()->Last()) {
420 lastbuffer->next()->MakeSameLayout(lastbuffer);
421 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
422 } else if (!lastbuffer->Last()) {
423 lastbuffer->MakeSameLayout(lastbuffer->next_);
424 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
426 lastbuffer->next()->StripLeadingSpaces(tc);
428 // restore the simple cut buffer
429 buf = simple_cut_clone;
432 // open the paragraph for inserting the buf
434 if (((*par)->size() > pos) || !(*par)->next()) {
435 (*par)->BreakParagraphConservative(current_view->buffer()->params,
437 paste_the_end = true;
439 // set the end for redoing later
440 *endpar = (*par)->next()->next();
443 lastbuffer->next((*par)->next());
444 (*par)->next()->previous(lastbuffer);
449 if ((*par)->next() == lastbuffer)
452 (*par)->PasteParagraph(current_view->buffer()->params);
453 // store the new cursor position
455 pos = lastbuffer->size();
456 // maybe some pasting
457 if (lastbuffer->next() && paste_the_end) {
458 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
459 lastbuffer->PasteParagraph(current_view->buffer()->params);
460 } else if (!lastbuffer->next()->size()) {
461 lastbuffer->next()->MakeSameLayout(lastbuffer);
462 lastbuffer->PasteParagraph(current_view->buffer()->params);
463 } else if (!lastbuffer->size()) {
464 lastbuffer->MakeSameLayout(lastbuffer->next());
465 lastbuffer->PasteParagraph(current_view->buffer()->params);
467 lastbuffer->next()->StripLeadingSpaces(tc);
469 // restore the simple cut buffer
470 buf = simple_cut_clone;
478 int CutAndPaste::nrOfParagraphs()
484 LyXParagraph * tmppar = buf;
486 while(tmppar->next_) {
488 tmppar = tmppar->next_;
491 while(tmppar->next()) {
493 tmppar = tmppar->next();
500 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
501 LyXTextClassList::size_type c2,
505 if (!par || c1 == c2)
508 par = par->FirstPhysicalPar();
511 string name = textclasslist.NameOfLayout(c1, par->layout);
513 pair<bool, LyXTextClass::LayoutList::size_type> pp =
514 textclasslist.NumberOfLayout(c2, name);
517 } else { // layout not found
518 // use default layout "Standard" (0)
523 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
525 string s = _("Layout had to be changed from\n")
527 + textclasslist.NameOfLayout(c2, par->layout)
528 + _("\nbecause of class conversion from\n")
529 + textclasslist.NameOfClass(c1) + _(" to ")
530 + textclasslist.NameOfClass(c2);
531 InsetError * new_inset = new InsetError(s);
532 par->InsertInset(0, new_inset);
545 bool CutAndPaste::checkPastePossible(LyXParagraph * par)
547 bool CutAndPaste::checkPastePossible(LyXParagraph *)
550 if (!buf) return false;
553 // be carefull with footnotes in footnotes
554 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
555 // check whether the cut_buffer includes a footnote
556 LyXParagraph * tmppar = buf;
557 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
558 tmppar = tmppar->next_;
561 WriteAlert(_("Impossible operation"),
562 _("Can't paste float into float!"),