Implement LFUN_REFERENCE_TO_PARAGRAPH

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)

(cherry picked from commit 3fe99bf6f5)
This commit is contained in:
Juergen Spitzmueller 2024-07-28 15:54:55 +02:00
parent ce64021ce9
commit 69614db1ec
3 changed files with 59 additions and 0 deletions

View File

@ -1282,6 +1282,12 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
|| getInsetByCode<InsetMathRef>(cur, MATH_REF_CODE)); || getInsetByCode<InsetMathRef>(cur, MATH_REF_CODE));
break; 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_CHANGES_MERGE:
case LFUN_CHANGE_NEXT: case LFUN_CHANGE_NEXT:
case LFUN_CHANGE_PREVIOUS: case LFUN_CHANGE_PREVIOUS:
@ -1641,6 +1647,45 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
break; 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: case LFUN_NOTE_NEXT:
if (gotoInset(this, { NOTE_CODE }, false)) if (gotoInset(this, { NOTE_CODE }, false))
dr.screenUpdate(Update::Force); dr.screenUpdate(Update::Force);

View File

@ -511,6 +511,7 @@ enum FuncCode
LFUN_STATISTICS_REFERENCE_CLAMP,// sanda, 20240324 LFUN_STATISTICS_REFERENCE_CLAMP,// sanda, 20240324
LFUN_REFERENCE_INSERT, // spitz, 20240728 LFUN_REFERENCE_INSERT, // spitz, 20240728
// 400 // 400
LFUN_REFERENCE_TO_PARAGRAPH, // spitz, 20240728
LFUN_LASTACTION // end of the table LFUN_LASTACTION // end of the table
}; };

View File

@ -3531,6 +3531,19 @@ void LyXAction::init()
*/ */
{ LFUN_REFERENCE_NEXT, "reference-next", ReadOnly, Edit }, { 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 * \var lyx::FuncCode lyx::LFUN_REGEXP_MODE