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
39 b += "|++|" + options + "|++|" + contents;
44 void InsetCommandParams::setFromString( string const & b )
46 string::size_type idx = b.find("|++|");
47 if( idx == string::npos ) {
54 cmdname = b.substr(0, idx);
55 string tmp = b.substr(idx+4);
57 idx = tmp.find("|++|");
58 if( idx == string::npos ) {
61 options = tmp.substr(0, idx);
62 contents = tmp.substr(idx+4);
67 bool InsetCommandParams::operator==(InsetCommandParams const & o) const
69 if (cmdname == o.cmdname && contents == o.contents && options == o.options) return true;
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'");
154 token = lex.GetString();
155 if (token == "\\end_inset")
158 if (token != "\\end_inset") {
159 lex.printError("Missing \\end_inset at this point. "
165 void InsetCommandParams::Write(ostream & os) const
167 os << "LatexCommand " << getCommand() << "\n";
171 string const InsetCommandParams::getCommand() const
174 if (!getCmdName().empty()) s += "\\"+getCmdName();
175 if (!getOptions().empty()) s += "["+getOptions()+']';
176 s += "{"+getContents()+'}';
181 InsetCommand::InsetCommand( InsetCommandParams const & p )
182 : p_( p.getCmdName(), p.getContents(), p.getOptions() )
186 void InsetCommand::setParams(InsetCommandParams const & p )
188 p_.setCmdName( p.getCmdName() );
189 p_.setContents( p.getContents() );
190 p_.setOptions( p.getOptions() );
194 int InsetCommand::Latex(Buffer const *, ostream & os,
195 bool /*fragile*/, bool/*fs*/) const
202 int InsetCommand::Ascii(Buffer const *, ostream &, int) const
208 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
214 int InsetCommand::DocBook(Buffer const *, ostream &) const