]> git.lyx.org Git - lyx.git/commitdiff
Add option to ignore a parameter in InsetCommandParams
authorGuillaume Munch <gm@lyx.org>
Tue, 14 Feb 2017 20:16:39 +0000 (21:16 +0100)
committerGuillaume Munch <gm@lyx.org>
Fri, 10 Mar 2017 23:50:57 +0000 (00:50 +0100)
It will not be saved to the file and will always be equal to a given default
value.

src/insets/InsetCommandParams.cpp
src/insets/InsetCommandParams.h

index 5ea28ff3fcd4090257de9ed3f977292d73fd48c8..1acb72f2f762166ec206b76a5493fe1e8f449e39 100644 (file)
@@ -99,8 +99,10 @@ static ParamInfo const & findInfo(InsetCode code, string const & cmdName)
 /////////////////////////////////////////////////////////////////////
 
 ParamInfo::ParamData::ParamData(std::string const & s, ParamType t,
-                               ParamHandling h)
-       : name_(s), type_(t), handling_(h)
+                                ParamHandling h, bool ignore,
+                                docstring default_value)
+       : name_(s), type_(t), handling_(h), ignore_(ignore),
+         default_value_(default_value)
 {}
 
 
@@ -130,9 +132,10 @@ bool ParamInfo::hasParam(std::string const & name) const
 
 
 void ParamInfo::add(std::string const & name, ParamType type,
-                   ParamHandling handling)
-{ 
-       info_.push_back(ParamData(name, type, handling)); 
+                    ParamHandling handling, bool ignore,
+                    docstring default_value)
+{
+       info_.push_back(ParamData(name, type, handling, ignore, default_value));
 }
 
 
@@ -296,6 +299,11 @@ void InsetCommandParams::Read(Lexer & lex, Buffer const * buffer)
        }
 
        info_ = findInfo(insetCode_, cmdName_);
+
+       for (ParamInfo::ParamData const & param : info_)
+               if (param.ignore()) {
+                       params_[param.name()] = param.defaultValue();
+               }
        
        string token;
        while (lex.isOK()) {
@@ -361,6 +369,8 @@ void InsetCommandParams::Write(ostream & os, Buffer const * buffer) const
        ParamInfo::const_iterator it  = info_.begin();
        ParamInfo::const_iterator end = info_.end();
        for (; it != end; ++it) {
+               if (it->ignore())
+                       continue;
                string const & name = it->name();
                string data = to_utf8((*this)[name]);
                if (!data.empty()) {
@@ -538,6 +548,9 @@ docstring const & InsetCommandParams::operator[](string const & name) const
        ParamMap::const_iterator data = params_.find(name);
        if (data == params_.end() || data->second.empty())
                return dummy;
+       ParamInfo::ParamData const & param = info_[name];
+       if (param.ignore())
+               return param.defaultValue();
        return data->second;
 }
 
@@ -546,6 +559,9 @@ docstring & InsetCommandParams::operator[](string const & name)
 {
        LATTEST(info_.hasParam(name));
        // this will add the name in release mode
+       ParamInfo::ParamData const & param = info_[name];
+       if (param.ignore())
+               params_[name] = param.defaultValue();
        return params_[name];
 }
 
index 4107378487d0f8195454ea34860d3497888dc06c..861565a31ec9825d1d48b07ed6b83b72c94af5b3 100644 (file)
@@ -52,7 +52,9 @@ public:
        // flag for all commands.
        public:
                ///
-               ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE);
+               ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
+                         bool ignore = false,
+                         docstring default_value = docstring());
                ///
                std::string name() const { return name_; }
                ///
@@ -62,6 +64,10 @@ public:
                /// whether this is an optional LaTeX argument
                bool isOptional() const;
                ///
+               bool ignore() const { return ignore_; }
+               ///
+               docstring const & defaultValue() const { return default_value_; }
+               ///
                bool operator==(ParamData const &) const;
                /// 
                bool operator!=(ParamData const & rhs) const
@@ -73,11 +79,18 @@ public:
                ParamType type_;
                /// do we need special handling on latex output?
                ParamHandling handling_;
+               ///
+               bool ignore_;
+               ///
+               docstring default_value_;
        };
 
        /// adds a new parameter
+       /// If ignore is true, then the parameter is never saved, and is always
+       /// given the default value.
        void add(std::string const & name, ParamType type,
-                ParamHandling = HANDLING_NONE);
+                ParamHandling = HANDLING_NONE, bool ignore = false,
+                docstring default_value = docstring());
        ///
        bool empty() const { return info_.empty(); }
        ///