From: Juergen Spitzmueller Date: Tue, 31 Dec 2019 11:29:53 +0000 (+0100) Subject: Honor change tracking when automatically renaming refs to labels X-Git-Tag: lyx-2.4.0dev-acb2ca7b~1370 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=affc006baeb50c3d730ac06cf6dcfe050d6c98d8;p=features.git Honor change tracking when automatically renaming refs to labels Fixes rest of #11556 --- diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index bcb72904bb..897b73f61e 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -105,7 +105,7 @@ void InsetLabel::updateLabelAndRefs(docstring const & new_label, UndoGroupHelper ugh(&buffer()); if (cursor) cursor->recordUndo(); - if (buffer().params().track_changes) { + if (buffer().masterParams().track_changes) { // With change tracking, we insert a new label and // delete the old one InsetCommandParams p(LABEL_CODE, "label"); @@ -123,15 +123,23 @@ void InsetLabel::updateReferences(docstring const & old_label, docstring const & new_label) { UndoGroupHelper ugh; - for (auto const & p: buffer().references(old_label)) { - ugh.resetBuffer(p.second.buffer()); - CursorData(p.second).recordUndo(); - if (p.first->lyxCode() == MATH_REF_CODE) { - InsetMathRef * mi = p.first->asInsetMath()->asRefInset(); - mi->changeTarget(new_label); - } else { - InsetCommand * ref = p.first->asInsetCommand(); - ref->setParam("reference", new_label); + if (buffer().masterParams().track_changes) { + // With change tracking, we insert a new ref and + // delete the old one + lyx::dispatch(FuncRequest(LFUN_MASTER_BUFFER_FORALL, + "inset-forall Ref inset-modify ref changetarget " + + old_label + " " + new_label)); + } else { + for (auto const & p: buffer().references(old_label)) { + ugh.resetBuffer(p.second.buffer()); + CursorData(p.second).recordUndo(); + if (p.first->lyxCode() == MATH_REF_CODE) { + InsetMathRef * mi = p.first->asInsetMath()->asRefInset(); + mi->changeTarget(new_label); + } else { + InsetCommand * ref = p.first->asInsetCommand(); + ref->setParam("reference", new_label); + } } } } diff --git a/src/insets/InsetRef.cpp b/src/insets/InsetRef.cpp index 82db322320..de0a531e9c 100644 --- a/src/insets/InsetRef.cpp +++ b/src/insets/InsetRef.cpp @@ -79,6 +79,28 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */) } +docstring InsetRef::layoutName() const +{ + return from_ascii("Ref"); +} + + +void InsetRef::changeTarget(docstring const & new_label) +{ + // With change tracking, we insert a new ref + // and delete the old one + if (buffer().masterParams().track_changes) { + InsetCommandParams icp(REF_CODE, "ref"); + icp["reference"] = new_label; + string const data = InsetCommand::params2string(icp); + lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data)); + lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD)); + } else + setParam("reference", new_label); +} + + + void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd) { string const inset = cmd.getArg(0); @@ -91,6 +113,15 @@ void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd) pstring = "caps"; else if (arg == "toggle-noprefix") pstring = "noprefix"; + else if (arg == "changetarget") { + string const oldtarget = cmd.getArg(2); + string const newtarget = cmd.getArg(3); + if (!oldtarget.empty() && !newtarget.empty() + && getParam("reference") == from_utf8(oldtarget)) + changeTarget(from_utf8(newtarget)); + cur.forceBufferUpdate(); + return; + } } // otherwise not for us if (pstring.empty()) @@ -111,6 +142,8 @@ bool InsetRef::getStatus(Cursor & cur, FuncRequest const & cmd, string const arg = cmd.getArg(1); string pstring; + if (arg == "changetarget") + return true; if (arg == "toggle-plural") pstring = "plural"; else if (arg == "toggle-caps") diff --git a/src/insets/InsetRef.h b/src/insets/InsetRef.h index 9243aa63fa..37a7c3ec3c 100644 --- a/src/insets/InsetRef.h +++ b/src/insets/InsetRef.h @@ -33,9 +33,14 @@ public: /// InsetRef(Buffer * buffer, InsetCommandParams const &); + /// + void changeTarget(docstring const & new_label); + /// \name Public functions inherited from Inset class //@{ /// + docstring layoutName() const; + /// void doDispatch(Cursor & cur, FuncRequest & cmd); /// bool getStatus(Cursor & cur, FuncRequest const & cmd, FuncStatus & status) const; diff --git a/src/mathed/InsetMathRef.cpp b/src/mathed/InsetMathRef.cpp index 97591dc7d2..717bd419d8 100644 --- a/src/mathed/InsetMathRef.cpp +++ b/src/mathed/InsetMathRef.cpp @@ -64,6 +64,15 @@ void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd) switch (cmd.action()) { case LFUN_INSET_MODIFY: if (cmd.getArg(0) == "ref") { + if (cmd.getArg(1) == "changetarget") { + string const oldtarget = cmd.getArg(2); + string const newtarget = cmd.getArg(3); + if (!oldtarget.empty() && !newtarget.empty() + && asString(cell(0)) == from_utf8(oldtarget)) + changeTarget(from_utf8(newtarget)); + cur.forceBufferUpdate(); + break; + } MathData ar; if (createInsetMath_fromDialogStr(cmd.argument(), ar)) { cur.recordUndo();