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"
25 #include "lyxtextclasslist.h"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "ParagraphParameters.h"
29 #include "ParagraphList_fwd.h"
30 #include "pariterator.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];
86 std::swap(in.paragraphs(), pars);
88 ParIterator end = par_iterator_end(in);
89 for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
90 string const name = it->layout()->name();
91 bool hasLayout = tclass2.hasLayout(name);
94 it->layout(tclass2[name]);
96 it->layout(tclass2.defaultLayout());
98 if (!hasLayout && name != tclass1.defaultLayoutName()) {
100 string const s = bformat(
101 _("Layout had to be changed from\n%1$s to %2$s\n"
102 "because of class conversion from\n%3$s to %4$s"),
103 name, it->layout()->name(), tclass1.name(), tclass2.name());
104 // To warn the user that something had to be done.
105 errorlist.push_back(ErrorItem("Changed Layout", s,
110 std::swap(in.paragraphs(), pars);
115 std::vector<string> const availableSelections(Buffer const & buffer)
117 vector<string> selList;
119 CutStack::const_iterator cit = cuts.begin();
120 CutStack::const_iterator end = cuts.end();
121 for (; cit != end; ++cit) {
122 // we do not use cit-> here because gcc 2.9x does not
124 ParagraphList const & pars = (*cit).first;
126 ParagraphList::const_iterator pit = pars.begin();
127 ParagraphList::const_iterator pend = pars.end();
128 for (; pit != pend; ++pit) {
129 asciiSel += pit->asString(buffer, false);
130 if (asciiSel.size() > 25) {
131 asciiSel.replace(22, string::npos, "...");
136 selList.push_back(asciiSel);
143 PitPosPair eraseSelection(BufferParams const & params, ParagraphList & pars,
144 par_type startpit, par_type endpit,
145 int startpos, int endpos, bool doclear)
147 if (startpit == par_type(pars.size()) ||
148 (startpos > pars[startpit].size()))
149 return PitPosPair(endpit, endpos);
151 if (endpit == par_type(pars.size()) ||
152 startpit == endpit) {
153 endpos -= pars[startpit].erase(startpos, endpos);
154 return PitPosPair(endpit, endpos);
157 // clear end/begin fragments of the first/last par in selection
158 bool all_erased = true;
160 pars[startpit].erase(startpos, pars[startpit].size());
161 if (pars[startpit].size() != startpos)
164 endpos -= pars[endpit].erase(0, endpos);
168 // Loop through the deleted pars if any, erasing as needed
169 for (par_type pit = startpit + 1; pit != endpit;) {
170 // "erase" the contents of the par
171 pars[pit].erase(0, pars[pit].size());
172 if (!pars[pit].size()) {
173 // remove the par if it's now empty
174 pars.erase(pars.begin() + pit);
182 #if 0 // FIXME: why for cut but not copy ?
183 // the cut selection should begin with standard layout
185 buf->params().clear();
187 buf->layout(textclasslist[buffer->params.textclass].defaultLayoutName());
191 if (startpit + 1 == par_type(pars.size()))
192 return PitPosPair(endpit, endpos);
195 pars[startpit + 1].stripLeadingSpaces();
198 // paste the paragraphs again, if possible
200 (pars[startpit].hasSameLayout(pars[startpit + 1]) ||
201 pars[startpit + 1].empty())) {
202 mergeParagraph(params, pars, startpit);
203 // this because endpar gets deleted here!
208 return PitPosPair(endpit, endpos);
213 bool copySelection(ParagraphList & pars,
214 par_type startpit, par_type endpit,
215 int start, int end, textclass_type tc)
217 BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
218 BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
219 BOOST_ASSERT(startpit != endpit || start <= end);
221 // Clone the paragraphs within the selection.
222 ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
223 for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
225 // Cut out the end of the last paragraph.
226 Paragraph & back = paragraphs.back();
227 back.erase(end, back.size());
229 // Cut out the begin of the first paragraph
230 Paragraph & front = paragraphs.front();
231 front.erase(0, start);
233 cuts.push(make_pair(paragraphs, tc));
239 PitPosPair cutSelection(BufferParams const & params, ParagraphList & pars,
240 par_type startpit, par_type endpit,
241 int startpos, int endpos, textclass_type tc, bool doclear)
243 copySelection(pars, startpit, endpit, startpos, endpos, tc);
244 return eraseSelection(params, pars, startpit, endpit, startpos,
249 pair<PitPosPair, par_type>
250 pasteSelection(Buffer const & buffer, ParagraphList & pars,
251 par_type pit, int pos,
252 textclass_type tc, size_t cut_index, ErrorList & errorlist)
254 if (!checkPastePossible())
255 return make_pair(PitPosPair(pit, pos), pit);
257 BOOST_ASSERT (pos <= pars[pit].size());
259 // Make a copy of the CaP paragraphs.
260 ParagraphList insertion = cuts[cut_index].first;
261 textclass_type const textclass = cuts[cut_index].second;
263 // Now remove all out of the pars which is NOT allowed in the
264 // new environment and set also another font if that is required.
266 // Make sure there is no class difference.
267 SwitchLayoutsBetweenClasses(textclass, tc, insertion,
270 ParagraphList::iterator tmpbuf = insertion.begin();
271 int depth_delta = pars[pit].params().depth() - tmpbuf->params().depth();
273 Paragraph::depth_type max_depth = pars[pit].getMaxDepthAfter();
275 for (; tmpbuf != insertion.end(); ++tmpbuf) {
276 // If we have a negative jump so that the depth would
277 // go below 0 depth then we have to redo the delta to
278 // this new max depth level so that subsequent
279 // paragraphs are aligned correctly to this paragraph
281 if (int(tmpbuf->params().depth()) + depth_delta < 0)
284 // Set the right depth so that we are not too deep or shallow.
285 tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
286 if (tmpbuf->params().depth() > max_depth)
287 tmpbuf->params().depth(max_depth);
289 // Only set this from the 2nd on as the 2nd depends
290 // for maxDepth still on pit.
291 if (tmpbuf != insertion.begin())
292 max_depth = tmpbuf->getMaxDepthAfter();
294 // Set the inset owner of this paragraph.
295 tmpbuf->setInsetOwner(pars[pit].inInset());
296 for (pos_type i = 0; i < tmpbuf->size(); ++i) {
297 if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
298 if (!pars[pit].insetAllowed(tmpbuf->getInset(i)->lyxCode()))
304 // Make the buf exactly the same layout as the cursor paragraph.
305 insertion.begin()->makeSameLayout(pars[pit]);
307 // Prepare the paragraphs and insets for insertion.
308 // A couple of insets store buffer references so need updating.
310 std::swap(in.paragraphs(), insertion);
312 ParIterator fpit = par_iterator_begin(in);
313 ParIterator fend = par_iterator_end(in);
315 for (; fpit != fend; ++fpit) {
316 InsetList::iterator lit = fpit->insetlist.begin();
317 InsetList::iterator eit = fpit->insetlist.end();
319 for (; lit != eit; ++lit) {
320 switch (lit->inset->lyxCode()) {
321 case InsetOld::TABULAR_CODE: {
322 InsetTabular * it = static_cast<InsetTabular*>(lit->inset);
323 it->buffer(const_cast<Buffer*>(&buffer));
332 std::swap(in.paragraphs(), insertion);
334 // Split the paragraph for inserting the buf if necessary.
335 bool did_split = false;
336 if (pars[pit].size() || pit + 1 == par_type(pars.size())) {
337 breakParagraphConservative(buffer.params(), pars, pit, pos);
342 pars.insert(pars.begin() + pit + 1, insertion.begin(), insertion.end());
343 mergeParagraph(buffer.params(), pars, pit);
345 par_type last_paste = pit + insertion.size() - 1;
347 // Store the new cursor position.
349 pos = pars[last_paste].size();
351 // Maybe some pasting.
352 if (did_split && last_paste + 1 != par_type(pars.size())) {
353 if (pars[last_paste + 1].hasSameLayout(pars[last_paste])) {
354 mergeParagraph(buffer.params(), pars, last_paste);
355 } else if (pars[last_paste + 1].empty()) {
356 pars[last_paste + 1].makeSameLayout(pars[last_paste]);
357 mergeParagraph(buffer.params(), pars, last_paste);
358 } else if (pars[last_paste].empty()) {
359 pars[last_paste].makeSameLayout(pars[last_paste + 1]);
360 mergeParagraph(buffer.params(), pars, last_paste);
362 pars[last_paste + 1].stripLeadingSpaces();
367 return make_pair(PitPosPair(pit, pos), last_paste + 1);
371 pair<PitPosPair, par_type>
372 pasteSelection(Buffer const & buffer, ParagraphList & pars,
373 par_type pit, int pos, textclass_type tc, ErrorList & errorlist)
375 return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
381 return cuts.empty() ? 0 : cuts[0].first.size();
385 bool checkPastePossible()
387 return !cuts.empty() && !cuts[0].first.empty();
391 void cutSelection(LCursor & cur, bool doclear, bool realcut)
393 LyXText * text = cur.text();
395 // Stuff what we got on the clipboard. Even if there is no selection.
397 // There is a problem with having the stuffing here in that the
398 // larger the selection the slower LyX will get. This can be
399 // solved by running the line below only when the selection has
400 // finished. The solution used currently just works, to make it
401 // faster we need to be more clever and probably also have more
402 // calls to stuffClipboard. (Lgb)
403 cur.bv().stuffClipboard(cur.selectionAsString(true));
405 // This doesn't make sense, if there is no selection
406 if (!cur.selection())
409 // OK, we have a selection. This is always between cur.selBegin()
412 // make sure that the depth behind the selection are restored, too
413 recordUndoSelection(cur);
414 par_type begpit = cur.selBegin().par();
415 par_type endpit = cur.selEnd().par();
417 int endpos = cur.selEnd().pos();
419 BufferParams const & bufparams = cur.buffer().params();
420 boost::tie(endpit, endpos) = realcut ?
421 cutSelection(bufparams,
424 cur.selBegin().pos(), endpos,
427 : eraseSelection(bufparams,
430 cur.selBegin().pos(), endpos,
432 // sometimes necessary
434 text->paragraphs()[begpit].stripLeadingSpaces();
436 text->redoParagraphs(begpit, begpit + 1);
437 // cutSelection can invalidate the cursor so we need to set
439 // we prefer the end for when tracking changes
443 // need a valid cursor. (Lgb)
444 cur.clearSelection();
445 text->updateCounters();
449 void copySelection(LCursor & cur)
451 LyXText * text = cur.text();
453 // stuff the selection onto the X clipboard, from an explicit copy request
454 cur.bv().stuffClipboard(cur.selectionAsString(true));
456 // this doesn't make sense, if there is no selection
457 if (!cur.selection())
460 // ok we have a selection. This is always between cur.selBegin()
461 // and sel_end cursor
463 // copy behind a space if there is one
464 ParagraphList & pars = text->paragraphs();
465 pos_type pos = cur.selBegin().pos();
466 par_type par = cur.selBegin().par();
467 while (pos < pars[par].size()
468 && pars[par].isLineSeparator(pos)
469 && (par != cur.selEnd().par() || pos < cur.selEnd().pos()))
472 copySelection(pars, par, cur.selEnd().par(),
473 pos, cur.selEnd().pos(), cur.buffer().params().textclass);
477 void pasteSelection(LCursor & cur, size_t sel_index)
479 LyXText * text = cur.text();
481 // this does not make sense, if there is nothing to paste
482 if (!checkPastePossible())
492 boost::tie(ppp, endpit) =
493 pasteSelection(cur.buffer(),
495 cur.par(), cur.pos(),
496 cur.buffer().params().textclass,
498 bufferErrors(cur.buffer(), el);
499 cur.bv().showErrorList(_("Paste"));
501 text->redoParagraphs(cur.par(), endpit);
503 cur.clearSelection();
505 text->setCursor(cur, ppp.first, ppp.second);
507 text->updateCounters();
511 void setSelectionRange(LCursor & cur, pos_type length)
513 LyXText * text = cur.text();
519 text->cursorRight(cur);
524 // simple replacing. The font of the first selected character is used
525 void replaceSelectionWithString(LCursor & cur, string const & str)
527 LyXText * text = cur.text();
531 // Get font setting before we cut
532 pos_type pos = cur.selEnd().pos();
533 LyXFont const font = text->getPar(cur.selBegin().par()).
534 getFontSettings(cur.buffer().params(), cur.selBegin().pos());
536 // Insert the new string
537 string::const_iterator cit = str.begin();
538 string::const_iterator end = str.end();
539 for (; cit != end; ++cit, ++pos)
540 text->getPar(cur.selEnd().par()).insertChar(pos, (*cit), font);
543 cutSelection(cur, true, false);
547 void replaceSelection(LCursor & cur)
550 cutSelection(cur, true, false);
554 // only used by the spellchecker
555 void replaceWord(LCursor & cur, string const & replacestring)
557 LyXText * text = cur.text();
560 replaceSelectionWithString(cur, replacestring);
561 setSelectionRange(cur, replacestring.length());
563 // Go back so that replacement string is also spellchecked
564 for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
565 text->cursorLeft(cur);