]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.cpp
Fix some bugs in the bibinfo caching mechanism. Comments to follow.
[lyx.git] / src / insets / InsetCommandParams.cpp
1 /**
2  * \file InsetCommandParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Georg Baum
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14 #include <algorithm>
15
16 #include "InsetCommandParams.h"
17
18 #include "InsetBibitem.h"
19 #include "InsetBibtex.h"
20 #include "InsetCitation.h"
21 #include "InsetFloatList.h"
22 #include "InsetHyperlink.h"
23 #include "InsetInclude.h"
24 #include "InsetIndex.h"
25 #include "InsetLabel.h"
26 #include "InsetNomencl.h"
27 #include "InsetRef.h"
28 #include "InsetTOC.h"
29
30 #include "Encoding.h"
31 #include "Lexer.h"
32 #include "OutputParams.h"
33
34 #include "frontends/alert.h"
35
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/ExceptionMessage.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41
42 #include "support/lassert.h"
43
44 using namespace std;
45 using namespace lyx::support;
46
47
48 namespace lyx {
49
50 /// Get information for \p code and command \p cmdName.
51 /// Returns 0 if the combination is not known.  [FIXME: 0?]
52 /// Don't call this without first making sure the command name is
53 /// acceptable to the inset.
54 static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
55 {
56         switch (code) {
57         case BIBITEM_CODE:
58                 return InsetBibitem::findInfo(cmdName);
59         case BIBTEX_CODE:
60                 return InsetBibtex::findInfo(cmdName);
61         case CITE_CODE:
62                 return InsetCitation::findInfo(cmdName);
63         case FLOAT_LIST_CODE:
64                 return InsetFloatList::findInfo(cmdName);
65         case HYPERLINK_CODE:
66                 return InsetHyperlink::findInfo(cmdName);
67         case INCLUDE_CODE:
68                 return InsetInclude::findInfo(cmdName);
69         case INDEX_PRINT_CODE:
70                 return InsetPrintIndex::findInfo(cmdName);
71         case LABEL_CODE:
72                 return InsetLabel::findInfo(cmdName);
73         case NOMENCL_CODE:
74                 return InsetNomencl::findInfo(cmdName);
75         case NOMENCL_PRINT_CODE:
76                 return InsetPrintNomencl::findInfo(cmdName);
77         case REF_CODE:
78                 return InsetRef::findInfo(cmdName);
79         case TOC_CODE:
80                 return InsetTOC::findInfo(cmdName);
81         default:
82                 LASSERT(false, /**/);
83         }
84         static const ParamInfo pi;
85         return pi; // to silence the warning
86 }
87
88
89 /////////////////////////////////////////////////////////////////////
90 //
91 // ParamInfo::ParamData
92 //
93 /////////////////////////////////////////////////////////////////////
94
95 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
96                                 ParamHandling h)
97         : name_(s), type_(t), handling_(h)
98 {}
99
100
101 bool ParamInfo::ParamData::isOptional() const
102 {
103         return type_ == ParamInfo::LATEX_OPTIONAL;
104 }
105
106
107 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
108 {
109         return name() == rhs.name() && type() == rhs.type()
110                 && handling() == rhs.handling();
111 }
112
113
114 bool ParamInfo::hasParam(std::string const & name) const
115 {
116         const_iterator it = begin();
117         const_iterator last = end();
118         for (; it != last; ++it) {
119                 if (it->name() == name)
120                         return true;
121         }
122         return false;
123 }
124
125
126 void ParamInfo::add(std::string const & name, ParamType type,
127                     ParamHandling handling)
128
129         info_.push_back(ParamData(name, type, handling)); 
130 }
131
132
133 bool ParamInfo::operator==(ParamInfo const & rhs) const
134 {
135         if (size() != rhs.size())
136                 return false;
137         return equal(begin(), end(), rhs.begin());
138 }
139
140
141 ParamInfo::ParamData const & 
142         ParamInfo::operator[](std::string const & name) const
143 {
144         LASSERT(hasParam(name), /**/);
145         const_iterator it = begin();
146         const_iterator last = end();
147         for (; it != last; ++it) {
148                 if (it->name() == name)
149                         return *it;
150         }
151         return *it; // silence warning
152 }
153
154
155 /////////////////////////////////////////////////////////////////////
156 //
157 // InsetCommandParams
158 //
159 /////////////////////////////////////////////////////////////////////
160
161
162 InsetCommandParams::InsetCommandParams(InsetCode code)
163         : insetCode_(code), preview_(false)
164 {
165         cmdName_ = getDefaultCmd(code);
166         info_ = findInfo(code, cmdName_);
167 }
168
169
170 InsetCommandParams::InsetCommandParams(InsetCode code,
171         string const & cmdName)
172         : insetCode_(code), cmdName_(cmdName), preview_(false)
173 {
174         info_ = findInfo(code, cmdName);
175 }
176
177
178 std::string InsetCommandParams::insetType() const
179 {
180         return insetName(insetCode_);
181 }
182
183
184 string InsetCommandParams::getDefaultCmd(InsetCode code)
185 {
186         switch (code) {
187                 case BIBITEM_CODE: 
188                         return InsetBibitem::defaultCommand();
189                 case BIBTEX_CODE:
190                         return InsetBibtex::defaultCommand();
191                 case CITE_CODE:
192                         return InsetCitation::defaultCommand();
193                 case FLOAT_LIST_CODE:
194                         return InsetFloatList::defaultCommand();
195                 case HYPERLINK_CODE:
196                         return InsetHyperlink::defaultCommand();
197                 case INCLUDE_CODE:
198                         return InsetInclude::defaultCommand();
199                 case INDEX_PRINT_CODE:
200                         return InsetPrintIndex::defaultCommand();
201                 case LABEL_CODE:
202                         return InsetLabel::defaultCommand();
203                 case NOMENCL_CODE:
204                         return InsetNomencl::defaultCommand();
205                 case NOMENCL_PRINT_CODE:
206                         return InsetPrintNomencl::defaultCommand();
207                 case REF_CODE:
208                         return InsetRef::defaultCommand();
209                 case TOC_CODE:
210                         return InsetTOC::defaultCommand();
211                 default:
212                         LASSERT(false, /**/);
213         }
214         return string(); // silence the warning
215 }
216
217
218 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
219 {
220         switch (code) {
221                 case BIBITEM_CODE: 
222                         return InsetBibitem::isCompatibleCommand(s);
223                 case BIBTEX_CODE:
224                         return InsetBibtex::isCompatibleCommand(s);
225                 case CITE_CODE:
226                         return InsetCitation::isCompatibleCommand(s);
227                 case FLOAT_LIST_CODE:
228                         return InsetFloatList::isCompatibleCommand(s);
229                 case HYPERLINK_CODE:
230                         return InsetHyperlink::isCompatibleCommand(s);
231                 case INCLUDE_CODE:
232                         return InsetInclude::isCompatibleCommand(s);
233                 case INDEX_PRINT_CODE:
234                         return InsetPrintIndex::isCompatibleCommand(s);
235                 case LABEL_CODE:
236                         return InsetLabel::isCompatibleCommand(s);
237                 case NOMENCL_CODE:
238                         return InsetNomencl::isCompatibleCommand(s);
239                 case NOMENCL_PRINT_CODE:
240                         return InsetPrintNomencl::isCompatibleCommand(s);
241                 case REF_CODE:
242                         return InsetRef::isCompatibleCommand(s);
243                 case TOC_CODE:
244                         return InsetTOC::isCompatibleCommand(s);
245                 default:
246                         LASSERT(false, /**/);
247         }
248         return false; // silence the warning
249 }
250
251
252 void InsetCommandParams::setCmdName(string const & name)
253 {
254         if (!isCompatibleCommand(insetCode_, name)) {
255                 LYXERR0("InsetCommand: Incompatible command name " << 
256                                 name << ".");
257                 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
258                                        _("Incompatible command name."));
259         }
260
261         cmdName_ = name;
262         info_ = findInfo(insetCode_, cmdName_);
263 }
264
265
266 void InsetCommandParams::read(Lexer & lex)
267 {
268         lex.setContext("InsetCommandParams::read");
269         lex >> insetName(insetCode_).c_str();
270         lex >> "LatexCommand";
271         lex >> cmdName_;
272         if (!isCompatibleCommand(insetCode_, cmdName_)) {
273                 lex.printError("Incompatible command name " + cmdName_ + ".");
274                 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
275                                        _("Incompatible command name."));
276         }
277
278         info_ = findInfo(insetCode_, cmdName_);
279         
280         string token;
281         while (lex.isOK()) {
282                 lex.next();
283                 token = lex.getString();
284                 if (token == "\\end_inset")
285                         break;
286                 if (token == "preview") {
287                         lex.next();
288                         preview_ = lex.getBool();
289                         continue;
290                 }
291                 if (info_.hasParam(token)) {
292                         lex.next(true);
293                         params_[token] = lex.getDocString();
294                 } else {
295                         lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
296                         throw ExceptionMessage(WarningException,
297                                 _("InsetCommandParams: ") + from_ascii(cmdName_),
298                                 _("Unknown parameter name: ") + from_utf8(token));
299                 }
300         }
301         if (token != "\\end_inset") {
302                 lex.printError("Missing \\end_inset at this point. "
303                                "Read: `$$Token'");
304                 throw ExceptionMessage(WarningException,
305                         _("InsetCommandParams Error: "),
306                         _("Missing \\end_inset at this point: ") + from_utf8(token));
307         }
308 }
309
310
311 void InsetCommandParams::write(ostream & os) const
312 {
313         os << "CommandInset " << insetType() << '\n';
314         os << "LatexCommand " << cmdName_ << '\n';
315         if (preview_)
316                 os << "preview true\n";
317         ParamInfo::const_iterator it  = info_.begin();
318         ParamInfo::const_iterator end = info_.end();
319         for (; it != end; ++it) {
320                 std::string const & name = it->name();
321                 docstring const & data = (*this)[name];
322                 if (!data.empty()) {
323                         // FIXME UNICODE
324                         os << name << ' '
325                            << Lexer::quoteString(to_utf8(data))
326                            << '\n';
327                 }
328         }
329 }
330
331
332 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
333 {
334         if (!ci->isOptional()) {
335                 LASSERT(false, /**/);
336         }
337         ++ci; // we want to start with the next one
338         ParamInfo::const_iterator end = info_.end();
339         for (; ci != end; ++ci) {
340                 switch (ci->type()) {
341                 case ParamInfo::LYX_INTERNAL:
342                         break;
343
344                 case ParamInfo::LATEX_REQUIRED:
345                         return false;
346
347                 case ParamInfo::LATEX_OPTIONAL: {
348                         std::string const & name = ci->name();
349                         docstring const & data = (*this)[name];
350                         if (!data.empty())
351                                 return true;
352                         break;
353                 }
354
355                 } //end switch
356         }
357         return false;
358 }
359
360
361
362 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
363                                              docstring const & command,
364                                              ParamInfo::ParamHandling handling) const
365 {
366         docstring result;
367         if (handling == ParamInfo::HANDLING_LATEXIFY) {
368                 docstring uncodable;
369                 for (size_t n = 0; n < command.size(); ++n) {
370                         try {
371                                 result += runparams.encoding->latexChar(command[n]);
372                         } catch (EncodingException & /* e */) {
373                                 LYXERR0("Uncodable character in command inset!");
374                                 if (runparams.dryrun) {
375                                         result += "<" + _("LyX Warning: ")
376                                                 + _("uncodable character") + " '";
377                                         result += docstring(1, command[n]);
378                                         result += "'>";
379                                 } else
380                                         uncodable += command[n];
381                         }
382                 }
383                 if (!uncodable.empty()) {
384                         // issue a warning about omitted characters
385                         // FIXME: should be passed to the error dialog
386                         frontend::Alert::warning(_("Uncodable characters"),
387                                 bformat(_("The following characters that are used in the inset %1$s are not\n"
388                                           "representable in the current encoding and therefore have been omitted:\n%2$s."),
389                                         from_utf8(insetType()), uncodable));
390                 }
391         } else if (handling == ParamInfo::HANDLING_ESCAPE)
392                 result = escape(command);
393         else
394                 result = command;
395
396         return result;
397 }
398
399
400 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
401 {
402         docstring s = '\\' + from_ascii(cmdName_);
403         bool noparam = true;
404         ParamInfo::const_iterator it  = info_.begin();
405         ParamInfo::const_iterator end = info_.end();
406         for (; it != end; ++it) {
407                 std::string const & name = it->name();
408                 switch (it->type()) {
409                 case ParamInfo::LYX_INTERNAL:
410                         break;
411
412                 case ParamInfo::LATEX_REQUIRED: {
413                         docstring const & data =
414                                 prepareCommand(runparams, (*this)[name], it->handling());
415                         s += '{' + data + '}';
416                         noparam = false;
417                         break;
418                 }
419                 case ParamInfo::LATEX_OPTIONAL: {
420                         docstring const & data =
421                                 prepareCommand(runparams, (*this)[name], it->handling());
422                         if (!data.empty()) {
423                                 s += '[' + data + ']';
424                                 noparam = false;
425                         } else if (writeEmptyOptional(it)) {
426                                         s += "[]";
427                                         noparam = false;
428                         }
429                         break;
430                 } 
431                 } //end switch
432         }
433         if (noparam)
434                 // Make sure that following stuff does not change the
435                 // command name.
436                 s += "{}";
437         return s;
438 }
439
440
441 docstring InsetCommandParams::getFirstNonOptParam() const
442 {
443         ParamInfo::const_iterator it = 
444                 find_if(info_.begin(), info_.end(), 
445                         not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
446         if (it == info_.end()) {
447                 LASSERT(false, return docstring());
448         }
449         return (*this)[it->name()];
450 }
451
452
453 docstring const & InsetCommandParams::operator[](string const & name) const
454 {
455         static const docstring dummy; //so we don't return a ref to temporary
456         LASSERT(info_.hasParam(name), return dummy);
457         ParamMap::const_iterator data = params_.find(name);
458         if (data == params_.end() || data->second.empty())
459                 return dummy;
460         return data->second;
461 }
462
463
464 docstring & InsetCommandParams::operator[](string const & name)
465 {
466         LASSERT(info_.hasParam(name), /**/);
467         return params_[name];
468 }
469
470
471 void InsetCommandParams::clear()
472 {
473         params_.clear();
474 }
475
476
477 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
478 {
479         return o1.insetCode_ == o2.insetCode_
480                 && o1.cmdName_ == o2.cmdName_
481                 && o1.info_ == o2.info_
482                 && o1.params_ == o2.params_
483                 && o1.preview_ == o2.preview_;
484 }
485
486
487 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)
488 {
489         return !(o1 == o2);
490 }
491
492
493 } // namespace lyx