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 "InsetLayout.h"
26 #include "InsetList.h"
28 #include "LaTeXFeatures.h"
31 #include "texstream.h"
32 #include "TocBackend.h"
34 #include "frontends/FontMetrics.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/Length.h"
40 #include "support/lstrings.h"
43 using namespace lyx::support;
48 /////////////////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////////
54 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
55 : InsetCommand(buf, p)
59 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
61 static ParamInfo param_info_;
62 if (param_info_.empty()) {
63 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
64 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
65 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
66 | ParamInfo::HANDLING_INDEX_ESCAPE));
67 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
68 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
69 | ParamInfo::HANDLING_INDEX_ESCAPE));
70 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
76 docstring InsetNomencl::screenLabel() const
78 size_t const maxLabelChars = 25;
79 docstring label = _("Nom: ") + getParam("symbol");
80 support::truncateWithEllipsis(label, maxLabelChars);
85 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
87 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
88 tip += _("Description: ") + "\t"
89 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
90 if (!getParam("prefix").empty())
91 tip += "\n" + _("Sorting: ") + getParam("prefix");
96 int InsetNomencl::plaintext(odocstringstream & os,
97 OutputParams const &, size_t) const
99 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
105 void InsetNomencl::docbook(XMLStream & xs, OutputParams const &) const
107 docstring attr = "linkend=\"" + xml::cleanID(from_ascii("nomen") + getParam("symbol")) + "\"";
108 xs << xml::StartTag("glossterm", attr);
109 xs << xml::escapeString(getParam("symbol"));
110 xs << xml::EndTag("glossterm");
114 docstring InsetNomencl::xhtml(XMLStream &, OutputParams const &) const
120 void InsetNomencl::validate(LaTeXFeatures & features) const
122 features.require("nomencl");
123 InsetCommand::validate(features);
127 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
128 UpdateType, TocBackend & backend) const
130 docstring const str = getParam("symbol");
131 TocBuilder & b = backend.builder("nomencl");
132 b.pushItem(cpit, str, output_active);
137 /////////////////////////////////////////////////////////////////////
141 /////////////////////////////////////////////////////////////////////
143 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
144 : InsetCommand(buf, p)
148 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
150 // The symbol width is set via nomencl's \nomlabelwidth in
151 // InsetPrintNomencl::latex and not as optional parameter of
152 // \printnomenclature
153 static ParamInfo param_info_;
154 if (param_info_.empty()) {
155 // how is the width set?
156 // values: none|auto|custom
157 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
159 param_info_.add("width", ParamInfo::LYX_INTERNAL);
165 docstring InsetPrintNomencl::screenLabel() const
167 return _("Nomenclature");
171 struct NomenclEntry {
172 NomenclEntry() : par(nullptr) {}
173 NomenclEntry(docstring s, docstring d, Paragraph const * p)
174 : symbol(s), desc(d), par(p)
179 Paragraph const * par;
183 typedef map<docstring, NomenclEntry > EntryMap;
186 docstring InsetPrintNomencl::xhtml(XMLStream &, OutputParams const & op) const
188 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
191 Toc::const_iterator it = toc->begin();
192 Toc::const_iterator const en = toc->end();
193 for (; it != en; ++it) {
194 DocIterator dit = it->dit();
195 Paragraph const & par = dit.innerParagraph();
196 Inset const * inset = par.getInset(dit.top().pos());
199 InsetCommand const * ic = inset->asInsetCommand();
203 // FIXME We need a link to the paragraph here, so we
204 // need some kind of struct.
205 docstring const symbol = ic->getParam("symbol");
206 docstring const desc = ic->getParam("description");
207 docstring const prefix = ic->getParam("prefix");
208 docstring const sortas = prefix.empty() ? symbol : prefix;
210 entries[sortas] = NomenclEntry(symbol, desc, &par);
216 // we'll use our own stream, because we are going to defer everything.
217 // that's how we deal with the fact that we're probably inside a standard
218 // paragraph, and we don't want to be.
219 odocstringstream ods;
222 InsetLayout const & il = getLayout();
223 string const & tag = il.htmltag();
224 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
225 op.local_font->language()->lang());
227 xs << xml::StartTag("div", "class='nomencl'")
228 << xml::StartTag(tag, "class='nomencl'")
232 << xml::StartTag("dl")
235 EntryMap::const_iterator eit = entries.begin();
236 EntryMap::const_iterator const een = entries.end();
237 for (; eit != een; ++eit) {
238 NomenclEntry const & ne = eit->second;
239 string const parid = ne.par->magicLabel();
240 xs << xml::StartTag("dt")
241 << xml::StartTag("a", "href='#" + parid + "' class='nomencl'")
246 << xml::StartTag("dd")
252 xs << xml::EndTag("dl")
254 << xml::EndTag("div")
261 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
263 switch (cmd.action()) {
265 case LFUN_INSET_MODIFY: {
266 InsetCommandParams p(NOMENCL_PRINT_CODE);
268 InsetCommand::string2params(to_utf8(cmd.argument()), p);
269 if (p.getCmdName().empty()) {
270 cur.noScreenUpdate();
280 InsetCommand::doDispatch(cur, cmd);
286 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
287 FuncStatus & status) const
289 switch (cmd.action()) {
291 case LFUN_INSET_DIALOG_UPDATE:
292 case LFUN_INSET_MODIFY:
293 status.setEnabled(true);
297 return InsetCommand::getStatus(cur, cmd, status);
302 void InsetPrintNomencl::docbook(XMLStream & xs, OutputParams const & runparams) const
304 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
307 Toc::const_iterator it = toc->begin();
308 Toc::const_iterator const en = toc->end();
309 for (; it != en; ++it) {
310 DocIterator dit = it->dit();
311 Paragraph const & par = dit.innerParagraph();
312 Inset const * inset = par.getInset(dit.top().pos());
315 InsetCommand const * ic = inset->asInsetCommand();
319 // FIXME We need a link to the paragraph here, so we
320 // need some kind of struct.
321 docstring const symbol = ic->getParam("symbol");
322 docstring const desc = ic->getParam("description");
323 docstring const prefix = ic->getParam("prefix");
324 docstring const sortas = prefix.empty() ? symbol : prefix;
326 entries[sortas] = NomenclEntry(symbol, desc, &par);
332 // As opposed to XHTML, no need to defer everything until the end of time, so write directly to xs.
333 // TODO: At least, that's what was done before...
335 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
336 runparams.local_font->language()->lang());
338 xs << xml::StartTag("glossary");
340 xs << xml::StartTag("title");
342 xs << xml::EndTag("title");
345 EntryMap::const_iterator eit = entries.begin();
346 EntryMap::const_iterator const een = entries.end();
347 for (; eit != een; ++eit) {
348 NomenclEntry const & ne = eit->second;
350 xs << xml::StartTag("glossentry", "xml:id=\"" + xml::cleanID(from_ascii("nomen") + ne.symbol) + "\"");
352 xs << xml::StartTag("glossterm");
354 xs << xml::EndTag("glossterm");
356 xs << xml::StartTag("glossdef");
358 xs << xml::StartTag("para");
360 xs << xml::EndTag("para");
362 xs << xml::EndTag("glossdef");
364 xs << xml::EndTag("glossentry");
368 xs << xml::EndTag("glossary");
374 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
376 // nomenclWidest() determines and returns the widest used
377 // nomenclature symbol in the document
381 InsetNomencl const * nomencl = nullptr;
382 ParagraphList::const_iterator it = buffer.paragraphs().begin();
383 ParagraphList::const_iterator end = buffer.paragraphs().end();
385 for (; it != end; ++it) {
386 if (it->insetList().empty())
388 InsetList::const_iterator iit = it->insetList().begin();
389 InsetList::const_iterator eend = it->insetList().end();
390 for (; iit != eend; ++iit) {
391 Inset * inset = iit->inset;
392 if (inset->lyxCode() != NOMENCL_CODE)
394 nomencl = static_cast<InsetNomencl const *>(inset);
395 // Use proper formatting. We do not escape makeindex chars here
396 docstring symbol = nomencl ?
397 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
398 ParamInfo::HANDLING_LATEXIFY)
400 // strip out % characters which are used as escape in nomencl
401 // but act as comment in our context here
402 symbol = subst(symbol, from_ascii("%"), docstring());
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";