3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
15 #include "CutAndPaste.h"
18 #include "buffer_funcs.h"
19 #include "bufferparams.h"
20 #include "BufferView.h"
22 #include "errorlist.h"
24 #include "iterators.h"
26 #include "lyxtextclasslist.h"
27 #include "paragraph.h"
28 #include "paragraph_funcs.h"
29 #include "ParagraphParameters.h"
30 #include "ParagraphList_fwd.h"
33 #include "insets/insettabular.h"
35 #include "support/lstrings.h"
37 #include <boost/tuple/tuple.hpp>
41 using lyx::textclass_type;
43 using lyx::support::bformat;
54 typedef std::pair<lyx::par_type, int> PitPosPair;
56 typedef limited_stack<pair<ParagraphList, textclass_type> > CutStack;
60 struct resetOwnerAndChanges : public std::unary_function<Paragraph, void> {
61 void operator()(Paragraph & p) const {
74 int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2,
75 ParagraphList & pars, ErrorList & errorlist)
77 BOOST_ASSERT(!pars.empty());
82 LyXTextClass const & tclass1 = textclasslist[c1];
83 LyXTextClass const & tclass2 = textclasslist[c2];
84 ParIterator end = ParIterator(pars.size(), pars);
85 for (ParIterator it = ParIterator(0, pars); it != end; ++it) {
86 string const name = it->layout()->name();
87 bool hasLayout = tclass2.hasLayout(name);
90 it->layout(tclass2[name]);
92 it->layout(tclass2.defaultLayout());
94 if (!hasLayout && name != tclass1.defaultLayoutName()) {
96 string const s = bformat(
97 _("Layout had to be changed from\n%1$s to %2$s\n"
98 "because of class conversion from\n%3$s to %4$s"),
99 name, it->layout()->name(), tclass1.name(), tclass2.name());
100 // To warn the user that something had to be done.
101 errorlist.push_back(ErrorItem("Changed Layout", s,
110 std::vector<string> const availableSelections(Buffer const & buffer)
112 vector<string> selList;
114 CutStack::const_iterator cit = cuts.begin();
115 CutStack::const_iterator end = cuts.end();
116 for (; cit != end; ++cit) {
117 // we do not use cit-> here because gcc 2.9x does not
119 ParagraphList const & pars = (*cit).first;
121 ParagraphList::const_iterator pit = pars.begin();
122 ParagraphList::const_iterator pend = pars.end();
123 for (; pit != pend; ++pit) {
124 asciiSel += pit->asString(buffer, false);
125 if (asciiSel.size() > 25) {
126 asciiSel.replace(22, string::npos, "...");
131 selList.push_back(asciiSel);
138 PitPosPair eraseSelection(BufferParams const & params, ParagraphList & pars,
139 par_type startpit, par_type endpit,
140 int startpos, int endpos, bool doclear)
142 if (startpit == par_type(pars.size()) ||
143 (startpos > pars[startpit].size()))
144 return PitPosPair(endpit, endpos);
146 if (endpit == par_type(pars.size()) ||
147 startpit == endpit) {
148 endpos -= pars[startpit].erase(startpos, endpos);
149 return PitPosPair(endpit, endpos);
152 // clear end/begin fragments of the first/last par in selection
153 bool all_erased = true;
155 pars[startpit].erase(startpos, pars[startpit].size());
156 if (pars[startpit].size() != startpos)
159 endpos -= pars[endpit].erase(0, endpos);
163 // Loop through the deleted pars if any, erasing as needed
165 par_type pit = startpit + 1;
167 while (pit != endpit && pit != par_type(pars.size())) {
168 par_type const next = pit + 1;
169 // "erase" the contents of the par
170 pars[pit].erase(0, pars[pit].size());
171 if (!pars[pit].size()) {
172 // remove the par if it's now empty
173 pars.erase(pars.begin() + pit);
179 #if 0 // FIXME: why for cut but not copy ?
180 // the cut selection should begin with standard layout
182 buf->params().clear();
184 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
188 if (startpit + 1 == par_type(pars.size()))
189 return PitPosPair(endpit, endpos);
192 pars[startpit + 1].stripLeadingSpaces();
195 // paste the paragraphs again, if possible
197 (pars[startpit].hasSameLayout(pars[startpit + 1]) ||
198 pars[startpit + 1].empty())) {
199 mergeParagraph(params, pars, startpit);
200 // this because endpar gets deleted here!
205 return PitPosPair(endpit, endpos);
210 bool copySelection(ParagraphList & pars,
211 par_type startpit, par_type endpit,
212 int start, int end, textclass_type tc)
214 BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
215 BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
216 BOOST_ASSERT(startpit != endpit || start <= end);
218 // Clone the paragraphs within the selection.
219 ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
220 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
222 // Cut out the end of the last paragraph.
223 Paragraph & back = paragraphs.back();
224 back.erase(end, back.size());
226 // Cut out the begin of the first paragraph
227 Paragraph & front = paragraphs.front();
228 front.erase(0, start);
230 cuts.push(make_pair(paragraphs, tc));
236 PitPosPair cutSelection(BufferParams const & params, ParagraphList & pars,
237 par_type startpit, par_type endpit,
238 int startpos, int endpos, textclass_type tc, bool doclear)
240 copySelection(pars, startpit, endpit, startpos, endpos, tc);
241 return eraseSelection(params, pars, startpit, endpit, startpos,
246 pair<PitPosPair, par_type>
247 pasteSelection(Buffer const & buffer, ParagraphList & pars,
248 par_type pit, int pos,
249 textclass_type tc, size_t cut_index, ErrorList & errorlist)
251 if (!checkPastePossible())
252 return make_pair(PitPosPair(pit, pos), pit);
254 BOOST_ASSERT (pos <= pars[pit].size());
256 // Make a copy of the CaP paragraphs.
257 ParagraphList insertion = cuts[cut_index].first;
258 textclass_type const textclass = cuts[cut_index].second;
260 // Now remove all out of the pars which is NOT allowed in the
261 // new environment and set also another font if that is required.
263 // Make sure there is no class difference.
264 SwitchLayoutsBetweenClasses(textclass, tc, insertion,
267 ParagraphList::iterator tmpbuf = insertion.begin();
268 int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
270 Paragraph::depth_type max_depth = pars[pit].getMaxDepthAfter();
272 for (; tmpbuf != insertion.end(); ++tmpbuf) {
273 // If we have a negative jump so that the depth would
274 // go below 0 depth then we have to redo the delta to
275 // this new max depth level so that subsequent
276 // paragraphs are aligned correctly to this paragraph
278 if (int(tmpbuf->params().depth()) + depth_delta < 0)
281 // Set the right depth so that we are not too deep or shallow.
282 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
283 if (tmpbuf->params().depth() > max_depth)
284 tmpbuf->params().depth(max_depth);
286 // Only set this from the 2nd on as the 2nd depends
287 // for maxDepth still on pit.
288 if (tmpbuf != insertion.begin())
289 max_depth = tmpbuf->getMaxDepthAfter();
291 // Set the inset owner of this paragraph.
292 tmpbuf->setInsetOwner(pars[pit].inInset());
293 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
294 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
295 if (!pars[pit].insetAllowed(tmpbuf->getInset(i)->lyxCode()))
301 // Make the buf exactly the same layout as the cursor paragraph.
302 insertion.begin()->makeSameLayout(pars[pit]);
304 // Prepare the paragraphs and insets for insertion.
305 // A couple of insets store buffer references so need updating.
306 ParIterator fpit(0, insertion);
307 ParIterator fend(insertion.size(), insertion);
309 for (; fpit != fend; ++fpit) {
310 InsetList::iterator lit = fpit->insetlist.begin();
311 InsetList::iterator eit = fpit->insetlist.end();
313 for (; lit != eit; ++lit) {
314 switch (lit->inset->lyxCode()) {
315 case InsetOld::TABULAR_CODE: {
316 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
317 it->buffer(const_cast<Buffer*>(&buffer));
327 // Split the paragraph for inserting the buf if necessary.
328 bool did_split = false;
329 if (pars[pit].size() || pit + 1 == par_type(pars.size())) {
330 breakParagraphConservative(buffer.params(), pars, pit, pos);
335 pars.insert(pars.begin() + pit + 1, insertion.begin(), insertion.end());
336 par_type last_paste = pit + insertion.size();
338 // If we only inserted one paragraph.
339 if (insertion.size() == 1)
342 mergeParagraph(buffer.params(), pars, pit);
344 // Store the new cursor position.
346 pos = pars[last_paste].size();
348 // Maybe some pasting.
349 if (did_split && last_paste + 1 != par_type(pars.size())) {
350 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
351 mergeParagraph(buffer.params(), pars, last_paste);
352 } else if (pars[last_paste + 1].empty()) {
353 pars[last_paste + 1].makeSameLayout(pars[last_paste]);
354 mergeParagraph(buffer.params(), pars, last_paste);
355 } else if (pars[last_paste].empty()) {
356 pars[last_paste].makeSameLayout(pars[last_paste]);
357 mergeParagraph(buffer.params(), pars, last_paste);
359 pars[last_paste + 1].stripLeadingSpaces();
362 return make_pair(PitPosPair(pit, pos), pit + insertion.size() + 1);
366 pair<PitPosPair, par_type>
367 pasteSelection(Buffer const & buffer, ParagraphList & pars,
368 par_type pit, int pos, textclass_type tc, ErrorList & errorlist)
370 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
376 return cuts.empty() ? 0 : cuts[0].first.size();
380 bool checkPastePossible()
382 return !cuts.empty() && !cuts[0].first.empty();
386 void cutSelection(LCursor & cur, bool doclear, bool realcut)
388 LyXText * text = cur.text();
390 // Stuff what we got on the clipboard. Even if there is no selection.
392 // There is a problem with having the stuffing here in that the
393 // larger the selection the slower LyX will get. This can be
394 // solved by running the line below only when the selection has
395 // finished. The solution used currently just works, to make it
396 // faster we need to be more clever and probably also have more
397 // calls to stuffClipboard. (Lgb)
398 cur.bv().stuffClipboard(cur.selectionAsString(true));
400 // This doesn't make sense, if there is no selection
401 if (!cur.selection())
404 // OK, we have a selection. This is always between cur.selBegin()
407 // make sure that the depth behind the selection are restored, too
408 recordUndoSelection(cur);
409 par_type begpit = cur.selBegin().par();
410 par_type endpit = cur.selEnd().par();
412 int endpos = cur.selEnd().pos();
414 BufferParams const & bufparams = cur.bv().buffer()->params();
415 boost::tie(endpit, endpos) = realcut ?
416 cutSelection(bufparams,
419 cur.selBegin().pos(), endpos,
422 : eraseSelection(bufparams,
425 cur.selBegin().pos(), endpos,
427 // sometimes necessary
429 text->paragraphs()[begpit].stripLeadingSpaces();
431 text->redoParagraphs(begpit, begpit + 1);
432 // cutSelection can invalidate the cursor so we need to set
434 // we prefer the end for when tracking changes
438 // need a valid cursor. (Lgb)
439 cur.clearSelection();
440 text->updateCounters();
444 void copySelection(LCursor & cur)
446 LyXText * text = cur.text();
448 // stuff the selection onto the X clipboard, from an explicit copy request
449 cur.bv().stuffClipboard(cur.selectionAsString(true));
451 // this doesn't make sense, if there is no selection
452 if (!cur.selection())
455 // ok we have a selection. This is always between cur.selBegin()
456 // and sel_end cursor
458 // copy behind a space if there is one
459 ParagraphList & pars = text->paragraphs();
460 pos_type pos = cur.selBegin().pos();
461 par_type par = cur.selBegin().par();
462 while (pos < pars[par].size()
463 && pars[par].isLineSeparator(pos)
464 && (par != cur.selEnd().par() || pos < cur.selEnd().pos()))
467 copySelection(pars, par, cur.selEnd().par(),
468 pos, cur.selEnd().pos(), cur.bv().buffer()->params().textclass);
472 void pasteSelection(LCursor & cur, size_t sel_index)
474 LyXText * text = cur.text();
476 // this does not make sense, if there is nothing to paste
477 if (!checkPastePossible())
487 boost::tie(ppp, endpit) =
488 pasteSelection(*cur.bv().buffer(),
490 cur.par(), cur.pos(),
491 cur.bv().buffer()->params().textclass,
493 bufferErrors(*cur.bv().buffer(), el);
494 text->bv()->showErrorList(_("Paste"));
496 text->redoParagraphs(cur.par(), endpit);
498 cur.clearSelection();
500 text->setCursor(cur, ppp.first, ppp.second);
502 text->updateCounters();
506 void setSelectionRange(LCursor & cur, pos_type length)
508 LyXText * text = cur.text();
514 text->cursorRight(cur);
519 // simple replacing. The font of the first selected character is used
520 void replaceSelectionWithString(LCursor & cur, string const & str)
522 LyXText * text = cur.text();
526 // Get font setting before we cut
527 pos_type pos = cur.selEnd().pos();
528 LyXFont const font = text->getPar(cur.selBegin().par()).
529 getFontSettings(cur.bv().buffer()->params(), cur.selBegin().pos());
531 // Insert the new string
532 string::const_iterator cit = str.begin();
533 string::const_iterator end = str.end();
534 for (; cit != end; ++cit, ++pos)
535 text->getPar(cur.selEnd().par()).insertChar(pos, (*cit), font);
538 cutSelection(cur, true, false);
542 void replaceSelection(LCursor & cur)
545 cutSelection(cur, true, false);
549 // only used by the spellchecker
550 void replaceWord(LCursor & cur, string const & replacestring)
552 LyXText * text = cur.text();
555 replaceSelectionWithString(cur, replacestring);
556 setSelectionRange(cur, replacestring.length());
558 // Go back so that replacement string is also spellchecked
559 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
560 text->cursorLeft(cur);