[LyX/master] Implement reference-to-paragraph in outliner (#1624)

Juergen Spitzmueller spitz at lyx.org
Sun Jul 28 14:00:58 UTC 2024


commit 143e534d1e749936a37e4f3607c39161eb89d293
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Jul 28 15:59:06 2024 +0200

    Implement reference-to-paragraph in outliner (#1624)
    
    This allows to insert a cross-reference to headings, figures or tables
    by right-clicking on the outliner item.
    
    If the item in question does not have a label yet, it is inserted.
---
 lib/ui/stdcontext.inc          | 18 ++++++++++++++++++
 src/frontends/qt/TocWidget.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index 98d23bf177..46b7562eda 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -584,6 +584,8 @@ Menuset
 
 	Menu "context-toc-figure"
 		Item "Settings...|S" "inset-settings"
+		Separator
+		SubMenu "Insert Cross-Reference to this Item|C" "context-toc-ref-to-par"
 	End
 
 #
@@ -600,6 +602,8 @@ Menuset
 
 	Menu "context-toc-table"
 		Item "Settings...|S" "inset-settings"
+		Separator
+		SubMenu "Insert Cross-Reference to this Item|C" "context-toc-ref-to-par"
 	End
 
 #
@@ -707,6 +711,18 @@ Menuset
 # Toc Table of Context context menu
 #
 
+        Menu "context-toc-ref-to-par"
+        	Item "<Reference>|R" "reference-to-paragraph ref"
+		Item "(<Reference>)|e" "reference-to-paragraph eqref"
+		Item "<Page>|P" "reference-to-paragraph pageref"
+		Item "On Page <Page>|O" "reference-to-paragraph vpageref"
+		Item "<Reference> on Page <Page>|f" "reference-to-paragraph vref"
+		Item "Formatted Reference|t" "reference-to-paragraph formatted"
+		Item "Textual Reference|x" "reference-to-paragraph nameref"
+		Item "Label Only|L" "reference-to-paragraph labelonly"
+        
+        End
+
 	Menu "context-toc-tableofcontents"
 		Item "Promote Section|P" "outline-out"
 		Item "Demote Section|D" "outline-in"
@@ -714,6 +730,8 @@ Menuset
 		Item "Move Section Down|w" "outline-down"
 		Separator
 		Item "Select Section|S" "section-select"
+		Separator
+		SubMenu "Insert Cross-Reference to this Item|C" "context-toc-ref-to-par"
 	End
 
 #
diff --git a/src/frontends/qt/TocWidget.cpp b/src/frontends/qt/TocWidget.cpp
index 8cc2be8a91..9779a5c903 100644
--- a/src/frontends/qt/TocWidget.cpp
+++ b/src/frontends/qt/TocWidget.cpp
@@ -27,6 +27,7 @@
 #include "FuncStatus.h"
 #include "LyX.h"
 #include "Menus.h"
+#include "Paragraph.h"
 #include "TocBackend.h"
 
 #include "insets/InsetCommand.h"
@@ -180,6 +181,7 @@ bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
 	case LFUN_OUTLINE_DOWN:
 	case LFUN_OUTLINE_IN:
 	case LFUN_OUTLINE_OUT:
+	case LFUN_REFERENCE_TO_PARAGRAPH:
 	case LFUN_SECTION_SELECT:
 		status.setEnabled((bool)item.dit());
 		return true;
@@ -248,6 +250,46 @@ void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd,
 		break;
 	}
 
+	case LFUN_REFERENCE_TO_PARAGRAPH: {
+		docstring const type = cmd.argument();
+		TocItem const & item =
+			gui_view_.tocModels().currentItem(current_type_, index);
+		if (item.action().action() == LFUN_PARAGRAPH_GOTO) {
+			// easy case
+			docstring const id = item.dit().paragraphGotoArgument(true);
+			docstring const arg = (type.empty()) ? id : id + " " + type;
+			dispatch(FuncRequest(cmd, arg));
+			break;
+		}
+		// Captions etc.
+		// Here we cannot employ LFUN_REFERENCE_TO_PARAGRAPH
+		// as it won't land in the inset. Seo we do it ourselves;
+		// 1. save current position
+		lyx::dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
+		// go to the item
+		sendDispatch(item.action());
+		// check if it has a label
+		docstring label = from_utf8(cur.innerParagraph().getLabel());
+		if (label.empty()) {
+			// if not:
+			// insert a new label
+			// we do not want to open the dialog, hence we
+			// do not employ LFUN_LABEL_INSERT
+			InsetCommandParams p(LABEL_CODE);
+			label = cur.getPossibleLabel();
+			p["name"] = label;
+			string const data = InsetCommand::params2string(p);
+			lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
+		}
+		// now go back to the original position ...
+		lyx::dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
+		// ... to insert the ref
+		docstring const arg = (type.empty()) ? label
+						  : label + from_ascii(" ") + type;
+		lyx::dispatch(FuncRequest(LFUN_REFERENCE_INSERT, arg));
+		break;
+	}
+
 	case LFUN_OUTLINE_UP:
 	case LFUN_OUTLINE_DOWN:
 	case LFUN_OUTLINE_IN:


More information about the lyx-cvs mailing list