]> git.lyx.org Git - lyx.git/commitdiff
Implement reference-to-paragraph in outliner (#1624)
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 28 Jul 2024 13:59:06 +0000 (15:59 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 28 Jul 2024 13:59:06 +0000 (15:59 +0200)
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
src/frontends/qt/TocWidget.cpp

index 98d23bf17714338dd6ee2c93e4d486c61828baa3..46b7562eda20a8c36780f49b2d2e4795a6381c39 100644 (file)
@@ -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
 
 #
index 8cc2be8a913a6ae36cd612a30305ac6dfa588040..9779a5c903de6436cae704dcc4b9ade2302446e1 100644 (file)
@@ -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: