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;
75 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
76 int start, int & end, char tc, bool doclear)
78 if (!startpar || (start > startpar->Last()))
87 (startpar->ParFromPos(start) ==
88 (*endpar)->ParFromPos(end))
90 (startpar == (*endpar))
93 // only within one paragraph
94 buf = new LyXParagraph;
95 LyXParagraph::size_type i = start;
96 if (end > startpar->Last())
97 end = startpar->Last();
98 for (; i < end; ++i) {
99 startpar->CopyIntoMinibuffer(*current_view->buffer(), start);
100 startpar->Erase(start);
102 buf->InsertFromMinibuffer(buf->Last());
106 // more than one paragraph
107 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
109 *endpar = (*endpar)->next();
112 startpar->BreakParagraphConservative(current_view->buffer()->params,
115 // store the selection
117 buf = startpar->ParFromPos(start)->next_;
119 buf = startpar->next();
122 (*endpar)->previous()->next(0);
126 startpar->ParFromPos(start)->next(*endpar);
128 (*endpar)->previous(startpar->ParFromPos(start));
130 startpar->next(*endpar);
132 (*endpar)->previous(startpar);
136 // care about footnotes
137 if (buf->footnoteflag) {
138 LyXParagraph * tmppar = buf;
140 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
141 tmppar = tmppar->next_;
146 // the cut selection should begin with standard layout
149 // paste the paragraphs again, if possible
151 startpar->next()->StripLeadingSpaces(textclass);
153 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->next()) ||
155 if (startpar->HasSameLayout(startpar->next()) ||
157 !startpar->next()->Last()) {
159 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
161 startpar->PasteParagraph(current_view->buffer()->params);
163 (*endpar) = startpar; // this because endpar gets deleted here!
170 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
171 int start, int end, char tc)
173 if (!startpar || (start > startpar->Last()))
182 (startpar->ParFromPos(start) ==
183 (endpar)->ParFromPos(end))
188 // only within one paragraph
189 buf = new LyXParagraph;
190 LyXParagraph::size_type i = start;
191 if (end > startpar->Last())
192 end = startpar->Last();
193 for (; i < end; ++i) {
194 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
195 buf->InsertFromMinibuffer(buf->Last());
198 // copy more than one paragraph
199 // clone the paragraphs within the selection
201 LyXParagraph * tmppar = startpar->ParFromPos(start);
203 LyXParagraph * tmppar = startpar;
205 buf = tmppar->Clone();
206 LyXParagraph * tmppar2 = buf;
209 while (tmppar != endpar->ParFromPos(end)
211 tmppar = tmppar->next_;
212 tmppar2->next(tmppar->Clone());
213 tmppar2->next_->previous(tmppar2);
214 tmppar2 = tmppar2->next_;
218 while (tmppar != endpar
220 tmppar = tmppar->next();
221 tmppar2->next(tmppar->Clone());
222 tmppar2->next()->previous(tmppar2);
223 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
281 // only within a paragraph
282 tmpbuf = buf->Clone();
283 // Some provisions should be done here for checking
284 // if we are inserting at the beginning of a
285 // paragraph. If there are a space at the beginning
286 // of the text to insert and we are inserting at
287 // the beginning of the paragraph the space should
289 while (buf->size()) {
290 // This is an attempt to fix the
291 // "never insert a space at the
292 // beginning of a paragraph" problem.
293 if (!tmppos && buf->IsLineSeparator(0)) {
296 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
298 if (tmppar->InsertFromMinibuffer(tmppos))
304 *endpar = tmppar->next();
309 // make a copy of the simple cut_buffer
311 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
312 LyXParagraph * tmpbuf2 = simple_cut_clone;
314 if ((*par)->footnoteflag) {
315 tmpbuf->footnoteflag = (*par)->footnoteflag;
316 tmpbuf->footnotekind = (*par)->footnotekind;
318 while (tmpbuf->next_) {
319 tmpbuf = tmpbuf->next_;
320 tmpbuf2->next(tmpbuf->Clone());
321 tmpbuf2->next_->previous(tmpbuf2);
322 tmpbuf2 = tmpbuf2->next_;
323 if ((*par)->footnoteflag){
324 tmpbuf->footnoteflag = (*par)->footnoteflag;
325 tmpbuf->footnotekind = (*par)->footnotekind;
329 while (tmpbuf->next()) {
330 tmpbuf = tmpbuf->next();
331 tmpbuf2->next(tmpbuf->Clone());
332 tmpbuf2->next()->previous(tmpbuf2);
333 tmpbuf2 = tmpbuf2->next();
337 // make sure there is no class difference
338 SwitchLayoutsBetweenClasses(textclass, tc, buf);
340 // make the buf exactly the same layout than
341 // the cursor paragraph
342 buf->MakeSameLayout(*par);
344 // find the end of the buffer
345 LyXParagraph * lastbuffer = buf;
346 while (lastbuffer->next())
347 lastbuffer = lastbuffer->next();
349 bool paste_the_end = false;
351 // open the paragraph for inserting the buf
353 if (((*par)->Last() > pos) || !(*par)->next()) {
354 (*par)->BreakParagraphConservative(current_view->buffer()->params,
356 paste_the_end = true;
359 // set the end for redoing later
361 *endpar = (*par)->ParFromPos(pos)->next_->next();
364 lastbuffer->ParFromPos(lastbuffer->Last())->next(
365 (*par)->ParFromPos(pos)->next_);
366 (*par)->ParFromPos(pos)->next()->previous(
367 lastbuffer->ParFromPos(lastbuffer->Last()));
369 (*par)->ParFromPos(pos)->next(buf);
370 buf->previous((*par)->ParFromPos(pos));
372 if ((*par)->ParFromPos(pos)->next() == lastbuffer)
375 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
377 *endpar = (*par)->next()->next();
380 lastbuffer->next((*par)->next());
381 (*par)->next()->previous(lastbuffer);
386 if ((*par)->next() == lastbuffer)
389 (*par)->PasteParagraph(current_view->buffer()->params);
391 // store the new cursor position
393 pos = lastbuffer->Last();
395 // maybe some pasting
396 if (lastbuffer->next() && paste_the_end) {
398 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
399 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
400 } else if (!lastbuffer->next()->Last()) {
401 lastbuffer->next()->MakeSameLayout(lastbuffer);
402 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
403 } else if (!lastbuffer->Last()) {
404 lastbuffer->MakeSameLayout(lastbuffer->next_);
405 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
407 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
408 lastbuffer->PasteParagraph(current_view->buffer()->params);
409 } else if (!lastbuffer->next()->Last()) {
410 lastbuffer->next()->MakeSameLayout(lastbuffer);
411 lastbuffer->PasteParagraph(current_view->buffer()->params);
412 } else if (!lastbuffer->Last()) {
413 lastbuffer->MakeSameLayout(lastbuffer->next());
414 lastbuffer->PasteParagraph(current_view->buffer()->params);
417 lastbuffer->next()->StripLeadingSpaces(tc);
419 // restore the simple cut buffer
420 buf = simple_cut_clone;
427 int CutAndPaste::nrOfParagraphs()
432 LyXParagraph * tmppar = buf;
434 while(tmppar->next_) {
436 tmppar = tmppar->next_;
439 while(tmppar->next()) {
441 tmppar = tmppar->next();
448 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
449 LyXTextClassList::size_type c2,
453 if (!par || c1 == c2)
456 par = par->FirstPhysicalPar();
459 string name = textclasslist.NameOfLayout(c1, par->layout);
461 pair<bool, LyXTextClass::LayoutList::size_type> pp =
462 textclasslist.NumberOfLayout(c2, name);
465 } else { // layout not found
466 // use default layout "Standard" (0)
471 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
473 string s = _("Layout had to be changed from\n")
475 + textclasslist.NameOfLayout(c2, par->layout)
476 + _("\nbecause of class conversion from\n")
477 + textclasslist.NameOfClass(c1) + _(" to ")
478 + textclasslist.NameOfClass(c2);
479 InsetError * new_inset = new InsetError(s);
480 par->InsertInset(0, new_inset);
492 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int)
494 if (!buf) return false;
497 // be carefull with footnotes in footnotes
498 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
499 // check whether the cut_buffer includes a footnote
500 LyXParagraph * tmppar = buf;
501 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
502 tmppar = tmppar->next_;
505 WriteAlert(_("Impossible operation"),
506 _("Can't paste float into float!"),