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 "support/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)
59 ParagraphList paragraphs;
60 textclass_type textclass = 0;
65 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph * endpar,
66 int start, int & end, textclass_type tc,
67 bool doclear, bool realcut)
69 if (!startpar || (start > startpar->size()))
73 copySelection(startpar, endpar, start, end, tc);
76 if (!endpar || startpar == endpar) {
77 if (startpar->erase(start, end)) {
78 // Some chars were erased, go to start to be safe
84 bool actually_erased = false;
86 // clear end/begin fragments of the first/last par in selection
87 actually_erased |= (startpar)->erase(start, startpar->size());
88 if (endpar->erase(0, end)) {
89 actually_erased = true;
93 // Loop through the deleted pars if any, erasing as needed
95 Paragraph * pit = startpar->next();
102 Paragraph * next = pit->next();
104 // "erase" the contents of the par
106 actually_erased |= pit->erase(0, pit->size());
108 // remove the par if it's now empty
109 if (actually_erased) {
110 pit->previous()->next(pit->next());
112 next->previous(pit->previous());
125 #if 0 // FIXME: why for cut but not copy ?
126 // the cut selection should begin with standard layout
128 buf->params().clear();
130 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
134 if (!startpar->next())
137 Buffer * buffer = current_view->buffer();
140 startpar->next()->stripLeadingSpaces();
143 if (!actually_erased)
146 // paste the paragraphs again, if possible
147 if (startpar->hasSameLayout(startpar->next()) ||
148 startpar->next()->empty()) {
149 #warning This is suspect. (Lgb)
150 // When doing this merge we must know if the par really
151 // belongs to an inset, and if it does then we have to use
152 // the insets paragraphs, and not the buffers. (Lgb)
153 mergeParagraph(buffer->params, buffer->paragraphs, startpar);
154 // this because endpar gets deleted here!
162 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
163 int start, int end, textclass_type tc)
165 if (!startpar || (start > startpar->size()))
172 if (!endpar || startpar == endpar) {
173 // only within one paragraph
174 ParagraphList::iterator buf =
175 paragraphs.insert(paragraphs.begin(), new Paragraph);
177 buf->layout(startpar->layout());
179 if (end > startpar->size())
180 end = startpar->size();
181 for (; i < end; ++i) {
182 startpar->copyIntoMinibuffer(*current_view->buffer(), i);
183 buf->insertFromMinibuffer(buf->size());
186 // copy more than one paragraph
187 // clone the paragraphs within the selection
188 Paragraph * tmppar = startpar;
190 while (tmppar != endpar) {
191 Paragraph * newpar = new Paragraph(*tmppar, false);
193 newpar->cleanChanges();
194 newpar->setInsetOwner(0);
196 paragraphs.push_back(newpar);
197 tmppar = tmppar->next();
200 // The first paragraph is too big.
201 Paragraph & front = paragraphs.front();
202 pos_type tmpi2 = start;
203 for (; tmpi2; --tmpi2)
206 // Now last paragraph is too big, delete all after end.
207 Paragraph & back = paragraphs.back();
209 while (back.size() > tmpi2) {
210 back.erase(back.size() - 1);
217 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
218 int & pos, textclass_type tc)
220 if (!checkPastePossible())
223 if (pos > (*par)->size())
224 pos = (*par)->size();
228 // make a copy of the simple cut_buffer
230 ParagraphList::iterator it = paragraphs.begin();
232 ParagraphList simple_cut_clone;
233 simple_cut_clone.insert(simple_cut_clone.begin(),
234 new Paragraph(*it, false));
236 ParagraphList::iterator end = paragraphs.end();
237 while (boost::next(it) != end) {
239 simple_cut_clone.insert(simple_cut_clone.end(),
240 new Paragraph(*it, false));
243 // Later we want it done like this:
244 ParagraphList simple_cut_clone(paragraphs.begin(),
247 // now remove all out of the buffer which is NOT allowed in the
248 // new environment and set also another font if that is required
249 ParagraphList::iterator tmpbuf = paragraphs.begin();
250 int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
251 // Temporary set *par as previous of tmpbuf as we might have
252 // to realize the font.
253 tmpbuf->previous(*par);
255 // make sure there is no class difference
256 SwitchLayoutsBetweenClasses(textclass, tc, &*tmpbuf,
257 current_view->buffer()->params);
259 Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter();
261 while (tmpbuf != paragraphs.end()) {
262 // If we have a negative jump so that the depth would
263 // go below 0 depth then we have to redo the delta to
264 // this new max depth level so that subsequent
265 // paragraphs are aligned correctly to this paragraph
267 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
269 // set the right depth so that we are not too deep or shallow.
270 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
271 if (tmpbuf->params().depth() > max_depth)
272 tmpbuf->params().depth(max_depth);
273 // only set this from the 2nd on as the 2nd depends for maxDepth
275 if (tmpbuf->previous() != (*par))
276 max_depth = tmpbuf->getMaxDepthAfter();
277 // set the inset owner of this paragraph
278 tmpbuf->setInsetOwner((*par)->inInset());
279 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
280 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
281 if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
285 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
287 if (!(*par)->checkInsertChar(f1)) {
289 } else if (f1 != f2) {
290 tmpbuf->setFont(i, f1);
294 tmpbuf = tmpbuf->next();
298 paragraphs.begin()->previous(0);
300 // make the buf exactly the same layout than
301 // the cursor paragraph
302 paragraphs.begin()->makeSameLayout(*par);
304 // find the end of the buffer
305 ParagraphList::iterator lastbuffer = paragraphs.begin();
306 while (boost::next(lastbuffer) != paragraphs.end())
309 bool paste_the_end = false;
311 // open the paragraph for inserting the buf
313 if (((*par)->size() > pos) || !(*par)->next()) {
314 breakParagraphConservative(
315 current_view->buffer()->params, current_view->buffer()->paragraphs, *par, pos);
316 paste_the_end = true;
318 // set the end for redoing later
319 *endpar = (*par)->next()->next();
322 lastbuffer->next((*par)->next());
323 (*par)->next()->previous(&*lastbuffer);
325 (*par)->next(&*paragraphs.begin());
326 paragraphs.begin()->previous(*par);
328 if ((*par)->next() == lastbuffer)
331 mergeParagraph(current_view->buffer()->params,
332 current_view->buffer()->paragraphs, *par);
333 // store the new cursor position
335 pos = lastbuffer->size();
336 // maybe some pasting
337 if (lastbuffer->next() && paste_the_end) {
338 if (lastbuffer->next()->hasSameLayout(&*lastbuffer)) {
339 mergeParagraph(current_view->buffer()->params,
340 current_view->buffer()->paragraphs, lastbuffer);
341 } else if (!lastbuffer->next()->size()) {
342 lastbuffer->next()->makeSameLayout(&*lastbuffer);
343 mergeParagraph(current_view->buffer()->params, current_view->buffer()->paragraphs, lastbuffer);
344 } else if (!lastbuffer->size()) {
345 lastbuffer->makeSameLayout(lastbuffer->next());
346 mergeParagraph(current_view->buffer()->params,
347 current_view->buffer()->paragraphs, lastbuffer);
349 lastbuffer->next()->stripLeadingSpaces();
351 // restore the simple cut buffer
352 paragraphs = simple_cut_clone;
358 int CutAndPaste::nrOfParagraphs()
360 return paragraphs.size();
364 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
367 BufferParams const & /*bparams*/)
370 if (!par || c1 == c2)
373 LyXTextClass const & tclass1 = textclasslist[c1];
374 LyXTextClass const & tclass2 = textclasslist[c2];
375 ParIterator end = ParIterator();
376 for (ParIterator it = ParIterator(par); it != end; ++it) {
378 string const name = par->layout()->name();
379 bool hasLayout = tclass2.hasLayout(name);
382 par->layout(tclass2[name]);
384 par->layout(tclass2.defaultLayout());
386 if (!hasLayout && name != tclass1.defaultLayoutName()) {
389 boost::format fmt(_("Layout had to be changed from\n"
391 "because of class conversion from\n"
394 % par->layout()->name()
398 string const s = fmt.str();
400 string const s = _("Layout had to be changed from\n")
402 + par->layout()->name()
403 + _("\nbecause of class conversion from\n")
404 + tclass1.name() + _(" to ")
408 InsetError * new_inset = new InsetError(s);
409 LyXText * txt = current_view->getLyXText();
410 LyXCursor cur = txt->cursor;
411 txt->setCursorIntern(par, 0);
412 txt->insertInset(new_inset);
414 txt->setCursorIntern(cur.par(), cur.pos());
422 bool CutAndPaste::checkPastePossible()
424 return !paragraphs.empty();