1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
13 #pragma implementation
16 #include "CutAndPaste.h"
17 #include "BufferView.h"
19 #include "paragraph.h"
20 #include "lyxcursor.h"
22 #include "iterators.h"
23 #include "lyxtextclasslist.h"
25 #include "insets/inseterror.h"
29 using lyx::textclass_type;
31 extern BufferView * current_view;
33 // Jürgen, note that this means that you cannot currently have a list
34 // of selections cut/copied. So IMHO later we should have a
35 // list/vector/deque that we could store
36 // struct selection_item {
38 // LyXTextClassList::size_type textclass;
40 // in and some method of choosing beween them (based on the first few chars
41 // in the selection probably.) This would be a nice feature and quite
42 // easy to implement. (Lgb)
44 // Sure but I just cleaned up this code for now with the same functionality
45 // as before. I also want to add a XClipboard function so that we can copy
46 // text from LyX to some other X-application in the form of ASCII or in the
47 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
48 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
49 // then do a middle mouse button click in the application you want and have
50 // the whole formula there in LaTeX-Code. (Jug)
55 textclass_type textclass = 0;
57 // for now here this should be in another Cut&Paste Class!
58 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
59 // member of the CutAndPaste class and in addition it was private.
60 // Perhaps it even should take a parameter? (Lgb)
79 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
80 int start, int & end, char tc, bool doclear,
83 if (!startpar || (start > startpar->size()))
91 if (!(*endpar) || startpar == (*endpar)) {
92 // only within one paragraph
96 if (end > startpar->size())
97 end = startpar->size();
98 for (; i < end; ++i) {
100 startpar->copyIntoMinibuffer(*current_view->buffer(),
102 startpar->erase(start);
104 buf->insertFromMinibuffer(buf->size());
108 // more than one paragraph
109 (*endpar)->breakParagraphConservative(current_view->buffer()->params,
111 *endpar = (*endpar)->next();
114 startpar->breakParagraphConservative(current_view->buffer()->params,
117 // store the selection
119 buf = startpar->next();
122 startpar->next()->previous(0);
124 (*endpar)->previous()->next(0);
127 startpar->next(*endpar);
129 (*endpar)->previous(startpar);
131 // the cut selection should begin with standard layout
135 // paste the paragraphs again, if possible
137 startpar->next()->stripLeadingSpaces(textclass);
138 if (startpar->hasSameLayout(startpar->next()) ||
139 !startpar->next()->size()) {
140 startpar->pasteParagraph(current_view->buffer()->params);
141 (*endpar) = startpar; // this because endpar gets deleted here!
143 // this paragraph's are of noone's owner!
154 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
155 int start, int end, char tc)
157 if (!startpar || (start > startpar->size()))
164 if (!endpar || startpar == endpar) {
165 // only within one paragraph
168 if (end > startpar->size())
169 end = startpar->size();
170 for (; i < end; ++i) {
171 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
172 buf->insertFromMinibuffer(buf->size());
175 // copy more than one paragraph
176 // clone the paragraphs within the selection
177 Paragraph * tmppar = startpar;
178 buf = new Paragraph(*tmppar, false);
179 Paragraph * tmppar2 = buf;
181 while (tmppar != endpar
183 tmppar = tmppar->next();
184 tmppar2->next(new Paragraph(*tmppar, false));
185 tmppar2->next()->previous(tmppar2);
186 tmppar2 = tmppar2->next();
190 // the buf paragraph is too big
191 pos_type tmpi2 = start;
192 for (; tmpi2; --tmpi2)
195 // now tmppar 2 is too big, delete all after end
197 while (tmppar2->size() > tmpi2) {
198 tmppar2->erase(tmppar2->size() - 1);
200 // this paragraph's are of noone's owner!
203 tmppar->setInsetOwner(0);
204 tmppar = tmppar->next();
211 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
214 if (!checkPastePossible(*par))
217 if (pos > (*par)->size())
218 pos = (*par)->size();
221 // Paragraph * tmpbuf;
222 Paragraph * tmppar = *par;
225 // There are two cases: cutbuffer only one paragraph or many
227 // only within a paragraph
228 Paragraph * tmpbuf = new Paragraph(*buf, false);
230 // Some provisions should be done here for checking
231 // if we are inserting at the beginning of a
232 // paragraph. If there are a space at the beginning
233 // of the text to insert and we are inserting at
234 // the beginning of the paragraph the space should
236 while (buf->size()) {
237 // This is an attempt to fix the
238 // "never insert a space at the
239 // beginning of a paragraph" problem.
240 if (!tmppos && buf->isLineSeparator(0)) {
243 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
245 if (tmppar->insertFromMinibuffer(tmppos))
251 *endpar = tmppar->next();
258 // make a copy of the simple cut_buffer
259 Paragraph * tmpbuf = buf;
260 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
261 Paragraph * tmpbuf2 = simple_cut_clone;
263 while (tmpbuf->next()) {
264 tmpbuf = tmpbuf->next();
265 tmpbuf2->next(new Paragraph(*tmpbuf, false));
266 tmpbuf2->next()->previous(tmpbuf2);
267 tmpbuf2 = tmpbuf2->next();
270 // now remove all out of the buffer which is NOT allowed in the
271 // new environment and set also another font if that is required
274 // set the inset owner of this paragraph
275 tmpbuf->setInsetOwner((*par)->inInset());
276 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
277 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
278 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
283 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
285 if (!(*par)->checkInsertChar(f1)) {
287 } else if (f1 != f2) {
288 tmpbuf->setFont(i, f1);
292 tmpbuf = tmpbuf->next();
295 // make sure there is no class difference
296 SwitchLayoutsBetweenClasses(textclass, tc, buf,
297 current_view->buffer()->params);
299 // make the buf exactly the same layout than
300 // the cursor paragraph
301 buf->makeSameLayout(*par);
303 // find the end of the buffer
304 Paragraph * lastbuffer = buf;
305 while (lastbuffer->next())
306 lastbuffer = lastbuffer->next();
308 bool paste_the_end = false;
310 // open the paragraph for inserting the buf
312 if (((*par)->size() > pos) || !(*par)->next()) {
313 (*par)->breakParagraphConservative(current_view->buffer()->params,
315 paste_the_end = true;
317 // set the end for redoing later
318 *endpar = (*par)->next()->next();
321 lastbuffer->next((*par)->next());
322 (*par)->next()->previous(lastbuffer);
327 if ((*par)->next() == lastbuffer)
330 (*par)->pasteParagraph(current_view->buffer()->params);
331 // store the new cursor position
333 pos = lastbuffer->size();
334 // maybe some pasting
335 if (lastbuffer->next() && paste_the_end) {
336 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
337 lastbuffer->pasteParagraph(current_view->buffer()->params);
338 } else if (!lastbuffer->next()->size()) {
339 lastbuffer->next()->makeSameLayout(lastbuffer);
340 lastbuffer->pasteParagraph(current_view->buffer()->params);
341 } else if (!lastbuffer->size()) {
342 lastbuffer->makeSameLayout(lastbuffer->next());
343 lastbuffer->pasteParagraph(current_view->buffer()->params);
345 lastbuffer->next()->stripLeadingSpaces(tc);
347 // restore the simple cut buffer
348 buf = simple_cut_clone;
355 int CutAndPaste::nrOfParagraphs()
361 Paragraph * tmppar = buf;
362 while (tmppar->next()) {
364 tmppar = tmppar->next();
370 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
373 BufferParams const & bparams)
376 if (!par || c1 == c2)
379 ParIterator end = ParIterator();
380 for (ParIterator it = ParIterator(par); it != end; ++it) {
382 string const name = par->layout();
383 LyXTextClass const & tclass = textclasslist[c2];
385 bool hasLayout = tclass.hasLayout(name);
387 string lay = tclass.defaultLayoutName();
391 // not found: use default layout
392 lay = tclass.defaultLayoutName();
396 if (name != par->layout()) {
398 string const s = _("Layout had to be changed from\n")
401 + _("\nbecause of class conversion from\n")
402 + textclasslist[c1].name() + _(" to ")
403 + textclasslist[c2].name();
404 InsetError * new_inset = new InsetError(s);
405 par->insertInset(0, new_inset,
406 LyXFont(LyXFont::ALL_INHERIT,
414 bool CutAndPaste::checkPastePossible(Paragraph *)
416 if (!buf) return false;