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
12 #pragma implementation
15 #include "insetcommandparams.h"
19 #include "support/LOstream.h"
25 InsetCommandParams::InsetCommandParams()
29 InsetCommandParams::InsetCommandParams(string const & n,
32 : cmdname(n), contents(c), options(o), preview_(false)
36 string const InsetCommandParams::getAsString() const
38 return cmdname + "|++|" + contents + "|++|" + options;
42 void InsetCommandParams::setFromString(string const & b)
44 string::size_type idx = b.find("|++|");
45 if (idx == string::npos) {
52 cmdname = b.substr(0, idx);
53 string tmp = b.substr(idx+4);
55 idx = tmp.find("|++|");
56 if (idx == string::npos) {
60 contents = tmp.substr(0, idx);
61 options = tmp.substr(idx+4);
66 void InsetCommandParams::scanCommand(string const & cmd)
68 string tcmdname, toptions, tcontents;
70 if (cmd.empty()) return;
72 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
74 // Used to handle things like \command[foo[bar]]{foo{bar}}
77 for (string::size_type i = 0; i < cmd.length(); ++i) {
79 if ((state == CMDNAME && c == ' ') ||
80 (state == CMDNAME && c == '[') ||
81 (state == CMDNAME && c == '{')) {
84 if ((state == OPTION && c == ']') ||
85 (state == CONTENT && c == '}')) {
92 if ((state == OPTION && c == '[') ||
93 (state == CONTENT && c == '{')) {
97 case CMDNAME: tcmdname += c; break;
98 case OPTION: toptions += c; break;
99 case CONTENT: tcontents += c; break;
103 } else if (c == '[') {
105 nestdepth = 0; // Just to be sure
106 } else if (c == '{') {
108 nestdepth = 0; // Just to be sure
114 // Don't mess with this.
115 if (!tcmdname.empty()) setCmdName(tcmdname);
116 if (!toptions.empty()) setOptions(toptions);
117 if (!tcontents.empty()) setContents(tcontents);
119 if (lyxerr.debugging(Debug::PARSER))
120 lyxerr << "Command <" << cmd
121 << "> == <" << getCommand()
122 << "> == <" << getCmdName()
123 << '|' << getContents()
124 << '|' << getOptions() << '>' << endl;
128 void InsetCommandParams::read(LyXLex & lex)
133 token = lex.getString();
136 lex.printError("InsetCommand: Parse error: `$$Token'");
141 token = lex.getString();
142 if (token == "\\end_inset")
144 if (token == "preview") {
146 preview_ = lex.getBool();
149 if (token != "\\end_inset") {
150 lex.printError("Missing \\end_inset at this point. "
156 void InsetCommandParams::write(ostream & os) const
158 os << "LatexCommand " << getCommand() << "\n";
162 string const InsetCommandParams::getCommand() const
165 if (!getCmdName().empty()) s += "\\"+getCmdName();
166 if (!getOptions().empty()) s += "["+getOptions()+']';
167 s += "{"+getContents()+'}';
172 bool operator==(InsetCommandParams const & o1,
173 InsetCommandParams const & o2)
175 return o1.getCmdName() == o2.getCmdName()
176 && o1.getContents() == o2.getContents()
177 && o1.getOptions() == o2.getOptions()
178 && o1.preview() == o2.preview();
182 bool operator!=(InsetCommandParams const & o1,
183 InsetCommandParams const & o2)