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