]> git.lyx.org Git - lyx.git/blob - src/insets/InsetIndex.cpp
Pass two string parameters as reference
[lyx.git] / src / insets / InsetIndex.cpp
1 /**
2  * \file InsetIndex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetIndex.h"
14 #include "InsetIndexMacro.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "ColorSet.h"
20 #include "Cursor.h"
21 #include "CutAndPaste.h"
22 #include "DispatchResult.h"
23 #include "Encoding.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "IndicesList.h"
28 #include "InsetList.h"
29 #include "Language.h"
30 #include "Paragraph.h"
31 #include "LaTeX.h"
32 #include "LaTeXFeatures.h"
33 #include "Lexer.h"
34 #include "LyX.h"
35 #include "output_latex.h"
36 #include "output_xhtml.h"
37 #include "xml.h"
38 #include "texstream.h"
39 #include "TextClass.h"
40 #include "TocBackend.h"
41
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"
48
49 #include "frontends/alert.h"
50
51 #include <algorithm>
52 #include <set>
53 #include <iostream>
54
55 #include <QThreadStorage>
56
57 using namespace std;
58 using namespace lyx::support;
59
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
62
63 namespace lyx {
64
65 namespace {
66
67 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
68 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
69
70 PageRangeTranslator const init_insetindexpagerangetranslator()
71 {
72         PageRangeTranslator translator("none", InsetIndexParams::None);
73         translator.addPair("start", InsetIndexParams::Start);
74         translator.addPair("end", InsetIndexParams::End);
75         return translator;
76 }
77
78 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
79 {
80         PageRangeTranslator translator("", InsetIndexParams::None);
81         translator.addPair("(", InsetIndexParams::Start);
82         translator.addPair(")", InsetIndexParams::End);
83         return translator;
84 }
85
86
87 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
88 {
89         PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
90         translator.addPair(_("Starts page range"), InsetIndexParams::Start);
91         translator.addPair(_("Ends page range"), InsetIndexParams::End);
92         return translator;
93 }
94
95
96 PageRangeTranslator const & insetindexpagerangetranslator()
97 {
98         static PageRangeTranslator const prtranslator =
99                         init_insetindexpagerangetranslator();
100         return prtranslator;
101 }
102
103
104 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
105 {
106         static PageRangeTranslatorLoc const translator =
107                         init_insetindexpagerangetranslator_loc();
108         return translator;
109 }
110
111
112 PageRangeTranslator const & insetindexpagerangetranslator_latex()
113 {
114         static PageRangeTranslator const lttranslator =
115                         init_insetindexpagerangetranslator_latex();
116         return lttranslator;
117 }
118
119 } // namespace anon
120
121 /////////////////////////////////////////////////////////////////////
122 //
123 // InsetIndex
124 //
125 ///////////////////////////////////////////////////////////////////////
126
127
128 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
129         : InsetCollapsible(buf), params_(params)
130 {}
131
132
133 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
134 {
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;
140
141         otexstringstream os;
142
143         if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
144                 && params_.index != "idx") {
145                 os << "\\sindex[";
146                 os << escape(params_.index);
147                 os << "]{";
148         } else {
149                 os << "\\index";
150                 os << '{';
151         }
152
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() << "}";
161                 return;
162         }
163
164         if (hasSortKey()) {
165                 getSortkey(os, runparams);
166                 os << "@";
167                 os << ourlatex.str();
168                 getSubentries(os, runparams, ourlatex.str());
169                 if (hasSeeRef()) {
170                         os << "|";
171                         os << insetindexpagerangetranslator_latex().find(params_.range);
172                         getSeeRefs(os, runparams);
173                 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
174                         os << "|";
175                         os << insetindexpagerangetranslator_latex().find(params_.range);
176                         os << from_utf8(params_.pagefmt);
177                 } else if (params_.range != InsetIndexParams::PageRange::None) {
178                         os << "|";
179                         os << insetindexpagerangetranslator_latex().find(params_.range);
180                 }
181         } else {
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);
186
187                 // These are the LaTeX and plaintext representations
188                 docstring latexstr = ourlatex.str();
189                 docstring plainstr = ourplain.str();
190         
191                 // This will get what follows | if anything does,
192                 // the command (e.g., see, textbf) for pagination
193                 // formatting
194                 docstring cmd;
195
196                 if (hasSeeRef()) {
197                         odocstringstream seeref;
198                         otexstream otsee(seeref);
199                         getSeeRefs(otsee, runparams);
200                         cmd = seeref.str();
201                 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
202                         cmd = from_utf8(params_.pagefmt);
203                 } else {
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
207                         // prevent that.
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);
218                                 else
219                                         LYXERR0("The `|' separator was not found in the plaintext version!");
220                         }
221                 }
222
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
230                         // prevent that.
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);
235                 
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();
239                         bool first = true;
240                         for (; it != end; ++it) {
241                                 if ((*it).empty()) {
242                                         emptySubentriesWarning(ourlatex.str());
243                                         if (it2 < levels_plain.end())
244                                                 ++it2;
245                                         continue;
246                                 }
247                                 // The separator needs to be put back when
248                                 // writing the levels, except for the first level
249                                 if (!first)
250                                         os << '!';
251                                 else
252                                         first = false;
253                 
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())
266                                         ++it2;
267                         }
268                 } else {
269                         processLatexSorting(os, runparams, latexstr, plainstr);
270                         os << subentries.str();
271                 }
272
273                 // At last, re-insert the command, separated by "|"
274                 if (!cmd.empty()) {
275                         os << "|"
276                            << insetindexpagerangetranslator_latex().find(params_.range)
277                            << cmd;
278                 } else if (params_.range != InsetIndexParams::PageRange::None) {
279                         os << "|";
280                         os << insetindexpagerangetranslator_latex().find(params_.range);
281                 }
282         }
283         os << '}';
284
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();
289         else
290                 ios << os.release();
291 }
292
293
294 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
295                                 docstring const & latex, docstring const & spart) const
296 {
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) {
307                         TeXErrors terr;
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);
317                 }
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("\\\""));
323
324                 // Now insert the sortkey, separated by '@'.
325                 os << ppart;
326                 os << '@';
327         }
328         // Insert the actual level text
329         os << latex;
330 }
331
332
333 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
334 {
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.
339
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());
347
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 + "\"";
352         }
353
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);
357         docstring command;
358         if (positionVerticalBar != lyx::docstring::npos) {
359                 command = latexString.substr(positionVerticalBar + 1);
360         }
361
362         // Handle sorting issues, with @.
363         docstring sortAs;
364         if (hasSortKey()) {
365                 sortAs = getSortkeyAsText(runparams);
366                 // indexTerms may contain a sort key if the user has both the inset and the manual key.
367         } else {
368                 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
369                 if (sortingElements.size() == 2) {
370                         sortAs = sortingElements[0];
371                         indexTerms = sortingElements[1];
372                 }
373         }
374
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);
384         } else {
385                 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
386         }
387
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;
394
395         if (hasInsetRange) {
396                 // Remove the ranges from the command if they do not appear at the beginning.
397                 size_t index = 0;
398                 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
399                         command.erase(index, 1);
400                 index = 0;
401                 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
402                         command.erase(index, 1);
403
404                 // Remove the ranges when they are the only vertical bar in the complete string.
405                 if (command[0] == '(' || command[0] == ')')
406                         command.erase(0, 1);
407         }
408
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);
413
414         if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
415                 // Unescape brackets.
416                 size_t index = 0;
417                 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
418                         command.erase(index, 1);
419                 index = 0;
420                 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
421                         command.erase(index, 1);
422
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);
427
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);
431                 } else {
432                         see = list;
433
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("\"");
437                                 LYXERR0(error);
438                                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
439                         }
440                 }
441
442                 // Remove the complete see/seealso from the commands, in case there is something else to parse.
443                 command = command.substr(positionClosingBracket + 1);
444         }
445
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("\"");
454                 LYXERR0(error);
455                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
456         }
457
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("\"");
461                 LYXERR0(error);
462                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
463         } else {
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
469                 // xml::cleanID.
470                 // indexType can only be used for singular and startofrange types!
471                 docstring attrs;
472                 if (!hasStartRange && !hasEndRange) {
473                         attrs = indexType;
474                 } else {
475                         // Append an ID if uniqueness is not guaranteed across the document.
476                         static QThreadStorage<set<docstring>> tKnownTermLists;
477                         static QThreadStorage<int> tID;
478
479                         set<docstring> &knownTermLists = tKnownTermLists.localData();
480                         int &ID = tID.localData();
481
482                         if (!tID.hasLocalData()) {
483                                 tID.localData() = 0;
484                         }
485
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));
490
491                                 // Only increment for the end of range, so that the same number is used for the start of range.
492                                 if (hasEndRange) {
493                                         ID++;
494                                 }
495                         }
496
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);
500                         }
501
502                         // Generate the attributes.
503                         docstring id = xml::cleanID(newIndexTerms);
504                         if (hasStartRange) {
505                                 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
506                         } else {
507                                 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
508                         }
509                 }
510
511                 // Handle the index terms (including the specific index for this entry).
512                 if (hasEndRange) {
513                         xs << xml::CompTag("indexterm", attrs);
514                 } else {
515                         xs << xml::StartTag("indexterm", attrs);
516                         if (!terms.empty()) { // hasEndRange has no content.
517                                 docstring attr;
518                                 if (!sortAs.empty()) {
519                                         attr = from_utf8("sortas='") + sortAs + from_utf8("'");
520                                 }
521
522                                 xs << xml::StartTag("primary", attr);
523                                 xs << terms[0];
524                                 xs << xml::EndTag("primary");
525                         }
526                         if (terms.size() > 1) {
527                                 xs << xml::StartTag("secondary");
528                                 xs << terms[1];
529                                 xs << xml::EndTag("secondary");
530                         }
531                         if (terms.size() > 2) {
532                                 xs << xml::StartTag("tertiary");
533                                 xs << terms[2];
534                                 xs << xml::EndTag("tertiary");
535                         }
536
537                         // Handle see and see also.
538                         if (!see.empty()) {
539                                 xs << xml::StartTag("see");
540                                 xs << see;
541                                 xs << xml::EndTag("see");
542                         }
543
544                         if (!seeAlsoes.empty()) {
545                                 for (auto &entry : seeAlsoes) {
546                                         xs << xml::StartTag("seealso");
547                                         xs << entry;
548                                         xs << xml::EndTag("seealso");
549                                 }
550                         }
551
552                         // Close the entry.
553                         xs << xml::EndTag("indexterm");
554                 }
555         }
556 }
557
558
559 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
560 {
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);
566         return docstring();
567 }
568
569
570 bool InsetIndex::showInsetDialog(BufferView * bv) const
571 {
572         bv->showDialog("index", params2string(params_),
573                         const_cast<InsetIndex *>(this));
574         return true;
575 }
576
577
578 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
579 {
580         switch (cmd.action()) {
581
582         case LFUN_INSET_MODIFY: {
583                 if (cmd.getArg(0) == "changetype") {
584                         cur.recordUndoInset(this);
585                         params_.index = from_utf8(cmd.getArg(1));
586                         break;
587                 }
588                 if (cmd.getArg(0) == "changeparam") {
589                         string const p = cmd.getArg(1);
590                         string const v = cmd.getArg(2);
591                         cur.recordUndoInset(this);
592                         if (p == "range")
593                                 params_.range = insetindexpagerangetranslator().find(v);
594                         if (p == "pagefmt") {
595                                 if (v == "default" || v == "textbf"
596                                     || v == "textit" || v == "emph")
597                                         params_.pagefmt = v;
598                                 else
599                                         lyx::dispatch(FuncRequest(LFUN_INSET_SETTINGS, "index"));
600                         }
601                         break;
602                 }
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();
612                 break;
613         }
614
615         case LFUN_INSET_DIALOG_UPDATE:
616                 cur.bv().updateDialog("index", params2string(params_));
617                 break;
618
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");
623                 lyx::dispatch(fr);
624                 break;
625         }
626
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"));
632                 } else
633                         cur.undispatched();
634                 break;
635         }
636
637         default:
638                 InsetCollapsible::doDispatch(cur, cmd);
639                 break;
640         }
641 }
642
643
644 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
645                 FuncStatus & flag) const
646 {
647         switch (cmd.action()) {
648
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);
656                         flag.setOnOff(
657                                 from_utf8(cmd.getArg(1)) == params_.index);
658                         return true;
659                 }
660                 if (cmd.getArg(0) == "changeparam") {
661                         string const p = cmd.getArg(1);
662                         string const v = cmd.getArg(2);
663                         if (p == "range") {
664                                 flag.setEnabled(v == "none" || v == "start" || v == "end");
665                                 flag.setOnOff(params_.range == insetindexpagerangetranslator().find(v));
666                         }
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);
672                                 else
673                                         flag.setOnOff(v == "custom");
674                         }
675                         return true;
676                 }
677                 return InsetCollapsible::getStatus(cur, cmd, flag);
678
679         case LFUN_INSET_DIALOG_UPDATE: {
680                 Buffer const & realbuffer = *buffer().masterBuffer();
681                 flag.setEnabled(realbuffer.params().use_indices);
682                 return true;
683         }
684
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()));
689                 return true;
690
691         case LFUN_PARAGRAPH_BREAK:
692                 return macrosPossible("subentry");
693
694         case LFUN_INDEXMACRO_INSERT:
695                 return macrosPossible(cmd.getArg(0));
696
697         case LFUN_INDEX_TAG_ALL: {
698                 if (cur.pos() == 0)
699                         // nothing to tag
700                         return false;
701                 // move backwards into preceding word
702                 // skip over other index insets
703                 DocIterator dit(cur);
704                 dit.backwardPosIgnoreCollapsed();
705                 while (true) {
706                         if (dit.inset().lyxCode() == INDEX_CODE)
707                                 dit.pop_back();
708                         else if (dit.prevInset() && dit.prevInset()->lyxCode() == INDEX_CODE)
709                                 dit.backwardPosIgnoreCollapsed();
710                         else
711                                 break;
712                 }
713                 if (!dit.inTexted())
714                         // action not possible
715                         return false;
716                 // Check if we actually have a word to tag
717                 FontSpan tw = dit.locateWord(WHOLE_WORD);
718
719                 // action possible if we have a word of at least one char
720                 return (tw.size() > 0);
721         }
722
723         default:
724                 return InsetCollapsible::getStatus(cur, cmd, flag);
725         }
726 }
727
728
729 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
730 {
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);
739                         return;
740                 }
741         }
742 }
743
744
745 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
746 {
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) {
752                         otexstringstream os;
753                         InsetIndexMacro const & iim =
754                                 static_cast<InsetIndexMacro const &>(inset);
755                         iim.getLatex(os, runparams);
756                         return os.str();
757                 }
758         }
759         return from_ascii("");
760 }
761
762
763 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
764 {
765         // Empty subentries crash makeindex. So warn and ignore this.
766         TeXErrors terr;
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);
774 }
775
776
777 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
778                                docstring const & mainentry) const
779 {
780         Paragraph const & par = paragraphs().front();
781         InsetList::const_iterator it = par.insetList().begin();
782         int i = 0;
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);
791                                         continue;
792                                 }
793                                 ++i;
794                                 if (i > 2)
795                                         return;
796                                 os << "!";
797                                 iim.getLatex(os, runparams);
798                         }
799                 }
800         }
801 }
802
803
804 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
805                                                        bool const asLabel) const
806 {
807         std::vector<docstring> subentries;
808
809         Paragraph const & par = paragraphs().front();
810         InsetList::const_iterator it = par.insetList().begin();
811         int i = 0;
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) {
818                                 ++i;
819                                 if (i > 2)
820                                         break;
821                                 if (asLabel) {
822                                         docstring const l;
823                                         docstring const sl = iim.getNewLabel(l);
824                                         subentries.emplace_back(sl);
825                                 } else {
826                                         otexstringstream os;
827                                         iim.getLatex(os, runparams);
828                                         subentries.emplace_back(os.str());
829                                 }
830                         }
831                 }
832         }
833
834         return subentries;
835 }
836
837
838 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
839 {
840         otexstringstream os;
841         InsetText::latex(os, runparams);
842         return os.str();
843 }
844
845
846 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
847 {
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);
858                                 return;
859                         }
860                 }
861         }
862 }
863
864
865 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
866                                    bool const asLabel) const
867 {
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) {
876                                 if (asLabel) {
877                                         docstring const l;
878                                         return iim.getNewLabel(l);
879                                 } else {
880                                         otexstringstream os;
881                                         iim.getLatex(os, runparams);
882                                         return os.str();
883                                 }
884                         }
885                 }
886         }
887         return from_ascii("");
888 }
889
890
891 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
892                                                       bool const asLabel) const
893 {
894         std::vector<docstring> seeAlsoes;
895
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) {
904                                 if (asLabel) {
905                                         docstring const l;
906                                         seeAlsoes.emplace_back(iim.getNewLabel(l));
907                                 } else {
908                                         otexstringstream os;
909                                         iim.getLatex(os, runparams);
910                                         seeAlsoes.emplace_back(os.str());
911                                 }
912                         }
913                 }
914         }
915
916         return seeAlsoes;
917 }
918
919
920 namespace {
921
922 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
923                                           const std::set<InsetIndexMacroParams::Type> types = {})
924 {
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) {
930                         if (types.empty())
931                                 return true;
932
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())
937                                 return true;
938                 }
939         }
940         return false;
941 }
942
943 } // namespace
944
945
946 bool InsetIndex::hasSubentries() const
947 {
948         return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
949 }
950
951
952 bool InsetIndex::hasSeeRef() const
953 {
954         return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
955 }
956
957
958 bool InsetIndex::hasSortKey() const
959 {
960         return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
961 }
962
963
964 bool InsetIndex::macrosPossible(string const type) const
965 {
966         if (type != "see" && type != "seealso"
967             && type != "sortkey" && type != "subentry")
968                 return false;
969
970         Paragraph const & par = paragraphs().front();
971         InsetList::const_iterator it = par.insetList().begin();
972         int subidxs = 0;
973         for (; it != par.insetList().end(); ++it) {
974                 Inset & inset = *it->inset;
975                 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
976                         return false;
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))
982                                 return false;
983                         if (type == "subentry"
984                              && iim.params().type == InsetIndexMacroParams::Subentry) {
985                                 ++subidxs;
986                                 if (subidxs > 1)
987                                         return false;
988                         }
989                 }
990         }
991         return true;
992 }
993
994
995 ColorCode InsetIndex::labelColor() const
996 {
997         if (params_.index.empty() || params_.index == from_ascii("idx"))
998                 return InsetCollapsible::labelColor();
999         // FIXME UNICODE
1000         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
1001                                             + "@" + buffer().fileName().absFileName());
1002         if (c == Color_none)
1003                 c = InsetCollapsible::labelColor();
1004         return c;
1005 }
1006
1007
1008 docstring InsetIndex::toolTip(BufferView const &, int, int) const
1009 {
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();
1014                 tip += " (";
1015                 Index const * index = indiceslist.findShortcut(params_.index);
1016                 if (!index)
1017                         tip += _("unknown type!");
1018                 else
1019                         tip += index->index();
1020                 tip += ")";
1021         }
1022         tip += ": ";
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")
1029                         res += _("bold");
1030                 else if (params_.pagefmt == "textit")
1031                         res += _("italic");
1032                 else if (params_.pagefmt == "emph")
1033                         res += _("emphasized");
1034                 else
1035                         res += from_utf8(params_.pagefmt);
1036         }
1037         return res;
1038 }
1039
1040
1041 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
1042 {
1043         InsetLayout const & il = getLayout();
1044         docstring label = translateIfPossible(il.labelstring());
1045
1046         if (buffer().params().use_indices && !params_.index.empty()) {
1047                 Buffer const & realbuffer = *buffer().masterBuffer();
1048                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
1049                 label += " (";
1050                 Index const * index = indiceslist.findShortcut(params_.index);
1051                 if (!index)
1052                         label += _("unknown type!");
1053                 else
1054                         label += index->index();
1055                 label += ")";
1056         }
1057
1058         docstring res;
1059         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
1060                 res = label;
1061         else {
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;
1068                 }
1069                 docstring see = getSeeAsText(rp, true);
1070                 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1071                         see = getSeeAlsoesAsText(rp, true).front();
1072                 if (!see.empty()) {
1073                         res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1074                         res += " " + see;
1075                 }
1076         }
1077         if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
1078                 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
1079         return res;
1080 }
1081
1082
1083 void InsetIndex::write(ostream & os) const
1084 {
1085         os << to_utf8(layoutName());
1086         params_.write(os);
1087         InsetCollapsible::write(os);
1088 }
1089
1090
1091 void InsetIndex::read(Lexer & lex)
1092 {
1093         params_.read(lex);
1094         InsetCollapsible::read(lex);
1095 }
1096
1097
1098 string InsetIndex::params2string(InsetIndexParams const & params)
1099 {
1100         ostringstream data;
1101         data << "index";
1102         params.write(data);
1103         return data.str();
1104 }
1105
1106
1107 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1108 {
1109         params = InsetIndexParams();
1110         if (in.empty())
1111                 return;
1112
1113         istringstream data(in);
1114         Lexer lex;
1115         lex.setStream(data);
1116         lex.setContext("InsetIndex::string2params");
1117         lex >> "index";
1118         params.read(lex);
1119 }
1120
1121
1122 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1123                                                   UpdateType utype, TocBackend & backend) const
1124 {
1125         DocIterator pit = cpit;
1126         pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1127         docstring str;
1128         InsetLayout const & il = getLayout();
1129         docstring label = translateIfPossible(il.labelstring());
1130         if (!il.contentaslabel())
1131                 str = label;
1132         else {
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;
1139                 }
1140                 docstring see = getSeeAsText(rp, true);
1141                 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1142                         see = getSeeAlsoesAsText(rp, true).front();
1143                 if (!see.empty()) {
1144                         str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1145                         str += " " + see;
1146                 }
1147         }
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);
1155         b.pop();
1156 }
1157
1158
1159 void InsetIndex::validate(LaTeXFeatures & features) const
1160 {
1161         if (buffer().masterBuffer()->params().use_indices
1162             && !params_.index.empty()
1163             && params_.index != "idx")
1164                 features.require("splitidx");
1165         InsetCollapsible::validate(features);
1166 }
1167
1168
1169 string InsetIndex::contextMenuName() const
1170 {
1171         return "context-index";
1172 }
1173
1174
1175 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1176 {
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);
1181 }
1182
1183
1184 bool InsetIndex::hasSettings() const
1185 {
1186         return true;
1187 }
1188
1189
1190 bool InsetIndex::insetAllowed(InsetCode code) const
1191 {
1192         switch (code) {
1193         case INDEXMACRO_CODE:
1194         case INDEXMACRO_SORTKEY_CODE:
1195                 return true;
1196         case INDEX_CODE:
1197                 return false;
1198         default:
1199                 return InsetCollapsible::insetAllowed(code);
1200         }
1201 }
1202
1203
1204 /////////////////////////////////////////////////////////////////////
1205 //
1206 // InsetIndexParams
1207 //
1208 ///////////////////////////////////////////////////////////////////////
1209
1210
1211 void InsetIndexParams::write(ostream & os) const
1212 {
1213         os << ' ';
1214         if (!index.empty())
1215                 os << to_utf8(index);
1216         else
1217                 os << "idx";
1218         os << '\n';
1219         os << "range "
1220            << insetindexpagerangetranslator().find(range)
1221            << '\n';
1222         os << "pageformat "
1223            << pagefmt
1224            << '\n';
1225 }
1226
1227
1228 void InsetIndexParams::read(Lexer & lex)
1229 {
1230         if (lex.eatLine())
1231                 index = lex.getDocString();
1232         else
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());
1239                 }
1240         }
1241         if (lex.checkFor("pageformat") && lex.eatLine()) {
1242                 pagefmt = lex.getString();
1243         }
1244 }
1245
1246
1247 /////////////////////////////////////////////////////////////////////
1248 //
1249 // InsetPrintIndex
1250 //
1251 ///////////////////////////////////////////////////////////////////////
1252
1253 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1254         : InsetCommand(buf, p)
1255 {}
1256
1257
1258 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1259 {
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);
1267         }
1268         return param_info_;
1269 }
1270
1271
1272 docstring InsetPrintIndex::screenLabel() const
1273 {
1274         bool const printall = suffixIs(getCmdName(), '*');
1275         bool const multind = buffer().masterBuffer()->params().use_indices;
1276         if ((!multind
1277              && getParam("type") == from_ascii("idx"))
1278             || (getParam("type").empty() && !printall))
1279                 return _("Index");
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();
1286         if (!multind)
1287                 res += " (" + _("non-active") + ")";
1288         else if (contains(getCmdName(), "printsubindex"))
1289                 res += " (" + _("subindex") + ")";
1290         return res;
1291 }
1292
1293
1294 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1295 {
1296         return s == "printindex" || s == "printsubindex"
1297                 || s == "printindex*" || s == "printsubindex*";
1298 }
1299
1300
1301 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1302 {
1303         switch (cmd.action()) {
1304
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");
1310                         else
1311                                 scmd = subst(scmd, "printsubindex", "printindex");
1312                         cur.recordUndo();
1313                         setCmdName(scmd);
1314                         break;
1315                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1316                         string scmd = getCmdName();
1317                         if (suffixIs(scmd, '*'))
1318                                 break;
1319                         scmd += '*';
1320                         cur.recordUndo();
1321                         setParam("type", docstring());
1322                         setCmdName(scmd);
1323                         break;
1324                 }
1325                 InsetCommandParams p(INDEX_PRINT_CODE);
1326                 // FIXME UNICODE
1327                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1328                 if (p.getCmdName().empty()) {
1329                         cur.noScreenUpdate();
1330                         break;
1331                 }
1332                 cur.recordUndo();
1333                 setParams(p);
1334                 break;
1335         }
1336
1337         default:
1338                 InsetCommand::doDispatch(cur, cmd);
1339                 break;
1340         }
1341 }
1342
1343
1344 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1345         FuncStatus & status) const
1346 {
1347         switch (cmd.action()) {
1348
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"));
1353                         return true;
1354                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1355                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
1356                         status.setOnOff(suffixIs(getCmdName(), '*'));
1357                         return true;
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);
1365                                 return true;
1366                         }
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"));
1373                         return true;
1374                 } else
1375                         return InsetCommand::getStatus(cur, cmd, status);
1376         }
1377
1378         case LFUN_INSET_DIALOG_UPDATE: {
1379                 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1380                 return true;
1381         }
1382
1383         default:
1384                 return InsetCommand::getStatus(cur, cmd, status);
1385         }
1386 }
1387
1388
1389 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1390 {
1391         Index const * index =
1392                 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1393         if (index)
1394                 setParam("name", index->index());
1395 }
1396
1397
1398 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1399 {
1400         if (!buffer().masterBuffer()->params().use_indices) {
1401                 if (getParam("type") == from_ascii("idx"))
1402                         os << "\\printindex" << termcmd;
1403                 return;
1404         }
1405         OutputParams runparams = runparams_in;
1406         os << getCommand(runparams);
1407 }
1408
1409
1410 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1411 {
1412         features.require("makeidx");
1413         if (buffer().masterBuffer()->params().use_indices)
1414                 features.require("splitidx");
1415         InsetCommand::validate(features);
1416 }
1417
1418
1419 string InsetPrintIndex::contextMenuName() const
1420 {
1421         return buffer().masterBuffer()->params().use_indices ?
1422                 "context-indexprint" : string();
1423 }
1424
1425
1426 bool InsetPrintIndex::hasSettings() const
1427 {
1428         return buffer().masterBuffer()->params().use_indices;
1429 }
1430
1431
1432 class IndexEntry
1433 {
1434 public:
1435         /// Builds an entry for the index.
1436         IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1437         {
1438                 LASSERT(runparams, return);
1439
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();
1446
1447                 // Determine in which index this entry belongs to.
1448                 if (inset_->buffer().masterBuffer()->params().use_indices) {
1449                         index_ = inset_->params_.index;
1450                 }
1451
1452                 // Attempt parsing the inset.
1453                 if (isModern())
1454                         parseAsModern();
1455                 else
1456                         parseAsLegacy();
1457         }
1458
1459
1460 private:
1461         bool isModern()
1462         {
1463 #ifdef LYX_INSET_INDEX_DEBUG
1464                 std::cout << to_utf8(entry_) << std::endl;
1465 #endif // LYX_INSET_INDEX_DEBUG
1466
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;
1475
1476                 if (is_definitely_legacy && is_definitely_modern)
1477                         output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1478
1479                 // Truth table:
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;
1487         }
1488
1489         void parseAsModern()
1490         {
1491                 LASSERT(runparams_, return);
1492
1493                 if (inset_->hasSortKey()) {
1494                         sort_as_ = inset_->getSortkeyAsText(*runparams_);
1495                 }
1496
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_));
1502
1503                 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1504                 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1505
1506                 see_ = inset_->getSeeAsText(*runparams_);
1507                 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1508         }
1509
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("\". ");
1517                 }
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("\". ");
1521                 }
1522
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);
1526                 docstring command;
1527                 if (positionVerticalBar != lyx::docstring::npos) {
1528                         command = entry_.substr(positionVerticalBar + 1);
1529                 }
1530
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];
1536                 }
1537
1538                 // Handle entries, subentries, and subsubentries.
1539                 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1540
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;
1544
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);
1549                 range_index = 0;
1550                 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1551                         command.erase(range_index, 1);
1552
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);
1556
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);
1567
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);
1573
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);
1577                         } else {
1578                                 see_ = argument;
1579
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("\". ");
1583                                 }
1584                         }
1585
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);
1588                 }
1589
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("\". ");
1598                 }
1599         }
1600
1601 public:
1602         int level() const {
1603                 return terms_.size();
1604         }
1605
1606         const std::vector<docstring>& terms() const {
1607                 return terms_;
1608         }
1609
1610         std::vector<docstring>& terms() {
1611                 return terms_;
1612         }
1613
1614         const InsetIndex* inset() const {
1615                 return inset_;
1616         }
1617
1618 private:
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_;
1623         docstring entry_;
1624         docstring index_; // Useful when there are multiple indices in the same document.
1625
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_;
1629
1630         // Parsed index entry.
1631         std::vector<docstring> terms_; // Up to three entries, in general.
1632         docstring sort_as_;
1633         docstring command_;
1634         bool has_start_range_;
1635         bool has_end_range_;
1636         docstring see_;
1637         vector<docstring> see_alsoes_;
1638
1639         // Operators used for sorting entries (alphabetical order).
1640         friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1641 };
1642
1643 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1644 {
1645         if (lhs.terms_.empty())
1646                 return false;
1647
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]);
1650                 if (comp != 0)
1651                         return comp < 0;
1652         }
1653         return false;
1654 }
1655
1656
1657 namespace {
1658 std::string generateCssClassAtDepth(unsigned depth) {
1659         std::string css_class = "entry";
1660
1661         while (depth > 0) {
1662                 depth -= 1;
1663                 css_class.insert(0, "sub");
1664         }
1665
1666         return css_class;
1667 }
1668
1669 struct IndexNode {
1670         std::vector<IndexEntry> entries;
1671         std::vector<IndexNode*> children;
1672
1673         ~IndexNode() {
1674                 for (IndexNode * child : children) {
1675                         if (!child)
1676                                 continue;
1677                         delete child;
1678                 }
1679         }
1680 };
1681
1682 docstring termAtLevel(const IndexNode* node, unsigned depth)
1683 {
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];
1690         }
1691
1692         if (!node->children.empty()) {
1693                 return termAtLevel(*node->children.begin(), depth);
1694         }
1695
1696         LASSERT(false, return from_ascii(""));
1697 }
1698
1699 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1700 {
1701         // Do not insert empty entries.
1702         if (entry.terms().empty())
1703                 return;
1704
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);
1712                                 return;
1713                         } else {
1714                                 insertIntoNode(entry, child, depth + 1);
1715                                 return;
1716                         }
1717                 }
1718         }
1719
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;
1725         do {
1726                 new_node->children.emplace_back(new IndexNode{{}, {}});
1727                 new_node = new_node->children.back();
1728                 depth += 1;
1729         } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1730         new_node->entries.emplace_back(entry);
1731 }
1732
1733 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1734 {
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());
1740
1741         // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1742         // as children.
1743         auto* index_root = new IndexNode{{}, {}};
1744         for (const IndexEntry& entry : entries) {
1745                 insertIntoNode(entry, index_root);
1746         }
1747
1748         return index_root;
1749 }
1750
1751 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1752 {
1753         LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1754
1755         xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1756         xs << xml::CR();
1757         xs << termAtLevel(root_node, depth);
1758         // By tree assumption, all the entries at this node have the same set of terms.
1759
1760         if (!root_node->entries.empty()) {
1761                 xs << XMLStream::ESCAPE_NONE << " &#8212; "; // Em dash, i.e. long (---).
1762                 unsigned entry_number = 1;
1763
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");
1769                 };
1770
1771                 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1772                         const IndexEntry &entry = root_node->entries[i];
1773
1774                         switch (entry.inset()->params().range) {
1775                                 case InsetIndexParams::PageRange::None:
1776                                         writeLinkToEntry(entry, entry_number);
1777                                         break;
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:
1782                                         //     1--, 2, --3
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.
1787
1788                                         writeLinkToEntry(entry, entry_number);
1789                                         xs << XMLStream::ESCAPE_NONE << " &#8211; "; // En dash, i.e. semi-long (--).
1790
1791                                         if (nextEntryIsEnd) {
1792                                                 // Skip the next entry in the loop, write it right now, after the dash.
1793                                                 entry_number += 1;
1794                                                 i += 1;
1795                                                 writeLinkToEntry(root_node->entries[i], entry_number);
1796                                         }
1797                                 }
1798                                         break;
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 << " &#8211; "; // En dash, i.e. semi-long (--).
1802                                         writeLinkToEntry(root_node->entries[i], entry_number);
1803                         }
1804
1805                         if (i < root_node->entries.size() - 1) {
1806                                 xs << ", ";
1807                         }
1808                         entry_number += 1;
1809                 }
1810                 xs << xml::CR();
1811         }
1812
1813         if (!root_node->entries.empty() && !root_node->children.empty()) {
1814                 xs << xml::CR();
1815         }
1816
1817         if (!root_node->children.empty()) {
1818                 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1819                 xs << xml::CR();
1820
1821                 for (const IndexNode* child : root_node->children) {
1822                         outputIndexPage(xs, child, depth + 1);
1823                 }
1824
1825                 xs << xml::EndTag("ul");
1826                 xs << xml::CR();
1827         }
1828
1829         xs << xml::EndTag("li");
1830         xs << xml::CR();
1831 }
1832
1833 #ifdef LYX_INSET_INDEX_DEBUG
1834 void printTree(const IndexNode* root_node, unsigned depth = 0)
1835 {
1836         static const std::string pattern = "    ";
1837         std::string prefix;
1838         for (unsigned i = 0; i < depth; ++i) {
1839                 prefix += pattern;
1840         }
1841         const std::string prefix_long = prefix + pattern + pattern;
1842
1843         docstring term_at_level;
1844         if (depth == 0) {
1845                 // The root has no term.
1846                 std::cout << "<ROOT>" << std::endl;
1847         } else {
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()) << ")"
1851                           << std::endl;
1852         }
1853
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;
1857                 }
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)
1860                                 << std::endl;
1861                 }
1862         }
1863
1864         for (const IndexNode* node : root_node->children) {
1865                 printTree(node, depth + 1);
1866         }
1867 }
1868 #endif // LYX_INSET_INDEX_DEBUG
1869 }
1870
1871
1872 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1873 {
1874         BufferParams const & bp = buffer().masterBuffer()->params();
1875
1876         shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1877         if (toc->empty())
1878                 return docstring();
1879
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});
1888         }
1889
1890         // If all the index entries are in notes or not displayed, get out sooner.
1891         if (entries.empty())
1892                 return docstring();
1893
1894         const IndexNode* index_root = buildIndexTree(entries);
1895 #ifdef LYX_INSET_INDEX_DEBUG
1896         printTree(index_root);
1897 #endif
1898
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"));
1904
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;
1909         XMLStream xs(ods);
1910
1911         xs << xml::StartTag("div", tocattr);
1912         xs << xml::CR();
1913         xs << xml::StartTag(lay.htmltag(), lay.htmlGetAttrString());
1914         xs << translateIfPossible(indexName, getLocalOrDefaultLang(op)->lang());
1915         xs << xml::EndTag(lay.htmltag());
1916         xs << xml::CR();
1917         xs << xml::StartTag("ul", "class='main'");
1918         xs << xml::CR();
1919
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);
1923         }
1924         delete index_root;
1925
1926         xs << xml::EndTag("ul");
1927         xs << xml::CR();
1928         xs << xml::EndTag("div");
1929
1930         return ods.str();
1931 }
1932
1933 } // namespace lyx