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 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 ) return false;
70 if( contents != o.contents ) return false;
71 if( options != o.options ) return false;
76 bool InsetCommandParams::operator!=(InsetCommandParams const & o) const
78 if( cmdname != o.cmdname ) return true;
79 if( contents != o.contents ) return true;
80 if( options != o.options ) return true;
85 void InsetCommandParams::scanCommand(string const & cmd)
87 string tcmdname, toptions, tcontents;
89 if (cmd.empty()) return;
91 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
93 // Used to handle things like \command[foo[bar]]{foo{bar}}
96 for (string::size_type i = 0; i < cmd.length(); ++i) {
98 if ((state == CMDNAME && c == ' ') ||
99 (state == CMDNAME && c == '[') ||
100 (state == CMDNAME && c == '{')) {
103 if ((state == OPTION && c == ']') ||
104 (state == CONTENT && c == '}')) {
105 if (nestdepth == 0) {
111 if ((state == OPTION && c == '[') ||
112 (state == CONTENT && c == '{')) {
116 case CMDNAME: tcmdname += c; break;
117 case OPTION: toptions += c; break;
118 case CONTENT: tcontents += c; break;
122 } else if (c == '[') {
124 nestdepth = 0; // Just to be sure
125 } else if (c == '{') {
127 nestdepth = 0; // Just to be sure
133 // Don't mess with this.
134 if (!tcmdname.empty()) setCmdName( tcmdname );
135 if (!toptions.empty()) setOptions( toptions );
136 if (!tcontents.empty()) setContents( tcontents );
138 if (lyxerr.debugging(Debug::PARSER))
139 lyxerr << "Command <" << cmd
140 << "> == <" << getCommand()
141 << "> == <" << getCmdName()
142 << '|' << getContents()
143 << '|' << getOptions() << '>' << endl;
147 // This function will not be necessary when lyx3
148 void InsetCommandParams::Read(LyXLex & lex)
153 token = lex.GetString();
156 lex.printError("InsetCommand: Parse error: `$$Token'");
159 token = lex.GetString();
160 if (token == "\\end_inset")
163 if (token != "\\end_inset") {
164 lex.printError("Missing \\end_inset at this point. "
170 void InsetCommandParams::Write(ostream & os) const
172 os << "LatexCommand " << getCommand() << "\n";
176 string InsetCommandParams::getCommand() const
179 if (!getCmdName().empty()) s += "\\"+getCmdName();
180 if (!getOptions().empty()) s += "["+getOptions()+']';
181 s += "{"+getContents()+'}';
186 InsetCommand::InsetCommand( InsetCommandParams const & p )
187 : p_( p.getCmdName(), p.getContents(), p.getOptions() )
191 void InsetCommand::setParams(InsetCommandParams const & p )
193 p_.setCmdName( p.getCmdName() );
194 p_.setContents( p.getContents() );
195 p_.setOptions( p.getOptions() );
199 int InsetCommand::Latex(Buffer const *, ostream & os,
200 bool /*fragile*/, bool/*fs*/) const
207 int InsetCommand::Ascii(Buffer const *, ostream &) const
213 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
219 int InsetCommand::DocBook(Buffer const *, ostream &) const