X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FInsetCommandParams.h;h=861565a31ec9825d1d48b07ed6b83b72c94af5b3;hb=f365a650686cc27487f686a7803968df1f24e0b8;hp=f02d2a6ebd195a17bc8e9529e5c55da5b32910f0;hpb=0787ade6c07edda44559352ce2080e6f27fd15a7;p=lyx.git diff --git a/src/insets/InsetCommandParams.h b/src/insets/InsetCommandParams.h index f02d2a6ebd..861565a31e 100644 --- a/src/insets/InsetCommandParams.h +++ b/src/insets/InsetCommandParams.h @@ -6,6 +6,7 @@ * * \author Angus Leeming * \author Georg Baum + * \author Richard Heck * * Full author contact details are available in file CREDITS. */ @@ -13,53 +14,142 @@ #ifndef INSETCOMMANDPARAMS_H #define INSETCOMMANDPARAMS_H +#include "InsetCode.h" + +#include "OutputParams.h" + #include "support/docstring.h" -#include +#include #include +#include namespace lyx { class Lexer; +class Buffer; + +class ParamInfo { +public: + /// + ParamInfo() {} + /// Types of parameters + enum ParamType { + LATEX_OPTIONAL, /// normal optional argument + LATEX_REQUIRED, /// normal required argument + LYX_INTERNAL /// a parameter used internally by LyX + }; + /// Special handling on output + enum ParamHandling { + HANDLING_NONE, /// no special handling + HANDLING_ESCAPE, /// escape special characters + HANDLING_LATEXIFY /// transform special characters to LaTeX macros + }; + /// + class ParamData { + // No parameter may be named "preview", because that is a required + // flag for all commands. + public: + /// + ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE, + bool ignore = false, + docstring default_value = docstring()); + /// + std::string name() const { return name_; } + /// + ParamType type() const { return type_; } + /// + ParamHandling handling() const { return handling_; } + /// 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 + { return !(*this == rhs); } + private: + /// + std::string name_; + /// + 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, bool ignore = false, + docstring default_value = docstring()); + /// + bool empty() const { return info_.empty(); } + /// + size_t size() const { return info_.size(); } + /// + typedef std::vector::const_iterator const_iterator; + /// + const_iterator const begin() const { return info_.begin(); } + /// + const_iterator const end() const { return info_.end(); } + /// \return true if name corresponds to a parameter of some sort. + /// \return false if the parameter does not exist at all of it it + bool hasParam(std::string const & name) const; + /// + ParamData const & operator[](std::string const & name) const; + /// + bool operator==(ParamInfo const &) const; +private: + /// + std::vector info_; +}; + class InsetCommandParams { public: - /// Construct parameters for inset \p insetType, using - /// \p insetType as default for \p cmdName_. - explicit InsetCommandParams(std::string const & insetType); - /// Construct parameters for inset \p insetType with + /// Construct parameters for inset of type \p code. + explicit InsetCommandParams(InsetCode code); + /// Construct parameters for inset of type \p code with /// command name \p cmdName. - explicit InsetCommandParams(std::string const & insetType, + explicit InsetCommandParams(InsetCode code, std::string const & cmdName); /// - std::string insetType() { return insetType_; } + std::string insetType() const; /// - void read(Lexer &); + InsetCode code() const { return insetCode_; } /// Parse the command - /// FIXME remove - void scanCommand(std::string const &); + void read(Lexer &); + /// + void Read(Lexer &, Buffer const *); /// void write(std::ostream &) const; + /// + void Write(std::ostream & os, Buffer const * buf) const; /// Build the complete LaTeX command - docstring const getCommand() const; + docstring getCommand(OutputParams const &) const; /// Return the command name std::string const & getCmdName() const { return cmdName_; } - /// this is used by listings package. - std::string const getOptions() const; - /// FIXME remove - std::string const getContents() const; /// Set the name to \p n. This must be a known name. All parameters /// are cleared except those that exist also in the new command. /// What matters here is the parameter name, not position. void setCmdName(std::string const & n); - /// this is used by the listings package - void setOptions(std::string const &); - /// FIXME remove - void setContents(std::string const &); + /// FIXME Would be better removed, but is used in BufferView.cpp in + /// ways that make removal hard. + docstring getFirstNonOptParam() const; /// get parameter \p name + /// LyX will assert if name is not a valid parameter. docstring const & operator[](std::string const & name) const; /// set parameter \p name + /// LyX will assert if name is not a valid parameter. docstring & operator[](std::string const & name); /// bool preview() const { return preview_; } @@ -67,44 +157,32 @@ public: void preview(bool p) { preview_ = p; } /// Clear the values of all parameters void clear(); - + /// + static bool isCompatibleCommand(InsetCode code, std::string const & s); + /// + ParamInfo const & info() const { return info_; } + /// + docstring prepareCommand(OutputParams const & runparams, + docstring const & command, ParamInfo::ParamHandling handling) const; private: - /// FIXME remove - std::string const getSecOptions() const; - /// FIXME remove - void setSecOptions(std::string const &); - /// - struct CommandInfo { - /// Number of parameters - size_t n; - /// Parameter names. paramnames[n] must be "". - char const * const * paramnames; - /// Tells whether a parameter is optional - bool const * optional; - }; - /// Get information for inset type \p insetType. - /// Returns 0 if the inset is not known. - static CommandInfo const * findInfo(std::string const & insetType); - /// Get information for \p insetType and command \p cmdName. - /// Returns 0 if the combination is not known. - /// Don't call this without first making sure the command name is - /// acceptable to the inset. - static CommandInfo const * findInfo(std::string const & insetType, - std::string const & cmdName); - /// - std::string getDefaultCmd(std::string insetType); + std::string getDefaultCmd(InsetCode code); + /// checks whether we need to write an empty optional parameter + /// \return true if a non-empty optional parameter follows ci + bool writeEmptyOptional(ParamInfo::const_iterator ci) const; + /// Description of all command properties - CommandInfo const * info_; + ParamInfo info_; /// what kind of inset we're the parameters for - std::string insetType_; + InsetCode insetCode_; /// The name of this command as it appears in .lyx and .tex files std::string cmdName_; /// - typedef std::vector ParamVector; - /// The parameters (both optional and required ones). The order is - /// the same that is required for LaTeX output. The size of params_ - /// is always info_->n. - ParamVector params_; + // if we need to allow more than one value for a parameter, this + // could be made a multimap. it may be that the only thing that + // would then need changing is operator[]. + typedef std::map ParamMap; + /// The parameters, by name. + ParamMap params_; /// bool preview_; /// @@ -112,10 +190,8 @@ private: InsetCommandParams const &); }; - /// bool operator==(InsetCommandParams const &, InsetCommandParams const &); - /// bool operator!=(InsetCommandParams const &, InsetCommandParams const &);