]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Do not escape makeindex chars in bibitemWidest.
[lyx.git] / src / insets / InsetNomencl.cpp
1 /**
2  * \file InsetNomencl.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author O. U. Baran
8  * \author Uwe Stöhr
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13 #include <config.h>
14
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
17
18 #include "Buffer.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "Font.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Length.h"
30 #include "LyX.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
33 #include "sgml.h"
34 #include "texstream.h"
35 #include "TocBackend.h"
36
37 #include "frontends/FontMetrics.h"
38
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49
50 /////////////////////////////////////////////////////////////////////
51 //
52 // InsetNomencl
53 //
54 /////////////////////////////////////////////////////////////////////
55
56 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
57         : InsetCommand(buf, p),
58           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
59 {}
60
61
62 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
63 {
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::ParamHandling(ParamInfo::HANDLING_LATEXIFY
69                                                          | ParamInfo::HANDLING_INDEX_ESCAPE));
70                 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
71                                 ParamInfo::ParamHandling(ParamInfo::HANDLING_LATEXIFY
72                                                          | ParamInfo::HANDLING_INDEX_ESCAPE));
73                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
74         }
75         return param_info_;
76 }
77
78
79 docstring InsetNomencl::screenLabel() const
80 {
81         size_t const maxLabelChars = 25;
82         docstring label = _("Nom: ") + getParam("symbol");
83         support::truncateWithEllipsis(label, maxLabelChars);
84         return label;
85 }
86
87
88 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
89 {
90         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
91         tip += _("Description: ") + "\t"
92                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
93         if (!getParam("prefix").empty())
94                 tip += "\n" + _("Sorting: ") + getParam("prefix");
95         return tip;
96 }
97
98
99 int InsetNomencl::plaintext(odocstringstream & os,
100         OutputParams const &, size_t) const
101 {
102         docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
103         os << s;
104         return s.size();
105 }
106
107
108 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
109 {
110         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
111            << sgml::escapeString(getParam("symbol"))
112            << "</glossterm>";
113         return 0;
114 }
115
116
117 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
118 {
119         return docstring();
120 }
121
122
123 int InsetNomencl::docbookGlossary(odocstream & os) const
124 {
125         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
126            << "<glossterm>"
127            << sgml::escapeString(getParam("symbol"))
128            << "</glossterm>\n"
129            << "<glossdef><para>"
130            << sgml::escapeString(getParam("description"))
131            << "</para></glossdef>\n"
132            <<"</glossentry>\n";
133         return 4;
134 }
135
136
137 void InsetNomencl::validate(LaTeXFeatures & features) const
138 {
139         features.require("nomencl");
140         InsetCommand::validate(features);
141 }
142
143
144 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
145                                                         UpdateType, TocBackend & backend) const
146 {
147         docstring const str = getParam("symbol");
148         TocBuilder & b = backend.builder("nomencl");
149         b.pushItem(cpit, str, output_active);
150         b.pop();
151 }
152
153
154 /////////////////////////////////////////////////////////////////////
155 //
156 // InsetPrintNomencl
157 //
158 /////////////////////////////////////////////////////////////////////
159
160 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
161         : InsetCommand(buf, p)
162 {}
163
164
165 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
166 {
167         // The symbol width is set via nomencl's \nomlabelwidth in
168         // InsetPrintNomencl::latex and not as optional parameter of
169         // \printnomenclature
170         static ParamInfo param_info_;
171         if (param_info_.empty()) {
172                 // how is the width set?
173                 // values: none|auto|custom
174                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
175                 // custom width
176                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
177         }
178         return param_info_;
179 }
180
181
182 docstring InsetPrintNomencl::screenLabel() const
183 {
184         return _("Nomenclature");
185 }
186
187
188 struct NomenclEntry {
189         NomenclEntry() : par(0) {}
190         NomenclEntry(docstring s, docstring d, Paragraph const * p)
191           : symbol(s), desc(d), par(p)
192         {}
193
194         docstring symbol;
195         docstring desc;
196         Paragraph const * par;
197 };
198
199
200 typedef map<docstring, NomenclEntry > EntryMap;
201
202
203 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
204 {
205         shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
206
207         EntryMap entries;
208         Toc::const_iterator it = toc->begin();
209         Toc::const_iterator const en = toc->end();
210         for (; it != en; ++it) {
211                 DocIterator dit = it->dit();
212                 Paragraph const & par = dit.innerParagraph();
213                 Inset const * inset = par.getInset(dit.top().pos());
214                 if (!inset)
215                         return docstring();
216                 InsetCommand const * ic = inset->asInsetCommand();
217                 if (!ic)
218                         return docstring();
219
220                 // FIXME We need a link to the paragraph here, so we
221                 // need some kind of struct.
222                 docstring const symbol = ic->getParam("symbol");
223                 docstring const desc = ic->getParam("description");
224                 docstring const prefix = ic->getParam("prefix");
225                 docstring const sortas = prefix.empty() ? symbol : prefix;
226
227                 entries[sortas] = NomenclEntry(symbol, desc, &par);
228         }
229
230         if (entries.empty())
231                 return docstring();
232
233         // we'll use our own stream, because we are going to defer everything.
234         // that's how we deal with the fact that we're probably inside a standard
235         // paragraph, and we don't want to be.
236         odocstringstream ods;
237         XHTMLStream xs(ods);
238
239         InsetLayout const & il = getLayout();
240         string const & tag = il.htmltag();
241         docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
242                 op.local_font->language()->lang());
243
244         xs << html::StartTag("div", "class='nomencl'")
245            << html::StartTag(tag, "class='nomencl'")
246                  << toclabel
247                  << html::EndTag(tag)
248            << html::CR()
249            << html::StartTag("dl")
250            << html::CR();
251
252         EntryMap::const_iterator eit = entries.begin();
253         EntryMap::const_iterator const een = entries.end();
254         for (; eit != een; ++eit) {
255                 NomenclEntry const & ne = eit->second;
256                 string const parid = ne.par->magicLabel();
257                 xs << html::StartTag("dt")
258                    << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
259                    << ne.symbol
260                    << html::EndTag("a")
261                    << html::EndTag("dt")
262                    << html::CR()
263                    << html::StartTag("dd")
264                    << ne.desc
265                    << html::EndTag("dd")
266                    << html::CR();
267         }
268
269         xs << html::EndTag("dl")
270            << html::CR()
271            << html::EndTag("div")
272            << html::CR();
273
274         return ods.str();
275 }
276
277
278 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
279 {
280         switch (cmd.action()) {
281
282         case LFUN_INSET_MODIFY: {
283                 InsetCommandParams p(NOMENCL_PRINT_CODE);
284                 // FIXME UNICODE
285                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
286                 if (p.getCmdName().empty()) {
287                         cur.noScreenUpdate();
288                         break;
289                 }
290
291                 cur.recordUndo();
292                 setParams(p);
293                 break;
294         }
295
296         default:
297                 InsetCommand::doDispatch(cur, cmd);
298                 break;
299         }
300 }
301
302
303 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
304         FuncStatus & status) const
305 {
306         switch (cmd.action()) {
307
308         case LFUN_INSET_DIALOG_UPDATE:
309         case LFUN_INSET_MODIFY:
310                 status.setEnabled(true);
311                 return true;
312
313         default:
314                 return InsetCommand::getStatus(cur, cmd, status);
315         }
316 }
317
318
319 // FIXME This should be changed to use the TOC. Perhaps
320 // that could be done when XHTML output is added.
321 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
322 {
323         os << "<glossary>\n";
324         int newlines = 2;
325         InsetIterator it = inset_iterator_begin(buffer().inset());
326         while (it) {
327                 if (it->lyxCode() == NOMENCL_CODE) {
328                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
329                         ++it;
330                 } else if (!it->producesOutput()) {
331                         // Ignore contents of insets that are not in output
332                         size_t const depth = it.depth();
333                         ++it;
334                         while (it.depth() > depth)
335                                 ++it;
336                 } else {
337                         ++it;
338                 }
339         }
340         os << "</glossary>\n";
341         return newlines;
342 }
343
344
345 namespace {
346 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
347 {
348         // nomenclWidest() determines and returns the widest used
349         // nomenclature symbol in the document
350
351         int w = 0;
352         docstring symb;
353         InsetNomencl const * nomencl = 0;
354         ParagraphList::const_iterator it = buffer.paragraphs().begin();
355         ParagraphList::const_iterator end = buffer.paragraphs().end();
356
357         for (; it != end; ++it) {
358                 if (it->insetList().empty())
359                         continue;
360                 InsetList::const_iterator iit = it->insetList().begin();
361                 InsetList::const_iterator eend = it->insetList().end();
362                 for (; iit != eend; ++iit) {
363                         Inset * inset = iit->inset;
364                         if (inset->lyxCode() != NOMENCL_CODE)
365                                 continue;
366                         nomencl = static_cast<InsetNomencl const *>(inset);
367                         // Use proper formatting. We do not escape makeindex chars here
368                         docstring const symbol = nomencl ?
369                                 nomencl->params().prepareCommand(runparams, nomencl->getParam("symbol"),
370                                                         ParamInfo::HANDLING_LATEXIFY)
371                                 : docstring();
372                         // This is only an approximation,
373                         // but the best we can get.
374                         int const wx = use_gui ?
375                                 theFontMetrics(Font()).width(symbol) :
376                                 symbol.size();
377                         if (wx > w) {
378                                 w = wx;
379                                 symb = symbol;
380                         }
381                 }
382         }
383         // return the widest (or an empty) string
384         return symb;
385 }
386 } // namespace
387
388
389 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
390 {
391         OutputParams runparams = runparams_in;
392         if (getParam("set_width") == "auto") {
393                 docstring widest = nomenclWidest(buffer(), runparams);
394                 // Set the label width via nomencl's command \nomlabelwidth.
395                 // This must be output before the command \printnomenclature
396                 if (!widest.empty()) {
397                         os << "\\settowidth{\\nomlabelwidth}{"
398                            << widest
399                            << "}\n";
400                 }
401         } else if (getParam("set_width") == "custom") {
402                 // custom length as optional arg of \printnomenclature
403                 string const width =
404                         Length(to_ascii(getParam("width"))).asLatexString();
405                 os << '\\'
406                    << from_ascii(getCmdName())
407                    << '['
408                    << from_ascii(width)
409                    << "]"
410                    << termcmd;
411                 return;
412         }
413         // output the command \printnomenclature
414         os << getCommand(runparams);
415 }
416
417
418 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
419 {
420         features.useInsetLayout(getLayout());
421         features.require("nomencl");
422 }
423
424
425 InsetCode InsetPrintNomencl::lyxCode() const
426 {
427         return NOMENCL_PRINT_CODE;
428 }
429
430
431 string InsetPrintNomencl::contextMenuName() const
432 {
433         return "context-nomenclprint";
434 }
435
436
437 } // namespace lyx