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 "ParagraphParameters.h"
22 #include "lyxcursor.h"
24 #include "iterators.h"
25 #include "lyxtextclasslist.h"
26 #include "undo_funcs.h"
27 #include "paragraph_funcs.h"
30 #include "insets/inseterror.h"
32 #include "BoostFormat.h"
37 using lyx::textclass_type;
39 extern BufferView * current_view;
41 // Jürgen, note that this means that you cannot currently have a list
42 // of selections cut/copied. So IMHO later we should have a
43 // list/vector/deque that we could store
44 // struct selection_item {
46 // LyXTextClassList::size_type textclass;
48 // in and some method of choosing beween them (based on the first few chars
49 // in the selection probably.) This would be a nice feature and quite
50 // easy to implement. (Lgb)
52 // Sure but I just cleaned up this code for now with the same functionality
53 // as before. I also want to add a XClipboard function so that we can copy
54 // text from LyX to some other X-application in the form of ASCII or in the
55 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
56 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
57 // then do a middle mouse button click in the application you want and have
58 // the whole formula there in LaTeX-Code. (Jug)
64 textclass_type textclass = 0;
66 // for now here this should be in another Cut&Paste Class!
67 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
68 // member of the CutAndPaste class and in addition it was private.
69 // Perhaps it even should take a parameter? (Lgb)
88 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
89 int start, int & end, char tc, bool doclear,
92 if (!startpar || (start > startpar->size()))
96 copySelection(startpar, *endpar, start, end, tc);
99 if (!endpar || startpar == *endpar) {
100 if (startpar->erase(start, end)) {
101 // Some chars were erased, go to start to be safe
107 bool actually_erased = false;
109 // clear end/begin fragments of the first/last par in selection
110 actually_erased |= (startpar)->erase(start, startpar->size());
111 if ((*endpar)->erase(0, end)) {
112 actually_erased = true;
116 // Loop through the deleted pars if any, erasing as needed
118 Paragraph * pit = startpar->next();
125 Paragraph * next = pit->next();
127 // "erase" the contents of the par
128 if (pit != *endpar) {
129 actually_erased |= pit->erase(0, pit->size());
131 // remove the par if it's now empty
132 if (actually_erased) {
133 pit->previous()->next(pit->next());
135 next->previous(pit->previous());
148 #if 0 // FIXME: why for cut but not copy ?
149 // the cut selection should begin with standard layout
151 buf->params().clear();
153 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
157 if (!startpar->next())
160 Buffer * buffer = current_view->buffer();
163 startpar->next()->stripLeadingSpaces();
166 if (!actually_erased)
169 // paste the paragraphs again, if possible
170 if (startpar->hasSameLayout(startpar->next()) ||
171 startpar->next()->empty()) {
172 mergeParagraph(buffer->params, startpar);
173 // this because endpar gets deleted here!
174 (*endpar) = startpar;
181 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
182 int start, int end, char tc)
184 if (!startpar || (start > startpar->size()))
191 if (!endpar || startpar == endpar) {
192 // only within one paragraph
194 buf->layout(startpar->layout());
196 if (end > startpar->size())
197 end = startpar->size();
198 for (; i < end; ++i) {
199 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
200 buf->insertFromMinibuffer(buf->size());
203 // copy more than one paragraph
204 // clone the paragraphs within the selection
205 Paragraph * tmppar = startpar;
206 buf = new Paragraph(*tmppar, false);
207 Paragraph * tmppar2 = buf;
208 tmppar2->cleanChanges();
210 while (tmppar != endpar
212 tmppar = tmppar->next();
213 tmppar2->next(new Paragraph(*tmppar, false));
214 tmppar2->next()->previous(tmppar2);
215 tmppar2 = tmppar2->next();
217 tmppar2->cleanChanges();
221 // the buf paragraph is too big
222 pos_type tmpi2 = start;
223 for (; tmpi2; --tmpi2)
226 // now tmppar 2 is too big, delete all after end
228 while (tmppar2->size() > tmpi2) {
229 tmppar2->erase(tmppar2->size() - 1);
231 // this paragraph's are of noone's owner!
234 tmppar->setInsetOwner(0);
235 tmppar = tmppar->next();
242 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
245 if (!checkPastePossible(*par))
248 if (pos > (*par)->size())
249 pos = (*par)->size();
252 // Paragraph * tmpbuf;
253 Paragraph * tmppar = *par;
256 // There are two cases: cutbuffer only one paragraph or many
258 // only within a paragraph
259 Paragraph * tmpbuf = new Paragraph(*buf, false);
261 // Some provisions should be done here for checking
262 // if we are inserting at the beginning of a
263 // paragraph. If there are a space at the beginning
264 // of the text to insert and we are inserting at
265 // the beginning of the paragraph the space should
267 while (buf->size()) {
268 // This is an attempt to fix the
269 // "never insert a space at the
270 // beginning of a paragraph" problem.
271 if (!tmppos && buf->isLineSeparator(0)) {
274 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
276 if (tmppar->insertFromMinibuffer(tmppos))
282 *endpar = tmppar->next();
289 // make a copy of the simple cut_buffer
290 Paragraph * tmpbuf = buf;
291 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
292 Paragraph * tmpbuf2 = simple_cut_clone;
294 while (tmpbuf->next()) {
295 tmpbuf = tmpbuf->next();
296 tmpbuf2->next(new Paragraph(*tmpbuf, false));
297 tmpbuf2->next()->previous(tmpbuf2);
298 tmpbuf2 = tmpbuf2->next();
301 // now remove all out of the buffer which is NOT allowed in the
302 // new environment and set also another font if that is required
304 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
305 // temporary set *par as previous of tmpbuf as we might have to realize
307 tmpbuf->previous(*par);
309 // make sure there is no class difference
310 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
311 current_view->buffer()->params);
313 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
316 // if we have a negative jump so that the depth would go below
317 // 0 depth then we have to redo the delta to this new max depth
318 // level so that subsequent paragraphs are aligned correctly to
319 // this paragraph at level 0.
320 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
322 // set the right depth so that we are not too deep or shallow.
323 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
324 if (tmpbuf->params().depth() > max_depth)
325 tmpbuf->params().depth(max_depth);
326 // only set this from the 2nd on as the 2nd depends for maxDepth
328 if (tmpbuf->previous() != (*par))
329 max_depth = tmpbuf->getMaxDepthAfter();
330 // set the inset owner of this paragraph
331 tmpbuf->setInsetOwner((*par)->inInset());
332 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
333 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
334 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
339 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
341 if (!(*par)->checkInsertChar(f1)) {
343 } else if (f1 != f2) {
344 tmpbuf->setFont(i, f1);
348 tmpbuf = tmpbuf->next();
353 // make the buf exactly the same layout than
354 // the cursor paragraph
355 buf->makeSameLayout(*par);
357 // find the end of the buffer
358 Paragraph * lastbuffer = buf;
359 while (lastbuffer->next())
360 lastbuffer = lastbuffer->next();
362 bool paste_the_end = false;
364 // open the paragraph for inserting the buf
366 if (((*par)->size() > pos) || !(*par)->next()) {
367 breakParagraphConservative(
368 current_view->buffer()->params, *par, pos);
369 paste_the_end = true;
371 // set the end for redoing later
372 *endpar = (*par)->next()->next();
375 lastbuffer->next((*par)->next());
376 (*par)->next()->previous(lastbuffer);
381 if ((*par)->next() == lastbuffer)
384 mergeParagraph(current_view->buffer()->params, *par);
385 // store the new cursor position
387 pos = lastbuffer->size();
388 // maybe some pasting
389 if (lastbuffer->next() && paste_the_end) {
390 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
391 mergeParagraph(current_view->buffer()->params, lastbuffer);
392 } else if (!lastbuffer->next()->size()) {
393 lastbuffer->next()->makeSameLayout(lastbuffer);
394 mergeParagraph(current_view->buffer()->params, lastbuffer);
395 } else if (!lastbuffer->size()) {
396 lastbuffer->makeSameLayout(lastbuffer->next());
397 mergeParagraph(current_view->buffer()->params, lastbuffer);
399 lastbuffer->next()->stripLeadingSpaces();
401 // restore the simple cut buffer
402 buf = simple_cut_clone;
409 int CutAndPaste::nrOfParagraphs()
415 Paragraph * tmppar = buf;
416 while (tmppar->next()) {
418 tmppar = tmppar->next();
424 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
427 BufferParams const & /*bparams*/)
430 if (!par || c1 == c2)
433 LyXTextClass const & tclass1 = textclasslist[c1];
434 LyXTextClass const & tclass2 = textclasslist[c2];
435 ParIterator end = ParIterator();
436 for (ParIterator it = ParIterator(par); it != end; ++it) {
438 string const name = par->layout()->name();
439 bool hasLayout = tclass2.hasLayout(name);
442 par->layout(tclass2[name]);
444 par->layout(tclass2.defaultLayout());
446 if (!hasLayout && name != tclass1.defaultLayoutName()) {
449 boost::format fmt(_("Layout had to be changed from\n"
451 "because of class conversion from\n"
454 % par->layout()->name()
458 string const s = fmt.str();
460 string const s = _("Layout had to be changed from\n")
462 + par->layout()->name()
463 + _("\nbecause of class conversion from\n")
464 + tclass1.name() + _(" to ")
468 InsetError * new_inset = new InsetError(s);
469 LyXText * txt = current_view->getLyXText();
470 LyXCursor cur = txt->cursor;
471 txt->setCursorIntern(current_view, par, 0);
472 txt->insertInset(current_view, new_inset);
473 txt->fullRebreak(current_view);
474 txt->setCursorIntern(current_view, cur.par(), cur.pos());
482 bool CutAndPaste::checkPastePossible(Paragraph *)
484 if (!buf) return false;