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 "CutAndPaste.h"
22 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "IndicesList.h"
28 #include "InsetList.h"
30 #include "Paragraph.h"
32 #include "LaTeXFeatures.h"
35 #include "output_latex.h"
36 #include "output_xhtml.h"
38 #include "texstream.h"
39 #include "TextClass.h"
40 #include "TocBackend.h"
42 #include "support/debug.h"
43 #include "support/docstream.h"
44 #include "support/FileName.h"
45 #include "support/gettext.h"
46 #include "support/lstrings.h"
47 #include "support/Translator.h"
49 #include "frontends/alert.h"
55 #include <QThreadStorage>
58 using namespace lyx::support;
60 // Uncomment to enable InsetIndex-specific debugging mode: the tree for the index will be printed to std::cout.
61 // #define LYX_INSET_INDEX_DEBUG
67 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
68 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
70 PageRangeTranslator const init_insetindexpagerangetranslator()
72 PageRangeTranslator translator("none", InsetIndexParams::None);
73 translator.addPair("start", InsetIndexParams::Start);
74 translator.addPair("end", InsetIndexParams::End);
78 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
80 PageRangeTranslator translator("", InsetIndexParams::None);
81 translator.addPair("(", InsetIndexParams::Start);
82 translator.addPair(")", InsetIndexParams::End);
87 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
89 PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
90 translator.addPair(_("Starts page range"), InsetIndexParams::Start);
91 translator.addPair(_("Ends page range"), InsetIndexParams::End);
96 PageRangeTranslator const & insetindexpagerangetranslator()
98 static PageRangeTranslator const prtranslator =
99 init_insetindexpagerangetranslator();
104 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
106 static PageRangeTranslatorLoc const translator =
107 init_insetindexpagerangetranslator_loc();
112 PageRangeTranslator const & insetindexpagerangetranslator_latex()
114 static PageRangeTranslator const lttranslator =
115 init_insetindexpagerangetranslator_latex();
121 /////////////////////////////////////////////////////////////////////
125 ///////////////////////////////////////////////////////////////////////
128 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
129 : InsetCollapsible(buf), params_(params)
133 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
135 OutputParams runparams(runparams_in);
136 runparams.inIndexEntry = true;
137 if (runparams_in.postpone_fragile_stuff)
138 // This is not needed and would impact sorting
139 runparams.moving_arg = false;
143 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
144 && params_.index != "idx") {
146 os << escape(params_.index);
153 // Get the LaTeX output from InsetText. We need to deconstruct this later
154 // in order to check if we need to generate a sorting key
155 odocstringstream ourlatex;
156 otexstream ots(ourlatex);
157 InsetText::latex(ots, runparams);
158 if (runparams.find_effective()) {
159 // No need for special handling, if we are only searching for some patterns
160 os << ourlatex.str() << "}";
165 getSortkey(os, runparams);
167 os << ourlatex.str();
168 getSubentries(os, runparams, ourlatex.str());
171 os << insetindexpagerangetranslator_latex().find(params_.range);
172 getSeeRefs(os, runparams);
173 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
175 os << insetindexpagerangetranslator_latex().find(params_.range);
176 os << from_utf8(params_.pagefmt);
177 } else if (params_.range != InsetIndexParams::PageRange::None) {
179 os << insetindexpagerangetranslator_latex().find(params_.range);
182 // We check whether we need a sort key.
183 // If so, we use the plaintext version
184 odocstringstream ourplain;
185 InsetText::plaintext(ourplain, runparams);
187 // These are the LaTeX and plaintext representations
188 docstring latexstr = ourlatex.str();
189 docstring plainstr = ourplain.str();
191 // This will get what follows | if anything does,
192 // the command (e.g., see, textbf) for pagination
197 odocstringstream seeref;
198 otexstream otsee(seeref);
199 getSeeRefs(otsee, runparams);
201 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
202 cmd = from_utf8(params_.pagefmt);
204 // Check for the | separator to strip the cmd.
205 // This goes wrong on an escaped "|", but as the escape
206 // character can be changed in style files, we cannot
208 size_t pos = latexstr.find(from_ascii("|"));
209 if (pos != docstring::npos) {
210 // Put the bit after "|" into cmd...
211 cmd = latexstr.substr(pos + 1);
212 // ...and erase that stuff from latexstr
213 latexstr = latexstr.erase(pos);
214 // ...as well as from plainstr
215 size_t ppos = plainstr.find(from_ascii("|"));
216 if (ppos < plainstr.size())
217 plainstr.erase(ppos);
219 LYXERR0("The `|' separator was not found in the plaintext version!");
223 odocstringstream subentries;
224 otexstream otsub(subentries);
225 getSubentries(otsub, runparams, ourlatex.str());
226 if (subentries.str().empty()) {
227 // Separate the entries and subentries, i.e., split on "!".
228 // This goes wrong on an escaped "!", but as the escape
229 // character can be changed in style files, we cannot
231 std::vector<docstring> const levels =
232 getVectorFromString(latexstr, from_ascii("!"), true);
233 std::vector<docstring> const levels_plain =
234 getVectorFromString(plainstr, from_ascii("!"), true);
236 vector<docstring>::const_iterator it = levels.begin();
237 vector<docstring>::const_iterator end = levels.end();
238 vector<docstring>::const_iterator it2 = levels_plain.begin();
240 for (; it != end; ++it) {
242 emptySubentriesWarning(ourlatex.str());
243 if (it2 < levels_plain.end())
247 // The separator needs to be put back when
248 // writing the levels, except for the first level
254 // Now here comes the reason for this whole procedure:
255 // We try to correctly sort macros and formatted strings.
256 // If we find a command, prepend a plain text
257 // version of the content to get sorting right,
258 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
259 // We do this on all levels.
260 // We don't do it if the level already contains a '@', though.
261 // Plaintext might return nothing (e.g. for ERTs).
262 // In that case, we use LaTeX.
263 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
264 processLatexSorting(os, runparams, *it, spart);
265 if (it2 < levels_plain.end())
269 processLatexSorting(os, runparams, latexstr, plainstr);
270 os << subentries.str();
273 // At last, re-insert the command, separated by "|"
276 << insetindexpagerangetranslator_latex().find(params_.range)
278 } else if (params_.range != InsetIndexParams::PageRange::None) {
280 os << insetindexpagerangetranslator_latex().find(params_.range);
285 // In macros with moving arguments, such as \section,
286 // we store the index and output it after the macro (#2154)
287 if (runparams_in.postpone_fragile_stuff)
288 runparams_in.post_macro += os.str();
294 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
295 docstring const & latex, docstring const & spart) const
297 if (contains(latex, '\\') && !contains(latex, '@')) {
298 // Now we need to validate that all characters in
299 // the sorting part are representable in the current
300 // encoding. If not try the LaTeX macro which might
301 // or might not be a good choice, and issue a warning.
302 pair<docstring, docstring> spart_latexed =
303 runparams.encoding->latexString(spart, runparams.dryrun);
304 if (!spart_latexed.second.empty())
305 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
306 if (spart != spart_latexed.first && !runparams.dryrun) {
308 ErrorList & errorList = buffer().errorList("Export");
309 docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
310 "problems with the entry '%1$s'.\n"
311 "Please specify the sorting of this entry manually, as "
312 "explained in the User Guide."), spart);
313 Paragraph const & par = buffer().paragraphs().front();
314 errorList.push_back(ErrorItem(_("Index sorting failed"), s,
315 {par.id(), 0}, {par.id(), -1}));
316 buffer().bufferErrors(terr, errorList);
318 // Remove remaining \'s from the sort key
319 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
320 // Plain quotes need to be escaped, however (#10649), as this
321 // is the default escape character
322 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
324 // Now insert the sortkey, separated by '@'.
328 // Insert the actual level text
333 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
335 // Two ways of processing this inset are implemented:
336 // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
337 // - the modern one, based on precise insets for indexing features
338 // Like the LaTeX implementation, consider the user chooses either of those options.
340 // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
341 // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
342 // otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
343 odocstringstream odss;
344 otexstream ots(odss);
345 InsetText::latex(ots, runparams);
346 docstring latexString = trim(odss.str());
348 // Handle several indices (indicated in the inset instead of the raw latexString).
349 docstring indexType = from_utf8("");
350 if (buffer().masterBuffer()->params().use_indices) {
351 indexType += " type=\"" + params_.index + "\"";
354 // Split the string into its main constituents: terms, and command (see, see also, range).
355 size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
356 docstring indexTerms = latexString.substr(0, positionVerticalBar);
358 if (positionVerticalBar != lyx::docstring::npos) {
359 command = latexString.substr(positionVerticalBar + 1);
362 // Handle sorting issues, with @.
365 sortAs = getSortkeyAsText(runparams);
366 // indexTerms may contain a sort key if the user has both the inset and the manual key.
368 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
369 if (sortingElements.size() == 2) {
370 sortAs = sortingElements[0];
371 indexTerms = sortingElements[1];
375 // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
376 vector<docstring> terms;
377 const vector<docstring> potential_terms = getSubentriesAsText(runparams);
378 if (!potential_terms.empty()) {
379 terms = potential_terms;
380 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
381 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
382 // full string within this inset (i.e. without the subinsets).
383 terms.insert(terms.begin(), latexString);
385 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
388 // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
389 const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
390 const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
391 latexString.find(from_ascii("|(")) != lyx::docstring::npos;
392 const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
393 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
396 // Remove the ranges from the command if they do not appear at the beginning.
398 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
399 command.erase(index, 1);
401 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
402 command.erase(index, 1);
404 // Remove the ranges when they are the only vertical bar in the complete string.
405 if (command[0] == '(' || command[0] == ')')
409 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
410 // Both commands are mutually exclusive!
411 docstring see = getSeeAsText(runparams);
412 vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
414 if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
415 // Unescape brackets.
417 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
418 command.erase(index, 1);
420 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
421 command.erase(index, 1);
423 // Retrieve the part between brackets, and remove the complete seealso.
424 size_t positionOpeningBracket = command.find(from_ascii("{"));
425 size_t positionClosingBracket = command.find(from_ascii("}"));
426 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
428 // Parse the list of referenced entries (or a single one for see).
429 if (command.substr(0, 7) == "seealso") {
430 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
434 if (see.find(from_ascii(",")) != std::string::npos) {
435 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
436 "Complete entry: \"") + latexString + from_utf8("\"");
438 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
442 // Remove the complete see/seealso from the commands, in case there is something else to parse.
443 command = command.substr(positionClosingBracket + 1);
446 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
447 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
448 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
449 // no range will be detected.
450 // TODO: Could handle formatting as significance="preferred"?
451 if (!command.empty()) {
452 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
453 + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
455 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
458 // Write all of this down.
459 if (terms.empty() && !hasEndRange) {
460 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
462 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
464 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
465 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
466 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
467 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
468 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
470 // indexType can only be used for singular and startofrange types!
472 if (!hasStartRange && !hasEndRange) {
475 // Append an ID if uniqueness is not guaranteed across the document.
476 static QThreadStorage<set<docstring>> tKnownTermLists;
477 static QThreadStorage<int> tID;
479 set<docstring> &knownTermLists = tKnownTermLists.localData();
480 int &ID = tID.localData();
482 if (!tID.hasLocalData()) {
486 // Modify the index terms to add the unique ID if needed.
487 docstring newIndexTerms = indexTerms;
488 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
489 newIndexTerms += from_ascii(string("-") + to_string(ID));
491 // Only increment for the end of range, so that the same number is used for the start of range.
497 // Term list not yet known: add it to the set AFTER the end of range. After
498 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
499 knownTermLists.insert(indexTerms);
502 // Generate the attributes.
503 docstring id = xml::cleanID(newIndexTerms);
505 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
507 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
511 // Handle the index terms (including the specific index for this entry).
513 xs << xml::CompTag("indexterm", attrs);
515 xs << xml::StartTag("indexterm", attrs);
516 if (!terms.empty()) { // hasEndRange has no content.
518 if (!sortAs.empty()) {
519 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
522 xs << xml::StartTag("primary", attr);
524 xs << xml::EndTag("primary");
526 if (terms.size() > 1) {
527 xs << xml::StartTag("secondary");
529 xs << xml::EndTag("secondary");
531 if (terms.size() > 2) {
532 xs << xml::StartTag("tertiary");
534 xs << xml::EndTag("tertiary");
537 // Handle see and see also.
539 xs << xml::StartTag("see");
541 xs << xml::EndTag("see");
544 if (!seeAlsoes.empty()) {
545 for (auto &entry : seeAlsoes) {
546 xs << xml::StartTag("seealso");
548 xs << xml::EndTag("seealso");
553 xs << xml::EndTag("indexterm");
559 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
561 // we just print an anchor, taking the paragraph ID from
562 // our own interior paragraph, which doesn't get printed
563 std::string const magic = paragraphs().front().magicLabel();
564 std::string const attr = "id='" + magic + "'";
565 xs << xml::CompTag("a", attr);
570 bool InsetIndex::showInsetDialog(BufferView * bv) const
572 bv->showDialog("index", params2string(params_),
573 const_cast<InsetIndex *>(this));
578 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
580 switch (cmd.action()) {
582 case LFUN_INSET_MODIFY: {
583 if (cmd.getArg(0) == "changetype") {
584 cur.recordUndoInset(this);
585 params_.index = from_utf8(cmd.getArg(1));
588 if (cmd.getArg(0) == "changeparam") {
589 string const p = cmd.getArg(1);
590 string const v = cmd.getArg(2);
591 cur.recordUndoInset(this);
593 params_.range = insetindexpagerangetranslator().find(v);
594 if (p == "pagefmt") {
595 if (v == "default" || v == "textbf"
596 || v == "textit" || v == "emph")
599 lyx::dispatch(FuncRequest(LFUN_INSET_SETTINGS, "index"));
603 InsetIndexParams params;
604 InsetIndex::string2params(to_utf8(cmd.argument()), params);
605 cur.recordUndoInset(this);
606 params_.index = params.index;
607 params_.range = params.range;
608 params_.pagefmt = params.pagefmt;
609 // what we really want here is a TOC update, but that means
610 // a full buffer update
611 cur.forceBufferUpdate();
615 case LFUN_INSET_DIALOG_UPDATE:
616 cur.bv().updateDialog("index", params2string(params_));
619 case LFUN_PARAGRAPH_BREAK: {
620 // Since this inset in single-par anyway, let's use
621 // return to enter subentries
622 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
627 case LFUN_INSET_INSERT_COPY: {
628 Cursor & bvcur = cur.bv().cursor();
629 if (cmd.origin() == FuncRequest::TOC && bvcur.inTexted()) {
630 cap::copyInsetToTemp(cur, clone());
631 cap::pasteFromTemp(bvcur, bvcur.buffer()->errorList("Paste"));
638 InsetCollapsible::doDispatch(cur, cmd);
644 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
645 FuncStatus & flag) const
647 switch (cmd.action()) {
649 case LFUN_INSET_MODIFY:
650 if (cmd.getArg(0) == "changetype") {
651 docstring const newtype = from_utf8(cmd.getArg(1));
652 Buffer const & realbuffer = *buffer().masterBuffer();
653 IndicesList const & indiceslist = realbuffer.params().indiceslist();
654 Index const * index = indiceslist.findShortcut(newtype);
655 flag.setEnabled(index != 0);
657 from_utf8(cmd.getArg(1)) == params_.index);
660 if (cmd.getArg(0) == "changeparam") {
661 string const p = cmd.getArg(1);
662 string const v = cmd.getArg(2);
664 flag.setEnabled(v == "none" || v == "start" || v == "end");
665 flag.setOnOff(params_.range == insetindexpagerangetranslator().find(v));
667 if (p == "pagefmt") {
668 flag.setEnabled(!v.empty());
669 if (params_.pagefmt == "default" || params_.pagefmt == "textbf"
670 || params_.pagefmt == "textit" || params_.pagefmt == "emph")
671 flag.setOnOff(params_.pagefmt == v);
673 flag.setOnOff(v == "custom");
677 return InsetCollapsible::getStatus(cur, cmd, flag);
679 case LFUN_INSET_DIALOG_UPDATE: {
680 Buffer const & realbuffer = *buffer().masterBuffer();
681 flag.setEnabled(realbuffer.params().use_indices);
685 case LFUN_INSET_INSERT_COPY:
686 // This can only be invoked by ToC widget
687 flag.setEnabled(cmd.origin() == FuncRequest::TOC
688 && cur.bv().cursor().inset().insetAllowed(lyxCode()));
691 case LFUN_PARAGRAPH_BREAK:
692 return macrosPossible("subentry");
694 case LFUN_INDEXMACRO_INSERT:
695 return macrosPossible(cmd.getArg(0));
697 case LFUN_INDEX_TAG_ALL: {
701 // move backwards into preceding word
702 // skip over other index insets
703 DocIterator dit(cur);
704 dit.backwardPosIgnoreCollapsed();
706 if (dit.inset().lyxCode() == INDEX_CODE)
708 else if (dit.prevInset() && dit.prevInset()->lyxCode() == INDEX_CODE)
709 dit.backwardPosIgnoreCollapsed();
714 // action not possible
716 // Check if we actually have a word to tag
717 FontSpan tw = dit.locateWord(WHOLE_WORD);
719 // action possible if we have a word of at least one char
720 return (tw.size() > 0);
724 return InsetCollapsible::getStatus(cur, cmd, flag);
729 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
731 Paragraph const & par = paragraphs().front();
732 InsetList::const_iterator it = par.insetList().begin();
733 for (; it != par.insetList().end(); ++it) {
734 Inset & inset = *it->inset;
735 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
736 InsetIndexMacro const & iim =
737 static_cast<InsetIndexMacro const &>(inset);
738 iim.getLatex(os, runparams);
745 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
747 Paragraph const & par = paragraphs().front();
748 InsetList::const_iterator it = par.insetList().begin();
749 for (; it != par.insetList().end(); ++it) {
750 Inset & inset = *it->inset;
751 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
753 InsetIndexMacro const & iim =
754 static_cast<InsetIndexMacro const &>(inset);
755 iim.getLatex(os, runparams);
759 return from_ascii("");
763 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
765 // Empty subentries crash makeindex. So warn and ignore this.
767 ErrorList & errorList = buffer().errorList("Export");
768 docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
769 "It will be ignored in the output."), mainentry);
770 Paragraph const & par = buffer().paragraphs().front();
771 errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
772 {par.id(), 0}, {par.id(), -1}));
773 buffer().bufferErrors(terr, errorList);
777 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
778 docstring const & mainentry) const
780 Paragraph const & par = paragraphs().front();
781 InsetList::const_iterator it = par.insetList().begin();
783 for (; it != par.insetList().end(); ++it) {
784 Inset & inset = *it->inset;
785 if (inset.lyxCode() == INDEXMACRO_CODE) {
786 InsetIndexMacro const & iim =
787 static_cast<InsetIndexMacro const &>(inset);
788 if (iim.params().type == InsetIndexMacroParams::Subentry) {
789 if (iim.hasNoContent()) {
790 emptySubentriesWarning(mainentry);
797 iim.getLatex(os, runparams);
804 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
805 bool const asLabel) const
807 std::vector<docstring> subentries;
809 Paragraph const & par = paragraphs().front();
810 InsetList::const_iterator it = par.insetList().begin();
812 for (; it != par.insetList().end(); ++it) {
813 Inset & inset = *it->inset;
814 if (inset.lyxCode() == INDEXMACRO_CODE) {
815 InsetIndexMacro const & iim =
816 static_cast<InsetIndexMacro const &>(inset);
817 if (iim.params().type == InsetIndexMacroParams::Subentry) {
823 docstring const sl = iim.getNewLabel(l);
824 subentries.emplace_back(sl);
827 iim.getLatex(os, runparams);
828 subentries.emplace_back(os.str());
838 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
841 InsetText::latex(os, runparams);
846 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
848 Paragraph const & par = paragraphs().front();
849 InsetList::const_iterator it = par.insetList().begin();
850 for (; it != par.insetList().end(); ++it) {
851 Inset & inset = *it->inset;
852 if (inset.lyxCode() == INDEXMACRO_CODE) {
853 InsetIndexMacro const & iim =
854 static_cast<InsetIndexMacro const &>(inset);
855 if (iim.params().type == InsetIndexMacroParams::See
856 || iim.params().type == InsetIndexMacroParams::Seealso) {
857 iim.getLatex(os, runparams);
865 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
866 bool const asLabel) const
868 Paragraph const & par = paragraphs().front();
869 InsetList::const_iterator it = par.insetList().begin();
870 for (; it != par.insetList().end(); ++it) {
871 Inset & inset = *it->inset;
872 if (inset.lyxCode() == INDEXMACRO_CODE) {
873 InsetIndexMacro const & iim =
874 static_cast<InsetIndexMacro const &>(inset);
875 if (iim.params().type == InsetIndexMacroParams::See) {
878 return iim.getNewLabel(l);
881 iim.getLatex(os, runparams);
887 return from_ascii("");
891 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
892 bool const asLabel) const
894 std::vector<docstring> seeAlsoes;
896 Paragraph const & par = paragraphs().front();
897 InsetList::const_iterator it = par.insetList().begin();
898 for (; it != par.insetList().end(); ++it) {
899 Inset & inset = *it->inset;
900 if (inset.lyxCode() == INDEXMACRO_CODE) {
901 InsetIndexMacro const & iim =
902 static_cast<InsetIndexMacro const &>(inset);
903 if (iim.params().type == InsetIndexMacroParams::Seealso) {
906 seeAlsoes.emplace_back(iim.getNewLabel(l));
909 iim.getLatex(os, runparams);
910 seeAlsoes.emplace_back(os.str());
922 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
923 const std::set<InsetIndexMacroParams::Type> types = {})
925 Paragraph const & par = inset_index->paragraphs().front();
926 InsetList::const_iterator it = par.insetList().begin();
927 for (; it != par.insetList().end(); ++it) {
928 Inset & inset = *it->inset;
929 if (inset.lyxCode() == code) {
933 LASSERT(code == INDEXMACRO_CODE, return false);
934 InsetIndexMacro const & iim =
935 static_cast<InsetIndexMacro const &>(inset);
936 if (types.find(iim.params().type) != types.end())
946 bool InsetIndex::hasSubentries() const
948 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
952 bool InsetIndex::hasSeeRef() const
954 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
958 bool InsetIndex::hasSortKey() const
960 return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
964 bool InsetIndex::macrosPossible(string const type) const
966 if (type != "see" && type != "seealso"
967 && type != "sortkey" && type != "subentry")
970 Paragraph const & par = paragraphs().front();
971 InsetList::const_iterator it = par.insetList().begin();
973 for (; it != par.insetList().end(); ++it) {
974 Inset & inset = *it->inset;
975 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
977 if (inset.lyxCode() == INDEXMACRO_CODE) {
978 InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
979 if ((type == "see" || type == "seealso")
980 && (iim.params().type == InsetIndexMacroParams::See
981 || iim.params().type == InsetIndexMacroParams::Seealso))
983 if (type == "subentry"
984 && iim.params().type == InsetIndexMacroParams::Subentry) {
995 ColorCode InsetIndex::labelColor() const
997 if (params_.index.empty() || params_.index == from_ascii("idx"))
998 return InsetCollapsible::labelColor();
1000 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
1001 + "@" + buffer().fileName().absFileName());
1002 if (c == Color_none)
1003 c = InsetCollapsible::labelColor();
1008 docstring InsetIndex::toolTip(BufferView const &, int, int) const
1010 docstring tip = _("Index Entry");
1011 if (buffer().params().use_indices && !params_.index.empty()) {
1012 Buffer const & realbuffer = *buffer().masterBuffer();
1013 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1015 Index const * index = indiceslist.findShortcut(params_.index);
1017 tip += _("unknown type!");
1019 tip += index->index();
1023 docstring res = toolTipText(tip);
1024 if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
1025 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
1026 if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
1027 res += "\n" + _("Pagination format:") + " ";
1028 if (params_.pagefmt == "textbf")
1030 else if (params_.pagefmt == "textit")
1032 else if (params_.pagefmt == "emph")
1033 res += _("emphasized");
1035 res += from_utf8(params_.pagefmt);
1041 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
1043 InsetLayout const & il = getLayout();
1044 docstring label = translateIfPossible(il.labelstring());
1046 if (buffer().params().use_indices && !params_.index.empty()) {
1047 Buffer const & realbuffer = *buffer().masterBuffer();
1048 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1050 Index const * index = indiceslist.findShortcut(params_.index);
1052 label += _("unknown type!");
1054 label += index->index();
1059 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
1062 res = getNewLabel(label);
1063 OutputParams const rp(0);
1064 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1065 for (auto const & sublbl : sublbls) {
1066 res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1067 res += " " + sublbl;
1069 docstring see = getSeeAsText(rp, true);
1070 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1071 see = getSeeAlsoesAsText(rp, true).front();
1073 res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1077 if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
1078 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
1083 void InsetIndex::write(ostream & os) const
1085 os << to_utf8(layoutName());
1087 InsetCollapsible::write(os);
1091 void InsetIndex::read(Lexer & lex)
1094 InsetCollapsible::read(lex);
1098 string InsetIndex::params2string(InsetIndexParams const & params)
1107 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1109 params = InsetIndexParams();
1113 istringstream data(in);
1115 lex.setStream(data);
1116 lex.setContext("InsetIndex::string2params");
1122 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1123 UpdateType utype, TocBackend & backend) const
1125 DocIterator pit = cpit;
1126 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1128 InsetLayout const & il = getLayout();
1129 docstring label = translateIfPossible(il.labelstring());
1130 if (!il.contentaslabel())
1133 str = getNewLabel(label);
1134 OutputParams const rp(0);
1135 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1136 for (auto const & sublbl : sublbls) {
1137 str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1138 str += " " + sublbl;
1140 docstring see = getSeeAsText(rp, true);
1141 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1142 see = getSeeAlsoesAsText(rp, true).front();
1144 str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1148 string type = "index";
1149 if (buffer().masterBuffer()->params().use_indices)
1150 type += ":" + to_utf8(params_.index);
1151 TocBuilder & b = backend.builder(type);
1152 b.pushItem(pit, str, output_active);
1153 // Proceed with the rest of the inset.
1154 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1159 void InsetIndex::validate(LaTeXFeatures & features) const
1161 if (buffer().masterBuffer()->params().use_indices
1162 && !params_.index.empty()
1163 && params_.index != "idx")
1164 features.require("splitidx");
1165 InsetCollapsible::validate(features);
1169 string InsetIndex::contextMenuName() const
1171 return "context-index";
1175 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1177 // We override the implementation of InsetCollapsible,
1178 // because we have extra entries.
1179 string owncm = "context-edit-index;";
1180 return owncm + InsetCollapsible::contextMenu(bv, x, y);
1184 bool InsetIndex::hasSettings() const
1190 bool InsetIndex::insetAllowed(InsetCode code) const
1193 case INDEXMACRO_CODE:
1194 case INDEXMACRO_SORTKEY_CODE:
1199 return InsetCollapsible::insetAllowed(code);
1204 /////////////////////////////////////////////////////////////////////
1208 ///////////////////////////////////////////////////////////////////////
1211 void InsetIndexParams::write(ostream & os) const
1215 os << to_utf8(index);
1220 << insetindexpagerangetranslator().find(range)
1228 void InsetIndexParams::read(Lexer & lex)
1231 index = lex.getDocString();
1233 index = from_ascii("idx");
1234 if (lex.checkFor("range")) {
1235 string st = lex.getString();
1236 if (lex.eatLine()) {
1237 st = lex.getString();
1238 range = insetindexpagerangetranslator().find(lex.getString());
1241 if (lex.checkFor("pageformat") && lex.eatLine()) {
1242 pagefmt = lex.getString();
1247 /////////////////////////////////////////////////////////////////////
1251 ///////////////////////////////////////////////////////////////////////
1253 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1254 : InsetCommand(buf, p)
1258 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1260 static ParamInfo param_info_;
1261 if (param_info_.empty()) {
1262 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1263 ParamInfo::HANDLING_ESCAPE);
1264 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1265 ParamInfo::HANDLING_LATEXIFY);
1266 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1272 docstring InsetPrintIndex::screenLabel() const
1274 bool const printall = suffixIs(getCmdName(), '*');
1275 bool const multind = buffer().masterBuffer()->params().use_indices;
1277 && getParam("type") == from_ascii("idx"))
1278 || (getParam("type").empty() && !printall))
1280 Buffer const & realbuffer = *buffer().masterBuffer();
1281 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1282 Index const * index = indiceslist.findShortcut(getParam("type"));
1283 if (!index && !printall)
1284 return _("Unknown index type!");
1285 docstring res = printall ? _("All indexes") : index->index();
1287 res += " (" + _("non-active") + ")";
1288 else if (contains(getCmdName(), "printsubindex"))
1289 res += " (" + _("subindex") + ")";
1294 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1296 return s == "printindex" || s == "printsubindex"
1297 || s == "printindex*" || s == "printsubindex*";
1301 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1303 switch (cmd.action()) {
1305 case LFUN_INSET_MODIFY: {
1306 if (cmd.argument() == from_ascii("toggle-subindex")) {
1307 string scmd = getCmdName();
1308 if (contains(scmd, "printindex"))
1309 scmd = subst(scmd, "printindex", "printsubindex");
1311 scmd = subst(scmd, "printsubindex", "printindex");
1315 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1316 string scmd = getCmdName();
1317 if (suffixIs(scmd, '*'))
1321 setParam("type", docstring());
1325 InsetCommandParams p(INDEX_PRINT_CODE);
1327 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1328 if (p.getCmdName().empty()) {
1329 cur.noScreenUpdate();
1338 InsetCommand::doDispatch(cur, cmd);
1344 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1345 FuncStatus & status) const
1347 switch (cmd.action()) {
1349 case LFUN_INSET_MODIFY: {
1350 if (cmd.argument() == from_ascii("toggle-subindex")) {
1351 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1352 status.setOnOff(contains(getCmdName(), "printsubindex"));
1354 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1355 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1356 status.setOnOff(suffixIs(getCmdName(), '*'));
1358 } if (cmd.getArg(0) == "index_print"
1359 && cmd.getArg(1) == "CommandInset") {
1360 InsetCommandParams p(INDEX_PRINT_CODE);
1361 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1362 if (suffixIs(p.getCmdName(), '*')) {
1363 status.setEnabled(true);
1364 status.setOnOff(false);
1367 Buffer const & realbuffer = *buffer().masterBuffer();
1368 IndicesList const & indiceslist =
1369 realbuffer.params().indiceslist();
1370 Index const * index = indiceslist.findShortcut(p["type"]);
1371 status.setEnabled(index != 0);
1372 status.setOnOff(p["type"] == getParam("type"));
1375 return InsetCommand::getStatus(cur, cmd, status);
1378 case LFUN_INSET_DIALOG_UPDATE: {
1379 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1384 return InsetCommand::getStatus(cur, cmd, status);
1389 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1391 Index const * index =
1392 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1394 setParam("name", index->index());
1398 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1400 if (!buffer().masterBuffer()->params().use_indices) {
1401 if (getParam("type") == from_ascii("idx"))
1402 os << "\\printindex" << termcmd;
1405 OutputParams runparams = runparams_in;
1406 os << getCommand(runparams);
1410 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1412 features.require("makeidx");
1413 if (buffer().masterBuffer()->params().use_indices)
1414 features.require("splitidx");
1415 InsetCommand::validate(features);
1419 string InsetPrintIndex::contextMenuName() const
1421 return buffer().masterBuffer()->params().use_indices ?
1422 "context-indexprint" : string();
1426 bool InsetPrintIndex::hasSettings() const
1428 return buffer().masterBuffer()->params().use_indices;
1435 /// Builds an entry for the index.
1436 IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1438 LASSERT(runparams, return);
1440 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1441 odocstringstream entry;
1442 OutputParams ours = *runparams;
1443 ours.for_toc = false;
1444 inset_->plaintext(entry, ours);
1445 entry_ = entry.str();
1447 // Determine in which index this entry belongs to.
1448 if (inset_->buffer().masterBuffer()->params().use_indices) {
1449 index_ = inset_->params_.index;
1452 // Attempt parsing the inset.
1463 #ifdef LYX_INSET_INDEX_DEBUG
1464 std::cout << to_utf8(entry_) << std::endl;
1465 #endif // LYX_INSET_INDEX_DEBUG
1467 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1468 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1469 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1470 // is not allowed (i.e. behaviour is undefined).
1471 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1472 || inset_->params_.range != InsetIndexParams::PageRange::None;
1473 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1474 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1476 if (is_definitely_legacy && is_definitely_modern)
1477 output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1480 // - is_definitely_modern == true:
1481 // - is_definitely_legacy == true: error (return whatever)
1482 // - is_definitely_legacy == false: return modern
1483 // - is_definitely_modern == false:
1484 // - is_definitely_legacy == true: return legacy
1485 // - is_definitely_legacy == false: return modern
1486 return !is_definitely_legacy;
1489 void parseAsModern()
1491 LASSERT(runparams_, return);
1493 if (inset_->hasSortKey()) {
1494 sort_as_ = inset_->getSortkeyAsText(*runparams_);
1497 terms_ = inset_->getSubentriesAsText(*runparams_);
1498 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1499 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1500 // full string within this inset (i.e. without the subinsets).
1501 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1503 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1504 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1506 see_ = inset_->getSeeAsText(*runparams_);
1507 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1510 void parseAsLegacy() {
1511 // Determine if some features are known not to be supported. For now, this is only formatting like
1512 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1513 // @ is supported, but only for sorting, without specific formatting.
1514 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1515 output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1516 "Complete entry: \"") + entry_ + from_utf8("\". ");
1518 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1519 output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1520 "Complete entry: \"") + entry_ + from_utf8("\". ");
1523 // Split the string into its main constituents: terms, and command (see, see also, range).
1524 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1525 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1527 if (positionVerticalBar != lyx::docstring::npos) {
1528 command = entry_.substr(positionVerticalBar + 1);
1531 // Handle sorting issues, with @.
1532 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1533 if (sortingElements.size() == 2) {
1534 sort_as_ = sortingElements[0];
1535 indexTerms = sortingElements[1];
1538 // Handle entries, subentries, and subsubentries.
1539 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1541 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1542 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1543 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1545 // - Remove the ranges from the command if they do not appear at the beginning.
1546 size_t range_index = 0;
1547 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1548 command.erase(range_index, 1);
1550 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1551 command.erase(range_index, 1);
1553 // - Remove the ranges when they are the only vertical bar in the complete string.
1554 if (command[0] == '(' || command[0] == ')')
1555 command.erase(0, 1);
1557 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1558 // Both commands are mutually exclusive!
1559 if (command.substr(0, 3) == "see") {
1560 // Unescape brackets.
1561 size_t index_argument_begin = 0;
1562 while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1563 command.erase(index_argument_begin, 1);
1564 size_t index_argument_end = 0;
1565 while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1566 command.erase(index_argument_end, 1);
1568 // Retrieve the part between brackets, and remove the complete seealso.
1569 size_t position_opening_bracket = command.find(from_ascii("{"));
1570 size_t position_closing_bracket = command.find(from_ascii("}"));
1571 docstring argument = command.substr(position_opening_bracket + 1,
1572 position_closing_bracket - position_opening_bracket - 1);
1574 // Parse the argument of referenced entries (or a single one for see).
1575 if (command.substr(0, 7) == "seealso") {
1576 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1580 if (see_.find(from_ascii(",")) != std::string::npos) {
1581 output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1582 "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1586 // Remove the complete see/seealso from the commands, in case there is something else to parse.
1587 command = command.substr(position_closing_bracket + 1);
1590 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1591 // things like formatting the entry or the page number, other strings for sorting.
1592 // https://wiki.lyx.org/Tips/Indexing
1593 // If there are such things in the index entry, then this code may miserably fail. For example, for
1594 // "Peter|(textbf", no range will be detected.
1595 if (!command.empty()) {
1596 output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1597 + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1603 return terms_.size();
1606 const std::vector<docstring>& terms() const {
1610 std::vector<docstring>& terms() {
1614 const InsetIndex* inset() const {
1619 // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1620 // the constructor).
1621 const InsetIndex * inset_;
1622 OutputParams const * runparams_;
1624 docstring index_; // Useful when there are multiple indices in the same document.
1626 // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1627 // constructor is done).
1628 docstring output_error_;
1630 // Parsed index entry.
1631 std::vector<docstring> terms_; // Up to three entries, in general.
1634 bool has_start_range_;
1635 bool has_end_range_;
1637 vector<docstring> see_alsoes_;
1639 // Operators used for sorting entries (alphabetical order).
1640 friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1643 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1645 if (lhs.terms_.empty())
1648 for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1649 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1658 std::string generateCssClassAtDepth(unsigned depth) {
1659 std::string css_class = "entry";
1663 css_class.insert(0, "sub");
1670 std::vector<IndexEntry> entries;
1671 std::vector<IndexNode*> children;
1674 for (IndexNode * child : children) {
1682 docstring termAtLevel(const IndexNode* node, unsigned depth)
1684 // 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
1685 // function could be made constant time by copying the term in each node, but that would make data duplication that
1686 // may fall out of sync; the performance benefit would probably be negligible.
1687 if (!node->entries.empty()) {
1688 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1689 return node->entries.begin()->terms()[depth];
1692 if (!node->children.empty()) {
1693 return termAtLevel(*node->children.begin(), depth);
1696 LASSERT(false, return from_ascii(""));
1699 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1701 // Do not insert empty entries.
1702 if (entry.terms().empty())
1705 // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1706 for (IndexNode* child : node->children) {
1707 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1708 if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1709 // All term entries match: it's an entry.
1710 if (!entry.terms()[depth].empty())
1711 child->entries.emplace_back(entry);
1714 insertIntoNode(entry, child, depth + 1);
1720 // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1721 // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1722 // first entry for a word has several levels that never appeared.
1723 // In particular, this case is called for the first entry.
1724 IndexNode* new_node = node;
1726 new_node->children.emplace_back(new IndexNode{{}, {}});
1727 new_node = new_node->children.back();
1729 } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1730 new_node->entries.emplace_back(entry);
1733 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1735 // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1736 // thanks to the implementation of operator<.
1737 // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1738 // unsorted entries would make its complexity blow up).
1739 stable_sort(entries.begin(), entries.end());
1741 // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1743 auto* index_root = new IndexNode{{}, {}};
1744 for (const IndexEntry& entry : entries) {
1745 insertIntoNode(entry, index_root);
1751 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1753 LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1755 xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1757 xs << termAtLevel(root_node, depth);
1758 // By tree assumption, all the entries at this node have the same set of terms.
1760 if (!root_node->entries.empty()) {
1761 xs << XMLStream::ESCAPE_NONE << " — "; // Em dash, i.e. long (---).
1762 unsigned entry_number = 1;
1764 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1765 std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1766 xs << xml::StartTag("a", link_attr);
1767 xs << from_ascii(std::to_string(entry_number));
1768 xs << xml::EndTag("a");
1771 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1772 const IndexEntry &entry = root_node->entries[i];
1774 switch (entry.inset()->params().range) {
1775 case InsetIndexParams::PageRange::None:
1776 writeLinkToEntry(entry, entry_number);
1778 case InsetIndexParams::PageRange::Start: {
1779 // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1780 // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1781 // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1783 const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1784 root_node->entries[i + 1].inset()->params().range ==
1785 InsetIndexParams::PageRange::End;
1786 // No need to check if both entries are for the same terms: they are in the same IndexNode.
1788 writeLinkToEntry(entry, entry_number);
1789 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1791 if (nextEntryIsEnd) {
1792 // Skip the next entry in the loop, write it right now, after the dash.
1795 writeLinkToEntry(root_node->entries[i], entry_number);
1799 case InsetIndexParams::PageRange::End:
1800 // This range end was not caught by the range start, do it now to avoid losing content.
1801 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1802 writeLinkToEntry(root_node->entries[i], entry_number);
1805 if (i < root_node->entries.size() - 1) {
1813 if (!root_node->entries.empty() && !root_node->children.empty()) {
1817 if (!root_node->children.empty()) {
1818 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1821 for (const IndexNode* child : root_node->children) {
1822 outputIndexPage(xs, child, depth + 1);
1825 xs << xml::EndTag("ul");
1829 xs << xml::EndTag("li");
1833 #ifdef LYX_INSET_INDEX_DEBUG
1834 void printTree(const IndexNode* root_node, unsigned depth = 0)
1836 static const std::string pattern = " ";
1838 for (unsigned i = 0; i < depth; ++i) {
1841 const std::string prefix_long = prefix + pattern + pattern;
1843 docstring term_at_level;
1845 // The root has no term.
1846 std::cout << "<ROOT>" << std::endl;
1848 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1849 term_at_level = termAtLevel(root_node, depth - 1);
1850 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1854 for (const IndexEntry& entry : root_node->entries) {
1855 if (entry.terms().size() != depth) {
1856 std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1858 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1859 std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1864 for (const IndexNode* node : root_node->children) {
1865 printTree(node, depth + 1);
1868 #endif // LYX_INSET_INDEX_DEBUG
1872 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1874 BufferParams const & bp = buffer().masterBuffer()->params();
1876 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1880 // Collect the index entries in a form we can use them.
1881 vector<IndexEntry> entries;
1882 const docstring defaultIndexType = from_ascii("idx");
1883 const docstring & indexType = params().getParamOr("type", defaultIndexType);
1884 for (const TocItem& item : *toc) {
1885 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1886 if (item.isOutput() && inset && inset->params().index == indexType)
1887 entries.emplace_back(IndexEntry{inset, &op});
1890 // If all the index entries are in notes or not displayed, get out sooner.
1891 if (entries.empty())
1894 const IndexNode* index_root = buildIndexTree(entries);
1895 #ifdef LYX_INSET_INDEX_DEBUG
1896 printTree(index_root);
1899 // Start generating the XHTML index.
1900 Layout const & lay = bp.documentClass().htmlTOCLayout();
1901 string const & tocclass = lay.defaultCSSClass();
1902 string const tocattr = "class='index " + tocclass + "'";
1903 docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1905 // we'll use our own stream, because we are going to defer everything.
1906 // that's how we deal with the fact that we're probably inside a standard
1907 // paragraph, and we don't want to be.
1908 odocstringstream ods;
1911 xs << xml::StartTag("div", tocattr);
1913 xs << xml::StartTag(lay.htmltag(), lay.htmlGetAttrString());
1914 xs << translateIfPossible(indexName, getLocalOrDefaultLang(op)->lang());
1915 xs << xml::EndTag(lay.htmltag());
1917 xs << xml::StartTag("ul", "class='main'");
1920 LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1921 for (const IndexNode* node : index_root->children) {
1922 outputIndexPage(xs, node);
1926 xs << xml::EndTag("ul");
1928 xs << xml::EndTag("div");