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 /// Returns 0 if the combination is not known. [FIXME: 0?]
53 /// Don't call this without first making sure the command name is
54 /// acceptable to the inset.
55 static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
59 return InsetBibitem::findInfo(cmdName);
61 return InsetBibtex::findInfo(cmdName);
63 return InsetCitation::findInfo(cmdName);
65 return InsetFloatList::findInfo(cmdName);
67 return InsetHyperlink::findInfo(cmdName);
69 return InsetInclude::findInfo(cmdName);
70 case INDEX_PRINT_CODE:
71 return InsetPrintIndex::findInfo(cmdName);
73 return InsetLabel::findInfo(cmdName);
75 return InsetLine::findInfo(cmdName);
77 return InsetNomencl::findInfo(cmdName);
78 case NOMENCL_PRINT_CODE:
79 return InsetPrintNomencl::findInfo(cmdName);
81 return InsetRef::findInfo(cmdName);
83 return InsetTOC::findInfo(cmdName);
87 static const ParamInfo pi;
88 return pi; // to silence the warning
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 LASSERT(hasParam(name), /**/);
148 const_iterator it = begin();
149 const_iterator last = end();
150 for (; it != last; ++it) {
151 if (it->name() == name)
154 return *it; // silence warning
158 /////////////////////////////////////////////////////////////////////
160 // InsetCommandParams
162 /////////////////////////////////////////////////////////////////////
165 InsetCommandParams::InsetCommandParams(InsetCode code)
166 : insetCode_(code), preview_(false)
168 cmdName_ = getDefaultCmd(code);
169 info_ = findInfo(code, cmdName_);
173 InsetCommandParams::InsetCommandParams(InsetCode code,
174 string const & cmdName)
175 : insetCode_(code), cmdName_(cmdName), preview_(false)
177 info_ = findInfo(code, cmdName);
181 std::string InsetCommandParams::insetType() const
183 return insetName(insetCode_);
187 string InsetCommandParams::getDefaultCmd(InsetCode code)
191 return InsetBibitem::defaultCommand();
193 return InsetBibtex::defaultCommand();
195 return InsetCitation::defaultCommand();
196 case FLOAT_LIST_CODE:
197 return InsetFloatList::defaultCommand();
199 return InsetHyperlink::defaultCommand();
201 return InsetInclude::defaultCommand();
202 case INDEX_PRINT_CODE:
203 return InsetPrintIndex::defaultCommand();
205 return InsetLabel::defaultCommand();
207 return InsetLine::defaultCommand();
209 return InsetNomencl::defaultCommand();
210 case NOMENCL_PRINT_CODE:
211 return InsetPrintNomencl::defaultCommand();
213 return InsetRef::defaultCommand();
215 return InsetTOC::defaultCommand();
217 LASSERT(false, /**/);
219 return string(); // silence the warning
223 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
227 return InsetBibitem::isCompatibleCommand(s);
229 return InsetBibtex::isCompatibleCommand(s);
231 return InsetCitation::isCompatibleCommand(s);
232 case FLOAT_LIST_CODE:
233 return InsetFloatList::isCompatibleCommand(s);
235 return InsetHyperlink::isCompatibleCommand(s);
237 return InsetInclude::isCompatibleCommand(s);
238 case INDEX_PRINT_CODE:
239 return InsetPrintIndex::isCompatibleCommand(s);
241 return InsetLabel::isCompatibleCommand(s);
243 return InsetLine::isCompatibleCommand(s);
245 return InsetNomencl::isCompatibleCommand(s);
246 case NOMENCL_PRINT_CODE:
247 return InsetPrintNomencl::isCompatibleCommand(s);
249 return InsetRef::isCompatibleCommand(s);
251 return InsetTOC::isCompatibleCommand(s);
253 LASSERT(false, /**/);
255 return false; // silence the warning
259 void InsetCommandParams::setCmdName(string const & name)
261 if (!isCompatibleCommand(insetCode_, name)) {
262 LYXERR0("InsetCommand: Incompatible command name " <<
264 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
265 _("Incompatible command name."));
269 info_ = findInfo(insetCode_, cmdName_);
273 void InsetCommandParams::read(Lexer & lex)
275 lex.setContext("InsetCommandParams::read");
276 lex >> insetName(insetCode_).c_str();
277 lex >> "LatexCommand";
279 if (!isCompatibleCommand(insetCode_, cmdName_)) {
280 lex.printError("Incompatible command name " + cmdName_ + ".");
281 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
282 _("Incompatible command name."));
285 info_ = findInfo(insetCode_, cmdName_);
290 token = lex.getString();
291 if (token == "\\end_inset")
293 if (token == "preview") {
295 preview_ = lex.getBool();
298 if (info_.hasParam(token)) {
300 params_[token] = lex.getDocString();
302 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
303 throw ExceptionMessage(WarningException,
304 _("InsetCommandParams: ") + from_ascii(cmdName_),
305 _("Unknown parameter name: ") + from_utf8(token));
308 if (token != "\\end_inset") {
309 lex.printError("Missing \\end_inset at this point. "
311 throw ExceptionMessage(WarningException,
312 _("InsetCommandParams Error: "),
313 _("Missing \\end_inset at this point: ") + from_utf8(token));
318 void InsetCommandParams::write(ostream & os) const
320 os << "CommandInset " << insetType() << '\n';
321 os << "LatexCommand " << cmdName_ << '\n';
323 os << "preview true\n";
324 ParamInfo::const_iterator it = info_.begin();
325 ParamInfo::const_iterator end = info_.end();
326 for (; it != end; ++it) {
327 std::string const & name = it->name();
328 docstring const & data = (*this)[name];
332 << Lexer::quoteString(to_utf8(data))
339 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
341 if (!ci->isOptional()) {
342 LASSERT(false, /**/);
344 ++ci; // we want to start with the next one
345 ParamInfo::const_iterator end = info_.end();
346 for (; ci != end; ++ci) {
347 switch (ci->type()) {
348 case ParamInfo::LYX_INTERNAL:
351 case ParamInfo::LATEX_REQUIRED:
354 case ParamInfo::LATEX_OPTIONAL: {
355 std::string const & name = ci->name();
356 docstring const & data = (*this)[name];
368 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
369 docstring const & command,
370 ParamInfo::ParamHandling handling) const
374 case ParamInfo::HANDLING_LATEXIFY: {
376 for (size_t n = 0; n < command.size(); ++n) {
378 char_type const c = command[n];
379 docstring const latex = runparams.encoding->latexChar(c);
381 if (latex.length() > 1 && latex[latex.length() - 1] != '}') {
382 // Prevent eating of a following
383 // space or command corruption by
384 // following characters
387 } catch (EncodingException & /* e */) {
388 LYXERR0("Uncodable character in command inset!");
389 if (runparams.dryrun) {
390 result += "<" + _("LyX Warning: ")
391 + _("uncodable character") + " '";
392 result += docstring(1, command[n]);
395 uncodable += command[n];
398 if (!uncodable.empty()) {
399 // issue a warning about omitted characters
400 // FIXME: should be passed to the error dialog
401 frontend::Alert::warning(_("Uncodable characters"),
402 bformat(_("The following characters that are used in the inset %1$s are not\n"
403 "representable in the current encoding and therefore have been omitted:\n%2$s."),
404 from_utf8(insetType()), uncodable));
408 case ParamInfo::HANDLING_ESCAPE:
409 result = escape(command);
411 case ParamInfo::HANDLING_NONE:
420 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
422 docstring s = '\\' + from_ascii(cmdName_);
424 ParamInfo::const_iterator it = info_.begin();
425 ParamInfo::const_iterator end = info_.end();
426 for (; it != end; ++it) {
427 std::string const & name = it->name();
428 switch (it->type()) {
429 case ParamInfo::LYX_INTERNAL:
432 case ParamInfo::LATEX_REQUIRED: {
433 docstring const data =
434 prepareCommand(runparams, (*this)[name], it->handling());
435 s += '{' + data + '}';
439 case ParamInfo::LATEX_OPTIONAL: {
440 docstring const data =
441 prepareCommand(runparams, (*this)[name], it->handling());
443 s += '[' + data + ']';
445 } else if (writeEmptyOptional(it)) {
454 // Make sure that following stuff does not change the
461 docstring InsetCommandParams::getFirstNonOptParam() const
463 ParamInfo::const_iterator it =
464 find_if(info_.begin(), info_.end(),
465 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
466 if (it == info_.end()) {
467 LASSERT(false, return docstring());
469 return (*this)[it->name()];
473 docstring const & InsetCommandParams::operator[](string const & name) const
475 static const docstring dummy; //so we don't return a ref to temporary
476 LASSERT(info_.hasParam(name), return dummy);
477 ParamMap::const_iterator data = params_.find(name);
478 if (data == params_.end() || data->second.empty())
484 docstring & InsetCommandParams::operator[](string const & name)
486 LASSERT(info_.hasParam(name), /**/);
487 return params_[name];
491 void InsetCommandParams::clear()
497 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
499 return o1.insetCode_ == o2.insetCode_
500 && o1.cmdName_ == o2.cmdName_
501 && o1.info_ == o2.info_
502 && o1.params_ == o2.params_
503 && o1.preview_ == o2.preview_;
507 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)