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::HANDLING_LATEXIFY);
69 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
70 ParamInfo::HANDLING_LATEXIFY);
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 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
107 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
108 << sgml::escapeString(getParam("symbol"))
114 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
120 int InsetNomencl::docbookGlossary(odocstream & os) const
122 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
124 << sgml::escapeString(getParam("symbol"))
126 << "<glossdef><para>"
127 << sgml::escapeString(getParam("description"))
128 << "</para></glossdef>\n"
134 void InsetNomencl::validate(LaTeXFeatures & features) const
136 features.require("nomencl");
140 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
143 docstring const str = getParam("symbol");
144 buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, output_active));
148 /////////////////////////////////////////////////////////////////////
152 /////////////////////////////////////////////////////////////////////
154 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
155 : InsetCommand(buf, p)
159 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
161 // The symbol width is set via nomencl's \nomlabelwidth in
162 // InsetPrintNomencl::latex and not as optional parameter of
163 // \printnomenclature
164 static ParamInfo param_info_;
165 if (param_info_.empty()) {
166 // how is the width set?
167 // values: none|auto|custom
168 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
170 param_info_.add("width", ParamInfo::LYX_INTERNAL);
176 docstring InsetPrintNomencl::screenLabel() const
178 return _("Nomenclature");
182 struct NomenclEntry {
183 NomenclEntry() : par(0) {}
184 NomenclEntry(docstring s, docstring d, Paragraph const * p)
185 : symbol(s), desc(d), par(p)
190 Paragraph const * par;
194 typedef map<docstring, NomenclEntry > EntryMap;
197 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
199 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
202 Toc::const_iterator it = toc->begin();
203 Toc::const_iterator const en = toc->end();
204 for (; it != en; ++it) {
205 DocIterator dit = it->dit();
206 Paragraph const & par = dit.innerParagraph();
207 Inset const * inset = par.getInset(dit.top().pos());
210 InsetCommand const * ic = inset->asInsetCommand();
214 // FIXME We need a link to the paragraph here, so we
215 // need some kind of struct.
216 docstring const symbol = ic->getParam("symbol");
217 docstring const desc = ic->getParam("description");
218 docstring const prefix = ic->getParam("prefix");
219 docstring const sortas = prefix.empty() ? symbol : prefix;
221 entries[sortas] = NomenclEntry(symbol, desc, &par);
227 // we'll use our own stream, because we are going to defer everything.
228 // that's how we deal with the fact that we're probably inside a standard
229 // paragraph, and we don't want to be.
230 odocstringstream ods;
233 InsetLayout const & il = getLayout();
234 string const & tag = il.htmltag();
235 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
236 op.local_font->language()->lang());
238 xs << html::StartTag("div", "class='nomencl'")
239 << html::StartTag(tag, "class='nomencl'")
243 << html::StartTag("dl")
246 EntryMap::const_iterator eit = entries.begin();
247 EntryMap::const_iterator const een = entries.end();
248 for (; eit != een; ++eit) {
249 NomenclEntry const & ne = eit->second;
250 string const parid = ne.par->magicLabel();
251 xs << html::StartTag("dt")
252 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
255 << html::EndTag("dt")
257 << html::StartTag("dd")
259 << html::EndTag("dd")
263 xs << html::EndTag("dl")
265 << html::EndTag("div")
272 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
274 switch (cmd.action()) {
276 case LFUN_INSET_MODIFY: {
277 InsetCommandParams p(NOMENCL_PRINT_CODE);
279 InsetCommand::string2params(to_utf8(cmd.argument()), p);
280 if (p.getCmdName().empty()) {
281 cur.noScreenUpdate();
291 InsetCommand::doDispatch(cur, cmd);
297 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
298 FuncStatus & status) const
300 switch (cmd.action()) {
302 case LFUN_INSET_DIALOG_UPDATE:
303 case LFUN_INSET_MODIFY:
304 status.setEnabled(true);
308 return InsetCommand::getStatus(cur, cmd, status);
313 // FIXME This should be changed to use the TOC. Perhaps
314 // that could be done when XHTML output is added.
315 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
317 os << "<glossary>\n";
319 InsetIterator it = inset_iterator_begin(buffer().inset());
321 if (it->lyxCode() == NOMENCL_CODE) {
322 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
324 } else if (!it->producesOutput()) {
325 // Ignore contents of insets that are not in output
326 size_t const depth = it.depth();
328 while (it.depth() > depth)
334 os << "</glossary>\n";
340 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
342 // nomenclWidest() determines and returns the widest used
343 // nomenclature symbol in the document
347 InsetNomencl const * nomencl = 0;
348 ParagraphList::const_iterator it = buffer.paragraphs().begin();
349 ParagraphList::const_iterator end = buffer.paragraphs().end();
351 for (; it != end; ++it) {
352 if (it->insetList().empty())
354 InsetList::const_iterator iit = it->insetList().begin();
355 InsetList::const_iterator eend = it->insetList().end();
356 for (; iit != eend; ++iit) {
357 Inset * inset = iit->inset;
358 if (inset->lyxCode() != NOMENCL_CODE)
360 nomencl = static_cast<InsetNomencl const *>(inset);
361 docstring const symbol =
362 nomencl->getParam("symbol");
363 // This is only an approximation,
364 // but the best we can get.
365 int const wx = use_gui ?
366 theFontMetrics(Font()).width(symbol) :
374 // return the widest (or an empty) string
378 // we have to encode the string properly
379 pair<docstring, docstring> latex_symb =
380 runparams.encoding->latexString(symb, runparams.dryrun);
381 if (!latex_symb.second.empty())
382 LYXERR0("Omitting uncodable characters '"
384 << "' in nomencl widest string!");
385 return latex_symb.first;
390 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
392 OutputParams runparams = runparams_in;
393 if (getParam("set_width") == "auto") {
394 docstring widest = nomenclWidest(buffer(), runparams);
395 // Set the label width via nomencl's command \nomlabelwidth.
396 // This must be output before the command \printnomenclature
397 if (!widest.empty()) {
398 os << "\\settowidth{\\nomlabelwidth}{"
402 } else if (getParam("set_width") == "custom") {
403 // custom length as optional arg of \printnomenclature
405 Length(to_ascii(getParam("width"))).asLatexString();
407 << from_ascii(getCmdName())
414 // output the command \printnomenclature
415 os << getCommand(runparams);
419 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
421 features.require("nomencl");
425 InsetCode InsetPrintNomencl::lyxCode() const
427 return NOMENCL_PRINT_CODE;
431 string InsetPrintNomencl::contextMenuName() const
433 return "context-nomenclprint";