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"
18 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "IndicesList.h"
23 #include "LaTeXFeatures.h"
25 #include "MetricsInfo.h"
27 #include "TocBackend.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
34 #include "frontends/alert.h"
39 using namespace lyx::support;
43 /////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////
50 InsetIndex::InsetIndex(Buffer const & buf, InsetIndexParams const & params)
51 : InsetCollapsable(buf), params_(params)
55 int InsetIndex::latex(odocstream & os,
56 OutputParams const & runparams) const
58 if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
59 && params_.index != "idx") {
69 // get contents of InsetText as LaTeX and plaintext
70 odocstringstream ourlatex;
71 InsetText::latex(ourlatex, runparams);
72 odocstringstream ourplain;
73 InsetText::plaintext(ourplain, runparams);
74 docstring latexstr = ourlatex.str();
75 docstring plainstr = ourplain.str();
77 // this will get what follows | if anything does
80 // check for the | separator
81 // FIXME This would go wrong on an escaped "|", but
82 // how far do we want to go here?
83 size_t pos = latexstr.find(from_ascii("|"));
84 if (pos != docstring::npos) {
85 // put the bit after "|" into cmd...
86 cmd = latexstr.substr(pos + 1);
87 // ...and erase that stuff from latexstr
88 latexstr = latexstr.erase(pos);
89 // ...and similarly from plainstr
90 size_t ppos = plainstr.find(from_ascii("|"));
91 if (ppos < plainstr.size())
94 LYXERR0("The `|' separator was not found in the plaintext version!");
97 // Separate the entires and subentries, i.e., split on "!"
98 // FIXME This would do the wrong thing with escaped ! characters
99 std::vector<docstring> const levels =
100 getVectorFromString(latexstr, from_ascii("!"), true);
101 std::vector<docstring> const levels_plain =
102 getVectorFromString(plainstr, from_ascii("!"), true);
104 vector<docstring>::const_iterator it = levels.begin();
105 vector<docstring>::const_iterator end = levels.end();
106 vector<docstring>::const_iterator it2 = levels_plain.begin();
108 for (; it != end; ++it) {
109 // write the separator except the first time
115 // correctly sort macros and formatted strings
116 // if we do find a command, prepend a plain text
117 // version of the content to get sorting right,
118 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
119 // Don't do that if the user entered '@' himself, though.
120 if (contains(*it, '\\') && !contains(*it, '@')) {
121 // Plaintext might return nothing (e.g. for ERTs)
122 docstring const spart =
123 (it2 < levels_plain.end() && !(*it2).empty())
125 // Now we need to validate that all characters in
126 // the sorting part are representable in the current
127 // encoding. If not try the LaTeX macro which might
128 // or might not be a good choice, and issue a warning.
130 for (size_t n = 0; n < spart.size(); ++n) {
132 spart2 += runparams.encoding->latexChar(spart[n]);
133 } catch (EncodingException & /* e */) {
134 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
137 if (spart != spart2 && !runparams.dryrun) {
138 // FIXME: warning should be passed to the error dialog
139 frontend::Alert::warning(_("Index sorting failed"),
140 bformat(_("LyX's automatic index sorting algorithm faced\n"
141 "problems with the entry '%1$s'.\n"
142 "Please specify the sorting of this entry manually, as\n"
143 "explained in the User Guide."), spart));
145 // remove remaining \'s for the sorting part
146 docstring const ppart =
147 subst(spart2, from_ascii("\\"), docstring());
151 docstring const tpart = *it;
153 if (it2 < levels_plain.end())
156 // write the bit that followed "|"
164 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
166 os << "<indexterm><primary>";
167 int const i = InsetText::docbook(os, runparams);
168 os << "</primary></indexterm>";
173 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY: {
178 if (cmd.getArg(0) == "changetype") {
179 params_.index = from_utf8(cmd.getArg(1));
180 setLayout(cur.buffer()->params());
186 InsetCollapsable::doDispatch(cur, cmd);
192 bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
193 FuncStatus & flag) const
195 switch (cmd.action) {
197 case LFUN_INSET_MODIFY:
198 if (cmd.getArg(0) == "changetype") {
199 docstring const newtype = from_utf8(cmd.getArg(1));
200 Buffer const & realbuffer = *buffer().masterBuffer();
201 IndicesList const & indiceslist = realbuffer.params().indiceslist();
202 Index const * index = indiceslist.findShortcut(newtype);
203 flag.setEnabled(index != 0);
205 from_utf8(cmd.getArg(1)) == params_.index);
210 return InsetCollapsable::getStatus(cur, cmd, flag);
215 docstring const InsetIndex::buttonLabel(BufferView const & bv) const
217 docstring s = _("Idx");
218 if (decoration() == InsetLayout::CLASSIC)
219 return isOpen(bv) ? s : getNewLabel(s);
221 return getNewLabel(s);
225 docstring InsetIndex::toolTip(BufferView const &, int, int) const
227 docstring tip = _("Index Entry");
228 if (buffer().params().use_indices && !params_.index.empty()) {
229 Buffer const & realbuffer = *buffer().masterBuffer();
230 IndicesList const & indiceslist = realbuffer.params().indiceslist();
232 Index const * index = indiceslist.findShortcut(params_.index);
234 tip += _("unknown type!");
236 tip += index->index();
240 OutputParams rp(&buffer().params().encoding());
241 odocstringstream ods;
242 InsetText::plaintext(ods, rp);
244 // shorten it if necessary
245 if (tip.size() > 200)
246 tip = tip.substr(0, 200) + "...";
251 void InsetIndex::write(ostream & os) const
253 os << to_utf8(name());
255 InsetCollapsable::write(os);
259 void InsetIndex::read(Lexer & lex)
262 InsetCollapsable::read(lex);
266 void InsetIndex::addToToc(DocIterator const & cpit)
268 DocIterator pit = cpit;
269 pit.push_back(CursorSlice(*this));
270 docstring const item = text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
271 buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, item));
272 // Proceed with the rest of the inset.
273 InsetCollapsable::addToToc(cpit);
277 void InsetIndex::validate(LaTeXFeatures & features) const
279 if (buffer().masterBuffer()->params().use_indices
280 && !params_.index.empty()
281 && params_.index != "idx")
282 features.require("splitidx");
286 docstring InsetIndex::contextMenu(BufferView const &, int, int) const
288 return from_ascii("context-index");
292 void InsetIndexParams::write(ostream & os) const
296 os << to_utf8(index);
303 void InsetIndexParams::read(Lexer & lex)
306 index = lex.getDocString();
308 index = from_ascii("idx");
312 /////////////////////////////////////////////////////////////////////
316 ///////////////////////////////////////////////////////////////////////
318 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
319 : InsetCommand(p, "index_print")
323 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
325 static ParamInfo param_info_;
326 if (param_info_.empty()) {
327 param_info_.add("type", ParamInfo::LATEX_OPTIONAL);
328 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
334 docstring InsetPrintIndex::screenLabel() const
336 if ((!buffer().masterBuffer()->params().use_indices
337 && getParam("type") == from_ascii("idx"))
338 || getParam("type").empty())
340 Buffer const & realbuffer = *buffer().masterBuffer();
341 IndicesList const & indiceslist = realbuffer.params().indiceslist();
342 Index const * index = indiceslist.findShortcut(getParam("type"));
344 return _("Unknown index type!");
345 docstring res = index->index();
346 if (!buffer().masterBuffer()->params().use_indices)
347 res += " (" + _("non-active") + ")";
352 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
353 FuncStatus & status) const
355 switch (cmd.action) {
357 case LFUN_INSET_MODIFY: {
358 InsetCommandParams p(INDEX_PRINT_CODE);
359 InsetCommand::string2params("index_print", to_utf8(cmd.argument()), p);
360 Buffer const & realbuffer = *buffer().masterBuffer();
361 IndicesList const & indiceslist = realbuffer.params().indiceslist();
362 Index const * index = indiceslist.findShortcut(p["type"]);
363 status.setEnabled(index != 0);
364 status.setOnOff(p["type"] == getParam("type"));
369 return InsetCommand::getStatus(cur, cmd, status);
374 int InsetPrintIndex::latex(odocstream & os, OutputParams const &) const
376 if (!buffer().masterBuffer()->params().use_indices) {
377 if (getParam("type") == from_ascii("idx"))
378 os << "\\printindex{}";
386 void InsetPrintIndex::validate(LaTeXFeatures & features) const
388 features.require("makeidx");
389 if (buffer().masterBuffer()->params().use_indices)
390 features.require("splitidx");
394 docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
396 return buffer().masterBuffer()->params().use_indices ?
397 from_ascii("context-indexprint") : docstring();