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"
19 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "IndicesList.h"
24 #include "LaTeXFeatures.h"
26 #include "MetricsInfo.h"
27 #include "output_latex.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
36 #include "frontends/alert.h"
41 using namespace lyx::support;
45 /////////////////////////////////////////////////////////////////////
49 ///////////////////////////////////////////////////////////////////////
52 InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
53 : InsetCollapsable(buf), params_(params)
57 int InsetIndex::latex(odocstream & os,
58 OutputParams const & runparams_in) const
60 OutputParams runparams(runparams_in);
61 runparams.inIndexEntry = true;
63 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
64 && params_.index != "idx") {
74 // get contents of InsetText as LaTeX and plaintext
75 odocstringstream ourlatex;
76 InsetText::latex(ourlatex, runparams);
77 odocstringstream ourplain;
78 InsetText::plaintext(ourplain, runparams);
79 docstring latexstr = ourlatex.str();
80 docstring plainstr = ourplain.str();
82 // this will get what follows | if anything does
85 // check for the | separator
86 // FIXME This would go wrong on an escaped "|", but
87 // how far do we want to go here?
88 size_t pos = latexstr.find(from_ascii("|"));
89 if (pos != docstring::npos) {
90 // put the bit after "|" into cmd...
91 cmd = latexstr.substr(pos + 1);
92 // ...and erase that stuff from latexstr
93 latexstr = latexstr.erase(pos);
94 // ...and similarly from plainstr
95 size_t ppos = plainstr.find(from_ascii("|"));
96 if (ppos < plainstr.size())
99 LYXERR0("The `|' separator was not found in the plaintext version!");
102 // Separate the entires and subentries, i.e., split on "!"
103 // FIXME This would do the wrong thing with escaped ! characters
104 std::vector<docstring> const levels =
105 getVectorFromString(latexstr, from_ascii("!"), true);
106 std::vector<docstring> const levels_plain =
107 getVectorFromString(plainstr, from_ascii("!"), true);
109 vector<docstring>::const_iterator it = levels.begin();
110 vector<docstring>::const_iterator end = levels.end();
111 vector<docstring>::const_iterator it2 = levels_plain.begin();
113 for (; it != end; ++it) {
114 // write the separator except the first time
120 // correctly sort macros and formatted strings
121 // if we do find a command, prepend a plain text
122 // version of the content to get sorting right,
123 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
124 // Don't do that if the user entered '@' himself, though.
125 if (contains(*it, '\\') && !contains(*it, '@')) {
126 // Plaintext might return nothing (e.g. for ERTs)
127 docstring const spart =
128 (it2 < levels_plain.end() && !(*it2).empty())
130 // Now we need to validate that all characters in
131 // the sorting part are representable in the current
132 // encoding. If not try the LaTeX macro which might
133 // or might not be a good choice, and issue a warning.
135 for (size_t n = 0; n < spart.size(); ++n) {
137 spart2 += runparams.encoding->latexChar(spart[n]);
138 } catch (EncodingException & /* e */) {
139 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
142 if (spart != spart2 && !runparams.dryrun) {
143 // FIXME: warning should be passed to the error dialog
144 frontend::Alert::warning(_("Index sorting failed"),
145 bformat(_("LyX's automatic index sorting algorithm faced\n"
146 "problems with the entry '%1$s'.\n"
147 "Please specify the sorting of this entry manually, as\n"
148 "explained in the User Guide."), spart));
150 // remove remaining \'s for the sorting part
151 docstring const ppart =
152 subst(spart2, from_ascii("\\"), docstring());
156 docstring const tpart = *it;
158 if (it2 < levels_plain.end())
161 // write the bit that followed "|"
169 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
171 os << "<indexterm><primary>";
172 int const i = InsetText::docbook(os, runparams);
173 os << "</primary></indexterm>";
178 docstring InsetIndex::xhtml(XHTMLStream &, OutputParams const &) const
184 bool InsetIndex::showInsetDialog(BufferView * bv) const
186 bv->showDialog("index", params2string(params_),
187 const_cast<InsetIndex *>(this));
192 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
194 switch (cmd.action) {
196 case LFUN_INSET_MODIFY: {
197 if (cmd.getArg(0) == "changetype") {
198 params_.index = from_utf8(cmd.getArg(1));
201 InsetIndexParams params;
202 InsetIndex::string2params(to_utf8(cmd.argument()), params);
203 params_.index = params.index;
207 case LFUN_INSET_DIALOG_UPDATE:
208 cur.bv().updateDialog("index", params2string(params_));
212 InsetCollapsable::doDispatch(cur, cmd);
218 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
219 FuncStatus & flag) const
221 switch (cmd.action) {
223 case LFUN_INSET_MODIFY:
224 if (cmd.getArg(0) == "changetype") {
225 docstring const newtype = from_utf8(cmd.getArg(1));
226 Buffer const & realbuffer = *buffer().masterBuffer();
227 IndicesList const & indiceslist = realbuffer.params().indiceslist();
228 Index const * index = indiceslist.findShortcut(newtype);
229 flag.setEnabled(index != 0);
231 from_utf8(cmd.getArg(1)) == params_.index);
234 flag.setEnabled(true);
237 case LFUN_INSET_DIALOG_UPDATE: {
238 Buffer const & realbuffer = *buffer().masterBuffer();
239 flag.setEnabled(realbuffer.params().use_indices);
244 return InsetCollapsable::getStatus(cur, cmd, flag);
249 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
251 docstring s = _("Idx");
252 if (decoration() == InsetLayout::CLASSIC)
253 return isOpen(bv) ? s : getNewLabel(s);
255 return getNewLabel(s);
259 ColorCode InsetIndex::labelColor() const
261 if (params_.index.empty() || params_.index == from_ascii("idx"))
262 return InsetCollapsable::labelColor();
264 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index));
266 c = InsetCollapsable::labelColor();
271 docstring InsetIndex::toolTip(BufferView const &, int, int) const
273 docstring tip = _("Index Entry");
274 if (buffer().params().use_indices && !params_.index.empty()) {
275 Buffer const & realbuffer = *buffer().masterBuffer();
276 IndicesList const & indiceslist = realbuffer.params().indiceslist();
278 Index const * index = indiceslist.findShortcut(params_.index);
280 tip += _("unknown type!");
282 tip += index->index();
286 OutputParams rp(&buffer().params().encoding());
287 odocstringstream ods;
288 InsetText::plaintext(ods, rp);
290 return wrapParas(tip);
294 void InsetIndex::write(ostream & os) const
296 os << to_utf8(name());
298 InsetCollapsable::write(os);
302 void InsetIndex::read(Lexer & lex)
305 InsetCollapsable::read(lex);
309 string InsetIndex::params2string(InsetIndexParams const & params)
318 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
320 params = InsetIndexParams();
324 istringstream data(in);
327 lex.setContext("InsetIndex::string2params");
333 void InsetIndex::addToToc(DocIterator const & cpit)
335 DocIterator pit = cpit;
336 pit.push_back(CursorSlice(*this));
337 docstring const item = text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
338 buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, item));
339 // Proceed with the rest of the inset.
340 InsetCollapsable::addToToc(cpit);
344 void InsetIndex::validate(LaTeXFeatures & features) const
346 if (buffer().masterBuffer()->params().use_indices
347 && !params_.index.empty()
348 && params_.index != "idx")
349 features.require("splitidx");
353 docstring InsetIndex::contextMenu(BufferView const &, int, int) const
355 return from_ascii("context-index");
359 bool InsetIndex::hasSettings() const
361 return buffer().masterBuffer()->params().use_indices;
367 /////////////////////////////////////////////////////////////////////
371 ///////////////////////////////////////////////////////////////////////
374 void InsetIndexParams::write(ostream & os) const
378 os << to_utf8(index);
385 void InsetIndexParams::read(Lexer & lex)
388 index = lex.getDocString();
390 index = from_ascii("idx");
394 /////////////////////////////////////////////////////////////////////
398 ///////////////////////////////////////////////////////////////////////
400 InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
401 : InsetCommand(buf, p, "index_print")
405 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
407 static ParamInfo param_info_;
408 if (param_info_.empty()) {
409 param_info_.add("type", ParamInfo::LATEX_OPTIONAL);
410 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
416 docstring InsetPrintIndex::screenLabel() const
418 bool const printall = suffixIs(getCmdName(), '*');
419 bool const multind = buffer().masterBuffer()->params().use_indices;
421 && getParam("type") == from_ascii("idx"))
422 || (getParam("type").empty() && !printall))
424 Buffer const & realbuffer = *buffer().masterBuffer();
425 IndicesList const & indiceslist = realbuffer.params().indiceslist();
426 Index const * index = indiceslist.findShortcut(getParam("type"));
427 if (!index && !printall)
428 return _("Unknown index type!");
429 docstring res = printall ? _("All indices") : index->index();
431 res += " (" + _("non-active") + ")";
432 else if (contains(getCmdName(), "printsubindex"))
433 res += " (" + _("subindex") + ")";
438 bool InsetPrintIndex::isCompatibleCommand(string const & s)
440 return s == "printindex" || s == "printsubindex"
441 || s == "printindex*" || s == "printsubindex*";
445 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
447 switch (cmd.action) {
449 case LFUN_INSET_MODIFY: {
450 if (cmd.argument() == from_ascii("toggle-subindex")) {
451 string cmd = getCmdName();
452 if (contains(cmd, "printindex"))
453 cmd = subst(cmd, "printindex", "printsubindex");
455 cmd = subst(cmd, "printsubindex", "printindex");
458 } else if (cmd.argument() == from_ascii("check-printindex*")) {
459 string cmd = getCmdName();
460 if (suffixIs(cmd, '*'))
463 setParam("type", docstring());
467 InsetCommandParams p(INDEX_PRINT_CODE);
469 InsetCommand::string2params("index_print",
470 to_utf8(cmd.argument()), p);
471 if (p.getCmdName().empty()) {
480 InsetCommand::doDispatch(cur, cmd);
486 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
487 FuncStatus & status) const
489 switch (cmd.action) {
491 case LFUN_INSET_MODIFY: {
492 if (cmd.argument() == from_ascii("toggle-subindex")) {
493 status.setEnabled(buffer().masterBuffer()->params().use_indices);
494 status.setOnOff(contains(getCmdName(), "printsubindex"));
496 } else if (cmd.argument() == from_ascii("check-printindex*")) {
497 status.setEnabled(buffer().masterBuffer()->params().use_indices);
498 status.setOnOff(suffixIs(getCmdName(), '*'));
500 } if (cmd.getArg(0) == "index_print"
501 && cmd.getArg(1) == "CommandInset") {
502 InsetCommandParams p(INDEX_PRINT_CODE);
503 InsetCommand::string2params("index_print",
504 to_utf8(cmd.argument()), p);
505 if (suffixIs(p.getCmdName(), '*')) {
506 status.setEnabled(true);
507 status.setOnOff(false);
510 Buffer const & realbuffer = *buffer().masterBuffer();
511 IndicesList const & indiceslist =
512 realbuffer.params().indiceslist();
513 Index const * index = indiceslist.findShortcut(p["type"]);
514 status.setEnabled(index != 0);
515 status.setOnOff(p["type"] == getParam("type"));
518 return InsetCommand::getStatus(cur, cmd, status);
521 case LFUN_INSET_DIALOG_UPDATE: {
522 status.setEnabled(buffer().masterBuffer()->params().use_indices);
527 return InsetCommand::getStatus(cur, cmd, status);
532 int InsetPrintIndex::latex(odocstream & os, OutputParams const &) const
534 if (!buffer().masterBuffer()->params().use_indices) {
535 if (getParam("type") == from_ascii("idx"))
536 os << "\\printindex{}";
544 void InsetPrintIndex::validate(LaTeXFeatures & features) const
546 features.require("makeidx");
547 if (buffer().masterBuffer()->params().use_indices)
548 features.require("splitidx");
552 docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
554 return buffer().masterBuffer()->params().use_indices ?
555 from_ascii("context-indexprint") : docstring();
559 bool InsetPrintIndex::hasSettings() const
561 return buffer().masterBuffer()->params().use_indices;
565 docstring InsetPrintIndex::xhtml(XHTMLStream &, OutputParams const &) const