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()) {
176 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
178 startpar->PasteParagraph(current_view->buffer()->params);
180 (*endpar) = startpar; // this because endpar gets deleted here!
187 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
188 int start, int end, char tc)
191 if (!startpar || (start > startpar->Last()))
194 if (!startpar || (start > startpar->size()))
204 (startpar->ParFromPos(start) ==
205 (endpar)->ParFromPos(end))
210 // only within one paragraph
211 buf = new LyXParagraph;
212 LyXParagraph::size_type i = start;
214 if (end > startpar->Last())
215 end = startpar->Last();
217 if (end > startpar->size())
218 end = startpar->size();
220 for (; i < end; ++i) {
221 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
223 buf->InsertFromMinibuffer(buf->Last());
225 buf->InsertFromMinibuffer(buf->size());
229 // copy more than one paragraph
230 // clone the paragraphs within the selection
232 LyXParagraph * tmppar = startpar->ParFromPos(start);
234 LyXParagraph * tmppar = startpar;
236 buf = tmppar->Clone();
237 LyXParagraph * tmppar2 = buf;
240 while (tmppar != endpar->ParFromPos(end)
242 tmppar = tmppar->next_;
243 tmppar2->next(tmppar->Clone());
244 tmppar2->next_->previous(tmppar2);
245 tmppar2 = tmppar2->next_;
249 while (tmppar != endpar
251 tmppar = tmppar->next();
252 tmppar2->next(tmppar->Clone());
253 tmppar2->next()->previous(tmppar2);
254 tmppar2 = tmppar2->next();
260 // care about footnotes
261 if (buf->footnoteflag) {
264 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
265 tmppar = tmppar->next_;
270 // the buf paragraph is too big
272 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
274 LyXParagraph::size_type tmpi2 = start;
276 for (; tmpi2; --tmpi2)
279 // now tmppar 2 is too big, delete all after end
281 tmpi2 = endpar->PositionInParFromPos(end);
285 while (tmppar2->size() > tmpi2) {
286 tmppar2->Erase(tmppar2->size() - 1);
293 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
296 if (!checkPastePossible(*par))
300 if (pos > (*par)->Last())
301 pos = (*par)->Last();
303 if (pos > (*par)->size())
304 pos = (*par)->size();
307 LyXParagraph * tmpbuf;
308 LyXParagraph * tmppar = *par;
311 // There are two cases: cutbuffer only one paragraph or many
317 // only within a paragraph
318 tmpbuf = buf->Clone();
319 // Some provisions should be done here for checking
320 // if we are inserting at the beginning of a
321 // paragraph. If there are a space at the beginning
322 // of the text to insert and we are inserting at
323 // the beginning of the paragraph the space should
325 while (buf->size()) {
326 // This is an attempt to fix the
327 // "never insert a space at the
328 // beginning of a paragraph" problem.
329 if (!tmppos && buf->IsLineSeparator(0)) {
332 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
334 if (tmppar->InsertFromMinibuffer(tmppos))
340 *endpar = tmppar->next();
345 // make a copy of the simple cut_buffer
347 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
348 LyXParagraph * tmpbuf2 = simple_cut_clone;
350 if ((*par)->footnoteflag) {
351 tmpbuf->footnoteflag = (*par)->footnoteflag;
352 tmpbuf->footnotekind = (*par)->footnotekind;
354 while (tmpbuf->next_) {
355 tmpbuf = tmpbuf->next_;
356 tmpbuf2->next(tmpbuf->Clone());
357 tmpbuf2->next_->previous(tmpbuf2);
358 tmpbuf2 = tmpbuf2->next_;
359 if ((*par)->footnoteflag){
360 tmpbuf->footnoteflag = (*par)->footnoteflag;
361 tmpbuf->footnotekind = (*par)->footnotekind;
365 while (tmpbuf->next()) {
366 tmpbuf = tmpbuf->next();
367 tmpbuf2->next(tmpbuf->Clone());
368 tmpbuf2->next()->previous(tmpbuf2);
369 tmpbuf2 = tmpbuf2->next();
373 // make sure there is no class difference
374 SwitchLayoutsBetweenClasses(textclass, tc, buf);
376 // make the buf exactly the same layout than
377 // the cursor paragraph
378 buf->MakeSameLayout(*par);
380 // find the end of the buffer
381 LyXParagraph * lastbuffer = buf;
382 while (lastbuffer->next())
383 lastbuffer = lastbuffer->next();
385 bool paste_the_end = false;
388 // open the paragraph for inserting the buf
390 if (((*par)->Last() > pos) || !(*par)->next()) {
391 (*par)->BreakParagraphConservative(current_view->buffer()->params,
393 paste_the_end = true;
395 // set the end for redoing later
396 *endpar = (*par)->ParFromPos(pos)->next_->next();
399 lastbuffer->ParFromPos(lastbuffer->Last())->next(
400 (*par)->ParFromPos(pos)->next_);
401 (*par)->ParFromPos(pos)->next()->previous(
402 lastbuffer->ParFromPos(lastbuffer->Last()));
404 (*par)->ParFromPos(pos)->next(buf);
405 buf->previous((*par)->ParFromPos(pos));
407 if ((*par)->ParFromPos(pos)->next() == lastbuffer)
410 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
411 // store the new cursor position
413 pos = lastbuffer->Last();
414 // maybe some pasting
415 if (lastbuffer->next() && paste_the_end) {
416 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
417 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
418 } else if (!lastbuffer->next()->Last()) {
419 lastbuffer->next()->MakeSameLayout(lastbuffer);
420 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
421 } else if (!lastbuffer->Last()) {
422 lastbuffer->MakeSameLayout(lastbuffer->next_);
423 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
425 lastbuffer->next()->StripLeadingSpaces(tc);
427 // restore the simple cut buffer
428 buf = simple_cut_clone;
431 // open the paragraph for inserting the buf
433 if (((*par)->size() > pos) || !(*par)->next()) {
434 (*par)->BreakParagraphConservative(current_view->buffer()->params,
436 paste_the_end = true;
438 // set the end for redoing later
439 *endpar = (*par)->next()->next();
442 lastbuffer->next((*par)->next());
443 (*par)->next()->previous(lastbuffer);
448 if ((*par)->next() == lastbuffer)
451 (*par)->PasteParagraph(current_view->buffer()->params);
452 // store the new cursor position
454 pos = lastbuffer->size();
455 // maybe some pasting
456 if (lastbuffer->next() && paste_the_end) {
457 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
458 lastbuffer->PasteParagraph(current_view->buffer()->params);
459 } else if (!lastbuffer->next()->size()) {
460 lastbuffer->next()->MakeSameLayout(lastbuffer);
461 lastbuffer->PasteParagraph(current_view->buffer()->params);
462 } else if (!lastbuffer->size()) {
463 lastbuffer->MakeSameLayout(lastbuffer->next());
464 lastbuffer->PasteParagraph(current_view->buffer()->params);
466 lastbuffer->next()->StripLeadingSpaces(tc);
468 // restore the simple cut buffer
469 buf = simple_cut_clone;
477 int CutAndPaste::nrOfParagraphs()
482 LyXParagraph * tmppar = buf;
484 while(tmppar->next_) {
486 tmppar = tmppar->next_;
489 while(tmppar->next()) {
491 tmppar = tmppar->next();
498 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
499 LyXTextClassList::size_type c2,
503 if (!par || c1 == c2)
506 par = par->FirstPhysicalPar();
509 string name = textclasslist.NameOfLayout(c1, par->layout);
511 pair<bool, LyXTextClass::LayoutList::size_type> pp =
512 textclasslist.NumberOfLayout(c2, name);
515 } else { // layout not found
516 // use default layout "Standard" (0)
521 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
523 string s = _("Layout had to be changed from\n")
525 + textclasslist.NameOfLayout(c2, par->layout)
526 + _("\nbecause of class conversion from\n")
527 + textclasslist.NameOfClass(c1) + _(" to ")
528 + textclasslist.NameOfClass(c2);
529 InsetError * new_inset = new InsetError(s);
530 par->InsertInset(0, new_inset);
542 bool CutAndPaste::checkPastePossible(LyXParagraph * par)
544 if (!buf) return false;
547 // be carefull with footnotes in footnotes
548 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
549 // check whether the cut_buffer includes a footnote
550 LyXParagraph * tmppar = buf;
551 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
552 tmppar = tmppar->next_;
555 WriteAlert(_("Impossible operation"),
556 _("Can't paste float into float!"),