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"
29 #include "support/LAssert.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)
63 ParagraphList paragraphs;
64 textclass_type textclass = 0;
68 PitPosPair CutAndPaste::cutSelection(ParagraphList & pars,
69 ParagraphList::iterator startpit,
70 ParagraphList::iterator endpit,
71 int startpos, int endpos,
72 textclass_type tc, bool doclear)
74 copySelection(startpit, endpit, startpos, endpos, tc);
75 return eraseSelection(pars, startpit, endpit, startpos,
80 PitPosPair CutAndPaste::eraseSelection(ParagraphList & pars,
81 ParagraphList::iterator startpit,
82 ParagraphList::iterator endpit,
83 int startpos, int endpos, bool doclear)
85 if (startpit == pars.end() || (startpos > startpit->size()))
86 return PitPosPair(endpit, endpos);
88 if (endpit == pars.end() || startpit == endpit) {
89 endpos -= startpit->erase(startpos, endpos);
90 return PitPosPair(endpit, endpos);
93 // clear end/begin fragments of the first/last par in selection
94 bool all_erased = true;
96 startpit->erase(startpos, startpit->size());
97 if (startpit->size() != startpos)
100 endpos -= endpit->erase(0, endpos);
104 // Loop through the deleted pars if any, erasing as needed
106 ParagraphList::iterator pit = boost::next(startpit);
108 while (pit != endpit && pit != pars.end()) {
109 ParagraphList::iterator const next = boost::next(pit);
110 // "erase" the contents of the par
111 pit->erase(0, pit->size());
113 // remove the par if it's now empty
120 #if 0 // FIXME: why for cut but not copy ?
121 // the cut selection should begin with standard layout
123 buf->params().clear();
125 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
129 if (boost::next(startpit) == pars.end())
130 return PitPosPair(endpit, endpos);
133 boost::next(startpit)->stripLeadingSpaces();
136 // paste the paragraphs again, if possible
138 (startpit->hasSameLayout(*boost::next(startpit)) ||
139 boost::next(startpit)->empty())) {
140 #warning current_view used here.
141 // should we pass buffer or buffer->params around?
142 Buffer * buffer = current_view->buffer();
143 mergeParagraph(buffer->params, pars, &*startpit);
144 // this because endpar gets deleted here!
149 return PitPosPair(endpit, endpos);
156 struct resetOwnerAndChanges {
157 void operator()(Paragraph & p) {
165 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
166 ParagraphList::iterator endpit,
167 int start, int end, textclass_type tc)
169 lyx::Assert(&*startpit);
170 lyx::Assert(&*endpit);
171 lyx::Assert(0 <= start && start <= startpit->size());
172 lyx::Assert(0 <= end && end <= endpit->size());
173 lyx::Assert(startpit != endpit || start <= end);
177 // Clone the paragraphs within the selection.
178 ParagraphList::iterator postend = boost::next(endpit);
180 paragraphs.assign(startpit, postend);
181 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
183 // Cut out the end of the last paragraph.
184 Paragraph & back = paragraphs.back();
185 back.erase(end, back.size());
187 // Cut out the begin of the first paragraph
188 Paragraph & front = paragraphs.front();
189 front.erase(0, start);
195 pair<PitPosPair, ParagraphList::iterator>
196 CutAndPaste::pasteSelection(ParagraphList & pars,
197 ParagraphList::iterator pit, int pos,
200 if (!checkPastePossible())
201 return make_pair(PitPosPair(pit, pos), pit);
203 lyx::Assert (pos <= pit->size());
205 // make a copy of the simple cut_buffer
207 ParagraphList simple_cut_clone;
208 ParagraphList::iterator it = paragraphs.begin();
209 ParagraphList::iterator end = paragraphs.end();
210 for (; it != end; ++it) {
211 simple_cut_clone.push_back(new Paragraph(*it, false));
214 // Later we want it done like this:
215 ParagraphList simple_cut_clone = paragraphs;
217 // now remove all out of the buffer which is NOT allowed in the
218 // new environment and set also another font if that is required
219 ParagraphList::iterator tmpbuf = paragraphs.begin();
220 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
222 // make sure there is no class difference
223 #warning current_view used here
224 SwitchLayoutsBetweenClasses(textclass, tc, &*tmpbuf,
225 current_view->buffer()->params);
227 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
229 for (; tmpbuf != paragraphs.end(); ++tmpbuf) {
230 // If we have a negative jump so that the depth would
231 // go below 0 depth then we have to redo the delta to
232 // this new max depth level so that subsequent
233 // paragraphs are aligned correctly to this paragraph
235 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
237 // set the right depth so that we are not too deep or shallow.
238 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
239 if (tmpbuf->params().depth() > max_depth)
240 tmpbuf->params().depth(max_depth);
241 // only set this from the 2nd on as the 2nd depends for maxDepth
243 if (tmpbuf->previous() != pit)
244 max_depth = tmpbuf->getMaxDepthAfter();
245 // set the inset owner of this paragraph
246 tmpbuf->setInsetOwner(pit->inInset());
247 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
248 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
249 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
254 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params, i, outerFont(tmpbuf, pars));
256 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params, i, outerFont(pit, pars));
259 if (!pit->checkInsertChar(f1)) {
261 } else if (f1 != f2) {
262 tmpbuf->setFont(i, f1);
268 // make the buf exactly the same layout than
269 // the cursor paragraph
270 paragraphs.begin()->makeSameLayout(*pit);
272 // find the end of the buffer
273 // FIXME: change this to end() - 1
274 ParagraphList::iterator lastbuffer = paragraphs.begin();
275 while (boost::next(lastbuffer) != paragraphs.end())
278 bool paste_the_end = false;
280 // open the paragraph for inserting the buf
282 if (pit->size() > pos || !pit->next()) {
283 breakParagraphConservative(current_view->buffer()->params,
285 paste_the_end = true;
287 // set the end for redoing later
288 ParagraphList::iterator endpit = boost::next(boost::next(pit));
291 lastbuffer->next(pit->next());
292 pit->next()->previous(&*lastbuffer);
294 pit->next(&*paragraphs.begin());
295 paragraphs.begin()->previous(&*pit);
297 if (boost::next(pit) == lastbuffer)
300 mergeParagraph(current_view->buffer()->params, pars, pit);
301 // store the new cursor position
303 pos = lastbuffer->size();
304 // maybe some pasting
305 if (boost::next(lastbuffer) != paragraphs.end() && paste_the_end) {
306 if (boost::next(lastbuffer)->hasSameLayout(*lastbuffer)) {
307 mergeParagraph(current_view->buffer()->params, pars,
309 } else if (!boost::next(lastbuffer)->size()) {
310 boost::next(lastbuffer)->makeSameLayout(*lastbuffer);
311 mergeParagraph(current_view->buffer()->params, pars,
313 } else if (!lastbuffer->size()) {
314 lastbuffer->makeSameLayout(*boost::next(lastbuffer));
315 mergeParagraph(current_view->buffer()->params, pars,
318 boost::next(lastbuffer)->stripLeadingSpaces();
320 // restore the simple cut buffer
321 paragraphs = simple_cut_clone;
323 return make_pair(PitPosPair(pit, pos), endpit);
327 int CutAndPaste::nrOfParagraphs()
329 return paragraphs.size();
333 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
336 BufferParams const & /*bparams*/)
339 if (!par || c1 == c2)
342 LyXTextClass const & tclass1 = textclasslist[c1];
343 LyXTextClass const & tclass2 = textclasslist[c2];
344 ParIterator end = ParIterator();
345 for (ParIterator it = ParIterator(par); it != end; ++it) {
347 string const name = par->layout()->name();
348 bool hasLayout = tclass2.hasLayout(name);
351 par->layout(tclass2[name]);
353 par->layout(tclass2.defaultLayout());
355 if (!hasLayout && name != tclass1.defaultLayoutName()) {
358 boost::format fmt(_("Layout had to be changed from\n"
360 "because of class conversion from\n"
363 % par->layout()->name()
367 string const s = fmt.str();
369 string const s = _("Layout had to be changed from\n")
371 + par->layout()->name()
372 + _("\nbecause of class conversion from\n")
373 + tclass1.name() + _(" to ")
377 InsetError * new_inset = new InsetError(s);
378 LyXText * txt = current_view->getLyXText();
379 LyXCursor cur = txt->cursor;
380 txt->setCursorIntern(par, 0);
381 txt->insertInset(new_inset);
383 txt->setCursorIntern(cur.par(), cur.pos());
391 bool CutAndPaste::checkPastePossible()
393 return !paragraphs.empty();