1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetcommand.h"
26 InsetCommandParams::InsetCommandParams()
30 InsetCommandParams::InsetCommandParams( string const & n,
33 : cmdname(n), contents(c), options(o)
37 string const InsetCommandParams::getAsString() const
39 return cmdname + "|++|" + contents + "|++|" + options;
43 void InsetCommandParams::setFromString( string const & b )
45 string::size_type idx = b.find("|++|");
46 if (idx == string::npos) {
53 cmdname = b.substr(0, idx);
54 string tmp = b.substr(idx+4);
56 idx = tmp.find("|++|");
57 if (idx == string::npos) {
61 contents = tmp.substr(0, idx);
62 options = tmp.substr(idx+4);
67 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
69 return cmdname == o.cmdname && contents == o.contents
70 && options == o.options;
74 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
80 void InsetCommandParams::scanCommand(string const & cmd)
82 string tcmdname, toptions, tcontents;
84 if (cmd.empty()) return;
86 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
88 // Used to handle things like \command[foo[bar]]{foo{bar}}
91 for (string::size_type i = 0; i < cmd.length(); ++i) {
93 if ((state == CMDNAME && c == ' ') ||
94 (state == CMDNAME && c == '[') ||
95 (state == CMDNAME && c == '{')) {
98 if ((state == OPTION && c == ']') ||
99 (state == CONTENT && c == '}')) {
100 if (nestdepth == 0) {
106 if ((state == OPTION && c == '[') ||
107 (state == CONTENT && c == '{')) {
111 case CMDNAME: tcmdname += c; break;
112 case OPTION: toptions += c; break;
113 case CONTENT: tcontents += c; break;
117 } else if (c == '[') {
119 nestdepth = 0; // Just to be sure
120 } else if (c == '{') {
122 nestdepth = 0; // Just to be sure
128 // Don't mess with this.
129 if (!tcmdname.empty()) setCmdName( tcmdname );
130 if (!toptions.empty()) setOptions( toptions );
131 if (!tcontents.empty()) setContents( tcontents );
133 if (lyxerr.debugging(Debug::PARSER))
134 lyxerr << "Command <" << cmd
135 << "> == <" << getCommand()
136 << "> == <" << getCmdName()
137 << '|' << getContents()
138 << '|' << getOptions() << '>' << endl;
142 // This function will not be necessary when lyx3
143 void InsetCommandParams::read(LyXLex & lex)
148 token = lex.getString();
151 lex.printError("InsetCommand: Parse error: `$$Token'");
156 token = lex.getString();
157 if (token == "\\end_inset")
160 if (token != "\\end_inset") {
161 lex.printError("Missing \\end_inset at this point. "
167 void InsetCommandParams::write(ostream & os) const
169 os << "LatexCommand " << getCommand() << "\n";
173 string const InsetCommandParams::getCommand() const
176 if (!getCmdName().empty()) s += "\\"+getCmdName();
177 if (!getOptions().empty()) s += "["+getOptions()+']';
178 s += "{"+getContents()+'}';
183 InsetCommand::InsetCommand(InsetCommandParams const & p, bool)
184 : p_( p.getCmdName(), p.getContents(), p.getOptions() )
188 void InsetCommand::setParams(InsetCommandParams const & p )
190 p_.setCmdName( p.getCmdName() );
191 p_.setContents( p.getContents() );
192 p_.setOptions( p.getOptions() );
196 int InsetCommand::latex(Buffer const *, ostream & os,
197 bool /*fragile*/, bool/*fs*/) const
204 int InsetCommand::ascii(Buffer const *, ostream &, int) const
210 int InsetCommand::linuxdoc(Buffer const *, ostream &) const
216 int InsetCommand::docBook(Buffer const *, ostream &) const