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
14 #pragma implementation
17 #include "insetcommandparams.h"
21 #include "support/LOstream.h"
27 InsetCommandParams::InsetCommandParams()
31 InsetCommandParams::InsetCommandParams(string const & n,
34 : cmdname(n), contents(c), options(o), preview_(false)
38 string const InsetCommandParams::getAsString() const
40 return cmdname + "|++|" + contents + "|++|" + options;
44 void InsetCommandParams::setFromString(string const & b)
46 string::size_type idx = b.find("|++|");
47 if (idx == string::npos) {
54 cmdname = b.substr(0, idx);
55 string tmp = b.substr(idx+4);
57 idx = tmp.find("|++|");
58 if (idx == string::npos) {
62 contents = tmp.substr(0, idx);
63 options = tmp.substr(idx+4);
68 void InsetCommandParams::scanCommand(string const & cmd)
70 string tcmdname, toptions, tcontents;
72 if (cmd.empty()) return;
74 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
76 // Used to handle things like \command[foo[bar]]{foo{bar}}
79 for (string::size_type i = 0; i < cmd.length(); ++i) {
81 if ((state == CMDNAME && c == ' ') ||
82 (state == CMDNAME && c == '[') ||
83 (state == CMDNAME && c == '{')) {
86 if ((state == OPTION && c == ']') ||
87 (state == CONTENT && c == '}')) {
94 if ((state == OPTION && c == '[') ||
95 (state == CONTENT && c == '{')) {
99 case CMDNAME: tcmdname += c; break;
100 case OPTION: toptions += c; break;
101 case CONTENT: tcontents += c; break;
105 } else if (c == '[') {
107 nestdepth = 0; // Just to be sure
108 } else if (c == '{') {
110 nestdepth = 0; // Just to be sure
116 // Don't mess with this.
117 if (!tcmdname.empty()) setCmdName(tcmdname);
118 if (!toptions.empty()) setOptions(toptions);
119 if (!tcontents.empty()) setContents(tcontents);
121 if (lyxerr.debugging(Debug::PARSER))
122 lyxerr << "Command <" << cmd
123 << "> == <" << getCommand()
124 << "> == <" << getCmdName()
125 << '|' << getContents()
126 << '|' << getOptions() << '>' << endl;
130 void InsetCommandParams::read(LyXLex & lex)
135 token = lex.getString();
138 lex.printError("InsetCommand: Parse error: `$$Token'");
143 token = lex.getString();
144 if (token == "\\end_inset")
146 if (token == "preview") {
148 preview_ = lex.getBool();
151 if (token != "\\end_inset") {
152 lex.printError("Missing \\end_inset at this point. "
158 void InsetCommandParams::write(ostream & os) const
160 os << "LatexCommand " << getCommand() << "\n";
164 string const InsetCommandParams::getCommand() const
167 if (!getCmdName().empty()) s += '\\' + getCmdName();
168 if (!getOptions().empty()) s += '[' + getOptions() + ']';
169 s += '{' + getContents() + '}';
174 bool operator==(InsetCommandParams const & o1,
175 InsetCommandParams const & o2)
177 return o1.getCmdName() == o2.getCmdName()
178 && o1.getContents() == o2.getContents()
179 && o1.getOptions() == o2.getOptions()
180 && o1.preview() == o2.preview();
184 bool operator!=(InsetCommandParams const & o1,
185 InsetCommandParams const & o2)