1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "CutAndPaste.h"
13 #include "BufferView.h"
15 #include "paragraph.h"
16 #include "insets/inseterror.h"
17 #include "lyx_gui_misc.h"
18 #include "lyxcursor.h"
22 #pragma implementation
27 using lyx::layout_type;
28 using lyx::textclass_type;
30 extern BufferView * current_view;
32 // Jürgen, note that this means that you cannot currently have a list
33 // of selections cut/copied. So IMHO later we should have a
34 // list/vector/deque that we could store
35 // struct selection_item {
37 // LyXTextClassList::size_type textclass;
39 // in and some method of choosing beween them (based on the first few chars
40 // in the selection probably.) This would be a nice feature and quite
41 // easy to implement. (Lgb)
43 // Sure but I just cleaned up this code for now with the same functionality
44 // as before. I also want to add a XClipboard function so that we can copy
45 // text from LyX to some other X-application in the form of ASCII or in the
46 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
47 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
48 // then do a middle mouse button click in the application you want and have
49 // the whole formula there in LaTeX-Code. (Jug)
54 textclass_type textclass = 0;
56 // for now here this should be in another Cut&Paste Class!
57 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
58 // member of the CutAndPaste class and in addition it was private.
59 // Perhaps it even should take a parameter? (Lgb)
78 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
79 int start, int & end, char tc, bool doclear,
82 if (!startpar || (start > startpar->size()))
90 if (!(*endpar) || startpar == (*endpar)) {
91 // only within one paragraph
95 if (end > startpar->size())
96 end = startpar->size();
97 for (; i < end; ++i) {
99 startpar->copyIntoMinibuffer(*current_view->buffer(),
101 startpar->erase(start);
103 buf->insertFromMinibuffer(buf->size());
107 // more than one paragraph
108 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
110 *endpar = (*endpar)->next();
113 startpar->breakParagraphConservative(current_view->buffer()->params,
116 // store the selection
118 buf = startpar->next();
121 startpar->next()->previous(0);
123 (*endpar)->previous()->next(0);
126 startpar->next(*endpar);
128 (*endpar)->previous(startpar);
130 // the cut selection should begin with standard layout
134 // paste the paragraphs again, if possible
136 startpar->next()->stripLeadingSpaces(textclass);
137 if (startpar->hasSameLayout(startpar->next()) ||
138 !startpar->next()->size()) {
139 startpar->pasteParagraph(current_view->buffer()->params);
140 (*endpar) = startpar; // this because endpar gets deleted here!
142 // this paragraph's are of noone's owner!
153 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
154 int start, int end, char tc)
156 if (!startpar || (start > startpar->size()))
163 if (!endpar || startpar == endpar) {
164 // only within one paragraph
167 if (end > startpar->size())
168 end = startpar->size();
169 for (; i < end; ++i) {
170 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
171 buf->insertFromMinibuffer(buf->size());
174 // copy more than one paragraph
175 // clone the paragraphs within the selection
176 Paragraph * tmppar = startpar;
177 buf = new Paragraph(*tmppar, false);
178 Paragraph * tmppar2 = buf;
180 while (tmppar != endpar
182 tmppar = tmppar->next();
183 tmppar2->next(new Paragraph(*tmppar, false));
184 tmppar2->next()->previous(tmppar2);
185 tmppar2 = tmppar2->next();
189 // the buf paragraph is too big
190 pos_type tmpi2 = start;
191 for (; tmpi2; --tmpi2)
194 // now tmppar 2 is too big, delete all after end
196 while (tmppar2->size() > tmpi2) {
197 tmppar2->erase(tmppar2->size() - 1);
199 // this paragraph's are of noone's owner!
202 tmppar->setInsetOwner(0);
203 tmppar = tmppar->next();
210 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
213 if (!checkPastePossible(*par))
216 if (pos > (*par)->size())
217 pos = (*par)->size();
219 // Paragraph * tmpbuf;
220 Paragraph * tmppar = *par;
223 // There are two cases: cutbuffer only one paragraph or many
225 // only within a paragraph
226 Paragraph * tmpbuf = new Paragraph(*buf, false);
228 // Some provisions should be done here for checking
229 // if we are inserting at the beginning of a
230 // paragraph. If there are a space at the beginning
231 // of the text to insert and we are inserting at
232 // the beginning of the paragraph the space should
234 while (buf->size()) {
235 // This is an attempt to fix the
236 // "never insert a space at the
237 // beginning of a paragraph" problem.
238 if (!tmppos && buf->isLineSeparator(0)) {
241 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
243 if (tmppar->insertFromMinibuffer(tmppos))
249 *endpar = tmppar->next();
254 // make a copy of the simple cut_buffer
255 Paragraph * tmpbuf = buf;
256 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
257 Paragraph * tmpbuf2 = simple_cut_clone;
259 while (tmpbuf->next()) {
260 tmpbuf = tmpbuf->next();
261 tmpbuf2->next(new Paragraph(*tmpbuf, false));
262 tmpbuf2->next()->previous(tmpbuf2);
263 tmpbuf2 = tmpbuf2->next();
266 // make sure there is no class difference
267 SwitchLayoutsBetweenClasses(textclass, tc, buf);
269 // make the buf exactly the same layout than
270 // the cursor paragraph
271 buf->makeSameLayout(*par);
273 // find the end of the buffer
274 Paragraph * lastbuffer = buf;
275 while (lastbuffer->next())
276 lastbuffer = lastbuffer->next();
278 bool paste_the_end = false;
280 // open the paragraph for inserting the buf
282 if (((*par)->size() > pos) || !(*par)->next()) {
283 (*par)->breakParagraphConservative(current_view->buffer()->params,
285 paste_the_end = true;
287 // set the end for redoing later
288 *endpar = (*par)->next()->next();
291 lastbuffer->next((*par)->next());
292 (*par)->next()->previous(lastbuffer);
297 if ((*par)->next() == lastbuffer)
300 (*par)->pasteParagraph(current_view->buffer()->params);
301 // store the new cursor position
303 pos = lastbuffer->size();
304 // maybe some pasting
305 if (lastbuffer->next() && paste_the_end) {
306 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
307 lastbuffer->pasteParagraph(current_view->buffer()->params);
308 } else if (!lastbuffer->next()->size()) {
309 lastbuffer->next()->makeSameLayout(lastbuffer);
310 lastbuffer->pasteParagraph(current_view->buffer()->params);
311 } else if (!lastbuffer->size()) {
312 lastbuffer->makeSameLayout(lastbuffer->next());
313 lastbuffer->pasteParagraph(current_view->buffer()->params);
315 lastbuffer->next()->stripLeadingSpaces(tc);
317 // restore the simple cut buffer
318 buf = simple_cut_clone;
325 int CutAndPaste::nrOfParagraphs()
331 Paragraph * tmppar = buf;
332 while(tmppar->next()) {
334 tmppar = tmppar->next();
340 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
341 textclass_type c2, Paragraph * par)
344 if (!par || c1 == c2)
348 string const name = textclasslist.NameOfLayout(c1, par->layout);
350 pair<bool, layout_type> pp =
351 textclasslist.NumberOfLayout(c2, name);
354 } else { // layout not found
355 // use default layout "Standard" (0)
360 if (name != textclasslist.NameOfLayout(c2, par->layout)) {
362 string const s = _("Layout had to be changed from\n")
364 + textclasslist.NameOfLayout(c2, par->layout)
365 + _("\nbecause of class conversion from\n")
366 + textclasslist.NameOfClass(c1) + _(" to ")
367 + textclasslist.NameOfClass(c2);
368 InsetError * new_inset = new InsetError(s);
369 par->insertInset(0, new_inset);
377 bool CutAndPaste::checkPastePossible(Paragraph *)
379 if (!buf) return false;