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 "InsetList.h"
27 #include "LaTeXFeatures.h"
30 #include "texstream.h"
31 #include "TocBackend.h"
33 #include "frontends/FontMetrics.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/Length.h"
39 #include "support/lstrings.h"
42 using namespace lyx::support;
47 /////////////////////////////////////////////////////////////////////
51 /////////////////////////////////////////////////////////////////////
53 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
54 : InsetCommand(buf, p)
58 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
60 static ParamInfo param_info_;
61 if (param_info_.empty()) {
62 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
63 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
64 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
65 | ParamInfo::HANDLING_INDEX_ESCAPE));
66 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
68 | ParamInfo::HANDLING_INDEX_ESCAPE));
69 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
75 docstring InsetNomencl::screenLabel() const
77 size_t const maxLabelChars = 25;
78 docstring label = _("Nom: ") + getParam("symbol");
79 support::truncateWithEllipsis(label, maxLabelChars);
84 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
86 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
87 tip += _("Description: ") + "\t"
88 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
89 if (!getParam("prefix").empty())
90 tip += "\n" + _("Sorting: ") + getParam("prefix");
95 int InsetNomencl::plaintext(odocstringstream & os,
96 OutputParams const &, size_t) const
98 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
104 void InsetNomencl::docbook(XMLStream & xs, OutputParams const &) const
106 docstring attr = "linkend=\"" + xml::cleanID(from_ascii("nomen") + getParam("symbol")) + "\"";
107 xs << xml::StartTag("glossterm", attr);
108 xs << xml::escapeString(getParam("symbol"));
109 xs << xml::EndTag("glossterm");
113 docstring InsetNomencl::xhtml(XMLStream &, OutputParams const &) const
119 void InsetNomencl::validate(LaTeXFeatures & features) const
121 features.require("nomencl");
122 InsetCommand::validate(features);
126 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
127 UpdateType, TocBackend & backend) const
129 docstring const str = getParam("symbol");
130 TocBuilder & b = backend.builder("nomencl");
131 b.pushItem(cpit, str, output_active);
136 /////////////////////////////////////////////////////////////////////
140 /////////////////////////////////////////////////////////////////////
142 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
143 : InsetCommand(buf, p)
147 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
149 // The symbol width is set via nomencl's \nomlabelwidth in
150 // InsetPrintNomencl::latex and not as optional parameter of
151 // \printnomenclature
152 static ParamInfo param_info_;
153 if (param_info_.empty()) {
154 // how is the width set?
155 // values: none|auto|custom
156 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
158 param_info_.add("width", ParamInfo::LYX_INTERNAL);
164 docstring InsetPrintNomencl::screenLabel() const
166 return _("Nomenclature");
170 struct NomenclEntry {
171 NomenclEntry() : par(nullptr) {}
172 NomenclEntry(docstring s, docstring d, Paragraph const * p)
173 : symbol(s), desc(d), par(p)
178 Paragraph const * par;
182 typedef map<docstring, NomenclEntry > EntryMap;
185 docstring InsetPrintNomencl::xhtml(XMLStream &, OutputParams const & op) const
187 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
190 Toc::const_iterator it = toc->begin();
191 Toc::const_iterator const en = toc->end();
192 for (; it != en; ++it) {
193 DocIterator dit = it->dit();
194 Paragraph const & par = dit.innerParagraph();
195 Inset const * inset = par.getInset(dit.top().pos());
198 InsetCommand const * ic = inset->asInsetCommand();
202 // FIXME We need a link to the paragraph here, so we
203 // need some kind of struct.
204 docstring const symbol = ic->getParam("symbol");
205 docstring const desc = ic->getParam("description");
206 docstring const prefix = ic->getParam("prefix");
207 docstring const sortas = prefix.empty() ? symbol : prefix;
209 entries[sortas] = NomenclEntry(symbol, desc, &par);
215 // we'll use our own stream, because we are going to defer everything.
216 // that's how we deal with the fact that we're probably inside a standard
217 // paragraph, and we don't want to be.
218 odocstringstream ods;
221 InsetLayout const & il = getLayout();
222 string const & tag = il.htmltag();
223 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
224 op.local_font->language()->lang());
226 xs << xml::StartTag("div", "class='nomencl'")
227 << xml::StartTag(tag, "class='nomencl'")
231 << xml::StartTag("dl")
234 EntryMap::const_iterator eit = entries.begin();
235 EntryMap::const_iterator const een = entries.end();
236 for (; eit != een; ++eit) {
237 NomenclEntry const & ne = eit->second;
238 string const parid = ne.par->magicLabel();
239 xs << xml::StartTag("dt")
240 << xml::StartTag("a", "href='#" + parid + "' class='nomencl'")
245 << xml::StartTag("dd")
251 xs << xml::EndTag("dl")
253 << xml::EndTag("div")
260 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
262 switch (cmd.action()) {
264 case LFUN_INSET_MODIFY: {
265 InsetCommandParams p(NOMENCL_PRINT_CODE);
267 InsetCommand::string2params(to_utf8(cmd.argument()), p);
268 if (p.getCmdName().empty()) {
269 cur.noScreenUpdate();
279 InsetCommand::doDispatch(cur, cmd);
285 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
286 FuncStatus & status) const
288 switch (cmd.action()) {
290 case LFUN_INSET_DIALOG_UPDATE:
291 case LFUN_INSET_MODIFY:
292 status.setEnabled(true);
296 return InsetCommand::getStatus(cur, cmd, status);
301 void InsetPrintNomencl::docbook(XMLStream & xs, OutputParams const & runparams) const
303 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
306 Toc::const_iterator it = toc->begin();
307 Toc::const_iterator const en = toc->end();
308 for (; it != en; ++it) {
309 DocIterator dit = it->dit();
310 Paragraph const & par = dit.innerParagraph();
311 Inset const * inset = par.getInset(dit.top().pos());
314 InsetCommand const * ic = inset->asInsetCommand();
318 // FIXME We need a link to the paragraph here, so we
319 // need some kind of struct.
320 docstring const symbol = ic->getParam("symbol");
321 docstring const desc = ic->getParam("description");
322 docstring const prefix = ic->getParam("prefix");
323 docstring const sortas = prefix.empty() ? symbol : prefix;
325 entries[sortas] = NomenclEntry(symbol, desc, &par);
331 // As opposed to XHTML, no need to defer everything until the end of time, so write directly to xs.
332 // TODO: At least, that's what was done before...
334 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
335 runparams.local_font->language()->lang());
337 xs << xml::StartTag("glossary");
339 xs << xml::StartTag("title");
341 xs << xml::EndTag("title");
344 EntryMap::const_iterator eit = entries.begin();
345 EntryMap::const_iterator const een = entries.end();
346 for (; eit != een; ++eit) {
347 NomenclEntry const & ne = eit->second;
349 xs << xml::StartTag("glossentry", "xml:id=\"" + xml::cleanID(from_ascii("nomen") + ne.symbol) + "\"");
351 xs << xml::StartTag("glossterm");
353 xs << xml::EndTag("glossterm");
355 xs << xml::StartTag("glossdef");
357 xs << xml::StartTag("para");
359 xs << xml::EndTag("para");
361 xs << xml::EndTag("glossdef");
363 xs << xml::EndTag("glossentry");
367 xs << xml::EndTag("glossary");
373 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
375 // nomenclWidest() determines and returns the widest used
376 // nomenclature symbol in the document
380 InsetNomencl const * nomencl = nullptr;
381 ParagraphList::const_iterator it = buffer.paragraphs().begin();
382 ParagraphList::const_iterator end = buffer.paragraphs().end();
384 for (; it != end; ++it) {
385 if (it->insetList().empty())
387 InsetList::const_iterator iit = it->insetList().begin();
388 InsetList::const_iterator eend = it->insetList().end();
389 for (; iit != eend; ++iit) {
390 Inset * inset = iit->inset;
391 if (inset->lyxCode() != NOMENCL_CODE)
393 nomencl = static_cast<InsetNomencl const *>(inset);
394 // Use proper formatting. We do not escape makeindex chars here
395 docstring const symbol = nomencl ?
396 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
397 ParamInfo::HANDLING_LATEXIFY)
399 // This is only an approximation,
400 // but the best we can get.
401 int const wx = use_gui ?
402 theFontMetrics(Font()).width(symbol) :
410 // return the widest (or an empty) string
416 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
418 OutputParams runparams = runparams_in;
419 if (getParam("set_width") == "auto") {
420 docstring widest = nomenclWidest(buffer(), runparams);
421 // Set the label width via nomencl's command \nomlabelwidth.
422 // This must be output before the command \printnomenclature
423 if (!widest.empty()) {
424 os << "\\settowidth{\\nomlabelwidth}{"
428 } else if (getParam("set_width") == "custom") {
429 // custom length as optional arg of \printnomenclature
431 Length(to_ascii(getParam("width"))).asLatexString();
433 << from_ascii(getCmdName())
440 // output the command \printnomenclature
441 os << getCommand(runparams);
445 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
447 features.useInsetLayout(getLayout());
448 features.require("nomencl");
452 InsetCode InsetPrintNomencl::lyxCode() const
454 return NOMENCL_PRINT_CODE;
458 string InsetPrintNomencl::contextMenuName() const
460 return "context-nomenclprint";