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)
60 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
62 static ParamInfo param_info_;
63 if (param_info_.empty()) {
64 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
65 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
66 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
67 | ParamInfo::HANDLING_INDEX_ESCAPE));
68 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
69 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
70 | ParamInfo::HANDLING_INDEX_ESCAPE));
71 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
77 docstring InsetNomencl::screenLabel() const
79 size_t const maxLabelChars = 25;
80 docstring label = _("Nom: ") + getParam("symbol");
81 support::truncateWithEllipsis(label, maxLabelChars);
86 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
88 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
89 tip += _("Description: ") + "\t"
90 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
91 if (!getParam("prefix").empty())
92 tip += "\n" + _("Sorting: ") + getParam("prefix");
97 int InsetNomencl::plaintext(odocstringstream & os,
98 OutputParams const &, size_t) const
100 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
106 void InsetNomencl::docbook(XMLStream & xs, OutputParams const &) const
108 docstring attr = "linkend=\"" + xml::cleanID(from_ascii("nomen") + getParam("symbol")) + "\"";
109 xs << xml::StartTag("glossterm", attr);
110 xs << xml::escapeString(getParam("symbol"));
111 xs << xml::EndTag("glossterm");
115 docstring InsetNomencl::xhtml(XMLStream &, OutputParams const &) const
121 void InsetNomencl::validate(LaTeXFeatures & features) const
123 features.require("nomencl");
124 InsetCommand::validate(features);
128 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
129 UpdateType, TocBackend & backend) const
131 docstring const str = getParam("symbol");
132 TocBuilder & b = backend.builder("nomencl");
133 b.pushItem(cpit, str, output_active);
138 /////////////////////////////////////////////////////////////////////
142 /////////////////////////////////////////////////////////////////////
144 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
145 : InsetCommand(buf, p)
149 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
151 // The symbol width is set via nomencl's \nomlabelwidth in
152 // InsetPrintNomencl::latex and not as optional parameter of
153 // \printnomenclature
154 static ParamInfo param_info_;
155 if (param_info_.empty()) {
156 // how is the width set?
157 // values: none|auto|custom
158 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
160 param_info_.add("width", ParamInfo::LYX_INTERNAL);
166 docstring InsetPrintNomencl::screenLabel() const
168 return _("Nomenclature");
172 struct NomenclEntry {
173 NomenclEntry() : par(nullptr) {}
174 NomenclEntry(docstring s, docstring d, Paragraph const * p)
175 : symbol(s), desc(d), par(p)
180 Paragraph const * par;
184 typedef map<docstring, NomenclEntry > EntryMap;
187 docstring InsetPrintNomencl::xhtml(XMLStream &, OutputParams const & op) const
189 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
192 Toc::const_iterator it = toc->begin();
193 Toc::const_iterator const en = toc->end();
194 for (; it != en; ++it) {
195 DocIterator dit = it->dit();
196 Paragraph const & par = dit.innerParagraph();
197 Inset const * inset = par.getInset(dit.top().pos());
200 InsetCommand const * ic = inset->asInsetCommand();
204 // FIXME We need a link to the paragraph here, so we
205 // need some kind of struct.
206 docstring const symbol = ic->getParam("symbol");
207 docstring const desc = ic->getParam("description");
208 docstring const prefix = ic->getParam("prefix");
209 docstring const sortas = prefix.empty() ? symbol : prefix;
211 entries[sortas] = NomenclEntry(symbol, desc, &par);
217 // we'll use our own stream, because we are going to defer everything.
218 // that's how we deal with the fact that we're probably inside a standard
219 // paragraph, and we don't want to be.
220 odocstringstream ods;
223 InsetLayout const & il = getLayout();
224 string const & tag = il.htmltag();
225 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
226 op.local_font->language()->lang());
228 xs << xml::StartTag("div", "class='nomencl'")
229 << xml::StartTag(tag, "class='nomencl'")
233 << xml::StartTag("dl")
236 EntryMap::const_iterator eit = entries.begin();
237 EntryMap::const_iterator const een = entries.end();
238 for (; eit != een; ++eit) {
239 NomenclEntry const & ne = eit->second;
240 string const parid = ne.par->magicLabel();
241 xs << xml::StartTag("dt")
242 << xml::StartTag("a", "href='#" + parid + "' class='nomencl'")
247 << xml::StartTag("dd")
253 xs << xml::EndTag("dl")
255 << xml::EndTag("div")
262 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
264 switch (cmd.action()) {
266 case LFUN_INSET_MODIFY: {
267 InsetCommandParams p(NOMENCL_PRINT_CODE);
269 InsetCommand::string2params(to_utf8(cmd.argument()), p);
270 if (p.getCmdName().empty()) {
271 cur.noScreenUpdate();
281 InsetCommand::doDispatch(cur, cmd);
287 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
288 FuncStatus & status) const
290 switch (cmd.action()) {
292 case LFUN_INSET_DIALOG_UPDATE:
293 case LFUN_INSET_MODIFY:
294 status.setEnabled(true);
298 return InsetCommand::getStatus(cur, cmd, status);
303 void InsetPrintNomencl::docbook(XMLStream & xs, OutputParams const & runparams) const
305 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
308 Toc::const_iterator it = toc->begin();
309 Toc::const_iterator const en = toc->end();
310 for (; it != en; ++it) {
311 DocIterator dit = it->dit();
312 Paragraph const & par = dit.innerParagraph();
313 Inset const * inset = par.getInset(dit.top().pos());
316 InsetCommand const * ic = inset->asInsetCommand();
320 // FIXME We need a link to the paragraph here, so we
321 // need some kind of struct.
322 docstring const symbol = ic->getParam("symbol");
323 docstring const desc = ic->getParam("description");
324 docstring const prefix = ic->getParam("prefix");
325 docstring const sortas = prefix.empty() ? symbol : prefix;
327 entries[sortas] = NomenclEntry(symbol, desc, &par);
333 // As opposed to XHTML, no need to defer everything until the end of time, so write directly to xs.
334 // TODO: At least, that's what was done before...
336 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
337 runparams.local_font->language()->lang());
339 xs << xml::StartTag("glossary");
341 xs << xml::StartTag("title");
343 xs << xml::EndTag("title");
346 EntryMap::const_iterator eit = entries.begin();
347 EntryMap::const_iterator const een = entries.end();
348 for (; eit != een; ++eit) {
349 NomenclEntry const & ne = eit->second;
351 xs << xml::StartTag("glossentry", "xml:id=\"" + xml::cleanID(from_ascii("nomen") + ne.symbol) + "\"");
353 xs << xml::StartTag("glossterm");
355 xs << xml::EndTag("glossterm");
357 xs << xml::StartTag("glossdef");
359 xs << xml::StartTag("para");
361 xs << xml::EndTag("para");
363 xs << xml::EndTag("glossdef");
365 xs << xml::EndTag("glossentry");
369 xs << xml::EndTag("glossary");
375 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
377 // nomenclWidest() determines and returns the widest used
378 // nomenclature symbol in the document
382 InsetNomencl const * nomencl = nullptr;
383 ParagraphList::const_iterator it = buffer.paragraphs().begin();
384 ParagraphList::const_iterator end = buffer.paragraphs().end();
386 for (; it != end; ++it) {
387 if (it->insetList().empty())
389 InsetList::const_iterator iit = it->insetList().begin();
390 InsetList::const_iterator eend = it->insetList().end();
391 for (; iit != eend; ++iit) {
392 Inset * inset = iit->inset;
393 if (inset->lyxCode() != NOMENCL_CODE)
395 nomencl = static_cast<InsetNomencl const *>(inset);
396 // Use proper formatting. We do not escape makeindex chars here
397 docstring const symbol = nomencl ?
398 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
399 ParamInfo::HANDLING_LATEXIFY)
401 // This is only an approximation,
402 // but the best we can get.
403 int const wx = use_gui ?
404 theFontMetrics(Font()).width(symbol) :
412 // return the widest (or an empty) string
418 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
420 OutputParams runparams = runparams_in;
421 if (getParam("set_width") == "auto") {
422 docstring widest = nomenclWidest(buffer(), runparams);
423 // Set the label width via nomencl's command \nomlabelwidth.
424 // This must be output before the command \printnomenclature
425 if (!widest.empty()) {
426 os << "\\settowidth{\\nomlabelwidth}{"
430 } else if (getParam("set_width") == "custom") {
431 // custom length as optional arg of \printnomenclature
433 Length(to_ascii(getParam("width"))).asLatexString();
435 << from_ascii(getCmdName())
442 // output the command \printnomenclature
443 os << getCommand(runparams);
447 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
449 features.useInsetLayout(getLayout());
450 features.require("nomencl");
454 InsetCode InsetPrintNomencl::lyxCode() const
456 return NOMENCL_PRINT_CODE;
460 string InsetPrintNomencl::contextMenuName() const
462 return "context-nomenclprint";