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 "ParagraphParameters.h"
18 #include "lyxcursor.h"
20 #include "iterators.h"
21 #include "lyxtextclasslist.h"
22 #include "undo_funcs.h"
23 #include "paragraph_funcs.h"
26 #include "insets/inseterror.h"
28 #include "BoostFormat.h"
33 using lyx::textclass_type;
35 extern BufferView * current_view;
37 // Jürgen, note that this means that you cannot currently have a list
38 // of selections cut/copied. So IMHO later we should have a
39 // list/vector/deque that we could store
40 // struct selection_item {
42 // LyXTextClassList::size_type textclass;
44 // in and some method of choosing beween them (based on the first few chars
45 // in the selection probably.) This would be a nice feature and quite
46 // easy to implement. (Lgb)
48 // Sure but I just cleaned up this code for now with the same functionality
49 // as before. I also want to add a XClipboard function so that we can copy
50 // text from LyX to some other X-application in the form of ASCII or in the
51 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
52 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
53 // then do a middle mouse button click in the application you want and have
54 // the whole formula there in LaTeX-Code. (Jug)
60 textclass_type textclass = 0;
62 // for now here this should be in another Cut&Paste Class!
63 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
64 // member of the CutAndPaste class and in addition it was private.
65 // Perhaps it even should take a parameter? (Lgb)
84 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
85 int start, int & end, char tc, bool doclear,
88 if (!startpar || (start > startpar->size()))
92 copySelection(startpar, *endpar, start, end, tc);
95 if (!endpar || startpar == *endpar) {
96 if (startpar->erase(start, end)) {
97 // Some chars were erased, go to start to be safe
103 bool actually_erased = false;
105 // clear end/begin fragments of the first/last par in selection
106 actually_erased |= (startpar)->erase(start, startpar->size());
107 if ((*endpar)->erase(0, end)) {
108 actually_erased = true;
112 // Loop through the deleted pars if any, erasing as needed
114 Paragraph * pit = startpar->next();
121 Paragraph * next = pit->next();
123 // "erase" the contents of the par
124 if (pit != *endpar) {
125 actually_erased |= pit->erase(0, pit->size());
127 // remove the par if it's now empty
128 if (actually_erased) {
129 pit->previous()->next(pit->next());
131 next->previous(pit->previous());
144 #if 0 // FIXME: why for cut but not copy ?
145 // the cut selection should begin with standard layout
147 buf->params().clear();
149 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
153 if (!startpar->next())
156 Buffer * buffer = current_view->buffer();
159 startpar->next()->stripLeadingSpaces();
162 if (!actually_erased)
165 // paste the paragraphs again, if possible
166 if (startpar->hasSameLayout(startpar->next()) ||
167 startpar->next()->empty()) {
168 mergeParagraph(buffer->params, startpar);
169 // this because endpar gets deleted here!
170 (*endpar) = startpar;
177 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
178 int start, int end, char tc)
180 if (!startpar || (start > startpar->size()))
187 if (!endpar || startpar == endpar) {
188 // only within one paragraph
190 buf->layout(startpar->layout());
192 if (end > startpar->size())
193 end = startpar->size();
194 for (; i < end; ++i) {
195 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
196 buf->insertFromMinibuffer(buf->size());
199 // copy more than one paragraph
200 // clone the paragraphs within the selection
201 Paragraph * tmppar = startpar;
202 buf = new Paragraph(*tmppar, false);
203 Paragraph * tmppar2 = buf;
204 tmppar2->cleanChanges();
206 while (tmppar != endpar
208 tmppar = tmppar->next();
209 tmppar2->next(new Paragraph(*tmppar, false));
210 tmppar2->next()->previous(tmppar2);
211 tmppar2 = tmppar2->next();
213 tmppar2->cleanChanges();
217 // the buf paragraph is too big
218 pos_type tmpi2 = start;
219 for (; tmpi2; --tmpi2)
222 // now tmppar 2 is too big, delete all after end
224 while (tmppar2->size() > tmpi2) {
225 tmppar2->erase(tmppar2->size() - 1);
227 // this paragraph's are of noone's owner!
230 tmppar->setInsetOwner(0);
231 tmppar = tmppar->next();
238 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
241 if (!checkPastePossible(*par))
244 if (pos > (*par)->size())
245 pos = (*par)->size();
248 // Paragraph * tmpbuf;
249 Paragraph * tmppar = *par;
252 // There are two cases: cutbuffer only one paragraph or many
254 // only within a paragraph
255 Paragraph * tmpbuf = new Paragraph(*buf, false);
257 // Some provisions should be done here for checking
258 // if we are inserting at the beginning of a
259 // paragraph. If there are a space at the beginning
260 // of the text to insert and we are inserting at
261 // the beginning of the paragraph the space should
263 while (buf->size()) {
264 // This is an attempt to fix the
265 // "never insert a space at the
266 // beginning of a paragraph" problem.
267 if (!tmppos && buf->isLineSeparator(0)) {
270 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
272 if (tmppar->insertFromMinibuffer(tmppos))
278 *endpar = tmppar->next();
285 // make a copy of the simple cut_buffer
286 Paragraph * tmpbuf = buf;
287 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
288 Paragraph * tmpbuf2 = simple_cut_clone;
290 while (tmpbuf->next()) {
291 tmpbuf = tmpbuf->next();
292 tmpbuf2->next(new Paragraph(*tmpbuf, false));
293 tmpbuf2->next()->previous(tmpbuf2);
294 tmpbuf2 = tmpbuf2->next();
297 // now remove all out of the buffer which is NOT allowed in the
298 // new environment and set also another font if that is required
300 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
301 // temporary set *par as previous of tmpbuf as we might have to realize
303 tmpbuf->previous(*par);
305 // make sure there is no class difference
306 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
307 current_view->buffer()->params);
309 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
312 // if we have a negative jump so that the depth would go below
313 // 0 depth then we have to redo the delta to this new max depth
314 // level so that subsequent paragraphs are aligned correctly to
315 // this paragraph at level 0.
316 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
318 // set the right depth so that we are not too deep or shallow.
319 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
320 if (tmpbuf->params().depth() > max_depth)
321 tmpbuf->params().depth(max_depth);
322 // only set this from the 2nd on as the 2nd depends for maxDepth
324 if (tmpbuf->previous() != (*par))
325 max_depth = tmpbuf->getMaxDepthAfter();
326 // set the inset owner of this paragraph
327 tmpbuf->setInsetOwner((*par)->inInset());
328 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
329 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
330 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
335 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
337 if (!(*par)->checkInsertChar(f1)) {
339 } else if (f1 != f2) {
340 tmpbuf->setFont(i, f1);
344 tmpbuf = tmpbuf->next();
349 // make the buf exactly the same layout than
350 // the cursor paragraph
351 buf->makeSameLayout(*par);
353 // find the end of the buffer
354 Paragraph * lastbuffer = buf;
355 while (lastbuffer->next())
356 lastbuffer = lastbuffer->next();
358 bool paste_the_end = false;
360 // open the paragraph for inserting the buf
362 if (((*par)->size() > pos) || !(*par)->next()) {
363 breakParagraphConservative(
364 current_view->buffer()->params, *par, pos);
365 paste_the_end = true;
367 // set the end for redoing later
368 *endpar = (*par)->next()->next();
371 lastbuffer->next((*par)->next());
372 (*par)->next()->previous(lastbuffer);
377 if ((*par)->next() == lastbuffer)
380 mergeParagraph(current_view->buffer()->params, *par);
381 // store the new cursor position
383 pos = lastbuffer->size();
384 // maybe some pasting
385 if (lastbuffer->next() && paste_the_end) {
386 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
387 mergeParagraph(current_view->buffer()->params, lastbuffer);
388 } else if (!lastbuffer->next()->size()) {
389 lastbuffer->next()->makeSameLayout(lastbuffer);
390 mergeParagraph(current_view->buffer()->params, lastbuffer);
391 } else if (!lastbuffer->size()) {
392 lastbuffer->makeSameLayout(lastbuffer->next());
393 mergeParagraph(current_view->buffer()->params, lastbuffer);
395 lastbuffer->next()->stripLeadingSpaces();
397 // restore the simple cut buffer
398 buf = simple_cut_clone;
405 int CutAndPaste::nrOfParagraphs()
411 Paragraph * tmppar = buf;
412 while (tmppar->next()) {
414 tmppar = tmppar->next();
420 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
423 BufferParams const & /*bparams*/)
426 if (!par || c1 == c2)
429 LyXTextClass const & tclass1 = textclasslist[c1];
430 LyXTextClass const & tclass2 = textclasslist[c2];
431 ParIterator end = ParIterator();
432 for (ParIterator it = ParIterator(par); it != end; ++it) {
434 string const name = par->layout()->name();
435 bool hasLayout = tclass2.hasLayout(name);
438 par->layout(tclass2[name]);
440 par->layout(tclass2.defaultLayout());
442 if (!hasLayout && name != tclass1.defaultLayoutName()) {
445 boost::format fmt(_("Layout had to be changed from\n"
447 "because of class conversion from\n"
450 % par->layout()->name()
454 string const s = fmt.str();
456 string const s = _("Layout had to be changed from\n")
458 + par->layout()->name()
459 + _("\nbecause of class conversion from\n")
460 + tclass1.name() + _(" to ")
464 InsetError * new_inset = new InsetError(s);
465 LyXText * txt = current_view->getLyXText();
466 LyXCursor cur = txt->cursor;
467 txt->setCursorIntern(current_view, par, 0);
468 txt->insertInset(current_view, new_inset);
469 txt->fullRebreak(current_view);
470 txt->setCursorIntern(current_view, cur.par(), cur.pos());
478 bool CutAndPaste::checkPastePossible(Paragraph *)
480 if (!buf) return false;