2 * This file is part of LyX, the document processor.
3 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "CutAndPaste.h"
14 #include "BufferView.h"
16 #include "errorlist.h"
17 #include "paragraph.h"
18 #include "ParagraphParameters.h"
20 #include "lyxcursor.h"
21 #include "iterators.h"
22 #include "lyxtextclasslist.h"
23 #include "undo_funcs.h"
25 #include "paragraph_funcs.h"
28 #include "insets/inseterror.h"
30 #include "support/LAssert.h"
31 #include "support/lstrings.h"
32 #include "support/limited_stack.h"
40 using lyx::textclass_type;
42 extern BufferView * current_view;
44 // Jürgen, note that this means that you cannot currently have a list
45 // of selections cut/copied. So IMHO later we should have a
46 // list/vector/deque that we could store
47 // struct selection_item {
48 // ParagraphList copy_pars;
49 // LyXTextClassList::size_type textclass;
51 // in and some method of choosing beween them (based on the first few chars
52 // in the selection probably.) This would be a nice feature and quite
53 // easy to implement. (Lgb)
55 // Sure but I just cleaned up this code for now with the same functionality
56 // as before. I also want to add a XClipboard function so that we can copy
57 // text from LyX to some other X-application in the form of ASCII or in the
58 // form of LaTeX (or Docbook depending on the document-class!). Think how nice
59 // it could be to select a math-inset do a "Copy to X-Clipboard as LaTeX" and
60 // then do a middle mouse button click in the application you want and have
61 // the whole formula there in LaTeX-Code. (Jug)
65 limited_stack<pair<ParagraphList, textclass_type> > cuts(10);
69 PitPosPair CutAndPaste::cutSelection(ParagraphList & pars,
70 ParagraphList::iterator startpit,
71 ParagraphList::iterator endpit,
72 int startpos, int endpos,
73 textclass_type tc, bool doclear)
75 copySelection(startpit, endpit, startpos, endpos, tc);
76 return eraseSelection(pars, startpit, endpit, startpos,
81 PitPosPair CutAndPaste::eraseSelection(ParagraphList & pars,
82 ParagraphList::iterator startpit,
83 ParagraphList::iterator endpit,
84 int startpos, int endpos, bool doclear)
86 if (startpit == pars.end() || (startpos > startpit->size()))
87 return PitPosPair(endpit, endpos);
89 if (endpit == pars.end() || startpit == endpit) {
90 endpos -= startpit->erase(startpos, endpos);
91 return PitPosPair(endpit, endpos);
94 // clear end/begin fragments of the first/last par in selection
95 bool all_erased = true;
97 startpit->erase(startpos, startpit->size());
98 if (startpit->size() != startpos)
101 endpos -= endpit->erase(0, endpos);
105 // Loop through the deleted pars if any, erasing as needed
107 ParagraphList::iterator pit = boost::next(startpit);
109 while (pit != endpit && pit != pars.end()) {
110 ParagraphList::iterator const next = boost::next(pit);
111 // "erase" the contents of the par
112 pit->erase(0, pit->size());
114 // remove the par if it's now empty
121 #if 0 // FIXME: why for cut but not copy ?
122 // the cut selection should begin with standard layout
124 buf->params().clear();
126 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
130 if (boost::next(startpit) == pars.end())
131 return PitPosPair(endpit, endpos);
134 boost::next(startpit)->stripLeadingSpaces();
137 // paste the paragraphs again, if possible
139 (startpit->hasSameLayout(*boost::next(startpit)) ||
140 boost::next(startpit)->empty())) {
141 #warning current_view used here.
142 // should we pass buffer or buffer->params around?
143 Buffer * buffer = current_view->buffer();
144 mergeParagraph(buffer->params, pars, startpit);
145 // this because endpar gets deleted here!
150 return PitPosPair(endpit, endpos);
157 struct resetOwnerAndChanges {
158 void operator()(Paragraph & p) {
166 bool CutAndPaste::copySelection(ParagraphList::iterator startpit,
167 ParagraphList::iterator endpit,
168 int start, int end, textclass_type tc)
170 lyx::Assert(&*startpit);
171 lyx::Assert(&*endpit);
172 lyx::Assert(0 <= start && start <= startpit->size());
173 lyx::Assert(0 <= end && end <= endpit->size());
174 lyx::Assert(startpit != endpit || start <= end);
176 ParagraphList paragraphs;
178 // Clone the paragraphs within the selection.
179 ParagraphList::iterator postend = boost::next(endpit);
181 paragraphs.assign(startpit, postend);
182 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
184 // Cut out the end of the last paragraph.
185 Paragraph & back = paragraphs.back();
186 back.erase(end, back.size());
188 // Cut out the begin of the first paragraph
189 Paragraph & front = paragraphs.front();
190 front.erase(0, start);
192 cuts.push(make_pair(paragraphs, tc));
198 pair<PitPosPair, ParagraphList::iterator>
199 CutAndPaste::pasteSelection(ParagraphList & pars,
200 ParagraphList::iterator pit, int pos,
202 ErrorList & errorlist)
204 return pasteSelection(pars, pit, pos, tc, 0, errorlist);
207 pair<PitPosPair, ParagraphList::iterator>
208 CutAndPaste::pasteSelection(ParagraphList & pars,
209 ParagraphList::iterator pit, int pos,
210 textclass_type tc, size_t cut_index,
211 ErrorList & errorlist)
213 if (!checkPastePossible())
214 return make_pair(PitPosPair(pit, pos), pit);
216 lyx::Assert (pos <= pit->size());
218 // Make a copy of the CaP paragraphs.
219 ParagraphList simple_cut_clone = cuts[cut_index].first;
220 textclass_type const textclass = cuts[cut_index].second;
222 // Now remove all out of the pars which is NOT allowed in the
223 // new environment and set also another font if that is required.
225 // Make sure there is no class difference.
226 SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
229 ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
230 int depth_delta = pit->params().depth() - tmpbuf->params().depth();
232 Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
234 for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
235 // If we have a negative jump so that the depth would
236 // go below 0 depth then we have to redo the delta to
237 // this new max depth level so that subsequent
238 // paragraphs are aligned correctly to this paragraph
240 if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
243 // Set the right depth so that we are not too deep or shallow.
244 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
245 if (tmpbuf->params().depth() > max_depth)
246 tmpbuf->params().depth(max_depth);
248 // Only set this from the 2nd on as the 2nd depends
249 // for maxDepth still on pit.
250 if (tmpbuf != simple_cut_clone.begin())
251 max_depth = tmpbuf->getMaxDepthAfter();
253 // Set the inset owner of this paragraph.
254 tmpbuf->setInsetOwner(pit->inInset());
255 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
256 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
257 if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
261 LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params, i, outerFont(pit, pars));
263 if (!pit->checkInsertChar(f1)) {
265 } else if (f1 != f2) {
266 tmpbuf->setFont(i, f1);
272 // Make the buf exactly the same layout than
273 // the cursor paragraph.
274 simple_cut_clone.begin()->makeSameLayout(*pit);
276 bool paste_the_end = false;
278 // Open the paragraph for inserting the buf
280 if (pit->size() > pos || boost::next(pit) == pars.end()) {
281 breakParagraphConservative(current_view->buffer()->params,
283 paste_the_end = true;
286 // Set the end for redoing later.
287 ParagraphList::iterator endpit = boost::next(boost::next(pit));
291 ParagraphList::iterator past_pit = boost::next(pit);
292 pars.splice(past_pit, simple_cut_clone);
293 ParagraphList::iterator last_paste = boost::prior(past_pit);
295 // If we only inserted one paragraph.
296 if (boost::next(pit) == last_paste)
299 mergeParagraph(current_view->buffer()->params, pars, pit);
301 // Store the new cursor position.
303 pos = last_paste->size();
305 // Maybe some pasting.
306 #warning CHECK! Are we comparing last_paste to the wrong list here? (Lgb)
307 if (boost::next(last_paste) != pars.end() &&
309 if (boost::next(last_paste)->hasSameLayout(*last_paste)) {
310 mergeParagraph(current_view->buffer()->params, pars,
312 } else if (boost::next(last_paste)->empty()) {
313 boost::next(last_paste)->makeSameLayout(*last_paste);
314 mergeParagraph(current_view->buffer()->params, pars,
316 } else if (last_paste->empty()) {
317 last_paste->makeSameLayout(*boost::next(last_paste));
318 mergeParagraph(current_view->buffer()->params, pars,
321 boost::next(last_paste)->stripLeadingSpaces();
324 return make_pair(PitPosPair(pit, pos), endpit);
328 int CutAndPaste::nrOfParagraphs()
330 return cuts.empty() ? 0 : cuts[0].first.size();
334 int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
336 ParagraphList & pars,
337 ErrorList & errorlist)
339 lyx::Assert(!pars.empty());
341 Paragraph * par = &*pars.begin();
347 LyXTextClass const & tclass1 = textclasslist[c1];
348 LyXTextClass const & tclass2 = textclasslist[c2];
349 ParIterator end = ParIterator();
350 for (ParIterator it = ParIterator(par); it != end; ++it) {
352 string const name = par->layout()->name();
353 bool hasLayout = tclass2.hasLayout(name);
356 par->layout(tclass2[name]);
358 par->layout(tclass2.defaultLayout());
360 if (!hasLayout && name != tclass1.defaultLayoutName()) {
362 string const s = bformat(
363 _("Layout had to be changed from\n%1$s to %2$s\n"
364 "because of class conversion from\n%3$s to %4$s"),
365 name, par->layout()->name(), tclass1.name(), tclass2.name());
366 // To warn the user that something had to be done.
367 errorlist.push_back(ErrorItem("Changed Layout", s,
376 bool CutAndPaste::checkPastePossible()
378 return !cuts.empty() && !cuts[0].first.empty();