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
9 * Full author contact details are available in file CREDITS.
14 #include "InsetCommandParams.h"
20 #include "support/ExceptionMessage.h"
21 #include "support/lstrings.h"
23 #include <boost/assert.hpp>
28 using support::findToken;
34 using support::ExceptionMessage;
35 using support::WarningException;
37 InsetCommandParams::InsetCommandParams(string const & name)
38 : name_(name), preview_(false)
40 info_ = findInfo(name);
42 params_.resize(info_->n);
46 InsetCommandParams::CommandInfo const *
47 InsetCommandParams::findInfo(std::string const & name)
49 // No parameter may be named "preview", because that is a required
50 // flag for all commands.
53 if (name == "bibitem") {
54 static const char * const paramnames[] = {"label", "key", ""};
55 static const bool isoptional[] = {true, false};
56 static const CommandInfo info = {2, paramnames, isoptional};
61 if (name == "bibtex") {
62 static const char * const paramnames[] =
63 {"options", "btprint", "bibfiles", ""};
64 static const bool isoptional[] = {true, true, false};
65 static const CommandInfo info = {3, paramnames, isoptional};
70 // FIXME: Use is_possible_cite_command() in
71 // src/frontends/controllers/biblio.C, see comment in src/factory.C.
72 if (name == "cite" || name == "citet" || name == "citep" || name == "citealt" ||
73 name == "citealp" || name == "citeauthor" || name == "citeyear" ||
74 name == "citeyearpar" || name == "citet*" || name == "citep*" ||
75 name == "citealt*" || name == "citealp*" ||
76 name == "citeauthor*" || name == "Citet" || name == "Citep" ||
77 name == "Citealt" || name == "Citealp" || name == "Citeauthor" ||
78 name == "Citet*" || name == "Citep*" || name == "Citealt*" ||
79 name == "Citealp*" || name == "Citeauthor*" ||
80 name == "citefield" || name == "citetitle" || name == "cite*") {
81 // standard cite does only take one argument if jurabib is
82 // not used, but jurabib extends this to two arguments, so
83 // we have to allow both here. InsetCitation takes care that
84 // LaTeX output is nevertheless correct.
85 static const char * const paramnames[] =
86 {"after", "before", "key", ""};
87 static const bool isoptional[] = {true, true, false};
88 static const CommandInfo info = {3, paramnames, isoptional};
93 if (name == "floatlist") {
94 static const char * const paramnames[] = {"type", ""};
95 static const bool isoptional[] = {false};
96 static const CommandInfo info = {1, paramnames, isoptional};
101 if (name == "hfill") {
102 static const char * const paramnames[] = {""};
103 static const CommandInfo info = {0, paramnames, 0};
108 if (name == "include" || name == "input" || name == "verbatiminput" ||
109 name == "verbatiminput*") {
110 static const char * const paramnames[] = {"filename", ""};
111 static const bool isoptional[] = {false};
112 static const CommandInfo info = {1, paramnames, isoptional};
116 // InsetIndex, InsetPrintIndex, InsetLabel
117 if (name == "index" || name == "printindex" || name == "label") {
118 static const char * const paramnames[] = {"name", ""};
119 static const bool isoptional[] = {false};
120 static const CommandInfo info = {1, paramnames, isoptional};
125 if (name == "nomenclature") {
126 static const char * const paramnames[] = {"prefix", "symbol", "description", ""};
127 static const bool isoptional[] = {true, false, false};
128 static const CommandInfo info = {3, paramnames, isoptional};
133 if (name == "printnomenclature") {
134 static const char * const paramnames[] = {"labelwidth", ""};
135 static const bool isoptional[] = {true};
136 static const CommandInfo info = {1, paramnames, isoptional};
141 if (name == "eqref" || name == "pageref" || name == "vpageref" ||
142 name == "vref" || name == "prettyref" || name == "ref") {
143 static const char * const paramnames[] =
144 {"name", "reference", ""};
145 static const bool isoptional[] = {true, false};
146 static const CommandInfo info = {2, paramnames, isoptional};
151 if (name == "tableofcontents") {
152 static const char * const paramnames[] = {"type", ""};
153 static const bool isoptional[] = {false};
154 static const CommandInfo info = {1, paramnames, isoptional};
159 if (name == "htmlurl" || name == "url") {
160 static const char * const paramnames[] =
161 {"name", "target", ""};
162 static const bool isoptional[] = {true, false};
163 static const CommandInfo info = {2, paramnames, isoptional};
171 void InsetCommandParams::setCmdName(string const & name)
174 CommandInfo const * const info = findInfo(name);
176 ParamVector params(info->n);
177 // Overtake parameters with the same name
178 for (size_t i = 0; i < info_->n; ++i) {
179 int j = findToken(info->paramnames, info_->paramnames[i]);
181 params[j] = params_[i];
184 std::swap(params, params_);
188 void InsetCommandParams::scanCommand(string const & cmd)
190 string tcmdname, toptions, tsecoptions, tcontents;
192 if (cmd.empty()) return;
194 enum { WS, CMDNAME, OPTION, SECOPTION, CONTENT } state = WS;
196 // Used to handle things like \command[foo[bar]]{foo{bar}}
199 for (string::size_type i = 0; i < cmd.length(); ++i) {
200 char const c = cmd[i];
201 if ((state == CMDNAME && c == ' ') ||
202 (state == CMDNAME && c == '[') ||
203 (state == CMDNAME && c == '{')) {
206 if ((state == OPTION && c == ']') ||
207 (state == SECOPTION && c == ']') ||
208 (state == CONTENT && c == '}')) {
209 if (nestdepth == 0) {
215 if ((state == OPTION && c == '[') ||
216 (state == SECOPTION && c == '[') ||
217 (state == CONTENT && c == '{')) {
221 case CMDNAME: tcmdname += c; break;
222 case OPTION: toptions += c; break;
223 case SECOPTION: tsecoptions += c; break;
224 case CONTENT: tcontents += c; break;
226 char const b = i? cmd[i-1]: 0;
229 } else if (c == '[' && b != ']') {
231 nestdepth = 0; // Just to be sure
232 } else if (c == '[' && b == ']') {
234 nestdepth = 0; // Just to be sure
235 } else if (c == '{') {
237 nestdepth = 0; // Just to be sure
244 // Don't mess with this.
245 if (!tcmdname.empty()) setCmdName(tcmdname);
246 if (!toptions.empty()) setOptions(toptions);
247 if (!tsecoptions.empty()) setSecOptions(tsecoptions);
248 if (!tcontents.empty()) setContents(tcontents);
250 LYXERR(Debug::PARSER) << "Command <" << cmd
251 << "> == <" << to_utf8(getCommand())
252 << "> == <" << getCmdName()
253 << '|' << getContents()
254 << '|' << getOptions()
255 << '|' << getSecOptions() << '>' << endl;
259 void InsetCommandParams::read(LyXLex & lex)
263 name_ = lex.getString();
264 info_ = findInfo(name_);
266 lex.printError("InsetCommand: Unknown inset name `$$Token'");
267 throw ExceptionMessage(WarningException,
268 _("Unknown inset name: "),
276 token = lex.getString();
277 if (token == "\\end_inset")
279 // FIXME Why is preview_ read but not written?
280 if (token == "preview") {
282 preview_ = lex.getBool();
285 int const i = findToken(info_->paramnames, token);
288 params_[i] = lex.getDocString();
290 lex.printError("Unknown parameter name `$$Token' for command " + name_);
291 throw ExceptionMessage(WarningException,
292 _("Inset Command: ") + from_ascii(name_),
293 _("Unknown parameter name: ") + from_utf8(token));
296 if (token != "\\end_inset") {
297 lex.printError("Missing \\end_inset at this point. "
299 throw ExceptionMessage(WarningException,
300 _("Missing \\end_inset at this point."),
306 void InsetCommandParams::write(ostream & os) const
308 os << "LatexCommand " << name_ << '\n';
309 for (size_t i = 0; i < info_->n; ++i)
310 if (!params_[i].empty())
312 os << info_->paramnames[i] << ' '
313 << LyXLex::quoteString(to_utf8(params_[i]))
318 docstring const InsetCommandParams::getCommand() const
320 docstring s = '\\' + from_ascii(name_);
322 for (size_t i = 0; i < info_->n; ++i) {
323 if (info_->optional[i]) {
324 if (params_[i].empty()) {
325 // We need to write this parameter even if
326 // it is empty if nonempty optional parameters
327 // follow before the next required parameter.
328 for (size_t j = i + 1; j < info_->n; ++j) {
329 if (!info_->optional[j])
331 if (!params_[j].empty()) {
338 s += '[' + params_[i] + ']';
342 s += '{' + params_[i] + '}';
347 // Make sure that following stuff does not change the
354 std::string const InsetCommandParams::getOptions() const
356 for (size_t i = 0; i < info_->n; ++i)
357 if (info_->optional[i])
358 return to_utf8(params_[i]);
359 lyxerr << "Programming error: get nonexisting option in "
360 << name_ << " inset." << endl;;
365 std::string const InsetCommandParams::getSecOptions() const
368 for (size_t i = 0; i < info_->n; ++i)
369 if (info_->optional[i]) {
373 return to_utf8(params_[i]);
375 // Happens in InsetCitation
376 lyxerr << "Programming error: get nonexisting second option in "
377 << name_ << " inset." << endl;;
382 std::string const InsetCommandParams::getContents() const
384 for (size_t i = 0; i < info_->n; ++i)
385 if (!info_->optional[i])
386 return to_utf8(params_[i]);
392 void InsetCommandParams::setOptions(std::string const & o)
394 for (size_t i = 0; i < info_->n; ++i)
395 if (info_->optional[i]) {
396 params_[i] = from_utf8(o);
399 lyxerr << "Programming error: set nonexisting option in "
400 << name_ << " inset." << endl;;
404 void InsetCommandParams::setSecOptions(std::string const & s)
407 for (size_t i = 0; i < info_->n; ++i)
408 if (info_->optional[i]) {
412 params_[i] = from_utf8(s);
416 // Happens in InsetCitation
417 lyxerr << "Programming error: set nonexisting second option in "
418 << name_ << " inset." << endl;;
422 void InsetCommandParams::setContents(std::string const & c)
424 for (size_t i = 0; i < info_->n; ++i)
425 if (!info_->optional[i]) {
426 params_[i] = from_utf8(c);
433 docstring const & InsetCommandParams::operator[](string const & name) const
435 int const i = findToken(info_->paramnames, name);
436 BOOST_ASSERT(i >= 0);
441 docstring & InsetCommandParams::operator[](string const & name)
443 int const i = findToken(info_->paramnames, name);
444 BOOST_ASSERT(i >= 0);
449 void InsetCommandParams::clear()
451 for (size_t i = 0; i < info_->n; ++i)
456 bool operator==(InsetCommandParams const & o1,
457 InsetCommandParams const & o2)
459 return o1.name_ == o2.name_ &&
460 o1.info_ == o2.info_ &&
461 o1.params_ == o2.params_ &&
462 o1.preview_ == o2.preview_;
466 bool operator!=(InsetCommandParams const & o1,
467 InsetCommandParams const & o2)