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(), start);
96 startpar->Erase(start);
98 buf->InsertFromMinibuffer(buf->Last());
102 // more than one paragraph
103 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
105 *endpar = (*endpar)->Next();
108 startpar->BreakParagraphConservative(current_view->buffer()->params,
111 // store the selection
113 buf = startpar->ParFromPos(start)->next;
115 buf = startpar->next;
118 (*endpar)->previous->next = 0;
122 startpar->ParFromPos(start)->next = (*endpar);
124 (*endpar)->previous = startpar->ParFromPos(start);
126 startpar->next = (*endpar);
128 (*endpar)->previous = startpar;
132 // care about footnotes
133 if (buf->footnoteflag) {
134 LyXParagraph * tmppar = buf;
136 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
137 tmppar = tmppar->next;
142 // the cut selection should begin with standard layout
145 // paste the paragraphs again, if possible
147 startpar->Next()->StripLeadingSpaces(textclass);
149 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
151 if (startpar->HasSameLayout(startpar->Next()) ||
153 !startpar->Next()->Last()) {
155 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
157 startpar->PasteParagraph(current_view->buffer()->params);
159 (*endpar) = startpar; // this because endpar gets deleted here!
166 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
167 int start, int end, char tc)
169 if (!startpar || (start > startpar->Last()))
178 (startpar->ParFromPos(start) ==
179 (endpar)->ParFromPos(end))
184 // only within one paragraph
185 buf = new LyXParagraph;
186 LyXParagraph::size_type i = start;
187 if (end > startpar->Last())
188 end = startpar->Last();
189 for (; i < end; ++i) {
190 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
191 buf->InsertFromMinibuffer(buf->Last());
194 // copy more than one paragraph
195 // clone the paragraphs within the selection
197 LyXParagraph * tmppar = startpar->ParFromPos(start);
199 LyXParagraph * tmppar = startpar;
201 buf = tmppar->Clone();
202 LyXParagraph * tmppar2 = buf;
206 tmppar != endpar->ParFromPos(end)
211 tmppar = tmppar->next;
212 tmppar2->next = tmppar->Clone();
213 tmppar2->next->previous = tmppar2;
214 tmppar2 = tmppar2->next;
219 // care about footnotes
220 if (buf->footnoteflag) {
223 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
224 tmppar = tmppar->next;
229 // the buf paragraph is too big
231 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
233 LyXParagraph::size_type tmpi2 = start;
235 for (; tmpi2; --tmpi2)
238 // now tmppar 2 is too big, delete all after end
240 tmpi2 = endpar->PositionInParFromPos(end);
244 while (tmppar2->size() > tmpi2) {
245 tmppar2->Erase(tmppar2->size() - 1);
252 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
255 if (!checkPastePossible(*par, pos))
258 if (pos > (*par)->Last())
259 pos = (*par)->Last();
261 LyXParagraph * tmpbuf;
262 LyXParagraph * tmppar = *par;
265 // There are two cases: cutbuffer only one paragraph or many
267 // only within a paragraph
268 tmpbuf = buf->Clone();
269 // Some provisions should be done here for checking
270 // if we are inserting at the beginning of a
271 // paragraph. If there are a space at the beginning
272 // of the text to insert and we are inserting at
273 // the beginning of the paragraph the space should
275 while (buf->size()) {
276 // This is an attempt to fix the
277 // "never insert a space at the
278 // beginning of a paragraph" problem.
279 if (!tmppos && buf->IsLineSeparator(0)) {
282 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
284 if (tmppar->InsertFromMinibuffer(tmppos))
290 *endpar = tmppar->Next();
295 // make a copy of the simple cut_buffer
297 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
298 LyXParagraph * tmpbuf2 = simple_cut_clone;
300 if ((*par)->footnoteflag){
301 tmpbuf->footnoteflag = (*par)->footnoteflag;
302 tmpbuf->footnotekind = (*par)->footnotekind;
305 while (tmpbuf->next) {
306 tmpbuf = tmpbuf->next;
307 tmpbuf2->next = tmpbuf->Clone();
308 tmpbuf2->next->previous = tmpbuf2;
309 tmpbuf2 = tmpbuf2->next;
311 if ((*par)->footnoteflag){
312 tmpbuf->footnoteflag = (*par)->footnoteflag;
313 tmpbuf->footnotekind = (*par)->footnotekind;
318 // make sure there is no class difference
319 SwitchLayoutsBetweenClasses(textclass, tc, buf);
321 // make the buf exactly the same layout than
322 // the cursor paragraph
323 buf->MakeSameLayout(*par);
325 // find the end of the buffer
326 LyXParagraph * lastbuffer = buf;
327 while (lastbuffer->Next())
328 lastbuffer = lastbuffer->Next();
330 bool paste_the_end = false;
332 // open the paragraph for inserting the buf
334 if (((*par)->Last() > pos) || !(*par)->Next()) {
335 (*par)->BreakParagraphConservative(current_view->buffer()->params,
337 paste_the_end = true;
340 // set the end for redoing later
342 *endpar = (*par)->ParFromPos(pos)->next->Next();
345 lastbuffer->ParFromPos(lastbuffer->Last())->next =
346 (*par)->ParFromPos(pos)->next;
347 (*par)->ParFromPos(pos)->next->previous =
348 lastbuffer->ParFromPos(lastbuffer->Last());
350 (*par)->ParFromPos(pos)->next = buf;
351 buf->previous = (*par)->ParFromPos(pos);
353 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
356 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
358 *endpar = (*par)->next->Next();
361 lastbuffer->next = (*par)->next;
362 (*par)->next->previous = lastbuffer;
365 buf->previous = (*par);
367 if ((*par)->Next() == lastbuffer)
370 (*par)->PasteParagraph(current_view->buffer()->params);
372 // store the new cursor position
374 pos = lastbuffer->Last();
376 // maybe some pasting
377 if (lastbuffer->Next() && paste_the_end) {
379 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
380 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
381 } else if (!lastbuffer->Next()->Last()) {
382 lastbuffer->Next()->MakeSameLayout(lastbuffer);
383 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
384 } else if (!lastbuffer->Last()) {
385 lastbuffer->MakeSameLayout(lastbuffer->next);
386 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
388 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
389 lastbuffer->PasteParagraph(current_view->buffer()->params);
390 } else if (!lastbuffer->Next()->Last()) {
391 lastbuffer->Next()->MakeSameLayout(lastbuffer);
392 lastbuffer->PasteParagraph(current_view->buffer()->params);
393 } else if (!lastbuffer->Last()) {
394 lastbuffer->MakeSameLayout(lastbuffer->next);
395 lastbuffer->PasteParagraph(current_view->buffer()->params);
398 lastbuffer->Next()->StripLeadingSpaces(tc);
400 // restore the simple cut buffer
401 buf = simple_cut_clone;
408 int CutAndPaste::nrOfParagraphs()
413 LyXParagraph * tmppar = buf;
414 while(tmppar->next) {
416 tmppar = tmppar->next;
422 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
423 LyXTextClassList::size_type c2,
427 if (!par || c1 == c2)
430 par = par->FirstPhysicalPar();
433 string name = textclasslist.NameOfLayout(c1, par->layout);
435 pair<bool, LyXTextClass::LayoutList::size_type> pp =
436 textclasslist.NumberOfLayout(c2, name);
439 } else { // layout not found
440 // use default layout "Standard" (0)
445 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
447 string s = _("Layout had to be changed from\n")
449 + textclasslist.NameOfLayout(c2, par->layout)
450 + _("\nbecause of class conversion from\n")
451 + textclasslist.NameOfClass(c1) + _(" to ")
452 + textclasslist.NameOfClass(c2);
453 InsetError * new_inset = new InsetError(s);
454 par->InsertInset(0, new_inset);
463 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int)
465 if (!buf) return false;
468 // be carefull with footnotes in footnotes
469 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
470 // check whether the cut_buffer includes a footnote
471 LyXParagraph * tmppar = buf;
472 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
473 tmppar = tmppar->next;
476 WriteAlert(_("Impossible operation"),
477 _("Can't paste float into float!"),