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());
137 lyxerr << "deleting pit " << pit << endl;
149 #if 0 // FIXME: why for cut but not copy ?
150 // the cut selection should begin with standard layout
152 buf->params().clear();
154 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
158 if (!startpar->next())
161 Buffer * buffer = current_view->buffer();
164 startpar->next()->stripLeadingSpaces();
167 if (!actually_erased)
170 // paste the paragraphs again, if possible
171 if (startpar->hasSameLayout(startpar->next()) ||
172 startpar->next()->empty()) {
173 mergeParagraph(buffer->params, startpar);
174 // this because endpar gets deleted here!
175 (*endpar) = startpar;
182 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
183 int start, int end, char tc)
185 if (!startpar || (start > startpar->size()))
192 if (!endpar || startpar == endpar) {
193 // only within one paragraph
195 buf->layout(startpar->layout());
197 if (end > startpar->size())
198 end = startpar->size();
199 for (; i < end; ++i) {
200 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
201 buf->insertFromMinibuffer(buf->size());
204 // copy more than one paragraph
205 // clone the paragraphs within the selection
206 Paragraph * tmppar = startpar;
207 buf = new Paragraph(*tmppar, false);
208 Paragraph * tmppar2 = buf;
209 tmppar2->cleanChanges();
211 while (tmppar != endpar
213 tmppar = tmppar->next();
214 tmppar2->next(new Paragraph(*tmppar, false));
215 tmppar2->next()->previous(tmppar2);
216 tmppar2 = tmppar2->next();
218 tmppar2->cleanChanges();
222 // the buf paragraph is too big
223 pos_type tmpi2 = start;
224 for (; tmpi2; --tmpi2)
227 // now tmppar 2 is too big, delete all after end
229 while (tmppar2->size() > tmpi2) {
230 tmppar2->erase(tmppar2->size() - 1);
232 // this paragraph's are of noone's owner!
235 tmppar->setInsetOwner(0);
236 tmppar = tmppar->next();
243 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
246 if (!checkPastePossible(*par))
249 if (pos > (*par)->size())
250 pos = (*par)->size();
253 // Paragraph * tmpbuf;
254 Paragraph * tmppar = *par;
257 // There are two cases: cutbuffer only one paragraph or many
259 // only within a paragraph
260 Paragraph * tmpbuf = new Paragraph(*buf, false);
262 // Some provisions should be done here for checking
263 // if we are inserting at the beginning of a
264 // paragraph. If there are a space at the beginning
265 // of the text to insert and we are inserting at
266 // the beginning of the paragraph the space should
268 while (buf->size()) {
269 // This is an attempt to fix the
270 // "never insert a space at the
271 // beginning of a paragraph" problem.
272 if (!tmppos && buf->isLineSeparator(0)) {
275 buf->cutIntoMinibuffer(current_view->buffer()->params, 0);
277 if (tmppar->insertFromMinibuffer(tmppos))
283 *endpar = tmppar->next();
290 // make a copy of the simple cut_buffer
291 Paragraph * tmpbuf = buf;
292 Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
293 Paragraph * tmpbuf2 = simple_cut_clone;
295 while (tmpbuf->next()) {
296 tmpbuf = tmpbuf->next();
297 tmpbuf2->next(new Paragraph(*tmpbuf, false));
298 tmpbuf2->next()->previous(tmpbuf2);
299 tmpbuf2 = tmpbuf2->next();
302 // now remove all out of the buffer which is NOT allowed in the
303 // new environment and set also another font if that is required
305 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
306 // temporary set *par as previous of tmpbuf as we might have to realize
308 tmpbuf->previous(*par);
310 // make sure there is no class difference
311 SwitchLayoutsBetweenClasses(textclass, tc, tmpbuf,
312 current_view->buffer()->params);
314 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
317 // if we have a negative jump so that the depth would go below
318 // 0 depth then we have to redo the delta to this new max depth
319 // level so that subsequent paragraphs are aligned correctly to
320 // this paragraph at level 0.
321 if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
323 // set the right depth so that we are not too deep or shallow.
324 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
325 if (tmpbuf->params().depth() > max_depth)
326 tmpbuf->params().depth(max_depth);
327 // only set this from the 2nd on as the 2nd depends for maxDepth
329 if (tmpbuf->previous() != (*par))
330 max_depth = tmpbuf->getMaxDepthAfter();
331 // set the inset owner of this paragraph
332 tmpbuf->setInsetOwner((*par)->inInset());
333 for(pos_type i = 0; i < tmpbuf->size(); ++i) {
334 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
335 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
340 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
342 if (!(*par)->checkInsertChar(f1)) {
344 } else if (f1 != f2) {
345 tmpbuf->setFont(i, f1);
349 tmpbuf = tmpbuf->next();
354 // make the buf exactly the same layout than
355 // the cursor paragraph
356 buf->makeSameLayout(*par);
358 // find the end of the buffer
359 Paragraph * lastbuffer = buf;
360 while (lastbuffer->next())
361 lastbuffer = lastbuffer->next();
363 bool paste_the_end = false;
365 // open the paragraph for inserting the buf
367 if (((*par)->size() > pos) || !(*par)->next()) {
368 breakParagraphConservative(
369 current_view->buffer()->params, *par, pos);
370 paste_the_end = true;
372 // set the end for redoing later
373 *endpar = (*par)->next()->next();
376 lastbuffer->next((*par)->next());
377 (*par)->next()->previous(lastbuffer);
382 if ((*par)->next() == lastbuffer)
385 mergeParagraph(current_view->buffer()->params, *par);
386 // store the new cursor position
388 pos = lastbuffer->size();
389 // maybe some pasting
390 if (lastbuffer->next() && paste_the_end) {
391 if (lastbuffer->next()->hasSameLayout(lastbuffer)) {
392 mergeParagraph(current_view->buffer()->params, lastbuffer);
393 } else if (!lastbuffer->next()->size()) {
394 lastbuffer->next()->makeSameLayout(lastbuffer);
395 mergeParagraph(current_view->buffer()->params, lastbuffer);
396 } else if (!lastbuffer->size()) {
397 lastbuffer->makeSameLayout(lastbuffer->next());
398 mergeParagraph(current_view->buffer()->params, lastbuffer);
400 lastbuffer->next()->stripLeadingSpaces();
402 // restore the simple cut buffer
403 buf = simple_cut_clone;
410 int CutAndPaste::nrOfParagraphs()
416 Paragraph * tmppar = buf;
417 while (tmppar->next()) {
419 tmppar = tmppar->next();
425 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
428 BufferParams const & /*bparams*/)
431 if (!par || c1 == c2)
434 LyXTextClass const & tclass1 = textclasslist[c1];
435 LyXTextClass const & tclass2 = textclasslist[c2];
436 ParIterator end = ParIterator();
437 for (ParIterator it = ParIterator(par); it != end; ++it) {
439 string const name = par->layout()->name();
440 bool hasLayout = tclass2.hasLayout(name);
443 par->layout(tclass2[name]);
445 par->layout(tclass2.defaultLayout());
447 if (!hasLayout && name != tclass1.defaultLayoutName()) {
450 boost::format fmt(_("Layout had to be changed from\n"
452 "because of class conversion from\n"
455 % par->layout()->name()
459 string const s = fmt.str();
461 string const s = _("Layout had to be changed from\n")
463 + par->layout()->name()
464 + _("\nbecause of class conversion from\n")
465 + tclass1.name() + _(" to ")
469 InsetError * new_inset = new InsetError(s);
470 LyXText * txt = current_view->getLyXText();
471 LyXCursor cur = txt->cursor;
472 txt->setCursorIntern(current_view, par, 0);
473 txt->insertInset(current_view, new_inset);
474 txt->fullRebreak(current_view);
475 txt->setCursorIntern(current_view, cur.par(), cur.pos());
483 bool CutAndPaste::checkPastePossible(Paragraph *)
485 if (!buf) return false;