3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
13 #include "InsetIndex.h"
14 #include "InsetIndexMacro.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "ErrorList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "IndicesList.h"
27 #include "InsetList.h"
30 #include "LaTeXFeatures.h"
33 #include "output_latex.h"
34 #include "output_xhtml.h"
36 #include "texstream.h"
37 #include "TextClass.h"
38 #include "TocBackend.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/FileName.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
45 #include "support/Translator.h"
47 #include "frontends/alert.h"
53 #include <QThreadStorage>
56 using namespace lyx::support;
58 // Uncomment to enable InsetIndex-specific debugging mode: the tree for the index will be printed to std::cout.
59 // #define LYX_INSET_INDEX_DEBUG
65 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
66 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
68 PageRangeTranslator const init_insetindexpagerangetranslator()
70 PageRangeTranslator translator("none", InsetIndexParams::None);
71 translator.addPair("start", InsetIndexParams::Start);
72 translator.addPair("end", InsetIndexParams::End);
76 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
78 PageRangeTranslator translator("", InsetIndexParams::None);
79 translator.addPair("(", InsetIndexParams::Start);
80 translator.addPair(")", InsetIndexParams::End);
85 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
87 PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
88 translator.addPair(_("Starts page range"), InsetIndexParams::Start);
89 translator.addPair(_("Ends page range"), InsetIndexParams::End);
94 PageRangeTranslator const & insetindexpagerangetranslator()
96 static PageRangeTranslator const prtranslator =
97 init_insetindexpagerangetranslator();
102 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
104 static PageRangeTranslatorLoc const translator =
105 init_insetindexpagerangetranslator_loc();
110 PageRangeTranslator const & insetindexpagerangetranslator_latex()
112 static PageRangeTranslator const lttranslator =
113 init_insetindexpagerangetranslator_latex();
119 /////////////////////////////////////////////////////////////////////
123 ///////////////////////////////////////////////////////////////////////
126 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
127 : InsetCollapsible(buf), params_(params)
131 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
133 OutputParams runparams(runparams_in);
134 runparams.inIndexEntry = true;
138 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
139 && params_.index != "idx") {
141 os << escape(params_.index);
148 // Get the LaTeX output from InsetText. We need to deconstruct this later
149 // in order to check if we need to generate a sorting key
150 odocstringstream ourlatex;
151 otexstream ots(ourlatex);
152 InsetText::latex(ots, runparams);
153 if (runparams.find_effective()) {
154 // No need for special handling, if we are only searching for some patterns
155 os << ourlatex.str() << "}";
160 getSortkey(os, runparams);
162 os << ourlatex.str();
163 getSubentries(os, runparams, ourlatex.str());
166 os << insetindexpagerangetranslator_latex().find(params_.range);
167 getSeeRefs(os, runparams);
168 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
170 os << insetindexpagerangetranslator_latex().find(params_.range);
171 os << from_utf8(params_.pagefmt);
172 } else if (params_.range != InsetIndexParams::PageRange::None) {
174 os << insetindexpagerangetranslator_latex().find(params_.range);
177 // We check whether we need a sort key.
178 // If so, we use the plaintext version
179 odocstringstream ourplain;
180 InsetText::plaintext(ourplain, runparams);
182 // These are the LaTeX and plaintext representations
183 docstring latexstr = ourlatex.str();
184 docstring plainstr = ourplain.str();
186 // This will get what follows | if anything does,
187 // the command (e.g., see, textbf) for pagination
192 odocstringstream seeref;
193 otexstream otsee(seeref);
194 getSeeRefs(otsee, runparams);
196 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
197 cmd = from_utf8(params_.pagefmt);
199 // Check for the | separator to strip the cmd.
200 // This goes wrong on an escaped "|", but as the escape
201 // character can be changed in style files, we cannot
203 size_t pos = latexstr.find(from_ascii("|"));
204 if (pos != docstring::npos) {
205 // Put the bit after "|" into cmd...
206 cmd = latexstr.substr(pos + 1);
207 // ...and erase that stuff from latexstr
208 latexstr = latexstr.erase(pos);
209 // ...as well as from plainstr
210 size_t ppos = plainstr.find(from_ascii("|"));
211 if (ppos < plainstr.size())
212 plainstr.erase(ppos);
214 LYXERR0("The `|' separator was not found in the plaintext version!");
218 odocstringstream subentries;
219 otexstream otsub(subentries);
220 getSubentries(otsub, runparams, ourlatex.str());
221 if (subentries.str().empty()) {
222 // Separate the entries and subentries, i.e., split on "!".
223 // This goes wrong on an escaped "!", but as the escape
224 // character can be changed in style files, we cannot
226 std::vector<docstring> const levels =
227 getVectorFromString(latexstr, from_ascii("!"), true);
228 std::vector<docstring> const levels_plain =
229 getVectorFromString(plainstr, from_ascii("!"), true);
231 vector<docstring>::const_iterator it = levels.begin();
232 vector<docstring>::const_iterator end = levels.end();
233 vector<docstring>::const_iterator it2 = levels_plain.begin();
235 for (; it != end; ++it) {
237 emptySubentriesWarning(ourlatex.str());
238 if (it2 < levels_plain.end())
242 // The separator needs to be put back when
243 // writing the levels, except for the first level
249 // Now here comes the reason for this whole procedure:
250 // We try to correctly sort macros and formatted strings.
251 // If we find a command, prepend a plain text
252 // version of the content to get sorting right,
253 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
254 // We do this on all levels.
255 // We don't do it if the level already contains a '@', though.
256 // Plaintext might return nothing (e.g. for ERTs).
257 // In that case, we use LaTeX.
258 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
259 processLatexSorting(os, runparams, *it, spart);
260 if (it2 < levels_plain.end())
264 processLatexSorting(os, runparams, latexstr, plainstr);
265 os << subentries.str();
268 // At last, re-insert the command, separated by "|"
271 << insetindexpagerangetranslator_latex().find(params_.range)
273 } else if (params_.range != InsetIndexParams::PageRange::None) {
275 os << insetindexpagerangetranslator_latex().find(params_.range);
280 // In macros with moving arguments, such as \section,
281 // we store the index and output it after the macro (#2154)
282 if (runparams_in.postpone_fragile_stuff)
283 runparams_in.post_macro += os.str();
289 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
290 docstring const latex, docstring const spart) const
292 if (contains(latex, '\\') && !contains(latex, '@')) {
293 // Now we need to validate that all characters in
294 // the sorting part are representable in the current
295 // encoding. If not try the LaTeX macro which might
296 // or might not be a good choice, and issue a warning.
297 pair<docstring, docstring> spart_latexed =
298 runparams.encoding->latexString(spart, runparams.dryrun);
299 if (!spart_latexed.second.empty())
300 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
301 if (spart != spart_latexed.first && !runparams.dryrun) {
303 ErrorList & errorList = buffer().errorList("Export");
304 docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
305 "problems with the entry '%1$s'.\n"
306 "Please specify the sorting of this entry manually, as "
307 "explained in the User Guide."), spart);
308 Paragraph const & par = buffer().paragraphs().front();
309 errorList.push_back(ErrorItem(_("Index sorting failed"), s,
310 {par.id(), 0}, {par.id(), -1}));
311 buffer().bufferErrors(terr, errorList);
313 // Remove remaining \'s from the sort key
314 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
315 // Plain quotes need to be escaped, however (#10649), as this
316 // is the default escape character
317 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
319 // Now insert the sortkey, separated by '@'.
323 // Insert the actual level text
328 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
330 // Two ways of processing this inset are implemented:
331 // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
332 // - the modern one, based on precise insets for indexing features
333 // Like the LaTeX implementation, consider the user chooses either of those options.
335 // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
336 // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
337 // otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
338 odocstringstream odss;
339 otexstream ots(odss);
340 InsetText::latex(ots, runparams);
341 docstring latexString = trim(odss.str());
343 // Handle several indices (indicated in the inset instead of the raw latexString).
344 docstring indexType = from_utf8("");
345 if (buffer().masterBuffer()->params().use_indices) {
346 indexType += " type=\"" + params_.index + "\"";
349 // Split the string into its main constituents: terms, and command (see, see also, range).
350 size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
351 docstring indexTerms = latexString.substr(0, positionVerticalBar);
353 if (positionVerticalBar != lyx::docstring::npos) {
354 command = latexString.substr(positionVerticalBar + 1);
357 // Handle sorting issues, with @.
360 sortAs = getSortkeyAsText(runparams);
361 // indexTerms may contain a sort key if the user has both the inset and the manual key.
363 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
364 if (sortingElements.size() == 2) {
365 sortAs = sortingElements[0];
366 indexTerms = sortingElements[1];
370 // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
371 vector<docstring> terms;
372 if (const vector<docstring> potential_terms = getSubentriesAsText(runparams); !potential_terms.empty()) {
373 terms = potential_terms;
374 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
375 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
376 // full string within this inset (i.e. without the subinsets).
377 terms.insert(terms.begin(), latexString);
379 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
382 // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
383 const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
384 const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
385 latexString.find(from_ascii("|(")) != lyx::docstring::npos;
386 const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
387 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
390 // Remove the ranges from the command if they do not appear at the beginning.
392 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
393 command.erase(index, 1);
395 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
396 command.erase(index, 1);
398 // Remove the ranges when they are the only vertical bar in the complete string.
399 if (command[0] == '(' || command[0] == ')')
403 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
404 // Both commands are mutually exclusive!
405 docstring see = getSeeAsText(runparams);
406 vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
408 if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
409 // Unescape brackets.
411 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
412 command.erase(index, 1);
414 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
415 command.erase(index, 1);
417 // Retrieve the part between brackets, and remove the complete seealso.
418 size_t positionOpeningBracket = command.find(from_ascii("{"));
419 size_t positionClosingBracket = command.find(from_ascii("}"));
420 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
422 // Parse the list of referenced entries (or a single one for see).
423 if (command.substr(0, 7) == "seealso") {
424 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
428 if (see.find(from_ascii(",")) != std::string::npos) {
429 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
430 "Complete entry: \"") + latexString + from_utf8("\"");
432 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
436 // Remove the complete see/seealso from the commands, in case there is something else to parse.
437 command = command.substr(positionClosingBracket + 1);
440 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
441 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
442 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
443 // no range will be detected.
444 // TODO: Could handle formatting as significance="preferred"?
445 if (!command.empty()) {
446 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
447 + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
449 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
452 // Write all of this down.
453 if (terms.empty() && !hasEndRange) {
454 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
456 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
458 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
459 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
460 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
461 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
462 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
464 // indexType can only be used for singular and startofrange types!
466 if (!hasStartRange && !hasEndRange) {
469 // Append an ID if uniqueness is not guaranteed across the document.
470 static QThreadStorage<set<docstring>> tKnownTermLists;
471 static QThreadStorage<int> tID;
473 set<docstring> &knownTermLists = tKnownTermLists.localData();
474 int &ID = tID.localData();
476 if (!tID.hasLocalData()) {
480 // Modify the index terms to add the unique ID if needed.
481 docstring newIndexTerms = indexTerms;
482 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
483 newIndexTerms += from_ascii(string("-") + to_string(ID));
485 // Only increment for the end of range, so that the same number is used for the start of range.
491 // Term list not yet known: add it to the set AFTER the end of range. After
492 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
493 knownTermLists.insert(indexTerms);
496 // Generate the attributes.
497 docstring id = xml::cleanID(newIndexTerms);
499 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
501 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
505 // Handle the index terms (including the specific index for this entry).
507 xs << xml::CompTag("indexterm", attrs);
509 xs << xml::StartTag("indexterm", attrs);
510 if (!terms.empty()) { // hasEndRange has no content.
512 if (!sortAs.empty()) {
513 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
516 xs << xml::StartTag("primary", attr);
518 xs << xml::EndTag("primary");
520 if (terms.size() > 1) {
521 xs << xml::StartTag("secondary");
523 xs << xml::EndTag("secondary");
525 if (terms.size() > 2) {
526 xs << xml::StartTag("tertiary");
528 xs << xml::EndTag("tertiary");
531 // Handle see and see also.
533 xs << xml::StartTag("see");
535 xs << xml::EndTag("see");
538 if (!seeAlsoes.empty()) {
539 for (auto &entry : seeAlsoes) {
540 xs << xml::StartTag("seealso");
542 xs << xml::EndTag("seealso");
547 xs << xml::EndTag("indexterm");
553 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
555 // we just print an anchor, taking the paragraph ID from
556 // our own interior paragraph, which doesn't get printed
557 std::string const magic = paragraphs().front().magicLabel();
558 std::string const attr = "id='" + magic + "'";
559 xs << xml::CompTag("a", attr);
564 bool InsetIndex::showInsetDialog(BufferView * bv) const
566 bv->showDialog("index", params2string(params_),
567 const_cast<InsetIndex *>(this));
572 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
574 switch (cmd.action()) {
576 case LFUN_INSET_MODIFY: {
577 if (cmd.getArg(0) == "changetype") {
578 cur.recordUndoInset(this);
579 params_.index = from_utf8(cmd.getArg(1));
582 InsetIndexParams params;
583 InsetIndex::string2params(to_utf8(cmd.argument()), params);
584 cur.recordUndoInset(this);
585 params_.index = params.index;
586 params_.range = params.range;
587 params_.pagefmt = params.pagefmt;
588 // what we really want here is a TOC update, but that means
589 // a full buffer update
590 cur.forceBufferUpdate();
594 case LFUN_INSET_DIALOG_UPDATE:
595 cur.bv().updateDialog("index", params2string(params_));
598 case LFUN_PARAGRAPH_BREAK: {
599 // Since this inset in single-par anyway, let's use
600 // return to enter subentries
601 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
607 InsetCollapsible::doDispatch(cur, cmd);
613 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
614 FuncStatus & flag) const
616 switch (cmd.action()) {
618 case LFUN_INSET_MODIFY:
619 if (cmd.getArg(0) == "changetype") {
620 docstring const newtype = from_utf8(cmd.getArg(1));
621 Buffer const & realbuffer = *buffer().masterBuffer();
622 IndicesList const & indiceslist = realbuffer.params().indiceslist();
623 Index const * index = indiceslist.findShortcut(newtype);
624 flag.setEnabled(index != 0);
626 from_utf8(cmd.getArg(1)) == params_.index);
629 return InsetCollapsible::getStatus(cur, cmd, flag);
631 case LFUN_INSET_DIALOG_UPDATE: {
632 Buffer const & realbuffer = *buffer().masterBuffer();
633 flag.setEnabled(realbuffer.params().use_indices);
637 case LFUN_PARAGRAPH_BREAK:
638 return macrosPossible("subentry");
640 case LFUN_INDEXMACRO_INSERT:
641 return macrosPossible(cmd.getArg(0));
644 return InsetCollapsible::getStatus(cur, cmd, flag);
649 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
651 Paragraph const & par = paragraphs().front();
652 InsetList::const_iterator it = par.insetList().begin();
653 for (; it != par.insetList().end(); ++it) {
654 Inset & inset = *it->inset;
655 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
656 InsetIndexMacro const & iim =
657 static_cast<InsetIndexMacro const &>(inset);
658 iim.getLatex(os, runparams);
665 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
667 Paragraph const & par = paragraphs().front();
668 InsetList::const_iterator it = par.insetList().begin();
669 for (; it != par.insetList().end(); ++it) {
670 Inset & inset = *it->inset;
671 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
673 InsetIndexMacro const & iim =
674 static_cast<InsetIndexMacro const &>(inset);
675 iim.getLatex(os, runparams);
679 return from_ascii("");
683 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
685 // Empty subentries crash makeindex. So warn and ignore this.
687 ErrorList & errorList = buffer().errorList("Export");
688 docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
689 "It will be ignored in the output."), mainentry);
690 Paragraph const & par = buffer().paragraphs().front();
691 errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
692 {par.id(), 0}, {par.id(), -1}));
693 buffer().bufferErrors(terr, errorList);
697 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
698 docstring const & mainentry) const
700 Paragraph const & par = paragraphs().front();
701 InsetList::const_iterator it = par.insetList().begin();
703 for (; it != par.insetList().end(); ++it) {
704 Inset & inset = *it->inset;
705 if (inset.lyxCode() == INDEXMACRO_CODE) {
706 InsetIndexMacro const & iim =
707 static_cast<InsetIndexMacro const &>(inset);
708 if (iim.params().type == InsetIndexMacroParams::Subentry) {
709 if (iim.hasNoContent()) {
710 emptySubentriesWarning(mainentry);
717 iim.getLatex(os, runparams);
724 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
725 bool const asLabel) const
727 std::vector<docstring> subentries;
729 Paragraph const & par = paragraphs().front();
730 InsetList::const_iterator it = par.insetList().begin();
732 for (; it != par.insetList().end(); ++it) {
733 Inset & inset = *it->inset;
734 if (inset.lyxCode() == INDEXMACRO_CODE) {
735 InsetIndexMacro const & iim =
736 static_cast<InsetIndexMacro const &>(inset);
737 if (iim.params().type == InsetIndexMacroParams::Subentry) {
743 docstring const sl = iim.getNewLabel(l);
744 subentries.emplace_back(sl);
747 iim.getLatex(os, runparams);
748 subentries.emplace_back(os.str());
758 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
761 InsetText::latex(os, runparams);
766 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
768 Paragraph const & par = paragraphs().front();
769 InsetList::const_iterator it = par.insetList().begin();
770 for (; it != par.insetList().end(); ++it) {
771 Inset & inset = *it->inset;
772 if (inset.lyxCode() == INDEXMACRO_CODE) {
773 InsetIndexMacro const & iim =
774 static_cast<InsetIndexMacro const &>(inset);
775 if (iim.params().type == InsetIndexMacroParams::See
776 || iim.params().type == InsetIndexMacroParams::Seealso) {
777 iim.getLatex(os, runparams);
785 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
786 bool const asLabel) const
788 Paragraph const & par = paragraphs().front();
789 InsetList::const_iterator it = par.insetList().begin();
790 for (; it != par.insetList().end(); ++it) {
791 Inset & inset = *it->inset;
792 if (inset.lyxCode() == INDEXMACRO_CODE) {
793 InsetIndexMacro const & iim =
794 static_cast<InsetIndexMacro const &>(inset);
795 if (iim.params().type == InsetIndexMacroParams::See) {
798 return iim.getNewLabel(l);
801 iim.getLatex(os, runparams);
807 return from_ascii("");
811 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
812 bool const asLabel) const
814 std::vector<docstring> seeAlsoes;
816 Paragraph const & par = paragraphs().front();
817 InsetList::const_iterator it = par.insetList().begin();
818 for (; it != par.insetList().end(); ++it) {
819 Inset & inset = *it->inset;
820 if (inset.lyxCode() == INDEXMACRO_CODE) {
821 InsetIndexMacro const & iim =
822 static_cast<InsetIndexMacro const &>(inset);
823 if (iim.params().type == InsetIndexMacroParams::Seealso) {
826 seeAlsoes.emplace_back(iim.getNewLabel(l));
829 iim.getLatex(os, runparams);
830 seeAlsoes.emplace_back(os.str());
842 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
843 const std::set<InsetIndexMacroParams::Type> types = {})
845 Paragraph const & par = inset_index->paragraphs().front();
846 InsetList::const_iterator it = par.insetList().begin();
847 for (; it != par.insetList().end(); ++it) {
848 Inset & inset = *it->inset;
849 if (inset.lyxCode() == code) {
853 LASSERT(code == INDEXMACRO_CODE, return false);
854 InsetIndexMacro const & iim =
855 static_cast<InsetIndexMacro const &>(inset);
856 if (types.find(iim.params().type) != types.end())
866 bool InsetIndex::hasSubentries() const
868 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
872 bool InsetIndex::hasSeeRef() const
874 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
878 bool InsetIndex::hasSortKey() const
880 return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
884 bool InsetIndex::macrosPossible(string const type) const
886 if (type != "see" && type != "seealso"
887 && type != "sortkey" && type != "subentry")
890 Paragraph const & par = paragraphs().front();
891 InsetList::const_iterator it = par.insetList().begin();
893 for (; it != par.insetList().end(); ++it) {
894 Inset & inset = *it->inset;
895 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
897 if (inset.lyxCode() == INDEXMACRO_CODE) {
898 InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
899 if ((type == "see" || type == "seealso")
900 && (iim.params().type == InsetIndexMacroParams::See
901 || iim.params().type == InsetIndexMacroParams::Seealso))
903 if (type == "subentry"
904 && iim.params().type == InsetIndexMacroParams::Subentry) {
915 ColorCode InsetIndex::labelColor() const
917 if (params_.index.empty() || params_.index == from_ascii("idx"))
918 return InsetCollapsible::labelColor();
920 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
921 + "@" + buffer().fileName().absFileName());
923 c = InsetCollapsible::labelColor();
928 docstring InsetIndex::toolTip(BufferView const &, int, int) const
930 docstring tip = _("Index Entry");
931 if (buffer().params().use_indices && !params_.index.empty()) {
932 Buffer const & realbuffer = *buffer().masterBuffer();
933 IndicesList const & indiceslist = realbuffer.params().indiceslist();
935 Index const * index = indiceslist.findShortcut(params_.index);
937 tip += _("unknown type!");
939 tip += index->index();
943 docstring res = toolTipText(tip);
944 if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
945 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
946 if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
947 res += "\n" + _("Pagination format:") + " ";
948 if (params_.pagefmt == "textbf")
950 else if (params_.pagefmt == "textit")
952 else if (params_.pagefmt == "emph")
953 res += _("emphasized");
955 res += from_utf8(params_.pagefmt);
961 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
963 InsetLayout const & il = getLayout();
964 docstring label = translateIfPossible(il.labelstring());
966 if (buffer().params().use_indices && !params_.index.empty()) {
967 Buffer const & realbuffer = *buffer().masterBuffer();
968 IndicesList const & indiceslist = realbuffer.params().indiceslist();
970 Index const * index = indiceslist.findShortcut(params_.index);
972 label += _("unknown type!");
974 label += index->index();
979 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
982 res = getNewLabel(label);
983 OutputParams const rp(0);
984 vector<docstring> sublbls = getSubentriesAsText(rp, true);
985 for (auto const & sublbl : sublbls) {
986 res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
989 docstring see = getSeeAsText(rp, true);
990 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
991 see = getSeeAlsoesAsText(rp, true).front();
993 res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
997 if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
998 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
1003 void InsetIndex::write(ostream & os) const
1005 os << to_utf8(layoutName());
1007 InsetCollapsible::write(os);
1011 void InsetIndex::read(Lexer & lex)
1014 InsetCollapsible::read(lex);
1018 string InsetIndex::params2string(InsetIndexParams const & params)
1027 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1029 params = InsetIndexParams();
1033 istringstream data(in);
1035 lex.setStream(data);
1036 lex.setContext("InsetIndex::string2params");
1042 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1043 UpdateType utype, TocBackend & backend) const
1045 DocIterator pit = cpit;
1046 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1048 InsetLayout const & il = getLayout();
1049 docstring label = translateIfPossible(il.labelstring());
1050 if (!il.contentaslabel())
1053 str = getNewLabel(label);
1054 OutputParams const rp(0);
1055 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1056 for (auto const & sublbl : sublbls) {
1057 str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1058 str += " " + sublbl;
1060 docstring see = getSeeAsText(rp, true);
1061 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1062 see = getSeeAlsoesAsText(rp, true).front();
1064 str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1068 string type = "index";
1069 if (buffer().masterBuffer()->params().use_indices)
1070 type += ":" + to_utf8(params_.index);
1071 TocBuilder & b = backend.builder(type);
1072 b.pushItem(pit, str, output_active);
1073 // Proceed with the rest of the inset.
1074 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1079 void InsetIndex::validate(LaTeXFeatures & features) const
1081 if (buffer().masterBuffer()->params().use_indices
1082 && !params_.index.empty()
1083 && params_.index != "idx")
1084 features.require("splitidx");
1085 InsetCollapsible::validate(features);
1089 string InsetIndex::contextMenuName() const
1091 return "context-index";
1095 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1097 // We override the implementation of InsetCollapsible,
1098 // because we have eytra entries.
1099 string owncm = "context-edit-index;";
1100 return owncm + InsetCollapsible::contextMenu(bv, x, y);
1104 bool InsetIndex::hasSettings() const
1110 bool InsetIndex::insetAllowed(InsetCode code) const
1113 case INDEXMACRO_CODE:
1114 case INDEXMACRO_SORTKEY_CODE:
1119 return InsetCollapsible::insetAllowed(code);
1124 /////////////////////////////////////////////////////////////////////
1128 ///////////////////////////////////////////////////////////////////////
1131 void InsetIndexParams::write(ostream & os) const
1135 os << to_utf8(index);
1140 << insetindexpagerangetranslator().find(range)
1148 void InsetIndexParams::read(Lexer & lex)
1151 index = lex.getDocString();
1153 index = from_ascii("idx");
1154 if (lex.checkFor("range")) {
1155 string st = lex.getString();
1156 if (lex.eatLine()) {
1157 st = lex.getString();
1158 range = insetindexpagerangetranslator().find(lex.getString());
1161 if (lex.checkFor("pageformat") && lex.eatLine()) {
1162 pagefmt = lex.getString();
1167 /////////////////////////////////////////////////////////////////////
1171 ///////////////////////////////////////////////////////////////////////
1173 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1174 : InsetCommand(buf, p)
1178 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1180 static ParamInfo param_info_;
1181 if (param_info_.empty()) {
1182 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1183 ParamInfo::HANDLING_ESCAPE);
1184 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1185 ParamInfo::HANDLING_LATEXIFY);
1186 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1192 docstring InsetPrintIndex::screenLabel() const
1194 bool const printall = suffixIs(getCmdName(), '*');
1195 bool const multind = buffer().masterBuffer()->params().use_indices;
1197 && getParam("type") == from_ascii("idx"))
1198 || (getParam("type").empty() && !printall))
1200 Buffer const & realbuffer = *buffer().masterBuffer();
1201 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1202 Index const * index = indiceslist.findShortcut(getParam("type"));
1203 if (!index && !printall)
1204 return _("Unknown index type!");
1205 docstring res = printall ? _("All indexes") : index->index();
1207 res += " (" + _("non-active") + ")";
1208 else if (contains(getCmdName(), "printsubindex"))
1209 res += " (" + _("subindex") + ")";
1214 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1216 return s == "printindex" || s == "printsubindex"
1217 || s == "printindex*" || s == "printsubindex*";
1221 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1223 switch (cmd.action()) {
1225 case LFUN_INSET_MODIFY: {
1226 if (cmd.argument() == from_ascii("toggle-subindex")) {
1227 string scmd = getCmdName();
1228 if (contains(scmd, "printindex"))
1229 scmd = subst(scmd, "printindex", "printsubindex");
1231 scmd = subst(scmd, "printsubindex", "printindex");
1235 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1236 string scmd = getCmdName();
1237 if (suffixIs(scmd, '*'))
1241 setParam("type", docstring());
1245 InsetCommandParams p(INDEX_PRINT_CODE);
1247 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1248 if (p.getCmdName().empty()) {
1249 cur.noScreenUpdate();
1258 InsetCommand::doDispatch(cur, cmd);
1264 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1265 FuncStatus & status) const
1267 switch (cmd.action()) {
1269 case LFUN_INSET_MODIFY: {
1270 if (cmd.argument() == from_ascii("toggle-subindex")) {
1271 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1272 status.setOnOff(contains(getCmdName(), "printsubindex"));
1274 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1275 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1276 status.setOnOff(suffixIs(getCmdName(), '*'));
1278 } if (cmd.getArg(0) == "index_print"
1279 && cmd.getArg(1) == "CommandInset") {
1280 InsetCommandParams p(INDEX_PRINT_CODE);
1281 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1282 if (suffixIs(p.getCmdName(), '*')) {
1283 status.setEnabled(true);
1284 status.setOnOff(false);
1287 Buffer const & realbuffer = *buffer().masterBuffer();
1288 IndicesList const & indiceslist =
1289 realbuffer.params().indiceslist();
1290 Index const * index = indiceslist.findShortcut(p["type"]);
1291 status.setEnabled(index != 0);
1292 status.setOnOff(p["type"] == getParam("type"));
1295 return InsetCommand::getStatus(cur, cmd, status);
1298 case LFUN_INSET_DIALOG_UPDATE: {
1299 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1304 return InsetCommand::getStatus(cur, cmd, status);
1309 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1311 Index const * index =
1312 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1314 setParam("name", index->index());
1318 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1320 if (!buffer().masterBuffer()->params().use_indices) {
1321 if (getParam("type") == from_ascii("idx"))
1322 os << "\\printindex" << termcmd;
1325 OutputParams runparams = runparams_in;
1326 os << getCommand(runparams);
1330 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1332 features.require("makeidx");
1333 if (buffer().masterBuffer()->params().use_indices)
1334 features.require("splitidx");
1335 InsetCommand::validate(features);
1339 string InsetPrintIndex::contextMenuName() const
1341 return buffer().masterBuffer()->params().use_indices ?
1342 "context-indexprint" : string();
1346 bool InsetPrintIndex::hasSettings() const
1348 return buffer().masterBuffer()->params().use_indices;
1355 /// Builds an entry for the index.
1356 IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1358 LASSERT(runparams, return);
1360 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1361 odocstringstream entry;
1362 OutputParams ours = *runparams;
1363 ours.for_toc = false;
1364 inset_->plaintext(entry, ours);
1365 entry_ = entry.str();
1367 // Determine in which index this entry belongs to.
1368 if (inset_->buffer().masterBuffer()->params().use_indices) {
1369 index_ = inset_->params_.index;
1372 // Attempt parsing the inset.
1379 /// When parsing this entry, some errors may be found; they are reported as a single string.
1380 // It is up to the caller to send this string to LYXERR and the output file, as needed.
1381 const docstring & output_error() const
1383 return output_error_;
1386 void output_error(XMLStream xs) const
1388 LYXERR0(output_error());
1389 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + output_error() + from_utf8(" -->\n"));
1396 std::cout << to_utf8(entry_) << std::endl;
1398 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1399 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1400 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1401 // is not allowed (i.e. behaviour is undefined).
1402 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1403 || inset_->params_.range != InsetIndexParams::PageRange::None;
1404 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1405 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1407 if (is_definitely_legacy && is_definitely_modern)
1408 output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1411 // - is_definitely_modern == true:
1412 // - is_definitely_legacy == true: error (return whatever)
1413 // - is_definitely_legacy == false: return modern
1414 // - is_definitely_modern == false:
1415 // - is_definitely_legacy == true: return legacy
1416 // - is_definitely_legacy == false: return modern
1417 return !is_definitely_legacy;
1420 void parseAsModern()
1422 LASSERT(runparams_, return);
1424 if (inset_->hasSortKey()) {
1425 sort_as_ = inset_->getSortkeyAsText(*runparams_);
1428 terms_ = inset_->getSubentriesAsText(*runparams_);
1429 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1430 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1431 // full string within this inset (i.e. without the subinsets).
1432 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1434 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1435 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1437 see_ = inset_->getSeeAsText(*runparams_);
1438 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1441 void parseAsLegacy() {
1442 // Determine if some features are known not to be supported. For now, this is only formatting like
1443 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1444 // @ is supported, but only for sorting, without specific formatting.
1445 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1446 output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1447 "Complete entry: \"") + entry_ + from_utf8("\". ");
1449 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1450 output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1451 "Complete entry: \"") + entry_ + from_utf8("\". ");
1454 // Split the string into its main constituents: terms, and command (see, see also, range).
1455 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1456 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1458 if (positionVerticalBar != lyx::docstring::npos) {
1459 command = entry_.substr(positionVerticalBar + 1);
1462 // Handle sorting issues, with @.
1463 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1464 if (sortingElements.size() == 2) {
1465 sort_as_ = sortingElements[0];
1466 indexTerms = sortingElements[1];
1469 // Handle entries, subentries, and subsubentries.
1470 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1472 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1473 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1474 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1476 // - Remove the ranges from the command if they do not appear at the beginning.
1477 size_t range_index = 0;
1478 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1479 command.erase(range_index, 1);
1481 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1482 command.erase(range_index, 1);
1484 // - Remove the ranges when they are the only vertical bar in the complete string.
1485 if (command[0] == '(' || command[0] == ')')
1486 command.erase(0, 1);
1488 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1489 // Both commands are mutually exclusive!
1490 if (command.substr(0, 3) == "see") {
1491 // Unescape brackets.
1492 size_t index_argument_begin = 0;
1493 while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1494 command.erase(index_argument_begin, 1);
1495 size_t index_argument_end = 0;
1496 while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1497 command.erase(index_argument_end, 1);
1499 // Retrieve the part between brackets, and remove the complete seealso.
1500 size_t position_opening_bracket = command.find(from_ascii("{"));
1501 size_t position_closing_bracket = command.find(from_ascii("}"));
1502 docstring argument = command.substr(position_opening_bracket + 1,
1503 position_closing_bracket - position_opening_bracket - 1);
1505 // Parse the argument of referenced entries (or a single one for see).
1506 if (command.substr(0, 7) == "seealso") {
1507 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1511 if (see_.find(from_ascii(",")) != std::string::npos) {
1512 output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1513 "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1517 // Remove the complete see/seealso from the commands, in case there is something else to parse.
1518 command = command.substr(position_closing_bracket + 1);
1521 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1522 // things like formatting the entry or the page number, other strings for sorting.
1523 // https://wiki.lyx.org/Tips/Indexing
1524 // If there are such things in the index entry, then this code may miserably fail. For example, for
1525 // "Peter|(textbf", no range will be detected.
1526 if (!command.empty()) {
1527 output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1528 + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1534 return terms_.size();
1537 const std::vector<docstring>& terms() const {
1541 std::vector<docstring>& terms() {
1545 const InsetIndex* inset() const {
1550 // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1551 // the constructor).
1552 const InsetIndex * inset_;
1553 OutputParams const * runparams_;
1555 docstring index_; // Useful when there are multiple indices in the same document.
1557 // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1558 // constructor is done).
1559 docstring output_error_;
1561 // Parsed index entry.
1562 std::vector<docstring> terms_; // Up to three entries, in general.
1565 bool has_start_range_;
1566 bool has_end_range_;
1568 vector<docstring> see_alsoes_;
1570 // Operators used for sorting entries (alphabetical order).
1571 friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1574 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1576 if (lhs.terms_.empty())
1579 for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1580 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1589 std::string generateCssClassAtDepth(unsigned depth) {
1590 std::string css_class = "entry";
1594 css_class.insert(0, "sub");
1601 std::vector<IndexEntry> entries;
1602 std::vector<IndexNode*> children;
1605 docstring termAtLevel(const IndexNode* node, unsigned depth)
1607 // The typical entry has a depth of 1 to 3: the call stack would then be at most 4 (due to the root node). This
1608 // function could be made constant time by copying the term in each node, but that would make data duplication that
1609 // may fall out of sync; the performance benefit would probably be negligible.
1610 if (!node->entries.empty()) {
1611 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1612 return node->entries.begin()->terms()[depth];
1615 if (!node->children.empty()) {
1616 return termAtLevel(*node->children.begin(), depth);
1619 LASSERT(false, return from_ascii(""));
1622 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1624 // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1625 for (IndexNode* child : node->children) {
1626 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1627 if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1628 // All term entries match: it's an entry.
1629 child->entries.emplace_back(entry);
1632 insertIntoNode(entry, child, depth + 1);
1638 // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1639 // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1640 // first entry for a word has several levels that never appeared.
1641 // In particular, this case is called for the first entry.
1642 IndexNode* new_node = node;
1644 new_node->children.emplace_back(new IndexNode{{}, {}});
1645 new_node = new_node->children.back();
1647 } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1648 new_node->entries.emplace_back(entry);
1651 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1653 // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1654 // thanks to the implementation of operator<.
1655 // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1656 // unsorted entries would make its complexity blow up).
1657 stable_sort(entries.begin(), entries.end());
1659 // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1661 auto* index_root = new IndexNode{{}, {}};
1662 for (const IndexEntry& entry : entries) {
1663 insertIntoNode(entry, index_root);
1669 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1671 LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1673 xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1675 xs << XMLStream::ESCAPE_NONE << termAtLevel(root_node, depth);
1676 // By tree assumption, all the entries at this node have the same set of terms.
1678 if (!root_node->entries.empty()) {
1679 xs << XMLStream::ESCAPE_NONE << " — "; // Em dash, i.e. long (---).
1680 unsigned entry_number = 1;
1682 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1683 std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1684 xs << xml::StartTag("a", link_attr);
1685 xs << from_ascii(std::to_string(entry_number));
1686 xs << xml::EndTag("a");
1689 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1690 const IndexEntry &entry = root_node->entries[i];
1692 switch (entry.inset()->params().range) {
1693 case InsetIndexParams::PageRange::None:
1694 writeLinkToEntry(entry, entry_number);
1696 case InsetIndexParams::PageRange::Start: {
1697 // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1698 // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1699 // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1701 const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1702 root_node->entries[i + 1].inset()->params().range ==
1703 InsetIndexParams::PageRange::End;
1704 // No need to check if both entries are for the same terms: they are in the same IndexNode.
1706 writeLinkToEntry(entry, entry_number);
1707 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1709 if (nextEntryIsEnd) {
1710 // Skip the next entry in the loop, write it right now, after the dash.
1713 writeLinkToEntry(root_node->entries[i], entry_number);
1717 case InsetIndexParams::PageRange::End:
1718 // This range end was not caught by the range start, do it now to avoid losing content.
1719 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1720 writeLinkToEntry(root_node->entries[i], entry_number);
1723 if (i < root_node->entries.size() - 1) {
1730 if (!root_node->entries.empty() && !root_node->children.empty()) {
1734 if (!root_node->children.empty()) {
1735 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1738 for (const IndexNode* child : root_node->children) {
1739 outputIndexPage(xs, child, depth + 1);
1742 xs << xml::EndTag("ul");
1746 xs << xml::EndTag("li");
1750 #ifdef LYX_INSET_INDEX_DEBUG
1751 void printTree(const IndexNode* root_node, unsigned depth = 0)
1753 static const std::string pattern = " ";
1755 for (unsigned i = 0; i < depth; ++i) {
1758 const std::string prefix_long = prefix + pattern + pattern;
1760 docstring term_at_level;
1762 // The root has no term.
1763 std::cout << "<ROOT>" << std::endl;
1765 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1766 term_at_level = termAtLevel(root_node, depth - 1);
1767 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1771 for (const IndexEntry& entry : root_node->entries) {
1772 if (entry.terms().size() != depth) {
1773 std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1775 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1776 std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1781 for (const IndexNode* node : root_node->children) {
1782 printTree(node, depth + 1);
1785 #endif // LYX_INSET_INDEX_DEBUG
1789 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1791 BufferParams const & bp = buffer().masterBuffer()->params();
1793 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1797 // Collect the index entries in a form we can use them.
1798 vector<IndexEntry> entries;
1799 const docstring & indexType = params().getParamOr("type", from_ascii("idx"));
1800 for (const TocItem& item : *toc) {
1801 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1802 if (item.isOutput() && inset->params().index == indexType)
1803 entries.emplace_back(IndexEntry{inset, &op});
1806 // If all the index entries are in notes or not displayed, get out sooner.
1807 if (entries.empty())
1810 const IndexNode* index_root = buildIndexTree(entries);
1811 #ifdef LYX_INSET_INDEX_DEBUG
1812 printTree(index_root);
1815 // Start generating the XHTML index.
1816 Layout const & lay = bp.documentClass().htmlTOCLayout();
1817 string const & tocclass = lay.defaultCSSClass();
1818 string const tocattr = "class='index " + tocclass + "'";
1819 docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1821 // we'll use our own stream, because we are going to defer everything.
1822 // that's how we deal with the fact that we're probably inside a standard
1823 // paragraph, and we don't want to be.
1824 odocstringstream ods;
1827 xs << xml::StartTag("div", tocattr);
1829 xs << xml::StartTag(lay.htmltag(), lay.htmlattr());
1830 xs << translateIfPossible(indexName, op.local_font->language()->lang());
1831 xs << xml::EndTag(lay.htmltag());
1833 xs << xml::StartTag("ul", "class='main'");
1836 LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1837 for (const IndexNode* node : index_root->children) {
1838 outputIndexPage(xs, node);
1841 xs << xml::EndTag("ul");
1843 xs << xml::EndTag("div");