]> git.lyx.org Git - features.git/commitdiff
Fix bug 2744:
authorJürgen Spitzmüller <spitz@lyx.org>
Mon, 9 Oct 2006 14:21:11 +0000 (14:21 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Mon, 9 Oct 2006 14:21:11 +0000 (14:21 +0000)
* 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
src/buffer.h
src/insets/insetbibitem.C
src/insets/insetcite.C
src/insets/insetcite.h
src/insets/insetcommand.C
src/insets/insetcommand.h
src/insets/insetlabel.C
src/mathed/InsetMathHull.C

index 59677959ca2ce303b5485882597e182a63b4d436..aab0435001bf49d21304de46593753307d282598 100644 (file)
@@ -68,9 +68,9 @@
 #include "support/lyxalgo.h"
 #include "support/filetools.h"
 #include "support/fs_extras.h"
-# include <boost/iostreams/filtering_stream.hpp>
-# include <boost/iostreams/filter/gzip.hpp>
-# include <boost/iostreams/device/file.hpp>
+#include <boost/iostreams/filtering_stream.hpp>
+#include <boost/iostreams/filter/gzip.hpp>
+#include <boost/iostreams/device/file.hpp>
 namespace io = boost::iostreams;
 #include "support/lyxlib.h"
 #include "support/os.h"
@@ -83,9 +83,9 @@ namespace io = boost::iostreams;
 #include <boost/filesystem/operations.hpp>
 
 #if defined (HAVE_UTIME_H)
-# include <utime.h>
+#include <utime.h>
 #elif defined (HAVE_SYS_UTIME_H)
-# include <sys/utime.h>
+#include <sys/utime.h>
 #endif
 
 #include <iomanip>
@@ -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<string> labels;
-       getLabelList(labels);
+
+       if (code == InsetBase::CITE_CODE) {
+               vector<pair<string, string> > keys;
+               fillWithBibKeys(keys);
+               vector<pair<string, string> >::const_iterator bit  = keys.begin();
+               vector<pair<string, string> >::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<InsetCommand *>(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<InsetCommand &>(*it);
+                       inset.replaceContents(from, to);
                }
        }
 }
index 2c52c7b04cb4d6e991a57243aa8bf4fd104fed38..4cab9b814e9bbd0452f37a487751a07ea94f463e 100644 (file)
@@ -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);
index 4cb361ca183e68b99bc748399b4b4ff1e855240b..294839b6fd683fdf94e4b2a2548d91f4e8e2bc2d 100644 (file)
@@ -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:
index 566b7661ad0f5a1ff2fcadb290931a32b7181b0a..7cf1151a5a1a9e45efd779b458104d6bd4072de7 100644 (file)
 #include "support/fs_extras.h"
 #include "support/lstrings.h"
 
+#include <algorithm>
+
 #include <boost/filesystem/operations.hpp>
 #include <boost/filesystem/exception.hpp>
 
 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<string> items = getVectorFromString(getContents());
+               replace(items.begin(), items.end(), from, to);
+               setContents(getStringFromVector(items));
+       }
+}
index 0733e93486ea60f3271797bf90698991ee2e62bf..c5bc0d207c0ac428cbde3497be1a9dba4a98e22d 100644 (file)
@@ -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<InsetBase> doClone() const
index 5a2f101c05a151925cfd8d19b0a0a45ca74e7f7a..272a9c1af8d3c31bfc499d6468de26b8e089fba5 100644 (file)
@@ -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)
index 61f6bb80c1bd87396a4c0d86d709157d833a2422..beb0c6a5562e7a2c68da08ce7b5084f71028fc15 100644 (file)
@@ -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(); }
index dcbf6cbd7f56e81ce104e3b2597c886abc3ea84e..b176327ca27464b6ba8512a3ebba0a5fabd83209 100644 (file)
@@ -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;
        }
index 1a3a85fe51e9f570c7cf3c3b4bec41e6ff729e36..547ad9a108160555f353aee0717b88db7bbd120d 100644 (file)
@@ -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;