2002-09-25 14:26:13 +00:00
|
|
|
/**
|
|
|
|
* \file insetcommandparams.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-04 13:54:28 +00:00
|
|
|
*
|
2002-09-25 14:26:13 +00:00
|
|
|
* \author Angus Leeming
|
2002-07-04 13:54:28 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 14:26:13 +00:00
|
|
|
*/
|
2002-07-04 13:54:28 +00:00
|
|
|
|
2002-11-04 00:15:56 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2002-07-04 13:54:28 +00:00
|
|
|
#include "insetcommandparams.h"
|
|
|
|
|
2003-09-05 09:01:27 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "lyxlex.h"
|
2002-07-04 17:45:35 +00:00
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
|
|
using std::string;
|
2002-07-04 17:45:35 +00:00
|
|
|
using std::endl;
|
2003-09-05 09:01:27 +00:00
|
|
|
using std::ostream;
|
2002-07-04 17:45:35 +00:00
|
|
|
|
2002-07-04 13:54:28 +00:00
|
|
|
|
|
|
|
InsetCommandParams::InsetCommandParams()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
InsetCommandParams::InsetCommandParams(string const & n,
|
|
|
|
string const & c,
|
2004-03-05 14:49:10 +00:00
|
|
|
string const & o,
|
|
|
|
string const & s)
|
2004-04-03 08:37:12 +00:00
|
|
|
: cmdname(n), contents(c), options(o), sec_options(s),
|
2004-03-05 14:49:10 +00:00
|
|
|
preview_(false)
|
2002-07-04 13:54:28 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCommandParams::scanCommand(string const & cmd)
|
|
|
|
{
|
2004-03-05 14:49:10 +00:00
|
|
|
string tcmdname, toptions, tsecoptions, tcontents;
|
2002-07-04 13:54:28 +00:00
|
|
|
|
|
|
|
if (cmd.empty()) return;
|
2004-04-03 08:37:12 +00:00
|
|
|
|
2004-03-05 14:49:10 +00:00
|
|
|
enum { WS, CMDNAME, OPTION, SECOPTION, CONTENT } state = WS;
|
2002-07-04 13:54:28 +00:00
|
|
|
|
|
|
|
// Used to handle things like \command[foo[bar]]{foo{bar}}
|
|
|
|
int nestdepth = 0;
|
|
|
|
|
|
|
|
for (string::size_type i = 0; i < cmd.length(); ++i) {
|
2005-05-06 14:15:31 +00:00
|
|
|
char const c = cmd[i];
|
2002-07-04 13:54:28 +00:00
|
|
|
if ((state == CMDNAME && c == ' ') ||
|
|
|
|
(state == CMDNAME && c == '[') ||
|
|
|
|
(state == CMDNAME && c == '{')) {
|
|
|
|
state = WS;
|
|
|
|
}
|
|
|
|
if ((state == OPTION && c == ']') ||
|
2004-03-05 14:49:10 +00:00
|
|
|
(state == SECOPTION && c == ']') ||
|
2002-07-04 13:54:28 +00:00
|
|
|
(state == CONTENT && c == '}')) {
|
|
|
|
if (nestdepth == 0) {
|
|
|
|
state = WS;
|
|
|
|
} else {
|
|
|
|
--nestdepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((state == OPTION && c == '[') ||
|
2004-03-05 14:49:10 +00:00
|
|
|
(state == SECOPTION && c == '[') ||
|
2002-07-04 13:54:28 +00:00
|
|
|
(state == CONTENT && c == '{')) {
|
|
|
|
++nestdepth;
|
|
|
|
}
|
|
|
|
switch (state) {
|
|
|
|
case CMDNAME: tcmdname += c; break;
|
|
|
|
case OPTION: toptions += c; break;
|
2004-03-05 14:49:10 +00:00
|
|
|
case SECOPTION: tsecoptions += c; break;
|
2002-07-04 13:54:28 +00:00
|
|
|
case CONTENT: tcontents += c; break;
|
2005-05-06 14:15:31 +00:00
|
|
|
case WS: {
|
|
|
|
char const b = i? cmd[i-1]: 0;
|
2002-07-04 13:54:28 +00:00
|
|
|
if (c == '\\') {
|
|
|
|
state = CMDNAME;
|
2004-03-05 14:49:10 +00:00
|
|
|
} else if (c == '[' && b != ']') {
|
2002-07-04 13:54:28 +00:00
|
|
|
state = OPTION;
|
|
|
|
nestdepth = 0; // Just to be sure
|
2004-03-05 14:49:10 +00:00
|
|
|
} else if (c == '[' && b == ']') {
|
|
|
|
state = SECOPTION;
|
|
|
|
nestdepth = 0; // Just to be sure
|
2002-07-04 13:54:28 +00:00
|
|
|
} else if (c == '{') {
|
|
|
|
state = CONTENT;
|
|
|
|
nestdepth = 0; // Just to be sure
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-05-06 14:15:31 +00:00
|
|
|
}
|
2002-07-04 13:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't mess with this.
|
|
|
|
if (!tcmdname.empty()) setCmdName(tcmdname);
|
|
|
|
if (!toptions.empty()) setOptions(toptions);
|
2004-03-05 14:49:10 +00:00
|
|
|
if (!tsecoptions.empty()) setSecOptions(tsecoptions);
|
2002-07-04 13:54:28 +00:00
|
|
|
if (!tcontents.empty()) setContents(tcontents);
|
|
|
|
|
|
|
|
if (lyxerr.debugging(Debug::PARSER))
|
|
|
|
lyxerr << "Command <" << cmd
|
|
|
|
<< "> == <" << getCommand()
|
|
|
|
<< "> == <" << getCmdName()
|
|
|
|
<< '|' << getContents()
|
2004-04-03 08:37:12 +00:00
|
|
|
<< '|' << getOptions()
|
2004-03-05 14:49:10 +00:00
|
|
|
<< '|' << getSecOptions() << '>' << endl;
|
2002-07-04 13:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCommandParams::read(LyXLex & lex)
|
|
|
|
{
|
|
|
|
string token;
|
|
|
|
|
|
|
|
if (lex.eatLine()) {
|
|
|
|
token = lex.getString();
|
|
|
|
scanCommand(token);
|
|
|
|
} else {
|
|
|
|
lex.printError("InsetCommand: Parse error: `$$Token'");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lex.isOK()) {
|
2002-08-02 18:25:25 +00:00
|
|
|
lex.next();
|
2002-07-04 13:54:28 +00:00
|
|
|
token = lex.getString();
|
|
|
|
if (token == "\\end_inset")
|
|
|
|
break;
|
2002-08-02 18:25:25 +00:00
|
|
|
if (token == "preview") {
|
|
|
|
lex.next();
|
|
|
|
preview_ = lex.getBool();
|
|
|
|
}
|
2002-07-04 13:54:28 +00:00
|
|
|
}
|
|
|
|
if (token != "\\end_inset") {
|
|
|
|
lex.printError("Missing \\end_inset at this point. "
|
|
|
|
"Read: `$$Token'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InsetCommandParams::write(ostream & os) const
|
|
|
|
{
|
|
|
|
os << "LatexCommand " << getCommand() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const InsetCommandParams::getCommand() const
|
|
|
|
{
|
|
|
|
string s;
|
2002-11-27 10:30:28 +00:00
|
|
|
if (!getCmdName().empty()) s += '\\' + getCmdName();
|
|
|
|
if (!getOptions().empty()) s += '[' + getOptions() + ']';
|
2004-03-05 14:49:10 +00:00
|
|
|
if (!getSecOptions().empty()) {
|
|
|
|
// for cases like \command[][sec_option]{arg}
|
|
|
|
if (getOptions().empty()) s += "[]";
|
|
|
|
s += '[' + getSecOptions() + ']';
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
s += '{' + getContents() + '}';
|
2002-07-04 13:54:28 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-07-04 17:45:35 +00:00
|
|
|
|
|
|
|
bool operator==(InsetCommandParams const & o1,
|
|
|
|
InsetCommandParams const & o2)
|
|
|
|
{
|
|
|
|
return o1.getCmdName() == o2.getCmdName()
|
|
|
|
&& o1.getContents() == o2.getContents()
|
2002-08-02 18:25:25 +00:00
|
|
|
&& o1.getOptions() == o2.getOptions()
|
2004-03-05 14:49:10 +00:00
|
|
|
&& o1.getSecOptions() == o2.getSecOptions()
|
2002-08-02 18:25:25 +00:00
|
|
|
&& o1.preview() == o2.preview();
|
2002-07-04 17:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(InsetCommandParams const & o1,
|
|
|
|
InsetCommandParams const & o2)
|
|
|
|
{
|
|
|
|
return !(o1 == o2);
|
|
|
|
}
|