+ 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 some inset might going to try to access
+ // the buffer() member.
+ it->setBuffer(const_cast<Buffer &>(buffer));
+ switch (it->lyxCode()) {
+
+ case MATH_HULL_CODE: {
+ // check for equation labels and resolve duplicates
+ InsetMathHull * ins = it->asInsetMath()->asHullInset();
+ std::vector<InsetLabel *> labels = ins->getLabels();
+ for (size_t i = 0; i != labels.size(); ++i) {
+ if (!labels[i])
+ continue;
+ InsetLabel * lab = labels[i];
+ docstring const oldname = lab->getParam("name");
+ lab->updateLabel(oldname);
+ // We need to update the buffer reference cache.
+ need_update = true;
+ docstring const newname = lab->getParam("name");
+ if (oldname == newname)
+ continue;
+ // adapt the references
+ for (InsetIterator itt = inset_iterator_begin(in);
+ itt != i_end; ++itt) {
+ if (itt->lyxCode() == REF_CODE) {
+ InsetCommand * ref = itt->asInsetCommand();
+ if (ref->getParam("reference") == oldname)
+ ref->setParam("reference", newname);
+ } else if (itt->lyxCode() == MATH_REF_CODE) {
+ InsetMathRef * mi = itt->asInsetMath()->asRefInset();
+ // this is necessary to prevent an uninitialized
+ // buffer when the RefInset is in a MathBox.
+ // FIXME audit setBuffer calls
+ mi->setBuffer(const_cast<Buffer &>(buffer));
+ if (mi->getTarget() == oldname)
+ mi->changeTarget(newname);
+ }
+ }
+ }
+ break;
+ }
+
+ case LABEL_CODE: {
+ // check for duplicates
+ InsetLabel & lab = static_cast<InsetLabel &>(*it);
+ docstring const oldname = lab.getParam("name");
+ lab.updateLabel(oldname);
+ // We need to update the buffer reference cache.
+ need_update = true;
+ docstring const newname = lab.getParam("name");
+ if (oldname == newname)
+ break;
+ // adapt the references
+ for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
+ if (itt->lyxCode() == REF_CODE) {
+ InsetCommand & ref = static_cast<InsetCommand &>(*itt);
+ if (ref.getParam("reference") == oldname)
+ ref.setParam("reference", newname);
+ } else if (itt->lyxCode() == MATH_REF_CODE) {
+ InsetMathRef * mi = itt->asInsetMath()->asRefInset();
+ // this is necessary to prevent an uninitialized
+ // buffer when the RefInset is in a MathBox.
+ // FIXME audit setBuffer calls
+ mi->setBuffer(const_cast<Buffer &>(buffer));
+ if (mi->getTarget() == oldname)
+ mi->changeTarget(newname);
+ }
+ }
+ break;
+ }
+
+ case INCLUDE_CODE: {
+ InsetInclude & inc = static_cast<InsetInclude &>(*it);
+ inc.updateCommand();
+ // We need to update the list of included files.
+ need_update = true;
+ break;
+ }
+
+ case BIBITEM_CODE: {
+ // check for duplicates
+ InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
+ docstring const oldkey = bib.getParam("key");
+ bib.updateCommand(oldkey, false);
+ // We need to update the buffer reference cache.
+ need_update = true;
+ docstring const newkey = bib.getParam("key");
+ if (oldkey == newkey)
+ break;
+ // adapt the references
+ for (InsetIterator itt = inset_iterator_begin(in);
+ itt != i_end; ++itt) {
+ if (itt->lyxCode() == CITE_CODE) {
+ InsetCommand * ref = itt->asInsetCommand();
+ if (ref->getParam("key") == oldkey)
+ ref->setParam("key", newkey);
+ }
+ }
+ break;
+ }
+
+ case BRANCH_CODE: {
+ // check if branch is known to target buffer
+ // or its master
+ InsetBranch & br = static_cast<InsetBranch &>(*it);
+ docstring const name = br.branch();
+ if (name.empty())
+ break;
+ bool const is_child = (&buffer != buffer.masterBuffer());
+ BranchList branchlist = buffer.params().branchlist();
+ if ((!is_child && branchlist.find(name))
+ || (is_child && (branchlist.find(name)
+ || buffer.masterBuffer()->params().branchlist().find(name))))
+ break;
+ switch(branchAction) {
+ case cap::BRANCH_ADD: {
+ // This is for a temporary buffer, so simply create the branch.
+ // Must not use lyx::dispatch(), since tmpbuffer has no view.
+ DispatchResult dr;
+ const_cast<Buffer&>(buffer).dispatch(FuncRequest(LFUN_BRANCH_ADD, name), dr);
+ break;
+ }
+ case cap::BRANCH_ASK: {
+ docstring text = bformat(
+ _("The pasted branch \"%1$s\" is undefined.\n"
+ "Do you want to add it to the document's branch list?"),
+ name);
+ if (frontend::Alert::prompt(_("Unknown branch"),
+ text, 0, 1, _("&Add"), _("&Don't Add")) != 0)
+ break;
+ lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, name));
+ break;
+ }
+ case cap::BRANCH_IGNORE:
+ break;
+ }
+ // We need to update the list of branches.
+ need_update = true;
+ break;
+ }