]> git.lyx.org Git - lyx.git/blob - src/insets/InsetIndex.cpp
8a75622c45f60b1c10ea06e5e327d5fefc85a85b
[lyx.git] / src / insets / InsetIndex.cpp
1 /**
2  * \file InsetIndex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetIndex.h"
14 #include "InsetIndexMacro.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "ColorSet.h"
20 #include "Cursor.h"
21 #include "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 "output_latex.h"
33 #include "output_xhtml.h"
34 #include "xml.h"
35 #include "texstream.h"
36 #include "TextClass.h"
37 #include "TocBackend.h"
38
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/FileName.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/Translator.h"
45
46 #include "frontends/alert.h"
47
48 #include <algorithm>
49 #include <set>
50 #include <ostream>
51
52 #include <QThreadStorage>
53
54 using namespace std;
55 using namespace lyx::support;
56
57 namespace lyx {
58
59 namespace {
60
61 typedef Translator<string, InsetIndexParams::PageRange> PageRangeTranslator;
62 typedef Translator<docstring, InsetIndexParams::PageRange> PageRangeTranslatorLoc;
63
64 PageRangeTranslator const init_insetindexpagerangetranslator()
65 {
66         PageRangeTranslator translator("none", InsetIndexParams::None);
67         translator.addPair("start", InsetIndexParams::Start);
68         translator.addPair("end", InsetIndexParams::End);
69         return translator;
70 }
71
72 PageRangeTranslator const init_insetindexpagerangetranslator_latex()
73 {
74         PageRangeTranslator translator("", InsetIndexParams::None);
75         translator.addPair("(", InsetIndexParams::Start);
76         translator.addPair(")", InsetIndexParams::End);
77         return translator;
78 }
79
80
81 PageRangeTranslatorLoc const init_insetindexpagerangetranslator_loc()
82 {
83         PageRangeTranslatorLoc translator(docstring(), InsetIndexParams::None);
84         translator.addPair(_("Starts page range"), InsetIndexParams::Start);
85         translator.addPair(_("Ends page range"), InsetIndexParams::End);
86         return translator;
87 }
88
89
90 PageRangeTranslator const & insetindexpagerangetranslator()
91 {
92         static PageRangeTranslator const prtranslator =
93                         init_insetindexpagerangetranslator();
94         return prtranslator;
95 }
96
97
98 PageRangeTranslatorLoc const & insetindexpagerangetranslator_loc()
99 {
100         static PageRangeTranslatorLoc const translator =
101                         init_insetindexpagerangetranslator_loc();
102         return translator;
103 }
104
105
106 PageRangeTranslator const & insetindexpagerangetranslator_latex()
107 {
108         static PageRangeTranslator const lttranslator =
109                         init_insetindexpagerangetranslator_latex();
110         return lttranslator;
111 }
112
113 } // namespace anon
114
115 /////////////////////////////////////////////////////////////////////
116 //
117 // InsetIndex
118 //
119 ///////////////////////////////////////////////////////////////////////
120
121
122 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
123         : InsetCollapsible(buf), params_(params)
124 {}
125
126
127 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
128 {
129         OutputParams runparams(runparams_in);
130         runparams.inIndexEntry = true;
131
132         otexstringstream os;
133
134         if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
135                 && params_.index != "idx") {
136                 os << "\\sindex[";
137                 os << escape(params_.index);
138                 os << "]{";
139         } else {
140                 os << "\\index";
141                 os << '{';
142         }
143
144         // Get the LaTeX output from InsetText. We need to deconstruct this later
145         // in order to check if we need to generate a sorting key
146         odocstringstream ourlatex;
147         otexstream ots(ourlatex);
148         InsetText::latex(ots, runparams);
149         if (runparams.for_search != OutputParams::NoSearch) {
150                 // No need for special handling, if we are only searching for some patterns
151                 os << ourlatex.str() << "}";
152                 return;
153         }
154
155         if (hasSortKey()) {
156                 getSortkey(os, runparams);
157                 os << "@";
158                 os << ourlatex.str();
159                 getSubentries(os, runparams);
160                 if (hasSeeRef()) {
161                         os << "|";
162                         os << insetindexpagerangetranslator_latex().find(params_.range);
163                         getSeeRefs(os, runparams);
164                 }
165         } else {
166                 // We check whether we need a sort key.
167                 // If so, we use the plaintext version
168                 odocstringstream ourplain;
169                 InsetText::plaintext(ourplain, runparams);
170
171                 // These are the LaTeX and plaintext representations
172                 docstring latexstr = ourlatex.str();
173                 docstring plainstr = ourplain.str();
174         
175                 // This will get what follows | if anything does,
176                 // the command (e.g., see, textbf) for pagination
177                 // formatting
178                 docstring cmd;
179
180                 if (hasSeeRef()) {
181                         odocstringstream seeref;
182                         otexstream otsee(seeref);
183                         getSeeRefs(otsee, runparams);
184                         cmd = seeref.str();
185                 } else if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
186                         cmd = from_utf8(params_.pagefmt);
187                 } else {
188                         // Check for the | separator to strip the cmd.
189                         // This goes wrong on an escaped "|", but as the escape
190                         // character can be changed in style files, we cannot
191                         // prevent that.
192                         size_t pos = latexstr.find(from_ascii("|"));
193                         if (pos != docstring::npos) {
194                                 // Put the bit after "|" into cmd...
195                                 cmd = latexstr.substr(pos + 1);
196                                 // ...and erase that stuff from latexstr
197                                 latexstr = latexstr.erase(pos);
198                                 // ...as well as from plainstr
199                                 size_t ppos = plainstr.find(from_ascii("|"));
200                                 if (ppos < plainstr.size())
201                                         plainstr.erase(ppos);
202                                 else
203                                         LYXERR0("The `|' separator was not found in the plaintext version!");
204                         }
205                 }
206
207                 odocstringstream subentries;
208                 otexstream otsub(subentries);
209                 getSubentries(otsub, runparams);
210                 if (subentries.str().empty()) {
211                         // Separate the entries and subentries, i.e., split on "!".
212                         // This goes wrong on an escaped "!", but as the escape
213                         // character can be changed in style files, we cannot
214                         // prevent that.
215                         std::vector<docstring> const levels =
216                                         getVectorFromString(latexstr, from_ascii("!"), true);
217                         std::vector<docstring> const levels_plain =
218                                         getVectorFromString(plainstr, from_ascii("!"), true);
219                 
220                         vector<docstring>::const_iterator it = levels.begin();
221                         vector<docstring>::const_iterator end = levels.end();
222                         vector<docstring>::const_iterator it2 = levels_plain.begin();
223                         bool first = true;
224                         for (; it != end; ++it) {
225                                 // The separator needs to be put back when
226                                 // writing the levels, except for the first level
227                                 if (!first)
228                                         os << '!';
229                                 else
230                                         first = false;
231                 
232                                 // Now here comes the reason for this whole procedure:
233                                 // We try to correctly sort macros and formatted strings.
234                                 // If we find a command, prepend a plain text
235                                 // version of the content to get sorting right,
236                                 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
237                                 // We do this on all levels.
238                                 // We don't do it if the level already contains a '@', though.
239                                 // Plaintext might return nothing (e.g. for ERTs).
240                                 // In that case, we use LaTeX.
241                                 docstring const spart = (levels_plain.empty() || (*it2).empty()) ? *it : *it2;
242                                 processLatexSorting(os, runparams, *it, spart);
243                                 if (it2 < levels_plain.end())
244                                         ++it2;
245                         }
246                 } else {
247                         processLatexSorting(os, runparams, latexstr, plainstr);
248                         os << subentries.str();
249                 }
250
251                 // At last, re-insert the command, separated by "|"
252                 if (!cmd.empty()) {
253                         os << "|"
254                            << insetindexpagerangetranslator_latex().find(params_.range)
255                            << cmd;
256                 }
257         }
258         os << '}';
259
260         // In macros with moving arguments, such as \section,
261         // we store the index and output it after the macro (#2154)
262         if (runparams_in.postpone_fragile_stuff)
263                 runparams_in.post_macro += os.str();
264         else
265                 ios << os.release();
266 }
267
268
269 void InsetIndex::processLatexSorting(otexstream & os, OutputParams const & runparams,
270                                 docstring const latex, docstring const spart) const
271 {
272         if (contains(latex, '\\') && !contains(latex, '@')) {
273                 // Now we need to validate that all characters in
274                 // the sorting part are representable in the current
275                 // encoding. If not try the LaTeX macro which might
276                 // or might not be a good choice, and issue a warning.
277                 pair<docstring, docstring> spart_latexed =
278                                 runparams.encoding->latexString(spart, runparams.dryrun);
279                 if (!spart_latexed.second.empty())
280                         LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
281                 if (spart != spart_latexed.first && !runparams.dryrun) {
282                         TeXErrors terr;
283                         ErrorList & errorList = buffer().errorList("Export");
284                         docstring const s = bformat(_("LyX's automatic index sorting algorithm faced "
285                                                       "problems with the entry '%1$s'.\n"
286                                                       "Please specify the sorting of this entry manually, as "
287                                                       "explained in the User Guide."), spart);
288                         Paragraph const & par = buffer().paragraphs().front();
289                         errorList.push_back(ErrorItem(_("Index sorting failed"), s,
290                                                       {par.id(), 0}, {par.id(), -1}));
291                         buffer().bufferErrors(terr, errorList);
292                 }
293                 // Remove remaining \'s from the sort key
294                 docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
295                 // Plain quotes need to be escaped, however (#10649), as this
296                 // is the default escape character
297                 ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
298
299                 // Now insert the sortkey, separated by '@'.
300                 os << ppart;
301                 os << '@';
302         }
303         // Insert the actual level text
304         os << latex;
305 }
306
307
308 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
309 {
310         // Two ways of processing this inset are implemented:
311         // - the legacy one, based on parsing the raw LaTeX (before LyX 2.4) -- unlikely to be deprecated
312         // - the modern one, based on precise insets for indexing features
313         // Like the LaTeX implementation, consider the user chooses either of those options.
314
315         // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
316         // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
317         //  otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
318         odocstringstream odss;
319         otexstream ots(odss);
320         InsetText::latex(ots, runparams);
321         docstring latexString = trim(odss.str());
322
323         // Check whether there are unsupported things. @ is supported, but only for sorting, without specific formatting.
324         if (latexString.find(from_utf8("@\\")) != lyx::docstring::npos) {
325                 docstring error = from_utf8("Unsupported feature: an index entry contains an @\\. "
326                                                                         "Complete entry: \"") + latexString + from_utf8("\"");
327                 LYXERR0(error);
328                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
329         }
330
331         // Handle several indices (indicated in the inset instead of the raw latexString).
332         docstring indexType = from_utf8("");
333         if (buffer().masterBuffer()->params().use_indices) {
334                 indexType += " type=\"" + params_.index + "\"";
335         }
336
337         // Split the string into its main constituents: terms, and command (see, see also, range).
338         size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
339         docstring indexTerms = latexString.substr(0, positionVerticalBar);
340         docstring command;
341         if (positionVerticalBar != lyx::docstring::npos) {
342                 command = latexString.substr(positionVerticalBar + 1);
343         }
344
345         // Handle sorting issues, with @.
346         docstring sortAs;
347         if (hasSortKey()) {
348                 sortAs = getSortkeyAsText(runparams);
349                 // indexTerms may contain a sort key if the user has both the inset and the manual key.
350         } else {
351                 vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
352                 if (sortingElements.size() == 2) {
353                         sortAs = sortingElements[0];
354                         indexTerms = sortingElements[1];
355                 }
356         }
357
358         // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
359         vector<docstring> terms;
360         if (const vector<docstring> potential_terms = getSubentriesAsText(runparams); !potential_terms.empty()) {
361                 terms = potential_terms;
362                 // The main term is not present in the vector, as it's not a subentry. The main index term is inserted raw in
363                 // the index inset. Considering that the user either uses the new or the legacy mechanism, the main term is the
364                 // full string within this inset (i.e. without the subinsets).
365                 terms.insert(terms.begin(), latexString);
366         } else {
367                 terms = getVectorFromString(indexTerms, from_ascii("!"), false);
368         }
369
370         // Handle ranges. Happily, in the raw LaTeX mode, (| and |) can only be at the end of the string!
371         bool hasInsetRange = params_.range != InsetIndexParams::PageRange::None;
372         bool hasStartRange = params_.range == InsetIndexParams::PageRange::Start ||
373                         latexString.find(from_ascii("|(")) != lyx::docstring::npos;
374         bool hasEndRange = params_.range == InsetIndexParams::PageRange::End ||
375                         latexString.find(from_ascii("|)")) != lyx::docstring::npos;
376
377         if (hasInsetRange) {
378                 // Remove the ranges from the command if they do not appear at the beginning.
379                 size_t index = 0;
380                 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
381                         command.erase(index, 1);
382                 index = 0;
383                 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
384                         command.erase(index, 1);
385
386                 // Remove the ranges when they are the only vertical bar in the complete string.
387                 if (command[0] == '(' || command[0] == ')')
388                         command.erase(0, 1);
389         }
390
391         // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
392         // Both commands are mutually exclusive!
393         docstring see = getSeeAsText(runparams);
394         vector<docstring> seeAlsoes = getSeeAlsoesAsText(runparams);
395
396         if (see.empty() && seeAlsoes.empty() && command.substr(0, 3) == "see") {
397                 // Unescape brackets.
398                 size_t index = 0;
399                 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
400                         command.erase(index, 1);
401                 index = 0;
402                 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
403                         command.erase(index, 1);
404
405                 // Retrieve the part between brackets, and remove the complete seealso.
406                 size_t positionOpeningBracket = command.find(from_ascii("{"));
407                 size_t positionClosingBracket = command.find(from_ascii("}"));
408                 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
409
410                 // Parse the list of referenced entries (or a single one for see).
411                 if (command.substr(0, 7) == "seealso") {
412                         seeAlsoes = getVectorFromString(list, from_ascii(","), false);
413                 } else {
414                         see = list;
415
416                         if (see.find(from_ascii(",")) != std::string::npos) {
417                                 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
418                                                                                         "Complete entry: \"") + latexString + from_utf8("\"");
419                                 LYXERR0(error);
420                                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
421                         }
422                 }
423
424                 // Remove the complete see/seealso from the commands, in case there is something else to parse.
425                 command = command.substr(positionClosingBracket + 1);
426         }
427
428         // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
429         // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
430         // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
431         // no range will be detected.
432         // TODO: Could handle formatting as significance="preferred"?
433         if (!command.empty()) {
434                 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
435                                           + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
436                 LYXERR0(error);
437                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
438         }
439
440     // Write all of this down.
441         if (terms.empty() && !hasEndRange) {
442                 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
443                 LYXERR0(error);
444                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
445         } else {
446                 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
447                 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
448                 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
449                 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
450                 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
451                 // xml::cleanID.
452                 // indexType can only be used for singular and startofrange types!
453                 docstring attrs;
454                 if (!hasStartRange && !hasEndRange) {
455                         attrs = indexType;
456                 } else {
457                         // Append an ID if uniqueness is not guaranteed across the document.
458                         static QThreadStorage<set<docstring>> tKnownTermLists;
459                         static QThreadStorage<int> tID;
460
461                         set<docstring> &knownTermLists = tKnownTermLists.localData();
462                         int &ID = tID.localData();
463
464                         if (!tID.hasLocalData()) {
465                                 tID.localData() = 0;
466                         }
467
468                         // Modify the index terms to add the unique ID if needed.
469                         docstring newIndexTerms = indexTerms;
470                         if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
471                                 newIndexTerms += from_ascii(string("-") + to_string(ID));
472
473                                 // Only increment for the end of range, so that the same number is used for the start of range.
474                                 if (hasEndRange) {
475                                         ID++;
476                                 }
477                         }
478
479                         // Term list not yet known: add it to the set AFTER the end of range. After
480                         if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
481                                 knownTermLists.insert(indexTerms);
482                         }
483
484                         // Generate the attributes.
485                         docstring id = xml::cleanID(newIndexTerms);
486                         if (hasStartRange) {
487                                 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
488                         } else {
489                                 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
490                         }
491                 }
492
493                 // Handle the index terms (including the specific index for this entry).
494                 if (hasEndRange) {
495                         xs << xml::CompTag("indexterm", attrs);
496                 } else {
497                         xs << xml::StartTag("indexterm", attrs);
498                         if (!terms.empty()) { // hasEndRange has no content.
499                                 docstring attr;
500                                 if (!sortAs.empty()) {
501                                         attr = from_utf8("sortas='") + sortAs + from_utf8("'");
502                                 }
503
504                                 xs << xml::StartTag("primary", attr);
505                                 xs << terms[0];
506                                 xs << xml::EndTag("primary");
507                         }
508                         if (terms.size() > 1) {
509                                 xs << xml::StartTag("secondary");
510                                 xs << terms[1];
511                                 xs << xml::EndTag("secondary");
512                         }
513                         if (terms.size() > 2) {
514                                 xs << xml::StartTag("tertiary");
515                                 xs << terms[2];
516                                 xs << xml::EndTag("tertiary");
517                         }
518
519                         // Handle see and see also.
520                         if (!see.empty()) {
521                                 xs << xml::StartTag("see");
522                                 xs << see;
523                                 xs << xml::EndTag("see");
524                         }
525
526                         if (!seeAlsoes.empty()) {
527                                 for (auto &entry : seeAlsoes) {
528                                         xs << xml::StartTag("seealso");
529                                         xs << entry;
530                                         xs << xml::EndTag("seealso");
531                                 }
532                         }
533
534                         // Close the entry.
535                         xs << xml::EndTag("indexterm");
536                 }
537         }
538 }
539
540
541 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
542 {
543         // we just print an anchor, taking the paragraph ID from
544         // our own interior paragraph, which doesn't get printed
545         std::string const magic = paragraphs().front().magicLabel();
546         std::string const attr = "id='" + magic + "'";
547         xs << xml::CompTag("a", attr);
548         return docstring();
549 }
550
551
552 bool InsetIndex::showInsetDialog(BufferView * bv) const
553 {
554         bv->showDialog("index", params2string(params_),
555                         const_cast<InsetIndex *>(this));
556         return true;
557 }
558
559
560 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
561 {
562         switch (cmd.action()) {
563
564         case LFUN_INSET_MODIFY: {
565                 if (cmd.getArg(0) == "changetype") {
566                         cur.recordUndoInset(this);
567                         params_.index = from_utf8(cmd.getArg(1));
568                         break;
569                 }
570                 InsetIndexParams params;
571                 InsetIndex::string2params(to_utf8(cmd.argument()), params);
572                 cur.recordUndoInset(this);
573                 params_.index = params.index;
574                 params_.range = params.range;
575                 params_.pagefmt = params.pagefmt;
576                 // what we really want here is a TOC update, but that means
577                 // a full buffer update
578                 cur.forceBufferUpdate();
579                 break;
580         }
581
582         case LFUN_INSET_DIALOG_UPDATE:
583                 cur.bv().updateDialog("index", params2string(params_));
584                 break;
585
586         default:
587                 InsetCollapsible::doDispatch(cur, cmd);
588                 break;
589         }
590 }
591
592
593 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
594                 FuncStatus & flag) const
595 {
596         switch (cmd.action()) {
597
598         case LFUN_INSET_MODIFY:
599                 if (cmd.getArg(0) == "changetype") {
600                         docstring const newtype = from_utf8(cmd.getArg(1));
601                         Buffer const & realbuffer = *buffer().masterBuffer();
602                         IndicesList const & indiceslist = realbuffer.params().indiceslist();
603                         Index const * index = indiceslist.findShortcut(newtype);
604                         flag.setEnabled(index != 0);
605                         flag.setOnOff(
606                                 from_utf8(cmd.getArg(1)) == params_.index);
607                         return true;
608                 }
609                 return InsetCollapsible::getStatus(cur, cmd, flag);
610
611         case LFUN_INSET_DIALOG_UPDATE: {
612                 Buffer const & realbuffer = *buffer().masterBuffer();
613                 flag.setEnabled(realbuffer.params().use_indices);
614                 return true;
615         }
616         
617         case LFUN_INDEXMACRO_INSERT:
618                 return macrosPossible(cmd.getArg(0));
619
620         default:
621                 return InsetCollapsible::getStatus(cur, cmd, flag);
622         }
623 }
624
625
626 void InsetIndex::getSortkey(otexstream & os, OutputParams const & runparams) const
627 {
628         Paragraph const & par = paragraphs().front();
629         InsetList::const_iterator it = par.insetList().begin();
630         for (; it != par.insetList().end(); ++it) {
631                 Inset & inset = *it->inset;
632                 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
633                         InsetIndexMacro const & iim =
634                                 static_cast<InsetIndexMacro const &>(inset);
635                         iim.getLatex(os, runparams);
636                         return;
637                 }
638         }
639 }
640
641
642 docstring InsetIndex::getSortkeyAsText(OutputParams const & runparams) const
643 {
644         Paragraph const & par = paragraphs().front();
645         InsetList::const_iterator it = par.insetList().begin();
646         for (; it != par.insetList().end(); ++it) {
647                 Inset & inset = *it->inset;
648                 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE) {
649                         otexstringstream os;
650                         InsetIndexMacro const & iim =
651                                 static_cast<InsetIndexMacro const &>(inset);
652                         iim.getLatex(os, runparams);
653                         return os.str();
654                 }
655         }
656         return from_ascii("");
657 }
658
659
660 void InsetIndex::getSubentries(otexstream & os, OutputParams const & runparams) const
661 {
662         Paragraph const & par = paragraphs().front();
663         InsetList::const_iterator it = par.insetList().begin();
664         int i = 0;
665         for (; it != par.insetList().end(); ++it) {
666                 Inset & inset = *it->inset;
667                 if (inset.lyxCode() == INDEXMACRO_CODE) {
668                         InsetIndexMacro const & iim =
669                                 static_cast<InsetIndexMacro const &>(inset);
670                         if (iim.params().type == InsetIndexMacroParams::Subindex) {
671                                 ++i;
672                                 if (i > 2)
673                                         return;
674                                 os << "!";
675                                 iim.getLatex(os, runparams);
676                         }
677                 }
678         }
679 }
680
681
682 std::vector<docstring> InsetIndex::getSubentriesAsText(OutputParams const & runparams) const
683 {
684         std::vector<docstring> subentries;
685
686         Paragraph const & par = paragraphs().front();
687         InsetList::const_iterator it = par.insetList().begin();
688         int i = 0;
689         for (; it != par.insetList().end(); ++it) {
690                 Inset & inset = *it->inset;
691                 if (inset.lyxCode() == INDEXMACRO_CODE) {
692                         InsetIndexMacro const & iim =
693                                 static_cast<InsetIndexMacro const &>(inset);
694                         if (iim.params().type == InsetIndexMacroParams::Subindex) {
695                                 ++i;
696                                 if (i > 2)
697                                         break;
698
699                                 otexstringstream os;
700                                 iim.getLatex(os, runparams);
701                                 subentries.emplace_back(os.str());
702                         }
703                 }
704         }
705
706         return subentries;
707 }
708
709
710 void InsetIndex::getSeeRefs(otexstream & os, OutputParams const & runparams) const
711 {
712         Paragraph const & par = paragraphs().front();
713         InsetList::const_iterator it = par.insetList().begin();
714         for (; it != par.insetList().end(); ++it) {
715                 Inset & inset = *it->inset;
716                 if (inset.lyxCode() == INDEXMACRO_CODE) {
717                         InsetIndexMacro const & iim =
718                                 static_cast<InsetIndexMacro const &>(inset);
719                         if (iim.params().type == InsetIndexMacroParams::See
720                             || iim.params().type == InsetIndexMacroParams::Seealso) {
721                                 iim.getLatex(os, runparams);
722                                 return;
723                         }
724                 }
725         }
726 }
727
728
729 docstring InsetIndex::getSeeAsText(OutputParams const & runparams) const
730 {
731         Paragraph const & par = paragraphs().front();
732         InsetList::const_iterator it = par.insetList().begin();
733         for (; it != par.insetList().end(); ++it) {
734                 Inset & inset = *it->inset;
735                 if (inset.lyxCode() == INDEXMACRO_CODE) {
736                         InsetIndexMacro const & iim =
737                                 static_cast<InsetIndexMacro const &>(inset);
738                         if (iim.params().type == InsetIndexMacroParams::See) {
739                                 otexstringstream os;
740                                 iim.getLatex(os, runparams);
741                                 return os.str();
742                         }
743                 }
744         }
745         return from_ascii("");
746 }
747
748
749 std::vector<docstring> InsetIndex::getSeeAlsoesAsText(OutputParams const & runparams) const
750 {
751         std::vector<docstring> seeAlsoes;
752
753         Paragraph const & par = paragraphs().front();
754         InsetList::const_iterator it = par.insetList().begin();
755         for (; it != par.insetList().end(); ++it) {
756                 Inset & inset = *it->inset;
757                 if (inset.lyxCode() == INDEXMACRO_CODE) {
758                         InsetIndexMacro const & iim =
759                                 static_cast<InsetIndexMacro const &>(inset);
760                         if (iim.params().type == InsetIndexMacroParams::Seealso) {
761                                 otexstringstream os;
762                                 iim.getLatex(os, runparams);
763                                 seeAlsoes.emplace_back(os.str());
764                         }
765                 }
766         }
767
768         return seeAlsoes;
769 }
770
771
772 bool InsetIndex::hasSubentries() const
773 {
774         Paragraph const & par = paragraphs().front();
775         InsetList::const_iterator it = par.insetList().begin();
776         for (; it != par.insetList().end(); ++it) {
777                 Inset & inset = *it->inset;
778                 if (inset.lyxCode() == INDEXMACRO_CODE) {
779                         InsetIndexMacro const & iim =
780                                 static_cast<InsetIndexMacro const &>(inset);
781                         if (iim.params().type == InsetIndexMacroParams::Subindex)
782                                 return true;
783                 }
784         }
785         return false;
786 }
787
788
789 bool InsetIndex::hasSeeRef() 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                             || iim.params().type == InsetIndexMacroParams::Seealso)
800                                 return true;
801                 }
802         }
803         return false;
804 }
805
806
807 bool InsetIndex::hasSortKey() const
808 {
809         Paragraph const & par = paragraphs().front();
810         InsetList::const_iterator it = par.insetList().begin();
811         for (; it != par.insetList().end(); ++it) {
812                 Inset & inset = *it->inset;
813                 if (inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
814                         return true;
815         }
816         return false;
817 }
818
819
820 bool InsetIndex::macrosPossible(string const type) const
821 {
822         if (type != "see" && type != "seealso"
823             && type != "sortkey" && type != "subindex")
824                 return false;
825
826         Paragraph const & par = paragraphs().front();
827         InsetList::const_iterator it = par.insetList().begin();
828         int subidxs = 0;
829         for (; it != par.insetList().end(); ++it) {
830                 Inset & inset = *it->inset;
831                 if (type == "sortkey" && inset.lyxCode() == INDEXMACRO_SORTKEY_CODE)
832                         return false;
833                 if (inset.lyxCode() == INDEXMACRO_CODE) {
834                         InsetIndexMacro const & iim = static_cast<InsetIndexMacro const &>(inset);
835                         if ((type == "see" || type == "seealso")
836                              && (iim.params().type == InsetIndexMacroParams::See
837                                  || iim.params().type == InsetIndexMacroParams::Seealso))
838                                 return false;
839                         if (type == "subindex"
840                              && iim.params().type == InsetIndexMacroParams::Subindex) {
841                                 ++subidxs;
842                                 if (subidxs > 1)
843                                         return false;
844                         }
845                 }
846         }
847         return true;
848 }
849
850
851 ColorCode InsetIndex::labelColor() const
852 {
853         if (params_.index.empty() || params_.index == from_ascii("idx"))
854                 return InsetCollapsible::labelColor();
855         // FIXME UNICODE
856         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
857                                             + "@" + buffer().fileName().absFileName());
858         if (c == Color_none)
859                 c = InsetCollapsible::labelColor();
860         return c;
861 }
862
863
864 docstring InsetIndex::toolTip(BufferView const &, int, int) const
865 {
866         docstring tip = _("Index Entry");
867         if (buffer().params().use_indices && !params_.index.empty()) {
868                 Buffer const & realbuffer = *buffer().masterBuffer();
869                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
870                 tip += " (";
871                 Index const * index = indiceslist.findShortcut(params_.index);
872                 if (!index)
873                         tip += _("unknown type!");
874                 else
875                         tip += index->index();
876                 tip += ")";
877         }
878         tip += ": ";
879         docstring res = toolTipText(tip);
880         if (!insetindexpagerangetranslator_loc().find(params_.range).empty())
881                 res += "\n" + insetindexpagerangetranslator_loc().find(params_.range);
882         if (!params_.pagefmt.empty() && params_.pagefmt != "default") {
883                 res += "\n" + _("Pagination format:") + " ";
884                 if (params_.pagefmt == "textbf")
885                         res += _("bold");
886                 else if (params_.pagefmt == "textit")
887                         res += _("italic");
888                 else if (params_.pagefmt == "emph")
889                         res += _("emphasized");
890                 else
891                         res += from_utf8(params_.pagefmt);
892         }
893         return res;
894 }
895
896
897 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
898 {
899         InsetLayout const & il = getLayout();
900         docstring label = translateIfPossible(il.labelstring());
901
902         if (buffer().params().use_indices && !params_.index.empty()) {
903                 Buffer const & realbuffer = *buffer().masterBuffer();
904                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
905                 label += " (";
906                 Index const * index = indiceslist.findShortcut(params_.index);
907                 if (!index)
908                         label += _("unknown type!");
909                 else
910                         label += index->index();
911                 label += ")";
912         }
913
914         docstring res;
915         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
916                 res = label;
917         else
918                 res = getNewLabel(label);
919         if (!insetindexpagerangetranslator_latex().find(params_.range).empty())
920                 res += " " + from_ascii(insetindexpagerangetranslator_latex().find(params_.range));
921         return res;
922 }
923
924
925 void InsetIndex::write(ostream & os) const
926 {
927         os << to_utf8(layoutName());
928         params_.write(os);
929         InsetCollapsible::write(os);
930 }
931
932
933 void InsetIndex::read(Lexer & lex)
934 {
935         params_.read(lex);
936         InsetCollapsible::read(lex);
937 }
938
939
940 string InsetIndex::params2string(InsetIndexParams const & params)
941 {
942         ostringstream data;
943         data << "index";
944         params.write(data);
945         return data.str();
946 }
947
948
949 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
950 {
951         params = InsetIndexParams();
952         if (in.empty())
953                 return;
954
955         istringstream data(in);
956         Lexer lex;
957         lex.setStream(data);
958         lex.setContext("InsetIndex::string2params");
959         lex >> "index";
960         params.read(lex);
961 }
962
963
964 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
965                                                   UpdateType utype, TocBackend & backend) const
966 {
967         DocIterator pit = cpit;
968         pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
969         docstring str;
970         string type = "index";
971         if (buffer().masterBuffer()->params().use_indices)
972                 type += ":" + to_utf8(params_.index);
973         // this is unlikely to be terribly long
974         text().forOutliner(str, INT_MAX);
975         TocBuilder & b = backend.builder(type);
976         b.pushItem(pit, str, output_active);
977         // Proceed with the rest of the inset.
978         InsetCollapsible::addToToc(cpit, output_active, utype, backend);
979         b.pop();
980 }
981
982
983 void InsetIndex::validate(LaTeXFeatures & features) const
984 {
985         if (buffer().masterBuffer()->params().use_indices
986             && !params_.index.empty()
987             && params_.index != "idx")
988                 features.require("splitidx");
989         InsetCollapsible::validate(features);
990 }
991
992
993 string InsetIndex::contextMenuName() const
994 {
995         return "context-index";
996 }
997
998
999 string InsetIndex::contextMenu(BufferView const & bv, int x, int y) const
1000 {
1001         // We override the implementation of InsetCollapsible,
1002         // because we have eytra entries.
1003         string owncm = "context-edit-index;";
1004         return owncm + InsetCollapsible::contextMenu(bv, x, y);
1005 }
1006
1007
1008 bool InsetIndex::hasSettings() const
1009 {
1010         return true;
1011 }
1012
1013
1014 bool InsetIndex::insetAllowed(InsetCode code) const
1015 {
1016         switch (code) {
1017         case INDEXMACRO_CODE:
1018         case INDEXMACRO_SORTKEY_CODE:
1019                 return true;
1020         case INDEX_CODE:
1021                 return false;
1022         default:
1023                 return InsetCollapsible::insetAllowed(code);
1024         }
1025 }
1026
1027
1028 /////////////////////////////////////////////////////////////////////
1029 //
1030 // InsetIndexParams
1031 //
1032 ///////////////////////////////////////////////////////////////////////
1033
1034
1035 void InsetIndexParams::write(ostream & os) const
1036 {
1037         os << ' ';
1038         if (!index.empty())
1039                 os << to_utf8(index);
1040         else
1041                 os << "idx";
1042         os << '\n';
1043         os << "range "
1044            << insetindexpagerangetranslator().find(range)
1045            << '\n';
1046         os << "pageformat "
1047            << pagefmt
1048            << '\n';
1049 }
1050
1051
1052 void InsetIndexParams::read(Lexer & lex)
1053 {
1054         if (lex.eatLine())
1055                 index = lex.getDocString();
1056         else
1057                 index = from_ascii("idx");
1058         if (lex.checkFor("range")) {
1059                 string st = lex.getString();
1060                 if (lex.eatLine()) {
1061                         st = lex.getString();
1062                         range = insetindexpagerangetranslator().find(lex.getString());
1063                 }
1064         }
1065         if (lex.checkFor("pageformat") && lex.eatLine()) {
1066                 pagefmt = lex.getString();
1067         }
1068 }
1069
1070
1071 /////////////////////////////////////////////////////////////////////
1072 //
1073 // InsetPrintIndex
1074 //
1075 ///////////////////////////////////////////////////////////////////////
1076
1077 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
1078         : InsetCommand(buf, p)
1079 {}
1080
1081
1082 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
1083 {
1084         static ParamInfo param_info_;
1085         if (param_info_.empty()) {
1086                 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
1087                                 ParamInfo::HANDLING_ESCAPE);
1088                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
1089                                 ParamInfo::HANDLING_LATEXIFY);
1090                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
1091         }
1092         return param_info_;
1093 }
1094
1095
1096 docstring InsetPrintIndex::screenLabel() const
1097 {
1098         bool const printall = suffixIs(getCmdName(), '*');
1099         bool const multind = buffer().masterBuffer()->params().use_indices;
1100         if ((!multind
1101              && getParam("type") == from_ascii("idx"))
1102             || (getParam("type").empty() && !printall))
1103                 return _("Index");
1104         Buffer const & realbuffer = *buffer().masterBuffer();
1105         IndicesList const & indiceslist = realbuffer.params().indiceslist();
1106         Index const * index = indiceslist.findShortcut(getParam("type"));
1107         if (!index && !printall)
1108                 return _("Unknown index type!");
1109         docstring res = printall ? _("All indexes") : index->index();
1110         if (!multind)
1111                 res += " (" + _("non-active") + ")";
1112         else if (contains(getCmdName(), "printsubindex"))
1113                 res += " (" + _("subindex") + ")";
1114         return res;
1115 }
1116
1117
1118 bool InsetPrintIndex::isCompatibleCommand(string const & s)
1119 {
1120         return s == "printindex" || s == "printsubindex"
1121                 || s == "printindex*" || s == "printsubindex*";
1122 }
1123
1124
1125 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
1126 {
1127         switch (cmd.action()) {
1128
1129         case LFUN_INSET_MODIFY: {
1130                 if (cmd.argument() == from_ascii("toggle-subindex")) {
1131                         string scmd = getCmdName();
1132                         if (contains(scmd, "printindex"))
1133                                 scmd = subst(scmd, "printindex", "printsubindex");
1134                         else
1135                                 scmd = subst(scmd, "printsubindex", "printindex");
1136                         cur.recordUndo();
1137                         setCmdName(scmd);
1138                         break;
1139                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1140                         string scmd = getCmdName();
1141                         if (suffixIs(scmd, '*'))
1142                                 break;
1143                         scmd += '*';
1144                         cur.recordUndo();
1145                         setParam("type", docstring());
1146                         setCmdName(scmd);
1147                         break;
1148                 }
1149                 InsetCommandParams p(INDEX_PRINT_CODE);
1150                 // FIXME UNICODE
1151                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
1152                 if (p.getCmdName().empty()) {
1153                         cur.noScreenUpdate();
1154                         break;
1155                 }
1156                 cur.recordUndo();
1157                 setParams(p);
1158                 break;
1159         }
1160
1161         default:
1162                 InsetCommand::doDispatch(cur, cmd);
1163                 break;
1164         }
1165 }
1166
1167
1168 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
1169         FuncStatus & status) const
1170 {
1171         switch (cmd.action()) {
1172
1173         case LFUN_INSET_MODIFY: {
1174                 if (cmd.argument() == from_ascii("toggle-subindex")) {
1175                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
1176                         status.setOnOff(contains(getCmdName(), "printsubindex"));
1177                         return true;
1178                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
1179                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
1180                         status.setOnOff(suffixIs(getCmdName(), '*'));
1181                         return true;
1182                 } if (cmd.getArg(0) == "index_print"
1183                     && cmd.getArg(1) == "CommandInset") {
1184                         InsetCommandParams p(INDEX_PRINT_CODE);
1185                         InsetCommand::string2params(to_utf8(cmd.argument()), p);
1186                         if (suffixIs(p.getCmdName(), '*')) {
1187                                 status.setEnabled(true);
1188                                 status.setOnOff(false);
1189                                 return true;
1190                         }
1191                         Buffer const & realbuffer = *buffer().masterBuffer();
1192                         IndicesList const & indiceslist =
1193                                 realbuffer.params().indiceslist();
1194                         Index const * index = indiceslist.findShortcut(p["type"]);
1195                         status.setEnabled(index != 0);
1196                         status.setOnOff(p["type"] == getParam("type"));
1197                         return true;
1198                 } else
1199                         return InsetCommand::getStatus(cur, cmd, status);
1200         }
1201
1202         case LFUN_INSET_DIALOG_UPDATE: {
1203                 status.setEnabled(buffer().masterBuffer()->params().use_indices);
1204                 return true;
1205         }
1206
1207         default:
1208                 return InsetCommand::getStatus(cur, cmd, status);
1209         }
1210 }
1211
1212
1213 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
1214 {
1215         Index const * index =
1216                 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
1217         if (index)
1218                 setParam("name", index->index());
1219 }
1220
1221
1222 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
1223 {
1224         if (!buffer().masterBuffer()->params().use_indices) {
1225                 if (getParam("type") == from_ascii("idx"))
1226                         os << "\\printindex" << termcmd;
1227                 return;
1228         }
1229         OutputParams runparams = runparams_in;
1230         os << getCommand(runparams);
1231 }
1232
1233
1234 void InsetPrintIndex::validate(LaTeXFeatures & features) const
1235 {
1236         features.require("makeidx");
1237         if (buffer().masterBuffer()->params().use_indices)
1238                 features.require("splitidx");
1239         InsetCommand::validate(features);
1240 }
1241
1242
1243 string InsetPrintIndex::contextMenuName() const
1244 {
1245         return buffer().masterBuffer()->params().use_indices ?
1246                 "context-indexprint" : string();
1247 }
1248
1249
1250 bool InsetPrintIndex::hasSettings() const
1251 {
1252         return buffer().masterBuffer()->params().use_indices;
1253 }
1254
1255
1256 namespace {
1257 struct IndexEntry
1258 {
1259         IndexEntry() = default;
1260
1261         /// Builds an entry for the index with the given LaTeX index entry `s` and a pointer to the index inset `d`.
1262         IndexEntry(docstring const & s, DocIterator const & d, bool for_output = false)
1263                         : dit_(d), output_error_{}
1264         {
1265                 extractSubentries(s);
1266                 parseItem(main_, for_output);
1267                 parseItem(sub_, for_output);
1268                 parseItem(subsub_, for_output);
1269         }
1270
1271         /// Comparison between two entries only based on their LaTeX representation.
1272         bool equal(IndexEntry const & rhs) const
1273         {
1274                 return main_ == rhs.main_ && sub_ == rhs.sub_ && subsub_ == rhs.subsub_;
1275         }
1276
1277         bool same_sub(IndexEntry const & rhs) const
1278         {
1279                 return main_ == rhs.main_ && sub_ == rhs.sub_;
1280         }
1281
1282         bool same_main(IndexEntry const & rhs) const
1283         {
1284                 return main_ == rhs.main_;
1285         }
1286
1287         const Paragraph & innerParagraph() const
1288         {
1289                 return dit_.innerParagraph();
1290         }
1291
1292         const docstring & main() const
1293         {
1294                 return main_;
1295         }
1296
1297         const docstring & sub() const
1298         {
1299                 return sub_;
1300         }
1301
1302         const docstring & subsub() const
1303         {
1304                 return subsub_;
1305         }
1306
1307         const DocIterator & dit() const
1308         {
1309                 return dit_;
1310         }
1311
1312         /// When parsing this entry, some errors may be found; they are reported as a single string.
1313         // It is up to the caller to send this string to LYXERR and the output file, as needed.
1314         const docstring & output_error() const
1315         {
1316                 return output_error_;
1317         }
1318
1319
1320 private:
1321         void checkForUnsupportedFeatures(docstring const & entry)
1322         {
1323                 if (entry.find(from_utf8("@\\")) != lyx::docstring::npos) {
1324                         output_error_ = from_utf8("Unsupported feature: an index entry contains an @\\. "
1325                                                                           "Complete entry: \"") + entry + from_utf8("\"");
1326                 }
1327         }
1328
1329         /// Splits the LaTeX entry into subentries.
1330         void extractSubentries(docstring const & entry)
1331         {
1332                 if (entry.empty())
1333                         return;
1334
1335                 size_type const loc_first_bang = entry.find(from_ascii("!"));
1336                 if (loc_first_bang == string::npos) {
1337                         // No subentry.
1338                         main_ = entry;
1339                 } else {
1340                         main_ = trim(entry.substr(0, loc_first_bang));
1341                         size_t const loc_after_first_bang = loc_first_bang + 1;
1342
1343                         size_type const loc_second_bang = entry.find(from_ascii("!"), loc_after_first_bang);
1344                         if (loc_second_bang == string::npos) {
1345                                 // Only a subentry, no subsubentry.
1346                                 sub_ = trim(entry.substr(loc_after_first_bang));
1347                         } else {
1348                                 // Subsubentry.
1349                                 sub_ = trim(entry.substr(loc_after_first_bang, loc_second_bang - loc_after_first_bang));
1350                                 subsub_ = trim(entry.substr(loc_second_bang + 1));
1351                         }
1352                 }
1353         }
1354
1355         void parseItem(docstring & s, bool for_output)
1356         {
1357                 // this does not yet check for escaped things
1358                 size_type loc = s.find(from_ascii("@"));
1359                 if (loc != string::npos) {
1360                         if (for_output)
1361                                 s.erase(0, loc + 1);
1362                         else
1363                                 s.erase(loc);
1364                 }
1365                 loc = s.find(from_ascii("|"));
1366                 if (loc != string::npos)
1367                         s.erase(loc);
1368         }
1369
1370 private:
1371         docstring output_error_;
1372         docstring main_;
1373         docstring sub_;
1374         docstring subsub_;
1375         DocIterator dit_;
1376
1377         friend bool operator<(IndexEntry const & lhs, IndexEntry const & rhs);
1378 };
1379
1380 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
1381 {
1382         int comp = compare_no_case(lhs.main_, rhs.main_);
1383         if (comp == 0)
1384                 comp = compare_no_case(lhs.sub_, rhs.sub_);
1385         if (comp == 0)
1386                 comp = compare_no_case(lhs.subsub_, rhs.subsub_);
1387         return comp < 0;
1388 }
1389
1390 } // namespace
1391
1392
1393 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
1394 {
1395         BufferParams const & bp = buffer().masterBuffer()->params();
1396
1397         // we do not presently support multiple indices, so we refuse to print
1398         // anything but the main index, so as not to generate multiple indices.
1399         // NOTE Multiple index support would require some work. The reason
1400         // is that the TOC does not know about multiple indices. Either it would
1401         // need to be told about them (not a bad idea), or else the index entries
1402         // would need to be collected differently, say, during validation.
1403         if (bp.use_indices && getParam("type") != from_ascii("idx"))
1404                 return docstring();
1405
1406         shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
1407         if (toc->empty())
1408                 return docstring();
1409
1410         // Collect the index entries in a form we can use them.
1411         Toc::const_iterator it = toc->begin();
1412         Toc::const_iterator const en = toc->end();
1413         vector<IndexEntry> entries;
1414         for (; it != en; ++it)
1415                 if (it->isOutput())
1416                         entries.push_back(IndexEntry(it->str(), it->dit()));
1417
1418         if (entries.empty())
1419                 // not very likely that all the index entries are in notes or
1420                 // whatever, but....
1421                 return docstring();
1422
1423         // Sort the entries, first on the main entry, then the subentry, then the subsubentry,
1424         // thanks to the implementation of operator<.
1425         stable_sort(entries.begin(), entries.end());
1426
1427         Layout const & lay = bp.documentClass().htmlTOCLayout();
1428         string const & tocclass = lay.defaultCSSClass();
1429         string const tocattr = "class='index " + tocclass + "'";
1430
1431         // we'll use our own stream, because we are going to defer everything.
1432         // that's how we deal with the fact that we're probably inside a standard
1433         // paragraph, and we don't want to be.
1434         odocstringstream ods;
1435         XMLStream xs(ods);
1436
1437         xs << xml::StartTag("div", tocattr);
1438         xs << xml::StartTag(lay.htmltag(), lay.htmlattr())
1439                  << translateIfPossible(from_ascii("Index"),
1440                                   op.local_font->language()->lang())
1441                  << xml::EndTag(lay.htmltag());
1442         xs << xml::StartTag("ul", "class='main'");
1443         Font const dummy;
1444
1445         vector<IndexEntry>::const_iterator eit = entries.begin();
1446         vector<IndexEntry>::const_iterator const een = entries.end();
1447         // tracks whether we are already inside a main entry (1),
1448         // a sub-entry (2), or a sub-sub-entry (3). see below for the
1449         // details.
1450         int level = 1;
1451         // the last one we saw
1452         IndexEntry last;
1453         int entry_number = -1;
1454         for (; eit != een; ++eit) {
1455                 Paragraph const & par = eit->innerParagraph();
1456                 if (entry_number == -1 || !eit->equal(last)) {
1457                         if (entry_number != -1) {
1458                                 // not the first time through the loop, so
1459                                 // close last entry or entries, depending.
1460                                 if (level == 3) {
1461                                         // close this sub-sub-entry
1462                                         xs << xml::EndTag("li") << xml::CR();
1463                                         // is this another sub-sub-entry within the same sub-entry?
1464                                         if (!eit->same_sub(last)) {
1465                                                 // close this level
1466                                                 xs << xml::EndTag("ul") << xml::CR();
1467                                                 level = 2;
1468                                         }
1469                                 }
1470                                 // the point of the second test here is that we might get
1471                                 // here two ways: (i) by falling through from above; (ii) because,
1472                                 // though the sub-entry hasn't changed, the sub-sub-entry has,
1473                                 // which means that it is the first sub-sub-entry within this
1474                                 // sub-entry. In that case, we do not want to close anything.
1475                                 if (level == 2 && !eit->same_sub(last)) {
1476                                         // close sub-entry
1477                                         xs << xml::EndTag("li") << xml::CR();
1478                                         // is this another sub-entry with the same main entry?
1479                                         if (!eit->same_main(last)) {
1480                                                 // close this level
1481                                                 xs << xml::EndTag("ul") << xml::CR();
1482                                                 level = 1;
1483                                         }
1484                                 }
1485                                 // again, we can get here two ways: from above, or because we have
1486                                 // found the first sub-entry. in the latter case, we do not want to
1487                                 // close the entry.
1488                                 if (level == 1 && !eit->same_main(last)) {
1489                                         // close entry
1490                                         xs << xml::EndTag("li") << xml::CR();
1491                                 }
1492                         }
1493
1494                         // we'll be starting new entries
1495                         entry_number = 0;
1496
1497                         // We need to use our own stream, since we will have to
1498                         // modify what we get back.
1499                         odocstringstream ent;
1500                         XMLStream entstream(ent);
1501                         OutputParams ours = op;
1502                         ours.for_toc = true;
1503                         par.simpleLyXHTMLOnePar(buffer(), entstream, ours, dummy);
1504
1505                         // these will contain XHTML versions of the main entry, etc.
1506                         // remember that everything will already have been escaped,
1507                         // so we'll need to use NextRaw() during output.
1508                         IndexEntry xhtml_entry = IndexEntry(ent.str(), eit->dit(), true);
1509
1510                         if (level == 3) {
1511                                 // another subsubentry
1512                                 xs << xml::StartTag("li", "class='subsubentry'")
1513                                    << XMLStream::ESCAPE_NONE << xhtml_entry.subsub();
1514                         } else if (level == 2) {
1515                                 // there are two ways we can be here:
1516                                 // (i) we can actually be inside a sub-entry already and be about
1517                                 //     to output the first sub-sub-entry. in this case, our sub
1518                                 //     and the last sub will be the same.
1519                                 // (ii) we can just have closed a sub-entry, possibly after also
1520                                 //     closing a list of sub-sub-entries. here our sub and the last
1521                                 //     sub are different.
1522                                 // only in the latter case do we need to output the new sub-entry.
1523                                 // note that in this case, too, though, the sub-entry might already
1524                                 // have a sub-sub-entry.
1525                                 if (eit->sub() != last.sub())
1526                                         xs << xml::StartTag("li", "class='subentry'")
1527                                            << XMLStream::ESCAPE_NONE << xhtml_entry.sub();
1528                                 if (!xhtml_entry.subsub().empty()) {
1529                                         // it's actually a subsubentry, so we need to start that list
1530                                         xs << xml::CR()
1531                                            << xml::StartTag("ul", "class='subsubentry'")
1532                                            << xml::StartTag("li", "class='subsubentry'")
1533                                            << XMLStream::ESCAPE_NONE << xhtml_entry.subsub();
1534                                         level = 3;
1535                                 }
1536                         } else {
1537                                 // there are also two ways we can be here:
1538                                 // (i) we can actually be inside an entry already and be about
1539                                 //     to output the first sub-entry. in this case, our main
1540                                 //     and the last main will be the same.
1541                                 // (ii) we can just have closed an entry, possibly after also
1542                                 //     closing a list of sub-entries. here our main and the last
1543                                 //     main are different.
1544                                 // only in the latter case do we need to output the new main entry.
1545                                 // note that in this case, too, though, the main entry might already
1546                                 // have a sub-entry, or even a sub-sub-entry.
1547                                 if (eit->main() != last.main())
1548                                         xs << xml::StartTag("li", "class='main'") << xhtml_entry.main();
1549                                 if (!xhtml_entry.sub().empty()) {
1550                                         // there's a sub-entry, too
1551                                         xs << xml::CR()
1552                                            << xml::StartTag("ul", "class='subentry'")
1553                                            << xml::StartTag("li", "class='subentry'")
1554                                            << XMLStream::ESCAPE_NONE << xhtml_entry.sub();
1555                                         level = 2;
1556                                         if (!xhtml_entry.subsub().empty()) {
1557                                                 // and a sub-sub-entry
1558                                                 xs << xml::CR()
1559                                                    << xml::StartTag("ul", "class='subsubentry'")
1560                                                    << xml::StartTag("li", "class='subsubentry'")
1561                                                    << XMLStream::ESCAPE_NONE << xhtml_entry.subsub();
1562                                                 level = 3;
1563                                         }
1564                                 }
1565                         }
1566                 }
1567                 // finally, then, we can output the index link itself
1568                 string const parattr = "href='#" + par.magicLabel() + "'";
1569                 xs << (entry_number == 0 ? ":" : ",");
1570                 xs << " " << xml::StartTag("a", parattr)
1571                    << ++entry_number << xml::EndTag("a");
1572                 last = *eit;
1573         }
1574         // now we have to close all the open levels
1575         while (level > 0) {
1576                 xs << xml::EndTag("li") << xml::EndTag("ul") << xml::CR();
1577                 --level;
1578         }
1579         xs << xml::EndTag("div") << xml::CR();
1580         return ods.str();
1581 }
1582
1583 } // namespace lyx