3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
13 #include "InsetIndex.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
20 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "IndicesList.h"
26 #include "LaTeXFeatures.h"
28 #include "output_latex.h"
29 #include "output_xhtml.h"
31 #include "texstream.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
40 #include "frontends/alert.h"
46 using namespace lyx::support;
50 /////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////
57 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
58 : InsetCollapsible(buf), params_(params)
62 void InsetIndex::latex(otexstream & ios, OutputParams const & runparams_in) const
64 OutputParams runparams(runparams_in);
65 runparams.inIndexEntry = true;
69 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
70 && params_.index != "idx") {
72 os << escape(params_.index);
79 odocstringstream ourlatex;
80 otexstream ots(ourlatex);
81 InsetText::latex(ots, runparams);
82 if (runparams.for_search) {
83 // No need for special handling, if we are only searching for some patterns
84 os << ourlatex.str() << "}";
87 // get contents of InsetText as LaTeX and plaintext
88 odocstringstream ourplain;
89 InsetText::plaintext(ourplain, runparams);
90 // FIXME: do Tex/Row correspondence (I don't currently understand what is
91 // being generated from latexstr below)
92 docstring latexstr = ourlatex.str();
93 docstring plainstr = ourplain.str();
95 // this will get what follows | if anything does
98 // check for the | separator
99 // FIXME This would go wrong on an escaped "|", but
100 // how far do we want to go here?
101 size_t pos = latexstr.find(from_ascii("|"));
102 if (pos != docstring::npos) {
103 // put the bit after "|" into cmd...
104 cmd = latexstr.substr(pos + 1);
105 // ...and erase that stuff from latexstr
106 latexstr = latexstr.erase(pos);
107 // ...and similarly from plainstr
108 size_t ppos = plainstr.find(from_ascii("|"));
109 if (ppos < plainstr.size())
110 plainstr.erase(ppos);
112 LYXERR0("The `|' separator was not found in the plaintext version!");
115 // Separate the entires and subentries, i.e., split on "!"
116 // FIXME This would do the wrong thing with escaped ! characters
117 std::vector<docstring> const levels =
118 getVectorFromString(latexstr, from_ascii("!"), true);
119 std::vector<docstring> const levels_plain =
120 getVectorFromString(plainstr, from_ascii("!"), true);
122 vector<docstring>::const_iterator it = levels.begin();
123 vector<docstring>::const_iterator end = levels.end();
124 vector<docstring>::const_iterator it2 = levels_plain.begin();
126 for (; it != end; ++it) {
127 // write the separator except the first time
133 // correctly sort macros and formatted strings
134 // if we do find a command, prepend a plain text
135 // version of the content to get sorting right,
136 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
137 // Don't do that if the user entered '@' himself, though.
138 if (contains(*it, '\\') && !contains(*it, '@')) {
139 // Plaintext might return nothing (e.g. for ERTs)
140 docstring const spart =
141 (it2 < levels_plain.end() && !(*it2).empty())
143 // Now we need to validate that all characters in
144 // the sorting part are representable in the current
145 // encoding. If not try the LaTeX macro which might
146 // or might not be a good choice, and issue a warning.
147 pair<docstring, docstring> spart_latexed =
148 runparams.encoding->latexString(spart, runparams.dryrun);
149 if (!spart_latexed.second.empty())
150 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
151 if (spart != spart_latexed.first && !runparams.dryrun) {
152 // FIXME: warning should be passed to the error dialog
153 frontend::Alert::warning(_("Index sorting failed"),
154 bformat(_("LyX's automatic index sorting algorithm faced\n"
155 "problems with the entry '%1$s'.\n"
156 "Please specify the sorting of this entry manually, as\n"
157 "explained in the User Guide."), spart));
159 // remove remaining \'s for the sorting part
160 docstring const ppart =
161 subst(spart_latexed.first, from_ascii("\\"), docstring());
165 docstring const tpart = *it;
167 if (it2 < levels_plain.end())
170 // write the bit that followed "|"
176 // In macros with moving arguments, such as \section,
177 // we store the index and output it after the macro (#2154)
178 if (runparams_in.postpone_fragile_stuff)
179 runparams_in.post_macro += os.str();
185 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
187 os << "<indexterm><primary>";
188 int const i = InsetText::docbook(os, runparams);
189 os << "</primary></indexterm>";
194 docstring InsetIndex::xhtml(XHTMLStream & xs, OutputParams const &) const
196 // we just print an anchor, taking the paragraph ID from
197 // our own interior paragraph, which doesn't get printed
198 std::string const magic = paragraphs().front().magicLabel();
199 std::string const attr = "id='" + magic + "'";
200 xs << html::CompTag("a", attr);
205 bool InsetIndex::showInsetDialog(BufferView * bv) const
207 bv->showDialog("index", params2string(params_),
208 const_cast<InsetIndex *>(this));
213 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
215 switch (cmd.action()) {
217 case LFUN_INSET_MODIFY: {
218 if (cmd.getArg(0) == "changetype") {
219 cur.recordUndoInset(this);
220 params_.index = from_utf8(cmd.getArg(1));
223 InsetIndexParams params;
224 InsetIndex::string2params(to_utf8(cmd.argument()), params);
225 cur.recordUndoInset(this);
226 params_.index = params.index;
227 // what we really want here is a TOC update, but that means
228 // a full buffer update
229 cur.forceBufferUpdate();
233 case LFUN_INSET_DIALOG_UPDATE:
234 cur.bv().updateDialog("index", params2string(params_));
238 InsetCollapsible::doDispatch(cur, cmd);
244 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
245 FuncStatus & flag) const
247 switch (cmd.action()) {
249 case LFUN_INSET_MODIFY:
250 if (cmd.getArg(0) == "changetype") {
251 docstring const newtype = from_utf8(cmd.getArg(1));
252 Buffer const & realbuffer = *buffer().masterBuffer();
253 IndicesList const & indiceslist = realbuffer.params().indiceslist();
254 Index const * index = indiceslist.findShortcut(newtype);
255 flag.setEnabled(index != 0);
257 from_utf8(cmd.getArg(1)) == params_.index);
260 return InsetCollapsible::getStatus(cur, cmd, flag);
262 case LFUN_INSET_DIALOG_UPDATE: {
263 Buffer const & realbuffer = *buffer().masterBuffer();
264 flag.setEnabled(realbuffer.params().use_indices);
269 return InsetCollapsible::getStatus(cur, cmd, flag);
274 ColorCode InsetIndex::labelColor() const
276 if (params_.index.empty() || params_.index == from_ascii("idx"))
277 return InsetCollapsible::labelColor();
279 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index));
281 c = InsetCollapsible::labelColor();
286 docstring InsetIndex::toolTip(BufferView const &, int, int) const
288 docstring tip = _("Index Entry");
289 if (buffer().params().use_indices && !params_.index.empty()) {
290 Buffer const & realbuffer = *buffer().masterBuffer();
291 IndicesList const & indiceslist = realbuffer.params().indiceslist();
293 Index const * index = indiceslist.findShortcut(params_.index);
295 tip += _("unknown type!");
297 tip += index->index();
301 return toolTipText(tip);
305 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
307 InsetLayout const & il = getLayout();
308 docstring label = translateIfPossible(il.labelstring());
310 if (buffer().params().use_indices && !params_.index.empty()) {
311 Buffer const & realbuffer = *buffer().masterBuffer();
312 IndicesList const & indiceslist = realbuffer.params().indiceslist();
314 Index const * index = indiceslist.findShortcut(params_.index);
316 label += _("unknown type!");
318 label += index->index();
322 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
324 return getNewLabel(label);
328 void InsetIndex::write(ostream & os) const
330 os << to_utf8(layoutName());
332 InsetCollapsible::write(os);
336 void InsetIndex::read(Lexer & lex)
339 InsetCollapsible::read(lex);
343 string InsetIndex::params2string(InsetIndexParams const & params)
352 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
354 params = InsetIndexParams();
358 istringstream data(in);
361 lex.setContext("InsetIndex::string2params");
367 void InsetIndex::addToToc(DocIterator const & cpit, bool output_active,
368 UpdateType utype, TocBackend & backend) const
370 DocIterator pit = cpit;
371 pit.push_back(CursorSlice(const_cast<InsetIndex &>(*this)));
373 string type = "index";
374 if (buffer().masterBuffer()->params().use_indices)
375 type += ":" + to_utf8(params_.index);
376 // this is unlikely to be terribly long
377 text().forOutliner(str, INT_MAX);
378 TocBuilder & b = backend.builder(type);
379 b.pushItem(pit, str, output_active);
380 // Proceed with the rest of the inset.
381 InsetCollapsible::addToToc(cpit, output_active, utype, backend);
386 void InsetIndex::validate(LaTeXFeatures & features) const
388 if (buffer().masterBuffer()->params().use_indices
389 && !params_.index.empty()
390 && params_.index != "idx")
391 features.require("splitidx");
392 InsetCollapsible::validate(features);
396 string InsetIndex::contextMenuName() const
398 return "context-index";
402 bool InsetIndex::hasSettings() const
404 return buffer().masterBuffer()->params().use_indices;
410 /////////////////////////////////////////////////////////////////////
414 ///////////////////////////////////////////////////////////////////////
417 void InsetIndexParams::write(ostream & os) const
421 os << to_utf8(index);
428 void InsetIndexParams::read(Lexer & lex)
431 index = lex.getDocString();
433 index = from_ascii("idx");
437 /////////////////////////////////////////////////////////////////////
441 ///////////////////////////////////////////////////////////////////////
443 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
444 : InsetCommand(buf, p)
448 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
450 static ParamInfo param_info_;
451 if (param_info_.empty()) {
452 param_info_.add("type", ParamInfo::LATEX_OPTIONAL,
453 ParamInfo::HANDLING_ESCAPE);
454 param_info_.add("name", ParamInfo::LATEX_OPTIONAL,
455 ParamInfo::HANDLING_LATEXIFY);
456 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
462 docstring InsetPrintIndex::screenLabel() const
464 bool const printall = suffixIs(getCmdName(), '*');
465 bool const multind = buffer().masterBuffer()->params().use_indices;
467 && getParam("type") == from_ascii("idx"))
468 || (getParam("type").empty() && !printall))
470 Buffer const & realbuffer = *buffer().masterBuffer();
471 IndicesList const & indiceslist = realbuffer.params().indiceslist();
472 Index const * index = indiceslist.findShortcut(getParam("type"));
473 if (!index && !printall)
474 return _("Unknown index type!");
475 docstring res = printall ? _("All indexes") : index->index();
477 res += " (" + _("non-active") + ")";
478 else if (contains(getCmdName(), "printsubindex"))
479 res += " (" + _("subindex") + ")";
484 bool InsetPrintIndex::isCompatibleCommand(string const & s)
486 return s == "printindex" || s == "printsubindex"
487 || s == "printindex*" || s == "printsubindex*";
491 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
493 switch (cmd.action()) {
495 case LFUN_INSET_MODIFY: {
496 if (cmd.argument() == from_ascii("toggle-subindex")) {
497 string scmd = getCmdName();
498 if (contains(scmd, "printindex"))
499 scmd = subst(scmd, "printindex", "printsubindex");
501 scmd = subst(scmd, "printsubindex", "printindex");
505 } else if (cmd.argument() == from_ascii("check-printindex*")) {
506 string scmd = getCmdName();
507 if (suffixIs(scmd, '*'))
511 setParam("type", docstring());
515 InsetCommandParams p(INDEX_PRINT_CODE);
517 InsetCommand::string2params(to_utf8(cmd.argument()), p);
518 if (p.getCmdName().empty()) {
519 cur.noScreenUpdate();
528 InsetCommand::doDispatch(cur, cmd);
534 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
535 FuncStatus & status) const
537 switch (cmd.action()) {
539 case LFUN_INSET_MODIFY: {
540 if (cmd.argument() == from_ascii("toggle-subindex")) {
541 status.setEnabled(buffer().masterBuffer()->params().use_indices);
542 status.setOnOff(contains(getCmdName(), "printsubindex"));
544 } else if (cmd.argument() == from_ascii("check-printindex*")) {
545 status.setEnabled(buffer().masterBuffer()->params().use_indices);
546 status.setOnOff(suffixIs(getCmdName(), '*'));
548 } if (cmd.getArg(0) == "index_print"
549 && cmd.getArg(1) == "CommandInset") {
550 InsetCommandParams p(INDEX_PRINT_CODE);
551 InsetCommand::string2params(to_utf8(cmd.argument()), p);
552 if (suffixIs(p.getCmdName(), '*')) {
553 status.setEnabled(true);
554 status.setOnOff(false);
557 Buffer const & realbuffer = *buffer().masterBuffer();
558 IndicesList const & indiceslist =
559 realbuffer.params().indiceslist();
560 Index const * index = indiceslist.findShortcut(p["type"]);
561 status.setEnabled(index != 0);
562 status.setOnOff(p["type"] == getParam("type"));
565 return InsetCommand::getStatus(cur, cmd, status);
568 case LFUN_INSET_DIALOG_UPDATE: {
569 status.setEnabled(buffer().masterBuffer()->params().use_indices);
574 return InsetCommand::getStatus(cur, cmd, status);
579 void InsetPrintIndex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
581 Index const * index =
582 buffer().masterParams().indiceslist().findShortcut(getParam("type"));
584 setParam("name", index->index());
588 void InsetPrintIndex::latex(otexstream & os, OutputParams const & runparams_in) const
590 if (!buffer().masterBuffer()->params().use_indices) {
591 if (getParam("type") == from_ascii("idx"))
592 os << "\\printindex" << termcmd;
595 OutputParams runparams = runparams_in;
596 os << getCommand(runparams);
600 void InsetPrintIndex::validate(LaTeXFeatures & features) const
602 features.require("makeidx");
603 if (buffer().masterBuffer()->params().use_indices)
604 features.require("splitidx");
605 InsetCommand::validate(features);
609 string InsetPrintIndex::contextMenuName() const
611 return buffer().masterBuffer()->params().use_indices ?
612 "context-indexprint" : string();
616 bool InsetPrintIndex::hasSettings() const
618 return buffer().masterBuffer()->params().use_indices;
624 void parseItem(docstring & s, bool for_output)
626 // this does not yet check for escaped things
627 size_type loc = s.find(from_ascii("@"));
628 if (loc != string::npos) {
634 loc = s.find(from_ascii("|"));
635 if (loc != string::npos)
640 void extractSubentries(docstring const & entry, docstring & main,
641 docstring & sub1, docstring & sub2)
645 size_type const loc = entry.find(from_ascii(" ! "));
646 if (loc == string::npos)
649 main = trim(entry.substr(0, loc));
650 size_t const locend = loc + 3;
651 size_type const loc2 = entry.find(from_ascii(" ! "), locend);
652 if (loc2 == string::npos) {
653 sub1 = trim(entry.substr(locend));
655 sub1 = trim(entry.substr(locend, loc2 - locend));
656 sub2 = trim(entry.substr(loc2 + 3));
667 IndexEntry(docstring const & s, DocIterator const & d)
670 extractSubentries(s, main, sub, subsub);
671 parseItem(main, false);
672 parseItem(sub, false);
673 parseItem(subsub, false);
676 bool equal(IndexEntry const & rhs) const
678 return main == rhs.main && sub == rhs.sub && subsub == rhs.subsub;
681 bool same_sub(IndexEntry const & rhs) const
683 return main == rhs.main && sub == rhs.sub;
686 bool same_main(IndexEntry const & rhs) const
688 return main == rhs.main;
697 bool operator<(IndexEntry const & lhs, IndexEntry const & rhs)
699 int comp = compare_no_case(lhs.main, rhs.main);
701 comp = compare_no_case(lhs.sub, rhs.sub);
703 comp = compare_no_case(lhs.subsub, rhs.subsub);
710 docstring InsetPrintIndex::xhtml(XHTMLStream &, OutputParams const & op) const
712 BufferParams const & bp = buffer().masterBuffer()->params();
714 // we do not presently support multiple indices, so we refuse to print
715 // anything but the main index, so as not to generate multiple indices.
716 // NOTE Multiple index support would require some work. The reason
717 // is that the TOC does not know about multiple indices. Either it would
718 // need to be told about them (not a bad idea), or else the index entries
719 // would need to be collected differently, say, during validation.
720 if (bp.use_indices && getParam("type") != from_ascii("idx"))
723 shared_ptr<Toc const> toc = buffer().tocBackend().toc("index");
727 // Collect the index entries in a form we can use them.
728 Toc::const_iterator it = toc->begin();
729 Toc::const_iterator const en = toc->end();
730 vector<IndexEntry> entries;
731 for (; it != en; ++it)
733 entries.push_back(IndexEntry(it->str(), it->dit()));
736 // not very likely that all the index entries are in notes or
740 stable_sort(entries.begin(), entries.end());
742 Layout const & lay = bp.documentClass().htmlTOCLayout();
743 string const & tocclass = lay.defaultCSSClass();
744 string const tocattr = "class='index " + tocclass + "'";
746 // we'll use our own stream, because we are going to defer everything.
747 // that's how we deal with the fact that we're probably inside a standard
748 // paragraph, and we don't want to be.
749 odocstringstream ods;
752 xs << html::StartTag("div", tocattr);
753 xs << html::StartTag(lay.htmltag(), lay.htmlattr())
754 << translateIfPossible(from_ascii("Index"),
755 op.local_font->language()->lang())
756 << html::EndTag(lay.htmltag());
757 xs << html::StartTag("ul", "class='main'");
760 vector<IndexEntry>::const_iterator eit = entries.begin();
761 vector<IndexEntry>::const_iterator const een = entries.end();
762 // tracks whether we are already inside a main entry (1),
763 // a sub-entry (2), or a sub-sub-entry (3). see below for the
766 // the last one we saw
768 int entry_number = -1;
769 for (; eit != een; ++eit) {
770 Paragraph const & par = eit->dit.innerParagraph();
771 if (entry_number == -1 || !eit->equal(last)) {
772 if (entry_number != -1) {
773 // not the first time through the loop, so
774 // close last entry or entries, depending.
776 // close this sub-sub-entry
777 xs << html::EndTag("li") << html::CR();
778 // is this another sub-sub-entry within the same sub-entry?
779 if (!eit->same_sub(last)) {
781 xs << html::EndTag("ul") << html::CR();
785 // the point of the second test here is that we might get
786 // here two ways: (i) by falling through from above; (ii) because,
787 // though the sub-entry hasn't changed, the sub-sub-entry has,
788 // which means that it is the first sub-sub-entry within this
789 // sub-entry. In that case, we do not want to close anything.
790 if (level == 2 && !eit->same_sub(last)) {
792 xs << html::EndTag("li") << html::CR();
793 // is this another sub-entry with the same main entry?
794 if (!eit->same_main(last)) {
796 xs << html::EndTag("ul") << html::CR();
800 // again, we can get here two ways: from above, or because we have
801 // found the first sub-entry. in the latter case, we do not want to
803 if (level == 1 && !eit->same_main(last)) {
805 xs << html::EndTag("li") << html::CR();
809 // we'll be starting new entries
812 // We need to use our own stream, since we will have to
813 // modify what we get back.
814 odocstringstream ent;
815 XHTMLStream entstream(ent);
816 OutputParams ours = op;
818 par.simpleLyXHTMLOnePar(buffer(), entstream, ours, dummy);
820 // these will contain XHTML versions of the main entry, etc
821 // remember that everything will already have been escaped,
822 // so we'll need to use NextRaw() during output.
826 extractSubentries(ent.str(), main, sub, subsub);
827 parseItem(main, true);
828 parseItem(sub, true);
829 parseItem(subsub, true);
832 // another subsubentry
833 xs << html::StartTag("li", "class='subsubentry'")
834 << XHTMLStream::ESCAPE_NONE << subsub;
835 } else if (level == 2) {
836 // there are two ways we can be here:
837 // (i) we can actually be inside a sub-entry already and be about
838 // to output the first sub-sub-entry. in this case, our sub
839 // and the last sub will be the same.
840 // (ii) we can just have closed a sub-entry, possibly after also
841 // closing a list of sub-sub-entries. here our sub and the last
842 // sub are different.
843 // only in the latter case do we need to output the new sub-entry.
844 // note that in this case, too, though, the sub-entry might already
845 // have a sub-sub-entry.
846 if (eit->sub != last.sub)
847 xs << html::StartTag("li", "class='subentry'")
848 << XHTMLStream::ESCAPE_NONE << sub;
849 if (!subsub.empty()) {
850 // it's actually a subsubentry, so we need to start that list
852 << html::StartTag("ul", "class='subsubentry'")
853 << html::StartTag("li", "class='subsubentry'")
854 << XHTMLStream::ESCAPE_NONE << subsub;
858 // there are also two ways we can be here:
859 // (i) we can actually be inside an entry already and be about
860 // to output the first sub-entry. in this case, our main
861 // and the last main will be the same.
862 // (ii) we can just have closed an entry, possibly after also
863 // closing a list of sub-entries. here our main and the last
864 // main are different.
865 // only in the latter case do we need to output the new main entry.
866 // note that in this case, too, though, the main entry might already
867 // have a sub-entry, or even a sub-sub-entry.
868 if (eit->main != last.main)
869 xs << html::StartTag("li", "class='main'") << main;
871 // there's a sub-entry, too
873 << html::StartTag("ul", "class='subentry'")
874 << html::StartTag("li", "class='subentry'")
875 << XHTMLStream::ESCAPE_NONE << sub;
877 if (!subsub.empty()) {
878 // and a sub-sub-entry
880 << html::StartTag("ul", "class='subsubentry'")
881 << html::StartTag("li", "class='subsubentry'")
882 << XHTMLStream::ESCAPE_NONE << subsub;
888 // finally, then, we can output the index link itself
889 string const parattr = "href='#" + par.magicLabel() + "'";
890 xs << (entry_number == 0 ? ":" : ",");
891 xs << " " << html::StartTag("a", parattr)
892 << ++entry_number << html::EndTag("a");
895 // now we have to close all the open levels
897 xs << html::EndTag("li") << html::EndTag("ul") << html::CR();
900 xs << html::EndTag("div") << html::CR();