InsetIterator const i_end = inset_iterator_end(in);
for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
// Even though this will also be done later, it has to be done here
- // since, e.g., InsetLabel::updateCommand() is going to try to access
+ // since some inset might going to try to access
// the buffer() member.
it->setBuffer(const_cast<Buffer &>(buffer));
switch (it->lyxCode()) {
continue;
InsetLabel * lab = labels[i];
docstring const oldname = lab->getParam("name");
- lab->updateCommand(oldname, false);
+ lab->updateLabel(oldname);
// We need to update the buffer reference cache.
cur.forceBufferUpdate();
docstring const newname = lab->getParam("name");
// check for duplicates
InsetLabel & lab = static_cast<InsetLabel &>(*it);
docstring const oldname = lab.getParam("name");
- lab.updateCommand(oldname, false);
+ lab.updateLabel(oldname);
// We need to update the buffer reference cache.
cur.forceBufferUpdate();
docstring const newname = lab.getParam("name");
}
if (new_label != old_label) {
- label_->updateCommand(new_label);
+ label_->updateLabelAndRefs(new_label, &cur);
// the label might have been adapted (duplicate)
if (new_label != label_->getParam("name")) {
new_params.addParam("label", "{" +
return;
docstring old_label = label_->getParam("name");
- label_->updateCommand(old_label, false);
+ label_->updateLabel(old_label);
// the label might have been adapted (duplicate)
docstring new_label = label_->getParam("name");
if (old_label == new_label)
setParams(p);
}
+
void InsetInclude::updateBuffer(ParIterator const & it, UpdateType utype)
{
Buffer const * const childbuffer = getChildBuffer();
void InsetLabel::initView()
{
- updateCommand(getParam("name"));
+ // FIXME: This seems to be used only for inset creation so
+ // we probably just need to call updateLabel() here.
+ updateLabelAndRefs(getParam("name"));
}
-void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
+void InsetLabel::uniqueLabel(docstring & label) const
{
- docstring const old_label = getParam("name");
- docstring label = new_label;
+ docstring const new_label = label;
int i = 1;
while (buffer().insetLabel(label)) {
label = new_label + '-' + convert<docstring>(i);
++i;
}
-
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 be changed to %2$s."), new_label, label));
}
+}
+
+
+void InsetLabel::updateLabel(docstring const & new_label)
+{
+ docstring label = new_label;
+ uniqueLabel(label);
+ setParam("name", label);
+}
+
+
+void InsetLabel::updateLabelAndRefs(docstring const & new_label,
+ Cursor * cursor)
+{
+ docstring const old_label = getParam("name");
+ docstring label = new_label;
+ uniqueLabel(label);
+ if (label == old_label)
+ return;
buffer().undo().beginUndoGroup();
+ if (cursor)
+ cursor->recordUndo();
setParam("name", label);
+ updateReferences(old_label, label);
+ buffer().undo().endUndoGroup();
+}
- if (updaterefs) {
- Buffer::References & refs = buffer().references(old_label);
- Buffer::References::iterator it = refs.begin();
- Buffer::References::iterator end = refs.end();
- for (; it != end; ++it) {
- buffer().undo().recordUndo(it->second);
- if (it->first->lyxCode() == MATH_REF_CODE) {
- InsetMathHull * mi = it->first->asInsetMath()->asHullInset();
- mi->asRefInset()->changeTarget(label);
- } else {
- InsetCommand * ref = it->first->asInsetCommand();
- ref->setParam("reference", label);
- }
+
+void InsetLabel::updateReferences(docstring const & old_label,
+ docstring const & new_label)
+{
+ Buffer::References & refs = buffer().references(old_label);
+ Buffer::References::iterator it = refs.begin();
+ Buffer::References::iterator end = refs.end();
+ for (; it != end; ++it) {
+ buffer().undo().recordUndo(it->second);
+ if (it->first->lyxCode() == MATH_REF_CODE) {
+ InsetMathHull * mi = it->first->asInsetMath()->asHullInset();
+ mi->asRefInset()->changeTarget(new_label);
+ } else {
+ InsetCommand * ref = it->first->asInsetCommand();
+ ref->setParam("reference", new_label);
}
}
- buffer().undo().endUndoGroup();
}
break;
}
if (p["name"] != params()["name"]) {
- // undo is handled in updateCommand
- updateCommand(p["name"]);
+ // undo is handled in updateLabelAndRefs
+ updateLabelAndRefs(p["name"], &cur);
}
cur.forceBufferUpdate();
break;
docstring const & counterValue() const { return counter_value_; }
///
docstring const & prettyCounter() const { return pretty_counter_; }
- ///
- void updateCommand(docstring const & new_label, bool updaterefs = true);
+ /// Updates only the label string, doesn't handle undo nor references.
+ void updateLabel(docstring const & new_label);
+ /// Updates the label and the references to it.
+ /// Will also handle undo/redo if \p cursor is passed.
+ void updateLabelAndRefs(docstring const & new_label, Cursor * cursor = 0);
/// \name Public functions inherited from Inset class
//@{
void doDispatch(Cursor & cur, FuncRequest & cmd);
//@}
+ ///
+ void uniqueLabel(docstring & label) const;
+ ///
+ void updateReferences(docstring const & old_label,
+ docstring const & new_label);
///
docstring screen_label_;
///