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"
37 /// Types of parameters
39 LATEX_OPTIONAL, /// normal optional argument
40 LATEX_REQUIRED, /// normal required argument
41 LYX_INTERNAL /// a parameter used internally by LyX
43 /// Special handling on output
45 HANDLING_NONE, /// no special handling
46 HANDLING_ESCAPE, /// escape special characters
47 HANDLING_LATEXIFY /// transform special characters to LaTeX macros
51 // No parameter may be named "preview", because that is a required
52 // flag for all commands.
55 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
57 docstring default_value = docstring());
59 std::string name() const { return name_; }
61 ParamType type() const { return type_; }
63 ParamHandling handling() const { return handling_; }
64 /// whether this is an optional LaTeX argument
65 bool isOptional() const;
67 bool ignore() const { return ignore_; }
69 docstring const & defaultValue() const { return default_value_; }
71 bool operator==(ParamData const &) const;
73 bool operator!=(ParamData const & rhs) const
74 { return !(*this == rhs); }
80 /// do we need special handling on latex output?
81 ParamHandling handling_;
85 docstring default_value_;
88 /// adds a new parameter
89 /// If ignore is true, then the parameter is never saved, and is always
90 /// given the default value.
91 void add(std::string const & name, ParamType type,
92 ParamHandling = HANDLING_NONE, bool ignore = false,
93 docstring default_value = docstring());
95 bool empty() const { return info_.empty(); }
97 size_t size() const { return info_.size(); }
99 typedef std::vector<ParamData>::const_iterator const_iterator;
101 const_iterator const begin() const { return info_.begin(); }
103 const_iterator const end() const { return info_.end(); }
104 /// \return true if name corresponds to a parameter of some sort.
105 /// \return false if the parameter does not exist at all of it it
106 bool hasParam(std::string const & name) const;
108 ParamData const & operator[](std::string const & name) const;
110 bool operator==(ParamInfo const &) const;
113 std::vector<ParamData> info_;
117 class InsetCommandParams {
119 /// Construct parameters for inset of type \p code.
120 explicit InsetCommandParams(InsetCode code);
121 /// Construct parameters for inset of type \p code with
122 /// command name \p cmdName.
123 explicit InsetCommandParams(InsetCode code,
124 std::string const & cmdName);
126 std::string insetType() const;
128 InsetCode code() const { return insetCode_; }
129 /// Parse the command
132 void Read(Lexer &, Buffer const *);
134 void write(std::ostream &) const;
136 void Write(std::ostream & os, Buffer const * buf) const;
137 /// Build the complete LaTeX command
138 docstring getCommand(OutputParams const &) const;
139 /// Return the command name
140 std::string const & getCmdName() const { return cmdName_; }
141 /// Set the name to \p n. This must be a known name. All parameters
142 /// are cleared except those that exist also in the new command.
143 /// What matters here is the parameter name, not position.
144 void setCmdName(std::string const & n);
145 /// FIXME Would be better removed, but is used in BufferView.cpp in
146 /// ways that make removal hard.
147 docstring getFirstNonOptParam() const;
148 /// get parameter \p name
149 /// LyX will assert if name is not a valid parameter.
150 docstring const & operator[](std::string const & name) const;
151 /// set parameter \p name
152 /// LyX will assert if name is not a valid parameter.
153 docstring & operator[](std::string const & name);
155 bool preview() const { return preview_; }
157 void preview(bool p) { preview_ = p; }
158 /// Clear the values of all parameters
161 static bool isCompatibleCommand(InsetCode code, std::string const & s);
163 ParamInfo const & info() const { return info_; }
165 docstring prepareCommand(OutputParams const & runparams,
166 docstring const & command, ParamInfo::ParamHandling handling) const;
168 std::string getDefaultCmd(InsetCode code);
169 /// checks whether we need to write an empty optional parameter
170 /// \return true if a non-empty optional parameter follows ci
171 bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
173 /// Description of all command properties
175 /// what kind of inset we're the parameters for
176 InsetCode insetCode_;
177 /// The name of this command as it appears in .lyx and .tex files
178 std::string cmdName_;
180 // if we need to allow more than one value for a parameter, this
181 // could be made a multimap. it may be that the only thing that
182 // would then need changing is operator[].
183 typedef std::map<std::string, docstring> ParamMap;
184 /// The parameters, by name.
189 friend bool operator==(InsetCommandParams const &,
190 InsetCommandParams const &);
194 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
196 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);