]> git.lyx.org Git - features.git/commitdiff
InsetListingsParams.h/cpp: Allow , in parameter string which is common in caption
authorBo Peng <bpeng@lyx.org>
Sat, 12 May 2007 15:07:18 +0000 (15:07 +0000)
committerBo Peng <bpeng@lyx.org>
Sat, 12 May 2007 15:07:18 +0000 (15:07 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18279 a592a061-630c-0410-9148-cb99ea01b6c8

src/insets/InsetListingsParams.cpp
src/insets/InsetListingsParams.h

index dac5309e92d032dae7840f8d6d24a86f66c3bebc..90a79dc8c12596b2ceeecf9417871508690c212c 100644 (file)
@@ -27,6 +27,7 @@ using std::ostream;
 using std::string;
 using std::exception;
 using lyx::support::trim;
+using lyx::support::isStrInt;
 
 namespace lyx
 {
@@ -314,7 +315,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
@@ -435,6 +436,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 +459,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;
+                       continue;
+               } else if (par[i] == ',' && braces == 0) {
                        addParam(trim(key), trim(value));
                        key = string();
                        value = string();
                        isValue = false;
-               } else if (par[i] == '=')
+                       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 +497,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 +526,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;
index 3b111a11d148aec618f2ca852c9ce1362f29a3e5..3d99a30d095f9869fc7f09e56272a0051089e48e 100644 (file)
@@ -39,6 +39,9 @@ public:
        
        /// add key=value to params_
        void addParam(std::string const & key, std::string const & value);
+
+       /// add a few parameters
+       void addParams(std::string const & par);
        
        /// set params_ with par, throw an exception if par is valid
        void setParams(std::string const & par);
@@ -74,6 +77,9 @@ private:
        /// that can be passed to listing packages.
        std::string params_;
 
+       /// keys defined in params_ 
+       std::vector<std::string> keys_;
+
        /// collapsable status
        InsetCollapsable::CollapseStatus status_;
 };