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 = 1, /// no special handling
46 HANDLING_ESCAPE = 2, /// escape special characters
47 HANDLING_LATEXIFY = 4, /// transform special characters to LaTeX macros
48 HANDLING_LTRIM = 8, /// trim blanks on the left
49 HANDLING_INDEX_ESCAPE = 16, /// escape makeindex special chars
53 // No parameter may be named "preview", because that is a required
54 // flag for all commands.
57 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
59 docstring default_value = docstring());
61 std::string name() const { return name_; }
63 ParamType type() const { return type_; }
65 ParamHandling handling() const { return handling_; }
66 /// whether this is an optional LaTeX argument
67 bool isOptional() const;
69 bool ignore() const { return ignore_; }
71 docstring const & defaultValue() const { return default_value_; }
73 bool operator==(ParamData const &) const;
75 bool operator!=(ParamData const & rhs) const
76 { return !(*this == rhs); }
82 /// do we need special handling on latex output?
83 ParamHandling handling_;
87 docstring default_value_;
90 /// adds a new parameter
91 /// If ignoreval is true, then the parameter is never saved, and is always
92 /// given the default value.
93 void add(std::string const & name, ParamType type,
94 ParamHandling = HANDLING_NONE, bool ignoreval = false,
95 docstring default_value = docstring());
97 bool empty() const { return info_.empty(); }
99 size_t size() const { return info_.size(); }
101 typedef std::vector<ParamData>::const_iterator const_iterator;
103 const_iterator const begin() const { return info_.begin(); }
105 const_iterator const end() const { return info_.end(); }
106 /// \return true if name corresponds to a parameter of some sort.
107 /// \return false if the parameter does not exist at all of it it
108 bool hasParam(std::string const & name) const;
110 ParamData const & operator[](std::string const & name) const;
112 bool operator==(ParamInfo const &) const;
115 std::vector<ParamData> info_;
119 class InsetCommandParams {
121 /// Construct parameters for inset of type \p code.
122 explicit InsetCommandParams(InsetCode code);
123 /// Construct parameters for inset of type \p code with
124 /// command name \p cmdName.
125 explicit InsetCommandParams(InsetCode code,
126 std::string const & cmdName);
128 std::string insetType() const;
130 InsetCode code() const { return insetCode_; }
131 /// Parse the command
134 void Read(Lexer &, Buffer const *);
136 void write(std::ostream &) const;
138 void Write(std::ostream & os, Buffer const * buf) const;
139 /// Build the complete LaTeX command
140 docstring getCommand(OutputParams const &) const;
141 /// Return the command name
142 std::string const & getCmdName() const { return cmdName_; }
143 /// Set the name to \p n. This must be a known name. All parameters
144 /// are cleared except those that exist also in the new command.
145 /// What matters here is the parameter name, not position.
146 void setCmdName(std::string const & n);
147 /// FIXME Would be better removed, but is used in BufferView.cpp in
148 /// ways that make removal hard.
149 docstring getFirstNonOptParam() const;
150 /// get parameter \p name
151 /// LyX will assert if name is not a valid parameter.
152 docstring const & operator[](std::string const & name) const;
153 /// set parameter \p name
154 /// LyX will assert if name is not a valid parameter.
155 docstring & operator[](std::string const & name);
157 bool preview() const { return preview_; }
159 void preview(bool p) { preview_ = p; }
160 /// Clear the values of all parameters
163 static bool isCompatibleCommand(InsetCode code, std::string const & s);
165 ParamInfo const & info() const { return info_; }
167 docstring prepareCommand(OutputParams const & runparams,
168 docstring const & command, ParamInfo::ParamHandling handling) const;
170 std::string getDefaultCmd(InsetCode code);
171 /// checks whether we need to write an empty optional parameter
172 /// \return true if a non-empty optional parameter follows ci
173 bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
175 /// Description of all command properties
177 /// what kind of inset we're the parameters for
178 InsetCode insetCode_;
179 /// The name of this command as it appears in .lyx and .tex files
180 std::string cmdName_;
182 // if we need to allow more than one value for a parameter, this
183 // could be made a multimap. it may be that the only thing that
184 // would then need changing is operator[].
185 typedef std::map<std::string, docstring> ParamMap;
186 /// The parameters, by name.
191 friend bool operator==(InsetCommandParams const &,
192 InsetCommandParams const &);
196 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
198 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);