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)
284 void InsetCommandParams::Read(Lexer & lex, Buffer const * buffer)
286 lex.setContext("InsetCommandParams::read");
287 lex >> insetName(insetCode_).c_str();
288 lex >> "LatexCommand";
290 if (!isCompatibleCommand(insetCode_, cmdName_)) {
291 lex.printError("Incompatible command name " + cmdName_ + ".");
292 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
293 _("Incompatible command name."));
296 info_ = findInfo(insetCode_, cmdName_);
301 token = lex.getString();
302 if (token == "\\end_inset")
304 if (token == "preview") {
306 preview_ = lex.getBool();
309 if (info_.hasParam(token)) {
311 docstring data = lex.getDocString();
312 if (buffer && token == "filename") {
313 data = from_utf8(buffer->includedFilePath(to_utf8(data)));
314 } else if (buffer && token == "bibfiles") {
317 docstring bib = support::token(data, ',', i);
318 while (!bib.empty()) {
319 bib = from_utf8(buffer->includedFilePath(to_utf8(bib), "bib"));
320 if (!newdata.empty())
321 newdata.append(1, ',');
323 bib = support::token(data, ',', ++i);
326 } else if (buffer && token == "options") {
327 data = from_utf8(buffer->includedFilePath(to_utf8(data), "bst"));
329 params_[token] = data;
331 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
332 throw ExceptionMessage(WarningException,
333 _("InsetCommandParams: ") + from_ascii(cmdName_),
334 _("Unknown parameter name: ") + from_utf8(token));
337 if (token != "\\end_inset") {
338 lex.printError("Missing \\end_inset at this point. "
340 throw ExceptionMessage(WarningException,
341 _("InsetCommandParams Error: "),
342 _("Missing \\end_inset at this point: ") + from_utf8(token));
347 void InsetCommandParams::write(ostream & os) const
353 void InsetCommandParams::Write(ostream & os, Buffer const * buffer) const
355 os << "CommandInset " << insetType() << '\n';
356 os << "LatexCommand " << cmdName_ << '\n';
358 os << "preview true\n";
359 ParamInfo::const_iterator it = info_.begin();
360 ParamInfo::const_iterator end = info_.end();
361 for (; it != end; ++it) {
362 string const & name = it->name();
363 string data = to_utf8((*this)[name]);
365 // Adjust path of files if document was moved
366 if (buffer && name == "filename") {
367 data = buffer->includedFilePath(data);
368 } else if (buffer && name == "bibfiles") {
371 string bib = token(data, ',', i);
372 while (!bib.empty()) {
373 bib = buffer->includedFilePath(bib, "bib");
374 if (!newdata.empty())
375 newdata.append(1, ',');
377 bib = token(data, ',', ++i);
380 } else if (buffer && name == "options") {
381 data = buffer->includedFilePath(data, "bst");
384 << Lexer::quoteString(data)
391 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
393 LASSERT(ci->isOptional(), return false);
395 ++ci; // we want to start with the next one
396 ParamInfo::const_iterator end = info_.end();
397 for (; ci != end; ++ci) {
398 switch (ci->type()) {
399 case ParamInfo::LYX_INTERNAL:
402 case ParamInfo::LATEX_REQUIRED:
405 case ParamInfo::LATEX_OPTIONAL: {
406 std::string const & name = ci->name();
407 docstring const & data = (*this)[name];
419 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
420 docstring const & command,
421 ParamInfo::ParamHandling handling) const
425 case ParamInfo::HANDLING_LATEXIFY: {
426 pair<docstring, docstring> command_latexed =
427 runparams.encoding->latexString(command, runparams.dryrun);
428 result = command_latexed.first;
429 if (!command_latexed.second.empty()) {
430 // issue a warning about omitted characters
431 // FIXME: should be passed to the error dialog
432 frontend::Alert::warning(_("Uncodable characters"),
433 bformat(_("The following characters that are used in the inset %1$s are not\n"
434 "representable in the current encoding and therefore have been omitted:\n%2$s."),
435 from_utf8(insetType()), command_latexed.second));
439 case ParamInfo::HANDLING_ESCAPE:
440 result = escape(command);
442 case ParamInfo::HANDLING_NONE:
451 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
453 docstring s = '\\' + from_ascii(cmdName_);
455 ParamInfo::const_iterator it = info_.begin();
456 ParamInfo::const_iterator end = info_.end();
457 for (; it != end; ++it) {
458 std::string const & name = it->name();
459 switch (it->type()) {
460 case ParamInfo::LYX_INTERNAL:
463 case ParamInfo::LATEX_REQUIRED: {
464 docstring const data =
465 prepareCommand(runparams, (*this)[name], it->handling());
466 s += '{' + data + '}';
470 case ParamInfo::LATEX_OPTIONAL: {
471 docstring const data =
472 prepareCommand(runparams, (*this)[name], it->handling());
474 s += '[' + data + ']';
476 } else if (writeEmptyOptional(it)) {
485 // Make sure that following stuff does not change the
492 docstring InsetCommandParams::getFirstNonOptParam() const
494 ParamInfo::const_iterator it =
495 find_if(info_.begin(), info_.end(),
496 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
497 LASSERT(it != info_.end(), return docstring());
498 return (*this)[it->name()];
502 docstring const & InsetCommandParams::operator[](string const & name) const
504 static const docstring dummy;
505 LASSERT(info_.hasParam(name), return dummy);
506 ParamMap::const_iterator data = params_.find(name);
507 if (data == params_.end() || data->second.empty())
513 docstring & InsetCommandParams::operator[](string const & name)
515 LATTEST(info_.hasParam(name));
516 // this will add the name in release mode
517 return params_[name];
521 void InsetCommandParams::clear()
527 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
529 return o1.insetCode_ == o2.insetCode_
530 && o1.cmdName_ == o2.cmdName_
531 && o1.info_ == o2.info_
532 && o1.params_ == o2.params_
533 && o1.preview_ == o2.preview_;
537 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)