From 3bbce7f24afbb469fff5ad352778af73d57b44d8 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sat, 5 Nov 2022 18:39:33 +0100 Subject: [PATCH] Introduce index-tag-all lfun This is a convenience function for indexing. It adds a copy of the index inset under cursor after any word in the buffer that is equal (case-insensitively) to the word preceding the index inset. The function can be easily used to generate bad indexes (and I warn about that in the UserGuide), but if used with care, it can also be extremely convenient. --- lib/RELEASE-NOTES | 3 + lib/doc/UserGuide.lyx | 143 +++++++++++++++++++++++++++++++++++--- lib/ui/stdcontext.inc | 2 + src/BufferView.cpp | 74 ++++++++++++++++++++ src/FuncCode.h | 2 + src/LyXAction.cpp | 11 +++ src/insets/Inset.h | 3 + src/insets/InsetIndex.cpp | 3 + src/insets/InsetIndex.h | 2 + 9 files changed, 235 insertions(+), 8 deletions(-) diff --git a/lib/RELEASE-NOTES b/lib/RELEASE-NOTES index 2ae5e72a52..3c211a2025 100644 --- a/lib/RELEASE-NOTES +++ b/lib/RELEASE-NOTES @@ -142,6 +142,9 @@ * spelling-remove-local removes words for a given language from the document's local spelling dictionary. + +* index-tag-all adds a copy of the index entry under cursor to after all occurrences of the + word that precedes the index inset. * inset-insert-copy inserts the inset's content (in ToC pane) at the position of the cursor. diff --git a/lib/doc/UserGuide.lyx b/lib/doc/UserGuide.lyx index 1de5ee5846..a0533dfcc4 100644 --- a/lib/doc/UserGuide.lyx +++ b/lib/doc/UserGuide.lyx @@ -9621,12 +9621,14 @@ Verbatim \end_layout \begin_layout Verbatim + This is Verbatim. \end_layout \begin_layout Verbatim \noindent \align block + The following 2 lines are empty: \end_layout @@ -9639,6 +9641,7 @@ The following 2 lines are empty: \end_layout \begin_layout Verbatim + Almost everything is allowed in Verbatim:"%&$ยง#~'` \backslash }][{| @@ -9662,6 +9665,7 @@ Verbatim \end_layout \begin_layout Verbatim* + This is Verbatim*. \end_layout @@ -35407,31 +35411,46 @@ type "icon" arg "index-insert" \end_inset + +\change_inserted -712698321 1667668430 + (but see section +\begin_inset space ~ +\end_inset + + +\begin_inset CommandInset ref +LatexCommand ref +reference "subsec:Convenience-Functions-Index" + +\end_inset + + for some advanced methods) +\change_unchanged . A -\change_inserted -712698321 1667110584 +\change_inserted -712698321 1667665903 collapsible inset \change_unchanged \change_deleted -712698321 1667110543 box -\change_inserted -712698321 1667110812 +\change_inserted -712698321 1667665903 with green label \change_unchanged is inserted -\change_inserted -712698321 1667110886 +\change_inserted -712698321 1667665903 ; this \change_unchanged \change_deleted -712698321 1667110903 contain -\change_inserted -712698321 1667110904 +\change_inserted -712698321 1667665903 holds \change_deleted -712698321 1667110872 ing \change_unchanged the text that appears in the index -\change_inserted -712698321 1667112993 +\change_inserted -712698321 1667665903 (if you read this in \SpecialChar LyX , see the \begin_inset Quotes eld @@ -35445,11 +35464,11 @@ Index generation \change_unchanged . The word where the cursor is in -\change_inserted -712698321 1667112984 +\change_inserted -712698321 1667665903 , \change_unchanged or the currently highlighted text -\change_inserted -712698321 1667112984 +\change_inserted -712698321 1667665903 , \change_unchanged is proposed by \SpecialChar LyX @@ -35458,7 +35477,7 @@ Index generation the \change_unchanged index entry -\change_inserted -712698321 1667112984 +\change_inserted -712698321 1667665903 (i. \begin_inset space \thinspace{} \end_inset @@ -37422,6 +37441,114 @@ reference "subsec:TeX-Code-Boxes" \end_layout +\begin_layout Subsection + +\change_inserted -712698321 1667669398 +\begin_inset CommandInset label +LatexCommand label +name "subsec:Convenience-Functions-Index" + +\end_inset + +Convenience Functions for Index Handling +\end_layout + +\begin_layout Standard + +\change_inserted -712698321 1667669552 +Indexing a document can be a tedious task, since you often have to insert + the same index entry multiple times in order to refer to passages at different + pages. + This is not only time-consuming, but also error-prone; e. +\begin_inset space \thinspace{} +\end_inset + +g., you have to remember how exactly an inset entry has been inserted to + avoid redundant entries. + \SpecialChar LyX + provides some functions to ease the task. +\end_layout + +\begin_layout Standard + +\change_inserted -712698321 1667669398 +First, if you want to add an entry which you already used before, you can + open the outliner via +\family sans +View\SpecialChar menuseparator +Outline Pane +\family default + (see section +\begin_inset space ~ +\end_inset + + +\begin_inset CommandInset ref +LatexCommand ref +reference "subsec:The-Outliner" + +\end_inset + +), select the +\family sans +Index Entries +\family default + section (it is convenient to have this open while indexing anyway), scroll + to the entry in question (if you check +\family sans +Sort +\family default +, it will be more easier), then +\emph on +right +\emph default +-click on the entry and select +\family sans +Insert Copy at Cursor Position +\family default + from the context menu. + This will do just what it says: it will insert a copy of that index inset + at the position where the cursor is. +\end_layout + +\begin_layout Standard + +\change_inserted -712698321 1667669772 +Second, \SpecialChar LyX + also provides a semi-automated solution to index a word. + If you right-click on an index entry inset, you can select from the context + menu +\family sans +Index All Occurrences of this Word +\family default +. + This will search the whole document (only single documents, not master + and child documents) and insert a copy of this index inset after each occurrenc +e of the word that precedes the inset. + Note that casing of the word does not matter, but only full words in the + same grammatical form are considered (so if +\emph on +table +\emph default + is the word before the index entry, +\emph on +Table +\emph default + will be considered as well, but not +\emph on +tables +\emph default +). + Please take care to not overuse this function and to carefully check the + result afterwards. + Remember, a good index does not simply list all occurrences of a given + word in a specific document (that is a concordance rather), but only relevant + occurrences! So as convenient as this function might seem, it is suitable + especially for particular cases. + With others, you will have more work with removing again falsely inserted + entries than you would have with manually inserting them at the right place. +\end_layout + \begin_layout Subsection Index \change_deleted -712698321 1667120869 diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc index 64cafd0012..d6f1fd8470 100644 --- a/lib/ui/stdcontext.inc +++ b/lib/ui/stdcontext.inc @@ -633,6 +633,8 @@ Menuset Menu "context-index" IndicesContext Separator + Item "Index All Occurrences of this Word|W" "index-tag-all" + Separator OptItem "Single Page (No Page Range)|P" "inset-modify changeparam range none" OptItem "Start Page Range|t" "inset-modify changeparam range start" OptItem "End Page Range|E" "inset-modify changeparam range end" diff --git a/src/BufferView.cpp b/src/BufferView.cpp index b36b8ee5b6..0bbf649796 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -38,6 +38,7 @@ #include "MetricsInfo.h" #include "Paragraph.h" #include "Session.h" +#include "texstream.h" #include "Text.h" #include "TextMetrics.h" #include "TexRow.h" @@ -47,6 +48,7 @@ #include "insets/InsetCitation.h" #include "insets/InsetCommand.h" // ChangeRefs #include "insets/InsetGraphics.h" +#include "insets/InsetIndex.h" #include "insets/InsetRef.h" #include "insets/InsetText.h" @@ -1760,6 +1762,78 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr) break; } + case LFUN_INDEX_TAG_ALL: { + Inset * ins = cur.nextInset(); + if (!ins || ins->lyxCode() != INDEX_CODE) + // not at index inset + break; + + // clone the index inset + InsetIndex * cins = + new InsetIndex(static_cast(*cur.nextInset())); + // In order to avoid duplication, we compare the + // LaTeX output if we find another index inset after + // the word + odocstringstream oilatex; + otexstream oits(oilatex); + OutputParams rp(&cur.buffer()->params().encoding()); + ins->latex(oits, rp); + cap::copyInsetToTemp(cur, cins); + + // move backwards into preceding word + // skip over other index insets + cur.backwardPosIgnoreCollapsed(); + while (true) { + if (cur.inset().lyxCode() == INDEX_CODE) + cur.pop_back(); + else if (cur.prevInset() && cur.prevInset()->lyxCode() == INDEX_CODE) + cur.backwardPosIgnoreCollapsed(); + else + break; + } + if (!cur.inTexted()) { + // Nothing to do here. + setCursorFromInset(ins); + break; + } + // Get word or selection + cur.text()->selectWord(cur, WHOLE_WORD); + docstring const searched_string = cur.selectionAsString(false); + // Start from the beginning + lyx::dispatch(FuncRequest(LFUN_BUFFER_BEGIN)); + while (findOne(this, searched_string, + false,// case sensitive + true,// match whole word only + true,// forward + false,//find deleted + false,//check wrap + false,// auto-wrap + false,// instant + false// only selection + )) { + cur.clearSelection(); + Inset * ains = cur.nextInset(); + if (ains && ains->lyxCode() == INDEX_CODE) { + // We have an index inset. + // Check whether it has the same + // LaTeX content and move on if so. + odocstringstream filatex; + otexstream fits(filatex); + ains->latex(fits, rp); + if (oilatex.str() == filatex.str()) + continue; + } + // Paste the inset and possibly continue + cap::pasteFromTemp(cursor(), cursor().buffer()->errorList("Paste")); + } + // Go back to start position. + setCursorFromInset(ins); + dr.screenUpdate(cur.result().screenUpdate()); + if (cur.result().needBufferUpdate()) + dr.forceBufferUpdate(); + break; + } + case LFUN_MARK_OFF: cur.clearSelection(); dr.setMessage(from_utf8(N_("Mark off"))); diff --git a/src/FuncCode.h b/src/FuncCode.h index 3667a98c1d..956a3a4890 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -503,6 +503,8 @@ enum FuncCode LFUN_BRANCH_SYNC_ALL, // sanda 20220415 LFUN_INDEXMACRO_INSERT, // spitz 20220220 LFUN_INSET_INSERT_COPY, // spitz 20221101 + LFUN_INDEX_TAG_ALL, // spitz 20221105 + // 395 LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index e1f91d2d89..1692605945 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -1975,6 +1975,17 @@ void LyXAction::init() */ { LFUN_INDEX_INSERT, "index-insert", Noop, Edit }, +/*! + * \var lyx::FuncCode lyx::LFUN_INDEX_TAG_ALL + * \li Action: Tags all occurrences of the word before the cursor with the + * index inset at cursor. + * \li Notion: It automatically takes the word on the cursor position. + * \li Syntax: index-tag-all + * \li Origin: spitz, 5 Nov 2022 + * \endvar + */ + { LFUN_INDEX_TAG_ALL, "index-tag-all", AtPoint, Edit }, + /*! * \var lyx::FuncCode lyx::LFUN_INDEX_PRINT * \li Action: Inserts list of Index entries on a new page. diff --git a/src/insets/Inset.h b/src/insets/Inset.h index c73fa49d68..fd55d69215 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -46,6 +46,7 @@ class InsetArgument; class InsetCollapsible; class InsetCommand; class InsetGraphics; +class InsetIndex; class InsetIterator; class InsetLayout; class InsetList; @@ -146,6 +147,8 @@ public: virtual InsetCommand const * asInsetCommand() const { return nullptr; } /// is this inset based on the InsetArgument class? virtual InsetArgument const * asInsetArgument() const { return nullptr; } + /// is this inset based on the InsetIndex class? + virtual InsetIndex const * asInsetIndex() const { return nullptr; } /// is this inset based on the InsetGraphics class? virtual InsetGraphics * asInsetGraphics() { return nullptr; } /// is this inset based on the InsetGraphics class? diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp index 839d09472a..bec373d5dd 100644 --- a/src/insets/InsetIndex.cpp +++ b/src/insets/InsetIndex.cpp @@ -692,6 +692,9 @@ bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_INDEXMACRO_INSERT: return macrosPossible(cmd.getArg(0)); + case LFUN_INDEX_TAG_ALL: + return true; + default: return InsetCollapsible::getStatus(cur, cmd, flag); } diff --git a/src/insets/InsetIndex.h b/src/insets/InsetIndex.h index a89a684974..b3fe2713d9 100644 --- a/src/insets/InsetIndex.h +++ b/src/insets/InsetIndex.h @@ -58,6 +58,8 @@ public: const InsetIndexParams& params() const { return params_; } /// int rowFlags() const override { return CanBreakBefore | CanBreakAfter; } + /// + InsetIndex const * asInsetIndex() const override { return this; } private: /// bool hasSettings() const override;