]> git.lyx.org Git - features.git/blob - src/insets/InsetIndex.cpp
DocBook: merge code duplicates for HTML and CALS tables.
[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
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "ColorSet.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "Encoding.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "IndicesList.h"
25 #include "Language.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "output_latex.h"
29 #include "output_xhtml.h"
30 #include "xml.h"
31 #include "texstream.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
34
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include "frontends/alert.h"
42
43 #include <algorithm>
44 #include <set>
45 #include <ostream>
46
47 #include <QThreadStorage>
48
49 using namespace std;
50 using namespace lyx::support;
51
52 namespace lyx {
53
54 /////////////////////////////////////////////////////////////////////
55 //
56 // InsetIndex
57 //
58 ///////////////////////////////////////////////////////////////////////
59
60
61 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
62         : InsetCollapsible(buf), params_(params)
63 {}
64
65
66 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
67 {
68         OutputParams runparams(runparams_in);
69         runparams.inIndexEntry = true;
70
71         otexstringstream os;
72
73         if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
74                 && params_.index != "idx") {
75                 os << "\\sindex[";
76                 os << escape(params_.index);
77                 os << "]{";
78         } else {
79                 os << "\\index";
80                 os << '{';
81         }
82
83         // Get the LaTeX output from InsetText. We need to deconstruct this later
84         // in order to check if we need to generate a sorting key
85         odocstringstream ourlatex;
86         otexstream ots(ourlatex);
87         InsetText::latex(ots, runparams);
88         if (runparams.for_searchAdv != OutputParams::NoSearch) {
89                 // No need for special handling, if we are only searching for some patterns
90                 os << ourlatex.str() << "}";
91                 return;
92         }
93
94         // For the sorting key, we use the plaintext version
95         odocstringstream ourplain;
96         InsetText::plaintext(ourplain, runparams);
97
98         // These are the LaTeX and plaintext representations
99         docstring latexstr = ourlatex.str();
100         docstring plainstr = ourplain.str();
101
102         // This will get what follows | if anything does,
103         // the command (e.g., see, textbf) for pagination
104         // formatting
105         docstring cmd;
106
107         // Check for the | separator to strip the cmd.
108         // This goes wrong on an escaped "|", but as the escape
109         // character can be changed in style files, we cannot
110         // prevent that.
111         size_t pos = latexstr.find(from_ascii("|"));
112         if (pos != docstring::npos) {
113                 // Put the bit after "|" into cmd...
114                 cmd = latexstr.substr(pos + 1);
115                 // ...and erase that stuff from latexstr
116                 latexstr = latexstr.erase(pos);
117                 // ...as well as from plainstr
118                 size_t ppos = plainstr.find(from_ascii("|"));
119                 if (ppos < plainstr.size())
120                         plainstr.erase(ppos);
121                 else
122                         LYXERR0("The `|' separator was not found in the plaintext version!");
123         }
124
125         // Separate the entries and subentries, i.e., split on "!".
126         // This goes wrong on an escaped "!", but as the escape
127         // character can be changed in style files, we cannot
128         // prevent that.
129         std::vector<docstring> const levels =
130                         getVectorFromString(latexstr, from_ascii("!"), true);
131         std::vector<docstring> const levels_plain =
132                         getVectorFromString(plainstr, from_ascii("!"), true);
133
134         vector<docstring>::const_iterator it = levels.begin();
135         vector<docstring>::const_iterator end = levels.end();
136         vector<docstring>::const_iterator it2 = levels_plain.begin();
137         bool first = true;
138         for (; it != end; ++it) {
139                 // The separator needs to be put back when
140                 // writing the levels, except for the first level
141                 if (!first)
142                         os << '!';
143                 else
144                         first = false;
145
146                 // Now here comes the reason for this whole procedure:
147                 // We try to correctly sort macros and formatted strings.
148                 // If we find a command, prepend a plain text
149                 // version of the content to get sorting right,
150                 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}.
151                 // We do this on all levels.
152                 // We don't do it if the level already contains a '@', though.
153                 if (contains(*it, '\\') && !contains(*it, '@')) {
154                         // Plaintext might return nothing (e.g. for ERTs).
155                         // In that case, we use LaTeX.
156                         docstring const spart =
157                                         (it2 < levels_plain.end() && !(*it2).empty())
158                                         ? *it2 : *it;
159                         // Now we need to validate that all characters in
160                         // the sorting part are representable in the current
161                         // encoding. If not try the LaTeX macro which might
162                         // or might not be a good choice, and issue a warning.
163                         pair<docstring, docstring> spart_latexed =
164                                         runparams.encoding->latexString(spart, runparams.dryrun);
165                         if (!spart_latexed.second.empty())
166                                 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
167                         if (spart != spart_latexed.first && !runparams.dryrun) {
168                                 // FIXME: warning should be passed to the error dialog
169                                 frontend::Alert::warning(_("Index sorting failed"),
170                                                          bformat(_("LyX's automatic index sorting algorithm faced\n"
171                                                                    "problems with the entry '%1$s'.\n"
172                                                                    "Please specify the sorting of this entry manually, as\n"
173                                                                    "explained in the User Guide."), spart));
174                         }
175                         // Remove remaining \'s from the sort key
176                         docstring ppart = subst(spart_latexed.first, from_ascii("\\"), docstring());
177                         // Plain quotes need to be escaped, however (#10649), as this
178                         // is the default escape character
179                         ppart = subst(ppart, from_ascii("\""), from_ascii("\\\""));
180
181                         // Now insert the sortkey, separated by '@'.
182                         os << ppart;
183                         os << '@';
184                 }
185                 // Insert the actual level text
186                 docstring const tpart = *it;
187                 os << tpart;
188                 if (it2 < levels_plain.end())
189                         ++it2;
190         }
191         // At last, re-insert the command, separated by "|"
192         if (!cmd.empty()) {
193                 os << "|" << cmd;
194         }
195         os << '}';
196
197         // In macros with moving arguments, such as \section,
198         // we store the index and output it after the macro (#2154)
199         if (runparams_in.postpone_fragile_stuff)
200                 runparams_in.post_macro += os.str();
201         else
202                 ios << os.release();
203 }
204
205
206 void InsetIndex::docbook(XMLStream & xs, OutputParams const & runparams) const
207 {
208         // Get the content of the inset as LaTeX, as some things may be encoded as ERT (like {}).
209         // TODO: if there is an ERT within the index term, its conversion should be tried, in case it becomes useful;
210         //  otherwise, ERTs should become comments. For now, they are just copied as-is, which is barely satisfactory.
211         odocstringstream odss;
212         otexstream ots(odss);
213         InsetText::latex(ots, runparams);
214         docstring latexString = trim(odss.str());
215
216         // Check whether there are unsupported things. @ is supported, but only for sorting, without specific formatting.
217         if (latexString.find(from_utf8("@\\")) != lyx::docstring::npos) {
218                 docstring error = from_utf8("Unsupported feature: an index entry contains an @\\. "
219                                                                         "Complete entry: \"") + latexString + from_utf8("\"");
220                 LYXERR0(error);
221                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
222         }
223
224         // Handle several indices (indicated in the inset instead of the raw latexString).
225         docstring indexType = from_utf8("");
226         if (buffer().masterBuffer()->params().use_indices) {
227                 indexType += " type=\"" + params_.index + "\"";
228         }
229
230         // Split the string into its main constituents: terms, and command (see, see also, range).
231         size_t positionVerticalBar = latexString.find(from_ascii("|")); // What comes before | is (sub)(sub)entries.
232         docstring indexTerms = latexString.substr(0, positionVerticalBar);
233         docstring command;
234         if (positionVerticalBar != lyx::docstring::npos) {
235                 command =  latexString.substr(positionVerticalBar + 1);
236         }
237
238         // Handle sorting issues, with @.
239         vector<docstring> sortingElements = getVectorFromString(indexTerms, from_ascii("@"), false);
240         docstring sortAs;
241         if (sortingElements.size() == 2) {
242                 sortAs = sortingElements[0];
243                 indexTerms = sortingElements[1];
244         }
245
246         // Handle primary, secondary, and tertiary terms (entries, subentries, and subsubentries, for LaTeX).
247         vector<docstring> terms = getVectorFromString(indexTerms, from_ascii("!"), false);
248
249         // Handle ranges. Happily, (| and |) can only be at the end of the string!
250         bool hasStartRange = latexString.find(from_ascii("|(")) != lyx::docstring::npos;
251         bool hasEndRange = latexString.find(from_ascii("|)")) != lyx::docstring::npos;
252         if (hasStartRange || hasEndRange) {
253                 // Remove the ranges from the command if they do not appear at the beginning.
254                 size_t index = 0;
255                 while ((index = command.find(from_utf8("|("), index)) != std::string::npos)
256                         command.erase(index, 1);
257                 index = 0;
258                 while ((index = command.find(from_utf8("|)"), index)) != std::string::npos)
259                         command.erase(index, 1);
260
261                 // Remove the ranges when they are the only vertical bar in the complete string.
262                 if (command[0] == '(' || command[0] == ')')
263                         command.erase(0, 1);
264         }
265
266         // Handle see and seealso. As "see" is a prefix of "seealso", the order of the comparisons is important.
267         // Both commands are mutually exclusive!
268         docstring see = from_utf8("");
269         vector<docstring> seeAlsoes;
270         if (command.substr(0, 3) == "see") {
271                 // Unescape brackets.
272                 size_t index = 0;
273                 while ((index = command.find(from_utf8("\\{"), index)) != std::string::npos)
274                         command.erase(index, 1);
275                 index = 0;
276                 while ((index = command.find(from_utf8("\\}"), index)) != std::string::npos)
277                         command.erase(index, 1);
278
279                 // Retrieve the part between brackets, and remove the complete seealso.
280                 size_t positionOpeningBracket = command.find(from_ascii("{"));
281                 size_t positionClosingBracket = command.find(from_ascii("}"));
282                 docstring list = command.substr(positionOpeningBracket + 1, positionClosingBracket - positionOpeningBracket - 1);
283
284                 // Parse the list of referenced entries (or a single one for see).
285                 if (command.substr(0, 7) == "seealso") {
286                         seeAlsoes = getVectorFromString(list, from_ascii(","), false);
287                 } else {
288                         see = list;
289
290                         if (see.find(from_ascii(",")) != std::string::npos) {
291                                 docstring error = from_utf8("Several index terms found as \"see\"! Only one is acceptable. "
292                                                                                         "Complete entry: \"") + latexString + from_utf8("\"");
293                                 LYXERR0(error);
294                                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
295                         }
296                 }
297
298                 // Remove the complete see/seealso from the commands, in case there is something else to parse.
299                 command = command.substr(positionClosingBracket + 1);
300         }
301
302         // Some parts of the strings are not parsed, as they do not have anything matching in DocBook: things like
303         // formatting the entry or the page number, other strings for sorting. https://wiki.lyx.org/Tips/Indexing
304         // If there are such things in the index entry, then this code may miserably fail. For example, for "Peter|(textbf",
305         // no range will be detected.
306         // TODO: Could handle formatting as significance="preferred"?
307         if (!command.empty()) {
308                 docstring error = from_utf8("Unsupported feature: an index entry contains a | with an unsupported command, ")
309                                           + command + from_utf8(". ") + from_utf8("Complete entry: \"") + latexString + from_utf8("\"");
310                 LYXERR0(error);
311                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
312         }
313
314     // Write all of this down.
315         if (terms.empty() && !hasEndRange) {
316                 docstring error = from_utf8("No index term found! Complete entry: \"") + latexString + from_utf8("\"");
317                 LYXERR0(error);
318                 xs << XMLStream::ESCAPE_NONE << (from_utf8("<!-- Output Error: ") + error + from_utf8(" -->\n"));
319         } else {
320                 // Generate the attributes for ranges. It is based on the terms that are indexed, but the ID must be unique
321                 // to this indexing area (xml::cleanID does not guarantee this: for each call with the same arguments,
322                 // the same legal ID is produced; here, as the input would be the same, the output must be, by design).
323                 // Hence the thread-local storage, as the numbers must strictly be unique, and thus cannot be shared across
324                 // a paragraph (making the solution used for HTML worthless). This solution is very similar to the one used in
325                 // xml::cleanID.
326                 // indexType can only be used for singular and startofrange types!
327                 docstring attrs;
328                 if (!hasStartRange && !hasEndRange) {
329                         attrs = indexType;
330                 } else {
331                         // Append an ID if uniqueness is not guaranteed across the document.
332                         static QThreadStorage<set<docstring>> tKnownTermLists;
333                         static QThreadStorage<int> tID;
334
335                         set<docstring> &knownTermLists = tKnownTermLists.localData();
336                         int &ID = tID.localData();
337
338                         if (!tID.hasLocalData()) {
339                                 tID.localData() = 0;
340                         }
341
342                         // Modify the index terms to add the unique ID if needed.
343                         docstring newIndexTerms = indexTerms;
344                         if (knownTermLists.find(indexTerms) != knownTermLists.end()) {
345                                 newIndexTerms += from_ascii(string("-") + to_string(ID));
346
347                                 // Only increment for the end of range, so that the same number is used for the start of range.
348                                 if (hasEndRange) {
349                                         ID++;
350                                 }
351                         }
352
353                         // Term list not yet known: add it to the set AFTER the end of range. After
354                         if (knownTermLists.find(indexTerms) == knownTermLists.end() && hasEndRange) {
355                                 knownTermLists.insert(indexTerms);
356                         }
357
358                         // Generate the attributes.
359                         docstring id = xml::cleanID(newIndexTerms);
360                         if (hasStartRange) {
361                                 attrs = indexType + " class=\"startofrange\" xml:id=\"" + id + "\"";
362                         } else {
363                                 attrs = " class=\"endofrange\" startref=\"" + id + "\"";
364                         }
365                 }
366
367                 // Handle the index terms (including the specific index for this entry).
368                 if (hasEndRange) {
369                         xs << xml::CompTag("indexterm", attrs);
370                 } else {
371                         xs << xml::StartTag("indexterm", attrs);
372                         if (!terms.empty()) { // hasEndRange has no content.
373                                 docstring attr;
374                                 if (!sortAs.empty()) {
375                                         attr = from_utf8("sortas='") + sortAs + from_utf8("'");
376                                 }
377
378                                 xs << xml::StartTag("primary", attr);
379                                 xs << terms[0];
380                                 xs << xml::EndTag("primary");
381                         }
382                         if (terms.size() > 1) {
383                                 xs << xml::StartTag("secondary");
384                                 xs << terms[1];
385                                 xs << xml::EndTag("secondary");
386                         }
387                         if (terms.size() > 2) {
388                                 xs << xml::StartTag("tertiary");
389                                 xs << terms[2];
390                                 xs << xml::EndTag("tertiary");
391                         }
392
393                         // Handle see and see also.
394                         if (!see.empty()) {
395                                 xs << xml::StartTag("see");
396                                 xs << see;
397                                 xs << xml::EndTag("see");
398                         }
399
400                         if (!seeAlsoes.empty()) {
401                                 for (auto &entry : seeAlsoes) {
402                                         xs << xml::StartTag("seealso");
403                                         xs << entry;
404                                         xs << xml::EndTag("seealso");
405                                 }
406                         }
407
408                         // Close the entry.
409                         xs << xml::EndTag("indexterm");
410                 }
411         }
412 }
413
414
415 docstring InsetIndex::xhtml(XMLStream & xs, OutputParams const &) const
416 {
417         // we just print an anchor, taking the paragraph ID from
418         // our own interior paragraph, which doesn't get printed
419         std::string const magic = paragraphs().front().magicLabel();
420         std::string const attr = "id='" + magic + "'";
421         xs << xml::CompTag("a", attr);
422         return docstring();
423 }
424
425
426 bool InsetIndex::showInsetDialog(BufferView * bv) const
427 {
428         bv->showDialog("index", params2string(params_),
429                         const_cast<InsetIndex *>(this));
430         return true;
431 }
432
433
434 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
435 {
436         switch (cmd.action()) {
437
438         case LFUN_INSET_MODIFY: {
439                 if (cmd.getArg(0) == "changetype") {
440                         cur.recordUndoInset(this);
441                         params_.index = from_utf8(cmd.getArg(1));
442                         break;
443                 }
444                 InsetIndexParams params;
445                 InsetIndex::string2params(to_utf8(cmd.argument()), params);
446                 cur.recordUndoInset(this);
447                 params_.index = params.index;
448                 // what we really want here is a TOC update, but that means
449                 // a full buffer update
450                 cur.forceBufferUpdate();
451                 break;
452         }
453
454         case LFUN_INSET_DIALOG_UPDATE:
455                 cur.bv().updateDialog("index", params2string(params_));
456                 break;
457
458         default:
459                 InsetCollapsible::doDispatch(cur, cmd);
460                 break;
461         }
462 }
463
464
465 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
466                 FuncStatus & flag) const
467 {
468         switch (cmd.action()) {
469
470         case LFUN_INSET_MODIFY:
471                 if (cmd.getArg(0) == "changetype") {
472                         docstring const newtype = from_utf8(cmd.getArg(1));
473                         Buffer const & realbuffer = *buffer().masterBuffer();
474                         IndicesList const & indiceslist = realbuffer.params().indiceslist();
475                         Index const * index = indiceslist.findShortcut(newtype);
476                         flag.setEnabled(index != 0);
477                         flag.setOnOff(
478                                 from_utf8(cmd.getArg(1)) == params_.index);
479                         return true;
480                 }
481                 return InsetCollapsible::getStatus(cur, cmd, flag);
482
483         case LFUN_INSET_DIALOG_UPDATE: {
484                 Buffer const & realbuffer = *buffer().masterBuffer();
485                 flag.setEnabled(realbuffer.params().use_indices);
486                 return true;
487         }
488
489         default:
490                 return InsetCollapsible::getStatus(cur, cmd, flag);
491         }
492 }
493
494
495 ColorCode InsetIndex::labelColor() const
496 {
497         if (params_.index.empty() || params_.index == from_ascii("idx"))
498                 return InsetCollapsible::labelColor();
499         // FIXME UNICODE
500         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index)
501                                             + "@" + buffer().fileName().absFileName());
502         if (c == Color_none)
503                 c = InsetCollapsible::labelColor();
504         return c;
505 }
506
507
508 docstring InsetIndex::toolTip(BufferView const &, int, int) const
509 {
510         docstring tip = _("Index Entry");
511         if (buffer().params().use_indices && !params_.index.empty()) {
512                 Buffer const & realbuffer = *buffer().masterBuffer();
513                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
514                 tip += " (";
515                 Index const * index = indiceslist.findShortcut(params_.index);
516                 if (!index)
517                         tip += _("unknown type!");
518                 else
519                         tip += index->index();
520                 tip += ")";
521         }
522         tip += ": ";
523         return toolTipText(tip);
524 }
525
526
527 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
528 {
529         InsetLayout const & il = getLayout();
530         docstring label = translateIfPossible(il.labelstring());
531
532         if (buffer().params().use_indices && !params_.index.empty()) {
533                 Buffer const & realbuffer = *buffer().masterBuffer();
534                 IndicesList const & indiceslist = realbuffer.params().indiceslist();
535                 label += " (";
536                 Index const * index = indiceslist.findShortcut(params_.index);
537                 if (!index)
538                         label += _("unknown type!");
539                 else
540                         label += index->index();
541                 label += ")";
542         }
543
544         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
545                 return label;
546         return getNewLabel(label);
547 }
548
549
550 void InsetIndex::write(ostream & os) const
551 {
552         os << to_utf8(layoutName());
553         params_.write(os);
554         InsetCollapsible::write(os);
555 }
556
557
558 void InsetIndex::read(Lexer & lex)
559 {
560         params_.read(lex);
561         InsetCollapsible::read(lex);
562 }
563
564
565 string InsetIndex::params2string(InsetIndexParams const & params)
566 {
567         ostringstream data;
568         data << "index";
569         params.write(data);
570         return data.str();
571 }
572
573
574 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
575 {
576         params = InsetIndexParams();
577         if (in.empty())
578                 return;
579
580         istringstream data(in);
581         Lexer lex;
582         lex.setStream(data);
583         lex.setContext("InsetIndex::string2params");
584         lex >> "index";
585         params.read(lex);
586 }
587
588
589 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
590                                                   UpdateType utype, TocBackend & backend) const
591 {
592         DocIterator pit = cpit;
593         pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
594         docstring str;
595         string type = "index";
596         if (buffer().masterBuffer()->params().use_indices)
597                 type += ":" + to_utf8(params_.index);
598         // this is unlikely to be terribly long
599         text().forOutliner(str, INT_MAX);
600         TocBuilder & b = backend.builder(type);
601         b.pushItem(pit, str, output_active);
602         // Proceed with the rest of the inset.
603         InsetCollapsible::addToToc(cpit, output_active, utype, backend);
604         b.pop();
605 }
606
607
608 void InsetIndex::validate(LaTeXFeatures & features) const
609 {
610         if (buffer().masterBuffer()->params().use_indices
611             && !params_.index.empty()
612             && params_.index != "idx")
613                 features.require("splitidx");
614         InsetCollapsible::validate(features);
615 }
616
617
618 string InsetIndex::contextMenuName() const
619 {
620         return "context-index";
621 }
622
623
624 bool InsetIndex::hasSettings() const
625 {
626         return buffer().masterBuffer()->params().use_indices;
627 }
628
629
630
631
632 /////////////////////////////////////////////////////////////////////
633 //
634 // InsetIndexParams
635 //
636 ///////////////////////////////////////////////////////////////////////
637
638
639 void InsetIndexParams::write(ostream & os) const
640 {
641         os << ' ';
642         if (!index.empty())
643                 os << to_utf8(index);
644         else
645                 os << "idx";
646         os << '\n';
647 }
648
649
650 void InsetIndexParams::read(Lexer & lex)
651 {
652         if (lex.eatLine())
653                 index = lex.getDocString();
654         else
655                 index = from_ascii("idx");
656 }
657
658
659 /////////////////////////////////////////////////////////////////////
660 //
661 // InsetPrintIndex
662 //
663 ///////////////////////////////////////////////////////////////////////
664
665 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
666         : InsetCommand(buf, p)
667 {}
668
669
670 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
671 {
672         static ParamInfo param_info_;
673         if (param_info_.empty()) {
674                 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
675                                 ParamInfo::HANDLING_ESCAPE);
676                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
677                                 ParamInfo::HANDLING_LATEXIFY);
678                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
679         }
680         return param_info_;
681 }
682
683
684 docstring InsetPrintIndex::screenLabel() const
685 {
686         bool const printall = suffixIs(getCmdName(), '*');
687         bool const multind = buffer().masterBuffer()->params().use_indices;
688         if ((!multind
689              && getParam("type") == from_ascii("idx"))
690             || (getParam("type").empty() && !printall))
691                 return _("Index");
692         Buffer const & realbuffer = *buffer().masterBuffer();
693         IndicesList const & indiceslist = realbuffer.params().indiceslist();
694         Index const * index = indiceslist.findShortcut(getParam("type"));
695         if (!index && !printall)
696                 return _("Unknown index type!");
697         docstring res = printall ? _("All indexes") : index->index();
698         if (!multind)
699                 res += " (" + _("non-active") + ")";
700         else if (contains(getCmdName(), "printsubindex"))
701                 res += " (" + _("subindex") + ")";
702         return res;
703 }
704
705
706 bool InsetPrintIndex::isCompatibleCommand(string const & s)
707 {
708         return s == "printindex" || s == "printsubindex"
709                 || s == "printindex*" || s == "printsubindex*";
710 }
711
712
713 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
714 {
715         switch (cmd.action()) {
716
717         case LFUN_INSET_MODIFY: {
718                 if (cmd.argument() == from_ascii("toggle-subindex")) {
719                         string scmd = getCmdName();
720                         if (contains(scmd, "printindex"))
721                                 scmd = subst(scmd, "printindex", "printsubindex");
722                         else
723                                 scmd = subst(scmd, "printsubindex", "printindex");
724                         cur.recordUndo();
725                         setCmdName(scmd);
726                         break;
727                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
728                         string scmd = getCmdName();
729                         if (suffixIs(scmd, '*'))
730                                 break;
731                         scmd += '*';
732                         cur.recordUndo();
733                         setParam("type", docstring());
734                         setCmdName(scmd);
735                         break;
736                 }
737                 InsetCommandParams p(INDEX_PRINT_CODE);
738                 // FIXME UNICODE
739                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
740                 if (p.getCmdName().empty()) {
741                         cur.noScreenUpdate();
742                         break;
743                 }
744                 cur.recordUndo();
745                 setParams(p);
746                 break;
747         }
748
749         default:
750                 InsetCommand::doDispatch(cur, cmd);
751                 break;
752         }
753 }
754
755
756 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
757         FuncStatus & status) const
758 {
759         switch (cmd.action()) {
760
761         case LFUN_INSET_MODIFY: {
762                 if (cmd.argument() == from_ascii("toggle-subindex")) {
763                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
764                         status.setOnOff(contains(getCmdName(), "printsubindex"));
765                         return true;
766                 } else if (cmd.argument() == from_ascii("check-printindex*")) {
767                         status.setEnabled(buffer().masterBuffer()->params().use_indices);
768                         status.setOnOff(suffixIs(getCmdName(), '*'));
769                         return true;
770                 } if (cmd.getArg(0) == "index_print"
771                     && cmd.getArg(1) == "CommandInset") {
772                         InsetCommandParams p(INDEX_PRINT_CODE);
773                         InsetCommand::string2params(to_utf8(cmd.argument()), p);
774                         if (suffixIs(p.getCmdName(), '*')) {
775                                 status.setEnabled(true);
776                                 status.setOnOff(false);
777                                 return true;
778                         }
779                         Buffer const & realbuffer = *buffer().masterBuffer();
780                         IndicesList const & indiceslist =
781                                 realbuffer.params().indiceslist();
782                         Index const * index = indiceslist.findShortcut(p["type"]);
783                         status.setEnabled(index != 0);
784                         status.setOnOff(p["type"] == getParam("type"));
785                         return true;
786                 } else
787                         return InsetCommand::getStatus(cur, cmd, status);
788         }
789
790         case LFUN_INSET_DIALOG_UPDATE: {
791                 status.setEnabled(buffer().masterBuffer()->params().use_indices);
792                 return true;
793         }
794
795         default:
796                 return InsetCommand::getStatus(cur, cmd, status);
797         }
798 }
799
800
801 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
802 {
803         Index const * index =
804                 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
805         if (index)
806                 setParam("name", index->index());
807 }
808
809
810 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
811 {
812         if (!buffer().masterBuffer()->params().use_indices) {
813                 if (getParam("type") == from_ascii("idx"))
814                         os << "\\printindex" << termcmd;
815                 return;
816         }
817         OutputParams runparams = runparams_in;
818         os << getCommand(runparams);
819 }
820
821
822 void InsetPrintIndex::validate(LaTeXFeatures & features) const
823 {
824         features.require("makeidx");
825         if (buffer().masterBuffer()->params().use_indices)
826                 features.require("splitidx");
827         InsetCommand::validate(features);
828 }
829
830
831 string InsetPrintIndex::contextMenuName() const
832 {
833         return buffer().masterBuffer()->params().use_indices ?
834                 "context-indexprint" : string();
835 }
836
837
838 bool InsetPrintIndex::hasSettings() const
839 {
840         return buffer().masterBuffer()->params().use_indices;
841 }
842
843
844 namespace {
845
846 void parseItem(docstring & s, bool for_output)
847 {
848         // this does not yet check for escaped things
849         size_type loc = s.find(from_ascii("@"));
850         if (loc != string::npos) {
851                 if (for_output)
852                         s.erase(0, loc + 1);
853                 else
854                         s.erase(loc);
855         }
856         loc = s.find(from_ascii("|"));
857         if (loc != string::npos)
858                 s.erase(loc);
859 }
860
861
862 void extractSubentries(docstring const & entry, docstring & main,
863                 docstring & sub1, docstring & sub2)
864 {
865         if (entry.empty())
866                 return;
867         size_type const loc = entry.find(from_ascii(" ! "));
868         if (loc == string::npos)
869                 main = entry;
870         else {
871                 main = trim(entry.substr(0, loc));
872                 size_t const locend = loc + 3;
873                 size_type const loc2 = entry.find(from_ascii(" ! "), locend);
874                 if (loc2 == string::npos) {
875                         sub1 = trim(entry.substr(locend));
876                 } else {
877                         sub1 = trim(entry.substr(locend, loc2 - locend));
878                         sub2 = trim(entry.substr(loc2 + 3));
879                 }
880         }
881 }
882
883
884 struct IndexEntry
885 {
886         IndexEntry()
887         {}
888
889         IndexEntry(docstring const & s, DocIterator const & d)
890                         : dit(d)
891         {
892                 extractSubentries(s, main, sub, subsub);
893                 parseItem(main, false);
894                 parseItem(sub, false);
895                 parseItem(subsub, false);
896         }
897
898         bool equal(IndexEntry const & rhs) const
899         {
900                 return main == rhs.main && sub == rhs.sub && subsub == rhs.subsub;
901         }
902
903         bool same_sub(IndexEntry const & rhs) const
904         {
905                 return main == rhs.main && sub == rhs.sub;
906         }
907
908         bool same_main(IndexEntry const & rhs) const
909         {
910                 return main == rhs.main;
911         }
912
913         docstring main;
914         docstring sub;
915         docstring subsub;
916         DocIterator dit;
917 };
918
919 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
920 {
921         int comp = compare_no_case(lhs.main, rhs.main);
922         if (comp == 0)
923                 comp = compare_no_case(lhs.sub, rhs.sub);
924         if (comp == 0)
925                 comp = compare_no_case(lhs.subsub, rhs.subsub);
926         return (comp < 0);
927 }
928
929 } // namespace
930
931
932 docstring InsetPrintIndex::xhtml(XMLStream &, OutputParams const & op) const
933 {
934         BufferParams const & bp = buffer().masterBuffer()->params();
935
936         // we do not presently support multiple indices, so we refuse to print
937         // anything but the main index, so as not to generate multiple indices.
938         // NOTE Multiple index support would require some work. The reason
939         // is that the TOC does not know about multiple indices. Either it would
940         // need to be told about them (not a bad idea), or else the index entries
941         // would need to be collected differently, say, during validation.
942         if (bp.use_indices && getParam("type") != from_ascii("idx"))
943                 return docstring();
944
945         shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
946         if (toc->empty())
947                 return docstring();
948
949         // Collect the index entries in a form we can use them.
950         Toc::const_iterator it = toc->begin();
951         Toc::const_iterator const en = toc->end();
952         vector<IndexEntry> entries;
953         for (; it != en; ++it)
954                 if (it->isOutput())
955                         entries.push_back(IndexEntry(it->str(), it->dit()));
956
957         if (entries.empty())
958                 // not very likely that all the index entries are in notes or
959                 // whatever, but....
960                 return docstring();
961
962         stable_sort(entries.begin(), entries.end());
963
964         Layout const & lay = bp.documentClass().htmlTOCLayout();
965         string const & tocclass = lay.defaultCSSClass();
966         string const tocattr = "class='index " + tocclass + "'";
967
968         // we'll use our own stream, because we are going to defer everything.
969         // that's how we deal with the fact that we're probably inside a standard
970         // paragraph, and we don't want to be.
971         odocstringstream ods;
972         XMLStream xs(ods);
973
974         xs << xml::StartTag("div", tocattr);
975         xs << xml::StartTag(lay.htmltag(), lay.htmlattr())
976                  << translateIfPossible(from_ascii("Index"),
977                                   op.local_font->language()->lang())
978                  << xml::EndTag(lay.htmltag());
979         xs << xml::StartTag("ul", "class='main'");
980         Font const dummy;
981
982         vector<IndexEntry>::const_iterator eit = entries.begin();
983         vector<IndexEntry>::const_iterator const een = entries.end();
984         // tracks whether we are already inside a main entry (1),
985         // a sub-entry (2), or a sub-sub-entry (3). see below for the
986         // details.
987         int level = 1;
988         // the last one we saw
989         IndexEntry last;
990         int entry_number = -1;
991         for (; eit != een; ++eit) {
992                 Paragraph const & par = eit->dit.innerParagraph();
993                 if (entry_number == -1 || !eit->equal(last)) {
994                         if (entry_number != -1) {
995                                 // not the first time through the loop, so
996                                 // close last entry or entries, depending.
997                                 if (level == 3) {
998                                         // close this sub-sub-entry
999                                         xs << xml::EndTag("li") << xml::CR();
1000                                         // is this another sub-sub-entry within the same sub-entry?
1001                                         if (!eit->same_sub(last)) {
1002                                                 // close this level
1003                                                 xs << xml::EndTag("ul") << xml::CR();
1004                                                 level = 2;
1005                                         }
1006                                 }
1007                                 // the point of the second test here is that we might get
1008                                 // here two ways: (i) by falling through from above; (ii) because,
1009                                 // though the sub-entry hasn't changed, the sub-sub-entry has,
1010                                 // which means that it is the first sub-sub-entry within this
1011                                 // sub-entry. In that case, we do not want to close anything.
1012                                 if (level == 2 && !eit->same_sub(last)) {
1013                                         // close sub-entry
1014                                         xs << xml::EndTag("li") << xml::CR();
1015                                         // is this another sub-entry with the same main entry?
1016                                         if (!eit->same_main(last)) {
1017                                                 // close this level
1018                                                 xs << xml::EndTag("ul") << xml::CR();
1019                                                 level = 1;
1020                                         }
1021                                 }
1022                                 // again, we can get here two ways: from above, or because we have
1023                                 // found the first sub-entry. in the latter case, we do not want to
1024                                 // close the entry.
1025                                 if (level == 1 && !eit->same_main(last)) {
1026                                         // close entry
1027                                         xs << xml::EndTag("li") << xml::CR();
1028                                 }
1029                         }
1030
1031                         // we'll be starting new entries
1032                         entry_number = 0;
1033
1034                         // We need to use our own stream, since we will have to
1035                         // modify what we get back.
1036                         odocstringstream ent;
1037                         XMLStream entstream(ent);
1038                         OutputParams ours = op;
1039                         ours.for_toc = true;
1040                         par.simpleLyXHTMLOnePar(buffer(), entstream, ours, dummy);
1041
1042                         // these will contain XHTML versions of the main entry, etc
1043                         // remember that everything will already have been escaped,
1044                         // so we'll need to use NextRaw() during output.
1045                         docstring main;
1046                         docstring sub;
1047                         docstring subsub;
1048                         extractSubentries(ent.str(), main, sub, subsub);
1049                         parseItem(main, true);
1050                         parseItem(sub, true);
1051                         parseItem(subsub, true);
1052
1053                         if (level == 3) {
1054                                 // another subsubentry
1055                                 xs << xml::StartTag("li", "class='subsubentry'")
1056                                    << XMLStream::ESCAPE_NONE << subsub;
1057                         } else if (level == 2) {
1058                                 // there are two ways we can be here:
1059                                 // (i) we can actually be inside a sub-entry already and be about
1060                                 //     to output the first sub-sub-entry. in this case, our sub
1061                                 //     and the last sub will be the same.
1062                                 // (ii) we can just have closed a sub-entry, possibly after also
1063                                 //     closing a list of sub-sub-entries. here our sub and the last
1064                                 //     sub are different.
1065                                 // only in the latter case do we need to output the new sub-entry.
1066                                 // note that in this case, too, though, the sub-entry might already
1067                                 // have a sub-sub-entry.
1068                                 if (eit->sub != last.sub)
1069                                         xs << xml::StartTag("li", "class='subentry'")
1070                                            << XMLStream::ESCAPE_NONE << sub;
1071                                 if (!subsub.empty()) {
1072                                         // it's actually a subsubentry, so we need to start that list
1073                                         xs << xml::CR()
1074                                            << xml::StartTag("ul", "class='subsubentry'")
1075                                            << xml::StartTag("li", "class='subsubentry'")
1076                                            << XMLStream::ESCAPE_NONE << subsub;
1077                                         level = 3;
1078                                 }
1079                         } else {
1080                                 // there are also two ways we can be here:
1081                                 // (i) we can actually be inside an entry already and be about
1082                                 //     to output the first sub-entry. in this case, our main
1083                                 //     and the last main will be the same.
1084                                 // (ii) we can just have closed an entry, possibly after also
1085                                 //     closing a list of sub-entries. here our main and the last
1086                                 //     main are different.
1087                                 // only in the latter case do we need to output the new main entry.
1088                                 // note that in this case, too, though, the main entry might already
1089                                 // have a sub-entry, or even a sub-sub-entry.
1090                                 if (eit->main != last.main)
1091                                         xs << xml::StartTag("li", "class='main'") << main;
1092                                 if (!sub.empty()) {
1093                                         // there's a sub-entry, too
1094                                         xs << xml::CR()
1095                                            << xml::StartTag("ul", "class='subentry'")
1096                                            << xml::StartTag("li", "class='subentry'")
1097                                            << XMLStream::ESCAPE_NONE << sub;
1098                                         level = 2;
1099                                         if (!subsub.empty()) {
1100                                                 // and a sub-sub-entry
1101                                                 xs << xml::CR()
1102                                                    << xml::StartTag("ul", "class='subsubentry'")
1103                                                    << xml::StartTag("li", "class='subsubentry'")
1104                                                    << XMLStream::ESCAPE_NONE << subsub;
1105                                                 level = 3;
1106                                         }
1107                                 }
1108                         }
1109                 }
1110                 // finally, then, we can output the index link itself
1111                 string const parattr = "href='#" + par.magicLabel() + "'";
1112                 xs << (entry_number == 0 ? ":" : ",");
1113                 xs << " " << xml::StartTag("a", parattr)
1114                    << ++entry_number << xml::EndTag("a");
1115                 last = *eit;
1116         }
1117         // now we have to close all the open levels
1118         while (level > 0) {
1119                 xs << xml::EndTag("li") << xml::EndTag("ul") << xml::CR();
1120                 --level;
1121         }
1122         xs << xml::EndTag("div") << xml::CR();
1123         return ods.str();
1124 }
1125
1126 } // namespace lyx