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