1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetcommand.h"
25 InsetCommandParams::InsetCommandParams()
29 InsetCommandParams::InsetCommandParams( string const & n,
32 : cmdname(n), contents(c), options(o)
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 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
68 if (cmdname == o.cmdname && contents == o.contents && options == o.options) return true;
73 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
79 void InsetCommandParams::scanCommand(string const & cmd)
81 string tcmdname, toptions, tcontents;
83 if (cmd.empty()) return;
85 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
87 // Used to handle things like \command[foo[bar]]{foo{bar}}
90 for (string::size_type i = 0; i < cmd.length(); ++i) {
92 if ((state == CMDNAME && c == ' ') ||
93 (state == CMDNAME && c == '[') ||
94 (state == CMDNAME && c == '{')) {
97 if ((state == OPTION && c == ']') ||
98 (state == CONTENT && c == '}')) {
105 if ((state == OPTION && c == '[') ||
106 (state == CONTENT && c == '{')) {
110 case CMDNAME: tcmdname += c; break;
111 case OPTION: toptions += c; break;
112 case CONTENT: tcontents += c; break;
116 } else if (c == '[') {
118 nestdepth = 0; // Just to be sure
119 } else if (c == '{') {
121 nestdepth = 0; // Just to be sure
127 // Don't mess with this.
128 if (!tcmdname.empty()) setCmdName( tcmdname );
129 if (!toptions.empty()) setOptions( toptions );
130 if (!tcontents.empty()) setContents( tcontents );
132 if (lyxerr.debugging(Debug::PARSER))
133 lyxerr << "Command <" << cmd
134 << "> == <" << getCommand()
135 << "> == <" << getCmdName()
136 << '|' << getContents()
137 << '|' << getOptions() << '>' << endl;
141 // This function will not be necessary when lyx3
142 void InsetCommandParams::Read(LyXLex & lex)
147 token = lex.GetString();
150 lex.printError("InsetCommand: Parse error: `$$Token'");
153 token = lex.GetString();
154 if (token == "\\end_inset")
157 if (token != "\\end_inset") {
158 lex.printError("Missing \\end_inset at this point. "
164 void InsetCommandParams::Write(ostream & os) const
166 os << "LatexCommand " << getCommand() << "\n";
170 string const InsetCommandParams::getCommand() const
173 if (!getCmdName().empty()) s += "\\"+getCmdName();
174 if (!getOptions().empty()) s += "["+getOptions()+']';
175 s += "{"+getContents()+'}';
180 InsetCommand::InsetCommand( InsetCommandParams const & p )
181 : p_( p.getCmdName(), p.getContents(), p.getOptions() )
185 void InsetCommand::setParams(InsetCommandParams const & p )
187 p_.setCmdName( p.getCmdName() );
188 p_.setContents( p.getContents() );
189 p_.setOptions( p.getOptions() );
193 int InsetCommand::Latex(Buffer const *, ostream & os,
194 bool /*fragile*/, bool/*fs*/) const
201 int InsetCommand::Ascii(Buffer const *, ostream &, int) const
207 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
213 int InsetCommand::DocBook(Buffer const *, ostream &) const