commit 7760c5ccf2c558e48a54c101b779d0be25d7e7ba
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Thu May 21 16:49:19 2015 +0200

    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

diff --git a/src/Undo.cpp b/src/Undo.cpp
index 1ab11d3..d7bcd75 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 95514ab..c2acf4a 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 8898cd9..f40666c 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()));

Reply via email to