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.
15 #include "InsetCommandParams.h"
17 #include "InsetBibitem.h"
18 #include "InsetBibtex.h"
19 #include "InsetCitation.h"
20 #include "InsetFloatList.h"
21 #include "InsetHFill.h"
22 #include "InsetHyperlink.h"
23 #include "InsetInclude.h"
24 #include "InsetIndex.h"
25 #include "InsetLabel.h"
26 #include "InsetNomencl.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/lstrings.h"
37 #include <boost/assert.hpp>
42 using support::findToken;
48 using support::ExceptionMessage;
49 using support::WarningException;
51 InsetCommandParams::InsetCommandParams(InsetCode code)
52 : insetCode_(code), preview_(false)
54 cmdName_ = getDefaultCmd(code);
55 info_ = findInfo(code, cmdName_);
57 params_.resize(info_->n);
61 InsetCommandParams::InsetCommandParams(InsetCode code,
62 string const & cmdName)
63 : insetCode_(code), cmdName_(cmdName), preview_(false)
65 info_ = findInfo(code, cmdName);
67 params_.resize(info_->n);
71 CommandInfo const * InsetCommandParams::findInfo(
72 InsetCode code, std::string const & cmdName)
76 return InsetBibitem::findInfo(cmdName);
78 return InsetBibtex::findInfo(cmdName);
80 return InsetCitation::findInfo(cmdName);
82 return InsetFloatList::findInfo(cmdName);
84 return InsetHFill::findInfo(cmdName);
86 return InsetHyperlink::findInfo(cmdName);
88 return InsetInclude::findInfo(cmdName);
89 case INDEX_PRINT_CODE:
90 return InsetPrintIndex::findInfo(cmdName);
92 return InsetLabel::findInfo(cmdName);
94 return InsetNomencl::findInfo(cmdName);
95 case NOMENCL_PRINT_CODE:
96 return InsetPrintNomencl::findInfo(cmdName);
98 return InsetRef::findInfo(cmdName);
100 return InsetTOC::findInfo(cmdName);
108 std::string InsetCommandParams::getDefaultCmd(InsetCode code) {
111 return InsetBibitem::defaultCommand();
113 return InsetBibtex::defaultCommand();
115 return InsetCitation::defaultCommand();
116 case FLOAT_LIST_CODE:
117 return InsetFloatList::defaultCommand();
119 return InsetHFill::defaultCommand();
121 return InsetHyperlink::defaultCommand();
123 return InsetInclude::defaultCommand();
124 case INDEX_PRINT_CODE:
125 return InsetPrintIndex::defaultCommand();
127 return InsetLabel::defaultCommand();
129 return InsetNomencl::defaultCommand();
130 case NOMENCL_PRINT_CODE:
131 return InsetPrintNomencl::defaultCommand();
133 return InsetRef::defaultCommand();
135 return InsetTOC::defaultCommand();
139 return string(); //silence the warning
143 bool InsetCommandParams::isCompatibleCommand(
144 InsetCode code, std::string const & s)
148 return InsetBibitem::isCompatibleCommand(s);
150 return InsetBibtex::isCompatibleCommand(s);
152 return InsetCitation::isCompatibleCommand(s);
153 case FLOAT_LIST_CODE:
154 return InsetFloatList::isCompatibleCommand(s);
156 return InsetHFill::isCompatibleCommand(s);
158 return InsetHyperlink::isCompatibleCommand(s);
160 return InsetInclude::isCompatibleCommand(s);
161 case INDEX_PRINT_CODE:
162 return InsetPrintIndex::isCompatibleCommand(s);
164 return InsetLabel::isCompatibleCommand(s);
166 return InsetNomencl::isCompatibleCommand(s);
167 case NOMENCL_PRINT_CODE:
168 return InsetPrintNomencl::isCompatibleCommand(s);
170 return InsetRef::isCompatibleCommand(s);
172 return InsetTOC::isCompatibleCommand(s);
176 return false; //silence the warning
180 void InsetCommandParams::setCmdName(string const & name)
182 if (!isCompatibleCommand(insetCode_, cmdName_)){
183 lyxerr << "InsetCommand: Incompatible command name " <<
184 name << "." << std::endl;
185 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
186 from_utf8("Incompatible command name."));
190 CommandInfo const * const info = findInfo(insetCode_, cmdName_);
192 lyxerr << "Command '" << name << "' is not compatible with a '" <<
193 insetType() << "' inset." << std::endl;
196 ParamVector params(info->n);
197 // Overtake parameters with the same name
198 for (size_t i = 0; i < info_->n; ++i) {
199 int j = findToken(info->paramnames, info_->paramnames[i]);
201 params[j] = params_[i];
204 std::swap(params, params_);
208 void InsetCommandParams::read(Lexer & lex)
212 string const insetType = lex.getString();
213 InsetCode const code = insetCode(insetType);
214 if (code != insetCode_) {
215 lex.printError("InsetCommand: Attempt to change type of parameters.");
216 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
217 from_utf8("Attempt to change type of parameters."));
223 string const test = lex.getString();
224 if (test != "LatexCommand") {
225 lex.printError("InsetCommand: no LatexCommand line found.");
226 throw ExceptionMessage(WarningException, _("InsetCommand error:"),
227 from_utf8("Can't find LatexCommand line."));
231 cmdName_ = lex.getString();
232 if (!isCompatibleCommand(insetCode_, cmdName_)){
233 lex.printError("InsetCommand: Incompatible command name " + cmdName_ + ".");
234 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
235 from_utf8("Incompatible command name."));
238 info_ = findInfo(insetCode_, cmdName_);
240 lex.printError("InsetCommand: Unknown inset name `$$Token'");
241 throw ExceptionMessage(WarningException,
242 _("Unknown inset name: "), from_utf8(insetType()));
248 token = lex.getString();
249 if (token == "\\end_inset")
251 if (token == "preview") {
253 preview_ = lex.getBool();
256 int const i = findToken(info_->paramnames, token);
259 params_[i] = lex.getDocString();
261 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
262 throw ExceptionMessage(WarningException,
263 _("Inset Command: ") + from_ascii(cmdName_),
264 _("Unknown parameter name: ") + from_utf8(token));
267 if (token != "\\end_inset") {
268 lex.printError("Missing \\end_inset at this point. "
270 throw ExceptionMessage(WarningException,
271 _("Missing \\end_inset at this point."),
277 void InsetCommandParams::write(ostream & os) const
279 os << "CommandInset " << insetType() << '\n';
280 os << "LatexCommand " << cmdName_ << '\n';
282 os << "preview true\n";
283 for (size_t i = 0; i < info_->n; ++i)
284 if (!params_[i].empty())
286 os << info_->paramnames[i] << ' '
287 << Lexer::quoteString(to_utf8(params_[i]))
292 docstring const InsetCommandParams::getCommand() const
294 docstring s = '\\' + from_ascii(cmdName_);
296 for (size_t i = 0; i < info_->n; ++i) {
297 if (info_->optional[i]) {
298 if (params_[i].empty()) {
299 // We need to write this parameter even if
300 // it is empty if nonempty optional parameters
301 // follow before the next required parameter.
302 for (size_t j = i + 1; j < info_->n; ++j) {
303 if (!info_->optional[j])
305 if (!params_[j].empty()) {
312 s += '[' + params_[i] + ']';
316 s += '{' + params_[i] + '}';
321 // Make sure that following stuff does not change the
328 docstring const InsetCommandParams::getFirstNonOptParam() const
330 for (size_t i = 0; i < info_->n; ++i)
331 if (!info_->optional[i])
338 docstring const & InsetCommandParams::operator[](string const & name) const
340 int const i = findToken(info_->paramnames, name);
341 BOOST_ASSERT(i >= 0);
346 docstring & InsetCommandParams::operator[](string const & name)
348 int const i = findToken(info_->paramnames, name);
349 BOOST_ASSERT(i >= 0);
354 void InsetCommandParams::clear()
356 for (size_t i = 0; i < info_->n; ++i)
361 bool operator==(InsetCommandParams const & o1,
362 InsetCommandParams const & o2)
364 return o1.insetCode_ == o2.insetCode_ &&
365 o1.cmdName_ == o2.cmdName_ &&
366 o1.info_ == o2.info_ &&
367 o1.params_ == o2.params_ &&
368 o1.preview_ == o2.preview_;
372 bool operator!=(InsetCommandParams const & o1,
373 InsetCommandParams const & o2)