1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2002-2002 The LyX Team.
8 * ====================================================== */
12 #pragma implementation
15 #include "insetcommandparams.h"
20 InsetCommandParams::InsetCommandParams()
24 InsetCommandParams::InsetCommandParams(string const & n,
27 : cmdname(n), contents(c), options(o)
31 string const InsetCommandParams::getAsString() const
33 return cmdname + "|++|" + contents + "|++|" + options;
37 void InsetCommandParams::setFromString(string const & b)
39 string::size_type idx = b.find("|++|");
40 if (idx == string::npos) {
47 cmdname = b.substr(0, idx);
48 string tmp = b.substr(idx+4);
50 idx = tmp.find("|++|");
51 if (idx == string::npos) {
55 contents = tmp.substr(0, idx);
56 options = tmp.substr(idx+4);
61 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
63 return cmdname == o.cmdname && contents == o.contents
64 && options == o.options;
68 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
74 void InsetCommandParams::scanCommand(string const & cmd)
76 string tcmdname, toptions, tcontents;
78 if (cmd.empty()) return;
80 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
82 // Used to handle things like \command[foo[bar]]{foo{bar}}
85 for (string::size_type i = 0; i < cmd.length(); ++i) {
87 if ((state == CMDNAME && c == ' ') ||
88 (state == CMDNAME && c == '[') ||
89 (state == CMDNAME && c == '{')) {
92 if ((state == OPTION && c == ']') ||
93 (state == CONTENT && c == '}')) {
100 if ((state == OPTION && c == '[') ||
101 (state == CONTENT && c == '{')) {
105 case CMDNAME: tcmdname += c; break;
106 case OPTION: toptions += c; break;
107 case CONTENT: tcontents += c; break;
111 } else if (c == '[') {
113 nestdepth = 0; // Just to be sure
114 } else if (c == '{') {
116 nestdepth = 0; // Just to be sure
122 // Don't mess with this.
123 if (!tcmdname.empty()) setCmdName(tcmdname);
124 if (!toptions.empty()) setOptions(toptions);
125 if (!tcontents.empty()) setContents(tcontents);
127 if (lyxerr.debugging(Debug::PARSER))
128 lyxerr << "Command <" << cmd
129 << "> == <" << getCommand()
130 << "> == <" << getCmdName()
131 << '|' << getContents()
132 << '|' << getOptions() << '>' << endl;
136 void InsetCommandParams::read(LyXLex & lex)
141 token = lex.getString();
144 lex.printError("InsetCommand: Parse error: `$$Token'");
149 token = lex.getString();
150 if (token == "\\end_inset")
153 if (token != "\\end_inset") {
154 lex.printError("Missing \\end_inset at this point. "
160 void InsetCommandParams::write(ostream & os) const
162 os << "LatexCommand " << getCommand() << "\n";
166 string const InsetCommandParams::getCommand() const
169 if (!getCmdName().empty()) s += "\\"+getCmdName();
170 if (!getOptions().empty()) s += "["+getOptions()+']';
171 s += "{"+getContents()+'}';