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
This commit is contained in:
Jean-Marc Lasgouttes 2015-05-21 16:49:19 +02:00
parent c444fddc0b
commit 7760c5ccf2
3 changed files with 47 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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()));