2 * \file InsetCommandParams.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
16 #include "InsetCommandParams.h"
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"
32 #include "OutputParams.h"
34 #include "frontends/alert.h"
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"
42 #include "support/lassert.h"
45 using namespace lyx::support;
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)
58 return InsetBibitem::findInfo(cmdName);
60 return InsetBibtex::findInfo(cmdName);
62 return InsetCitation::findInfo(cmdName);
64 return InsetFloatList::findInfo(cmdName);
66 return InsetHyperlink::findInfo(cmdName);
68 return InsetInclude::findInfo(cmdName);
69 case INDEX_PRINT_CODE:
70 return InsetPrintIndex::findInfo(cmdName);
72 return InsetLabel::findInfo(cmdName);
74 return InsetNomencl::findInfo(cmdName);
75 case NOMENCL_PRINT_CODE:
76 return InsetPrintNomencl::findInfo(cmdName);
78 return InsetRef::findInfo(cmdName);
80 return InsetTOC::findInfo(cmdName);
84 static const ParamInfo pi;
85 return pi; // to silence the warning
89 /////////////////////////////////////////////////////////////////////
91 // ParamInfo::ParamData
93 /////////////////////////////////////////////////////////////////////
95 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
97 : name_(s), type_(t), handling_(h)
101 bool ParamInfo::ParamData::isOptional() const
103 return type_ == ParamInfo::LATEX_OPTIONAL;
107 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
109 return name() == rhs.name() && type() == rhs.type()
110 && handling() == rhs.handling();
114 bool ParamInfo::hasParam(std::string const & name) const
116 const_iterator it = begin();
117 const_iterator last = end();
118 for (; it != last; ++it) {
119 if (it->name() == name)
126 void ParamInfo::add(std::string const & name, ParamType type,
127 ParamHandling handling)
129 info_.push_back(ParamData(name, type, handling));
133 bool ParamInfo::operator==(ParamInfo const & rhs) const
135 if (size() != rhs.size())
137 return equal(begin(), end(), rhs.begin());
141 ParamInfo::ParamData const &
142 ParamInfo::operator[](std::string const & name) const
144 LASSERT(hasParam(name), /**/);
145 const_iterator it = begin();
146 const_iterator last = end();
147 for (; it != last; ++it) {
148 if (it->name() == name)
151 return *it; // silence warning
155 /////////////////////////////////////////////////////////////////////
157 // InsetCommandParams
159 /////////////////////////////////////////////////////////////////////
162 InsetCommandParams::InsetCommandParams(InsetCode code)
163 : insetCode_(code), preview_(false)
165 cmdName_ = getDefaultCmd(code);
166 info_ = findInfo(code, cmdName_);
170 InsetCommandParams::InsetCommandParams(InsetCode code,
171 string const & cmdName)
172 : insetCode_(code), cmdName_(cmdName), preview_(false)
174 info_ = findInfo(code, cmdName);
178 std::string InsetCommandParams::insetType() const
180 return insetName(insetCode_);
184 string InsetCommandParams::getDefaultCmd(InsetCode code)
188 return InsetBibitem::defaultCommand();
190 return InsetBibtex::defaultCommand();
192 return InsetCitation::defaultCommand();
193 case FLOAT_LIST_CODE:
194 return InsetFloatList::defaultCommand();
196 return InsetHyperlink::defaultCommand();
198 return InsetInclude::defaultCommand();
199 case INDEX_PRINT_CODE:
200 return InsetPrintIndex::defaultCommand();
202 return InsetLabel::defaultCommand();
204 return InsetNomencl::defaultCommand();
205 case NOMENCL_PRINT_CODE:
206 return InsetPrintNomencl::defaultCommand();
208 return InsetRef::defaultCommand();
210 return InsetTOC::defaultCommand();
212 LASSERT(false, /**/);
214 return string(); // silence the warning
218 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
222 return InsetBibitem::isCompatibleCommand(s);
224 return InsetBibtex::isCompatibleCommand(s);
226 return InsetCitation::isCompatibleCommand(s);
227 case FLOAT_LIST_CODE:
228 return InsetFloatList::isCompatibleCommand(s);
230 return InsetHyperlink::isCompatibleCommand(s);
232 return InsetInclude::isCompatibleCommand(s);
233 case INDEX_PRINT_CODE:
234 return InsetPrintIndex::isCompatibleCommand(s);
236 return InsetLabel::isCompatibleCommand(s);
238 return InsetNomencl::isCompatibleCommand(s);
239 case NOMENCL_PRINT_CODE:
240 return InsetPrintNomencl::isCompatibleCommand(s);
242 return InsetRef::isCompatibleCommand(s);
244 return InsetTOC::isCompatibleCommand(s);
246 LASSERT(false, /**/);
248 return false; // silence the warning
252 void InsetCommandParams::setCmdName(string const & name)
254 if (!isCompatibleCommand(insetCode_, name)) {
255 LYXERR0("InsetCommand: Incompatible command name " <<
257 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
258 _("Incompatible command name."));
262 info_ = findInfo(insetCode_, cmdName_);
266 void InsetCommandParams::read(Lexer & lex)
268 lex.setContext("InsetCommandParams::read");
269 lex >> insetName(insetCode_).c_str();
270 lex >> "LatexCommand";
272 if (!isCompatibleCommand(insetCode_, cmdName_)) {
273 lex.printError("Incompatible command name " + cmdName_ + ".");
274 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
275 _("Incompatible command name."));
278 info_ = findInfo(insetCode_, cmdName_);
283 token = lex.getString();
284 if (token == "\\end_inset")
286 if (token == "preview") {
288 preview_ = lex.getBool();
291 if (info_.hasParam(token)) {
293 params_[token] = lex.getDocString();
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));
301 if (token != "\\end_inset") {
302 lex.printError("Missing \\end_inset at this point. "
304 throw ExceptionMessage(WarningException,
305 _("InsetCommandParams Error: "),
306 _("Missing \\end_inset at this point: ") + from_utf8(token));
311 void InsetCommandParams::write(ostream & os) const
313 os << "CommandInset " << insetType() << '\n';
314 os << "LatexCommand " << cmdName_ << '\n';
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];
325 << Lexer::quoteString(to_utf8(data))
332 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
334 if (!ci->isOptional()) {
335 LASSERT(false, /**/);
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:
344 case ParamInfo::LATEX_REQUIRED:
347 case ParamInfo::LATEX_OPTIONAL: {
348 std::string const & name = ci->name();
349 docstring const & data = (*this)[name];
362 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
363 docstring const & command,
364 ParamInfo::ParamHandling handling) const
367 if (handling == ParamInfo::HANDLING_LATEXIFY) {
369 for (size_t n = 0; n < command.size(); ++n) {
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]);
380 uncodable += command[n];
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));
391 } else if (handling == ParamInfo::HANDLING_ESCAPE)
392 result = escape(command);
400 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
402 docstring s = '\\' + from_ascii(cmdName_);
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:
412 case ParamInfo::LATEX_REQUIRED: {
413 docstring const & data =
414 prepareCommand(runparams, (*this)[name], it->handling());
415 s += '{' + data + '}';
419 case ParamInfo::LATEX_OPTIONAL: {
420 docstring const & data =
421 prepareCommand(runparams, (*this)[name], it->handling());
423 s += '[' + data + ']';
425 } else if (writeEmptyOptional(it)) {
434 // Make sure that following stuff does not change the
441 docstring InsetCommandParams::getFirstNonOptParam() const
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());
449 return (*this)[it->name()];
453 docstring const & InsetCommandParams::operator[](string const & name) const
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())
464 docstring & InsetCommandParams::operator[](string const & name)
466 LASSERT(info_.hasParam(name), /**/);
467 return params_[name];
471 void InsetCommandParams::clear()
477 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
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_;
487 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)