diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index c23cee2b06..9a9cb31ddd 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -14,6 +14,7 @@ #include "InsetRef.h" +#include "buffer_funcs.h" #include "Buffer.h" #include "BufferView.h" #include "DispatchResult.h" @@ -26,6 +27,7 @@ #include "frontends/alert.h" +#include "support/convert.h" #include "support/lyxalgo.h" #include "support/gettext.h" #include "support/lstrings.h" @@ -41,6 +43,38 @@ InsetLabel::InsetLabel(InsetCommandParams const & p) {} +void InsetLabel::update(docstring const & new_label) +{ + docstring const old_label = getParam("name"); + if (old_label == new_label) + return; + + docstring label = new_label; + int i = 0; + while (buffer().insetLabel(label)) { + label = new_label + '-' + convert(i); + ++i; + } + setParam("name", label); + + Buffer::References const & refs = buffer().references(old_label); + Buffer::References::const_iterator it = refs.begin(); + Buffer::References::const_iterator end = refs.end(); + for (; it != end; ++it) + it->first->setParam("reference", label); + + if (label != new_label) { + // Warn the user that the label has been changed to something else. + frontend::Alert::warning(_("Label names must be unique!"), + bformat(_("The label %1$s already exists,\n" + "it will been changed to %2$s."), new_label, label)); + } + // We need an update of the Buffer reference cache. This is achieved by + // updateLabel(). + lyx::updateLabels(buffer()); +} + + ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */) { static ParamInfo param_info_; @@ -104,10 +138,7 @@ void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd) cur.noUpdate(); break; } - if (p["name"] != params()["name"]) - cur.bv().buffer().changeRefsIfUnique(params()["name"], - p["name"], REF_CODE); - setParams(p); + update(p["name"]); break; } diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index 13284c8db2..e9e2669c10 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -52,6 +52,8 @@ protected: private: /// Inset * clone() const { return new InsetLabel(*this); } + /// + void update(docstring const & new_label); };