3 * \file InsetCommandParams.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
14 #ifndef INSETCOMMANDPARAMS_H
15 #define INSETCOMMANDPARAMS_H
17 #include "InsetCode.h"
19 #include "OutputParams.h"
21 #include "support/docstring.h"
34 /// Types of parameters
36 LATEX_OPTIONAL, /// normal optional argument
37 LATEX_REQUIRED, /// normal required argument
38 LYX_INTERNAL /// a parameter used internally by LyX
40 /// Special handling on output
42 HANDLING_NONE, /// no special handling
43 HANDLING_ESCAPE, /// escape special characters
44 HANDLING_LATEXIFY /// transform special characters to LaTeX macros
48 // No parameter may be named "preview", because that is a required
49 // flag for all commands.
52 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE);
54 std::string name() const { return name_; }
56 ParamType type() const { return type_; }
58 ParamHandling handling() const { return handling_; }
59 /// whether this is an optional LaTeX argument
60 bool isOptional() const;
62 bool operator==(ParamData const &) const;
64 bool operator!=(ParamData const & rhs) const
65 { return !(*this == rhs); }
71 /// do we need special handling on latex output?
72 ParamHandling handling_;
75 /// adds a new parameter
76 void add(std::string const & name, ParamType type,
77 ParamHandling = HANDLING_NONE);
79 bool empty() const { return info_.empty(); }
81 size_t size() const { return info_.size(); }
83 typedef std::vector<ParamData>::const_iterator const_iterator;
85 const_iterator const begin() const { return info_.begin(); }
87 const_iterator const end() const { return info_.end(); }
88 /// \return true if name corresponds to a parameter of some sort.
89 /// \return false if the parameter does not exist at all of it it
90 bool hasParam(std::string const & name) const;
92 ParamData const & operator[](std::string const & name) const;
94 bool operator==(ParamInfo const &) const;
97 std::vector<ParamData> info_;
101 class InsetCommandParams {
103 /// Construct parameters for inset of type \p code.
104 explicit InsetCommandParams(InsetCode code);
105 /// Construct parameters for inset of type \p code with
106 /// command name \p cmdName.
107 explicit InsetCommandParams(InsetCode code,
108 std::string const & cmdName);
110 std::string insetType() const;
112 InsetCode code() const { return insetCode_; }
115 /// Parse the command
117 void write(std::ostream &) const;
118 /// Build the complete LaTeX command
119 docstring getCommand(OutputParams const &) const;
120 /// Return the command name
121 std::string const & getCmdName() const { return cmdName_; }
122 /// Set the name to \p n. This must be a known name. All parameters
123 /// are cleared except those that exist also in the new command.
124 /// What matters here is the parameter name, not position.
125 void setCmdName(std::string const & n);
126 /// FIXME Would be better removed, but is used in BufferView.cpp in
127 /// ways that make removal hard.
128 docstring getFirstNonOptParam() const;
129 /// get parameter \p name
130 /// LyX will assert if name is not a valid parameter.
131 docstring const & operator[](std::string const & name) const;
132 /// set parameter \p name
133 /// LyX will assert if name is not a valid parameter.
134 docstring & operator[](std::string const & name);
136 bool preview() const { return preview_; }
138 void preview(bool p) { preview_ = p; }
139 /// Clear the values of all parameters
142 static bool isCompatibleCommand(InsetCode code, std::string const & s);
144 ParamInfo const & info() const { return info_; };
146 docstring prepareCommand(OutputParams const & runparams,
147 docstring const & command, ParamInfo::ParamHandling handling) const;
149 std::string getDefaultCmd(InsetCode code);
150 /// checks whether we need to write an empty optional parameter
151 /// \return true if a non-empty optional parameter follows ci
152 bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
154 /// Description of all command properties
156 /// what kind of inset we're the parameters for
157 InsetCode insetCode_;
158 /// The name of this command as it appears in .lyx and .tex files
159 std::string cmdName_;
161 // if we need to allow more than one value for a parameter, this
162 // could be made a multimap. it may be that the only thing that
163 // would then need changing is operator[].
164 typedef std::map<std::string, docstring> ParamMap;
165 /// The parameters, by name.
170 friend bool operator==(InsetCommandParams const &,
171 InsetCommandParams const &);
175 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
177 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);