]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Use LOCK in all cases
[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 "InsetList.h"
26 #include "Language.h"
27 #include "LaTeXFeatures.h"
28 #include "LyX.h"
29 #include "xml.h"
30 #include "texstream.h"
31 #include "TocBackend.h"
32
33 #include "frontends/FontMetrics.h"
34
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"
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46
47 /////////////////////////////////////////////////////////////////////
48 //
49 // InsetNomencl
50 //
51 /////////////////////////////////////////////////////////////////////
52
53 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
54         : InsetCommand(buf, p)
55 {}
56
57
58 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
59 {
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);
70         }
71         return param_info_;
72 }
73
74
75 docstring InsetNomencl::screenLabel() const
76 {
77         size_t const maxLabelChars = 25;
78         docstring label = _("Nom: ") + getParam("symbol");
79         support::truncateWithEllipsis(label, maxLabelChars);
80         return label;
81 }
82
83
84 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
85 {
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");
91         return tip;
92 }
93
94
95 int InsetNomencl::plaintext(odocstringstream & os,
96         OutputParams const &, size_t) const
97 {
98         docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
99         os << s;
100         return s.size();
101 }
102
103
104 void InsetNomencl::docbook(XMLStream & xs, OutputParams const &) const
105 {
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");
110 }
111
112
113 docstring InsetNomencl::xhtml(XMLStream &, OutputParams const &) const
114 {
115         return docstring();
116 }
117
118
119 void InsetNomencl::validate(LaTeXFeatures & features) const
120 {
121         features.require("nomencl");
122         InsetCommand::validate(features);
123 }
124
125
126 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
127                                                         UpdateType, TocBackend & backend) const
128 {
129         docstring const str = getParam("symbol");
130         TocBuilder & b = backend.builder("nomencl");
131         b.pushItem(cpit, str, output_active);
132         b.pop();
133 }
134
135
136 /////////////////////////////////////////////////////////////////////
137 //
138 // InsetPrintNomencl
139 //
140 /////////////////////////////////////////////////////////////////////
141
142 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
143         : InsetCommand(buf, p)
144 {}
145
146
147 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
148 {
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);
157                 // custom width
158                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
159         }
160         return param_info_;
161 }
162
163
164 docstring InsetPrintNomencl::screenLabel() const
165 {
166         return _("Nomenclature");
167 }
168
169
170 struct NomenclEntry {
171         NomenclEntry() : par(nullptr) {}
172         NomenclEntry(docstring s, docstring d, Paragraph const * p)
173           : symbol(s), desc(d), par(p)
174         {}
175
176         docstring symbol;
177         docstring desc;
178         Paragraph const * par;
179 };
180
181
182 typedef map<docstring, NomenclEntry > EntryMap;
183
184
185 docstring InsetPrintNomencl::xhtml(XMLStream &, OutputParams const & op) const
186 {
187         shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
188
189         EntryMap entries;
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());
196                 if (!inset)
197                         return docstring();
198                 InsetCommand const * ic = inset->asInsetCommand();
199                 if (!ic)
200                         return docstring();
201
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;
208
209                 entries[sortas] = NomenclEntry(symbol, desc, &par);
210         }
211
212         if (entries.empty())
213                 return docstring();
214
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;
219         XMLStream xs(ods);
220
221         InsetLayout const & il = getLayout();
222         string const & tag = il.htmltag();
223         docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
224                 op.local_font->language()->lang());
225
226         xs << xml::StartTag("div", "class='nomencl'")
227            << xml::StartTag(tag, "class='nomencl'")
228                  << toclabel
229                  << xml::EndTag(tag)
230            << xml::CR()
231            << xml::StartTag("dl")
232            << xml::CR();
233
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'")
241                    << ne.symbol
242                    << xml::EndTag("a")
243                    << xml::EndTag("dt")
244                    << xml::CR()
245                    << xml::StartTag("dd")
246                    << ne.desc
247                    << xml::EndTag("dd")
248                    << xml::CR();
249         }
250
251         xs << xml::EndTag("dl")
252            << xml::CR()
253            << xml::EndTag("div")
254            << xml::CR();
255
256         return ods.str();
257 }
258
259
260 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
261 {
262         switch (cmd.action()) {
263
264         case LFUN_INSET_MODIFY: {
265                 InsetCommandParams p(NOMENCL_PRINT_CODE);
266                 // FIXME UNICODE
267                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
268                 if (p.getCmdName().empty()) {
269                         cur.noScreenUpdate();
270                         break;
271                 }
272
273                 cur.recordUndo();
274                 setParams(p);
275                 break;
276         }
277
278         default:
279                 InsetCommand::doDispatch(cur, cmd);
280                 break;
281         }
282 }
283
284
285 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
286         FuncStatus & status) const
287 {
288         switch (cmd.action()) {
289
290         case LFUN_INSET_DIALOG_UPDATE:
291         case LFUN_INSET_MODIFY:
292                 status.setEnabled(true);
293                 return true;
294
295         default:
296                 return InsetCommand::getStatus(cur, cmd, status);
297         }
298 }
299
300
301 void InsetPrintNomencl::docbook(XMLStream & xs, OutputParams const & runparams) const
302 {
303         shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
304
305         EntryMap entries;
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());
312                 if (!inset)
313                         return;
314                 InsetCommand const * ic = inset->asInsetCommand();
315                 if (!ic)
316                         return;
317
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;
324
325                 entries[sortas] = NomenclEntry(symbol, desc, &par);
326         }
327
328         if (entries.empty())
329                 return;
330
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...
333
334         docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
335                                                                                          runparams.local_font->language()->lang());
336
337         xs << xml::StartTag("glossary");
338         xs << xml::CR();
339         xs << xml::StartTag("title");
340         xs << toclabel;
341         xs << xml::EndTag("title");
342         xs << xml::CR();
343
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;
348
349                 xs << xml::StartTag("glossentry", "xml:id=\"" + xml::cleanID(from_ascii("nomen") + ne.symbol) + "\"");
350                 xs << xml::CR();
351                 xs << xml::StartTag("glossterm");
352                 xs << ne.symbol;
353                 xs << xml::EndTag("glossterm");
354                 xs << xml::CR();
355                 xs << xml::StartTag("glossdef");
356                 xs << xml::CR();
357                 xs << xml::StartTag("para");
358                 xs << ne.desc;
359                 xs << xml::EndTag("para");
360                 xs << xml::CR();
361                 xs << xml::EndTag("glossdef");
362                 xs << xml::CR();
363                 xs << xml::EndTag("glossentry");
364                 xs << xml::CR();
365         }
366
367         xs << xml::EndTag("glossary");
368         xs << xml::CR();
369 }
370
371
372 namespace {
373 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
374 {
375         // nomenclWidest() determines and returns the widest used
376         // nomenclature symbol in the document
377
378         int w = 0;
379         docstring symb;
380         InsetNomencl const * nomencl = nullptr;
381         ParagraphList::const_iterator it = buffer.paragraphs().begin();
382         ParagraphList::const_iterator end = buffer.paragraphs().end();
383
384         for (; it != end; ++it) {
385                 if (it->insetList().empty())
386                         continue;
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)
392                                 continue;
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)
398                                 : docstring();
399                         // This is only an approximation,
400                         // but the best we can get.
401                         int const wx = use_gui ?
402                                 theFontMetrics(Font()).width(symbol) :
403                                 symbol.size();
404                         if (wx > w) {
405                                 w = wx;
406                                 symb = symbol;
407                         }
408                 }
409         }
410         // return the widest (or an empty) string
411         return symb;
412 }
413 } // namespace
414
415
416 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
417 {
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}{"
425                            << widest
426                            << "}\n";
427                 }
428         } else if (getParam("set_width") == "custom") {
429                 // custom length as optional arg of \printnomenclature
430                 string const width =
431                         Length(to_ascii(getParam("width"))).asLatexString();
432                 os << '\\'
433                    << from_ascii(getCmdName())
434                    << '['
435                    << from_ascii(width)
436                    << "]"
437                    << termcmd;
438                 return;
439         }
440         // output the command \printnomenclature
441         os << getCommand(runparams);
442 }
443
444
445 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
446 {
447         features.useInsetLayout(getLayout());
448         features.require("nomencl");
449 }
450
451
452 InsetCode InsetPrintNomencl::lyxCode() const
453 {
454         return NOMENCL_PRINT_CODE;
455 }
456
457
458 string InsetPrintNomencl::contextMenuName() const
459 {
460         return "context-nomenclprint";
461 }
462
463
464 } // namespace lyx