2 * \file InsetNomencl.cpp
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
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
20 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
28 #include "LaTeXFeatures.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
34 #include "texstream.h"
35 #include "TocBackend.h"
37 #include "frontends/FontMetrics.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
45 using namespace lyx::support;
50 /////////////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////
56 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
57 : InsetCommand(buf, p),
58 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
62 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
64 static ParamInfo param_info_;
65 if (param_info_.empty()) {
66 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
67 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
68 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
69 | ParamInfo::HANDLING_INDEX_ESCAPE));
70 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
71 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
72 | ParamInfo::HANDLING_INDEX_ESCAPE));
73 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
79 docstring InsetNomencl::screenLabel() const
81 size_t const maxLabelChars = 25;
82 docstring label = _("Nom: ") + getParam("symbol");
83 support::truncateWithEllipsis(label, maxLabelChars);
88 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
90 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
91 tip += _("Description: ") + "\t"
92 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
93 if (!getParam("prefix").empty())
94 tip += "\n" + _("Sorting: ") + getParam("prefix");
99 int InsetNomencl::plaintext(odocstringstream & os,
100 OutputParams const &, size_t) const
102 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
108 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
110 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
111 << sgml::escapeString(getParam("symbol"))
117 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
123 int InsetNomencl::docbookGlossary(odocstream & os) const
125 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
127 << sgml::escapeString(getParam("symbol"))
129 << "<glossdef><para>"
130 << sgml::escapeString(getParam("description"))
131 << "</para></glossdef>\n"
137 void InsetNomencl::validate(LaTeXFeatures & features) const
139 features.require("nomencl");
140 InsetCommand::validate(features);
144 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
145 UpdateType, TocBackend & backend) const
147 docstring const str = getParam("symbol");
148 TocBuilder & b = backend.builder("nomencl");
149 b.pushItem(cpit, str, output_active);
154 /////////////////////////////////////////////////////////////////////
158 /////////////////////////////////////////////////////////////////////
160 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
161 : InsetCommand(buf, p)
165 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
167 // The symbol width is set via nomencl's \nomlabelwidth in
168 // InsetPrintNomencl::latex and not as optional parameter of
169 // \printnomenclature
170 static ParamInfo param_info_;
171 if (param_info_.empty()) {
172 // how is the width set?
173 // values: none|auto|custom
174 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
176 param_info_.add("width", ParamInfo::LYX_INTERNAL);
182 docstring InsetPrintNomencl::screenLabel() const
184 return _("Nomenclature");
188 struct NomenclEntry {
189 NomenclEntry() : par(0) {}
190 NomenclEntry(docstring s, docstring d, Paragraph const * p)
191 : symbol(s), desc(d), par(p)
196 Paragraph const * par;
200 typedef map<docstring, NomenclEntry > EntryMap;
203 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
205 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
208 Toc::const_iterator it = toc->begin();
209 Toc::const_iterator const en = toc->end();
210 for (; it != en; ++it) {
211 DocIterator dit = it->dit();
212 Paragraph const & par = dit.innerParagraph();
213 Inset const * inset = par.getInset(dit.top().pos());
216 InsetCommand const * ic = inset->asInsetCommand();
220 // FIXME We need a link to the paragraph here, so we
221 // need some kind of struct.
222 docstring const symbol = ic->getParam("symbol");
223 docstring const desc = ic->getParam("description");
224 docstring const prefix = ic->getParam("prefix");
225 docstring const sortas = prefix.empty() ? symbol : prefix;
227 entries[sortas] = NomenclEntry(symbol, desc, &par);
233 // we'll use our own stream, because we are going to defer everything.
234 // that's how we deal with the fact that we're probably inside a standard
235 // paragraph, and we don't want to be.
236 odocstringstream ods;
239 InsetLayout const & il = getLayout();
240 string const & tag = il.htmltag();
241 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
242 op.local_font->language()->lang());
244 xs << html::StartTag("div", "class='nomencl'")
245 << html::StartTag(tag, "class='nomencl'")
249 << html::StartTag("dl")
252 EntryMap::const_iterator eit = entries.begin();
253 EntryMap::const_iterator const een = entries.end();
254 for (; eit != een; ++eit) {
255 NomenclEntry const & ne = eit->second;
256 string const parid = ne.par->magicLabel();
257 xs << html::StartTag("dt")
258 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
261 << html::EndTag("dt")
263 << html::StartTag("dd")
265 << html::EndTag("dd")
269 xs << html::EndTag("dl")
271 << html::EndTag("div")
278 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
280 switch (cmd.action()) {
282 case LFUN_INSET_MODIFY: {
283 InsetCommandParams p(NOMENCL_PRINT_CODE);
285 InsetCommand::string2params(to_utf8(cmd.argument()), p);
286 if (p.getCmdName().empty()) {
287 cur.noScreenUpdate();
297 InsetCommand::doDispatch(cur, cmd);
303 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
304 FuncStatus & status) const
306 switch (cmd.action()) {
308 case LFUN_INSET_DIALOG_UPDATE:
309 case LFUN_INSET_MODIFY:
310 status.setEnabled(true);
314 return InsetCommand::getStatus(cur, cmd, status);
319 // FIXME This should be changed to use the TOC. Perhaps
320 // that could be done when XHTML output is added.
321 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
323 os << "<glossary>\n";
325 InsetIterator it = inset_iterator_begin(buffer().inset());
327 if (it->lyxCode() == NOMENCL_CODE) {
328 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
330 } else if (!it->producesOutput()) {
331 // Ignore contents of insets that are not in output
332 size_t const depth = it.depth();
334 while (it.depth() > depth)
340 os << "</glossary>\n";
346 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
348 // nomenclWidest() determines and returns the widest used
349 // nomenclature symbol in the document
353 InsetNomencl const * nomencl = 0;
354 ParagraphList::const_iterator it = buffer.paragraphs().begin();
355 ParagraphList::const_iterator end = buffer.paragraphs().end();
357 for (; it != end; ++it) {
358 if (it->insetList().empty())
360 InsetList::const_iterator iit = it->insetList().begin();
361 InsetList::const_iterator eend = it->insetList().end();
362 for (; iit != eend; ++iit) {
363 Inset * inset = iit->inset;
364 if (inset->lyxCode() != NOMENCL_CODE)
366 nomencl = static_cast<InsetNomencl const *>(inset);
367 // Use proper formatting. We do not escape makeindex chars here
368 docstring const symbol = nomencl ?
369 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
370 ParamInfo::HANDLING_LATEXIFY)
372 // This is only an approximation,
373 // but the best we can get.
374 int const wx = use_gui ?
375 theFontMetrics(Font()).width(symbol) :
383 // return the widest (or an empty) string
389 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
391 OutputParams runparams = runparams_in;
392 if (getParam("set_width") == "auto") {
393 docstring widest = nomenclWidest(buffer(), runparams);
394 // Set the label width via nomencl's command \nomlabelwidth.
395 // This must be output before the command \printnomenclature
396 if (!widest.empty()) {
397 os << "\\settowidth{\\nomlabelwidth}{"
401 } else if (getParam("set_width") == "custom") {
402 // custom length as optional arg of \printnomenclature
404 Length(to_ascii(getParam("width"))).asLatexString();
406 << from_ascii(getCmdName())
413 // output the command \printnomenclature
414 os << getCommand(runparams);
418 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
420 features.useInsetLayout(getLayout());
421 features.require("nomencl");
425 InsetCode InsetPrintNomencl::lyxCode() const
427 return NOMENCL_PRINT_CODE;
431 string InsetPrintNomencl::contextMenuName() const
433 return "context-nomenclprint";