From 808dfaabeae9803bd00db673abc38b17af4ce5b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Sat, 11 Jul 2009 08:18:26 +0000 Subject: [PATCH] * Buffer.{cpp,h}: - rewrite LFUN_BRANCHES_RENAME with an InsetIterator approach. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@30464 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 79 ++++++++++++++++++-------------------------------- src/Buffer.h | 2 -- 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index ae94c4a554..32cfcf296d 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1707,9 +1707,37 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr) } case LFUN_BRANCHES_RENAME: { + if (func.argument().empty()) + break; + docstring const oldname = from_utf8(func.getArg(0)); docstring const newname = from_utf8(func.getArg(1)); - renameBranches(oldname, newname); + InsetIterator it = inset_iterator_begin(inset()); + InsetIterator const end = inset_iterator_end(inset()); + bool success = false; + for (; it != end; ++it) { + if (it->lyxCode() == BRANCH_CODE) { + InsetBranch & ins = static_cast(*it); + if (ins.branch() == oldname) { + undo().recordUndo(it); + ins.rename(newname); + success = true; + continue; + } + } + if (it->lyxCode() == INCLUDE_CODE) { + // get buffer of external file + InsetInclude const & ins = + static_cast(*it); + Buffer * child = ins.getChildBuffer(); + if (!child) + continue; + child->dispatch(func, dr); + } + } + + if (success) + dr.update(Update::Force); break; } @@ -2413,55 +2441,6 @@ void Buffer::getUsedBranches(std::list & result, bool const from_mast } -void Buffer::renameBranches(docstring const & oldname, docstring const & newname) -{ - // Iterate over buffer, starting with first paragraph - // The scope must be bigger than any lookup DocIterator - // later. For the global lookup, lastpit+1 is used, hence - // we use lastpit+2 here. - DocIterator it = par_iterator_begin(); - DocIterator scope = it; - scope.pit() = scope.lastpit() + 2; - pit_type lastpit = it.lastpit(); - - while (it.pit() <= lastpit) { - Paragraph & par = it.paragraph(); - - // iterate over the insets of the current paragraph - InsetList const & insets = par.insetList(); - InsetList::const_iterator iit = insets.begin(); - InsetList::const_iterator end = insets.end(); - for (; iit != end; ++iit) { - it.pos() = iit->pos; - - if (iit->inset->lyxCode() == BRANCH_CODE) { - // get buffer of external file - InsetBranch & br = - static_cast(*iit->inset); - undo().recordUndo(it); - if (br.branch() == oldname) - br.rename(newname); - continue; - } - - // is it an external file? - if (iit->inset->lyxCode() == INCLUDE_CODE) { - // get buffer of external file - InsetInclude const & inset = - static_cast(*iit->inset); - Buffer * child = inset.getChildBuffer(); - if (!child) - continue; - child->renameBranches(oldname, newname); - } - } - // next paragraph - it.pit()++; - it.pos() = 0; - } -} - - void Buffer::updateMacroInstances() const { LYXERR(Debug::MACROS, "updateMacroInstances for " diff --git a/src/Buffer.h b/src/Buffer.h index 012f73bf67..71e8ac32c1 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -509,8 +509,6 @@ public: /// return a list of all used branches (also in children) void getUsedBranches(std::list &, bool const from_master = false) const; - /// rename all branches of \p oldname in the buffer to \p newname. - void renameBranches(docstring const & oldname, docstring const & newname); /// sets the buffer_ member for every inset in this buffer. // FIXME This really shouldn't be needed, but at the moment it's not -- 2.39.5