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;
75 bool CutAndPaste::cutSelection(LyXParagraph * startpar, LyXParagraph ** endpar,
76 int start, int & end, char tc, bool doclear)
78 if (!startpar || (start > startpar->size()))
86 startpar == (*endpar)) {
87 // only within one paragraph
88 buf = new LyXParagraph;
89 LyXParagraph::size_type i = start;
90 if (end > startpar->size())
91 end = startpar->size();
92 for (; i < end; ++i) {
93 startpar->CopyIntoMinibuffer(*current_view->buffer(),
95 startpar->Erase(start);
97 buf->InsertFromMinibuffer(buf->size());
101 // more than one paragraph
102 (*endpar)->BreakParagraphConservative(current_view->buffer()->params,
104 *endpar = (*endpar)->next();
107 startpar->BreakParagraphConservative(current_view->buffer()->params,
110 // store the selection
111 buf = startpar->next();
114 (*endpar)->previous()->next(0);
117 startpar->next(*endpar);
119 (*endpar)->previous(startpar);
121 // the cut selection should begin with standard layout
124 // paste the paragraphs again, if possible
126 startpar->next()->StripLeadingSpaces(textclass);
127 if (startpar->HasSameLayout(startpar->next()) ||
128 !startpar->next()->size()) {
129 startpar->PasteParagraph(current_view->buffer()->params);
130 (*endpar) = startpar; // this because endpar gets deleted here!
137 bool CutAndPaste::copySelection(LyXParagraph * startpar, LyXParagraph * endpar,
138 int start, int end, char tc)
140 if (!startpar || (start > startpar->size()))
148 startpar == endpar) {
149 // only within one paragraph
150 buf = new LyXParagraph;
151 LyXParagraph::size_type i = start;
152 if (end > startpar->size())
153 end = startpar->size();
154 for (; i < end; ++i) {
155 startpar->CopyIntoMinibuffer(*current_view->buffer(), i);
156 buf->InsertFromMinibuffer(buf->size());
159 // copy more than one paragraph
160 // clone the paragraphs within the selection
161 LyXParagraph * tmppar = startpar;
163 buf = tmppar->Clone();
165 buf = new LyXParagraph(*tmppar);
167 LyXParagraph * tmppar2 = buf;
169 while (tmppar != endpar
171 tmppar = tmppar->next();
173 tmppar2->next(tmppar->Clone());
175 tmppar2->next(new LyXParagraph(*tmppar));
177 tmppar2->next()->previous(tmppar2);
178 tmppar2 = tmppar2->next();
182 // the buf paragraph is too big
183 LyXParagraph::size_type tmpi2 = start;
184 for (; tmpi2; --tmpi2)
187 // now tmppar 2 is too big, delete all after end
189 while (tmppar2->size() > tmpi2) {
190 tmppar2->Erase(tmppar2->size() - 1);
197 bool CutAndPaste::pasteSelection(LyXParagraph ** par, LyXParagraph ** endpar,
200 if (!checkPastePossible(*par))
203 if (pos > (*par)->size())
204 pos = (*par)->size();
206 // LyXParagraph * tmpbuf;
207 LyXParagraph * tmppar = *par;
210 // There are two cases: cutbuffer only one paragraph or many
212 // only within a paragraph
214 LyXParagraph * tmpbuf = buf->Clone();
216 LyXParagraph * tmpbuf = new LyXParagraph(*buf);
218 // Some provisions should be done here for checking
219 // if we are inserting at the beginning of a
220 // paragraph. If there are a space at the beginning
221 // of the text to insert and we are inserting at
222 // the beginning of the paragraph the space should
224 while (buf->size()) {
225 // This is an attempt to fix the
226 // "never insert a space at the
227 // beginning of a paragraph" problem.
228 if (!tmppos && buf->IsLineSeparator(0)) {
231 buf->CutIntoMinibuffer(current_view->buffer()->params, 0);
233 if (tmppar->InsertFromMinibuffer(tmppos))
239 *endpar = tmppar->next();
244 // make a copy of the simple cut_buffer
245 LyXParagraph * tmpbuf = buf;
247 LyXParagraph * simple_cut_clone = tmpbuf->Clone();
249 LyXParagraph * simple_cut_clone = new LyXParagraph(*tmpbuf);
251 LyXParagraph * tmpbuf2 = simple_cut_clone;
252 while (tmpbuf->next()) {
253 tmpbuf = tmpbuf->next();
255 tmpbuf2->next(tmpbuf->Clone());
257 tmpbuf2->next(new LyXParagraph(*tmpbuf));
259 tmpbuf2->next()->previous(tmpbuf2);
260 tmpbuf2 = tmpbuf2->next();
263 // make sure there is no class difference
264 SwitchLayoutsBetweenClasses(textclass, tc, buf);
266 // make the buf exactly the same layout than
267 // the cursor paragraph
268 buf->MakeSameLayout(*par);
270 // find the end of the buffer
271 LyXParagraph * lastbuffer = buf;
272 while (lastbuffer->next())
273 lastbuffer = lastbuffer->next();
275 bool paste_the_end = false;
277 // open the paragraph for inserting the buf
279 if (((*par)->size() > pos) || !(*par)->next()) {
280 (*par)->BreakParagraphConservative(current_view->buffer()->params,
282 paste_the_end = true;
284 // set the end for redoing later
285 *endpar = (*par)->next()->next();
288 lastbuffer->next((*par)->next());
289 (*par)->next()->previous(lastbuffer);
294 if ((*par)->next() == lastbuffer)
297 (*par)->PasteParagraph(current_view->buffer()->params);
298 // store the new cursor position
300 pos = lastbuffer->size();
301 // maybe some pasting
302 if (lastbuffer->next() && paste_the_end) {
303 if (lastbuffer->next()->HasSameLayout(lastbuffer)) {
304 lastbuffer->PasteParagraph(current_view->buffer()->params);
305 } else if (!lastbuffer->next()->size()) {
306 lastbuffer->next()->MakeSameLayout(lastbuffer);
307 lastbuffer->PasteParagraph(current_view->buffer()->params);
308 } else if (!lastbuffer->size()) {
309 lastbuffer->MakeSameLayout(lastbuffer->next());
310 lastbuffer->PasteParagraph(current_view->buffer()->params);
312 lastbuffer->next()->StripLeadingSpaces(tc);
314 // restore the simple cut buffer
315 buf = simple_cut_clone;
322 int CutAndPaste::nrOfParagraphs()
328 LyXParagraph * tmppar = buf;
329 while(tmppar->next()) {
331 tmppar = tmppar->next();
337 int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
338 LyXTextClassList::size_type c2,
342 if (!par || c1 == c2)
346 string name = textclasslist.NameOfLayout(c1, par->layout);
348 pair<bool, LyXTextClass::LayoutList::size_type> pp =
349 textclasslist.NumberOfLayout(c2, name);
352 } else { // layout not found
353 // use default layout "Standard" (0)
358 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
360 string s = _("Layout had to be changed from\n")
362 + textclasslist.NameOfLayout(c2, par->layout)
363 + _("\nbecause of class conversion from\n")
364 + textclasslist.NameOfClass(c1) + _(" to ")
365 + textclasslist.NameOfClass(c2);
366 InsetError * new_inset = new InsetError(s);
367 par->InsertInset(0, new_inset);
375 bool CutAndPaste::checkPastePossible(LyXParagraph *)
377 if (!buf) return false;