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.
15 #include "InsetCommandParams.h"
17 #include "InsetBibitem.h"
18 #include "InsetBibtex.h"
19 #include "InsetCitation.h"
20 #include "InsetFloatList.h"
21 #include "InsetHFill.h"
22 #include "InsetHyperlink.h"
23 #include "InsetInclude.h"
24 #include "InsetIndex.h"
25 #include "InsetLabel.h"
26 #include "InsetNomencl.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
38 #include <boost/assert.hpp>
41 using namespace lyx::support;
45 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t) :
50 bool ParamInfo::ParamData::isOptional() const
52 return type_ == ParamInfo::LATEX_OPTIONAL ||
53 type_ == ParamInfo::LATEX_KV_OPTIONAL;
57 bool ParamInfo::ParamData::isKeyValArg() const
59 return type_ == ParamInfo::LATEX_KV_REQUIRED
60 || type_ == ParamInfo::LATEX_KV_OPTIONAL;
64 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
66 return name() == rhs.name() && type() == rhs.type();
70 bool ParamInfo::hasParam(std::string const & name) const
72 const_iterator it = begin();
73 const_iterator last = end();
74 for (; it != last; ++it) {
75 if (it->name() == name)
76 return !it->isKeyValArg();
82 void ParamInfo::add(std::string const & name, ParamType type)
84 info_.push_back(ParamData(name, type));
88 bool ParamInfo::operator==(ParamInfo const & rhs) const
90 if (size() != rhs.size())
92 return equal(begin(), end(), rhs.begin());
96 ParamInfo::ParamData const &
97 ParamInfo::operator[](std::string const & name) const
99 BOOST_ASSERT(hasParam(name));
100 const_iterator it = begin();
101 const_iterator last = end();
102 for (; it != last; ++it) {
103 if (it->name() == name)
106 return *it; // silence warning
110 InsetCommandParams::InsetCommandParams(InsetCode code)
111 : insetCode_(code), preview_(false)
113 cmdName_ = getDefaultCmd(code);
114 info_ = findInfo(code, cmdName_);
118 InsetCommandParams::InsetCommandParams(InsetCode code,
119 string const & cmdName)
120 : insetCode_(code), cmdName_(cmdName), preview_(false)
122 info_ = findInfo(code, cmdName);
126 ParamInfo const & InsetCommandParams::findInfo(
127 InsetCode code, string const & cmdName)
131 return InsetBibitem::findInfo(cmdName);
133 return InsetBibtex::findInfo(cmdName);
135 return InsetCitation::findInfo(cmdName);
136 case FLOAT_LIST_CODE:
137 return InsetFloatList::findInfo(cmdName);
139 return InsetHFill::findInfo(cmdName);
141 return InsetHyperlink::findInfo(cmdName);
143 return InsetInclude::findInfo(cmdName);
144 case INDEX_PRINT_CODE:
145 return InsetPrintIndex::findInfo(cmdName);
147 return InsetLabel::findInfo(cmdName);
149 return InsetNomencl::findInfo(cmdName);
150 case NOMENCL_PRINT_CODE:
151 return InsetPrintNomencl::findInfo(cmdName);
153 return InsetRef::findInfo(cmdName);
155 return InsetTOC::findInfo(cmdName);
159 static const ParamInfo pi;
160 return pi; // to silence the warning
164 string InsetCommandParams::getDefaultCmd(InsetCode code) {
167 return InsetBibitem::defaultCommand();
169 return InsetBibtex::defaultCommand();
171 return InsetCitation::defaultCommand();
172 case FLOAT_LIST_CODE:
173 return InsetFloatList::defaultCommand();
175 return InsetHFill::defaultCommand();
177 return InsetHyperlink::defaultCommand();
179 return InsetInclude::defaultCommand();
180 case INDEX_PRINT_CODE:
181 return InsetPrintIndex::defaultCommand();
183 return InsetLabel::defaultCommand();
185 return InsetNomencl::defaultCommand();
186 case NOMENCL_PRINT_CODE:
187 return InsetPrintNomencl::defaultCommand();
189 return InsetRef::defaultCommand();
191 return InsetTOC::defaultCommand();
195 return string(); // silence the warning
199 bool InsetCommandParams::isCompatibleCommand(
200 InsetCode code, string const & s)
204 return InsetBibitem::isCompatibleCommand(s);
206 return InsetBibtex::isCompatibleCommand(s);
208 return InsetCitation::isCompatibleCommand(s);
209 case FLOAT_LIST_CODE:
210 return InsetFloatList::isCompatibleCommand(s);
212 return InsetHFill::isCompatibleCommand(s);
214 return InsetHyperlink::isCompatibleCommand(s);
216 return InsetInclude::isCompatibleCommand(s);
217 case INDEX_PRINT_CODE:
218 return InsetPrintIndex::isCompatibleCommand(s);
220 return InsetLabel::isCompatibleCommand(s);
222 return InsetNomencl::isCompatibleCommand(s);
223 case NOMENCL_PRINT_CODE:
224 return InsetPrintNomencl::isCompatibleCommand(s);
226 return InsetRef::isCompatibleCommand(s);
228 return InsetTOC::isCompatibleCommand(s);
232 return false; // silence the warning
236 void InsetCommandParams::setCmdName(string const & name)
238 if (!isCompatibleCommand(insetCode_, cmdName_)){
239 LYXERR0("InsetCommand: Incompatible command name " <<
241 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
242 _("Incompatible command name."));
246 info_ = findInfo(insetCode_, cmdName_);
250 void InsetCommandParams::read(Lexer & lex)
254 string const insetType = lex.getString();
255 InsetCode const code = insetCode(insetType);
256 if (code != insetCode_) {
257 lex.printError("InsetCommandParams: Attempt to change type of inset.");
258 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
259 _("Attempt to change type of parameters."));
265 string const test = lex.getString();
266 if (test != "LatexCommand") {
267 lex.printError("InsetCommandParams: No LatexCommand line found.");
268 throw ExceptionMessage(WarningException, _("InsetCommandParams error:"),
269 _("Can't find LatexCommand line."));
273 cmdName_ = lex.getString();
274 if (!isCompatibleCommand(insetCode_, cmdName_)){
275 lex.printError("InsetCommandParams: Incompatible command name " + cmdName_ + ".");
276 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
277 _("Incompatible command name."));
280 info_ = findInfo(insetCode_, cmdName_);
285 token = lex.getString();
286 if (token == "\\end_inset")
288 if (token == "preview") {
290 preview_ = lex.getBool();
293 if (info_.hasParam(token)) {
295 params_[token] = lex.getDocString();
297 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
298 throw ExceptionMessage(WarningException,
299 _("InsetCommandParams: ") + from_ascii(cmdName_),
300 _("Unknown parameter name: ") + from_utf8(token));
303 if (token != "\\end_inset") {
304 lex.printError("Missing \\end_inset at this point. "
306 throw ExceptionMessage(WarningException,
307 _("Missing \\end_inset at this point."),
313 void InsetCommandParams::write(ostream & os) const
315 os << "CommandInset " << insetType() << '\n';
316 os << "LatexCommand " << cmdName_ << '\n';
318 os << "preview true\n";
319 ParamInfo::const_iterator it = info_.begin();
320 ParamInfo::const_iterator end = info_.end();
321 for (; it != end; ++it) {
322 std::string const & name = it->name();
323 docstring const & data = (*this)[name];
327 << Lexer::quoteString(to_utf8(data))
334 docstring InsetCommandParams::makeKeyValArgument() const
338 ParamInfo::const_iterator it = info_.begin();
339 ParamInfo::const_iterator end = info_.end();
340 for (; it != end; ++it) {
343 string const & name = it->name();
344 docstring const & data = (*this)[name];
351 os << from_utf8(name) << "=" << data;
357 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
359 if (!ci->isOptional())
361 ++ci; // we want to start with the next one
362 ParamInfo::const_iterator end = info_.end();
363 for (; ci != end; ++ci) {
364 switch (ci->type()) {
365 case ParamInfo::LATEX_KEY:
366 case ParamInfo::LYX_INTERNAL:
369 case ParamInfo::LATEX_REQUIRED:
370 case ParamInfo::LATEX_KV_REQUIRED:
373 case ParamInfo::LATEX_OPTIONAL: {
374 std::string const & name = ci->name();
375 docstring const & data = (*this)[name];
381 case ParamInfo::LATEX_KV_OPTIONAL: {
382 docstring data = makeKeyValArgument();
393 docstring const InsetCommandParams::getCommand() const
395 docstring s = '\\' + from_ascii(cmdName_);
397 ParamInfo::const_iterator it = info_.begin();
398 ParamInfo::const_iterator end = info_.end();
399 for (; it != end; ++it) {
400 std::string const & name = it->name();
401 switch (it->type()) {
402 case ParamInfo::LATEX_KEY:
403 case ParamInfo::LYX_INTERNAL:
406 case ParamInfo::LATEX_REQUIRED: {
407 docstring const & data = (*this)[name];
408 s += '{' + data + '}';
412 case ParamInfo::LATEX_KV_REQUIRED: {
413 s += "{" + makeKeyValArgument() + "}";
417 case ParamInfo::LATEX_OPTIONAL: {
418 docstring const & data = (*this)[name];
420 s += '[' + data + ']';
422 } else if (writeEmptyOptional(it)) {
428 case ParamInfo::LATEX_KV_OPTIONAL: {
429 docstring data = makeKeyValArgument();
431 s += '[' + data + ']';
433 } else if (writeEmptyOptional(it)) {
442 // Make sure that following stuff does not change the
449 docstring const InsetCommandParams::getFirstNonOptParam() const
451 ParamInfo::const_iterator it =
452 find_if(info_.begin(), info_.end(),
453 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
454 if (it == info_.end())
456 return (*this)[it->name()];
460 docstring const & InsetCommandParams::operator[](string const & name) const
462 static const docstring dummy; //so we don't return a ref to temporary
463 BOOST_ASSERT(info_.hasParam(name));
464 ParamMap::const_iterator data = params_.find(name);
465 if (data == params_.end() || data->second.empty())
471 docstring & InsetCommandParams::operator[](string const & name)
473 BOOST_ASSERT(info_.hasParam(name));
474 ParamInfo::ParamData const & pd = info_[name];
475 return params_[name];
479 void InsetCommandParams::clear()
485 bool operator==(InsetCommandParams const & o1,
486 InsetCommandParams const & o2)
488 return o1.insetCode_ == o2.insetCode_ &&
489 o1.cmdName_ == o2.cmdName_ &&
490 o1.info_ == o2.info_ &&
491 o1.params_ == o2.params_ &&
492 o1.preview_ == o2.preview_;
496 bool operator!=(InsetCommandParams const & o1,
497 InsetCommandParams const & o2)