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 "InsetLine.h"
27 #include "InsetNomencl.h"
33 #include "OutputParams.h"
35 #include "frontends/alert.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
43 #include "support/lassert.h"
46 using namespace lyx::support;
51 /// Get information for \p code and command \p cmdName.
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 InsetLine::findInfo(cmdName);
76 return InsetNomencl::findInfo(cmdName);
77 case NOMENCL_PRINT_CODE:
78 return InsetPrintNomencl::findInfo(cmdName);
80 return InsetRef::findInfo(cmdName);
82 return InsetTOC::findInfo(cmdName);
85 // fall through in release mode
87 static const ParamInfo pi;
92 /////////////////////////////////////////////////////////////////////
94 // ParamInfo::ParamData
96 /////////////////////////////////////////////////////////////////////
98 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
100 : name_(s), type_(t), handling_(h)
104 bool ParamInfo::ParamData::isOptional() const
106 return type_ == ParamInfo::LATEX_OPTIONAL;
110 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
112 return name() == rhs.name() && type() == rhs.type()
113 && handling() == rhs.handling();
117 bool ParamInfo::hasParam(std::string const & name) const
119 const_iterator it = begin();
120 const_iterator last = end();
121 for (; it != last; ++it) {
122 if (it->name() == name)
129 void ParamInfo::add(std::string const & name, ParamType type,
130 ParamHandling handling)
132 info_.push_back(ParamData(name, type, handling));
136 bool ParamInfo::operator==(ParamInfo const & rhs) const
138 if (size() != rhs.size())
140 return equal(begin(), end(), rhs.begin());
144 ParamInfo::ParamData const &
145 ParamInfo::operator[](std::string const & name) const
147 const_iterator it = begin();
148 const_iterator last = end();
149 for (; it != last; ++it) {
150 if (it->name() == name)
154 // we will try to continue in release mode
155 static const ParamData pd("asdfghjkl", LYX_INTERNAL);
160 /////////////////////////////////////////////////////////////////////
162 // InsetCommandParams
164 /////////////////////////////////////////////////////////////////////
167 InsetCommandParams::InsetCommandParams(InsetCode code)
168 : insetCode_(code), preview_(false)
170 cmdName_ = getDefaultCmd(code);
171 info_ = findInfo(code, cmdName_);
175 InsetCommandParams::InsetCommandParams(InsetCode code,
176 string const & cmdName)
177 : insetCode_(code), cmdName_(cmdName), preview_(false)
179 info_ = findInfo(code, cmdName);
183 std::string InsetCommandParams::insetType() const
185 return insetName(insetCode_);
189 string InsetCommandParams::getDefaultCmd(InsetCode code)
193 return InsetBibitem::defaultCommand();
195 return InsetBibtex::defaultCommand();
197 return InsetCitation::defaultCommand();
198 case FLOAT_LIST_CODE:
199 return InsetFloatList::defaultCommand();
201 return InsetHyperlink::defaultCommand();
203 return InsetInclude::defaultCommand();
204 case INDEX_PRINT_CODE:
205 return InsetPrintIndex::defaultCommand();
207 return InsetLabel::defaultCommand();
209 return InsetLine::defaultCommand();
211 return InsetNomencl::defaultCommand();
212 case NOMENCL_PRINT_CODE:
213 return InsetPrintNomencl::defaultCommand();
215 return InsetRef::defaultCommand();
217 return InsetTOC::defaultCommand();
220 // fall through in release mode
226 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
230 return InsetBibitem::isCompatibleCommand(s);
232 return InsetBibtex::isCompatibleCommand(s);
234 return InsetCitation::isCompatibleCommand(s);
235 case FLOAT_LIST_CODE:
236 return InsetFloatList::isCompatibleCommand(s);
238 return InsetHyperlink::isCompatibleCommand(s);
240 return InsetInclude::isCompatibleCommand(s);
241 case INDEX_PRINT_CODE:
242 return InsetPrintIndex::isCompatibleCommand(s);
244 return InsetLabel::isCompatibleCommand(s);
246 return InsetLine::isCompatibleCommand(s);
248 return InsetNomencl::isCompatibleCommand(s);
249 case NOMENCL_PRINT_CODE:
250 return InsetPrintNomencl::isCompatibleCommand(s);
252 return InsetRef::isCompatibleCommand(s);
254 return InsetTOC::isCompatibleCommand(s);
257 // fall through in release mode
263 void InsetCommandParams::setCmdName(string const & name)
265 if (!isCompatibleCommand(insetCode_, name)) {
266 LYXERR0("InsetCommand: Incompatible command name " <<
268 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
269 _("Incompatible command name."));
273 info_ = findInfo(insetCode_, cmdName_);
277 void InsetCommandParams::read(Lexer & lex)
279 lex.setContext("InsetCommandParams::read");
280 lex >> insetName(insetCode_).c_str();
281 lex >> "LatexCommand";
283 if (!isCompatibleCommand(insetCode_, cmdName_)) {
284 lex.printError("Incompatible command name " + cmdName_ + ".");
285 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
286 _("Incompatible command name."));
289 info_ = findInfo(insetCode_, cmdName_);
294 token = lex.getString();
295 if (token == "\\end_inset")
297 if (token == "preview") {
299 preview_ = lex.getBool();
302 if (info_.hasParam(token)) {
304 params_[token] = lex.getDocString();
306 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
307 throw ExceptionMessage(WarningException,
308 _("InsetCommandParams: ") + from_ascii(cmdName_),
309 _("Unknown parameter name: ") + from_utf8(token));
312 if (token != "\\end_inset") {
313 lex.printError("Missing \\end_inset at this point. "
315 throw ExceptionMessage(WarningException,
316 _("InsetCommandParams Error: "),
317 _("Missing \\end_inset at this point: ") + from_utf8(token));
322 void InsetCommandParams::write(ostream & os) const
324 os << "CommandInset " << insetType() << '\n';
325 os << "LatexCommand " << cmdName_ << '\n';
327 os << "preview true\n";
328 ParamInfo::const_iterator it = info_.begin();
329 ParamInfo::const_iterator end = info_.end();
330 for (; it != end; ++it) {
331 std::string const & name = it->name();
332 docstring const & data = (*this)[name];
336 << Lexer::quoteString(to_utf8(data))
343 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
345 LASSERT(ci->isOptional(), return false);
347 ++ci; // we want to start with the next one
348 ParamInfo::const_iterator end = info_.end();
349 for (; ci != end; ++ci) {
350 switch (ci->type()) {
351 case ParamInfo::LYX_INTERNAL:
354 case ParamInfo::LATEX_REQUIRED:
357 case ParamInfo::LATEX_OPTIONAL: {
358 std::string const & name = ci->name();
359 docstring const & data = (*this)[name];
371 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
372 docstring const & command,
373 ParamInfo::ParamHandling handling) const
377 case ParamInfo::HANDLING_LATEXIFY: {
378 pair<docstring, docstring> command_latexed =
379 runparams.encoding->latexString(command, runparams.dryrun);
380 result = command_latexed.first;
381 if (!command_latexed.second.empty()) {
382 // issue a warning about omitted characters
383 // FIXME: should be passed to the error dialog
384 frontend::Alert::warning(_("Uncodable characters"),
385 bformat(_("The following characters that are used in the inset %1$s are not\n"
386 "representable in the current encoding and therefore have been omitted:\n%2$s."),
387 from_utf8(insetType()), command_latexed.second));
391 case ParamInfo::HANDLING_ESCAPE:
392 result = escape(command);
394 case ParamInfo::HANDLING_NONE:
403 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
405 docstring s = '\\' + from_ascii(cmdName_);
407 ParamInfo::const_iterator it = info_.begin();
408 ParamInfo::const_iterator end = info_.end();
409 for (; it != end; ++it) {
410 std::string const & name = it->name();
411 switch (it->type()) {
412 case ParamInfo::LYX_INTERNAL:
415 case ParamInfo::LATEX_REQUIRED: {
416 docstring const data =
417 prepareCommand(runparams, (*this)[name], it->handling());
418 s += '{' + data + '}';
422 case ParamInfo::LATEX_OPTIONAL: {
423 docstring const data =
424 prepareCommand(runparams, (*this)[name], it->handling());
426 s += '[' + data + ']';
428 } else if (writeEmptyOptional(it)) {
437 // Make sure that following stuff does not change the
444 docstring InsetCommandParams::getFirstNonOptParam() const
446 ParamInfo::const_iterator it =
447 find_if(info_.begin(), info_.end(),
448 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
449 LASSERT(it != info_.end(), return docstring());
450 return (*this)[it->name()];
454 docstring const & InsetCommandParams::operator[](string const & name) const
456 static const docstring dummy;
457 LASSERT(info_.hasParam(name), return dummy);
458 ParamMap::const_iterator data = params_.find(name);
459 if (data == params_.end() || data->second.empty())
465 docstring & InsetCommandParams::operator[](string const & name)
467 LATTEST(info_.hasParam(name));
468 // this will add the name in release mode
469 return params_[name];
473 void InsetCommandParams::clear()
479 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
481 return o1.insetCode_ == o2.insetCode_
482 && o1.cmdName_ == o2.cmdName_
483 && o1.info_ == o2.info_
484 && o1.params_ == o2.params_
485 && o1.preview_ == o2.preview_;
489 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)