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) {
51 if ((state == CMDNAME && c == ' ') ||
52 (state == CMDNAME && c == '[') ||
53 (state == CMDNAME && c == '{')) {
56 if ((state == OPTION && c == ']') ||
57 (state == SECOPTION && c == ']') ||
58 (state == CONTENT && c == '}')) {
65 if ((state == OPTION && c == '[') ||
66 (state == SECOPTION && c == '[') ||
67 (state == CONTENT && c == '{')) {
71 case CMDNAME: tcmdname += c; break;
72 case OPTION: toptions += c; break;
73 case SECOPTION: tsecoptions += c; break;
74 case CONTENT: tcontents += c; break;
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
92 // Don't mess with this.
93 if (!tcmdname.empty()) setCmdName(tcmdname);
94 if (!toptions.empty()) setOptions(toptions);
95 if (!tsecoptions.empty()) setSecOptions(tsecoptions);
96 if (!tcontents.empty()) setContents(tcontents);
98 if (lyxerr.debugging(Debug::PARSER))
99 lyxerr << "Command <" << cmd
100 << "> == <" << getCommand()
101 << "> == <" << getCmdName()
102 << '|' << getContents()
103 << '|' << getOptions()
104 << '|' << getSecOptions() << '>' << endl;
108 void InsetCommandParams::read(LyXLex & lex)
113 token = lex.getString();
116 lex.printError("InsetCommand: Parse error: `$$Token'");
121 token = lex.getString();
122 if (token == "\\end_inset")
124 if (token == "preview") {
126 preview_ = lex.getBool();
129 if (token != "\\end_inset") {
130 lex.printError("Missing \\end_inset at this point. "
136 void InsetCommandParams::write(ostream & os) const
138 os << "LatexCommand " << getCommand() << "\n";
142 string const InsetCommandParams::getCommand() const
145 if (!getCmdName().empty()) s += '\\' + getCmdName();
146 if (!getOptions().empty()) s += '[' + getOptions() + ']';
147 if (!getSecOptions().empty()) {
148 // for cases like \command[][sec_option]{arg}
149 if (getOptions().empty()) s += "[]";
150 s += '[' + getSecOptions() + ']';
152 s += '{' + getContents() + '}';
157 bool operator==(InsetCommandParams const & o1,
158 InsetCommandParams const & o2)
160 return o1.getCmdName() == o2.getCmdName()
161 && o1.getContents() == o2.getContents()
162 && o1.getOptions() == o2.getOptions()
163 && o1.getSecOptions() == o2.getSecOptions()
164 && o1.preview() == o2.preview();
168 bool operator!=(InsetCommandParams const & o1,
169 InsetCommandParams const & o2)