[LyX/master] Honor change tracking when automatically renaming refs to labels

Juergen Spitzmueller spitz at lyx.org
Tue Dec 31 11:14:54 UTC 2019


commit affc006baeb50c3d730ac06cf6dcfe050d6c98d8
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Tue Dec 31 12:29:53 2019 +0100

    Honor change tracking when automatically renaming refs to labels
    
    Fixes rest of #11556
---
 src/insets/InsetLabel.cpp   |   28 ++++++++++++++++++----------
 src/insets/InsetRef.cpp     |   33 +++++++++++++++++++++++++++++++++
 src/insets/InsetRef.h       |    5 +++++
 src/mathed/InsetMathRef.cpp |    9 +++++++++
 4 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp
index bcb7290..897b73f 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 82db322..de0a531 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 9243aa6..37a7c3e 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 97591dc..717bd41 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();


More information about the lyx-cvs mailing list