3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Stefan Schimanski
16 * \author Jürgen Vigna
18 * Full author contact details are available in file CREDITS.
27 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "CutAndPaste.h"
34 #include "DispatchResult.h"
35 #include "ErrorList.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "TextClass.h"
44 #include "TextMetrics.h"
46 #include "insets/InsetCollapsable.h"
48 #include "mathed/InsetMathHull.h"
50 #include "support/lassert.h"
51 #include "support/debug.h"
52 #include "support/gettext.h"
53 #include "support/textutils.h"
55 #include <boost/next_prior.hpp>
63 bool Text::isMainText() const
65 return &owner_->buffer().text() == this;
69 // Note that this is supposed to return a fully realized font.
70 FontInfo Text::layoutFont(pit_type const pit) const
72 Layout const & layout = pars_[pit].layout();
74 if (!pars_[pit].getDepth()) {
75 FontInfo lf = layout.resfont;
76 // In case the default family has been customized
77 if (layout.font.family() == INHERIT_FAMILY)
78 lf.setFamily(owner_->buffer().params().getFont().fontInfo().family());
80 // It ought to be possible here just to use Inset::getLayout() and skip
81 // the asInsetCollapsable() bit. Unfortunatley, that doesn't work right
82 // now, because Inset::getLayout() will return a default-constructed
83 // InsetLayout, and that e.g. sets the foreground color to red. So we
84 // need to do some work to make that possible.
85 InsetCollapsable const * icp = owner_->asInsetCollapsable();
88 FontInfo icf = icp->getLayout().font();
93 FontInfo font = layout.font;
94 // Realize with the fonts of lesser depth.
95 //font.realize(outerFont(pit));
96 font.realize(owner_->buffer().params().getFont().fontInfo());
102 // Note that this is supposed to return a fully realized font.
103 FontInfo Text::labelFont(Paragraph const & par) const
105 Buffer const & buffer = owner_->buffer();
106 Layout const & layout = par.layout();
108 if (!par.getDepth()) {
109 FontInfo lf = layout.reslabelfont;
110 // In case the default family has been customized
111 if (layout.labelfont.family() == INHERIT_FAMILY)
112 lf.setFamily(buffer.params().getFont().fontInfo().family());
116 FontInfo font = layout.labelfont;
117 // Realize with the fonts of lesser depth.
118 font.realize(buffer.params().getFont().fontInfo());
124 void Text::setCharFont(pit_type pit,
125 pos_type pos, Font const & fnt, Font const & display_font)
127 Buffer const & buffer = owner_->buffer();
129 Layout const & layout = pars_[pit].layout();
131 // Get concrete layout font to reduce against
134 if (pos < pars_[pit].beginOfBody())
135 layoutfont = layout.labelfont;
137 layoutfont = layout.font;
139 // Realize against environment font information
140 if (pars_[pit].getDepth()) {
142 while (!layoutfont.resolved() &&
143 tp != pit_type(paragraphs().size()) &&
144 pars_[tp].getDepth()) {
146 if (tp != pit_type(paragraphs().size()))
147 layoutfont.realize(pars_[tp].layout().font);
151 // Inside inset, apply the inset's font attributes if any
154 layoutfont.realize(display_font.fontInfo());
156 layoutfont.realize(buffer.params().getFont().fontInfo());
158 // Now, reduce font against full layout font
159 font.fontInfo().reduce(layoutfont);
161 pars_[pit].setFont(pos, font);
165 void Text::setInsetFont(BufferView const & bv, pit_type pit,
166 pos_type pos, Font const & font, bool toggleall)
168 Inset * const inset = pars_[pit].getInset(pos);
169 LASSERT(inset && inset->resetFontEdit(), /**/);
171 CursorSlice::idx_type endidx = inset->nargs();
172 for (CursorSlice cs(*inset); cs.idx() != endidx; ++cs.idx()) {
173 Text * text = cs.text();
175 // last position of the cell
176 CursorSlice cellend = cs;
177 cellend.pit() = cellend.lastpit();
178 cellend.pos() = cellend.lastpos();
179 text->setFont(bv, cs, cellend, font, toggleall);
185 // return past-the-last paragraph influenced by a layout change on pit
186 pit_type Text::undoSpan(pit_type pit)
188 pit_type const end = paragraphs().size();
189 pit_type nextpit = pit + 1;
192 //because of parindents
193 if (!pars_[pit].getDepth())
194 return boost::next(nextpit);
195 //because of depth constrains
196 for (; nextpit != end; ++pit, ++nextpit) {
197 if (!pars_[pit].getDepth())
204 void Text::setLayout(pit_type start, pit_type end,
205 docstring const & layout)
207 LASSERT(start != end, /**/);
209 Buffer const & buffer = owner_->buffer();
210 BufferParams const & bp = buffer.params();
211 Layout const & lyxlayout = bp.documentClass()[layout];
213 for (pit_type pit = start; pit != end; ++pit) {
214 Paragraph & par = pars_[pit];
215 par.applyLayout(lyxlayout);
216 if (lyxlayout.margintype == MARGIN_MANUAL)
217 par.setLabelWidthString(par.expandLabel(lyxlayout, bp));
222 // set layout over selection and make a total rebreak of those paragraphs
223 void Text::setLayout(Cursor & cur, docstring const & layout)
225 LASSERT(this == cur.text(), /**/);
227 pit_type start = cur.selBegin().pit();
228 pit_type end = cur.selEnd().pit() + 1;
229 pit_type undopit = undoSpan(end - 1);
230 recUndo(cur, start, undopit - 1);
231 setLayout(start, end, layout);
232 cur.forceBufferUpdate();
236 static bool changeDepthAllowed(Text::DEPTH_CHANGE type,
237 Paragraph const & par, int max_depth)
239 if (par.layout().labeltype == LABEL_BIBLIO)
241 int const depth = par.params().depth();
242 if (type == Text::INC_DEPTH && depth < max_depth)
244 if (type == Text::DEC_DEPTH && depth > 0)
250 bool Text::changeDepthAllowed(Cursor & cur, DEPTH_CHANGE type) const
252 LASSERT(this == cur.text(), /**/);
253 // this happens when selecting several cells in tabular (bug 2630)
254 if (cur.selBegin().idx() != cur.selEnd().idx())
257 pit_type const beg = cur.selBegin().pit();
258 pit_type const end = cur.selEnd().pit() + 1;
259 int max_depth = (beg != 0 ? pars_[beg - 1].getMaxDepthAfter() : 0);
261 for (pit_type pit = beg; pit != end; ++pit) {
262 if (lyx::changeDepthAllowed(type, pars_[pit], max_depth))
264 max_depth = pars_[pit].getMaxDepthAfter();
270 void Text::changeDepth(Cursor & cur, DEPTH_CHANGE type)
272 LASSERT(this == cur.text(), /**/);
273 pit_type const beg = cur.selBegin().pit();
274 pit_type const end = cur.selEnd().pit() + 1;
275 cur.recordUndoSelection();
276 int max_depth = (beg != 0 ? pars_[beg - 1].getMaxDepthAfter() : 0);
278 for (pit_type pit = beg; pit != end; ++pit) {
279 Paragraph & par = pars_[pit];
280 if (lyx::changeDepthAllowed(type, par, max_depth)) {
281 int const depth = par.params().depth();
282 if (type == INC_DEPTH)
283 par.params().depth(depth + 1);
285 par.params().depth(depth - 1);
287 max_depth = par.getMaxDepthAfter();
289 // this handles the counter labels, and also fixes up
290 // depth values for follow-on (child) paragraphs
291 cur.forceBufferUpdate();
295 void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
297 LASSERT(this == cur.text(), /**/);
298 // Set the current_font
299 // Determine basis font
301 pit_type pit = cur.pit();
302 if (cur.pos() < pars_[pit].beginOfBody())
303 layoutfont = labelFont(pars_[pit]);
305 layoutfont = layoutFont(pit);
307 // Update current font
308 cur.real_current_font.update(font,
309 cur.buffer()->params().language,
312 // Reduce to implicit settings
313 cur.current_font = cur.real_current_font;
314 cur.current_font.fontInfo().reduce(layoutfont);
315 // And resolve it completely
316 cur.real_current_font.fontInfo().realize(layoutfont);
318 // if there is no selection that's all we need to do
319 if (!cur.selection())
322 // Ok, we have a selection.
323 cur.recordUndoSelection();
325 setFont(cur.bv(), cur.selectionBegin().top(),
326 cur.selectionEnd().top(), font, toggleall);
330 void Text::setFont(BufferView const & bv, CursorSlice const & begin,
331 CursorSlice const & end, Font const & font,
334 Buffer const & buffer = bv.buffer();
336 // Don't use forwardChar here as ditend might have
337 // pos() == lastpos() and forwardChar would miss it.
338 // Can't use forwardPos either as this descends into
340 Language const * language = buffer.params().language;
341 for (CursorSlice dit = begin; dit != end; dit.forwardPos()) {
342 if (dit.pos() == dit.lastpos())
344 pit_type const pit = dit.pit();
345 pos_type const pos = dit.pos();
346 Inset * inset = pars_[pit].getInset(pos);
347 if (inset && inset->resetFontEdit()) {
348 // We need to propagate the font change to all
349 // text cells of the inset (bugs 1973, 6919).
350 setInsetFont(bv, pit, pos, font, toggleall);
352 TextMetrics const & tm = bv.textMetrics(this);
353 Font f = tm.displayFont(pit, pos);
354 f.update(font, language, toggleall);
355 setCharFont(pit, pos, f, tm.font_);
356 // font change may change language...
357 // spell checker has to know that
358 pars_[pit].requestSpellCheck(pos);
363 bool Text::cursorTop(Cursor & cur)
365 LASSERT(this == cur.text(), /**/);
366 return setCursor(cur, 0, 0);
370 bool Text::cursorBottom(Cursor & cur)
372 LASSERT(this == cur.text(), /**/);
373 return setCursor(cur, cur.lastpit(), boost::prior(paragraphs().end())->size());
377 void Text::toggleFree(Cursor & cur, Font const & font, bool toggleall)
379 LASSERT(this == cur.text(), /**/);
380 // If the mask is completely neutral, tell user
381 if (font.fontInfo() == ignore_font && font.language() == ignore_language) {
382 // Could only happen with user style
383 cur.message(_("No font change defined."));
387 // Try implicit word selection
388 // If there is a change in the language the implicit word selection
390 CursorSlice const resetCursor = cur.top();
391 bool const implicitSelection =
392 font.language() == ignore_language
393 && font.fontInfo().number() == FONT_IGNORE
394 && selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
397 setFont(cur, font, toggleall);
399 // Implicit selections are cleared afterwards
400 // and cursor is set to the original position.
401 if (implicitSelection) {
402 cur.clearSelection();
403 cur.top() = resetCursor;
409 docstring Text::getStringToIndex(Cursor const & cur)
411 LASSERT(this == cur.text(), /**/);
414 return cur.selectionAsString(false);
416 // Try implicit word selection. If there is a change
417 // in the language the implicit word selection is
420 selectWord(tmpcur, PREVIOUS_WORD);
422 if (!tmpcur.selection())
423 cur.message(_("Nothing to index!"));
424 else if (tmpcur.selBegin().pit() != tmpcur.selEnd().pit())
425 cur.message(_("Cannot index more than one paragraph!"));
427 return tmpcur.selectionAsString(false);
433 void Text::setLabelWidthStringToSequence(pit_type const par_offset,
436 pit_type offset = par_offset;
437 // Find first of same layout in sequence
438 while (!isFirstInSequence(offset)) {
439 offset = depthHook(offset, pars_[offset].getDepth());
442 // now apply label width string to every par
444 pit_type const end = pars_.size();
445 depth_type const depth = pars_[offset].getDepth();
446 Layout const & layout = pars_[offset].layout();
447 for (pit_type pit = offset; pit != end; ++pit) {
448 while (pars_[pit].getDepth() > depth)
450 if (pars_[pit].getDepth() < depth)
452 if (pars_[pit].layout() != layout)
454 pars_[pit].setLabelWidthString(s);
459 void Text::setParagraphs(Cursor & cur, docstring arg, bool merge)
461 LASSERT(cur.text(), /**/);
462 // make sure that the depth behind the selection are restored, too
463 pit_type undopit = undoSpan(cur.selEnd().pit());
464 recUndo(cur, cur.selBegin().pit(), undopit - 1);
467 string const argument = to_utf8(arg);
468 depth_type priordepth = -1;
470 for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit();
472 Paragraph & par = pars_[pit];
473 ParagraphParameters params = par.params();
474 params.read(argument, merge);
475 // Changes to label width string apply to all paragraphs
476 // with same layout in a sequence.
477 // Do this only once for a selected range of paragraphs
478 // of the same layout and depth.
479 if (par.getDepth() != priordepth || par.layout() != priorlayout)
480 setLabelWidthStringToSequence(pit, params.labelWidthString());
481 par.params().apply(params, par.layout());
482 priordepth = par.getDepth();
483 priorlayout = par.layout();
488 //FIXME This is a little redundant now, but it's probably worth keeping,
489 //especially if we're going to go away from using serialization internally
491 void Text::setParagraphs(Cursor & cur, ParagraphParameters const & p)
493 LASSERT(cur.text(), /**/);
494 // make sure that the depth behind the selection are restored, too
495 pit_type undopit = undoSpan(cur.selEnd().pit());
496 recUndo(cur, cur.selBegin().pit(), undopit - 1);
498 depth_type priordepth = -1;
500 for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit();
502 Paragraph & par = pars_[pit];
503 // Changes to label width string apply to all paragraphs
504 // with same layout in a sequence.
505 // Do this only once for a selected range of paragraphs
506 // of the same layout and depth.
507 if (par.getDepth() != priordepth || par.layout() != priorlayout)
508 setLabelWidthStringToSequence(pit,
509 par.params().labelWidthString());
510 par.params().apply(p, par.layout());
511 priordepth = par.getDepth();
512 priorlayout = par.layout();
517 // this really should just insert the inset and not move the cursor.
518 void Text::insertInset(Cursor & cur, Inset * inset)
520 LASSERT(this == cur.text(), /**/);
521 LASSERT(inset, /**/);
522 cur.paragraph().insertInset(cur.pos(), inset, cur.current_font,
523 Change(cur.buffer()->params().trackChanges
524 ? Change::INSERTED : Change::UNCHANGED));
528 bool Text::setCursor(Cursor & cur, pit_type par, pos_type pos,
529 bool setfont, bool boundary)
531 TextMetrics const & tm = cur.bv().textMetrics(this);
532 bool const update_needed = !tm.contains(par);
534 setCursorIntern(cur, par, pos, setfont, boundary);
535 return cur.bv().checkDepm(cur, old) || update_needed;
539 void Text::setCursor(CursorSlice & cur, pit_type par, pos_type pos)
541 LASSERT(par != int(paragraphs().size()), /**/);
545 // now some strict checking
546 Paragraph & para = getPar(par);
548 // None of these should happen, but we're scaredy-cats
550 lyxerr << "don't like -1" << endl;
551 LASSERT(false, /**/);
554 if (pos > para.size()) {
555 lyxerr << "don't like 1, pos: " << pos
556 << " size: " << para.size()
557 << " par: " << par << endl;
558 LASSERT(false, /**/);
563 void Text::setCursorIntern(Cursor & cur,
564 pit_type par, pos_type pos, bool setfont, bool boundary)
566 LASSERT(this == cur.text(), /**/);
567 cur.boundary(boundary);
568 setCursor(cur.top(), par, pos);
570 cur.setCurrentFont();
574 bool Text::checkAndActivateInset(Cursor & cur, bool front)
578 if (front && cur.pos() == cur.lastpos())
580 if (!front && cur.pos() == 0)
582 Inset * inset = front ? cur.nextInset() : cur.prevInset();
583 if (!inset || !inset->editable())
586 * Apparently, when entering an inset we are expected to be positioned
587 * *before* it in the containing paragraph, regardless of the direction
588 * from which we are entering. Otherwise, cursor placement goes awry,
589 * and when we exit from the beginning, we'll be placed *after* the
594 inset->edit(cur, front);
599 bool Text::checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool movingLeft)
605 if (cur.pos() == cur.lastpos())
607 Paragraph & par = cur.paragraph();
608 Inset * inset = par.isInset(cur.pos()) ? par.getInset(cur.pos()) : 0;
609 if (!inset || !inset->editable())
611 inset->edit(cur, movingForward,
612 movingLeft ? Inset::ENTRY_DIRECTION_RIGHT : Inset::ENTRY_DIRECTION_LEFT);
617 bool Text::cursorBackward(Cursor & cur)
619 // Tell BufferView to test for FitCursor in any case!
620 cur.screenUpdateFlags(Update::FitCursor);
622 // not at paragraph start?
624 // if on right side of boundary (i.e. not at paragraph end, but line end)
625 // -> skip it, i.e. set boundary to true, i.e. go only logically left
626 // there are some exceptions to ignore this: lineseps, newlines, spaces
628 // some effectless debug code to see the values in the debugger
629 bool bound = cur.boundary();
630 int rowpos = cur.textRow().pos();
632 bool sep = cur.paragraph().isSeparator(cur.pos() - 1);
633 bool newline = cur.paragraph().isNewline(cur.pos() - 1);
634 bool linesep = cur.paragraph().isLineSeparator(cur.pos() - 1);
636 if (!cur.boundary() &&
637 cur.textRow().pos() == cur.pos() &&
638 !cur.paragraph().isLineSeparator(cur.pos() - 1) &&
639 !cur.paragraph().isNewline(cur.pos() - 1) &&
640 !cur.paragraph().isSeparator(cur.pos() - 1)) {
641 return setCursor(cur, cur.pit(), cur.pos(), true, true);
644 // go left and try to enter inset
645 if (checkAndActivateInset(cur, false))
648 // normal character left
649 return setCursor(cur, cur.pit(), cur.pos() - 1, true, false);
652 // move to the previous paragraph or do nothing
654 return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size(), true, false);
659 bool Text::cursorVisLeft(Cursor & cur, bool skip_inset)
661 Cursor temp_cur = cur;
662 temp_cur.posVisLeft(skip_inset);
663 if (temp_cur.depth() > cur.depth()) {
667 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
668 true, temp_cur.boundary());
672 bool Text::cursorVisRight(Cursor & cur, bool skip_inset)
674 Cursor temp_cur = cur;
675 temp_cur.posVisRight(skip_inset);
676 if (temp_cur.depth() > cur.depth()) {
680 return setCursor(cur, temp_cur.pit(), temp_cur.pos(),
681 true, temp_cur.boundary());
685 bool Text::cursorForward(Cursor & cur)
687 // Tell BufferView to test for FitCursor in any case!
688 cur.screenUpdateFlags(Update::FitCursor);
690 // not at paragraph end?
691 if (cur.pos() != cur.lastpos()) {
692 // in front of editable inset, i.e. jump into it?
693 if (checkAndActivateInset(cur, true))
696 TextMetrics const & tm = cur.bv().textMetrics(this);
697 // if left of boundary -> just jump to right side
698 // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
699 if (cur.boundary() && !tm.isRTLBoundary(cur.pit(), cur.pos()))
700 return setCursor(cur, cur.pit(), cur.pos(), true, false);
702 // next position is left of boundary,
703 // but go to next line for special cases like space, newline, linesep
705 // some effectless debug code to see the values in the debugger
706 int endpos = cur.textRow().endpos();
707 int lastpos = cur.lastpos();
709 bool linesep = cur.paragraph().isLineSeparator(cur.pos());
710 bool newline = cur.paragraph().isNewline(cur.pos());
711 bool sep = cur.paragraph().isSeparator(cur.pos());
712 if (cur.pos() != cur.lastpos()) {
713 bool linesep2 = cur.paragraph().isLineSeparator(cur.pos()+1);
714 bool newline2 = cur.paragraph().isNewline(cur.pos()+1);
715 bool sep2 = cur.paragraph().isSeparator(cur.pos()+1);
718 if (cur.textRow().endpos() == cur.pos() + 1 &&
719 cur.textRow().endpos() != cur.lastpos() &&
720 !cur.paragraph().isNewline(cur.pos()) &&
721 !cur.paragraph().isLineSeparator(cur.pos()) &&
722 !cur.paragraph().isSeparator(cur.pos())) {
723 return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
726 // in front of RTL boundary? Stay on this side of the boundary because:
727 // ab|cDDEEFFghi -> abc|DDEEFFghi
728 if (tm.isRTLBoundary(cur.pit(), cur.pos() + 1))
729 return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
732 return setCursor(cur, cur.pit(), cur.pos() + 1, true, false);
735 // move to next paragraph
736 if (cur.pit() != cur.lastpit())
737 return setCursor(cur, cur.pit() + 1, 0, true, false);
742 bool Text::cursorUpParagraph(Cursor & cur)
744 bool updated = false;
746 updated = setCursor(cur, cur.pit(), 0);
747 else if (cur.pit() != 0)
748 updated = setCursor(cur, cur.pit() - 1, 0);
753 bool Text::cursorDownParagraph(Cursor & cur)
755 bool updated = false;
756 if (cur.pit() != cur.lastpit())
757 updated = setCursor(cur, cur.pit() + 1, 0);
759 updated = setCursor(cur, cur.pit(), cur.lastpos());
764 // fix the cursor `cur' after a characters has been deleted at `where'
765 // position. Called by deleteEmptyParagraphMechanism
766 void Text::fixCursorAfterDelete(CursorSlice & cur, CursorSlice const & where)
768 // Do nothing if cursor is not in the paragraph where the
770 if (cur.pit() != where.pit())
773 // If cursor position is after the deletion place update it
774 if (cur.pos() > where.pos())
777 // Check also if we don't want to set the cursor on a spot behind the
778 // pagragraph because we erased the last character.
779 if (cur.pos() > cur.lastpos())
780 cur.pos() = cur.lastpos();
784 bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
785 Cursor & old, bool & need_anchor_change)
787 //LYXERR(Debug::DEBUG, "DEPM: cur:\n" << cur << "old:\n" << old);
789 Paragraph & oldpar = old.paragraph();
791 // We allow all kinds of "mumbo-jumbo" when freespacing.
792 if (oldpar.isFreeSpacing())
795 /* Ok I'll put some comments here about what is missing.
796 There are still some small problems that can lead to
797 double spaces stored in the document file or space at
798 the beginning of paragraphs(). This happens if you have
799 the cursor between to spaces and then save. Or if you
800 cut and paste and the selection have a space at the
801 beginning and then save right after the paste. (Lgb)
804 // If old.pos() == 0 and old.pos()(1) == LineSeparator
805 // delete the LineSeparator.
808 // If old.pos() == 1 and old.pos()(0) == LineSeparator
809 // delete the LineSeparator.
812 // Find a common inset and the corresponding depth.
814 for (; depth < cur.depth(); ++depth)
815 if (&old.inset() == &cur[depth].inset())
818 // Whether a common inset is found and whether the cursor is still in
819 // the same paragraph (possibly nested).
820 bool const same_par = depth < cur.depth() && old.pit() == cur[depth].pit();
821 bool const same_par_pos = depth == cur.depth() - 1 && same_par
822 && old.pos() == cur[depth].pos();
824 // If the chars around the old cursor were spaces, delete one of them.
826 // Only if the cursor has really moved.
828 && old.pos() < oldpar.size()
829 && oldpar.isLineSeparator(old.pos())
830 && oldpar.isLineSeparator(old.pos() - 1)
831 && !oldpar.isDeleted(old.pos() - 1)
832 && !oldpar.isDeleted(old.pos())) {
833 oldpar.eraseChar(old.pos() - 1, cur.buffer()->params().trackChanges);
834 // FIXME: This will not work anymore when we have multiple views of the same buffer
835 // In this case, we will have to correct also the cursors held by
836 // other bufferviews. It will probably be easier to do that in a more
837 // automated way in CursorSlice code. (JMarc 26/09/2001)
838 // correct all cursor parts
840 fixCursorAfterDelete(cur[depth], old.top());
841 need_anchor_change = true;
847 // only do our magic if we changed paragraph
851 // don't delete anything if this is the ONLY paragraph!
852 if (old.lastpit() == 0)
855 // Do not delete empty paragraphs with keepempty set.
856 if (oldpar.allowEmpty())
859 if (oldpar.empty() || (oldpar.size() == 1 && oldpar.isLineSeparator(0))) {
861 old.recordUndo(ATOMIC_UNDO,
862 max(old.pit() - 1, pit_type(0)),
863 min(old.pit() + 1, old.lastpit()));
864 ParagraphList & plist = old.text()->paragraphs();
865 bool const soa = oldpar.params().startOfAppendix();
866 plist.erase(boost::next(plist.begin(), old.pit()));
867 // do not lose start of appendix marker (bug 4212)
868 if (soa && old.pit() < pit_type(plist.size()))
869 plist[old.pit()].params().startOfAppendix(true);
871 // see #warning (FIXME?) above
872 if (cur.depth() >= old.depth()) {
873 CursorSlice & curslice = cur[old.depth() - 1];
874 if (&curslice.inset() == &old.inset()
875 && curslice.pit() > old.pit()) {
877 // since a paragraph has been deleted, all the
878 // insets after `old' have been copied and
879 // their address has changed. Therefore we
880 // need to `regenerate' cur. (JMarc)
881 cur.updateInsets(&(cur.bottom().inset()));
882 need_anchor_change = true;
888 if (oldpar.stripLeadingSpaces(cur.buffer()->params().trackChanges)) {
889 need_anchor_change = true;
890 // We return true here because the Paragraph contents changed and
891 // we need a redraw before further action is processed.
899 void Text::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges)
901 LASSERT(first >= 0 && first <= last && last < (int) pars_.size(), /**/);
903 for (pit_type pit = first; pit <= last; ++pit) {
904 Paragraph & par = pars_[pit];
906 // We allow all kinds of "mumbo-jumbo" when freespacing.
907 if (par.isFreeSpacing())
910 for (pos_type pos = 1; pos < par.size(); ++pos) {
911 if (par.isLineSeparator(pos) && par.isLineSeparator(pos - 1)
912 && !par.isDeleted(pos - 1)) {
913 if (par.eraseChar(pos - 1, trackChanges)) {
919 // don't delete anything if this is the only remaining paragraph
920 // within the given range. Note: Text::acceptOrRejectChanges()
921 // sets the cursor to 'first' after calling DEPM
925 // don't delete empty paragraphs with keepempty set
926 if (par.allowEmpty())
929 if (par.empty() || (par.size() == 1 && par.isLineSeparator(0))) {
930 pars_.erase(boost::next(pars_.begin(), pit));
936 par.stripLeadingSpaces(trackChanges);
941 void Text::recUndo(Cursor & cur, pit_type first, pit_type last) const
943 cur.recordUndo(ATOMIC_UNDO, first, last);
947 void Text::recUndo(Cursor & cur, pit_type par) const
949 cur.recordUndo(ATOMIC_UNDO, par, par);