]> git.lyx.org Git - features.git/commitdiff
Honor change tracking when automatically renaming refs to labels
authorJuergen Spitzmueller <spitz@lyx.org>
Tue, 31 Dec 2019 11:29:53 +0000 (12:29 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Tue, 31 Dec 2019 11:29:53 +0000 (12:29 +0100)
Fixes rest of #11556

src/insets/InsetLabel.cpp
src/insets/InsetRef.cpp
src/insets/InsetRef.h
src/mathed/InsetMathRef.cpp

index bcb72904bb465b9527c5deae24e46869edf89bb9..897b73f61ee2d6229caf573560b51c7ae3967f06 100644 (file)
@@ -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);
+                       }
                }
        }
 }
index 82db322320aaae7d5d89bba6537ecfa69b65a120..de0a531e9ce266fa69a32ddd6364c06fb4535c10 100644 (file)
@@ -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")
index 9243aa63faaf322faf9917755e608707225e27d4..37a7c3ec3cdc61cc82c7f4c5b37d066fe9923720 100644 (file)
@@ -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;
index 97591dc7d2943f03d209e2d119b840dee9d44756..717bd419d8f51d83294132d1ecc5df803f881294 100644 (file)
@@ -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();