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"
30 #include "OutputParams.h"
32 #include "texstream.h"
33 #include "TocBackend.h"
35 #include "frontends/FontMetrics.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/Length.h"
41 #include "support/lstrings.h"
44 using namespace lyx::support;
49 /////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////
55 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
56 : InsetCommand(buf, p),
57 nomenclature_entry_id(xml::uniqueID(from_ascii("nomen")))
61 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
63 static ParamInfo param_info_;
64 if (param_info_.empty()) {
65 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
66 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
68 | ParamInfo::HANDLING_INDEX_ESCAPE));
69 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
70 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
71 | ParamInfo::HANDLING_INDEX_ESCAPE));
72 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
78 docstring InsetNomencl::screenLabel() const
80 size_t const maxLabelChars = 25;
81 docstring label = _("Nom: ") + getParam("symbol");
82 support::truncateWithEllipsis(label, maxLabelChars);
87 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
89 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
90 tip += _("Description: ") + "\t"
91 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
92 if (!getParam("prefix").empty())
93 tip += "\n" + _("Sorting: ") + getParam("prefix");
98 int InsetNomencl::plaintext(odocstringstream & os,
99 OutputParams const &, size_t) const
101 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
107 void InsetNomencl::docbook(XMLStream & xs, OutputParams const &) const
109 docstring attr = "linkend=\"" + nomenclature_entry_id + "\"";
110 xs << xml::StartTag("glossterm", attr);
111 xs << xml::escapeString(getParam("symbol"));
112 xs << xml::EndTag("glossterm");
116 docstring InsetNomencl::xhtml(XMLStream &, OutputParams const &) const
122 void InsetNomencl::validate(LaTeXFeatures & features) const
124 features.require("nomencl");
125 InsetCommand::validate(features);
129 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
130 UpdateType, TocBackend & backend) const
132 docstring const str = getParam("symbol");
133 TocBuilder & b = backend.builder("nomencl");
134 b.pushItem(cpit, str, output_active);
139 /////////////////////////////////////////////////////////////////////
143 /////////////////////////////////////////////////////////////////////
145 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
146 : InsetCommand(buf, p)
150 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
152 // The symbol width is set via nomencl's \nomlabelwidth in
153 // InsetPrintNomencl::latex and not as optional parameter of
154 // \printnomenclature
155 static ParamInfo param_info_;
156 if (param_info_.empty()) {
157 // how is the width set?
158 // values: none|auto|custom
159 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
161 param_info_.add("width", ParamInfo::LYX_INTERNAL);
167 docstring InsetPrintNomencl::screenLabel() const
169 return _("Nomenclature");
173 struct NomenclEntry {
174 NomenclEntry() : par(0) {}
175 NomenclEntry(docstring s, docstring d, Paragraph const * p)
176 : symbol(s), desc(d), par(p)
181 Paragraph const * par;
185 typedef map<docstring, NomenclEntry > EntryMap;
188 docstring InsetPrintNomencl::xhtml(XMLStream &, OutputParams const & op) const
190 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
193 Toc::const_iterator it = toc->begin();
194 Toc::const_iterator const en = toc->end();
195 for (; it != en; ++it) {
196 DocIterator dit = it->dit();
197 Paragraph const & par = dit.innerParagraph();
198 Inset const * inset = par.getInset(dit.top().pos());
201 InsetCommand const * ic = inset->asInsetCommand();
205 // FIXME We need a link to the paragraph here, so we
206 // need some kind of struct.
207 docstring const symbol = ic->getParam("symbol");
208 docstring const desc = ic->getParam("description");
209 docstring const prefix = ic->getParam("prefix");
210 docstring const sortas = prefix.empty() ? symbol : prefix;
212 entries[sortas] = NomenclEntry(symbol, desc, &par);
218 // we'll use our own stream, because we are going to defer everything.
219 // that's how we deal with the fact that we're probably inside a standard
220 // paragraph, and we don't want to be.
221 odocstringstream ods;
224 InsetLayout const & il = getLayout();
225 string const & tag = il.htmltag();
226 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
227 op.local_font->language()->lang());
229 xs << xml::StartTag("div", "class='nomencl'")
230 << xml::StartTag(tag, "class='nomencl'")
234 << xml::StartTag("dl")
237 EntryMap::const_iterator eit = entries.begin();
238 EntryMap::const_iterator const een = entries.end();
239 for (; eit != een; ++eit) {
240 NomenclEntry const & ne = eit->second;
241 string const parid = ne.par->magicLabel();
242 xs << xml::StartTag("dt")
243 << xml::StartTag("a", "href='#" + parid + "' class='nomencl'")
248 << xml::StartTag("dd")
254 xs << xml::EndTag("dl")
256 << xml::EndTag("div")
263 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
265 switch (cmd.action()) {
267 case LFUN_INSET_MODIFY: {
268 InsetCommandParams p(NOMENCL_PRINT_CODE);
270 InsetCommand::string2params(to_utf8(cmd.argument()), p);
271 if (p.getCmdName().empty()) {
272 cur.noScreenUpdate();
282 InsetCommand::doDispatch(cur, cmd);
288 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
289 FuncStatus & status) const
291 switch (cmd.action()) {
293 case LFUN_INSET_DIALOG_UPDATE:
294 case LFUN_INSET_MODIFY:
295 status.setEnabled(true);
299 return InsetCommand::getStatus(cur, cmd, status);
304 void InsetPrintNomencl::docbook(XMLStream & xs, OutputParams const & runparams) const
306 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
309 Toc::const_iterator it = toc->begin();
310 Toc::const_iterator const en = toc->end();
311 for (; it != en; ++it) {
312 DocIterator dit = it->dit();
313 Paragraph const & par = dit.innerParagraph();
314 Inset const * inset = par.getInset(dit.top().pos());
317 InsetCommand const * ic = inset->asInsetCommand();
321 // FIXME We need a link to the paragraph here, so we
322 // need some kind of struct.
323 docstring const symbol = ic->getParam("symbol");
324 docstring const desc = ic->getParam("description");
325 docstring const prefix = ic->getParam("prefix");
326 docstring const sortas = prefix.empty() ? symbol : prefix;
328 entries[sortas] = NomenclEntry(symbol, desc, &par);
334 // As opposed to XHTML, no need to defer everything until the end of time, so write directly to xs.
335 // TODO: At least, that's what was done before...
337 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
338 runparams.local_font->language()->lang());
340 xs << xml::StartTag("glossary");
342 xs << xml::StartTag("title");
344 xs << xml::EndTag("title");
347 EntryMap::const_iterator eit = entries.begin();
348 EntryMap::const_iterator const een = entries.end();
349 for (; eit != een; ++eit) {
350 NomenclEntry const & ne = eit->second;
351 string const parid = ne.par->magicLabel();
353 xs << xml::StartTag("glossentry", "xml:id=\"" + parid + "\"");
355 xs << xml::StartTag("glossterm");
357 xs << xml::EndTag("glossterm");
359 xs << xml::StartTag("glossdef");
361 xs << xml::StartTag("para");
363 xs << xml::EndTag("para");
365 xs << xml::EndTag("glossdef");
367 xs << xml::EndTag("glossentry");
371 xs << xml::EndTag("glossary");
377 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
379 // nomenclWidest() determines and returns the widest used
380 // nomenclature symbol in the document
384 InsetNomencl const * nomencl = 0;
385 ParagraphList::const_iterator it = buffer.paragraphs().begin();
386 ParagraphList::const_iterator end = buffer.paragraphs().end();
388 for (; it != end; ++it) {
389 if (it->insetList().empty())
391 InsetList::const_iterator iit = it->insetList().begin();
392 InsetList::const_iterator eend = it->insetList().end();
393 for (; iit != eend; ++iit) {
394 Inset * inset = iit->inset;
395 if (inset->lyxCode() != NOMENCL_CODE)
397 nomencl = static_cast<InsetNomencl const *>(inset);
398 // Use proper formatting. We do not escape makeindex chars here
399 docstring const symbol = nomencl ?
400 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
401 ParamInfo::HANDLING_LATEXIFY)
403 // This is only an approximation,
404 // but the best we can get.
405 int const wx = use_gui ?
406 theFontMetrics(Font()).width(symbol) :
414 // return the widest (or an empty) string
420 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
422 OutputParams runparams = runparams_in;
423 if (getParam("set_width") == "auto") {
424 docstring widest = nomenclWidest(buffer(), runparams);
425 // Set the label width via nomencl's command \nomlabelwidth.
426 // This must be output before the command \printnomenclature
427 if (!widest.empty()) {
428 os << "\\settowidth{\\nomlabelwidth}{"
432 } else if (getParam("set_width") == "custom") {
433 // custom length as optional arg of \printnomenclature
435 Length(to_ascii(getParam("width"))).asLatexString();
437 << from_ascii(getCmdName())
444 // output the command \printnomenclature
445 os << getCommand(runparams);
449 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
451 features.useInsetLayout(getLayout());
452 features.require("nomencl");
456 InsetCode InsetPrintNomencl::lyxCode() const
458 return NOMENCL_PRINT_CODE;
462 string InsetPrintNomencl::contextMenuName() const
464 return "context-nomenclprint";