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.
18 #include "InsetCommandParams.h"
20 #include "InsetBibitem.h"
21 #include "InsetBibtex.h"
22 #include "InsetCitation.h"
23 #include "InsetFloatList.h"
24 #include "InsetHyperlink.h"
25 #include "InsetInclude.h"
26 #include "InsetIndex.h"
27 #include "InsetLabel.h"
28 #include "InsetLine.h"
29 #include "InsetNomencl.h"
36 #include "OutputParams.h"
38 #include "frontends/alert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/ExceptionMessage.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
46 #include "support/lassert.h"
49 using namespace lyx::support;
54 /// Get information for \p code and command \p cmdName.
55 /// Don't call this without first making sure the command name is
56 /// acceptable to the inset.
57 static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
61 return InsetBibitem::findInfo(cmdName);
63 return InsetBibtex::findInfo(cmdName);
65 return InsetCitation::findInfo(cmdName);
67 return InsetFloatList::findInfo(cmdName);
69 return InsetHyperlink::findInfo(cmdName);
71 return InsetInclude::findInfo(cmdName);
72 case INDEX_PRINT_CODE:
73 return InsetPrintIndex::findInfo(cmdName);
75 return InsetLabel::findInfo(cmdName);
77 return InsetLine::findInfo(cmdName);
79 return InsetNomencl::findInfo(cmdName);
80 case NOMENCL_PRINT_CODE:
81 return InsetPrintNomencl::findInfo(cmdName);
83 return InsetRef::findInfo(cmdName);
85 return InsetTOC::findInfo(cmdName);
88 // fall through in release mode
90 static const ParamInfo pi;
95 /////////////////////////////////////////////////////////////////////
97 // ParamInfo::ParamData
99 /////////////////////////////////////////////////////////////////////
101 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
103 : name_(s), type_(t), handling_(h)
107 bool ParamInfo::ParamData::isOptional() const
109 return type_ == ParamInfo::LATEX_OPTIONAL;
113 bool ParamInfo::ParamData::operator==(ParamInfo::ParamData const & rhs) const
115 return name() == rhs.name() && type() == rhs.type()
116 && handling() == rhs.handling();
120 bool ParamInfo::hasParam(std::string const & name) const
122 const_iterator it = begin();
123 const_iterator last = end();
124 for (; it != last; ++it) {
125 if (it->name() == name)
132 void ParamInfo::add(std::string const & name, ParamType type,
133 ParamHandling handling)
135 info_.push_back(ParamData(name, type, handling));
139 bool ParamInfo::operator==(ParamInfo const & rhs) const
141 if (size() != rhs.size())
143 return equal(begin(), end(), rhs.begin());
147 ParamInfo::ParamData const &
148 ParamInfo::operator[](std::string const & name) const
150 const_iterator it = begin();
151 const_iterator last = end();
152 for (; it != last; ++it) {
153 if (it->name() == name)
157 // we will try to continue in release mode
158 static const ParamData pd("asdfghjkl", LYX_INTERNAL);
163 /////////////////////////////////////////////////////////////////////
165 // InsetCommandParams
167 /////////////////////////////////////////////////////////////////////
170 InsetCommandParams::InsetCommandParams(InsetCode code)
171 : insetCode_(code), preview_(false)
173 cmdName_ = getDefaultCmd(code);
174 info_ = findInfo(code, cmdName_);
178 InsetCommandParams::InsetCommandParams(InsetCode code,
179 string const & cmdName)
180 : insetCode_(code), cmdName_(cmdName), preview_(false)
182 info_ = findInfo(code, cmdName);
186 std::string InsetCommandParams::insetType() const
188 return insetName(insetCode_);
192 string InsetCommandParams::getDefaultCmd(InsetCode code)
196 return InsetBibitem::defaultCommand();
198 return InsetBibtex::defaultCommand();
200 return InsetCitation::defaultCommand();
201 case FLOAT_LIST_CODE:
202 return InsetFloatList::defaultCommand();
204 return InsetHyperlink::defaultCommand();
206 return InsetInclude::defaultCommand();
207 case INDEX_PRINT_CODE:
208 return InsetPrintIndex::defaultCommand();
210 return InsetLabel::defaultCommand();
212 return InsetLine::defaultCommand();
214 return InsetNomencl::defaultCommand();
215 case NOMENCL_PRINT_CODE:
216 return InsetPrintNomencl::defaultCommand();
218 return InsetRef::defaultCommand();
220 return InsetTOC::defaultCommand();
223 // fall through in release mode
229 bool InsetCommandParams::isCompatibleCommand(InsetCode code, string const & s)
233 return InsetBibitem::isCompatibleCommand(s);
235 return InsetBibtex::isCompatibleCommand(s);
237 return InsetCitation::isCompatibleCommand(s);
238 case FLOAT_LIST_CODE:
239 return InsetFloatList::isCompatibleCommand(s);
241 return InsetHyperlink::isCompatibleCommand(s);
243 return InsetInclude::isCompatibleCommand(s);
244 case INDEX_PRINT_CODE:
245 return InsetPrintIndex::isCompatibleCommand(s);
247 return InsetLabel::isCompatibleCommand(s);
249 return InsetLine::isCompatibleCommand(s);
251 return InsetNomencl::isCompatibleCommand(s);
252 case NOMENCL_PRINT_CODE:
253 return InsetPrintNomencl::isCompatibleCommand(s);
255 return InsetRef::isCompatibleCommand(s);
257 return InsetTOC::isCompatibleCommand(s);
260 // fall through in release mode
266 void InsetCommandParams::setCmdName(string const & name)
268 if (!isCompatibleCommand(insetCode_, name)) {
269 LYXERR0("InsetCommand: Incompatible command name " <<
271 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
272 _("Incompatible command name."));
276 info_ = findInfo(insetCode_, cmdName_);
280 void InsetCommandParams::read(Lexer & lex)
286 void InsetCommandParams::Read(Lexer & lex, Buffer const * buffer)
288 lex.setContext("InsetCommandParams::read");
289 lex >> insetName(insetCode_).c_str();
290 lex >> "LatexCommand";
292 if (!isCompatibleCommand(insetCode_, cmdName_)) {
293 lex.printError("Incompatible command name " + cmdName_ + ".");
294 throw ExceptionMessage(WarningException, _("InsetCommandParams Error: "),
295 _("Incompatible command name."));
298 info_ = findInfo(insetCode_, cmdName_);
303 token = lex.getString();
304 if (token == "\\end_inset")
306 if (token == "preview") {
308 preview_ = lex.getBool();
311 if (info_.hasParam(token)) {
313 docstring data = lex.getDocString();
314 if (buffer && token == "filename") {
315 data = from_utf8(buffer->includedFilePath(to_utf8(data)));
316 } else if (buffer && token == "bibfiles") {
319 docstring bib = support::token(data, ',', i);
320 while (!bib.empty()) {
321 bib = from_utf8(buffer->includedFilePath(to_utf8(bib), "bib"));
322 if (!newdata.empty())
323 newdata.append(1, ',');
325 bib = support::token(data, ',', ++i);
328 } else if (buffer && token == "options") {
329 data = from_utf8(buffer->includedFilePath(to_utf8(data), "bst"));
331 params_[token] = data;
333 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
334 throw ExceptionMessage(WarningException,
335 _("InsetCommandParams: ") + from_ascii(cmdName_),
336 _("Unknown parameter name: ") + from_utf8(token));
339 if (token != "\\end_inset") {
340 lex.printError("Missing \\end_inset at this point. "
342 throw ExceptionMessage(WarningException,
343 _("InsetCommandParams Error: "),
344 _("Missing \\end_inset at this point: ") + from_utf8(token));
349 void InsetCommandParams::write(ostream & os) const
355 void InsetCommandParams::Write(ostream & os, Buffer const * buffer) const
357 os << "CommandInset " << insetType() << '\n';
358 os << "LatexCommand " << cmdName_ << '\n';
360 os << "preview true\n";
361 ParamInfo::const_iterator it = info_.begin();
362 ParamInfo::const_iterator end = info_.end();
363 for (; it != end; ++it) {
364 string const & name = it->name();
365 string data = to_utf8((*this)[name]);
367 // Adjust path of files if document was moved
368 if (buffer && name == "filename") {
369 data = buffer->includedFilePath(data);
370 } else if (buffer && name == "bibfiles") {
373 string bib = token(data, ',', i);
374 while (!bib.empty()) {
375 bib = buffer->includedFilePath(bib, "bib");
376 if (!newdata.empty())
377 newdata.append(1, ',');
379 bib = token(data, ',', ++i);
382 } else if (buffer && name == "options") {
383 data = buffer->includedFilePath(data, "bst");
386 << Lexer::quoteString(data)
393 bool InsetCommandParams::writeEmptyOptional(ParamInfo::const_iterator ci) const
395 LASSERT(ci->isOptional(), return false);
397 ++ci; // we want to start with the next one
398 ParamInfo::const_iterator end = info_.end();
399 for (; ci != end; ++ci) {
400 switch (ci->type()) {
401 case ParamInfo::LYX_INTERNAL:
404 case ParamInfo::LATEX_REQUIRED:
407 case ParamInfo::LATEX_OPTIONAL: {
408 std::string const & name = ci->name();
409 docstring const & data = (*this)[name];
421 docstring InsetCommandParams::prepareCommand(OutputParams const & runparams,
422 docstring const & command,
423 ParamInfo::ParamHandling handling) const
425 if (handling == ParamInfo::HANDLING_LATEXIFY)
426 if ((*this)["literal"] == "true")
427 handling = ParamInfo::HANDLING_NONE;
430 case ParamInfo::HANDLING_LATEXIFY: {
431 // First handle backslash
432 result = subst(command, from_ascii("\\"), from_ascii("\\textbackslash{}"));
433 // Then get LaTeX macros
434 pair<docstring, docstring> command_latexed =
435 runparams.encoding->latexString(result, runparams.dryrun);
436 result = command_latexed.first;
437 if (!command_latexed.second.empty()) {
438 // Issue a warning about omitted characters
439 // FIXME: should be passed to the error dialog
440 frontend::Alert::warning(_("Uncodable characters"),
441 bformat(_("The following characters that are used in the inset %1$s are not\n"
442 "representable in the current encoding and therefore have been omitted:\n%2$s."),
443 from_utf8(insetType()), command_latexed.second));
445 // Now escape special commands
446 static docstring const backslash = from_ascii("\\");
447 static char_type const chars_escape[6] = {
448 '&', '_', '$', '%', '#', '^'};
450 if (!result.empty()) {
452 // The characters in chars_name[] need to be changed to a command when
453 // they are LaTeXified.
454 for (int k = 0; k < 6; k++)
455 for (size_t i = 0, pos;
456 (pos = result.find(chars_escape[k], i)) != string::npos;
458 //(Only) \\^ needs to be terminated
459 docstring const term = (k == 5) ? from_ascii("{}") : docstring();
464 // only if not already escaped
465 if (result[previous] != '\\')
466 result.replace(pos, 1, backslash + chars_escape[k] + term);
471 case ParamInfo::HANDLING_ESCAPE:
472 result = escape(command);
474 case ParamInfo::HANDLING_NONE:
483 docstring InsetCommandParams::getCommand(OutputParams const & runparams) const
485 docstring s = '\\' + from_ascii(cmdName_);
487 ParamInfo::const_iterator it = info_.begin();
488 ParamInfo::const_iterator end = info_.end();
489 for (; it != end; ++it) {
490 std::string const & name = it->name();
491 switch (it->type()) {
492 case ParamInfo::LYX_INTERNAL:
495 case ParamInfo::LATEX_REQUIRED: {
496 docstring const data =
497 prepareCommand(runparams, (*this)[name], it->handling());
498 s += '{' + data + '}';
502 case ParamInfo::LATEX_OPTIONAL: {
504 prepareCommand(runparams, (*this)[name], it->handling());
506 s += '[' + protectArgument(data) + ']';
508 } else if (writeEmptyOptional(it)) {
517 // Make sure that following stuff does not change the
524 docstring InsetCommandParams::getFirstNonOptParam() const
526 ParamInfo::const_iterator it =
527 find_if(info_.begin(), info_.end(),
528 not1(mem_fun_ref(&ParamInfo::ParamData::isOptional)));
529 LASSERT(it != info_.end(), return docstring());
530 return (*this)[it->name()];
534 docstring const & InsetCommandParams::operator[](string const & name) const
536 static const docstring dummy;
537 LASSERT(info_.hasParam(name), return dummy);
538 ParamMap::const_iterator data = params_.find(name);
539 if (data == params_.end() || data->second.empty())
545 docstring & InsetCommandParams::operator[](string const & name)
547 LATTEST(info_.hasParam(name));
548 // this will add the name in release mode
549 return params_[name];
553 void InsetCommandParams::clear()
559 bool operator==(InsetCommandParams const & o1, InsetCommandParams const & o2)
561 return o1.insetCode_ == o2.insetCode_
562 && o1.cmdName_ == o2.cmdName_
563 && o1.info_ == o2.info_
564 && o1.params_ == o2.params_
565 && o1.preview_ == o2.preview_;
569 bool operator!=(InsetCommandParams const & o1, InsetCommandParams const & o2)