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 bool InsetIndex::showInsetDialog(BufferView * bv) const
176 bv->showDialog("index", params2string(params_),
177 const_cast<InsetIndex *>(this));
182 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
184 LYXERR0( cmd.action);
185 LYXERR0( cmd.getArg(0));
186 switch (cmd.action) {
188 case LFUN_INSET_MODIFY: {
189 if (cmd.getArg(0) == "changetype") {
190 params_.index = from_utf8(cmd.getArg(1));
191 setLayout(cur.buffer()->params());
194 InsetIndexParams params;
195 InsetIndex::string2params(to_utf8(cmd.argument()), params);
196 params_.index = params.index;
197 setLayout(cur.buffer()->params());
201 case LFUN_INSET_DIALOG_UPDATE:
202 cur.bv().updateDialog("index", params2string(params_));
206 InsetCollapsable::doDispatch(cur, cmd);
212 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
213 FuncStatus & flag) const
215 LYXERR0( cmd.action);
216 LYXERR0( cmd.getArg(0));
217 switch (cmd.action) {
219 case LFUN_INSET_MODIFY:
220 if (cmd.getArg(0) == "changetype") {
221 docstring const newtype = from_utf8(cmd.getArg(1));
222 Buffer const & realbuffer = *buffer().masterBuffer();
223 IndicesList const & indiceslist = realbuffer.params().indiceslist();
224 Index const * index = indiceslist.findShortcut(newtype);
225 flag.setEnabled(index != 0);
227 from_utf8(cmd.getArg(1)) == params_.index);
230 flag.setEnabled(true);
233 case LFUN_INSET_DIALOG_UPDATE: {
234 Buffer const & realbuffer = *buffer().masterBuffer();
235 flag.setEnabled(realbuffer.params().use_indices);
240 return InsetCollapsable::getStatus(cur, cmd, flag);
245 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
247 docstring s = _("Idx");
248 if (decoration() == InsetLayout::CLASSIC)
249 return isOpen(bv) ? s : getNewLabel(s);
251 return getNewLabel(s);
255 docstring InsetIndex::toolTip(BufferView const &, int, int) const
257 docstring tip = _("Index Entry");
258 if (buffer().params().use_indices && !params_.index.empty()) {
259 Buffer const & realbuffer = *buffer().masterBuffer();
260 IndicesList const & indiceslist = realbuffer.params().indiceslist();
262 Index const * index = indiceslist.findShortcut(params_.index);
264 tip += _("unknown type!");
266 tip += index->index();
270 OutputParams rp(&buffer().params().encoding());
271 odocstringstream ods;
272 InsetText::plaintext(ods, rp);
274 // shorten it if necessary
275 if (tip.size() > 200)
276 tip = tip.substr(0, 200) + "...";
281 void InsetIndex::write(ostream & os) const
283 os << to_utf8(name());
285 InsetCollapsable::write(os);
289 void InsetIndex::read(Lexer & lex)
292 InsetCollapsable::read(lex);
296 string InsetIndex::params2string(InsetIndexParams const & params)
305 void InsetIndex::string2params(string const & in, InsetIndexParams & params)
307 params = InsetIndexParams();
311 istringstream data(in);
314 lex.setContext("InsetIndex::string2params");
320 void InsetIndex::addToToc(DocIterator const & cpit)
322 DocIterator pit = cpit;
323 pit.push_back(CursorSlice(*this));
324 docstring const item = text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
325 buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, item));
326 // Proceed with the rest of the inset.
327 InsetCollapsable::addToToc(cpit);
331 void InsetIndex::validate(LaTeXFeatures & features) const
333 if (buffer().masterBuffer()->params().use_indices
334 && !params_.index.empty()
335 && params_.index != "idx")
336 features.require("splitidx");
340 docstring InsetIndex::contextMenu(BufferView const &, int, int) const
342 return from_ascii("context-index");
346 bool InsetIndex::hasSettings() const
348 return buffer().masterBuffer()->params().use_indices;
354 /////////////////////////////////////////////////////////////////////
358 ///////////////////////////////////////////////////////////////////////
361 void InsetIndexParams::write(ostream & os) const
365 os << to_utf8(index);
372 void InsetIndexParams::read(Lexer & lex)
375 index = lex.getDocString();
377 index = from_ascii("idx");
381 /////////////////////////////////////////////////////////////////////
385 ///////////////////////////////////////////////////////////////////////
387 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
388 : InsetCommand(p, "index_print")
392 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
394 static ParamInfo param_info_;
395 if (param_info_.empty()) {
396 param_info_.add("type", ParamInfo::LATEX_OPTIONAL);
397 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
403 docstring InsetPrintIndex::screenLabel() const
405 if ((!buffer().masterBuffer()->params().use_indices
406 && getParam("type") == from_ascii("idx"))
407 || getParam("type").empty())
409 Buffer const & realbuffer = *buffer().masterBuffer();
410 IndicesList const & indiceslist = realbuffer.params().indiceslist();
411 Index const * index = indiceslist.findShortcut(getParam("type"));
413 return _("Unknown index type!");
414 docstring res = index->index();
415 if (!buffer().masterBuffer()->params().use_indices)
416 res += " (" + _("non-active") + ")";
421 void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
423 switch (cmd.action) {
425 case LFUN_INSET_MODIFY: {
426 InsetCommandParams p(INDEX_PRINT_CODE);
428 InsetCommand::string2params("index_print",
429 to_utf8(cmd.argument()), p);
430 if (p.getCmdName().empty()) {
434 setParam("type", p["type"]);
439 InsetCommand::doDispatch(cur, cmd);
445 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
446 FuncStatus & status) const
448 switch (cmd.action) {
450 case LFUN_INSET_MODIFY: {
451 if (cmd.getArg(0) == "index_print"
452 && cmd.getArg(1) == "CommandInset") {
453 InsetCommandParams p(INDEX_PRINT_CODE);
454 InsetCommand::string2params("index_print",
455 to_utf8(cmd.argument()), p);
456 Buffer const & realbuffer = *buffer().masterBuffer();
457 IndicesList const & indiceslist =
458 realbuffer.params().indiceslist();
459 Index const * index = indiceslist.findShortcut(p["type"]);
460 status.setEnabled(index != 0);
461 status.setOnOff(p["type"] == getParam("type"));
464 return InsetCommand::getStatus(cur, cmd, status);
467 case LFUN_INSET_DIALOG_UPDATE: {
468 Buffer const & realbuffer = *buffer().masterBuffer();
469 status.setEnabled(realbuffer.params().use_indices);
474 return InsetCommand::getStatus(cur, cmd, status);
479 int InsetPrintIndex::latex(odocstream & os, OutputParams const &) const
481 if (!buffer().masterBuffer()->params().use_indices) {
482 if (getParam("type") == from_ascii("idx"))
483 os << "\\printindex{}";
491 void InsetPrintIndex::validate(LaTeXFeatures & features) const
493 features.require("makeidx");
494 if (buffer().masterBuffer()->params().use_indices)
495 features.require("splitidx");
499 docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
501 return buffer().masterBuffer()->params().use_indices ?
502 from_ascii("context-indexprint") : docstring();
506 bool InsetPrintIndex::hasSettings() const
508 return buffer().masterBuffer()->params().use_indices;