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
8 * Full author contact details are available in file CREDITS.
12 #include "InsetIndex.h"
15 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
22 #include "TocBackend.h"
24 #include "support/debug.h"
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
29 #include "frontends/alert.h"
34 using namespace lyx::support;
38 /////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////
45 InsetIndex::InsetIndex(Buffer const & buf)
46 : InsetCollapsable(buf)
50 int InsetIndex::latex(odocstream & os,
51 OutputParams const & runparams) const
57 // get contents of InsetText as LaTeX and plaintext
58 odocstringstream ourlatex;
59 InsetText::latex(ourlatex, runparams);
60 odocstringstream ourplain;
61 InsetText::plaintext(ourplain, runparams);
62 docstring latexstr = ourlatex.str();
63 docstring plainstr = ourplain.str();
65 // this will get what follows | if anything does
68 // check for the | separator
69 // FIXME This would go wrong on an escaped "|", but
70 // how far do we want to go here?
71 size_t pos = latexstr.find(from_ascii("|"));
72 if (pos != docstring::npos) {
73 // put the bit after "|" into cmd...
74 cmd = latexstr.substr(pos + 1);
75 // ...and erase that stuff from latexstr
76 latexstr = latexstr.erase(pos);
77 // ...and similarly from plainstr
78 size_t ppos = plainstr.find(from_ascii("|"));
79 if (ppos < plainstr.size())
82 LYXERR0("The `|' separator was not found in the plaintext version!");
85 // Separate the entires and subentries, i.e., split on "!"
86 // FIXME This would do the wrong thing with escaped ! characters
87 std::vector<docstring> const levels =
88 getVectorFromString(latexstr, from_ascii("!"), true);
89 std::vector<docstring> const levels_plain =
90 getVectorFromString(plainstr, from_ascii("!"), true);
92 vector<docstring>::const_iterator it = levels.begin();
93 vector<docstring>::const_iterator end = levels.end();
94 vector<docstring>::const_iterator it2 = levels_plain.begin();
96 for (; it != end; ++it) {
97 // write the separator except the first time
104 // correctly sort macros and formatted strings
105 // if we do find a command, prepend a plain text
106 // version of the content to get sorting right,
107 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
108 // Don't do that if the user entered '@' himself, though.
109 if (contains(*it, '\\') && !contains(*it, '@')) {
110 // Plaintext might return nothing (e.g. for ERTs)
111 docstring const spart =
112 (it2 < levels_plain.end() && !(*it2).empty())
114 // Now we need to validate that all characters in
115 // the sorting part are representable in the current
116 // encoding. If not try the LaTeX macro which might
117 // or might not be a good choice, and issue a warning.
119 for (size_t n = 0; n < spart.size(); ++n) {
121 spart2 += runparams.encoding->latexChar(spart[n]);
122 } catch (EncodingException & /* e */) {
123 LYXERR0("Uncodable character in index entry. Sorting might be wrong!");
126 if (spart != spart2 && !runparams.dryrun) {
127 // FIXME: warning should be passed to the error dialog
128 frontend::Alert::warning(_("Index sorting failed"),
129 bformat(_("LyX's automatic index sorting algorithm faced\n"
130 "problems with the entry '%1$s'.\n"
131 "Please specify the sorting of this entry manually, as\n"
132 "explained in the User Guide."), spart));
134 // remove remaining \'s for the sorting part
135 docstring const ppart =
136 subst(spart2, from_ascii("\\"), docstring());
139 i += ppart.size() + 1;
141 docstring const tpart = *it;
144 if (it2 < levels_plain.end())
147 // write the bit that followed "|"
158 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
160 os << "<indexterm><primary>";
161 int const i = InsetText::docbook(os, runparams);
162 os << "</primary></indexterm>";
167 void InsetIndex::write(ostream & os) const
169 os << to_utf8(name()) << "\n";
170 InsetCollapsable::write(os);
174 void InsetIndex::textString(odocstream & os) const
176 os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
180 void InsetIndex::addToToc(DocIterator const & cpit)
182 DocIterator pit = cpit;
183 pit.push_back(CursorSlice(*this));
184 odocstringstream ods;
186 buffer().tocBackend().toc("index").push_back(TocItem(pit, 0, ods.str()));
187 // Proceed with the rest of the inset.
188 InsetCollapsable::addToToc(cpit);
192 /////////////////////////////////////////////////////////////////////
196 ///////////////////////////////////////////////////////////////////////
198 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
199 : InsetCommand(p, string())
203 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
205 static ParamInfo param_info_;
206 if (param_info_.empty())
207 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
212 docstring InsetPrintIndex::screenLabel() const
218 void InsetPrintIndex::validate(LaTeXFeatures & features) const
220 features.require("makeidx");
224 InsetCode InsetPrintIndex::lyxCode() const
226 return INDEX_PRINT_CODE;