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"
34 #include "output_latex.h"
35 #include "output_xhtml.h"
37 #include "texstream.h"
38 #include "TextClass.h"
39 #include "TocBackend.h"
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/FileName.h"
44 #include "support/gettext.h"
45 #include "support/Lexer.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 // Handle both modern ranges (params_.range) and legacy ones (with a suffix |( or |) as in pure LaTeX).
390 const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None ||
391 latexString.find(from_ascii("|(")) != lyx::docstring::npos ||
392 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
393 const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
394 latexString.find(from_ascii("|(")) != lyx::docstring::npos;
395 const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
396 latexString.find(from_ascii("|)")) != lyx::docstring::npos;
399 // Remove the ranges from the command if they do not appear at the beginning.
401 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
402 command.erase(index, 1);
404 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
405 command.erase(index, 1);
407 // Remove the ranges when they are the only vertical bar in the complete string.
408 if (command[0] == '(' || command[0] == ')')
412 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
413 // Both commands are mutually exclusive!
414 docstring see = getSeeAsText(runparams);
415 vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
417 if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
418 // Unescape brackets.
420 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
421 command.erase(index, 1);
423 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
424 command.erase(index, 1);
426 // Retrieve the part between brackets, and remove the complete seealso.
427 size_t positionOpeningBracket = command.find(from_ascii("{"));
428 size_t positionClosingBracket = command.find(from_ascii("}"));
429 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
431 // Parse the list of referenced entries (or a single one for see).
432 if (command.substr(0, 7) == "seealso") {
433 seeAlsoes = getVectorFromString(list, from_ascii(","), false);
437 if (see.find(from_ascii(",")) != std::string::npos) {
438 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
439 "Complete entry: \"") + latexString + from_utf8("\"");
441 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
445 // Remove the complete see/seealso from the commands, in case there is something else to parse.
446 command = command.substr(positionClosingBracket + 1);
449 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
450 // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
451 // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
452 // no range will be detected.
453 // TODO: Could handle formatting as significance="preferred"?
454 if (!command.empty()) {
455 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
456 + command + from_utf8(". Complete entry: \"") + latexString + from_utf8("\"");
458 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
461 // Write all of this down.
462 if (terms.empty() && !hasEndRange) {
463 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
465 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
467 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
468 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
469 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
470 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
471 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
473 // indexType can only be used for singular and startofrange types!
475 if (!hasStartRange && !hasEndRange) {
478 // Append an ID if uniqueness is not guaranteed across the document.
479 static QThreadStorage<set<docstring>> tKnownTermLists;
480 static QThreadStorage<int> tID;
482 set<docstring> &knownTermLists = tKnownTermLists.localData();
483 int &ID = tID.localData();
485 if (!tID.hasLocalData()) {
489 // Modify the index terms to add the unique ID if needed.
490 docstring newIndexTerms = indexTerms;
491 if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
492 newIndexTerms += from_ascii(string("-") + to_string(ID));
494 // Only increment for the end of range, so that the same number is used for the start of range.
500 // Term list not yet known: add it to the set AFTER the end of range. After
501 if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
502 knownTermLists.insert(indexTerms);
505 // Generate the attributes.
506 docstring id = xml::cleanID(newIndexTerms);
508 attrs = indexType + R"( class="startofrange" xml:id=")" + id + "\"";
510 attrs = R"( class="endofrange" startref=")" + id + "\"";
514 // Handle the index terms (including the specific index for this entry).
516 xs << xml::CompTag("indexterm", attrs);
518 xs << xml::StartTag("indexterm", attrs);
519 if (!terms.empty()) { // hasEndRange has no content.
521 if (!sortAs.empty()) {
522 attr = from_utf8("sortas='") + sortAs + from_utf8("'");
525 xs << xml::StartTag("primary", attr);
527 xs << xml::EndTag("primary");
529 if (terms.size() > 1) {
530 xs << xml::StartTag("secondary");
532 xs << xml::EndTag("secondary");
534 if (terms.size() > 2) {
535 xs << xml::StartTag("tertiary");
537 xs << xml::EndTag("tertiary");
540 // Handle see and see also.
542 xs << xml::StartTag("see");
544 xs << xml::EndTag("see");
547 if (!seeAlsoes.empty()) {
548 for (auto &entry : seeAlsoes) {
549 xs << xml::StartTag("seealso");
551 xs << xml::EndTag("seealso");
556 xs << xml::EndTag("indexterm");
562 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
564 // we just print an anchor, taking the paragraph ID from
565 // our own interior paragraph, which doesn't get printed
566 std::string const magic = paragraphs().front().magicLabel();
567 std::string const attr = "id='" + magic + "'";
568 xs << xml::CompTag("a", attr);
573 bool InsetIndex::showInsetDialog(BufferView * bv) const
575 bv->showDialog("index", params2string(params_),
576 const_cast<InsetIndex *>(this));
581 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
583 switch (cmd.action()) {
585 case LFUN_INSET_MODIFY: {
586 if (cmd.getArg(0) == "changetype") {
587 cur.recordUndoInset(this);
588 params_.index = from_utf8(cmd.getArg(1));
591 if (cmd.getArg(0) == "changeparam") {
592 string const p = cmd.getArg(1);
593 string const v = cmd.getArg(2);
594 cur.recordUndoInset(this);
596 params_.range = insetindexpagerangetranslator().find(v);
597 if (p == "pagefmt") {
598 if (v == "default" || v == "textbf"
599 || v == "textit" || v == "emph")
602 lyx::dispatch(FuncRequest(LFUN_INSET_SETTINGS, "index"));
606 InsetIndexParams params;
607 InsetIndex::string2params(to_utf8(cmd.argument()), params);
608 cur.recordUndoInset(this);
609 params_.index = params.index;
610 params_.range = params.range;
611 params_.pagefmt = params.pagefmt;
612 // what we really want here is a TOC update, but that means
613 // a full buffer update
614 cur.forceBufferUpdate();
618 case LFUN_INSET_DIALOG_UPDATE:
619 cur.bv().updateDialog("index", params2string(params_));
622 case LFUN_PARAGRAPH_BREAK: {
623 // Since this inset in single-par anyway, let's use
624 // return to enter subentries
625 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
630 case LFUN_INSET_INSERT_COPY: {
631 Cursor & bvcur = cur.bv().cursor();
632 if (cmd.origin() == FuncRequest::TOC && bvcur.inTexted()) {
633 cap::copyInsetToTemp(cur, clone());
634 cap::pasteFromTemp(bvcur, bvcur.buffer()->errorList("Paste"));
641 InsetCollapsible::doDispatch(cur, cmd);
647 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
648 FuncStatus & flag) const
650 switch (cmd.action()) {
652 case LFUN_INSET_MODIFY:
653 if (cmd.getArg(0) == "changetype") {
654 docstring const newtype = from_utf8(cmd.getArg(1));
655 Buffer const & realbuffer = *buffer().masterBuffer();
656 IndicesList const & indiceslist = realbuffer.params().indiceslist();
657 Index const * index = indiceslist.findShortcut(newtype);
658 flag.setEnabled(index != 0);
660 from_utf8(cmd.getArg(1)) == params_.index);
663 if (cmd.getArg(0) == "changeparam") {
664 string const p = cmd.getArg(1);
665 string const v = cmd.getArg(2);
667 flag.setEnabled(v == "none" || v == "start" || v == "end");
668 flag.setOnOff(params_.range == insetindexpagerangetranslator().find(v));
670 if (p == "pagefmt") {
671 flag.setEnabled(!v.empty());
672 if (params_.pagefmt == "default" || params_.pagefmt == "textbf"
673 || params_.pagefmt == "textit" || params_.pagefmt == "emph")
674 flag.setOnOff(params_.pagefmt == v);
676 flag.setOnOff(v == "custom");
680 return InsetCollapsible::getStatus(cur, cmd, flag);
682 case LFUN_INSET_DIALOG_UPDATE: {
683 Buffer const & realbuffer = *buffer().masterBuffer();
684 flag.setEnabled(realbuffer.params().use_indices);
688 case LFUN_INSET_INSERT_COPY:
689 // This can only be invoked by ToC widget
690 flag.setEnabled(cmd.origin() == FuncRequest::TOC
691 && cur.bv().cursor().inset().insetAllowed(lyxCode()));
694 case LFUN_PARAGRAPH_BREAK:
695 return macrosPossible("subentry");
697 case LFUN_INDEXMACRO_INSERT:
698 return macrosPossible(cmd.getArg(0));
700 case LFUN_INDEX_TAG_ALL: {
704 // move backwards into preceding word
705 // skip over other index insets
706 DocIterator dit(cur);
707 dit.backwardPosIgnoreCollapsed();
709 if (dit.inset().lyxCode() == INDEX_CODE)
711 else if (dit.prevInset() && dit.prevInset()->lyxCode() == INDEX_CODE)
712 dit.backwardPosIgnoreCollapsed();
717 // action not possible
719 // Check if we actually have a word to tag
720 FontSpan tw = dit.locateWord(WHOLE_WORD);
722 // action possible if we have a word of at least one char
723 return (tw.size() > 0);
727 return InsetCollapsible::getStatus(cur, cmd, flag);
732 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
734 Paragraph const & par = paragraphs().front();
735 InsetList::const_iterator it = par.insetList().begin();
736 for (; it != par.insetList().end(); ++it) {
737 Inset & inset = *it->inset;
738 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
739 InsetIndexMacro const & iim =
740 static_cast<InsetIndexMacro const &>(inset);
741 iim.getLatex(os, runparams);
748 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
750 Paragraph const & par = paragraphs().front();
751 InsetList::const_iterator it = par.insetList().begin();
752 for (; it != par.insetList().end(); ++it) {
753 Inset & inset = *it->inset;
754 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
756 InsetIndexMacro const & iim =
757 static_cast<InsetIndexMacro const &>(inset);
758 iim.getLatex(os, runparams);
762 return from_ascii("");
766 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
768 // Empty subentries crash makeindex. So warn and ignore this.
770 ErrorList & errorList = buffer().errorList("Export");
771 docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
772 "It will be ignored in the output."), mainentry);
773 Paragraph const & par = buffer().paragraphs().front();
774 errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
775 {par.id(), 0}, {par.id(), -1}));
776 buffer().bufferErrors(terr, errorList);
780 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
781 docstring const & mainentry) const
783 Paragraph const & par = paragraphs().front();
784 InsetList::const_iterator it = par.insetList().begin();
786 for (; it != par.insetList().end(); ++it) {
787 Inset & inset = *it->inset;
788 if (inset.lyxCode() == INDEXMACRO_CODE) {
789 InsetIndexMacro const & iim =
790 static_cast<InsetIndexMacro const &>(inset);
791 if (iim.params().type == InsetIndexMacroParams::Subentry) {
792 if (iim.hasNoContent()) {
793 emptySubentriesWarning(mainentry);
800 iim.getLatex(os, runparams);
807 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
808 bool const asLabel) const
810 std::vector<docstring> subentries;
812 Paragraph const & par = paragraphs().front();
813 InsetList::const_iterator it = par.insetList().begin();
815 for (; it != par.insetList().end(); ++it) {
816 Inset & inset = *it->inset;
817 if (inset.lyxCode() == INDEXMACRO_CODE) {
818 InsetIndexMacro const & iim =
819 static_cast<InsetIndexMacro const &>(inset);
820 if (iim.params().type == InsetIndexMacroParams::Subentry) {
826 docstring const sl = iim.getNewLabel(l);
827 subentries.emplace_back(sl);
830 iim.getLatex(os, runparams);
831 subentries.emplace_back(os.str());
841 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
844 InsetText::latex(os, runparams);
849 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
851 Paragraph const & par = paragraphs().front();
852 InsetList::const_iterator it = par.insetList().begin();
853 for (; it != par.insetList().end(); ++it) {
854 Inset & inset = *it->inset;
855 if (inset.lyxCode() == INDEXMACRO_CODE) {
856 InsetIndexMacro const & iim =
857 static_cast<InsetIndexMacro const &>(inset);
858 if (iim.params().type == InsetIndexMacroParams::See
859 || iim.params().type == InsetIndexMacroParams::Seealso) {
860 iim.getLatex(os, runparams);
868 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
869 bool const asLabel) const
871 Paragraph const & par = paragraphs().front();
872 InsetList::const_iterator it = par.insetList().begin();
873 for (; it != par.insetList().end(); ++it) {
874 Inset & inset = *it->inset;
875 if (inset.lyxCode() == INDEXMACRO_CODE) {
876 InsetIndexMacro const & iim =
877 static_cast<InsetIndexMacro const &>(inset);
878 if (iim.params().type == InsetIndexMacroParams::See) {
881 return iim.getNewLabel(l);
884 iim.getLatex(os, runparams);
890 return from_ascii("");
894 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
895 bool const asLabel) const
897 std::vector<docstring> seeAlsoes;
899 Paragraph const & par = paragraphs().front();
900 InsetList::const_iterator it = par.insetList().begin();
901 for (; it != par.insetList().end(); ++it) {
902 Inset & inset = *it->inset;
903 if (inset.lyxCode() == INDEXMACRO_CODE) {
904 InsetIndexMacro const & iim =
905 static_cast<InsetIndexMacro const &>(inset);
906 if (iim.params().type == InsetIndexMacroParams::Seealso) {
909 seeAlsoes.emplace_back(iim.getNewLabel(l));
912 iim.getLatex(os, runparams);
913 seeAlsoes.emplace_back(os.str());
925 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
926 const std::set<InsetIndexMacroParams::Type> types = {})
928 Paragraph const & par = inset_index->paragraphs().front();
929 InsetList::const_iterator it = par.insetList().begin();
930 for (; it != par.insetList().end(); ++it) {
931 Inset & inset = *it->inset;
932 if (inset.lyxCode() == code) {
936 LASSERT(code == INDEXMACRO_CODE, return false);
937 InsetIndexMacro const & iim =
938 static_cast<InsetIndexMacro const &>(inset);
939 if (types.find(iim.params().type) != types.end())
949 bool InsetIndex::hasSubentries() const
951 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
955 bool InsetIndex::hasSeeRef() const
957 return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
961 bool InsetIndex::hasSortKey() const
963 return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
967 bool InsetIndex::macrosPossible(string const type) const
969 if (type != "see" && type != "seealso"
970 && type != "sortkey" && type != "subentry")
973 Paragraph const & par = paragraphs().front();
974 InsetList::const_iterator it = par.insetList().begin();
976 for (; it != par.insetList().end(); ++it) {
977 Inset & inset = *it->inset;
978 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
980 if (inset.lyxCode() == INDEXMACRO_CODE) {
981 InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
982 if ((type == "see" || type == "seealso")
983 && (iim.params().type == InsetIndexMacroParams::See
984 || iim.params().type == InsetIndexMacroParams::Seealso))
986 if (type == "subentry"
987 && iim.params().type == InsetIndexMacroParams::Subentry) {
998 ColorCode InsetIndex::labelColor() const
1000 if (params_.index.empty() || params_.index == from_ascii("idx"))
1001 return InsetCollapsible::labelColor();
1003 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
1004 + "@" + buffer().fileName().absFileName());
1005 if (c == Color_none)
1006 c = InsetCollapsible::labelColor();
1011 docstring InsetIndex::toolTip(BufferView const &, int, int) const
1013 docstring tip = _("Index Entry");
1014 if (buffer().params().use_indices && !params_.index.empty()) {
1015 Buffer const & realbuffer = *buffer().masterBuffer();
1016 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1018 Index const * index = indiceslist.findShortcut(params_.index);
1020 tip += _("unknown type!");
1022 tip += index->index();
1026 docstring res = toolTipText(tip);
1027 if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
1028 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
1029 if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
1030 res += "\n" + _("Pagination format:") + " ";
1031 if (params_.pagefmt == "textbf")
1033 else if (params_.pagefmt == "textit")
1035 else if (params_.pagefmt == "emph")
1036 res += _("emphasized");
1038 res += from_utf8(params_.pagefmt);
1044 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
1046 InsetLayout const & il = getLayout();
1047 docstring label = translateIfPossible(il.labelstring());
1049 if (buffer().params().use_indices && !params_.index.empty()) {
1050 Buffer const & realbuffer = *buffer().masterBuffer();
1051 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1053 Index const * index = indiceslist.findShortcut(params_.index);
1055 label += _("unknown type!");
1057 label += index->index();
1062 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
1065 res = getNewLabel(label);
1066 OutputParams const rp(0);
1067 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1068 for (auto const & sublbl : sublbls) {
1069 res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1070 res += " " + sublbl;
1072 docstring see = getSeeAsText(rp, true);
1073 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1074 see = getSeeAlsoesAsText(rp, true).front();
1076 res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1080 if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
1081 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
1086 void InsetIndex::write(ostream & os) const
1088 os << to_utf8(layoutName());
1090 InsetCollapsible::write(os);
1094 void InsetIndex::read(Lexer & lex)
1097 InsetCollapsible::read(lex);
1101 string InsetIndex::params2string(InsetIndexParams const & params)
1110 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1112 params = InsetIndexParams();
1116 istringstream data(in);
1118 lex.setStream(data);
1119 lex.setContext("InsetIndex::string2params");
1125 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1126 UpdateType utype, TocBackend & backend) const
1128 DocIterator pit = cpit;
1129 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1131 InsetLayout const & il = getLayout();
1132 docstring label = translateIfPossible(il.labelstring());
1133 if (!il.contentaslabel())
1136 str = getNewLabel(label);
1137 OutputParams const rp(0);
1138 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1139 for (auto const & sublbl : sublbls) {
1140 str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1141 str += " " + sublbl;
1143 docstring see = getSeeAsText(rp, true);
1144 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1145 see = getSeeAlsoesAsText(rp, true).front();
1147 str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1151 string type = "index";
1152 if (buffer().masterBuffer()->params().use_indices)
1153 type += ":" + to_utf8(params_.index);
1154 TocBuilder & b = backend.builder(type);
1155 b.pushItem(pit, str, output_active);
1156 // Proceed with the rest of the inset.
1157 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1162 void InsetIndex::validate(LaTeXFeatures & features) const
1164 if (buffer().masterBuffer()->params().use_indices
1165 && !params_.index.empty()
1166 && params_.index != "idx")
1167 features.require("splitidx");
1168 InsetCollapsible::validate(features);
1172 string InsetIndex::contextMenuName() const
1174 return "context-index";
1178 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1180 // We override the implementation of InsetCollapsible,
1181 // because we have extra entries.
1182 string owncm = "context-edit-index;";
1183 return owncm + InsetCollapsible::contextMenu(bv, x, y);
1187 bool InsetIndex::hasSettings() const
1193 bool InsetIndex::insetAllowed(InsetCode code) const
1196 case INDEXMACRO_CODE:
1197 case INDEXMACRO_SORTKEY_CODE:
1202 return InsetCollapsible::insetAllowed(code);
1207 /////////////////////////////////////////////////////////////////////
1211 ///////////////////////////////////////////////////////////////////////
1214 void InsetIndexParams::write(ostream & os) const
1218 os << to_utf8(index);
1223 << insetindexpagerangetranslator().find(range)
1231 void InsetIndexParams::read(Lexer & lex)
1234 index = lex.getDocString();
1236 index = from_ascii("idx");
1237 if (lex.checkFor("range")) {
1238 string st = lex.getString();
1239 if (lex.eatLine()) {
1240 st = lex.getString();
1241 range = insetindexpagerangetranslator().find(lex.getString());
1244 if (lex.checkFor("pageformat") && lex.eatLine()) {
1245 pagefmt = lex.getString();
1250 /////////////////////////////////////////////////////////////////////
1254 ///////////////////////////////////////////////////////////////////////
1256 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1257 : InsetCommand(buf, p)
1261 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1263 static ParamInfo param_info_;
1264 if (param_info_.empty()) {
1265 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1266 ParamInfo::HANDLING_ESCAPE);
1267 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1268 ParamInfo::HANDLING_LATEXIFY);
1269 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1275 docstring InsetPrintIndex::screenLabel() const
1277 bool const printall = suffixIs(getCmdName(), '*');
1278 bool const multind = buffer().masterBuffer()->params().use_indices;
1280 && getParam("type") == from_ascii("idx"))
1281 || (getParam("type").empty() && !printall))
1283 Buffer const & realbuffer = *buffer().masterBuffer();
1284 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1285 Index const * index = indiceslist.findShortcut(getParam("type"));
1286 if (!index && !printall)
1287 return _("Unknown index type!");
1288 docstring res = printall ? _("All indexes") : index->index();
1290 res += " (" + _("non-active") + ")";
1291 else if (contains(getCmdName(), "printsubindex"))
1292 res += " (" + _("subindex") + ")";
1297 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1299 return s == "printindex" || s == "printsubindex"
1300 || s == "printindex*" || s == "printsubindex*";
1304 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1306 switch (cmd.action()) {
1308 case LFUN_INSET_MODIFY: {
1309 if (cmd.argument() == from_ascii("toggle-subindex")) {
1310 string scmd = getCmdName();
1311 if (contains(scmd, "printindex"))
1312 scmd = subst(scmd, "printindex", "printsubindex");
1314 scmd = subst(scmd, "printsubindex", "printindex");
1318 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1319 string scmd = getCmdName();
1320 if (suffixIs(scmd, '*'))
1324 setParam("type", docstring());
1328 InsetCommandParams p(INDEX_PRINT_CODE);
1330 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1331 if (p.getCmdName().empty()) {
1332 cur.noScreenUpdate();
1341 InsetCommand::doDispatch(cur, cmd);
1347 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1348 FuncStatus & status) const
1350 switch (cmd.action()) {
1352 case LFUN_INSET_MODIFY: {
1353 if (cmd.argument() == from_ascii("toggle-subindex")) {
1354 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1355 status.setOnOff(contains(getCmdName(), "printsubindex"));
1357 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1358 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1359 status.setOnOff(suffixIs(getCmdName(), '*'));
1361 } if (cmd.getArg(0) == "index_print"
1362 && cmd.getArg(1) == "CommandInset") {
1363 InsetCommandParams p(INDEX_PRINT_CODE);
1364 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1365 if (suffixIs(p.getCmdName(), '*')) {
1366 status.setEnabled(true);
1367 status.setOnOff(false);
1370 Buffer const & realbuffer = *buffer().masterBuffer();
1371 IndicesList const & indiceslist =
1372 realbuffer.params().indiceslist();
1373 Index const * index = indiceslist.findShortcut(p["type"]);
1374 status.setEnabled(index != 0);
1375 status.setOnOff(p["type"] == getParam("type"));
1378 return InsetCommand::getStatus(cur, cmd, status);
1381 case LFUN_INSET_DIALOG_UPDATE: {
1382 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1387 return InsetCommand::getStatus(cur, cmd, status);
1392 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1394 Index const * index =
1395 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1397 setParam("name", index->index());
1401 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1403 if (!buffer().masterBuffer()->params().use_indices) {
1404 if (getParam("type") == from_ascii("idx"))
1405 os << "\\printindex" << termcmd;
1408 OutputParams runparams = runparams_in;
1409 os << getCommand(runparams);
1413 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1415 features.require("makeidx");
1416 if (buffer().masterBuffer()->params().use_indices)
1417 features.require("splitidx");
1418 InsetCommand::validate(features);
1422 string InsetPrintIndex::contextMenuName() const
1424 return buffer().masterBuffer()->params().use_indices ?
1425 "context-indexprint" : string();
1429 bool InsetPrintIndex::hasSettings() const
1431 return buffer().masterBuffer()->params().use_indices;
1438 /// Builds an entry for the index.
1439 IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1441 LASSERT(runparams, return);
1443 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1444 odocstringstream entry;
1445 OutputParams ours = *runparams;
1446 ours.for_toc = false;
1447 inset_->plaintext(entry, ours);
1448 entry_ = entry.str();
1450 // Determine in which index this entry belongs to.
1451 if (inset_->buffer().masterBuffer()->params().use_indices) {
1452 index_ = inset_->params_.index;
1455 // Attempt parsing the inset.
1466 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1467 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1468 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1469 // is not allowed (i.e. behaviour is undefined).
1470 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1471 || inset_->params_.range != InsetIndexParams::PageRange::None;
1472 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1473 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1475 if (is_definitely_legacy && is_definitely_modern)
1476 output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1479 // - is_definitely_modern == true:
1480 // - is_definitely_legacy == true: error (return whatever)
1481 // - is_definitely_legacy == false: return modern
1482 // - is_definitely_modern == false:
1483 // - is_definitely_legacy == true: return legacy
1484 // - is_definitely_legacy == false: return modern
1485 return !is_definitely_legacy;
1488 void parseAsModern()
1490 LASSERT(runparams_, return);
1492 if (inset_->hasSortKey()) {
1493 sort_as_ = inset_->getSortkeyAsText(*runparams_);
1496 terms_ = inset_->getSubentriesAsText(*runparams_);
1497 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1498 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1499 // full string within this inset (i.e. without the subinsets).
1500 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1502 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1503 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1505 see_ = inset_->getSeeAsText(*runparams_);
1506 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1509 void parseAsLegacy() {
1510 // Determine if some features are known not to be supported. For now, this is only formatting like
1511 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1512 // @ is supported, but only for sorting, without specific formatting.
1513 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1514 output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1515 "Complete entry: \"") + entry_ + from_utf8("\". ");
1517 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1518 output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1519 "Complete entry: \"") + entry_ + from_utf8("\". ");
1522 // Split the string into its main constituents: terms, and command (see, see also, range).
1523 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1524 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1526 if (positionVerticalBar != lyx::docstring::npos) {
1527 command = entry_.substr(positionVerticalBar + 1);
1530 // Handle sorting issues, with @.
1531 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1532 if (sortingElements.size() == 2) {
1533 sort_as_ = sortingElements[0];
1534 indexTerms = sortingElements[1];
1537 // Handle entries, subentries, and subsubentries.
1538 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1540 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1541 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1542 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1544 // - Remove the ranges from the command if they do not appear at the beginning.
1545 size_t range_index = 0;
1546 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1547 command.erase(range_index, 1);
1549 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1550 command.erase(range_index, 1);
1552 // - Remove the ranges when they are the only vertical bar in the complete string.
1553 if (command[0] == '(' || command[0] == ')')
1554 command.erase(0, 1);
1556 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1557 // Both commands are mutually exclusive!
1558 if (command.substr(0, 3) == "see") {
1559 // Unescape brackets.
1560 size_t index_argument_begin = 0;
1561 while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1562 command.erase(index_argument_begin, 1);
1563 size_t index_argument_end = 0;
1564 while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1565 command.erase(index_argument_end, 1);
1567 // Retrieve the part between brackets, and remove the complete seealso.
1568 size_t position_opening_bracket = command.find(from_ascii("{"));
1569 size_t position_closing_bracket = command.find(from_ascii("}"));
1570 docstring argument = command.substr(position_opening_bracket + 1,
1571 position_closing_bracket - position_opening_bracket - 1);
1573 // Parse the argument of referenced entries (or a single one for see).
1574 if (command.substr(0, 7) == "seealso") {
1575 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1579 if (see_.find(from_ascii(",")) != std::string::npos) {
1580 output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1581 "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1585 // Remove the complete see/seealso from the commands, in case there is something else to parse.
1586 command = command.substr(position_closing_bracket + 1);
1589 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1590 // things like formatting the entry or the page number, other strings for sorting.
1591 // https://wiki.lyx.org/Tips/Indexing
1592 // If there are such things in the index entry, then this code may miserably fail. For example, for
1593 // "Peter|(textbf", no range will be detected.
1594 if (!command.empty()) {
1595 output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1596 + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1602 return terms_.size();
1605 const std::vector<docstring>& terms() const {
1609 std::vector<docstring>& terms() {
1613 const InsetIndex* inset() const {
1618 // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1619 // the constructor).
1620 const InsetIndex * inset_;
1621 OutputParams const * runparams_;
1623 docstring index_; // Useful when there are multiple indices in the same document.
1625 // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1626 // constructor is done).
1627 docstring output_error_;
1629 // Parsed index entry.
1630 std::vector<docstring> terms_; // Up to three entries, in general.
1633 bool has_start_range_ = false;
1634 bool has_end_range_ = false;
1636 vector<docstring> see_alsoes_;
1638 // Operators used for sorting entries (alphabetical order).
1639 friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1642 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1644 if (lhs.terms_.empty())
1647 for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1648 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1657 std::string generateCssClassAtDepth(unsigned depth) {
1658 std::string css_class = "entry";
1662 css_class.insert(0, "sub");
1669 std::vector<IndexEntry> entries;
1670 std::vector<IndexNode*> children;
1673 for (IndexNode * child : children) {
1681 docstring termAtLevel(const IndexNode* node, unsigned depth)
1683 // 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
1684 // function could be made constant time by copying the term in each node, but that would make data duplication that
1685 // may fall out of sync; the performance benefit would probably be negligible.
1686 if (!node->entries.empty()) {
1687 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1688 return node->entries.begin()->terms()[depth];
1691 if (!node->children.empty()) {
1692 return termAtLevel(*node->children.begin(), depth);
1695 LASSERT(false, return from_ascii(""));
1698 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1700 // Do not insert empty entries.
1701 if (entry.terms().empty())
1704 // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1705 for (IndexNode* child : node->children) {
1706 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1707 if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1708 // All term entries match: it's an entry.
1709 if (!entry.terms()[depth].empty())
1710 child->entries.emplace_back(entry);
1713 insertIntoNode(entry, child, depth + 1);
1719 // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1720 // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1721 // first entry for a word has several levels that never appeared.
1722 // In particular, this case is called for the first entry.
1723 IndexNode* new_node = node;
1725 new_node->children.emplace_back(new IndexNode{{}, {}});
1726 new_node = new_node->children.back();
1728 } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1729 new_node->entries.emplace_back(entry);
1732 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1734 // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1735 // thanks to the implementation of operator<.
1736 // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1737 // unsorted entries would make its complexity blow up).
1738 stable_sort(entries.begin(), entries.end());
1740 // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1742 auto* index_root = new IndexNode{{}, {}};
1743 for (const IndexEntry& entry : entries) {
1744 insertIntoNode(entry, index_root);
1750 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1752 LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1754 xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1756 xs << termAtLevel(root_node, depth);
1757 // By tree assumption, all the entries at this node have the same set of terms.
1759 if (!root_node->entries.empty()) {
1760 xs << XMLStream::ESCAPE_NONE << " — "; // Em dash, i.e. long (---).
1761 unsigned entry_number = 1;
1763 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1764 std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1765 xs << xml::StartTag("a", link_attr);
1766 xs << from_ascii(std::to_string(entry_number));
1767 xs << xml::EndTag("a");
1770 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1771 const IndexEntry &entry = root_node->entries[i];
1773 switch (entry.inset()->params().range) {
1774 case InsetIndexParams::PageRange::None:
1775 writeLinkToEntry(entry, entry_number);
1777 case InsetIndexParams::PageRange::Start: {
1778 // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1779 // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1780 // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1782 const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1783 root_node->entries[i + 1].inset()->params().range ==
1784 InsetIndexParams::PageRange::End;
1785 // No need to check if both entries are for the same terms: they are in the same IndexNode.
1787 writeLinkToEntry(entry, entry_number);
1788 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1790 if (nextEntryIsEnd) {
1791 // Skip the next entry in the loop, write it right now, after the dash.
1794 writeLinkToEntry(root_node->entries[i], entry_number);
1798 case InsetIndexParams::PageRange::End:
1799 // This range end was not caught by the range start, do it now to avoid losing content.
1800 xs << XMLStream::ESCAPE_NONE << " – "; // En dash, i.e. semi-long (--).
1801 writeLinkToEntry(root_node->entries[i], entry_number);
1804 if (i < root_node->entries.size() - 1) {
1812 if (!root_node->entries.empty() && !root_node->children.empty()) {
1816 if (!root_node->children.empty()) {
1817 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1820 for (const IndexNode* child : root_node->children) {
1821 outputIndexPage(xs, child, depth + 1);
1824 xs << xml::EndTag("ul");
1828 xs << xml::EndTag("li");
1832 #ifdef LYX_INSET_INDEX_DEBUG
1833 void printTree(const IndexNode* root_node, unsigned depth = 0)
1835 static const std::string pattern = " ";
1837 for (unsigned i = 0; i < depth; ++i) {
1840 const std::string prefix_long = prefix + pattern + pattern;
1842 docstring term_at_level;
1844 // The root has no term.
1845 std::cout << "<ROOT>" << std::endl;
1847 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1848 term_at_level = termAtLevel(root_node, depth - 1);
1849 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1853 for (const IndexEntry& entry : root_node->entries) {
1854 if (entry.terms().size() != depth) {
1855 std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1857 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1858 std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1863 for (const IndexNode* node : root_node->children) {
1864 printTree(node, depth + 1);
1867 #endif // LYX_INSET_INDEX_DEBUG
1871 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1873 BufferParams const & bp = buffer().masterBuffer()->params();
1875 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1879 // Collect the index entries in a form we can use them.
1880 vector<IndexEntry> entries;
1881 const docstring defaultIndexType = from_ascii("idx");
1882 const docstring & indexType = params().getParamOr("type", defaultIndexType);
1883 for (const TocItem& item : *toc) {
1884 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1885 if (item.isOutput() && inset && inset->params().index == indexType)
1886 entries.emplace_back(IndexEntry{inset, &op});
1889 // If all the index entries are in notes or not displayed, get out sooner.
1890 if (entries.empty())
1893 const IndexNode* index_root = buildIndexTree(entries);
1894 #ifdef LYX_INSET_INDEX_DEBUG
1895 printTree(index_root);
1898 // Start generating the XHTML index.
1899 Layout const & lay = bp.documentClass().htmlTOCLayout();
1900 string const & tocclass = lay.defaultCSSClass();
1901 string const tocattr = "class='index " + tocclass + "'";
1902 docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1904 // we'll use our own stream, because we are going to defer everything.
1905 // that's how we deal with the fact that we're probably inside a standard
1906 // paragraph, and we don't want to be.
1907 odocstringstream ods;
1910 xs << xml::StartTag("div", tocattr);
1912 xs << xml::StartTag(lay.htmltag(), lay.htmlGetAttrString());
1913 xs << translateIfPossible(indexName, getLocalOrDefaultLang(op)->lang());
1914 xs << xml::EndTag(lay.htmltag());
1916 xs << xml::StartTag("ul", "class='main'");
1919 LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1920 for (const IndexNode* node : index_root->children) {
1921 outputIndexPage(xs, node);
1925 xs << xml::EndTag("ul");
1927 xs << xml::EndTag("div");