]> git.lyx.org Git - features.git/blob - src/insets/InsetIndex.cpp
Fix page range without page format
[features.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 "DispatchResult.h"
22 #include "Encoding.h"
23 #include "ErrorList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "IndicesList.h"
27 #include "InsetList.h"
28 #include "Language.h"
29 #include "LaTeX.h"
30 #include "LaTeXFeatures.h"
31 #include "Lexer.h"
32 #include "LyX.h"
33 #include "output_latex.h"
34 #include "output_xhtml.h"
35 #include "xml.h"
36 #include "texstream.h"
37 #include "TextClass.h"
38 #include "TocBackend.h"
39
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/FileName.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
45 #include "support/Translator.h"
46
47 #include "frontends/alert.h"
48
49 #include <algorithm>
50 #include <set>
51 #include <iostream>
52
53 #include <QThreadStorage>
54
55 using namespace std;
56 using namespace lyx::support;
57
58 // Uncomment to enable InsetIndex-specific debugging mode: the tree for the index will be printed to std::cout.
59 // #define LYX_INSET_INDEX_DEBUG
60
61 namespace lyx {
62
63 namespace {
64
65 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
66 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
67
68 PageRangeTranslator const init_insetindexpagerangetranslator()
69 {
70         PageRangeTranslator translator("none", InsetIndexParams::None);
71         translator.addPair("start", InsetIndexParams::Start);
72         translator.addPair("end", InsetIndexParams::End);
73         return translator;
74 }
75
76 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
77 {
78         PageRangeTranslator translator("", InsetIndexParams::None);
79         translator.addPair("(", InsetIndexParams::Start);
80         translator.addPair(")", InsetIndexParams::End);
81         return translator;
82 }
83
84
85 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
86 {
87         PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
88         translator.addPair(_("Starts page range"), InsetIndexParams::Start);
89         translator.addPair(_("Ends page range"), InsetIndexParams::End);
90         return translator;
91 }
92
93
94 PageRangeTranslator const & insetindexpagerangetranslator()
95 {
96         static PageRangeTranslator const prtranslator =
97                         init_insetindexpagerangetranslator();
98         return prtranslator;
99 }
100
101
102 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
103 {
104         static PageRangeTranslatorLoc const translator =
105                         init_insetindexpagerangetranslator_loc();
106         return translator;
107 }
108
109
110 PageRangeTranslator const & insetindexpagerangetranslator_latex()
111 {
112         static PageRangeTranslator const lttranslator =
113                         init_insetindexpagerangetranslator_latex();
114         return lttranslator;
115 }
116
117 } // namespace anon
118
119 /////////////////////////////////////////////////////////////////////
120 //
121 // InsetIndex
122 //
123 ///////////////////////////////////////////////////////////////////////
124
125
126 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
127         : InsetCollapsible(buf), params_(params)
128 {}
129
130
131 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
132 {
133         OutputParams runparams(runparams_in);
134         runparams.inIndexEntry = true;
135
136         otexstringstream os;
137
138         if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
139                 && params_.index != "idx") {
140                 os << "\\sindex[";
141                 os << escape(params_.index);
142                 os << "]{";
143         } else {
144                 os << "\\index";
145                 os << '{';
146         }
147
148         // Get the LaTeX output from InsetText. We need to deconstruct this later
149         // in order to check if we need to generate a sorting key
150         odocstringstream ourlatex;
151         otexstream ots(ourlatex);
152         InsetText::latex(ots, runparams);
153         if (runparams.find_effective()) {
154                 // No need for special handling, if we are only searching for some patterns
155                 os << ourlatex.str() << "}";
156                 return;
157         }
158
159         if (hasSortKey()) {
160                 getSortkey(os, runparams);
161                 os << "@";
162                 os << ourlatex.str();
163                 getSubentries(os, runparams, ourlatex.str());
164                 if (hasSeeRef()) {
165                         os << "|";
166                         os << insetindexpagerangetranslator_latex().find(params_.range);
167                         getSeeRefs(os, runparams);
168                 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
169                         os << "|";
170                         os << insetindexpagerangetranslator_latex().find(params_.range);
171                         os << from_utf8(params_.pagefmt);
172                 } else if (params_.range != InsetIndexParams::PageRange::None) {
173                         os << "|";
174                         os << insetindexpagerangetranslator_latex().find(params_.range);
175                 }
176         } else {
177                 // We check whether we need a sort key.
178                 // If so, we use the plaintext version
179                 odocstringstream ourplain;
180                 InsetText::plaintext(ourplain, runparams);
181
182                 // These are the LaTeX and plaintext representations
183                 docstring latexstr = ourlatex.str();
184                 docstring plainstr = ourplain.str();
185         
186                 // This will get what follows | if anything does,
187                 // the command (e.g., see, textbf) for pagination
188                 // formatting
189                 docstring cmd;
190
191                 if (hasSeeRef()) {
192                         odocstringstream seeref;
193                         otexstream otsee(seeref);
194                         getSeeRefs(otsee, runparams);
195                         cmd = seeref.str();
196                 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
197                         cmd = from_utf8(params_.pagefmt);
198                 } else {
199                         // Check for the | separator to strip the cmd.
200                         // This goes wrong on an escaped "|", but as the escape
201                         // character can be changed in style files, we cannot
202                         // prevent that.
203                         size_t pos = latexstr.find(from_ascii("|"));
204                         if (pos != docstring::npos) {
205                                 // Put the bit after "|" into cmd...
206                                 cmd = latexstr.substr(pos + 1);
207                                 // ...and erase that stuff from latexstr
208                                 latexstr = latexstr.erase(pos);
209                                 // ...as well as from plainstr
210                                 size_t ppos = plainstr.find(from_ascii("|"));
211                                 if (ppos < plainstr.size())
212                                         plainstr.erase(ppos);
213                                 else
214                                         LYXERR0("The `|' separator was not found in the plaintext version!");
215                         }
216                 }
217
218                 odocstringstream subentries;
219                 otexstream otsub(subentries);
220                 getSubentries(otsub, runparams, ourlatex.str());
221                 if (subentries.str().empty()) {
222                         // Separate the entries and subentries, i.e., split on "!".
223                         // This goes wrong on an escaped "!", but as the escape
224                         // character can be changed in style files, we cannot
225                         // prevent that.
226                         std::vector<docstring> const levels =
227                                         getVectorFromString(latexstr, from_ascii("!"), true);
228                         std::vector<docstring> const levels_plain =
229                                         getVectorFromString(plainstr, from_ascii("!"), true);
230                 
231                         vector<docstring>::const_iterator it = levels.begin();
232                         vector<docstring>::const_iterator end = levels.end();
233                         vector<docstring>::const_iterator it2 = levels_plain.begin();
234                         bool first = true;
235                         for (; it != end; ++it) {
236                                 if ((*it).empty()) {
237                                         emptySubentriesWarning(ourlatex.str());
238                                         if (it2 < levels_plain.end())
239                                                 ++it2;
240                                         continue;
241                                 }
242                                 // The separator needs to be put back when
243                                 // writing the levels, except for the first level
244                                 if (!first)
245                                         os << '!';
246                                 else
247                                         first = false;
248                 
249                                 // Now here comes the reason for this whole procedure:
250                                 // We try to correctly sort macros and formatted strings.
251                                 // If we find a command, prepend a plain text
252                                 // version of the content to get sorting right,
253                                 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
254                                 // We do this on all levels.
255                                 // We don't do it if the level already contains a '@', though.
256                                 // Plaintext might return nothing (e.g. for ERTs).
257                                 // In that case, we use LaTeX.
258                                 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
259                                 processLatexSorting(os, runparams, *it, spart);
260                                 if (it2 < levels_plain.end())
261                                         ++it2;
262                         }
263                 } else {
264                         processLatexSorting(os, runparams, latexstr, plainstr);
265                         os << subentries.str();
266                 }
267
268                 // At last, re-insert the command, separated by "|"
269                 if (!cmd.empty()) {
270                         os << "|"
271                            << insetindexpagerangetranslator_latex().find(params_.range)
272                            << cmd;
273                 } else if (params_.range != InsetIndexParams::PageRange::None) {
274                         os << "|";
275                         os << insetindexpagerangetranslator_latex().find(params_.range);
276                 }
277         }
278         os << '}';
279
280         // In macros with moving arguments, such as \section,
281         // we store the index and output it after the macro (#2154)
282         if (runparams_in.postpone_fragile_stuff)
283                 runparams_in.post_macro += os.str();
284         else
285                 ios << os.release();
286 }
287
288
289 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
290                                 docstring const latex, docstring const spart) const
291 {
292         if (contains(latex, '\\') && !contains(latex, '@')) {
293                 // Now we need to validate that all characters in
294                 // the sorting part are representable in the current
295                 // encoding. If not try the LaTeX macro which might
296                 // or might not be a good choice, and issue a warning.
297                 pair<docstring, docstring> spart_latexed =
298                                 runparams.encoding->latexString(spart, runparams.dryrun);
299                 if (!spart_latexed.second.empty())
300                         LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
301                 if (spart != spart_latexed.first && !runparams.dryrun) {
302                         TeXErrors terr;
303                         ErrorList & errorList = buffer().errorList("Export");
304                         docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
305                                                       "problems with the entry '%1$s'.\n"
306                                                       "Please specify the sorting of this entry manually, as "
307                                                       "explained in the User Guide."), spart);
308                         Paragraph const & par = buffer().paragraphs().front();
309                         errorList.push_back(ErrorItem(_("Index sorting failed"), s,
310                                                       {par.id(), 0}, {par.id(), -1}));
311                         buffer().bufferErrors(terr, errorList);
312                 }
313                 // Remove remaining \'s from the sort key
314                 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
315                 // Plain quotes need to be escaped, however (#10649), as this
316                 // is the default escape character
317                 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
318
319                 // Now insert the sortkey, separated by '@'.
320                 os << ppart;
321                 os << '@';
322         }
323         // Insert the actual level text
324         os << latex;
325 }
326
327
328 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
329 {
330         // Two ways of processing this inset are implemented:
331         // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
332         // - the modern one, based on precise insets for indexing features
333         // Like the LaTeX implementation, consider the user chooses either of those options.
334
335         // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
336         // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
337         //  otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
338         odocstringstream odss;
339         otexstream ots(odss);
340         InsetText::latex(ots, runparams);
341         docstring latexString = trim(odss.str());
342
343         // Handle several indices (indicated in the inset instead of the raw latexString).
344         docstring indexType = from_utf8("");
345         if (buffer().masterBuffer()->params().use_indices) {
346                 indexType += " type=\"" + params_.index + "\"";
347         }
348
349         // Split the string into its main constituents: terms, and command (see, see also, range).
350         size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
351         docstring indexTerms = latexString.substr(0, positionVerticalBar);
352         docstring command;
353         if (positionVerticalBar != lyx::docstring::npos) {
354                 command = latexString.substr(positionVerticalBar + 1);
355         }
356
357         // Handle sorting issues, with @.
358         docstring sortAs;
359         if (hasSortKey()) {
360                 sortAs = getSortkeyAsText(runparams);
361                 // indexTerms may contain a sort key if the user has both the inset and the manual key.
362         } else {
363                 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
364                 if (sortingElements.size() == 2) {
365                         sortAs = sortingElements[0];
366                         indexTerms = sortingElements[1];
367                 }
368         }
369
370         // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
371         vector<docstring> terms;
372         if (const vector<docstring> potential_terms = getSubentriesAsText(runparams); !potential_terms.empty()) {
373                 terms = potential_terms;
374                 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
375                 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
376                 // full string within this inset (i.e. without the subinsets).
377                 terms.insert(terms.begin(), latexString);
378         } else {
379                 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
380         }
381
382         // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
383         const bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
384         const bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
385                         latexString.find(from_ascii("|(")) != lyx::docstring::npos;
386         const bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
387                         latexString.find(from_ascii("|)")) != lyx::docstring::npos;
388
389         if (hasInsetRange) {
390                 // Remove the ranges from the command if they do not appear at the beginning.
391                 size_t index = 0;
392                 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
393                         command.erase(index, 1);
394                 index = 0;
395                 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
396                         command.erase(index, 1);
397
398                 // Remove the ranges when they are the only vertical bar in the complete string.
399                 if (command[0] == '(' || command[0] == ')')
400                         command.erase(0, 1);
401         }
402
403         // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
404         // Both commands are mutually exclusive!
405         docstring see = getSeeAsText(runparams);
406         vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
407
408         if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
409                 // Unescape brackets.
410                 size_t index = 0;
411                 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
412                         command.erase(index, 1);
413                 index = 0;
414                 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
415                         command.erase(index, 1);
416
417                 // Retrieve the part between brackets, and remove the complete seealso.
418                 size_t positionOpeningBracket = command.find(from_ascii("{"));
419                 size_t positionClosingBracket = command.find(from_ascii("}"));
420                 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
421
422                 // Parse the list of referenced entries (or a single one for see).
423                 if (command.substr(0, 7) == "seealso") {
424                         seeAlsoes = getVectorFromString(list, from_ascii(","), false);
425                 } else {
426                         see = list;
427
428                         if (see.find(from_ascii(",")) != std::string::npos) {
429                                 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
430                                                                                         "Complete entry: \"") + latexString + from_utf8("\"");
431                                 LYXERR0(error);
432                                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
433                         }
434                 }
435
436                 // Remove the complete see/seealso from the commands, in case there is something else to parse.
437                 command = command.substr(positionClosingBracket + 1);
438         }
439
440         // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
441         // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
442         // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
443         // no range will be detected.
444         // TODO: Could handle formatting as significance="preferred"?
445         if (!command.empty()) {
446                 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
447                                           + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
448                 LYXERR0(error);
449                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
450         }
451
452         // Write all of this down.
453         if (terms.empty() && !hasEndRange) {
454                 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
455                 LYXERR0(error);
456                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
457         } else {
458                 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
459                 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
460                 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
461                 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
462                 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
463                 // xml::cleanID.
464                 // indexType can only be used for singular and startofrange types!
465                 docstring attrs;
466                 if (!hasStartRange && !hasEndRange) {
467                         attrs = indexType;
468                 } else {
469                         // Append an ID if uniqueness is not guaranteed across the document.
470                         static QThreadStorage<set<docstring>> tKnownTermLists;
471                         static QThreadStorage<int> tID;
472
473                         set<docstring> &knownTermLists = tKnownTermLists.localData();
474                         int &ID = tID.localData();
475
476                         if (!tID.hasLocalData()) {
477                                 tID.localData() = 0;
478                         }
479
480                         // Modify the index terms to add the unique ID if needed.
481                         docstring newIndexTerms = indexTerms;
482                         if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
483                                 newIndexTerms += from_ascii(string("-") + to_string(ID));
484
485                                 // Only increment for the end of range, so that the same number is used for the start of range.
486                                 if (hasEndRange) {
487                                         ID++;
488                                 }
489                         }
490
491                         // Term list not yet known: add it to the set AFTER the end of range. After
492                         if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
493                                 knownTermLists.insert(indexTerms);
494                         }
495
496                         // Generate the attributes.
497                         docstring id = xml::cleanID(newIndexTerms);
498                         if (hasStartRange) {
499                                 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
500                         } else {
501                                 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
502                         }
503                 }
504
505                 // Handle the index terms (including the specific index for this entry).
506                 if (hasEndRange) {
507                         xs << xml::CompTag("indexterm", attrs);
508                 } else {
509                         xs << xml::StartTag("indexterm", attrs);
510                         if (!terms.empty()) { // hasEndRange has no content.
511                                 docstring attr;
512                                 if (!sortAs.empty()) {
513                                         attr = from_utf8("sortas='") + sortAs + from_utf8("'");
514                                 }
515
516                                 xs << xml::StartTag("primary", attr);
517                                 xs << terms[0];
518                                 xs << xml::EndTag("primary");
519                         }
520                         if (terms.size() > 1) {
521                                 xs << xml::StartTag("secondary");
522                                 xs << terms[1];
523                                 xs << xml::EndTag("secondary");
524                         }
525                         if (terms.size() > 2) {
526                                 xs << xml::StartTag("tertiary");
527                                 xs << terms[2];
528                                 xs << xml::EndTag("tertiary");
529                         }
530
531                         // Handle see and see also.
532                         if (!see.empty()) {
533                                 xs << xml::StartTag("see");
534                                 xs << see;
535                                 xs << xml::EndTag("see");
536                         }
537
538                         if (!seeAlsoes.empty()) {
539                                 for (auto &entry : seeAlsoes) {
540                                         xs << xml::StartTag("seealso");
541                                         xs << entry;
542                                         xs << xml::EndTag("seealso");
543                                 }
544                         }
545
546                         // Close the entry.
547                         xs << xml::EndTag("indexterm");
548                 }
549         }
550 }
551
552
553 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
554 {
555         // we just print an anchor, taking the paragraph ID from
556         // our own interior paragraph, which doesn't get printed
557         std::string const magic = paragraphs().front().magicLabel();
558         std::string const attr = "id='" + magic + "'";
559         xs << xml::CompTag("a", attr);
560         return docstring();
561 }
562
563
564 bool InsetIndex::showInsetDialog(BufferView * bv) const
565 {
566         bv->showDialog("index", params2string(params_),
567                         const_cast<InsetIndex *>(this));
568         return true;
569 }
570
571
572 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
573 {
574         switch (cmd.action()) {
575
576         case LFUN_INSET_MODIFY: {
577                 if (cmd.getArg(0) == "changetype") {
578                         cur.recordUndoInset(this);
579                         params_.index = from_utf8(cmd.getArg(1));
580                         break;
581                 }
582                 InsetIndexParams params;
583                 InsetIndex::string2params(to_utf8(cmd.argument()), params);
584                 cur.recordUndoInset(this);
585                 params_.index = params.index;
586                 params_.range = params.range;
587                 params_.pagefmt = params.pagefmt;
588                 // what we really want here is a TOC update, but that means
589                 // a full buffer update
590                 cur.forceBufferUpdate();
591                 break;
592         }
593
594         case LFUN_INSET_DIALOG_UPDATE:
595                 cur.bv().updateDialog("index", params2string(params_));
596                 break;
597
598         case LFUN_PARAGRAPH_BREAK: {
599                 // Since this inset in single-par anyway, let's use
600                 // return to enter subentries
601                 FuncRequest fr(LFUN_INDEXMACRO_INSERT, "subentry");
602                 lyx::dispatch(fr);
603                 break;
604         }
605
606         default:
607                 InsetCollapsible::doDispatch(cur, cmd);
608                 break;
609         }
610 }
611
612
613 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
614                 FuncStatus & flag) const
615 {
616         switch (cmd.action()) {
617
618         case LFUN_INSET_MODIFY:
619                 if (cmd.getArg(0) == "changetype") {
620                         docstring const newtype = from_utf8(cmd.getArg(1));
621                         Buffer const & realbuffer = *buffer().masterBuffer();
622                         IndicesList const & indiceslist = realbuffer.params().indiceslist();
623                         Index const * index = indiceslist.findShortcut(newtype);
624                         flag.setEnabled(index != 0);
625                         flag.setOnOff(
626                                 from_utf8(cmd.getArg(1)) == params_.index);
627                         return true;
628                 }
629                 return InsetCollapsible::getStatus(cur, cmd, flag);
630
631         case LFUN_INSET_DIALOG_UPDATE: {
632                 Buffer const & realbuffer = *buffer().masterBuffer();
633                 flag.setEnabled(realbuffer.params().use_indices);
634                 return true;
635         }
636         
637         case LFUN_PARAGRAPH_BREAK:
638                 return macrosPossible("subentry");
639         
640         case LFUN_INDEXMACRO_INSERT:
641                 return macrosPossible(cmd.getArg(0));
642
643         default:
644                 return InsetCollapsible::getStatus(cur, cmd, flag);
645         }
646 }
647
648
649 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
650 {
651         Paragraph const & par = paragraphs().front();
652         InsetList::const_iterator it = par.insetList().begin();
653         for (; it != par.insetList().end(); ++it) {
654                 Inset & inset = *it->inset;
655                 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
656                         InsetIndexMacro const & iim =
657                                 static_cast<InsetIndexMacro const &>(inset);
658                         iim.getLatex(os, runparams);
659                         return;
660                 }
661         }
662 }
663
664
665 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
666 {
667         Paragraph const & par = paragraphs().front();
668         InsetList::const_iterator it = par.insetList().begin();
669         for (; it != par.insetList().end(); ++it) {
670                 Inset & inset = *it->inset;
671                 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
672                         otexstringstream os;
673                         InsetIndexMacro const & iim =
674                                 static_cast<InsetIndexMacro const &>(inset);
675                         iim.getLatex(os, runparams);
676                         return os.str();
677                 }
678         }
679         return from_ascii("");
680 }
681
682
683 void InsetIndex::emptySubentriesWarning(docstring const & mainentry) const
684 {
685         // Empty subentries crash makeindex. So warn and ignore this.
686         TeXErrors terr;
687         ErrorList & errorList = buffer().errorList("Export");
688         docstring const s = bformat(_("There is an empty index subentry in the entry '%1$s'.\n"
689                                       "It will be ignored in the output."), mainentry);
690         Paragraph const & par = buffer().paragraphs().front();
691         errorList.push_back(ErrorItem(_("Empty index subentry!"), s,
692                                       {par.id(), 0}, {par.id(), -1}));
693         buffer().bufferErrors(terr, errorList);
694 }
695
696
697 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams,
698                                docstring const & mainentry) const
699 {
700         Paragraph const & par = paragraphs().front();
701         InsetList::const_iterator it = par.insetList().begin();
702         int i = 0;
703         for (; it != par.insetList().end(); ++it) {
704                 Inset & inset = *it->inset;
705                 if (inset.lyxCode() == INDEXMACRO_CODE) {
706                         InsetIndexMacro const & iim =
707                                 static_cast<InsetIndexMacro const &>(inset);
708                         if (iim.params().type == InsetIndexMacroParams::Subentry) {
709                                 if (iim.hasNoContent()) {
710                                         emptySubentriesWarning(mainentry);
711                                         continue;
712                                 }
713                                 ++i;
714                                 if (i > 2)
715                                         return;
716                                 os << "!";
717                                 iim.getLatex(os, runparams);
718                         }
719                 }
720         }
721 }
722
723
724 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams,
725                                                        bool const asLabel) const
726 {
727         std::vector<docstring> subentries;
728
729         Paragraph const & par = paragraphs().front();
730         InsetList::const_iterator it = par.insetList().begin();
731         int i = 0;
732         for (; it != par.insetList().end(); ++it) {
733                 Inset & inset = *it->inset;
734                 if (inset.lyxCode() == INDEXMACRO_CODE) {
735                         InsetIndexMacro const & iim =
736                                 static_cast<InsetIndexMacro const &>(inset);
737                         if (iim.params().type == InsetIndexMacroParams::Subentry) {
738                                 ++i;
739                                 if (i > 2)
740                                         break;
741                                 if (asLabel) {
742                                         docstring const l;
743                                         docstring const sl = iim.getNewLabel(l);
744                                         subentries.emplace_back(sl);
745                                 } else {
746                                         otexstringstream os;
747                                         iim.getLatex(os, runparams);
748                                         subentries.emplace_back(os.str());
749                                 }
750                         }
751                 }
752         }
753
754         return subentries;
755 }
756
757
758 docstring InsetIndex::getMainSubentryAsText(OutputParams const & runparams) const
759 {
760         otexstringstream os;
761         InsetText::latex(os, runparams);
762         return os.str();
763 }
764
765
766 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
767 {
768         Paragraph const & par = paragraphs().front();
769         InsetList::const_iterator it = par.insetList().begin();
770         for (; it != par.insetList().end(); ++it) {
771                 Inset & inset = *it->inset;
772                 if (inset.lyxCode() == INDEXMACRO_CODE) {
773                         InsetIndexMacro const & iim =
774                                 static_cast<InsetIndexMacro const &>(inset);
775                         if (iim.params().type == InsetIndexMacroParams::See
776                             || iim.params().type == InsetIndexMacroParams::Seealso) {
777                                 iim.getLatex(os, runparams);
778                                 return;
779                         }
780                 }
781         }
782 }
783
784
785 docstring InsetIndex::getSeeAsText(OutputParams const & runparams,
786                                    bool const asLabel) const
787 {
788         Paragraph const & par = paragraphs().front();
789         InsetList::const_iterator it = par.insetList().begin();
790         for (; it != par.insetList().end(); ++it) {
791                 Inset & inset = *it->inset;
792                 if (inset.lyxCode() == INDEXMACRO_CODE) {
793                         InsetIndexMacro const & iim =
794                                 static_cast<InsetIndexMacro const &>(inset);
795                         if (iim.params().type == InsetIndexMacroParams::See) {
796                                 if (asLabel) {
797                                         docstring const l;
798                                         return iim.getNewLabel(l);
799                                 } else {
800                                         otexstringstream os;
801                                         iim.getLatex(os, runparams);
802                                         return os.str();
803                                 }
804                         }
805                 }
806         }
807         return from_ascii("");
808 }
809
810
811 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams,
812                                                       bool const asLabel) const
813 {
814         std::vector<docstring> seeAlsoes;
815
816         Paragraph const & par = paragraphs().front();
817         InsetList::const_iterator it = par.insetList().begin();
818         for (; it != par.insetList().end(); ++it) {
819                 Inset & inset = *it->inset;
820                 if (inset.lyxCode() == INDEXMACRO_CODE) {
821                         InsetIndexMacro const & iim =
822                                 static_cast<InsetIndexMacro const &>(inset);
823                         if (iim.params().type == InsetIndexMacroParams::Seealso) {
824                                 if (asLabel) {
825                                         docstring const l;
826                                         seeAlsoes.emplace_back(iim.getNewLabel(l));
827                                 } else {
828                                         otexstringstream os;
829                                         iim.getLatex(os, runparams);
830                                         seeAlsoes.emplace_back(os.str());
831                                 }
832                         }
833                 }
834         }
835
836         return seeAlsoes;
837 }
838
839
840 namespace {
841
842 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode code,
843                                           const std::set<InsetIndexMacroParams::Type> types = {})
844 {
845         Paragraph const & par = inset_index->paragraphs().front();
846         InsetList::const_iterator it = par.insetList().begin();
847         for (; it != par.insetList().end(); ++it) {
848                 Inset & inset = *it->inset;
849                 if (inset.lyxCode() == code) {
850                         if (types.empty())
851                                 return true;
852
853                         LASSERT(code == INDEXMACRO_CODE, return false);
854                         InsetIndexMacro const & iim =
855                                         static_cast<InsetIndexMacro const &>(inset);
856                         if (types.find(iim.params().type) != types.end())
857                                 return true;
858                 }
859         }
860         return false;
861 }
862
863 } // namespace
864
865
866 bool InsetIndex::hasSubentries() const
867 {
868         return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::Subentry});
869 }
870
871
872 bool InsetIndex::hasSeeRef() const
873 {
874         return hasInsetWithCode(this, INDEXMACRO_CODE, {InsetIndexMacroParams::See, InsetIndexMacroParams::Seealso});
875 }
876
877
878 bool InsetIndex::hasSortKey() const
879 {
880         return hasInsetWithCode(this, INDEXMACRO_SORTKEY_CODE);
881 }
882
883
884 bool InsetIndex::macrosPossible(string const type) const
885 {
886         if (type != "see" && type != "seealso"
887             && type != "sortkey" && type != "subentry")
888                 return false;
889
890         Paragraph const & par = paragraphs().front();
891         InsetList::const_iterator it = par.insetList().begin();
892         int subidxs = 0;
893         for (; it != par.insetList().end(); ++it) {
894                 Inset & inset = *it->inset;
895                 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
896                         return false;
897                 if (inset.lyxCode() == INDEXMACRO_CODE) {
898                         InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
899                         if ((type == "see" || type == "seealso")
900                              && (iim.params().type == InsetIndexMacroParams::See
901                                  || iim.params().type == InsetIndexMacroParams::Seealso))
902                                 return false;
903                         if (type == "subentry"
904                              && iim.params().type == InsetIndexMacroParams::Subentry) {
905                                 ++subidxs;
906                                 if (subidxs > 1)
907                                         return false;
908                         }
909                 }
910         }
911         return true;
912 }
913
914
915 ColorCode InsetIndex::labelColor() const
916 {
917         if (params_.index.empty() || params_.index == from_ascii("idx"))
918                 return InsetCollapsible::labelColor();
919         // FIXME UNICODE
920         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
921                                             + "@" + buffer().fileName().absFileName());
922         if (c == Color_none)
923                 c = InsetCollapsible::labelColor();
924         return c;
925 }
926
927
928 docstring InsetIndex::toolTip(BufferView const &, int, int) const
929 {
930         docstring tip = _("Index Entry");
931         if (buffer().params().use_indices && !params_.index.empty()) {
932                 Buffer const & realbuffer = *buffer().masterBuffer();
933                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
934                 tip += " (";
935                 Index const * index = indiceslist.findShortcut(params_.index);
936                 if (!index)
937                         tip += _("unknown type!");
938                 else
939                         tip += index->index();
940                 tip += ")";
941         }
942         tip += ": ";
943         docstring res = toolTipText(tip);
944         if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
945                 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
946         if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
947                 res += "\n" + _("Pagination format:") + " ";
948                 if (params_.pagefmt == "textbf")
949                         res += _("bold");
950                 else if (params_.pagefmt == "textit")
951                         res += _("italic");
952                 else if (params_.pagefmt == "emph")
953                         res += _("emphasized");
954                 else
955                         res += from_utf8(params_.pagefmt);
956         }
957         return res;
958 }
959
960
961 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
962 {
963         InsetLayout const & il = getLayout();
964         docstring label = translateIfPossible(il.labelstring());
965
966         if (buffer().params().use_indices && !params_.index.empty()) {
967                 Buffer const & realbuffer = *buffer().masterBuffer();
968                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
969                 label += " (";
970                 Index const * index = indiceslist.findShortcut(params_.index);
971                 if (!index)
972                         label += _("unknown type!");
973                 else
974                         label += index->index();
975                 label += ")";
976         }
977
978         docstring res;
979         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
980                 res = label;
981         else {
982                 res = getNewLabel(label);
983                 OutputParams const rp(0);
984                 vector<docstring> sublbls = getSubentriesAsText(rp, true);
985                 for (auto const & sublbl : sublbls) {
986                         res += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
987                         res += " " + sublbl;
988                 }
989                 docstring see = getSeeAsText(rp, true);
990                 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
991                         see = getSeeAlsoesAsText(rp, true).front();
992                 if (!see.empty()) {
993                         res += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
994                         res += " " + see;
995                 }
996         }
997         if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
998                 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
999         return res;
1000 }
1001
1002
1003 void InsetIndex::write(ostream & os) const
1004 {
1005         os << to_utf8(layoutName());
1006         params_.write(os);
1007         InsetCollapsible::write(os);
1008 }
1009
1010
1011 void InsetIndex::read(Lexer & lex)
1012 {
1013         params_.read(lex);
1014         InsetCollapsible::read(lex);
1015 }
1016
1017
1018 string InsetIndex::params2string(InsetIndexParams const & params)
1019 {
1020         ostringstream data;
1021         data << "index";
1022         params.write(data);
1023         return data.str();
1024 }
1025
1026
1027 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
1028 {
1029         params = InsetIndexParams();
1030         if (in.empty())
1031                 return;
1032
1033         istringstream data(in);
1034         Lexer lex;
1035         lex.setStream(data);
1036         lex.setContext("InsetIndex::string2params");
1037         lex >> "index";
1038         params.read(lex);
1039 }
1040
1041
1042 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
1043                                                   UpdateType utype, TocBackend & backend) const
1044 {
1045         DocIterator pit = cpit;
1046         pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
1047         docstring str;
1048         InsetLayout const & il = getLayout();
1049         docstring label = translateIfPossible(il.labelstring());
1050         if (!il.contentaslabel())
1051                 str = label;
1052         else {
1053                 str = getNewLabel(label);
1054                 OutputParams const rp(0);
1055                 vector<docstring> sublbls = getSubentriesAsText(rp, true);
1056                 for (auto const & sublbl : sublbls) {
1057                         str += " " + docstring(1, char_type(0x2023));// TRIANGULAR BULLET
1058                         str += " " + sublbl;
1059                 }
1060                 docstring see = getSeeAsText(rp, true);
1061                 if (see.empty() && !getSeeAlsoesAsText(rp, true).empty())
1062                         see = getSeeAlsoesAsText(rp, true).front();
1063                 if (!see.empty()) {
1064                         str += " " + docstring(1, char_type(0x261e));// WHITE RIGHT POINTING INDEX
1065                         str += " " + see;
1066                 }
1067         }
1068         string type = "index";
1069         if (buffer().masterBuffer()->params().use_indices)
1070                 type += ":" + to_utf8(params_.index);
1071         TocBuilder & b = backend.builder(type);
1072         b.pushItem(pit, str, output_active);
1073         // Proceed with the rest of the inset.
1074         InsetCollapsible::addToToc(cpit, output_active, utype, backend);
1075         b.pop();
1076 }
1077
1078
1079 void InsetIndex::validate(LaTeXFeatures & features) const
1080 {
1081         if (buffer().masterBuffer()->params().use_indices
1082             && !params_.index.empty()
1083             && params_.index != "idx")
1084                 features.require("splitidx");
1085         InsetCollapsible::validate(features);
1086 }
1087
1088
1089 string InsetIndex::contextMenuName() const
1090 {
1091         return "context-index";
1092 }
1093
1094
1095 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1096 {
1097         // We override the implementation of InsetCollapsible,
1098         // because we have eytra entries.
1099         string owncm = "context-edit-index;";
1100         return owncm + InsetCollapsible::contextMenu(bv, x, y);
1101 }
1102
1103
1104 bool InsetIndex::hasSettings() const
1105 {
1106         return true;
1107 }
1108
1109
1110 bool InsetIndex::insetAllowed(InsetCode code) const
1111 {
1112         switch (code) {
1113         case INDEXMACRO_CODE:
1114         case INDEXMACRO_SORTKEY_CODE:
1115                 return true;
1116         case INDEX_CODE:
1117                 return false;
1118         default:
1119                 return InsetCollapsible::insetAllowed(code);
1120         }
1121 }
1122
1123
1124 /////////////////////////////////////////////////////////////////////
1125 //
1126 // InsetIndexParams
1127 //
1128 ///////////////////////////////////////////////////////////////////////
1129
1130
1131 void InsetIndexParams::write(ostream & os) const
1132 {
1133         os << ' ';
1134         if (!index.empty())
1135                 os << to_utf8(index);
1136         else
1137                 os << "idx";
1138         os << '\n';
1139         os << "range "
1140            << insetindexpagerangetranslator().find(range)
1141            << '\n';
1142         os << "pageformat "
1143            << pagefmt
1144            << '\n';
1145 }
1146
1147
1148 void InsetIndexParams::read(Lexer & lex)
1149 {
1150         if (lex.eatLine())
1151                 index = lex.getDocString();
1152         else
1153                 index = from_ascii("idx");
1154         if (lex.checkFor("range")) {
1155                 string st = lex.getString();
1156                 if (lex.eatLine()) {
1157                         st = lex.getString();
1158                         range = insetindexpagerangetranslator().find(lex.getString());
1159                 }
1160         }
1161         if (lex.checkFor("pageformat") && lex.eatLine()) {
1162                 pagefmt = lex.getString();
1163         }
1164 }
1165
1166
1167 /////////////////////////////////////////////////////////////////////
1168 //
1169 // InsetPrintIndex
1170 //
1171 ///////////////////////////////////////////////////////////////////////
1172
1173 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1174         : InsetCommand(buf, p)
1175 {}
1176
1177
1178 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1179 {
1180         static ParamInfo param_info_;
1181         if (param_info_.empty()) {
1182                 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1183                                 ParamInfo::HANDLING_ESCAPE);
1184                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1185                                 ParamInfo::HANDLING_LATEXIFY);
1186                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1187         }
1188         return param_info_;
1189 }
1190
1191
1192 docstring InsetPrintIndex::screenLabel() const
1193 {
1194         bool const printall = suffixIs(getCmdName(), '*');
1195         bool const multind = buffer().masterBuffer()->params().use_indices;
1196         if ((!multind
1197              && getParam("type") == from_ascii("idx"))
1198             || (getParam("type").empty() && !printall))
1199                 return _("Index");
1200         Buffer const & realbuffer = *buffer().masterBuffer();
1201         IndicesList const & indiceslist = realbuffer.params().indiceslist();
1202         Index const * index = indiceslist.findShortcut(getParam("type"));
1203         if (!index && !printall)
1204                 return _("Unknown index type!");
1205         docstring res = printall ? _("All indexes") : index->index();
1206         if (!multind)
1207                 res += " (" + _("non-active") + ")";
1208         else if (contains(getCmdName(), "printsubindex"))
1209                 res += " (" + _("subindex") + ")";
1210         return res;
1211 }
1212
1213
1214 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1215 {
1216         return s == "printindex" || s == "printsubindex"
1217                 || s == "printindex*" || s == "printsubindex*";
1218 }
1219
1220
1221 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1222 {
1223         switch (cmd.action()) {
1224
1225         case LFUN_INSET_MODIFY: {
1226                 if (cmd.argument() == from_ascii("toggle-subindex")) {
1227                         string scmd = getCmdName();
1228                         if (contains(scmd, "printindex"))
1229                                 scmd = subst(scmd, "printindex", "printsubindex");
1230                         else
1231                                 scmd = subst(scmd, "printsubindex", "printindex");
1232                         cur.recordUndo();
1233                         setCmdName(scmd);
1234                         break;
1235                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1236                         string scmd = getCmdName();
1237                         if (suffixIs(scmd, '*'))
1238                                 break;
1239                         scmd += '*';
1240                         cur.recordUndo();
1241                         setParam("type", docstring());
1242                         setCmdName(scmd);
1243                         break;
1244                 }
1245                 InsetCommandParams p(INDEX_PRINT_CODE);
1246                 // FIXME UNICODE
1247                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1248                 if (p.getCmdName().empty()) {
1249                         cur.noScreenUpdate();
1250                         break;
1251                 }
1252                 cur.recordUndo();
1253                 setParams(p);
1254                 break;
1255         }
1256
1257         default:
1258                 InsetCommand::doDispatch(cur, cmd);
1259                 break;
1260         }
1261 }
1262
1263
1264 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1265         FuncStatus & status) const
1266 {
1267         switch (cmd.action()) {
1268
1269         case LFUN_INSET_MODIFY: {
1270                 if (cmd.argument() == from_ascii("toggle-subindex")) {
1271                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
1272                         status.setOnOff(contains(getCmdName(), "printsubindex"));
1273                         return true;
1274                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1275                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
1276                         status.setOnOff(suffixIs(getCmdName(), '*'));
1277                         return true;
1278                 } if (cmd.getArg(0) == "index_print"
1279                     && cmd.getArg(1) == "CommandInset") {
1280                         InsetCommandParams p(INDEX_PRINT_CODE);
1281                         InsetCommand::string2params(to_utf8(cmd.argument()), p);
1282                         if (suffixIs(p.getCmdName(), '*')) {
1283                                 status.setEnabled(true);
1284                                 status.setOnOff(false);
1285                                 return true;
1286                         }
1287                         Buffer const & realbuffer = *buffer().masterBuffer();
1288                         IndicesList const & indiceslist =
1289                                 realbuffer.params().indiceslist();
1290                         Index const * index = indiceslist.findShortcut(p["type"]);
1291                         status.setEnabled(index != 0);
1292                         status.setOnOff(p["type"] == getParam("type"));
1293                         return true;
1294                 } else
1295                         return InsetCommand::getStatus(cur, cmd, status);
1296         }
1297
1298         case LFUN_INSET_DIALOG_UPDATE: {
1299                 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1300                 return true;
1301         }
1302
1303         default:
1304                 return InsetCommand::getStatus(cur, cmd, status);
1305         }
1306 }
1307
1308
1309 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1310 {
1311         Index const * index =
1312                 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1313         if (index)
1314                 setParam("name", index->index());
1315 }
1316
1317
1318 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1319 {
1320         if (!buffer().masterBuffer()->params().use_indices) {
1321                 if (getParam("type") == from_ascii("idx"))
1322                         os << "\\printindex" << termcmd;
1323                 return;
1324         }
1325         OutputParams runparams = runparams_in;
1326         os << getCommand(runparams);
1327 }
1328
1329
1330 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1331 {
1332         features.require("makeidx");
1333         if (buffer().masterBuffer()->params().use_indices)
1334                 features.require("splitidx");
1335         InsetCommand::validate(features);
1336 }
1337
1338
1339 string InsetPrintIndex::contextMenuName() const
1340 {
1341         return buffer().masterBuffer()->params().use_indices ?
1342                 "context-indexprint" : string();
1343 }
1344
1345
1346 bool InsetPrintIndex::hasSettings() const
1347 {
1348         return buffer().masterBuffer()->params().use_indices;
1349 }
1350
1351
1352 class IndexEntry
1353 {
1354 public:
1355         /// Builds an entry for the index.
1356         IndexEntry(const InsetIndex * inset, OutputParams const * runparams) : inset_(inset), runparams_(runparams)
1357         {
1358                 LASSERT(runparams, return);
1359
1360                 // Convert the inset as text. The resulting text usually only contains an XHTML anchor (<a id='...'/>) and text.
1361                 odocstringstream entry;
1362                 OutputParams ours = *runparams;
1363                 ours.for_toc = false;
1364                 inset_->plaintext(entry, ours);
1365                 entry_ = entry.str();
1366
1367                 // Determine in which index this entry belongs to.
1368                 if (inset_->buffer().masterBuffer()->params().use_indices) {
1369                         index_ = inset_->params_.index;
1370                 }
1371
1372                 // Attempt parsing the inset.
1373                 if (isModern())
1374                         parseAsModern();
1375                 else
1376                         parseAsLegacy();
1377         }
1378
1379         /// When parsing this entry, some errors may be found; they are reported as a single string.
1380         // It is up to the caller to send this string to LYXERR and the output file, as needed.
1381         const docstring & output_error() const
1382         {
1383                 return output_error_;
1384         }
1385
1386         void output_error(XMLStream xs) const
1387         {
1388                 LYXERR0(output_error());
1389                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + output_error() + from_utf8(" -->\n"));
1390         }
1391
1392
1393 private:
1394         bool isModern()
1395         {
1396                 std::cout << to_utf8(entry_) << std::endl;
1397
1398                 // If a modern parameter is present, this is definitely a modern index inset. Similarly, if it contains the
1399                 // usual LaTeX symbols (!|@), then it is definitely a legacy index inset. Otherwise, if it has features of
1400                 // neither, it is both: consider this is a modern inset, to trigger the least complex code. Mixing both types
1401                 // is not allowed (i.e. behaviour is undefined).
1402                 const bool is_definitely_modern = inset_->hasSortKey() || inset_->hasSeeRef() || inset_->hasSubentries()
1403                                             || inset_->params_.range != InsetIndexParams::PageRange::None;
1404                 const bool is_definitely_legacy = entry_.find('@') != std::string::npos
1405                                 || entry_.find('|') != std::string::npos || entry_.find('!') != std::string::npos;
1406
1407                 if (is_definitely_legacy && is_definitely_modern)
1408                         output_error_ += from_utf8("Mix of index properties and raw LaTeX index commands is unsupported. ");
1409
1410                 // Truth table:
1411                 // - is_definitely_modern == true:
1412                 //   - is_definitely_legacy == true: error (return whatever)
1413                 //   - is_definitely_legacy == false: return modern
1414                 // - is_definitely_modern == false:
1415                 //   - is_definitely_legacy == true: return legacy
1416                 //   - is_definitely_legacy == false: return modern
1417                 return !is_definitely_legacy;
1418         }
1419
1420         void parseAsModern()
1421         {
1422                 LASSERT(runparams_, return);
1423
1424                 if (inset_->hasSortKey()) {
1425                         sort_as_ = inset_->getSortkeyAsText(*runparams_);
1426                 }
1427
1428                 terms_ = inset_->getSubentriesAsText(*runparams_);
1429                 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
1430                 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
1431                 // full string within this inset (i.e. without the subinsets).
1432                 terms_.insert(terms_.begin(), inset_->getMainSubentryAsText(*runparams_));
1433
1434                 has_start_range_ = inset_->params_.range == InsetIndexParams::PageRange::Start;
1435                 has_end_range_ = inset_->params_.range == InsetIndexParams::PageRange::End;
1436
1437                 see_ = inset_->getSeeAsText(*runparams_);
1438                 see_alsoes_ = inset_->getSeeAlsoesAsText(*runparams_);
1439         }
1440
1441         void parseAsLegacy() {
1442                 // Determine if some features are known not to be supported. For now, this is only formatting like
1443                 // \index{alpha@\textbf{alpha}} or \index{alpha@$\alpha$}.
1444                 // @ is supported, but only for sorting, without specific formatting.
1445                 if (entry_.find(from_utf8("@\\")) != lyx::docstring::npos) {
1446                         output_error_ += from_utf8("Unsupported feature: an index entry contains an @\\. "
1447                                                    "Complete entry: \"") + entry_ + from_utf8("\". ");
1448                 }
1449                 if (entry_.find(from_utf8("@$")) != lyx::docstring::npos) {
1450                         output_error_ += from_utf8("Unsupported feature: an index entry contains an @$. "
1451                                                    "Complete entry: \"") + entry_ + from_utf8("\". ");
1452                 }
1453
1454                 // Split the string into its main constituents: terms, and command (see, see also, range).
1455                 size_t positionVerticalBar = entry_.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
1456                 docstring indexTerms = entry_.substr(0, positionVerticalBar);
1457                 docstring command;
1458                 if (positionVerticalBar != lyx::docstring::npos) {
1459                         command = entry_.substr(positionVerticalBar + 1);
1460                 }
1461
1462                 // Handle sorting issues, with @.
1463                 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
1464                 if (sortingElements.size() == 2) {
1465                         sort_as_ = sortingElements[0];
1466                         indexTerms = sortingElements[1];
1467                 }
1468
1469                 // Handle entries, subentries, and subsubentries.
1470                 terms_ = getVectorFromString(indexTerms, from_ascii("!"), false);
1471
1472                 // Handle ranges. Happily, (| and |) can only be at the end of the string!
1473                 has_start_range_ = entry_.find(from_ascii("|(")) != lyx::docstring::npos;
1474                 has_end_range_ = entry_.find(from_ascii("|)")) != lyx::docstring::npos;
1475
1476                 // - Remove the ranges from the command if they do not appear at the beginning.
1477                 size_t range_index = 0;
1478                 while ((range_index = command.find(from_utf8("|("), range_index)) != std::string::npos)
1479                         command.erase(range_index, 1);
1480                 range_index = 0;
1481                 while ((range_index = command.find(from_utf8("|)"), range_index)) != std::string::npos)
1482                         command.erase(range_index, 1);
1483
1484                 // - Remove the ranges when they are the only vertical bar in the complete string.
1485                 if (command[0] == '(' || command[0] == ')')
1486                         command.erase(0, 1);
1487
1488                 // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
1489                 // Both commands are mutually exclusive!
1490                 if (command.substr(0, 3) == "see") {
1491                         // Unescape brackets.
1492                         size_t index_argument_begin = 0;
1493                         while ((index_argument_begin = command.find(from_utf8("\\{"), index_argument_begin)) != std::string::npos)
1494                                 command.erase(index_argument_begin, 1);
1495                         size_t index_argument_end = 0;
1496                         while ((index_argument_end = command.find(from_utf8("\\}"), index_argument_end)) != std::string::npos)
1497                                 command.erase(index_argument_end, 1);
1498
1499                         // Retrieve the part between brackets, and remove the complete seealso.
1500                         size_t position_opening_bracket = command.find(from_ascii("{"));
1501                         size_t position_closing_bracket = command.find(from_ascii("}"));
1502                         docstring argument = command.substr(position_opening_bracket + 1,
1503                                                                                                 position_closing_bracket - position_opening_bracket - 1);
1504
1505                         // Parse the argument of referenced entries (or a single one for see).
1506                         if (command.substr(0, 7) == "seealso") {
1507                                 see_alsoes_ = getVectorFromString(argument, from_ascii(","), false);
1508                         } else {
1509                                 see_ = argument;
1510
1511                                 if (see_.find(from_ascii(",")) != std::string::npos) {
1512                                         output_error_ += from_utf8("Several index_argument_end terms found as \"see\"! Only one is "
1513                                                                    "acceptable. Complete entry: \"") + entry_ + from_utf8("\". ");
1514                                 }
1515                         }
1516
1517                         // Remove the complete see/seealso from the commands, in case there is something else to parse.
1518                         command = command.substr(position_closing_bracket + 1);
1519                 }
1520
1521                 // Some parts of the strings are not parsed, as they do not have anything matching in DocBook or XHTML:
1522                 // things like formatting the entry or the page number, other strings for sorting.
1523                 // https://wiki.lyx.org/Tips/Indexing
1524                 // If there are such things in the index entry, then this code may miserably fail. For example, for
1525                 // "Peter|(textbf", no range will be detected.
1526                 if (!command.empty()) {
1527                         output_error_ += from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
1528                                          + command + from_utf8(". Complete entry: \"") + entry_ + from_utf8("\". ");
1529                 }
1530         }
1531
1532 public:
1533         int level() const {
1534                 return terms_.size();
1535         }
1536
1537         const std::vector<docstring>& terms() const {
1538                 return terms_;
1539         }
1540
1541         std::vector<docstring>& terms() {
1542                 return terms_;
1543         }
1544
1545         const InsetIndex* inset() const {
1546                 return inset_;
1547         }
1548
1549 private:
1550         // Input inset. These should only be used when parsing the inset (either parseAsModern or parseAsLegacy, called in
1551         // the constructor).
1552         const InsetIndex * inset_;
1553         OutputParams const * runparams_;
1554         docstring entry_;
1555         docstring index_; // Useful when there are multiple indices in the same document.
1556
1557         // Errors, concatenated as a single string, available as soon as parsing is done, const afterwards (i.e. once
1558         // constructor is done).
1559         docstring output_error_;
1560
1561         // Parsed index entry.
1562         std::vector<docstring> terms_; // Up to three entries, in general.
1563         docstring sort_as_;
1564         docstring command_;
1565         bool has_start_range_;
1566         bool has_end_range_;
1567         docstring see_;
1568         vector<docstring> see_alsoes_;
1569
1570         // Operators used for sorting entries (alphabetical order).
1571         friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1572 };
1573
1574 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1575 {
1576         if (lhs.terms_.empty())
1577                 return false;
1578
1579         for (unsigned i = 0; i < min(rhs.terms_.size(), lhs.terms_.size()); ++i) {
1580                 int comp = compare_no_case(lhs.terms_[i], rhs.terms_[i]);
1581                 if (comp != 0)
1582                         return comp < 0;
1583         }
1584         return false;
1585 }
1586
1587
1588 namespace {
1589 std::string generateCssClassAtDepth(unsigned depth) {
1590         std::string css_class = "entry";
1591
1592         while (depth > 0) {
1593                 depth -= 1;
1594                 css_class.insert(0, "sub");
1595         }
1596
1597         return css_class;
1598 }
1599
1600 struct IndexNode {
1601         std::vector<IndexEntry> entries;
1602         std::vector<IndexNode*> children;
1603 };
1604
1605 docstring termAtLevel(const IndexNode* node, unsigned depth)
1606 {
1607         // The typical entry has a depth of 1 to 3: the call stack would then be at most 4 (due to the root node). This
1608         // function could be made constant time by copying the term in each node, but that would make data duplication that
1609         // may fall out of sync; the performance benefit would probably be negligible.
1610         if (!node->entries.empty()) {
1611                 LASSERT(node->entries.begin()->terms().size() >= depth + 1, return from_ascii(""));
1612                 return node->entries.begin()->terms()[depth];
1613         }
1614
1615         if (!node->children.empty()) {
1616                 return termAtLevel(*node->children.begin(), depth);
1617         }
1618
1619         LASSERT(false, return from_ascii(""));
1620 }
1621
1622 void insertIntoNode(const IndexEntry& entry, IndexNode* node, unsigned depth = 0)
1623 {
1624         // depth == 0 is for the root, not yet the index, hence the increase when going to vector size.
1625         for (IndexNode* child : node->children) {
1626                 if (entry.terms()[depth] == termAtLevel(child, depth)) {
1627                         if (depth + 1 == entry.terms().size()) { // == child.entries.begin()->terms().size()
1628                                 // All term entries match: it's an entry.
1629                                 child->entries.emplace_back(entry);
1630                                 return;
1631                         } else {
1632                                 insertIntoNode(entry, child, depth + 1);
1633                                 return;
1634                         }
1635                 }
1636         }
1637
1638         // Out of the loop: no matching child found, create a new (possibly nested) child for this entry. Due to the
1639         // possibility of nestedness, only insert the current entry when the right level is reached. This is needed if the
1640         // first entry for a word has several levels that never appeared.
1641         // In particular, this case is called for the first entry.
1642         IndexNode* new_node = node;
1643         do {
1644                 new_node->children.emplace_back(new IndexNode{{}, {}});
1645                 new_node = new_node->children.back();
1646                 depth += 1;
1647         } while (depth + 1 <= entry.terms().size()); // depth == 0: root node, no text associated.
1648         new_node->entries.emplace_back(entry);
1649 }
1650
1651 IndexNode* buildIndexTree(vector<IndexEntry>& entries)
1652 {
1653         // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1654         // thanks to the implementation of operator<.
1655         // If this operation is not performed, the algorithm below is no more correct (and ensuring that it works with
1656         // unsorted entries would make its complexity blow up).
1657         stable_sort(entries.begin(), entries.end());
1658
1659         // Cook the index into a nice tree data structure: entries at a given level in the index as a node, with subentries
1660         // as children.
1661         auto* index_root = new IndexNode{{}, {}};
1662         for (const IndexEntry& entry : entries) {
1663                 insertIntoNode(entry, index_root);
1664         }
1665
1666         return index_root;
1667 }
1668
1669 void outputIndexPage(XMLStream & xs, const IndexNode* root_node, unsigned depth = 0) // NOLINT(misc-no-recursion)
1670 {
1671         LASSERT(root_node->entries.size() + root_node->children.size() > 0, return);
1672
1673         xs << xml::StartTag("li", "class='" + generateCssClassAtDepth(depth) + "'");
1674         xs << xml::CR();
1675         xs << XMLStream::ESCAPE_NONE << termAtLevel(root_node, depth);
1676         // By tree assumption, all the entries at this node have the same set of terms.
1677
1678         if (!root_node->entries.empty()) {
1679                 xs << XMLStream::ESCAPE_NONE << " &#8212; "; // Em dash, i.e. long (---).
1680                 unsigned entry_number = 1;
1681
1682                 auto writeLinkToEntry = [&xs](const IndexEntry &entry, unsigned entry_number) {
1683                         std::string const link_attr = "href='#" + entry.inset()->paragraphs()[0].magicLabel() + "'";
1684                         xs << xml::StartTag("a", link_attr);
1685                         xs << from_ascii(std::to_string(entry_number));
1686                         xs << xml::EndTag("a");
1687                 };
1688
1689                 for (unsigned i = 0; i < root_node->entries.size(); ++i) {
1690                         const IndexEntry &entry = root_node->entries[i];
1691
1692                         switch (entry.inset()->params().range) {
1693                                 case InsetIndexParams::PageRange::None:
1694                                         writeLinkToEntry(entry, entry_number);
1695                                         break;
1696                                 case InsetIndexParams::PageRange::Start: {
1697                                         // Try to find the end of the range, if it is just after. Otherwise, the output will be slightly
1698                                         // scrambled, but understandable. Doing better would mean implementing more of the indexing logic here
1699                                         // and more complex indexing here (skipping the end is not just incrementing i). Worst case output:
1700                                         //     1--, 2, --3
1701                                         const bool nextEntryIsEnd = i + 1 < root_node->entries.size() &&
1702                                                                     root_node->entries[i + 1].inset()->params().range ==
1703                                                                     InsetIndexParams::PageRange::End;
1704                                         // No need to check if both entries are for the same terms: they are in the same IndexNode.
1705
1706                                         writeLinkToEntry(entry, entry_number);
1707                                         xs << XMLStream::ESCAPE_NONE << " &#8211; "; // En dash, i.e. semi-long (--).
1708
1709                                         if (nextEntryIsEnd) {
1710                                                 // Skip the next entry in the loop, write it right now, after the dash.
1711                                                 entry_number += 1;
1712                                                 i += 1;
1713                                                 writeLinkToEntry(root_node->entries[i], entry_number);
1714                                         }
1715                                 }
1716                                         break;
1717                                 case InsetIndexParams::PageRange::End:
1718                                         // This range end was not caught by the range start, do it now to avoid losing content.
1719                                         xs << XMLStream::ESCAPE_NONE << " &#8211; "; // En dash, i.e. semi-long (--).
1720                                         writeLinkToEntry(root_node->entries[i], entry_number);
1721                         }
1722
1723                         if (i < root_node->entries.size() - 1) {
1724                                 xs << ", ";
1725                         }
1726                         entry_number += 1;
1727                 }
1728         }
1729
1730         if (!root_node->entries.empty() && !root_node->children.empty()) {
1731                 xs << xml::CR();
1732         }
1733
1734         if (!root_node->children.empty()) {
1735                 xs << xml::StartTag("ul", "class='" + generateCssClassAtDepth(depth) + "'");
1736                 xs << xml::CR();
1737
1738                 for (const IndexNode* child : root_node->children) {
1739                         outputIndexPage(xs, child, depth + 1);
1740                 }
1741
1742                 xs << xml::EndTag("ul");
1743                 xs << xml::CR();
1744         }
1745
1746         xs << xml::EndTag("li");
1747         xs << xml::CR();
1748 }
1749
1750 #ifdef LYX_INSET_INDEX_DEBUG
1751 void printTree(const IndexNode* root_node, unsigned depth = 0)
1752 {
1753         static const std::string pattern = "    ";
1754         std::string prefix;
1755         for (unsigned i = 0; i < depth; ++i) {
1756                 prefix += pattern;
1757         }
1758         const std::string prefix_long = prefix + pattern + pattern;
1759
1760         docstring term_at_level;
1761         if (depth == 0) {
1762                 // The root has no term.
1763                 std::cout << "<ROOT>" << std::endl;
1764         } else {
1765                 LASSERT(depth - 1 <= 10, return); // Check for overflows.
1766                 term_at_level = termAtLevel(root_node, depth - 1);
1767                 std::cout << prefix << to_utf8(term_at_level) << " (x " << std::to_string(root_node->entries.size()) << ")"
1768                           << std::endl;
1769         }
1770
1771         for (const IndexEntry& entry : root_node->entries) {
1772                 if (entry.terms().size() != depth) {
1773                         std::cout << prefix_long << "ERROR: an entry doesn't have the same number of terms" << std::endl;
1774                 }
1775                 if (depth > 0 && entry.terms()[depth - 1] != term_at_level) {
1776                         std::cout << prefix_long << "ERROR: an entry doesn't have the right term at depth " << std::to_string(depth)
1777                                 << std::endl;
1778                 }
1779         }
1780
1781         for (const IndexNode* node : root_node->children) {
1782                 printTree(node, depth + 1);
1783         }
1784 }
1785 #endif // LYX_INSET_INDEX_DEBUG
1786 }
1787
1788
1789 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1790 {
1791         BufferParams const & bp = buffer().masterBuffer()->params();
1792
1793         shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1794         if (toc->empty())
1795                 return docstring();
1796
1797         // Collect the index entries in a form we can use them.
1798         vector<IndexEntry> entries;
1799         const docstring & indexType = params().getParamOr("type", from_ascii("idx"));
1800         for (const TocItem& item : *toc) {
1801                 const auto* inset = static_cast<const InsetIndex*>(&(item.dit().inset()));
1802                 if (item.isOutput() && inset->params().index == indexType)
1803                         entries.emplace_back(IndexEntry{inset, &op});
1804         }
1805
1806         // If all the index entries are in notes or not displayed, get out sooner.
1807         if (entries.empty())
1808                 return docstring();
1809
1810         const IndexNode* index_root = buildIndexTree(entries);
1811 #ifdef LYX_INSET_INDEX_DEBUG
1812         printTree(index_root);
1813 #endif
1814
1815         // Start generating the XHTML index.
1816         Layout const & lay = bp.documentClass().htmlTOCLayout();
1817         string const & tocclass = lay.defaultCSSClass();
1818         string const tocattr = "class='index " + tocclass + "'";
1819         docstring const indexName = params().getParamOr("name", from_ascii("Index"));
1820
1821         // we'll use our own stream, because we are going to defer everything.
1822         // that's how we deal with the fact that we're probably inside a standard
1823         // paragraph, and we don't want to be.
1824         odocstringstream ods;
1825         XMLStream xs(ods);
1826
1827         xs << xml::StartTag("div", tocattr);
1828         xs << xml::CR();
1829         xs << xml::StartTag(lay.htmltag(), lay.htmlattr());
1830         xs << translateIfPossible(indexName, op.local_font->language()->lang());
1831         xs << xml::EndTag(lay.htmltag());
1832         xs << xml::CR();
1833         xs << xml::StartTag("ul", "class='main'");
1834         xs << xml::CR();
1835
1836         LASSERT(index_root->entries.empty(), return docstring()); // No index entry should have zero terms.
1837         for (const IndexNode* node : index_root->children) {
1838                 outputIndexPage(xs, node);
1839         }
1840
1841         xs << xml::EndTag("ul");
1842         xs << xml::CR();
1843         xs << xml::EndTag("div");
1844
1845         return ods.str();
1846 }
1847
1848 } // namespace lyx