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 #include "insetcommandparams.h"
18 #include "support/LOstream.h"
24 InsetCommandParams::InsetCommandParams()
28 InsetCommandParams::InsetCommandParams(string const & n,
31 : cmdname(n), contents(c), options(o), preview_(false)
35 string const InsetCommandParams::getAsString() const
37 return cmdname + "|++|" + contents + "|++|" + options;
41 void InsetCommandParams::setFromString(string const & b)
43 string::size_type idx = b.find("|++|");
44 if (idx == string::npos) {
51 cmdname = b.substr(0, idx);
52 string tmp = b.substr(idx+4);
54 idx = tmp.find("|++|");
55 if (idx == string::npos) {
59 contents = tmp.substr(0, idx);
60 options = tmp.substr(idx+4);
65 void InsetCommandParams::scanCommand(string const & cmd)
67 string tcmdname, toptions, tcontents;
69 if (cmd.empty()) return;
71 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
73 // Used to handle things like \command[foo[bar]]{foo{bar}}
76 for (string::size_type i = 0; i < cmd.length(); ++i) {
78 if ((state == CMDNAME && c == ' ') ||
79 (state == CMDNAME && c == '[') ||
80 (state == CMDNAME && c == '{')) {
83 if ((state == OPTION && c == ']') ||
84 (state == CONTENT && c == '}')) {
91 if ((state == OPTION && c == '[') ||
92 (state == CONTENT && c == '{')) {
96 case CMDNAME: tcmdname += c; break;
97 case OPTION: toptions += c; break;
98 case CONTENT: tcontents += c; break;
102 } else if (c == '[') {
104 nestdepth = 0; // Just to be sure
105 } else if (c == '{') {
107 nestdepth = 0; // Just to be sure
113 // Don't mess with this.
114 if (!tcmdname.empty()) setCmdName(tcmdname);
115 if (!toptions.empty()) setOptions(toptions);
116 if (!tcontents.empty()) setContents(tcontents);
118 if (lyxerr.debugging(Debug::PARSER))
119 lyxerr << "Command <" << cmd
120 << "> == <" << getCommand()
121 << "> == <" << getCmdName()
122 << '|' << getContents()
123 << '|' << getOptions() << '>' << endl;
127 void InsetCommandParams::read(LyXLex & lex)
132 token = lex.getString();
135 lex.printError("InsetCommand: Parse error: `$$Token'");
140 token = lex.getString();
141 if (token == "\\end_inset")
143 if (token == "preview") {
145 preview_ = lex.getBool();
148 if (token != "\\end_inset") {
149 lex.printError("Missing \\end_inset at this point. "
155 void InsetCommandParams::write(ostream & os) const
157 os << "LatexCommand " << getCommand() << "\n";
161 string const InsetCommandParams::getCommand() const
164 if (!getCmdName().empty()) s += '\\' + getCmdName();
165 if (!getOptions().empty()) s += '[' + getOptions() + ']';
166 s += '{' + getContents() + '}';
171 bool operator==(InsetCommandParams const & o1,
172 InsetCommandParams const & o2)
174 return o1.getCmdName() == o2.getCmdName()
175 && o1.getContents() == o2.getContents()
176 && o1.getOptions() == o2.getOptions()
177 && o1.preview() == o2.preview();
181 bool operator!=(InsetCommandParams const & o1,
182 InsetCommandParams const & o2)