2 * \file insetcommandparams.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "insetcommandparams.h"
24 InsetCommandParams::InsetCommandParams()
28 InsetCommandParams::InsetCommandParams(string const & n,
32 : cmdname(n), contents(c), options(o), sec_options(s),
37 void InsetCommandParams::scanCommand(string const & cmd)
39 string tcmdname, toptions, tsecoptions, tcontents;
41 if (cmd.empty()) return;
43 enum { WS, CMDNAME, OPTION, SECOPTION, CONTENT } state = WS;
45 // Used to handle things like \command[foo[bar]]{foo{bar}}
48 for (string::size_type i = 0; i < cmd.length(); ++i) {
49 char const c = cmd[i];
50 if ((state == CMDNAME && c == ' ') ||
51 (state == CMDNAME && c == '[') ||
52 (state == CMDNAME && c == '{')) {
55 if ((state == OPTION && c == ']') ||
56 (state == SECOPTION && c == ']') ||
57 (state == CONTENT && c == '}')) {
64 if ((state == OPTION && c == '[') ||
65 (state == SECOPTION && c == '[') ||
66 (state == CONTENT && c == '{')) {
70 case CMDNAME: tcmdname += c; break;
71 case OPTION: toptions += c; break;
72 case SECOPTION: tsecoptions += c; break;
73 case CONTENT: tcontents += c; break;
75 char const b = i? cmd[i-1]: 0;
78 } else if (c == '[' && b != ']') {
80 nestdepth = 0; // Just to be sure
81 } else if (c == '[' && b == ']') {
83 nestdepth = 0; // Just to be sure
84 } else if (c == '{') {
86 nestdepth = 0; // Just to be sure
93 // Don't mess with this.
94 if (!tcmdname.empty()) setCmdName(tcmdname);
95 if (!toptions.empty()) setOptions(toptions);
96 if (!tsecoptions.empty()) setSecOptions(tsecoptions);
97 if (!tcontents.empty()) setContents(tcontents);
99 if (lyxerr.debugging(Debug::PARSER))
100 lyxerr << "Command <" << cmd
101 << "> == <" << getCommand()
102 << "> == <" << getCmdName()
103 << '|' << getContents()
104 << '|' << getOptions()
105 << '|' << getSecOptions() << '>' << endl;
109 void InsetCommandParams::read(LyXLex & lex)
114 token = lex.getString();
117 lex.printError("InsetCommand: Parse error: `$$Token'");
122 token = lex.getString();
123 if (token == "\\end_inset")
125 if (token == "preview") {
127 preview_ = lex.getBool();
130 if (token != "\\end_inset") {
131 lex.printError("Missing \\end_inset at this point. "
137 void InsetCommandParams::write(ostream & os) const
139 os << "LatexCommand " << getCommand() << "\n";
143 string const InsetCommandParams::getCommand() const
146 if (!getCmdName().empty()) s += '\\' + getCmdName();
147 if (!getOptions().empty()) s += '[' + getOptions() + ']';
148 if (!getSecOptions().empty()) {
149 // for cases like \command[][sec_option]{arg}
150 if (getOptions().empty()) s += "[]";
151 s += '[' + getSecOptions() + ']';
153 s += '{' + getContents() + '}';
158 bool operator==(InsetCommandParams const & o1,
159 InsetCommandParams const & o2)
161 return o1.getCmdName() == o2.getCmdName()
162 && o1.getContents() == o2.getContents()
163 && o1.getOptions() == o2.getOptions()
164 && o1.getSecOptions() == o2.getSecOptions()
165 && o1.preview() == o2.preview();
169 bool operator!=(InsetCommandParams const & o1,
170 InsetCommandParams const & o2)