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
9 * \author Richard Kimberly Heck
11 * Full author contact details are available in file CREDITS.
14 #ifndef INSETCOMMANDPARAMS_H
15 #define INSETCOMMANDPARAMS_H
17 #include "InsetCode.h"
19 #include "support/docstring.h"
36 /// Types of parameters
38 LATEX_OPTIONAL, /// normal optional argument
39 LATEX_REQUIRED, /// normal required argument
40 LYX_INTERNAL /// a parameter used internally by LyX
42 /// Special handling on output
44 HANDLING_NONE = 1, /// no special handling
45 HANDLING_ESCAPE = 2, /// escape special characters
46 HANDLING_LATEXIFY = 4, /// transform special characters to LaTeX macros
47 HANDLING_LTRIM = 8, /// trim blanks on the left
48 HANDLING_INDEX_ESCAPE = 16, /// escape makeindex special chars
52 // No parameter may be named "preview", because that is a required
53 // flag for all commands.
56 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
58 docstring default_value = docstring());
60 std::string name() const { return name_; }
62 ParamType type() const { return type_; }
64 ParamHandling handling() const { return handling_; }
65 /// whether this is an optional LaTeX argument
66 bool isOptional() const;
68 bool ignore() const { return ignore_; }
70 docstring const & defaultValue() const { return default_value_; }
72 bool operator==(ParamData const &) const;
74 bool operator!=(ParamData const & rhs) const
75 { return !(*this == rhs); }
81 /// do we need special handling on latex output?
82 ParamHandling handling_;
86 docstring default_value_;
89 /// adds a new parameter
90 /// If ignoreval is true, then the parameter is never saved, and is always
91 /// given the default value.
92 void add(std::string const & name, ParamType type,
93 ParamHandling = HANDLING_NONE, bool ignoreval = false,
94 docstring default_value = docstring());
96 bool empty() const { return info_.empty(); }
98 size_t size() const { return info_.size(); }
100 typedef std::vector<ParamData>::const_iterator const_iterator;
102 const_iterator const begin() const { return info_.begin(); }
104 const_iterator const end() const { return info_.end(); }
105 /// \return true if name corresponds to a parameter of some sort.
106 /// \return false if the parameter does not exist at all of it it
107 bool hasParam(std::string const & name) const;
109 ParamData const & operator[](std::string const & name) const;
111 bool operator==(ParamInfo const &) const;
114 std::vector<ParamData> info_;
118 class InsetCommandParams {
120 /// Construct parameters for inset of type \p code.
121 explicit InsetCommandParams(InsetCode code);
122 /// Construct parameters for inset of type \p code with
123 /// command name \p cmdName.
124 explicit InsetCommandParams(InsetCode code,
125 std::string const & cmdName);
127 std::string insetType() const;
129 InsetCode code() const { return insetCode_; }
130 /// Parse the command
133 void Read(Lexer &, Buffer const *);
135 void write(std::ostream &) const;
137 void Write(std::ostream & os, Buffer const * buf) const;
138 /// Build the complete LaTeX command
139 docstring getCommand(OutputParams const &) const;
140 /// Return the command name
141 std::string const & getCmdName() const { return cmdName_; }
142 /// Set the name to \p n. This must be a known name. All parameters
143 /// are cleared except those that exist also in the new command.
144 /// What matters here is the parameter name, not position.
145 void setCmdName(std::string const & n);
146 /// FIXME Would be better removed, but is used in BufferView.cpp in
147 /// ways that make removal hard.
148 docstring getFirstNonOptParam() const;
149 /// get parameter \p name
150 /// LyX will assert if name is not a valid parameter.
151 docstring const & operator[](std::string const & name) const;
152 /// set parameter \p name
153 /// LyX will assert if name is not a valid parameter.
154 docstring & operator[](std::string const & name);
156 bool preview() const { return preview_; }
158 void preview(bool p) { preview_ = p; }
159 /// Clear the values of all parameters
162 static bool isCompatibleCommand(InsetCode code, std::string const & s);
164 ParamInfo const & info() const { return info_; }
166 docstring prepareCommand(OutputParams const & runparams,
167 docstring const & command, ParamInfo::ParamHandling handling) const;
169 std::string getDefaultCmd(InsetCode code);
170 /// checks whether we need to write an empty optional parameter
171 /// \return true if a non-empty optional parameter follows ci
172 bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
174 /// Description of all command properties
176 /// what kind of inset we're the parameters for
177 InsetCode insetCode_;
178 /// The name of this command as it appears in .lyx and .tex files
179 std::string cmdName_;
181 // if we need to allow more than one value for a parameter, this
182 // could be made a multimap. it may be that the only thing that
183 // would then need changing is operator[].
184 typedef std::map<std::string, docstring> ParamMap;
185 /// The parameters, by name.
190 friend bool operator==(InsetCommandParams const &,
191 InsetCommandParams const &);
195 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
197 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);