]> git.lyx.org Git - lyx.git/commitdiff
Implement LFUN_REFERENCE_TO_PARAGRAPH
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 28 Jul 2024 13:54:55 +0000 (15:54 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 28 Jul 2024 13:54:55 +0000 (15:54 +0200)
This function checks whether a paragraph (specified by ID) has a label.
If so, it simply inserts a reference to this at cursor position, if
not it inserts a label to that paragraph (pos 0) and then inserts
a reference at cursor position.

Needed to implement #1624 (insert cross references to items that do not
have yet a label)

src/BufferView.cpp
src/FuncCode.h
src/LyXAction.cpp

index 4c2a49546c1fdf508956b6dfe16725de6122ea5f..145839ba1438dbdadf9c18d11b1ce6529ebe2706 100644 (file)
@@ -1292,6 +1292,12 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                    || getInsetByCode<InsetMathRef>(cur, MATH_REF_CODE));
                break;
 
+       case LFUN_REFERENCE_TO_PARAGRAPH: {
+               int const id = convert<int>(cmd.getArg(0));
+               flag.setEnabled(id > 0);
+               break;
+       }
+
        case LFUN_CHANGES_MERGE:
        case LFUN_CHANGE_NEXT:
        case LFUN_CHANGE_PREVIOUS:
@@ -1651,6 +1657,45 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                break;
        }
 
+       case LFUN_REFERENCE_TO_PARAGRAPH: {
+               int const id = convert<int>(cmd.getArg(0));
+               if (id < 1)
+                       break;
+               string const type = cmd.getArg(1);
+               int i = 0;
+               for (Buffer * b = &buffer_; i == 0 || b != &buffer_;
+                       b = theBufferList().next(b)) {
+                       DocIterator const dit = b->getParFromID(id);
+                       string const label = dit.innerParagraph().getLabel();
+                       if (!label.empty()) {
+                               // if the paragraph has a label, we refer to this
+                               string const arg = (type.empty()) ? label : label + " " + type;
+                               lyx::dispatch(FuncRequest(LFUN_REFERENCE_INSERT, arg));
+                               break;
+                       } else {
+                               // if there is not a label yet, go to the paragraph ...
+                               lyx::dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
+                               lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, cmd.argument()));
+                               // ... insert the label
+                               // we do not want to open the dialog, hence we
+                               // do not employ LFUN_LABEL_INSERT
+                               InsetCommandParams p(LABEL_CODE);
+                               docstring const label = cur.getPossibleLabel();
+                               p["name"] = label;
+                               string const data = InsetCommand::params2string(p);
+                               lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
+                               // ... and go back to the original position
+                               lyx::dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
+                               // ... to insert the ref
+                               string const arg = (type.empty()) ? to_utf8(label)
+                                                                 : to_utf8(label) + " " + type;
+                               lyx::dispatch(FuncRequest(LFUN_REFERENCE_INSERT, arg));
+                               break;
+                       }
+               }
+               break;
+       }
+
        case LFUN_NOTE_NEXT:
                if (gotoInset(this, { NOTE_CODE }, false))
                        dr.screenUpdate(Update::Force);
index 7f35ca391dbe457578626b83fbf25c62cbf46105..b8e2e0a0ac5b7baa0a4d162207b4b53de6165402 100644 (file)
@@ -511,6 +511,7 @@ enum FuncCode
        LFUN_STATISTICS_REFERENCE_CLAMP,// sanda, 20240324
        LFUN_REFERENCE_INSERT,          // spitz, 20240728
        // 400
+       LFUN_REFERENCE_TO_PARAGRAPH,    // spitz, 20240728
        LFUN_LASTACTION                 // end of the table
 };
 
index 41df737332cae41e3a8b70c2d5ed21bf248b46e9..155ba9ef2bba0492b30a28036d77334a1cc189e9 100644 (file)
@@ -3531,6 +3531,19 @@ void LyXAction::init()
  */
                { LFUN_REFERENCE_NEXT, "reference-next", ReadOnly, Edit },
 
+/*!
+ * \var lyx::FuncCode lyx::LFUN_REFERENCE_TO_PARAGRAPH
+ * \li Action: Inserts a cross-reference to the paragraph with a given ID
+ * \li Notion: The function checks of the paragraph already has a label.
+ *             If so, it uses that. Otherwise it inserts a label and uses this.
+ * \li Syntax: reference-to-paragraph <PAR_ID> [<TYPE>]
+ * \li Params: <PAR_ID>:  paragraph id \n
+              <TYPE>: cross-references type
+ * \li Origin: spitz, 28 Jul 2024
+ * \endvar
+ */
+               { LFUN_REFERENCE_TO_PARAGRAPH, "reference-to-paragraph", ReadOnly | NoInternal, Edit },
+
 
 /*!
  * \var lyx::FuncCode lyx::LFUN_REGEXP_MODE