From 05be07de9e0d89bea3ad9c9be37d39b3557ebe33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Mon, 9 Oct 2006 14:21:11 +0000 Subject: [PATCH] Fix bug 2744: * buffer.[Ch] (changeRefsIfUnique): extend to handle bibitems as well (the function takes a InsetCode argument now); clean up by using InsetIterator. * InsetMathHull.C (doDispatch): changeRefsIfUnique needs a InsetCode argument now (bug 2744). * insetlabel (doDispatch): changeRefsIfUnique needs a InsetCode argument now. * insetbibitem (doDispatch): use changeRefsIfUnique (actual fix for bug 2744). * insetcommand.[Ch]: * insetcite.[Ch]: implement replaceContents, which is used by changeRefsIfUnique. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15289 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/buffer.C | 46 +++++++++++++++++++------------------- src/buffer.h | 2 +- src/insets/insetbibitem.C | 14 +++++++----- src/insets/insetcite.C | 15 +++++++++++++ src/insets/insetcite.h | 2 ++ src/insets/insetcommand.C | 7 ++++++ src/insets/insetcommand.h | 2 ++ src/insets/insetlabel.C | 2 +- src/mathed/InsetMathHull.C | 3 ++- 9 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/buffer.C b/src/buffer.C index 59677959ca..aab0435001 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -68,9 +68,9 @@ #include "support/lyxalgo.h" #include "support/filetools.h" #include "support/fs_extras.h" -# include -# include -# include +#include +#include +#include namespace io = boost::iostreams; #include "support/lyxlib.h" #include "support/os.h" @@ -83,9 +83,9 @@ namespace io = boost::iostreams; #include #if defined (HAVE_UTIME_H) -# include +#include #elif defined (HAVE_SYS_UTIME_H) -# include +#include #endif #include @@ -1556,31 +1556,31 @@ void Buffer::saveCursor(StableDocIterator cur, StableDocIterator anc) } -void Buffer::changeRefsIfUnique(string const & from, string const & to) +void Buffer::changeRefsIfUnique(string const & from, string const & to, InsetBase::Code code) { + //FIXME: This does not work for child documents yet. + BOOST_ASSERT(code == InsetBase::CITE_CODE || code == InsetBase::REF_CODE); // Check if the label 'from' appears more than once vector labels; - getLabelList(labels); + + if (code == InsetBase::CITE_CODE) { + vector > keys; + fillWithBibKeys(keys); + vector >::const_iterator bit = keys.begin(); + vector >::const_iterator bend = keys.end(); + + for (; bit != bend; ++bit) + labels.push_back(bit->first); + } else + getLabelList(labels); if (lyx::count(labels.begin(), labels.end(), from) > 1) return; - InsetBase::Code code = InsetBase::REF_CODE; - - ParIterator it = par_iterator_begin(); - ParIterator end = par_iterator_end(); - for ( ; it != end; ++it) { - bool changed_inset = false; - for (InsetList::iterator it2 = it->insetlist.begin(); - it2 != it->insetlist.end(); ++it2) { - if (it2->inset->lyxCode() == code) { - InsetCommand * inset = static_cast(it2->inset); - if (inset->getContents() == from) { - inset->setContents(to); - //inset->setButtonLabel(); - changed_inset = true; - } - } + for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { + if (it->lyxCode() == code) { + InsetCommand & inset = dynamic_cast(*it); + inset.replaceContents(from, to); } } } diff --git a/src/buffer.h b/src/buffer.h index 2c52c7b04c..4cab9b814e 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -340,7 +340,7 @@ public: /// StableDocIterator getAnchor() const { return anchor_; } /// - void changeRefsIfUnique(std::string const & from, std::string const & to); + void changeRefsIfUnique(std::string const & from, std::string const & to, InsetBase::Code code); /// get source code (latex/docbook) for some paragraphs, or all paragraphs /// including preamble void getSourceCode(std::ostream & os, lyx::pit_type par_begin, lyx::pit_type par_end, bool full_source); diff --git a/src/insets/insetbibitem.C b/src/insets/insetbibitem.C index 4cb361ca18..294839b6fd 100644 --- a/src/insets/insetbibitem.C +++ b/src/insets/insetbibitem.C @@ -13,6 +13,7 @@ #include "insetbibitem.h" #include "buffer.h" +#include "BufferView.h" #include "dispatchresult.h" #include "funcrequest.h" #include "lyxfont.h" @@ -63,11 +64,14 @@ void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd) case LFUN_INSET_MODIFY: { InsetCommandParams p; InsetCommandMailer::string2params("bibitem", lyx::to_utf8(cmd.argument()), p); - if (!p.getCmdName().empty()) - setParams(p); - else - cur.noUpdate(); - break; + if (p.getCmdName().empty()) { + cur.noUpdate(); + break; + } + if (p.getContents() != params().getContents()) + cur.bv().buffer()->changeRefsIfUnique(params().getContents(), + p.getContents(), InsetBase::CITE_CODE); + setParams(p); } default: diff --git a/src/insets/insetcite.C b/src/insets/insetcite.C index 566b7661ad..7cf1151a5a 100644 --- a/src/insets/insetcite.C +++ b/src/insets/insetcite.C @@ -25,17 +25,22 @@ #include "support/fs_extras.h" #include "support/lstrings.h" +#include + #include #include using lyx::support::ascii_lowercase; using lyx::support::contains; +using lyx::support::getStringFromVector; using lyx::support::getVectorFromString; using lyx::support::ltrim; using lyx::support::rtrim; using lyx::support::split; +using lyx::support::tokenPos; using std::endl; +using std::replace; using std::string; using std::ostream; using std::vector; @@ -437,3 +442,13 @@ void InsetCitation::validate(LaTeXFeatures & features) const break; } } + + +void InsetCitation::replaceContents(string const & from, string const & to) +{ + if (tokenPos(getContents(), ',', from) != -1) { + vector items = getVectorFromString(getContents()); + replace(items.begin(), items.end(), from, to); + setContents(getStringFromVector(items)); + } +} diff --git a/src/insets/insetcite.h b/src/insets/insetcite.h index 0733e93486..c5bc0d207c 100644 --- a/src/insets/insetcite.h +++ b/src/insets/insetcite.h @@ -43,6 +43,8 @@ public: OutputParams const &) const; /// void validate(LaTeXFeatures &) const; + /// + void replaceContents(std::string const & from, std::string const & to); private: virtual std::auto_ptr doClone() const diff --git a/src/insets/insetcommand.C b/src/insets/insetcommand.C index 5a2f101c05..272a9c1af8 100644 --- a/src/insets/insetcommand.C +++ b/src/insets/insetcommand.C @@ -149,6 +149,13 @@ bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd, } +void InsetCommand::replaceContents(std::string const & from, string const & to) +{ + if (getContents() == from) + setContents(to); +} + + InsetCommandMailer::InsetCommandMailer(string const & name, InsetCommand & inset) : name_(name), inset_(inset) diff --git a/src/insets/insetcommand.h b/src/insets/insetcommand.h index 61f6bb80c1..beb0c6a556 100644 --- a/src/insets/insetcommand.h +++ b/src/insets/insetcommand.h @@ -69,6 +69,8 @@ public: p_.setContents(c); } /// + virtual void replaceContents(std::string const & from, std::string const & to); + /// std::string const & getOptions() const { return p_.getOptions(); } /// std::string const & getSecOptions() const { return p_.getSecOptions(); } diff --git a/src/insets/insetlabel.C b/src/insets/insetlabel.C index dcbf6cbd7f..b176327ca2 100644 --- a/src/insets/insetlabel.C +++ b/src/insets/insetlabel.C @@ -69,7 +69,7 @@ void InsetLabel::doDispatch(LCursor & cur, FuncRequest & cmd) } if (p.getContents() != params().getContents()) cur.bv().buffer()->changeRefsIfUnique(params().getContents(), - p.getContents()); + p.getContents(), InsetBase::REF_CODE); setParams(p); break; } diff --git a/src/mathed/InsetMathHull.C b/src/mathed/InsetMathHull.C index 1a3a85fe51..547ad9a108 100644 --- a/src/mathed/InsetMathHull.C +++ b/src/mathed/InsetMathHull.C @@ -1121,7 +1121,8 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd) numbered(r, true); string old = label(r); if (str != old) { - cur.bv().buffer()->changeRefsIfUnique(old, str); + cur.bv().buffer()->changeRefsIfUnique(old, str, + InsetBase::REF_CODE); label(r, str); } break; -- 2.39.2