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"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "LaTeXFeatures.h"
19 #include "MetricsInfo.h"
21 #include "TocBackend.h"
23 #include "support/debug.h"
24 #include "support/docstream.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
31 using namespace lyx::support;
35 /////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////
42 InsetIndex::InsetIndex(Buffer const & buf)
43 : InsetCollapsable(buf)
47 int InsetIndex::latex(odocstream & os,
48 OutputParams const & runparams) const
54 // get contents of InsetText as LaTeX and plaintext
55 odocstringstream ourlatex;
56 InsetText::latex(ourlatex, runparams);
57 odocstringstream ourplain;
58 InsetText::plaintext(ourplain, runparams);
59 docstring latexstr = ourlatex.str();
60 docstring plainstr = ourplain.str();
62 // this will get what follows | if anything does
65 // check for the | separator
66 // FIXME This would go wrong on an escaped "|", but
67 // how far do we want to go here?
68 size_t pos = latexstr.find(from_ascii("|"));
69 if (pos != docstring::npos) {
70 // put the bit after "|" into cmd...
71 cmd = latexstr.substr(pos + 1);
72 // ...and erase that stuff from latexstr
73 latexstr = latexstr.erase(pos);
74 // ...and similarly from plainstr
75 size_t ppos = plainstr.find(from_ascii("|"));
76 if (ppos < plainstr.size())
79 LYXERR0("The `|' separator was not found in the plaintext version!");
82 // Separate the entires and subentries, i.e., split on "!"
83 // FIXME This would do the wrong thing with escaped ! characters
84 std::vector<docstring> const levels =
85 getVectorFromString(latexstr, from_ascii("!"), true);
86 std::vector<docstring> const levels_plain =
87 getVectorFromString(plainstr, from_ascii("!"), true);
89 vector<docstring>::const_iterator it = levels.begin();
90 vector<docstring>::const_iterator end = levels.end();
91 vector<docstring>::const_iterator it2 = levels_plain.begin();
93 for (; it != end; ++it) {
94 // write the separator except the first time
101 // correctly sort macros and formatted strings
102 // if we do find a command, prepend a plain text
103 // version of the content to get sorting right,
104 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
105 // Don't do that if the user entered '@' himself, though.
106 if (contains(*it, '\\') && !contains(*it, '@')) {
107 // Plaintext might return nothing (e.g. for ERTs)
108 docstring const spart =
109 (it2 < levels_plain.end() && !(*it2).empty()) ? *it2 : *it;
110 // remove remaining \'s for the sorting part
111 docstring const ppart =
112 subst(spart, from_ascii("\\"), docstring());
115 i += ppart.size() + 1;
117 docstring const tpart = *it;
120 if (it2 < levels_plain.end())
123 // write the bit that followed "|"
134 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
136 os << "<indexterm><primary>";
137 int const i = InsetText::docbook(os, runparams);
138 os << "</primary></indexterm>";
143 void InsetIndex::write(ostream & os) const
145 os << to_utf8(name()) << "\n";
146 InsetCollapsable::write(os);
150 void InsetIndex::addToToc(DocIterator const & cpit)
152 DocIterator pit = cpit;
153 pit.push_back(CursorSlice(*this));
155 Toc & toc = buffer().tocBackend().toc("index");
157 str = getNewLabel(str);
158 toc.push_back(TocItem(pit, 0, str));
159 // Proceed with the rest of the inset.
160 InsetCollapsable::addToToc(cpit);
164 /////////////////////////////////////////////////////////////////////
168 ///////////////////////////////////////////////////////////////////////
170 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
171 : InsetCommand(p, string())
175 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
177 static ParamInfo param_info_;
178 if (param_info_.empty())
179 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
184 docstring InsetPrintIndex::screenLabel() const
190 void InsetPrintIndex::validate(LaTeXFeatures & features) const
192 features.require("makeidx");
196 InsetCode InsetPrintIndex::lyxCode() const
198 return INDEX_PRINT_CODE;