From 11b7b84d1f48479cd96a2d2f1ce94b29a91d0eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Mon, 9 Oct 2006 09:52:07 +0000 Subject: [PATCH] Fix bug 2744: * buffer.[Ch] (changeRefsIfUnique): extend to handle bibitems as well (the function takes a InsetCode argument now) [bug 2744]; clean up by using InsetIterator. * math_hullinset.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/branches/BRANCH_1_4_X@15282 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 6 ++++++ src/buffer.C | 37 ++++++++++++++++++------------------- src/buffer.h | 2 +- src/insets/ChangeLog | 12 ++++++++++++ src/insets/insetbibitem.C | 10 +++++++--- src/insets/insetcite.C | 16 ++++++++++++++++ src/insets/insetcite.h | 2 ++ src/insets/insetcommand.C | 7 +++++++ src/insets/insetcommand.h | 2 ++ src/insets/insetlabel.C | 2 +- src/mathed/ChangeLog | 5 +++++ src/mathed/math_hullinset.C | 2 +- status.14x | 2 ++ 13 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 2158957a7a..48aa6302b3 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2006-10-09 Jürgen Spitzmüller + + * buffer.[Ch] (changeRefsIfUnique): extend to handle bibitems + as well (the function takes a InsetCode argument now) [bug 2744]; + clean up by using InsetIterator. + 2006-09-21 Jean-Marc Lasgouttes * lyxfunc.C (getStatus): fix handling of LFUN_RUNCHKTEX (bug 2831) diff --git a/src/buffer.C b/src/buffer.C index 0bbf3aa77a..92f0912b17 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -67,7 +67,7 @@ #include "support/lyxalgo.h" #include "support/filetools.h" #include "support/fs_extras.h" -# include "support/gzstream.h" +#include "support/gzstream.h" #include "support/lyxlib.h" #include "support/os.h" #include "support/path.h" @@ -1612,31 +1612,30 @@ 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) { + 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 00fe1f3b12..506b956a7b 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -343,7 +343,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); private: /** Inserts a file into a document diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 2eb66f867e..46a22c1c72 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,15 @@ +2006-10-09 Jürgen Spitzmüller + + * 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. + 2006-10-03 Jürgen Spitzmüller * insetvspace.C (doDispatch): open dialog on mouse release, diff --git a/src/insets/insetbibitem.C b/src/insets/insetbibitem.C index 4e1c5979f3..713b107150 100644 --- a/src/insets/insetbibitem.C +++ b/src/insets/insetbibitem.C @@ -61,10 +61,14 @@ void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd) case LFUN_INSET_MODIFY: { InsetCommandParams p; InsetCommandMailer::string2params("bibitem", cmd.argument, p); - if (!p.getCmdName().empty()) - setParams(p); - else + 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); break; } diff --git a/src/insets/insetcite.C b/src/insets/insetcite.C index bcc8e1cd7d..373884f2d0 100644 --- a/src/insets/insetcite.C +++ b/src/insets/insetcite.C @@ -28,14 +28,19 @@ #include +#include + using lyx::support::ascii_lowercase; using lyx::support::contains; using lyx::support::getVectorFromString; +using lyx::support::getStringFromVector; 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; @@ -430,3 +435,14 @@ 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 ff815add1f..d18e6261ca 100644 --- a/src/insets/insetcommand.C +++ b/src/insets/insetcommand.C @@ -156,6 +156,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 902ad19bda..10b038ea1e 100644 --- a/src/insets/insetcommand.h +++ b/src/insets/insetcommand.h @@ -72,6 +72,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 9d1264dbe0..4bcd62e86e 100644 --- a/src/insets/insetlabel.C +++ b/src/insets/insetlabel.C @@ -71,7 +71,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/ChangeLog b/src/mathed/ChangeLog index f30d66fd4a..3b16171565 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,8 @@ +2006-10-09 Jürgen Spitzmüller + + * math_hullinset.C (doDispatch): changeRefsIfUnique needs a + InsetCode argument now (bug 2744). + 2006-09-25 Enrico Forestieri * math_symbolinset.C (maxima): newer maxima versions use inf diff --git a/src/mathed/math_hullinset.C b/src/mathed/math_hullinset.C index 52f8fef45f..a6e84e5765 100644 --- a/src/mathed/math_hullinset.C +++ b/src/mathed/math_hullinset.C @@ -1078,7 +1078,7 @@ void MathHullInset::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; diff --git a/status.14x b/status.14x index d5aa99ad9e..eb6358b9b4 100644 --- a/status.14x +++ b/status.14x @@ -40,6 +40,8 @@ What's new - Support lgathered and rgathered math environments. +- Update bibliography references when the entry has been changed (bug 2744). + * User Interface: - Fix a crash that occured on exit if the clipboard was not empty (only on