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 std::string name() const { return name_; }
59 ParamType type() const { return type_; }
61 ParamHandling handling() const { return handling_; }
62 /// whether this is an optional LaTeX argument
63 bool isOptional() const;
65 bool operator==(ParamData const &) const;
67 bool operator!=(ParamData const & rhs) const
68 { return !(*this == rhs); }
74 /// do we need special handling on latex output?
75 ParamHandling handling_;
78 /// adds a new parameter
79 void add(std::string const & name, ParamType type,
80 ParamHandling = HANDLING_NONE);
82 bool empty() const { return info_.empty(); }
84 size_t size() const { return info_.size(); }
86 typedef std::vector<ParamData>::const_iterator const_iterator;
88 const_iterator const begin() const { return info_.begin(); }
90 const_iterator const end() const { return info_.end(); }
91 /// \return true if name corresponds to a parameter of some sort.
92 /// \return false if the parameter does not exist at all of it it
93 bool hasParam(std::string const & name) const;
95 ParamData const & operator[](std::string const & name) const;
97 bool operator==(ParamInfo const &) const;
100 std::vector<ParamData> info_;
104 class InsetCommandParams {
106 /// Construct parameters for inset of type \p code.
107 explicit InsetCommandParams(InsetCode code);
108 /// Construct parameters for inset of type \p code with
109 /// command name \p cmdName.
110 explicit InsetCommandParams(InsetCode code,
111 std::string const & cmdName);
113 std::string insetType() const;
115 InsetCode code() const { return insetCode_; }
116 /// Parse the command
119 void Read(Lexer &, Buffer const *);
121 void write(std::ostream &) const;
123 void Write(std::ostream & os, Buffer const * buf) const;
124 /// Build the complete LaTeX command
125 docstring getCommand(OutputParams const &) const;
126 /// Return the command name
127 std::string const & getCmdName() const { return cmdName_; }
128 /// Set the name to \p n. This must be a known name. All parameters
129 /// are cleared except those that exist also in the new command.
130 /// What matters here is the parameter name, not position.
131 void setCmdName(std::string const & n);
132 /// FIXME Would be better removed, but is used in BufferView.cpp in
133 /// ways that make removal hard.
134 docstring getFirstNonOptParam() const;
135 /// get parameter \p name
136 /// LyX will assert if name is not a valid parameter.
137 docstring const & operator[](std::string const & name) const;
138 /// set parameter \p name
139 /// LyX will assert if name is not a valid parameter.
140 docstring & operator[](std::string const & name);
142 bool preview() const { return preview_; }
144 void preview(bool p) { preview_ = p; }
145 /// Clear the values of all parameters
148 static bool isCompatibleCommand(InsetCode code, std::string const & s);
150 ParamInfo const & info() const { return info_; }
152 docstring prepareCommand(OutputParams const & runparams,
153 docstring const & command, ParamInfo::ParamHandling handling) const;
155 std::string getDefaultCmd(InsetCode code);
156 /// checks whether we need to write an empty optional parameter
157 /// \return true if a non-empty optional parameter follows ci
158 bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
160 /// Description of all command properties
162 /// what kind of inset we're the parameters for
163 InsetCode insetCode_;
164 /// The name of this command as it appears in .lyx and .tex files
165 std::string cmdName_;
167 // if we need to allow more than one value for a parameter, this
168 // could be made a multimap. it may be that the only thing that
169 // would then need changing is operator[].
170 typedef std::map<std::string, docstring> ParamMap;
171 /// The parameters, by name.
176 friend bool operator==(InsetCommandParams const &,
177 InsetCommandParams const &);
181 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
183 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);