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 ) return;
49 cmdname = b.substr(0, idx);
50 string tmp = b.substr(idx+4);
52 idx = tmp.find("|++|");
53 if( idx == string::npos ) {
56 options = tmp.substr(0, idx);
57 contents = tmp.substr(idx+4);
62 InsetCommand::InsetCommand()
66 InsetCommand::InsetCommand( string const & n,
73 InsetCommand::InsetCommand( InsetCommandParams const & p )
74 : p_( p.getCmdName(), p.getContents(), p.getOptions() )
78 void InsetCommand::setParams(InsetCommandParams const & p )
80 p_.setCmdName( p.getCmdName() );
81 p_.setContents( p.getContents() );
82 p_.setOptions( p.getOptions() );
86 // In lyxf3 this will be just LaTeX
87 void InsetCommand::Write(Buffer const *, ostream & os) const
89 os << "LatexCommand " << getCommand() << "\n";
93 void InsetCommand::scanCommand(string const & cmd)
95 string tcmdname, toptions, tcontents;
97 if (cmd.empty()) return;
99 enum { WS, CMDNAME, OPTION, CONTENT } state = WS;
101 // Used to handle things like \command[foo[bar]]{foo{bar}}
104 for (string::size_type i = 0; i < cmd.length(); ++i) {
106 if ((state == CMDNAME && c == ' ') ||
107 (state == CMDNAME && c == '[') ||
108 (state == CMDNAME && c == '{')) {
111 if ((state == OPTION && c == ']') ||
112 (state == CONTENT && c == '}')) {
113 if (nestdepth == 0) {
119 if ((state == OPTION && c == '[') ||
120 (state == CONTENT && c == '{')) {
124 case CMDNAME: tcmdname += c; break;
125 case OPTION: toptions += c; break;
126 case CONTENT: tcontents += c; break;
130 } else if (c == '[') {
132 nestdepth = 0; // Just to be sure
133 } else if (c == '{') {
135 nestdepth = 0; // Just to be sure
141 // Don't mess with this.
142 if (!tcmdname.empty()) setCmdName( tcmdname );
143 if (!toptions.empty()) setOptions( toptions );
144 if (!tcontents.empty()) setContents( tcontents );
146 if (lyxerr.debugging(Debug::PARSER))
147 lyxerr << "Command <" << cmd
148 << "> == <" << getCommand()
149 << "> == <" << getCmdName()
150 << '|' << getContents()
151 << '|' << getOptions() << '>' << endl;
155 // This function will not be necessary when lyx3
156 void InsetCommand::Read(Buffer const *, LyXLex & lex)
161 token = lex.GetString();
164 lex.printError("InsetCommand: Parse error: `$$Token'");
167 token = lex.GetString();
168 if (token == "\\end_inset")
171 if (token != "\\end_inset") {
172 lex.printError("Missing \\end_inset at this point. "
178 int InsetCommand::Latex(Buffer const *, ostream & os,
179 bool /*fragile*/, bool/*fs*/) const
186 int InsetCommand::Ascii(Buffer const *, ostream &) const
192 int InsetCommand::Linuxdoc(Buffer const *, ostream &) const
198 int InsetCommand::DocBook(Buffer const *, ostream &) const
204 Inset * InsetCommand::Clone() const
206 return new InsetCommand(getCmdName(), getContents(), getOptions());
210 string InsetCommand::getCommand() const
213 if (!getCmdName().empty()) s += "\\"+getCmdName();
214 if (!getOptions().empty()) s += "["+getOptions()+']';
215 s += "{"+getContents()+'}';