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"
34 #include "OutputParams.h"
36 #include "frontends/alert.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
44 #include "support/lassert.h"
47 using namespace lyx::support;
52 /// Get information for \p code and command \p cmdName.
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);
86 // fall through in release mode
88 static const ParamInfo pi;
93 /////////////////////////////////////////////////////////////////////
95 // ParamInfo::ParamData
97 /////////////////////////////////////////////////////////////////////
99 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
101 : name_(s), type_(t), handling_(h)
105 bool ParamInfo::ParamData::isOptional() const
107 return type_ == ParamInfo::LATEX_OPTIONAL;
111 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
113 return name() == rhs.name() && type() == rhs.type()
114 && handling() == rhs.handling();
118 bool ParamInfo::hasParam(std::string const & name) const
120 const_iterator it = begin();
121 const_iterator last = end();
122 for (; it != last; ++it) {
123 if (it->name() == name)
130 void ParamInfo::add(std::string const & name, ParamType type,
131 ParamHandling handling)
133 info_.push_back(ParamData(name, type, handling));
137 bool ParamInfo::operator==(ParamInfo const & rhs) const
139 if (size() != rhs.size())
141 return equal(begin(), end(), rhs.begin());
145 ParamInfo::ParamData const &
146 ParamInfo::operator[](std::string const & name) const
148 const_iterator it = begin();
149 const_iterator last = end();
150 for (; it != last; ++it) {
151 if (it->name() == name)
155 // we will try to continue in release mode
156 static const ParamData pd("asdfghjkl", LYX_INTERNAL);
161 /////////////////////////////////////////////////////////////////////
163 // InsetCommandParams
165 /////////////////////////////////////////////////////////////////////
168 InsetCommandParams::InsetCommandParams(InsetCode code)
169 : insetCode_(code), preview_(false)
171 cmdName_ = getDefaultCmd(code);
172 info_ = findInfo(code, cmdName_);
176 InsetCommandParams::InsetCommandParams(InsetCode code,
177 string const & cmdName)
178 : insetCode_(code), cmdName_(cmdName), preview_(false)
180 info_ = findInfo(code, cmdName);
184 std::string InsetCommandParams::insetType() const
186 return insetName(insetCode_);
190 string InsetCommandParams::getDefaultCmd(InsetCode code)
194 return InsetBibitem::defaultCommand();
196 return InsetBibtex::defaultCommand();
198 return InsetCitation::defaultCommand();
199 case FLOAT_LIST_CODE:
200 return InsetFloatList::defaultCommand();
202 return InsetHyperlink::defaultCommand();
204 return InsetInclude::defaultCommand();
205 case INDEX_PRINT_CODE:
206 return InsetPrintIndex::defaultCommand();
208 return InsetLabel::defaultCommand();
210 return InsetLine::defaultCommand();
212 return InsetNomencl::defaultCommand();
213 case NOMENCL_PRINT_CODE:
214 return InsetPrintNomencl::defaultCommand();
216 return InsetRef::defaultCommand();
218 return InsetTOC::defaultCommand();
221 // fall through in release mode
227 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
231 return InsetBibitem::isCompatibleCommand(s);
233 return InsetBibtex::isCompatibleCommand(s);
235 return InsetCitation::isCompatibleCommand(s);
236 case FLOAT_LIST_CODE:
237 return InsetFloatList::isCompatibleCommand(s);
239 return InsetHyperlink::isCompatibleCommand(s);
241 return InsetInclude::isCompatibleCommand(s);
242 case INDEX_PRINT_CODE:
243 return InsetPrintIndex::isCompatibleCommand(s);
245 return InsetLabel::isCompatibleCommand(s);
247 return InsetLine::isCompatibleCommand(s);
249 return InsetNomencl::isCompatibleCommand(s);
250 case NOMENCL_PRINT_CODE:
251 return InsetPrintNomencl::isCompatibleCommand(s);
253 return InsetRef::isCompatibleCommand(s);
255 return InsetTOC::isCompatibleCommand(s);
258 // fall through in release mode
264 void InsetCommandParams::setCmdName(string const & name)
266 if (!isCompatibleCommand(insetCode_, name)) {
267 LYXERR0("InsetCommand: Incompatible command name " <<
269 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
270 _("Incompatible command name."));
274 info_ = findInfo(insetCode_, cmdName_);
278 void InsetCommandParams::read(Lexer & lex)
280 lex.setContext("InsetCommandParams::read");
281 lex >> insetName(insetCode_).c_str();
282 lex >> "LatexCommand";
284 if (!isCompatibleCommand(insetCode_, cmdName_)) {
285 lex.printError("Incompatible command name " + cmdName_ + ".");
286 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
287 _("Incompatible command name."));
290 info_ = findInfo(insetCode_, cmdName_);
295 token = lex.getString();
296 if (token == "\\end_inset")
298 if (token == "preview") {
300 preview_ = lex.getBool();
303 if (info_.hasParam(token)) {
305 params_[token] = lex.getDocString();
307 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
308 throw ExceptionMessage(WarningException,
309 _("InsetCommandParams: ") + from_ascii(cmdName_),
310 _("Unknown parameter name: ") + from_utf8(token));
313 if (token != "\\end_inset") {
314 lex.printError("Missing \\end_inset at this point. "
316 throw ExceptionMessage(WarningException,
317 _("InsetCommandParams Error: "),
318 _("Missing \\end_inset at this point: ") + from_utf8(token));
323 void InsetCommandParams::write(ostream & os) const
329 void InsetCommandParams::Write(ostream & os, Buffer const * buffer) const
331 os << "CommandInset " << insetType() << '\n';
332 os << "LatexCommand " << cmdName_ << '\n';
334 os << "preview true\n";
335 ParamInfo::const_iterator it = info_.begin();
336 ParamInfo::const_iterator end = info_.end();
337 for (; it != end; ++it) {
338 string const & name = it->name();
339 string data = to_utf8((*this)[name]);
341 // Adjust path of files if document was moved
342 if (buffer && name == "filename") {
343 data = buffer->includedFilePath(data);
344 } else if (buffer && name == "bibfiles") {
347 string bib = token(data, ',', i);
348 while (!bib.empty()) {
349 bib = buffer->includedFilePath(bib, "bib");
350 if (!newdata.empty())
351 newdata.append(1, ',');
353 bib = token(data, ',', ++i);
358 << Lexer::quoteString(data)
365 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
367 LASSERT(ci->isOptional(), return false);
369 ++ci; // we want to start with the next one
370 ParamInfo::const_iterator end = info_.end();
371 for (; ci != end; ++ci) {
372 switch (ci->type()) {
373 case ParamInfo::LYX_INTERNAL:
376 case ParamInfo::LATEX_REQUIRED:
379 case ParamInfo::LATEX_OPTIONAL: {
380 std::string const & name = ci->name();
381 docstring const & data = (*this)[name];
393 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
394 docstring const & command,
395 ParamInfo::ParamHandling handling) const
399 case ParamInfo::HANDLING_LATEXIFY: {
400 pair<docstring, docstring> command_latexed =
401 runparams.encoding->latexString(command, runparams.dryrun);
402 result = command_latexed.first;
403 if (!command_latexed.second.empty()) {
404 // issue a warning about omitted characters
405 // FIXME: should be passed to the error dialog
406 frontend::Alert::warning(_("Uncodable characters"),
407 bformat(_("The following characters that are used in the inset %1$s are not\n"
408 "representable in the current encoding and therefore have been omitted:\n%2$s."),
409 from_utf8(insetType()), command_latexed.second));
413 case ParamInfo::HANDLING_ESCAPE:
414 result = escape(command);
416 case ParamInfo::HANDLING_NONE:
425 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
427 docstring s = '\\' + from_ascii(cmdName_);
429 ParamInfo::const_iterator it = info_.begin();
430 ParamInfo::const_iterator end = info_.end();
431 for (; it != end; ++it) {
432 std::string const & name = it->name();
433 switch (it->type()) {
434 case ParamInfo::LYX_INTERNAL:
437 case ParamInfo::LATEX_REQUIRED: {
438 docstring const data =
439 prepareCommand(runparams, (*this)[name], it->handling());
440 s += '{' + data + '}';
444 case ParamInfo::LATEX_OPTIONAL: {
445 docstring const data =
446 prepareCommand(runparams, (*this)[name], it->handling());
448 s += '[' + data + ']';
450 } else if (writeEmptyOptional(it)) {
459 // Make sure that following stuff does not change the
466 docstring InsetCommandParams::getFirstNonOptParam() const
468 ParamInfo::const_iterator it =
469 find_if(info_.begin(), info_.end(),
470 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
471 LASSERT(it != info_.end(), return docstring());
472 return (*this)[it->name()];
476 docstring const & InsetCommandParams::operator[](string const & name) const
478 static const docstring dummy;
479 LASSERT(info_.hasParam(name), return dummy);
480 ParamMap::const_iterator data = params_.find(name);
481 if (data == params_.end() || data->second.empty())
487 docstring & InsetCommandParams::operator[](string const & name)
489 LATTEST(info_.hasParam(name));
490 // this will add the name in release mode
491 return params_[name];
495 void InsetCommandParams::clear()
501 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
503 return o1.insetCode_ == o2.insetCode_
504 && o1.cmdName_ == o2.cmdName_
505 && o1.info_ == o2.info_
506 && o1.params_ == o2.params_
507 && o1.preview_ == o2.preview_;
511 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)