]> git.lyx.org Git - features.git/commitdiff
Introduce index-tag-all lfun
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 5 Nov 2022 17:39:33 +0000 (18:39 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 5 Nov 2022 17:39:33 +0000 (18:39 +0100)
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
lib/doc/UserGuide.lyx
lib/ui/stdcontext.inc
src/BufferView.cpp
src/FuncCode.h
src/LyXAction.cpp
src/insets/Inset.h
src/insets/InsetIndex.cpp
src/insets/InsetIndex.h

index 2ae5e72a5270e06e7985dcd7e70f2f26dddbde09..3c211a2025d7de4f47ce2a792f38cbf55c658417 100644 (file)
 
 * 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.
 
index 1de5ee5846c15e2ab383d927889e0f36c99efeda..a0533dfcc4bb2d21a4f4509088ed337796e54f40 100644 (file)
@@ -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
index 64cafd0012f57bb99a9f965e914e379de5710010..d6f1fd847011bbb5e47f72d529bc15c497412b36 100644 (file)
@@ -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"
index b36b8ee5b6a694fc226862fead4a719e370946bb..0bbf6497964536773073c44e2490c486d3f25476 100644 (file)
@@ -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<InsetIndex &>(*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")));
index 3667a98c1da6bd00bc7d7aaffaefcb12d30a6f18..956a3a4890c4fa1cf953ee88003edec879f541d2 100644 (file)
@@ -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
 };
 
index e1f91d2d89dd0f13b3eb11f1c90d5bb9b34fb804..16926059459674f8adca283b9e9d4cb8a5088690 100644 (file)
@@ -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.
index c73fa49d68ad962be1ad27504893a06ef3ace346..fd55d69215ae05c947582207369500942a0000b6 100644 (file)
@@ -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?
index 839d09472a42744375bcc4fa9264cd34e9e398d6..bec373d5dd5dea66838c1cc7995c4c243da938c0 100644 (file)
@@ -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);
        }
index a89a684974fa39e1ec9d4df81a44bc54981884b3..b3fe2713d9e66d6079dcb340a303061a752ce4a1 100644 (file)
@@ -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;