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)
47 static LyXParagraph * buf = 0;
48 static LyXTextClassList::size_type textclass = 0;
50 // for now here this should be in another Cut&Paste Class!
51 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
52 // member of the CutAndPaste class and in addition it was private.
53 // Perhaps it even should take a parameter? (Lgb)
60 LyXParagraph * tmppar;
71 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
72 int start, int & end, char tc, bool doclear)
74 if (!startpar || (start > startpar->Last()))
83 (startpar->ParFromPos(start) ==
84 (*endpar)->ParFromPos(end))
86 (startpar == (*endpar))
89 // only within one paragraph
90 buf = new LyXParagraph;
91 LyXParagraph::size_type i = start;
92 if (end > startpar->Last())
93 end = startpar->Last();
94 for (; i < end; ++i) {
95 startpar->CopyIntoMinibuffer(current_view->buffer()->params,
98 /* table stuff -- begin */
99 if (startpar->table && startpar->IsNewline(start)) {
102 /* table stuff -- end */
104 startpar->Erase(start);
108 buf->InsertFromMinibuffer(buf->Last());
112 // more than one paragraph
113 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
115 *endpar = (*endpar)->Next();
118 startpar->BreakParagraphConservative(current_view->buffer()->params,
121 // store the selection
123 buf = startpar->ParFromPos(start)->next;
125 buf = startpar->next;
128 (*endpar)->previous->next = 0;
132 startpar->ParFromPos(start)->next = (*endpar);
134 (*endpar)->previous = startpar->ParFromPos(start);
136 startpar->next = (*endpar);
138 (*endpar)->previous = startpar;
142 // care about footnotes
143 if (buf->footnoteflag) {
144 LyXParagraph * tmppar = buf;
146 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
147 tmppar = tmppar->next;
152 // the cut selection should begin with standard layout
155 // paste the paragraphs again, if possible
157 startpar->Next()->StripLeadingSpaces(textclass);
159 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
161 if (startpar->HasSameLayout(startpar->Next()) ||
163 !startpar->Next()->Last()) {
165 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
167 startpar->PasteParagraph(current_view->buffer()->params);
169 (*endpar) = startpar; // this because endpar gets deleted here!
176 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
177 int start, int end, char tc)
179 if (!startpar || (start > startpar->Last()))
188 (startpar->ParFromPos(start) ==
189 (endpar)->ParFromPos(end))
194 // only within one paragraph
195 buf = new LyXParagraph;
196 LyXParagraph::size_type i = start;
197 if (end > startpar->Last())
198 end = startpar->Last();
199 for (; i < end; ++i) {
200 startpar->CopyIntoMinibuffer(current_view->buffer()->params, i);
201 buf->InsertFromMinibuffer(buf->Last());
204 // copy more than one paragraph
205 // clone the paragraphs within the selection
207 LyXParagraph * tmppar = startpar->ParFromPos(start);
209 LyXParagraph * tmppar = startpar;
211 buf = tmppar->Clone();
212 LyXParagraph * tmppar2 = buf;
216 tmppar != endpar->ParFromPos(end)
221 tmppar = tmppar->next;
222 tmppar2->next = tmppar->Clone();
223 tmppar2->next->previous = tmppar2;
224 tmppar2 = tmppar2->next;
229 // care about footnotes
230 if (buf->footnoteflag) {
233 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
234 tmppar = tmppar->next;
239 // the buf paragraph is too big
241 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
243 LyXParagraph::size_type tmpi2 = start;
245 for (; tmpi2; --tmpi2)
248 // now tmppar 2 is too big, delete all after end
250 tmpi2 = endpar->PositionInParFromPos(end);
254 while (tmppar2->size() > tmpi2) {
255 tmppar2->Erase(tmppar2->size() - 1);
262 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
265 if (!checkPastePossible(*par, pos))
268 if (pos > (*par)->Last())
269 pos = (*par)->Last();
271 LyXParagraph * tmpbuf;
272 LyXParagraph * tmppar = *par;
275 // There are two cases: cutbuffer only one paragraph or many
277 // only within a paragraph
278 tmpbuf = buf->Clone();
280 /* table stuff -- begin */
281 bool table_too_small = false;
283 while (buf->size() && !table_too_small) {
284 if (buf->IsNewline(0)){
285 while((tmppos < tmppar->Last()) &&
286 !tmppar->IsNewline(tmppos))
289 if (tmppos < tmppar->Last())
292 table_too_small = true;
294 // This is an attempt to fix the
295 // "never insert a space at the
296 // beginning of a paragraph" problem.
297 if (!tmppos && buf->IsLineSeparator(0)) {
300 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
302 if (tmppar->InsertFromMinibuffer(tmppos))
308 /* table stuff -- end */
310 // Some provisions should be done here for checking
311 // if we are inserting at the beginning of a
312 // paragraph. If there are a space at the beginning
313 // of the text to insert and we are inserting at
314 // the beginning of the paragraph the space should
316 while (buf->size()) {
317 // This is an attempt to fix the
318 // "never insert a space at the
319 // beginning of a paragraph" problem.
320 if (!tmppos && buf->IsLineSeparator(0)) {
323 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
325 if (tmppar->InsertFromMinibuffer(tmppos))
334 *endpar = tmppar->Next();
339 // make a copy of the simple cut_buffer
341 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
342 LyXParagraph * tmpbuf2 = simple_cut_clone;
344 if ((*par)->footnoteflag){
345 tmpbuf->footnoteflag = (*par)->footnoteflag;
346 tmpbuf->footnotekind = (*par)->footnotekind;
349 while (tmpbuf->next) {
350 tmpbuf = tmpbuf->next;
351 tmpbuf2->next = tmpbuf->Clone();
352 tmpbuf2->next->previous = tmpbuf2;
353 tmpbuf2 = tmpbuf2->next;
355 if ((*par)->footnoteflag){
356 tmpbuf->footnoteflag = (*par)->footnoteflag;
357 tmpbuf->footnotekind = (*par)->footnotekind;
362 // make sure there is no class difference
363 SwitchLayoutsBetweenClasses(textclass, tc, buf);
365 // make the buf exactly the same layout than
366 // the cursor paragraph
367 buf->MakeSameLayout(*par);
369 // find the end of the buffer
370 LyXParagraph * lastbuffer = buf;
371 while (lastbuffer->Next())
372 lastbuffer = lastbuffer->Next();
374 bool paste_the_end = false;
376 // open the paragraph for inserting the buf
378 if (((*par)->Last() > pos) || !(*par)->Next()) {
379 (*par)->BreakParagraphConservative(current_view->buffer()->params,
381 paste_the_end = true;
384 // set the end for redoing later
386 *endpar = (*par)->ParFromPos(pos)->next->Next();
389 lastbuffer->ParFromPos(lastbuffer->Last())->next =
390 (*par)->ParFromPos(pos)->next;
391 (*par)->ParFromPos(pos)->next->previous =
392 lastbuffer->ParFromPos(lastbuffer->Last());
394 (*par)->ParFromPos(pos)->next = buf;
395 buf->previous = (*par)->ParFromPos(pos);
397 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
400 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
402 *endpar = (*par)->next->Next();
405 lastbuffer->next = (*par)->next;
406 (*par)->next->previous = lastbuffer;
409 buf->previous = (*par);
411 if ((*par)->Next() == lastbuffer)
414 (*par)->PasteParagraph(current_view->buffer()->params);
416 // store the new cursor position
418 pos = lastbuffer->Last();
420 // maybe some pasting
421 if (lastbuffer->Next() && paste_the_end) {
423 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
424 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
425 } else if (!lastbuffer->Next()->Last()) {
426 lastbuffer->Next()->MakeSameLayout(lastbuffer);
427 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
428 } else if (!lastbuffer->Last()) {
429 lastbuffer->MakeSameLayout(lastbuffer->next);
430 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
432 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
433 lastbuffer->PasteParagraph(current_view->buffer()->params);
434 } else if (!lastbuffer->Next()->Last()) {
435 lastbuffer->Next()->MakeSameLayout(lastbuffer);
436 lastbuffer->PasteParagraph(current_view->buffer()->params);
437 } else if (!lastbuffer->Last()) {
438 lastbuffer->MakeSameLayout(lastbuffer->next);
439 lastbuffer->PasteParagraph(current_view->buffer()->params);
442 lastbuffer->Next()->StripLeadingSpaces(tc);
444 // restore the simple cut buffer
445 buf = simple_cut_clone;
452 int CutAndPaste::nrOfParagraphs()
457 LyXParagraph * tmppar = buf;
458 while(tmppar->next) {
460 tmppar = tmppar->next;
466 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
467 LyXTextClassList::size_type c2,
471 if (!par || c1 == c2)
474 par = par->FirstPhysicalPar();
477 string name = textclasslist.NameOfLayout(c1, par->layout);
479 pair<bool, LyXTextClass::LayoutList::size_type> pp =
480 textclasslist.NumberOfLayout(c2, name);
483 } else { // layout not found
484 // use default layout "Standard" (0)
489 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
491 string s = _("Layout had to be changed from\n")
493 + textclasslist.NameOfLayout(c2, par->layout)
494 + _("\nbecause of class conversion from\n")
495 + textclasslist.NameOfClass(c1) + _(" to ")
496 + textclasslist.NameOfClass(c2);
497 InsetError * new_inset = new InsetError(s);
498 par->InsertInset(0, new_inset);
507 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int)
509 if (!buf) return false;
512 // be carefull with footnotes in footnotes
513 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
514 // check whether the cut_buffer includes a footnote
515 LyXParagraph * tmppar = buf;
516 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
517 tmppar = tmppar->next;
520 WriteAlert(_("Impossible operation"),
521 _("Can't paste float into float!"),
528 /* table stuff -- begin */
531 WriteAlert(_("Impossible operation"),
532 _("Table cell cannot include more than one paragraph!"),
537 /* table stuff -- end */