commit fd5adacef25eb40f813dedd961920c353448213a
Author: Juergen Spitzmueller <[email protected]>
Date:   Tue Nov 1 10:20:50 2022 +0100

    Handle empty index subentries (#7820)
---
 src/insets/InsetIndex.cpp      |   31 ++++++++++++++++++++++++++++---
 src/insets/InsetIndex.h        |    4 +++-
 src/insets/InsetIndexMacro.cpp |    6 ++++++
 src/insets/InsetIndexMacro.h   |    2 ++
 4 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index 13a7b4d..b3024bf 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -160,7 +160,7 @@ void InsetIndex::latex(otexstream & ios, OutputParams const 
& runparams_in) cons
                getSortkey(os, runparams);
                os << "@";
                os << ourlatex.str();
-               getSubentries(os, runparams);
+               getSubentries(os, runparams, ourlatex.str());
                if (hasSeeRef()) {
                        os << "|";
                        os << 
insetindexpagerangetranslator_latex().find(params_.range);
@@ -214,7 +214,7 @@ void InsetIndex::latex(otexstream & ios, OutputParams const 
& runparams_in) cons
 
                odocstringstream subentries;
                otexstream otsub(subentries);
-               getSubentries(otsub, runparams);
+               getSubentries(otsub, runparams, ourlatex.str());
                if (subentries.str().empty()) {
                        // Separate the entries and subentries, i.e., split on 
"!".
                        // This goes wrong on an escaped "!", but as the escape
@@ -230,6 +230,12 @@ void InsetIndex::latex(otexstream & ios, OutputParams 
const & runparams_in) cons
                        vector<docstring>::const_iterator it2 = 
levels_plain.begin();
                        bool first = true;
                        for (; it != end; ++it) {
+                               if ((*it).empty()) {
+                                       emptySubentriesWarning(ourlatex.str());
+                                       if (it2 < levels_plain.end())
+                                               ++it2;
+                                       continue;
+                               }
                                // The separator needs to be put back when
                                // writing the levels, except for the first 
level
                                if (!first)
@@ -668,7 +674,22 @@ docstring InsetIndex::getSortkeyAsText(OutputParams const 
& runparams) const
 }
 
 
-void InsetIndex::getSubentries(otexstream & os, OutputParams const & 
runparams) const
+void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
+{
+       // Empty subentries crash makeindex. So warn and ignore this.
+       TeXErrors terr;
+       ErrorList & errorList = buffer().errorList("Export");
+       docstring const s = bformat(_("There is an empty index subentry in the 
entry '%1$s'.\n"
+                                     "It will be ignored in the output."), 
mainentry);
+       Paragraph const & par = buffer().paragraphs().front();
+       errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
+                                     {par.id(), 0}, {par.id(), -1}));
+       buffer().bufferErrors(terr, errorList);
+}
+
+
+void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
+                              docstring const & mainentry) const
 {
        Paragraph const & par = paragraphs().front();
        InsetList::const_iterator it = par.insetList().begin();
@@ -679,6 +700,10 @@ void InsetIndex::getSubentries(otexstream & os, 
OutputParams const & runparams)
                        InsetIndexMacro const & iim =
                                static_cast<InsetIndexMacro const &>(inset);
                        if (iim.params().type == 
InsetIndexMacroParams::Subentry) {
+                               if (iim.hasNoContent()) {
+                                       emptySubentriesWarning(mainentry);
+                                       continue;
+                               }
                                ++i;
                                if (i > 2)
                                        return;
diff --git a/src/insets/InsetIndex.h b/src/insets/InsetIndex.h
index 1aaf2d7..742ecae 100644
--- a/src/insets/InsetIndex.h
+++ b/src/insets/InsetIndex.h
@@ -102,7 +102,9 @@ private:
        ///
        docstring getSortkeyAsText(OutputParams const &) const;
        ///
-       void getSubentries(otexstream &, OutputParams const &) const;
+       void emptySubentriesWarning(docstring const & mainentry) const;
+       ///
+       void getSubentries(otexstream &, OutputParams const &, docstring const 
&) const;
        ///
        std::vector<docstring> getSubentriesAsText(OutputParams const &,
                                                   bool const asLabel = false) 
const;
diff --git a/src/insets/InsetIndexMacro.cpp b/src/insets/InsetIndexMacro.cpp
index 3175430..e4dc167 100644
--- a/src/insets/InsetIndexMacro.cpp
+++ b/src/insets/InsetIndexMacro.cpp
@@ -205,6 +205,12 @@ docstring InsetIndexMacro::getXhtml(XMLStream & xs, 
OutputParams const & runpara
 }
 
 
+bool InsetIndexMacro::hasNoContent() const
+{
+       return paragraphs().front().empty();
+}
+
+
 void InsetIndexMacro::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action()) {
diff --git a/src/insets/InsetIndexMacro.h b/src/insets/InsetIndexMacro.h
index 3d56e39..07109f8 100644
--- a/src/insets/InsetIndexMacro.h
+++ b/src/insets/InsetIndexMacro.h
@@ -66,6 +66,8 @@ public:
        int getPlaintext(odocstringstream &, OutputParams const &, size_t) 
const;
        ///
        void getDocbook(XMLStream &, OutputParams const &) const;
+       ///
+       bool hasNoContent() const;
 private:
        ///
        InsetCode lyxCode() const override;
-- 
lyx-cvs mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to