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"
22 #pragma implementation
27 extern BufferView * current_view;
29 // Jürgen, note that this means that you cannot currently have a list
30 // of selections cut/copied. So IMHO later we should have a
31 // list/vector/deque that we could store
32 // struct selection_item {
33 // LyXParagraph * buf;
34 // LyXTextClassList::size_type textclass;
36 // in and some method of choosing beween them (based on the first few chars
37 // in the selection probably.) This would be a nice feature and quite
38 // easy to implement. (Lgb)
40 // Sure but I just cleaned up this code for now with the same functionality
41 // as before. I also want to add a XClipboard function so that we can copy
42 // text from LyX to some other X-application in the form of ASCII or in the
43 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
44 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
45 // then do a middle mouse button click in the application you want and have
46 // the whole formula there in LaTeX-Code. (Jug)
50 LyXParagraph * buf = 0;
51 LyXTextClassList::size_type textclass = 0;
53 // for now here this should be in another Cut&Paste Class!
54 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
55 // member of the CutAndPaste class and in addition it was private.
56 // Perhaps it even should take a parameter? (Lgb)
62 LyXParagraph * tmppar;
78 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
79 int start, int & end, char tc, bool doclear)
82 if (!startpar || (start > startpar->Last()))
85 if (!startpar || (start > startpar->size()))
95 (startpar->ParFromPos(start) ==
96 (*endpar)->ParFromPos(end))
98 (startpar == (*endpar))
101 // only within one paragraph
102 buf = new LyXParagraph;
103 LyXParagraph::size_type i = start;
105 if (end > startpar->Last())
106 end = startpar->Last();
108 if (end > startpar->size())
109 end = startpar->size();
111 for (; i < end; ++i) {
112 startpar->CopyIntoMinibuffer(*current_view->buffer(), start);
113 startpar->Erase(start);
116 buf->InsertFromMinibuffer(buf->Last());
118 buf->InsertFromMinibuffer(buf->size());
123 // more than one paragraph
124 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
126 *endpar = (*endpar)->next();
129 startpar->BreakParagraphConservative(current_view->buffer()->params,
132 // store the selection
134 buf = startpar->ParFromPos(start)->next_;
136 buf = startpar->next();
139 (*endpar)->previous()->next(0);
143 startpar->ParFromPos(start)->next(*endpar);
145 (*endpar)->previous(startpar->ParFromPos(start));
147 startpar->next(*endpar);
149 (*endpar)->previous(startpar);
153 // care about footnotes
154 if (buf->footnoteflag) {
155 LyXParagraph * tmppar = buf;
157 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
158 tmppar = tmppar->next_;
163 // the cut selection should begin with standard layout
166 // paste the paragraphs again, if possible
168 startpar->next()->StripLeadingSpaces(textclass);
170 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->next()) ||
171 !startpar->next()->Last())
173 if (startpar->HasSameLayout(startpar->next()) ||
174 !startpar->next()->size())
178 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
180 startpar->PasteParagraph(current_view->buffer()->params);
182 (*endpar) = startpar; // this because endpar gets deleted here!
189 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
190 int start, int end, char tc)
193 if (!startpar || (start > startpar->Last()))
196 if (!startpar || (start > startpar->size()))
206 (startpar->ParFromPos(start) ==
207 (endpar)->ParFromPos(end))
212 // only within one paragraph
213 buf = new LyXParagraph;
214 LyXParagraph::size_type i = start;
216 if (end > startpar->Last())
217 end = startpar->Last();
219 if (end > startpar->size())
220 end = startpar->size();
222 for (; i < end; ++i) {
223 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
225 buf->InsertFromMinibuffer(buf->Last());
227 buf->InsertFromMinibuffer(buf->size());
231 // copy more than one paragraph
232 // clone the paragraphs within the selection
234 LyXParagraph * tmppar = startpar->ParFromPos(start);
236 LyXParagraph * tmppar = startpar;
238 buf = tmppar->Clone();
239 LyXParagraph * tmppar2 = buf;
242 while (tmppar != endpar->ParFromPos(end)
244 tmppar = tmppar->next_;
245 tmppar2->next(tmppar->Clone());
246 tmppar2->next_->previous(tmppar2);
247 tmppar2 = tmppar2->next_;
251 while (tmppar != endpar
253 tmppar = tmppar->next();
254 tmppar2->next(tmppar->Clone());
255 tmppar2->next()->previous(tmppar2);
256 tmppar2 = tmppar2->next();
262 // care about footnotes
263 if (buf->footnoteflag) {
266 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
267 tmppar = tmppar->next_;
272 // the buf paragraph is too big
274 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
276 LyXParagraph::size_type tmpi2 = start;
278 for (; tmpi2; --tmpi2)
281 // now tmppar 2 is too big, delete all after end
283 tmpi2 = endpar->PositionInParFromPos(end);
287 while (tmppar2->size() > tmpi2) {
288 tmppar2->Erase(tmppar2->size() - 1);
295 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
298 if (!checkPastePossible(*par))
302 if (pos > (*par)->Last())
303 pos = (*par)->Last();
305 if (pos > (*par)->size())
306 pos = (*par)->size();
309 LyXParagraph * tmpbuf;
310 LyXParagraph * tmppar = *par;
313 // There are two cases: cutbuffer only one paragraph or many
319 // only within a paragraph
320 tmpbuf = buf->Clone();
321 // Some provisions should be done here for checking
322 // if we are inserting at the beginning of a
323 // paragraph. If there are a space at the beginning
324 // of the text to insert and we are inserting at
325 // the beginning of the paragraph the space should
327 while (buf->size()) {
328 // This is an attempt to fix the
329 // "never insert a space at the
330 // beginning of a paragraph" problem.
331 if (!tmppos && buf->IsLineSeparator(0)) {
334 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
336 if (tmppar->InsertFromMinibuffer(tmppos))
342 *endpar = tmppar->next();
347 // make a copy of the simple cut_buffer
349 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
350 LyXParagraph * tmpbuf2 = simple_cut_clone;
352 if ((*par)->footnoteflag) {
353 tmpbuf->footnoteflag = (*par)->footnoteflag;
354 tmpbuf->footnotekind = (*par)->footnotekind;
356 while (tmpbuf->next_) {
357 tmpbuf = tmpbuf->next_;
358 tmpbuf2->next(tmpbuf->Clone());
359 tmpbuf2->next_->previous(tmpbuf2);
360 tmpbuf2 = tmpbuf2->next_;
361 if ((*par)->footnoteflag){
362 tmpbuf->footnoteflag = (*par)->footnoteflag;
363 tmpbuf->footnotekind = (*par)->footnotekind;
367 while (tmpbuf->next()) {
368 tmpbuf = tmpbuf->next();
369 tmpbuf2->next(tmpbuf->Clone());
370 tmpbuf2->next()->previous(tmpbuf2);
371 tmpbuf2 = tmpbuf2->next();
375 // make sure there is no class difference
376 SwitchLayoutsBetweenClasses(textclass, tc, buf);
378 // make the buf exactly the same layout than
379 // the cursor paragraph
380 buf->MakeSameLayout(*par);
382 // find the end of the buffer
383 LyXParagraph * lastbuffer = buf;
384 while (lastbuffer->next())
385 lastbuffer = lastbuffer->next();
387 bool paste_the_end = false;
390 // open the paragraph for inserting the buf
392 if (((*par)->Last() > pos) || !(*par)->next()) {
393 (*par)->BreakParagraphConservative(current_view->buffer()->params,
395 paste_the_end = true;
397 // set the end for redoing later
398 *endpar = (*par)->ParFromPos(pos)->next_->next();
401 lastbuffer->ParFromPos(lastbuffer->Last())->next(
402 (*par)->ParFromPos(pos)->next_);
403 (*par)->ParFromPos(pos)->next()->previous(
404 lastbuffer->ParFromPos(lastbuffer->Last()));
406 (*par)->ParFromPos(pos)->next(buf);
407 buf->previous((*par)->ParFromPos(pos));
409 if ((*par)->ParFromPos(pos)->next() == lastbuffer)
412 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
413 // store the new cursor position
415 pos = lastbuffer->Last();
416 // maybe some pasting
417 if (lastbuffer->next() && paste_the_end) {
418 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
419 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
420 } else if (!lastbuffer->next()->Last()) {
421 lastbuffer->next()->MakeSameLayout(lastbuffer);
422 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
423 } else if (!lastbuffer->Last()) {
424 lastbuffer->MakeSameLayout(lastbuffer->next_);
425 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
427 lastbuffer->next()->StripLeadingSpaces(tc);
429 // restore the simple cut buffer
430 buf = simple_cut_clone;
433 // open the paragraph for inserting the buf
435 if (((*par)->size() > pos) || !(*par)->next()) {
436 (*par)->BreakParagraphConservative(current_view->buffer()->params,
438 paste_the_end = true;
440 // set the end for redoing later
441 *endpar = (*par)->next()->next();
444 lastbuffer->next((*par)->next());
445 (*par)->next()->previous(lastbuffer);
450 if ((*par)->next() == lastbuffer)
453 (*par)->PasteParagraph(current_view->buffer()->params);
454 // store the new cursor position
456 pos = lastbuffer->size();
457 // maybe some pasting
458 if (lastbuffer->next() && paste_the_end) {
459 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
460 lastbuffer->PasteParagraph(current_view->buffer()->params);
461 } else if (!lastbuffer->next()->size()) {
462 lastbuffer->next()->MakeSameLayout(lastbuffer);
463 lastbuffer->PasteParagraph(current_view->buffer()->params);
464 } else if (!lastbuffer->size()) {
465 lastbuffer->MakeSameLayout(lastbuffer->next());
466 lastbuffer->PasteParagraph(current_view->buffer()->params);
468 lastbuffer->next()->StripLeadingSpaces(tc);
470 // restore the simple cut buffer
471 buf = simple_cut_clone;
479 int CutAndPaste::nrOfParagraphs()
485 LyXParagraph * tmppar = buf;
487 while(tmppar->next_) {
489 tmppar = tmppar->next_;
492 while(tmppar->next()) {
494 tmppar = tmppar->next();
501 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
502 LyXTextClassList::size_type c2,
506 if (!par || c1 == c2)
509 par = par->FirstPhysicalPar();
512 string name = textclasslist.NameOfLayout(c1, par->layout);
514 pair<bool, LyXTextClass::LayoutList::size_type> pp =
515 textclasslist.NumberOfLayout(c2, name);
518 } else { // layout not found
519 // use default layout "Standard" (0)
524 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
526 string s = _("Layout had to be changed from\n")
528 + textclasslist.NameOfLayout(c2, par->layout)
529 + _("\nbecause of class conversion from\n")
530 + textclasslist.NameOfClass(c1) + _(" to ")
531 + textclasslist.NameOfClass(c2);
532 InsetError * new_inset = new InsetError(s);
533 par->InsertInset(0, new_inset);
546 bool CutAndPaste::checkPastePossible(LyXParagraph * par)
548 bool CutAndPaste::checkPastePossible(LyXParagraph *)
551 if (!buf) return false;
554 // be carefull with footnotes in footnotes
555 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
556 // check whether the cut_buffer includes a footnote
557 LyXParagraph * tmppar = buf;
558 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
559 tmppar = tmppar->next_;
562 WriteAlert(_("Impossible operation"),
563 _("Can't paste float into float!"),