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"
30 #include "support/debug.h"
31 #include "support/gettext.h"
34 #include "support/ExceptionMessage.h"
35 #include "support/lstrings.h"
36 #include "support/docstream.h"
38 #include <boost/assert.hpp>
45 using support::findToken;
46 using support::ExceptionMessage;
47 using support::WarningException;
49 InsetCommandParams::InsetCommandParams(InsetCode code)
50 : insetCode_(code), preview_(false)
52 cmdName_ = getDefaultCmd(code);
53 info_ = findInfo(code, cmdName_);
55 params_.resize(info_->n);
59 InsetCommandParams::InsetCommandParams(InsetCode code,
60 string const & cmdName)
61 : insetCode_(code), cmdName_(cmdName), preview_(false)
63 info_ = findInfo(code, cmdName);
65 params_.resize(info_->n);
69 CommandInfo const * InsetCommandParams::findInfo(
70 InsetCode code, std::string const & cmdName)
74 return InsetBibitem::findInfo(cmdName);
76 return InsetBibtex::findInfo(cmdName);
78 return InsetCitation::findInfo(cmdName);
80 return InsetFloatList::findInfo(cmdName);
82 return InsetHFill::findInfo(cmdName);
84 return InsetHyperlink::findInfo(cmdName);
86 return InsetInclude::findInfo(cmdName);
87 case INDEX_PRINT_CODE:
88 return InsetPrintIndex::findInfo(cmdName);
90 return InsetLabel::findInfo(cmdName);
92 return InsetNomencl::findInfo(cmdName);
93 case NOMENCL_PRINT_CODE:
94 return InsetPrintNomencl::findInfo(cmdName);
96 return InsetRef::findInfo(cmdName);
98 return InsetTOC::findInfo(cmdName);
106 std::string InsetCommandParams::getDefaultCmd(InsetCode code) {
109 return InsetBibitem::defaultCommand();
111 return InsetBibtex::defaultCommand();
113 return InsetCitation::defaultCommand();
114 case FLOAT_LIST_CODE:
115 return InsetFloatList::defaultCommand();
117 return InsetHFill::defaultCommand();
119 return InsetHyperlink::defaultCommand();
121 return InsetInclude::defaultCommand();
122 case INDEX_PRINT_CODE:
123 return InsetPrintIndex::defaultCommand();
125 return InsetLabel::defaultCommand();
127 return InsetNomencl::defaultCommand();
128 case NOMENCL_PRINT_CODE:
129 return InsetPrintNomencl::defaultCommand();
131 return InsetRef::defaultCommand();
133 return InsetTOC::defaultCommand();
137 return string(); //silence the warning
141 bool InsetCommandParams::isCompatibleCommand(
142 InsetCode code, std::string const & s)
146 return InsetBibitem::isCompatibleCommand(s);
148 return InsetBibtex::isCompatibleCommand(s);
150 return InsetCitation::isCompatibleCommand(s);
151 case FLOAT_LIST_CODE:
152 return InsetFloatList::isCompatibleCommand(s);
154 return InsetHFill::isCompatibleCommand(s);
156 return InsetHyperlink::isCompatibleCommand(s);
158 return InsetInclude::isCompatibleCommand(s);
159 case INDEX_PRINT_CODE:
160 return InsetPrintIndex::isCompatibleCommand(s);
162 return InsetLabel::isCompatibleCommand(s);
164 return InsetNomencl::isCompatibleCommand(s);
165 case NOMENCL_PRINT_CODE:
166 return InsetPrintNomencl::isCompatibleCommand(s);
168 return InsetRef::isCompatibleCommand(s);
170 return InsetTOC::isCompatibleCommand(s);
174 return false; //silence the warning
178 void InsetCommandParams::setCmdName(string const & name)
180 if (!isCompatibleCommand(insetCode_, cmdName_)){
181 LYXERR0("InsetCommand: Incompatible command name " <<
183 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
184 from_utf8("Incompatible command name."));
188 CommandInfo const * const info = findInfo(insetCode_, cmdName_);
190 LYXERR0("Command '" << name << "' is not compatible with a '" <<
191 insetType() << "' inset.");
194 ParamVector params(info->n);
195 // Overtake parameters with the same name
196 for (size_t i = 0; i < info_->n; ++i) {
197 int j = findToken(info->paramnames, info_->paramnames[i]);
199 params[j] = params_[i];
202 std::swap(params, params_);
206 void InsetCommandParams::read(Lexer & lex)
210 string const insetType = lex.getString();
211 InsetCode const code = insetCode(insetType);
212 if (code != insetCode_) {
213 lex.printError("InsetCommand: Attempt to change type of parameters.");
214 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
215 from_utf8("Attempt to change type of parameters."));
221 string const test = lex.getString();
222 if (test != "LatexCommand") {
223 lex.printError("InsetCommand: no LatexCommand line found.");
224 throw ExceptionMessage(WarningException, _("InsetCommand error:"),
225 from_utf8("Can't find LatexCommand line."));
229 cmdName_ = lex.getString();
230 if (!isCompatibleCommand(insetCode_, cmdName_)){
231 lex.printError("InsetCommand: Incompatible command name " + cmdName_ + ".");
232 throw ExceptionMessage(WarningException, _("InsetCommand Error: "),
233 from_utf8("Incompatible command name."));
236 info_ = findInfo(insetCode_, cmdName_);
238 lex.printError("InsetCommand: Unknown inset name `$$Token'");
239 throw ExceptionMessage(WarningException,
240 _("Unknown inset name: "), from_utf8(insetType()));
246 token = lex.getString();
247 if (token == "\\end_inset")
249 if (token == "preview") {
251 preview_ = lex.getBool();
254 int const i = findToken(info_->paramnames, token);
257 params_[i] = lex.getDocString();
259 lex.printError("Unknown parameter name `$$Token' for command " + cmdName_);
260 throw ExceptionMessage(WarningException,
261 _("Inset Command: ") + from_ascii(cmdName_),
262 _("Unknown parameter name: ") + from_utf8(token));
265 if (token != "\\end_inset") {
266 lex.printError("Missing \\end_inset at this point. "
268 throw ExceptionMessage(WarningException,
269 _("Missing \\end_inset at this point."),
275 void InsetCommandParams::write(std::ostream & os) const
277 os << "CommandInset " << insetType() << '\n';
278 os << "LatexCommand " << cmdName_ << '\n';
280 os << "preview true\n";
281 for (size_t i = 0; i < info_->n; ++i)
282 if (!params_[i].empty())
284 os << info_->paramnames[i] << ' '
285 << Lexer::quoteString(to_utf8(params_[i]))
290 docstring const InsetCommandParams::getCommand() const
292 docstring s = '\\' + from_ascii(cmdName_);
294 for (size_t i = 0; i < info_->n; ++i) {
295 if (info_->optional[i]) {
296 if (params_[i].empty()) {
297 // We need to write this parameter even if
298 // it is empty if nonempty optional parameters
299 // follow before the next required parameter.
300 for (size_t j = i + 1; j < info_->n; ++j) {
301 if (!info_->optional[j])
303 if (!params_[j].empty()) {
310 s += '[' + params_[i] + ']';
314 s += '{' + params_[i] + '}';
319 // Make sure that following stuff does not change the
326 docstring const InsetCommandParams::getFirstNonOptParam() const
328 for (size_t i = 0; i < info_->n; ++i)
329 if (!info_->optional[i])
336 docstring const & InsetCommandParams::operator[](string const & name) const
338 int const i = findToken(info_->paramnames, name);
339 BOOST_ASSERT(i >= 0);
344 docstring & InsetCommandParams::operator[](string const & name)
346 int const i = findToken(info_->paramnames, name);
347 BOOST_ASSERT(i >= 0);
352 void InsetCommandParams::clear()
354 for (size_t i = 0; i < info_->n; ++i)
359 bool operator==(InsetCommandParams const & o1,
360 InsetCommandParams const & o2)
362 return o1.insetCode_ == o2.insetCode_ &&
363 o1.cmdName_ == o2.cmdName_ &&
364 o1.info_ == o2.info_ &&
365 o1.params_ == o2.params_ &&
366 o1.preview_ == o2.preview_;
370 bool operator!=(InsetCommandParams const & o1,
371 InsetCommandParams const & o2)