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,
97 startpar->Erase(start);
99 buf->InsertFromMinibuffer(buf->Last());
103 // more than one paragraph
104 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
106 *endpar = (*endpar)->Next();
109 startpar->BreakParagraphConservative(current_view->buffer()->params,
112 // store the selection
114 buf = startpar->ParFromPos(start)->next;
116 buf = startpar->next;
119 (*endpar)->previous->next = 0;
123 startpar->ParFromPos(start)->next = (*endpar);
125 (*endpar)->previous = startpar->ParFromPos(start);
127 startpar->next = (*endpar);
129 (*endpar)->previous = startpar;
133 // care about footnotes
134 if (buf->footnoteflag) {
135 LyXParagraph * tmppar = buf;
137 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
138 tmppar = tmppar->next;
143 // the cut selection should begin with standard layout
146 // paste the paragraphs again, if possible
148 startpar->Next()->StripLeadingSpaces(textclass);
150 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
152 if (startpar->HasSameLayout(startpar->Next()) ||
154 !startpar->Next()->Last()) {
156 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
158 startpar->PasteParagraph(current_view->buffer()->params);
160 (*endpar) = startpar; // this because endpar gets deleted here!
167 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
168 int start, int end, char tc)
170 if (!startpar || (start > startpar->Last()))
179 (startpar->ParFromPos(start) ==
180 (endpar)->ParFromPos(end))
185 // only within one paragraph
186 buf = new LyXParagraph;
187 LyXParagraph::size_type i = start;
188 if (end > startpar->Last())
189 end = startpar->Last();
190 for (; i < end; ++i) {
191 startpar->CopyIntoMinibuffer(current_view->buffer()->params, i);
192 buf->InsertFromMinibuffer(buf->Last());
195 // copy more than one paragraph
196 // clone the paragraphs within the selection
198 LyXParagraph * tmppar = startpar->ParFromPos(start);
200 LyXParagraph * tmppar = startpar;
202 buf = tmppar->Clone();
203 LyXParagraph * tmppar2 = buf;
207 tmppar != endpar->ParFromPos(end)
212 tmppar = tmppar->next;
213 tmppar2->next = tmppar->Clone();
214 tmppar2->next->previous = tmppar2;
215 tmppar2 = tmppar2->next;
220 // care about footnotes
221 if (buf->footnoteflag) {
224 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
225 tmppar = tmppar->next;
230 // the buf paragraph is too big
232 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
234 LyXParagraph::size_type tmpi2 = start;
236 for (; tmpi2; --tmpi2)
239 // now tmppar 2 is too big, delete all after end
241 tmpi2 = endpar->PositionInParFromPos(end);
245 while (tmppar2->size() > tmpi2) {
246 tmppar2->Erase(tmppar2->size() - 1);
253 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
256 if (!checkPastePossible(*par, pos))
259 if (pos > (*par)->Last())
260 pos = (*par)->Last();
262 LyXParagraph * tmpbuf;
263 LyXParagraph * tmppar = *par;
266 // There are two cases: cutbuffer only one paragraph or many
268 // only within a paragraph
269 tmpbuf = buf->Clone();
270 // Some provisions should be done here for checking
271 // if we are inserting at the beginning of a
272 // paragraph. If there are a space at the beginning
273 // of the text to insert and we are inserting at
274 // the beginning of the paragraph the space should
276 while (buf->size()) {
277 // This is an attempt to fix the
278 // "never insert a space at the
279 // beginning of a paragraph" problem.
280 if (!tmppos && buf->IsLineSeparator(0)) {
283 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
285 if (tmppar->InsertFromMinibuffer(tmppos))
291 *endpar = tmppar->Next();
296 // make a copy of the simple cut_buffer
298 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
299 LyXParagraph * tmpbuf2 = simple_cut_clone;
301 if ((*par)->footnoteflag){
302 tmpbuf->footnoteflag = (*par)->footnoteflag;
303 tmpbuf->footnotekind = (*par)->footnotekind;
306 while (tmpbuf->next) {
307 tmpbuf = tmpbuf->next;
308 tmpbuf2->next = tmpbuf->Clone();
309 tmpbuf2->next->previous = tmpbuf2;
310 tmpbuf2 = tmpbuf2->next;
312 if ((*par)->footnoteflag){
313 tmpbuf->footnoteflag = (*par)->footnoteflag;
314 tmpbuf->footnotekind = (*par)->footnotekind;
319 // make sure there is no class difference
320 SwitchLayoutsBetweenClasses(textclass, tc, buf);
322 // make the buf exactly the same layout than
323 // the cursor paragraph
324 buf->MakeSameLayout(*par);
326 // find the end of the buffer
327 LyXParagraph * lastbuffer = buf;
328 while (lastbuffer->Next())
329 lastbuffer = lastbuffer->Next();
331 bool paste_the_end = false;
333 // open the paragraph for inserting the buf
335 if (((*par)->Last() > pos) || !(*par)->Next()) {
336 (*par)->BreakParagraphConservative(current_view->buffer()->params,
338 paste_the_end = true;
341 // set the end for redoing later
343 *endpar = (*par)->ParFromPos(pos)->next->Next();
346 lastbuffer->ParFromPos(lastbuffer->Last())->next =
347 (*par)->ParFromPos(pos)->next;
348 (*par)->ParFromPos(pos)->next->previous =
349 lastbuffer->ParFromPos(lastbuffer->Last());
351 (*par)->ParFromPos(pos)->next = buf;
352 buf->previous = (*par)->ParFromPos(pos);
354 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
357 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
359 *endpar = (*par)->next->Next();
362 lastbuffer->next = (*par)->next;
363 (*par)->next->previous = lastbuffer;
366 buf->previous = (*par);
368 if ((*par)->Next() == lastbuffer)
371 (*par)->PasteParagraph(current_view->buffer()->params);
373 // store the new cursor position
375 pos = lastbuffer->Last();
377 // maybe some pasting
378 if (lastbuffer->Next() && paste_the_end) {
380 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
381 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
382 } else if (!lastbuffer->Next()->Last()) {
383 lastbuffer->Next()->MakeSameLayout(lastbuffer);
384 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
385 } else if (!lastbuffer->Last()) {
386 lastbuffer->MakeSameLayout(lastbuffer->next);
387 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
389 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
390 lastbuffer->PasteParagraph(current_view->buffer()->params);
391 } else if (!lastbuffer->Next()->Last()) {
392 lastbuffer->Next()->MakeSameLayout(lastbuffer);
393 lastbuffer->PasteParagraph(current_view->buffer()->params);
394 } else if (!lastbuffer->Last()) {
395 lastbuffer->MakeSameLayout(lastbuffer->next);
396 lastbuffer->PasteParagraph(current_view->buffer()->params);
399 lastbuffer->Next()->StripLeadingSpaces(tc);
401 // restore the simple cut buffer
402 buf = simple_cut_clone;
409 int CutAndPaste::nrOfParagraphs()
414 LyXParagraph * tmppar = buf;
415 while(tmppar->next) {
417 tmppar = tmppar->next;
423 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
424 LyXTextClassList::size_type c2,
428 if (!par || c1 == c2)
431 par = par->FirstPhysicalPar();
434 string name = textclasslist.NameOfLayout(c1, par->layout);
436 pair<bool, LyXTextClass::LayoutList::size_type> pp =
437 textclasslist.NumberOfLayout(c2, name);
440 } else { // layout not found
441 // use default layout "Standard" (0)
446 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
448 string s = _("Layout had to be changed from\n")
450 + textclasslist.NameOfLayout(c2, par->layout)
451 + _("\nbecause of class conversion from\n")
452 + textclasslist.NameOfClass(c1) + _(" to ")
453 + textclasslist.NameOfClass(c2);
454 InsetError * new_inset = new InsetError(s);
455 par->InsertInset(0, new_inset);
464 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int)
466 if (!buf) return false;
469 // be carefull with footnotes in footnotes
470 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
471 // check whether the cut_buffer includes a footnote
472 LyXParagraph * tmppar = buf;
473 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
474 tmppar = tmppar->next;
477 WriteAlert(_("Impossible operation"),
478 _("Can't paste float into float!"),