]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListingsParams.cpp
Remove color dependency of framed note, fix bug 3598
[lyx.git] / src / insets / InsetListingsParams.cpp
index ef0bcae38043c740c6fe629e2e9d5000de1910de..473538475ce773b5a2d77170e136f497383425c1 100644 (file)
@@ -27,17 +27,21 @@ using std::ostream;
 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
 };
 
 
@@ -147,8 +151,12 @@ listings_param_info const listings_param_table[] = {
        { "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, "", "" },
@@ -156,7 +164,7 @@ listings_param_info const listings_param_table[] = {
        { "linewidth", "", false, LENGTH, "", "" },
        { "xleftmargin", "", false, LENGTH, "", "" },
        { "xrightmargin", "", false, LENGTH, "", "" },
-       { "resetmargin", "", false, TRUEFALSE, "", "" },
+       { "resetmargins", "", false, TRUEFALSE, "", "" },
        { "breaklines", "", false, TRUEFALSE, "", "" },
        { "prebreak", "", false, ALL, "", "" },
        { "postbreak", "", false, ALL, "", "" },
@@ -314,7 +322,7 @@ void parValidator::validate(std::string const & par) const
                return;
        }
        case INTEGER: {
-               if (par.empty() && !info->onoff) {
+               if (!isStrInt(par)) {
                        if (info->hint != "")
                                throw invalidParam(info->hint);
                        else
@@ -393,13 +401,13 @@ void parValidator::validate(std::string const & par) const
 
 
 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);
@@ -435,6 +443,11 @@ void InsetListingsParams::addParam(string const & key, string const & value)
                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())
@@ -453,22 +466,35 @@ void InsetListingsParams::addParam(string const & key, string const & value)
 }
 
 
-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];
@@ -478,6 +504,14 @@ void InsetListingsParams::setParams(string const & par)
 }
 
 
+void InsetListingsParams::setParams(string const & par)
+{
+       params_.clear();
+       keys_.clear();
+       addParams(par);
+}
+
+
 string InsetListingsParams::encodedString() const
 {
        // Encode string!
@@ -499,12 +533,19 @@ string InsetListingsParams::separatedParams(bool keepComma) const
        // , 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;
@@ -519,5 +560,30 @@ void InsetListingsParams::fromEncodedString(string const & in)
 }
 
 
+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