From 387ac780883f16ef8edb647c4b556d42f4cd33bf Mon Sep 17 00:00:00 2001 From: Guillaume Munch Date: Tue, 14 Feb 2017 21:16:39 +0100 Subject: [PATCH] Add option to ignore a parameter in InsetCommandParams It will not be saved to the file and will always be equal to a given default value. --- src/insets/InsetCommandParams.cpp | 26 +++++++++++++++++++++----- src/insets/InsetCommandParams.h | 17 +++++++++++++++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/insets/InsetCommandParams.cpp b/src/insets/InsetCommandParams.cpp index 5ea28ff3fc..1acb72f2f7 100644 --- a/src/insets/InsetCommandParams.cpp +++ b/src/insets/InsetCommandParams.cpp @@ -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]; } diff --git a/src/insets/InsetCommandParams.h b/src/insets/InsetCommandParams.h index 4107378487..861565a31e 100644 --- a/src/insets/InsetCommandParams.h +++ b/src/insets/InsetCommandParams.h @@ -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(); } ///