From 7760c5ccf2c558e48a54c101b779d0be25d7e7ba Mon Sep 17 00:00:00 2001 From: Jean-Marc Lasgouttes Date: Thu, 21 May 2015 16:49:19 +0200 Subject: [PATCH] Create proper undo groups for advanced find and replace Create new helper class UndoGroupHelper, which simplifies a lot the handling of undo groups in cases like this one. The class tracks open undo buffers and allows to switch buffers transparently. Using the class for advanced search and replace is trivial. THe class may be useful in some other classes. Fixes ticket #8658 --- src/Undo.cpp | 13 +++++++++++++ src/Undo.h | 28 ++++++++++++++++++++++++++++ src/frontends/qt4/FindAndReplace.cpp | 6 ++++++ 3 files changed, 47 insertions(+) diff --git a/src/Undo.cpp b/src/Undo.cpp index 1ab11d3774..d7bcd75ea0 100644 --- a/src/Undo.cpp +++ b/src/Undo.cpp @@ -617,5 +617,18 @@ void Undo::recordUndoFullBuffer(CursorData const & cur) endUndoGroup(); } +/// UndoGroupHelper class stuff + +void UndoGroupHelper::resetBuffer(Buffer * buf) +{ + if (buf == buffer_) + return; + if (buffer_) + buffer_->undo().endUndoGroup(); + buffer_ = buf; + if (buffer_) + buffer_->undo().beginUndoGroup(); +} + } // namespace lyx diff --git a/src/Undo.h b/src/Undo.h index 95514ab0b2..c2acf4af21 100644 --- a/src/Undo.h +++ b/src/Undo.h @@ -117,6 +117,34 @@ private: }; +/** Helper class to simplify the use of undo groups across several buffers. + * + * The undo group is created when the object is instanciated; it is + * then ended as the object goes out of scope or the buffer is reset + * (see below) + */ +class UndoGroupHelper { +public: + UndoGroupHelper(Buffer * buf) : buffer_(0) + { + resetBuffer(buf); + } + + ~UndoGroupHelper() + { + resetBuffer(0); + } + + /** Close the current undo group if necessary and create a new one + * for buffer \c buf. + */ + void resetBuffer(Buffer * buf); + +private: + Buffer * buffer_; +}; + + } // namespace lyx diff --git a/src/frontends/qt4/FindAndReplace.cpp b/src/frontends/qt4/FindAndReplace.cpp index 8898cd975d..f40666c39b 100644 --- a/src/frontends/qt4/FindAndReplace.cpp +++ b/src/frontends/qt4/FindAndReplace.cpp @@ -340,6 +340,8 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool } } + UndoGroupHelper helper(buf); + do { LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV"); dispatch(cmd); @@ -384,6 +386,9 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool if (buf != &view_.documentBufferView()->buffer()) lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH, buf->absFileName())); + + helper.resetBuffer(buf); + bv = view_.documentBufferView(); if (opt.forward) { bv->cursor().clear(); @@ -397,6 +402,7 @@ bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool } bv->clearSelection(); } while (wrap_answer != 1); + if (buf_orig != &view_.documentBufferView()->buffer()) lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH, buf_orig->absFileName()));