using std::string;
using std::exception;
using lyx::support::trim;
+using lyx::support::isStrInt;
+using lyx::support::prefixIs;
+using lyx::support::suffixIs;
+using lyx::support::getVectorFromString;
namespace lyx
{
enum param_type {
- ALL,
- TRUEFALSE,
- INTEGER,
- LENGTH,
- ONEOF,
- SUBSETOF,
+ ALL, // accept all
+ TRUEFALSE, // accept 'true' or 'false'
+ INTEGER, // accept an integer
+ LENGTH, // accept an latex length
+ ONEOF, // accept one of a few values
+ SUBSETOF, // accept a string composed of given characters
};
{ "name", "", false, ALL, "", "" },
{ "thelstnumber", "", false, ALL, "", "" },
{ "title", "", false, ALL, "", "" },
- { "caption", "", false, ALL, "", "" },
- { "label", "", false, ALL, "", "" },
+ // this option is not handled in the parameter box
+ { "caption", "", false, ALL, "", "This parameter should not be entered here. "
+ "Please use caption editbox (Include dialog) or insert->caption (listings inset)" },
+ // this option is not handled in the parameter box
+ { "label", "", false, ALL, "", "This parameter should not be entered here."
+ "Please use label editbox (Include dialog) or insert->caption (listings inset)"},
{ "nolol", "", false, TRUEFALSE, "", "" },
{ "captionpos", "", false, SUBSETOF, "tb", "" },
{ "abovecaptionskip", "", false, LENGTH, "", "" },
return;
}
case INTEGER: {
- if (par.empty() && !info->onoff) {
+ if (!isStrInt(par)) {
if (info->hint != "")
throw invalidParam(info->hint);
else
InsetListingsParams::InsetListingsParams() :
- inline_(false), status_(InsetCollapsable::Open), params_()
+ inline_(false), params_(), keys_(0), status_(InsetCollapsable::Open)
{
}
InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
- : inline_(in), status_(s)
+ : inline_(in), params_(), keys_(0), status_(s)
{
// this will activate parameter validation.
fromEncodedString(par);
return;
// exception may be thown.
parValidator(key.c_str()).validate(value);
+ // duplicate parameters!
+ if (find(keys_.begin(), keys_.end(), key) != keys_.end())
+ throw invalidParam("Parameter " + key + " has already been defined");
+ else
+ keys_.push_back(key);
if (!params_.empty())
params_ += ',';
if (value.empty())
}
-void InsetListingsParams::setParams(string const & par)
+void InsetListingsParams::addParams(string const & par)
{
string key;
string value;
bool isValue = false;
- params_.clear();
+ int braces = 0;
for (size_t i = 0; i < par.size(); ++i) {
// end of par
- if (par[i] == '\n' || par[i] == ',') {
+ if (par[i] == '\n') {
addParam(trim(key), trim(value));
key = string();
value = string();
isValue = false;
- } else if (par[i] == '=')
+ continue;
+ } else if (par[i] == ',' && braces == 0) {
+ addParam(trim(key), trim(value));
+ key = string();
+ value = string();
+ isValue = false;
+ continue;
+ } else if (par[i] == '=' && braces == 0) {
isValue = true;
- else if (isValue)
+ continue;
+ } else if (par[i] == '{' && par[i - 1] == '=')
+ braces ++;
+ else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
+ braces --;
+
+ if (isValue)
value += par[i];
else
key += par[i];
}
+void InsetListingsParams::setParams(string const & par)
+{
+ params_.clear();
+ keys_.clear();
+ addParams(par);
+}
+
+
string InsetListingsParams::encodedString() const
{
// Encode string!
// , might be used as regular parameter option so
// the prcess might be more complicated than what I am doing here
string opt;
+ int braces = 0;
for (size_t i = 0; i < params_.size(); ++i)
- if (params_[i] == ',') {
+ if (params_[i] == ',' && braces == 0) {
if (keepComma)
opt += ",\n";
else
opt += "\n";
+ } else if (params_[i] == '{' && params_[i - 1] == '=') {
+ braces ++;
+ opt += params_[i];
+ } else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
+ braces --;
+ opt += params_[i];
} else
opt += params_[i];
return opt;
}
+bool InsetListingsParams::isFloat() const
+{
+ return find(keys_.begin(), keys_.end(), "float") != keys_.end();
+}
+
+
+string InsetListingsParams::getParamValue(string const & param) const
+{
+ // is this parameter defined?
+ if (find(keys_.begin(), keys_.end(), param) == keys_.end())
+ return string();
+ // if so, search for it
+ vector<string> pars = getVectorFromString(separatedParams(), "\n");
+ for (vector<string>::iterator it = pars.begin(); it != pars.end(); ++it)
+ if (prefixIs(*it, param + "=")) {
+ string par = it->substr(param.size() + 1);
+ if (prefixIs(par, "{") && suffixIs(par, "}"))
+ return par.substr(1, par.size() - 2);
+ else
+ return par;
+ }
+ // if param= is not found, should be something like float, return ""
+ return string();
+}
+
} // namespace lyx