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()))
81 if (!(*endpar) || (startpar->ParFromPos(start) ==
82 (*endpar)->ParFromPos(end))) {
83 // only within one paragraph
84 buf = new LyXParagraph;
85 LyXParagraph::size_type i = start;
86 if (end > startpar->Last())
87 end = startpar->Last();
88 for (; i < end; ++i) {
89 startpar->CopyIntoMinibuffer(current_view->buffer()->params,
92 /* table stuff -- begin */
93 if (startpar->table && startpar->IsNewline(start)) {
96 /* table stuff -- end */
98 startpar->Erase(start);
102 buf->InsertFromMinibuffer(buf->Last());
105 // more than one paragraph
106 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
108 *endpar = (*endpar)->Next();
111 startpar->BreakParagraphConservative(current_view->buffer()->params,
114 // store the selection
115 buf = startpar->ParFromPos(start)->next;
117 (*endpar)->previous->next = 0;
120 startpar->ParFromPos(start)->next = (*endpar);
122 (*endpar)->previous = startpar->ParFromPos(start);
124 // care about footnotes
125 if (buf->footnoteflag) {
126 LyXParagraph * tmppar = buf;
128 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
129 tmppar = tmppar->next;
133 // the cut selection should begin with standard layout
136 // paste the paragraphs again, if possible
138 startpar->Next()->StripLeadingSpaces(textclass);
139 if (startpar->FirstPhysicalPar()->HasSameLayout(startpar->Next()) ||
140 !startpar->Next()->Last()) {
141 startpar->ParFromPos(start)->PasteParagraph(current_view->buffer()->params);
142 (*endpar) = startpar; // this because endpar gets deleted here!
149 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
150 int start, int end, char tc)
152 if (!startpar || (start > startpar->Last()))
159 if (!(endpar) || (startpar->ParFromPos(start) ==
160 (endpar)->ParFromPos(end))) {
161 // only within one paragraph
162 buf = new LyXParagraph;
163 LyXParagraph::size_type i = start;
164 if (end > startpar->Last())
165 end = startpar->Last();
166 for (; i < end; ++i) {
167 startpar->CopyIntoMinibuffer(current_view->buffer()->params, i);
168 buf->InsertFromMinibuffer(buf->Last());
171 // copy more than one paragraph
172 // clone the paragraphs within the selection
173 LyXParagraph *tmppar = startpar->ParFromPos(start);
174 buf = tmppar->Clone();
175 LyXParagraph *tmppar2 = buf;
177 while (tmppar != endpar->ParFromPos(end)
179 tmppar = tmppar->next;
180 tmppar2->next = tmppar->Clone();
181 tmppar2->next->previous = tmppar2;
182 tmppar2 = tmppar2->next;
186 // care about footnotes
187 if (buf->footnoteflag) {
190 tmppar->footnoteflag = LyXParagraph::NO_FOOTNOTE;
191 tmppar = tmppar->next;
195 // the buf paragraph is too big
196 LyXParagraph::size_type tmpi2 = startpar->PositionInParFromPos(start);
197 for (; tmpi2; --tmpi2)
200 // now tmppar 2 is too big, delete all after end
202 tmpi2 = endpar->PositionInParFromPos(end);
203 while (tmppar2->size() > tmpi2) {
204 tmppar2->Erase(tmppar2->size() - 1);
211 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
214 if (!checkPastePossible(*par, pos))
217 if (pos > (*par)->Last())
218 pos = (*par)->Last();
220 LyXParagraph * tmpbuf;
221 LyXParagraph * tmppar = *par;
224 // There are two cases: cutbuffer only one paragraph or many
226 // only within a paragraph
227 tmpbuf = buf->Clone();
229 /* table stuff -- begin */
230 bool table_too_small = false;
232 while (buf->size() && !table_too_small) {
233 if (buf->IsNewline(0)){
234 while((tmppos < tmppar->Last()) &&
235 !tmppar->IsNewline(tmppos))
238 if (tmppos < tmppar->Last())
241 table_too_small = true;
243 // This is an attempt to fix the
244 // "never insert a space at the
245 // beginning of a paragraph" problem.
246 if (!tmppos && buf->IsLineSeparator(0)) {
249 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
251 if (tmppar->InsertFromMinibuffer(tmppos))
257 /* table stuff -- end */
259 // Some provisions should be done here for checking
260 // if we are inserting at the beginning of a
261 // paragraph. If there are a space at the beginning
262 // of the text to insert and we are inserting at
263 // the beginning of the paragraph the space should
265 while (buf->size()) {
266 // This is an attempt to fix the
267 // "never insert a space at the
268 // beginning of a paragraph" problem.
269 if (!tmppos && buf->IsLineSeparator(0)) {
272 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
274 if (tmppar->InsertFromMinibuffer(tmppos))
283 *endpar = tmppar->Next();
288 // make a copy of the simple cut_buffer
290 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
291 LyXParagraph * tmpbuf2 = simple_cut_clone;
292 if ((*par)->footnoteflag){
293 tmpbuf->footnoteflag = (*par)->footnoteflag;
294 tmpbuf->footnotekind = (*par)->footnotekind;
296 while (tmpbuf->next) {
297 tmpbuf = tmpbuf->next;
298 tmpbuf2->next = tmpbuf->Clone();
299 tmpbuf2->next->previous = tmpbuf2;
300 tmpbuf2 = tmpbuf2->next;
301 if ((*par)->footnoteflag){
302 tmpbuf->footnoteflag = (*par)->footnoteflag;
303 tmpbuf->footnotekind = (*par)->footnotekind;
307 // make sure there is no class difference
308 SwitchLayoutsBetweenClasses(textclass, tc, buf);
310 // make the buf exactly the same layout than
311 // the cursor paragraph
312 buf->MakeSameLayout(*par);
314 // find the end of the buffer
315 LyXParagraph * lastbuffer = buf;
316 while (lastbuffer->Next())
317 lastbuffer = lastbuffer->Next();
319 bool paste_the_end = false;
321 // open the paragraph for inserting the buf
323 if (((*par)->Last() > pos) || !(*par)->Next()) {
324 (*par)->BreakParagraphConservative(current_view->buffer()->params,
326 paste_the_end = true;
329 // set the end for redoing later
330 *endpar = (*par)->ParFromPos(pos)->next->Next();
333 lastbuffer->ParFromPos(lastbuffer->Last())->next =
334 (*par)->ParFromPos(pos)->next;
335 (*par)->ParFromPos(pos)->next->previous =
336 lastbuffer->ParFromPos(lastbuffer->Last());
338 (*par)->ParFromPos(pos)->next = buf;
339 buf->previous = (*par)->ParFromPos(pos);
341 if ((*par)->ParFromPos(pos)->Next() == lastbuffer)
344 (*par)->ParFromPos(pos)->PasteParagraph(current_view->buffer()->params);
346 // store the new cursor position
348 pos = lastbuffer->Last();
350 // maybe some pasting
351 if (lastbuffer->Next() && paste_the_end) {
352 if (lastbuffer->Next()->HasSameLayout(lastbuffer)) {
353 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
354 } else if (!lastbuffer->Next()->Last()) {
355 lastbuffer->Next()->MakeSameLayout(lastbuffer);
356 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
357 } else if (!lastbuffer->Last()) {
358 lastbuffer->MakeSameLayout(lastbuffer->next);
359 lastbuffer->ParFromPos(lastbuffer->Last())->PasteParagraph(current_view->buffer()->params);
361 lastbuffer->Next()->StripLeadingSpaces(tc);
363 // restore the simple cut buffer
364 buf = simple_cut_clone;
371 int CutAndPaste::nrOfParagraphs() const
376 LyXParagraph * tmppar = buf;
377 while(tmppar->next) {
379 tmppar = tmppar->next;
385 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
386 LyXTextClassList::size_type c2,
390 if (!par || c1 == c2)
392 par = par->FirstPhysicalPar();
394 string name = textclasslist.NameOfLayout(c1, par->layout);
396 pair<bool, LyXTextClass::LayoutList::size_type> pp =
397 textclasslist.NumberOfLayout(c2, name);
400 } else { // layout not found
401 // use default layout "Standard" (0)
406 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
408 string s = _("Layout had to be changed from\n")
410 + textclasslist.NameOfLayout(c2, par->layout)
411 + _("\nbecause of class conversion from\n")
412 + textclasslist.NameOfClass(c1) + _(" to ")
413 + textclasslist.NameOfClass(c2);
414 InsetError * new_inset = new InsetError(s);
415 par->InsertInset(0, new_inset);
424 bool CutAndPaste::checkPastePossible(LyXParagraph * par, int) const
426 if (!buf) return false;
428 LyXParagraph * tmppar;
430 // be carefull with footnotes in footnotes
431 if (par->footnoteflag != LyXParagraph::NO_FOOTNOTE) {
432 // check whether the cut_buffer includes a footnote
434 while (tmppar && tmppar->footnoteflag == LyXParagraph::NO_FOOTNOTE)
435 tmppar = tmppar->next;
438 WriteAlert(_("Impossible operation"),
439 _("Can't paste float into float!"),
445 /* table stuff -- begin */
448 WriteAlert(_("Impossible operation"),
449 _("Table cell cannot include more than one paragraph!"),
454 /* table stuff -- end */