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"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/alert.h"
40 using namespace lyx::support;
44 /////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////
51 InsetIndex::InsetIndex(Buffer const & buf, InsetIndexParams const & params)
52 : InsetCollapsable(buf), params_(params)
56 int InsetIndex::latex(odocstream & os,
57 OutputParams const & runparams) const
59 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
60 && params_.index != "idx") {
70 // get contents of InsetText as LaTeX and plaintext
71 odocstringstream ourlatex;
72 InsetText::latex(ourlatex, runparams);
73 odocstringstream ourplain;
74 InsetText::plaintext(ourplain, runparams);
75 docstring latexstr = ourlatex.str();
76 docstring plainstr = ourplain.str();
78 // this will get what follows | if anything does
81 // check for the | separator
82 // FIXME This would go wrong on an escaped "|", but
83 // how far do we want to go here?
84 size_t pos = latexstr.find(from_ascii("|"));
85 if (pos != docstring::npos) {
86 // put the bit after "|" into cmd...
87 cmd = latexstr.substr(pos + 1);
88 // ...and erase that stuff from latexstr
89 latexstr = latexstr.erase(pos);
90 // ...and similarly from plainstr
91 size_t ppos = plainstr.find(from_ascii("|"));
92 if (ppos < plainstr.size())
95 LYXERR0("The `|' separator was not found in the plaintext version!");
98 // Separate the entires and subentries, i.e., split on "!"
99 // FIXME This would do the wrong thing with escaped ! characters
100 std::vector<docstring> const levels =
101 getVectorFromString(latexstr, from_ascii("!"), true);
102 std::vector<docstring> const levels_plain =
103 getVectorFromString(plainstr, from_ascii("!"), true);
105 vector<docstring>::const_iterator it = levels.begin();
106 vector<docstring>::const_iterator end = levels.end();
107 vector<docstring>::const_iterator it2 = levels_plain.begin();
109 for (; it != end; ++it) {
110 // write the separator except the first time
116 // correctly sort macros and formatted strings
117 // if we do find a command, prepend a plain text
118 // version of the content to get sorting right,
119 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
120 // Don't do that if the user entered '@' himself, though.
121 if (contains(*it, '\\') && !contains(*it, '@')) {
122 // Plaintext might return nothing (e.g. for ERTs)
123 docstring const spart =
124 (it2 < levels_plain.end() && !(*it2).empty())
126 // Now we need to validate that all characters in
127 // the sorting part are representable in the current
128 // encoding. If not try the LaTeX macro which might
129 // or might not be a good choice, and issue a warning.
131 for (size_t n = 0; n < spart.size(); ++n) {
133 spart2 += runparams.encoding->latexChar(spart[n]);
134 } catch (EncodingException & /* e */) {
135 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
138 if (spart != spart2 && !runparams.dryrun) {
139 // FIXME: warning should be passed to the error dialog
140 frontend::Alert::warning(_("Index sorting failed"),
141 bformat(_("LyX's automatic index sorting algorithm faced\n"
142 "problems with the entry '%1$s'.\n"
143 "Please specify the sorting of this entry manually, as\n"
144 "explained in the User Guide."), spart));
146 // remove remaining \'s for the sorting part
147 docstring const ppart =
148 subst(spart2, from_ascii("\\"), docstring());
152 docstring const tpart = *it;
154 if (it2 < levels_plain.end())
157 // write the bit that followed "|"
165 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
167 os << "<indexterm><primary>";
168 int const i = InsetText::docbook(os, runparams);
169 os << "</primary></indexterm>";
174 docstring InsetIndex::xhtml(odocstream &, OutputParams const &) const
180 bool InsetIndex::showInsetDialog(BufferView * bv) const
182 bv->showDialog("index", params2string(params_),
183 const_cast<InsetIndex *>(this));
188 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
190 switch (cmd.action) {
192 case LFUN_INSET_MODIFY: {
193 if (cmd.getArg(0) == "changetype") {
194 params_.index = from_utf8(cmd.getArg(1));
195 setLayout(cur.buffer()->params());
198 InsetIndexParams params;
199 InsetIndex::string2params(to_utf8(cmd.argument()), params);
200 params_.index = params.index;
201 setLayout(cur.buffer()->params());
205 case LFUN_INSET_DIALOG_UPDATE:
206 cur.bv().updateDialog("index", params2string(params_));
210 InsetCollapsable::doDispatch(cur, cmd);
216 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
217 FuncStatus & flag) const
219 switch (cmd.action) {
221 case LFUN_INSET_MODIFY:
222 if (cmd.getArg(0) == "changetype") {
223 docstring const newtype = from_utf8(cmd.getArg(1));
224 Buffer const & realbuffer = *buffer().masterBuffer();
225 IndicesList const & indiceslist = realbuffer.params().indiceslist();
226 Index const * index = indiceslist.findShortcut(newtype);
227 flag.setEnabled(index != 0);
229 from_utf8(cmd.getArg(1)) == params_.index);
232 flag.setEnabled(true);
235 case LFUN_INSET_DIALOG_UPDATE: {
236 Buffer const & realbuffer = *buffer().masterBuffer();
237 flag.setEnabled(realbuffer.params().use_indices);
242 return InsetCollapsable::getStatus(cur, cmd, flag);
247 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
249 docstring s = _("Idx");
250 if (decoration() == InsetLayout::CLASSIC)
251 return isOpen(bv) ? s : getNewLabel(s);
253 return getNewLabel(s);
257 docstring InsetIndex::toolTip(BufferView const &, int, int) const
259 docstring tip = _("Index Entry");
260 if (buffer().params().use_indices && !params_.index.empty()) {
261 Buffer const & realbuffer = *buffer().masterBuffer();
262 IndicesList const & indiceslist = realbuffer.params().indiceslist();
264 Index const * index = indiceslist.findShortcut(params_.index);
266 tip += _("unknown type!");
268 tip += index->index();
272 OutputParams rp(&buffer().params().encoding());
273 odocstringstream ods;
274 InsetText::plaintext(ods, rp);
276 return wrapParas(tip);
280 void InsetIndex::write(ostream & os) const
282 os << to_utf8(name());
284 InsetCollapsable::write(os);
288 void InsetIndex::read(Lexer & lex)
291 InsetCollapsable::read(lex);
295 string InsetIndex::params2string(InsetIndexParams const & params)
304 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
306 params = InsetIndexParams();
310 istringstream data(in);
313 lex.setContext("InsetIndex::string2params");
319 void InsetIndex::addToToc(DocIterator const & cpit)
321 DocIterator pit = cpit;
322 pit.push_back(CursorSlice(*this));
323 docstring const item = text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
324 buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, item));
325 // Proceed with the rest of the inset.
326 InsetCollapsable::addToToc(cpit);
330 void InsetIndex::validate(LaTeXFeatures & features) const
332 if (buffer().masterBuffer()->params().use_indices
333 && !params_.index.empty()
334 && params_.index != "idx")
335 features.require("splitidx");
339 docstring InsetIndex::contextMenu(BufferView const &, int, int) const
341 return from_ascii("context-index");
345 bool InsetIndex::hasSettings() const
347 return buffer().masterBuffer()->params().use_indices;
353 /////////////////////////////////////////////////////////////////////
357 ///////////////////////////////////////////////////////////////////////
360 void InsetIndexParams::write(ostream & os) const
364 os << to_utf8(index);
371 void InsetIndexParams::read(Lexer & lex)
374 index = lex.getDocString();
376 index = from_ascii("idx");
380 /////////////////////////////////////////////////////////////////////
384 ///////////////////////////////////////////////////////////////////////
386 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
387 : InsetCommand(p, "index_print")
391 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
393 static ParamInfo param_info_;
394 if (param_info_.empty()) {
395 param_info_.add("type", ParamInfo::LATEX_OPTIONAL);
396 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
402 docstring InsetPrintIndex::screenLabel() const
404 bool const printall = suffixIs(getCmdName(), '*');
405 bool const multind = buffer().masterBuffer()->params().use_indices;
407 && getParam("type") == from_ascii("idx"))
408 || (getParam("type").empty() && !printall))
410 Buffer const & realbuffer = *buffer().masterBuffer();
411 IndicesList const & indiceslist = realbuffer.params().indiceslist();
412 Index const * index = indiceslist.findShortcut(getParam("type"));
413 if (!index && !printall)
414 return _("Unknown index type!");
415 docstring res = printall ? _("All indices") : index->index();
417 res += " (" + _("non-active") + ")";
418 else if (contains(getCmdName(), "printsubindex"))
419 res += " (" + _("subindex") + ")";
424 bool InsetPrintIndex::isCompatibleCommand(string const & s)
426 return s == "printindex" || s == "printsubindex"
427 || s == "printindex*" || s == "printsubindex*";
431 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
433 switch (cmd.action) {
435 case LFUN_INSET_MODIFY: {
436 if (cmd.argument() == from_ascii("toggle-subindex")) {
437 string cmd = getCmdName();
438 if (contains(cmd, "printindex"))
439 cmd = subst(cmd, "printindex", "printsubindex");
441 cmd = subst(cmd, "printsubindex", "printindex");
444 } else if (cmd.argument() == from_ascii("check-printindex*")) {
445 string cmd = getCmdName();
446 if (suffixIs(cmd, '*'))
449 setParam("type", docstring());
453 InsetCommandParams p(INDEX_PRINT_CODE);
455 InsetCommand::string2params("index_print",
456 to_utf8(cmd.argument()), p);
457 if (p.getCmdName().empty()) {
466 InsetCommand::doDispatch(cur, cmd);
472 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
473 FuncStatus & status) const
475 switch (cmd.action) {
477 case LFUN_INSET_MODIFY: {
478 if (cmd.argument() == from_ascii("toggle-subindex")) {
479 status.setEnabled(buffer().masterBuffer()->params().use_indices);
480 status.setOnOff(contains(getCmdName(), "printsubindex"));
482 } else if (cmd.argument() == from_ascii("check-printindex*")) {
483 status.setEnabled(buffer().masterBuffer()->params().use_indices);
484 status.setOnOff(suffixIs(getCmdName(), '*'));
486 } if (cmd.getArg(0) == "index_print"
487 && cmd.getArg(1) == "CommandInset") {
488 InsetCommandParams p(INDEX_PRINT_CODE);
489 InsetCommand::string2params("index_print",
490 to_utf8(cmd.argument()), p);
491 if (suffixIs(p.getCmdName(), '*')) {
492 status.setEnabled(true);
493 status.setOnOff(false);
496 Buffer const & realbuffer = *buffer().masterBuffer();
497 IndicesList const & indiceslist =
498 realbuffer.params().indiceslist();
499 Index const * index = indiceslist.findShortcut(p["type"]);
500 status.setEnabled(index != 0);
501 status.setOnOff(p["type"] == getParam("type"));
504 return InsetCommand::getStatus(cur, cmd, status);
507 case LFUN_INSET_DIALOG_UPDATE: {
508 status.setEnabled(buffer().masterBuffer()->params().use_indices);
513 return InsetCommand::getStatus(cur, cmd, status);
518 int InsetPrintIndex::latex(odocstream & os, OutputParams const &) const
520 if (!buffer().masterBuffer()->params().use_indices) {
521 if (getParam("type") == from_ascii("idx"))
522 os << "\\printindex{}";
530 void InsetPrintIndex::validate(LaTeXFeatures & features) const
532 features.require("makeidx");
533 if (buffer().masterBuffer()->params().use_indices)
534 features.require("splitidx");
538 docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
540 return buffer().masterBuffer()->params().use_indices ?
541 from_ascii("context-indexprint") : docstring();
545 bool InsetPrintIndex::hasSettings() const
547 return buffer().masterBuffer()->params().use_indices;
550 docstring InsetPrintIndex::xhtml(odocstream &, OutputParams const &) const