From f4cc84d5a611fb99063540a57ac6f8c9353651a2 Mon Sep 17 00:00:00 2001 From: Michael Schmitt Date: Thu, 25 Jan 2007 21:18:36 +0000 Subject: [PATCH] * src/lyxtext.h: add enum ChangeOp (ACCEPT/REJECT); add deleteEmptyParagraphMechanism() * src/text.C: * src/text3.C: * src/BufferView.C: adjust * src/insets/insettext.C: acceptChanges() / rejectChanges(): fix pars_size bug introduced in previous commit; call deleteEmptyParagraphMechanism() * src/text2.C: track changes in DEPM (I am not sure about whether this is actually useful; time will tell..); add deleteEmptyParagraphMechanism(pit_type, pit_type, bool) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16858 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.C | 4 ++-- src/insets/insettext.C | 30 ++++++++++++++---------------- src/lyxtext.h | 13 +++++++++++-- src/text.C | 18 +++++++----------- src/text2.C | 40 +++++++++++++++++++++++++++++++++++++++- src/text3.C | 4 ++-- 6 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/BufferView.C b/src/BufferView.C index 926fb0b9c0..f63a617a9f 100644 --- a/src/BufferView.C +++ b/src/BufferView.C @@ -862,7 +862,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd) #warning FIXME changes #endif while (findNextChange(this)) - getLyXText()->acceptOrRejectChange(cursor_, true); + getLyXText()->acceptOrRejectChange(cursor_, LyXText::ChangeOp::ACCEPT); break; } @@ -872,7 +872,7 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd) #warning FIXME changes #endif while (findNextChange(this)) - getLyXText()->acceptOrRejectChange(cursor_, false); + getLyXText()->acceptOrRejectChange(cursor_, LyXText::ChangeOp::REJECT); break; } diff --git a/src/insets/insettext.C b/src/insets/insettext.C index d752aeff1e..d6f8f8ebb2 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -280,14 +280,13 @@ void InsetText::setChange(Change const & change) void InsetText::acceptChanges(BufferParams const & bparams) { ParagraphList & pars = paragraphs(); - pit_type const pars_size = (pit_type) pars.size(); + pit_type pars_size = (pit_type) pars.size(); // first, accept changes within each individual paragraph // (do not consider end-of-par) for (pit_type pit = 0; pit < pars_size; ++pit) { - if (pars[pit].empty()) // prevent assertion failure - continue; - pars[pit].acceptChanges(bparams, 0, pars[pit].size()); + if (!pars[pit].empty()) // prevent assertion failure + pars[pit].acceptChanges(bparams, 0, pars[pit].size()); } // next, accept imaginary end-of-par characters @@ -304,27 +303,26 @@ void InsetText::acceptChanges(BufferParams const & bparams) } else { mergeParagraph(bparams, pars, pit); --pit; + --pars_size; } } } - // FIXME: finally, invoke the DEPM - // This cannot be done here but at a higher calling level - // because we need BufferView::checkDepm(). + // finally, invoke the DEPM + text_.deleteEmptyParagraphMechanism(0, pars.size() - 1, bparams.trackChanges); } void InsetText::rejectChanges(BufferParams const & bparams) { ParagraphList & pars = paragraphs(); - pit_type const pars_size = (pit_type) pars.size(); + pit_type pars_size = (pit_type) pars.size(); - // first, reject changes within each individual paragraph (do not - // consider end-of-par) + // first, reject changes within each individual paragraph + // (do not consider end-of-par) for (pit_type pit = 0; pit < pars_size; ++pit) { - if (pars[pit].empty()) // prevent assertion failure - continue; - pars[pit].rejectChanges(bparams, 0, pars[pit].size()); + if (!pars[pit].empty()) // prevent assertion failure + pars[pit].rejectChanges(bparams, 0, pars[pit].size()); } // next, reject imaginary end-of-par characters @@ -341,13 +339,13 @@ void InsetText::rejectChanges(BufferParams const & bparams) } else { mergeParagraph(bparams, pars, pit); --pit; + --pars_size; } } } - // FIXME: finally, invoke the DEPM - // This cannot be done here but at a higher calling level - // because we need BufferView::checkDepm(). + // finally, invoke the DEPM + text_.deleteEmptyParagraphMechanism(0, pars.size() - 1, bparams.trackChanges); } diff --git a/src/lyxtext.h b/src/lyxtext.h index 9074b85828..55448a1a5d 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -153,8 +153,13 @@ public: /// just selects the word the cursor is in void selectWord(LCursor & cur, word_location loc); + /// what type of change operation to make + enum ChangeOp { + ACCEPT, + REJECT + }; /// accept or reject the selected change - void acceptOrRejectChange(LCursor & cur, bool accept); + void acceptOrRejectChange(LCursor & cur, ChangeOp op); /// returns true if par was empty and was removed bool setCursor(LCursor & cur, pit_type par, pos_type pos, @@ -336,13 +341,17 @@ public: int cursorY(BufferView const & bv, CursorSlice const & cursor, bool boundary) const; - /// delete double space or empty paragraphs around old cursor. + /// delete double spaces, leading spaces, and empty paragraphs around old cursor. /// \retval true if a change has happened and we need a redraw. /// FIXME: replace LCursor with DocIterator. This is not possible right /// now because recordUndo() is called which needs a LCursor. bool deleteEmptyParagraphMechanism(LCursor & cur, LCursor & old, bool & need_anchor_change); + /// delete double spaces, leading spaces, and empty paragraphs + /// from \first to \last paragraph + void deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges); + public: /// the current font settings LyXFont current_font; diff --git a/src/text.C b/src/text.C index 93b31bbec3..a7872c70cf 100644 --- a/src/text.C +++ b/src/text.C @@ -845,7 +845,7 @@ bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc) } -void LyXText::acceptOrRejectChange(LCursor & cur, bool accept) +void LyXText::acceptOrRejectChange(LCursor & cur, ChangeOp op) { BOOST_ASSERT(this == cur.text()); @@ -884,7 +884,7 @@ void LyXText::acceptOrRejectChange(LCursor & cur, bool accept) pos_type left = (pit == begPit ? begPos : 0); pos_type right = (pit == endPit ? endPos : parSize); - if (accept) { + if (op == ACCEPT) { pars_[pit].acceptChanges(cur.buffer().params(), left, right); } else { pars_[pit].rejectChanges(cur.buffer().params(), left, right); @@ -905,7 +905,7 @@ void LyXText::acceptOrRejectChange(LCursor & cur, bool accept) if (pit == endPit && pit != pars_.size() - 1) break; // last iteration anway - if (accept) { + if (op == ACCEPT) { if (pars_[pit].isInserted(pos)) { pars_[pit].setChange(pos, Change(Change::UNCHANGED)); } else if (pars_[pit].isDeleted(pos)) { @@ -936,14 +936,10 @@ void LyXText::acceptOrRejectChange(LCursor & cur, bool accept) } // finally, invoke the DEPM - // FIXME: the following code will be changed in the near future - setCursorIntern(cur, endPit, 0); - for (pit_type pit = endPit - 1; pit >= begPit; --pit) { - bool dummy; - LCursor old = cur; - setCursorIntern(cur, pit, 0); - deleteEmptyParagraphMechanism(cur, old, dummy); - } + + deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer().params().trackChanges); + + // finishUndo(); cur.clearSelection(); diff --git a/src/text2.C b/src/text2.C index 3dfaa49436..a447565c38 100644 --- a/src/text2.C +++ b/src/text2.C @@ -1180,7 +1180,7 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, && oldpar.isLineSeparator(old.pos()) && oldpar.isLineSeparator(old.pos() - 1) && !oldpar.isDeleted(old.pos() - 1)) { - oldpar.eraseChar(old.pos() - 1, false); // do not track changes in DEPM + oldpar.eraseChar(old.pos() - 1, cur.buffer().params().trackChanges); #ifdef WITH_WARNINGS #warning This will not work anymore when we have multiple views of the same buffer // In this case, we will have to correct also the cursors held by @@ -1244,6 +1244,44 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, } +void LyXText::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool trackChanges) +{ + for (pit_type pit = first; pit <= last; ++pit) { + Paragraph & par = pars_[pit]; + + // We allow all kinds of "mumbo-jumbo" when freespacing. + if (par.isFreeSpacing()) + continue; + + for (pos_type pos = 1; pos < par.size(); ++pos) { + if (par.isLineSeparator(pos) && par.isLineSeparator(pos - 1) + && !par.isDeleted(pos - 1)) { + if (par.eraseChar(pos - 1, trackChanges)) { + --pos; + } + } + } + + // don't delete anything if this is the ONLY paragraph + if (pars_.size() == 1) + continue; + + // don't delete empty paragraphs with keepempty set + if (par.allowEmpty()) + continue; + + if (par.empty() || (par.size() == 1 && par.isLineSeparator(0))) { + pars_.erase(boost::next(pars_.begin(), pit)); + --pit; + --last; + continue; + } + + par.stripLeadingSpaces(trackChanges); + } +} + + void LyXText::recUndo(LCursor & cur, pit_type first, pit_type last) const { recordUndo(cur, Undo::ATOMIC, first, last); diff --git a/src/text3.C b/src/text3.C index d874ed0719..d02e4e7540 100644 --- a/src/text3.C +++ b/src/text3.C @@ -1429,12 +1429,12 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd) } case LFUN_CHANGE_ACCEPT: { - acceptOrRejectChange(cur, true); + acceptOrRejectChange(cur, ACCEPT); break; } case LFUN_CHANGE_REJECT: { - acceptOrRejectChange(cur, false); + acceptOrRejectChange(cur, REJECT); break; } -- 2.39.5