]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
Fix bug #12795
[lyx.git] / src / insets / InsetCommandParams.h
1 // -*- C++ -*-
2 /**
3  * \file InsetCommandParams.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Georg Baum
9  * \author Richard Kimberly Heck
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef INSETCOMMANDPARAMS_H
15 #define INSETCOMMANDPARAMS_H
16
17 #include "InsetCode.h"
18
19 #include "support/docstring.h"
20
21 #include <string>
22 #include <vector>
23 #include <map>
24
25
26 namespace lyx {
27
28 class Buffer;
29 class Lexer;
30 class OutputParams;
31
32 class ParamInfo {
33 public:
34         ///
35         ParamInfo() {}
36         /// Types of parameters
37         enum ParamType {
38                 LATEX_OPTIONAL,    /// normal optional argument
39                 LATEX_REQUIRED,    /// normal required argument
40                 LYX_INTERNAL       /// a parameter used internally by LyX
41         };
42         /// Special handling on output
43         enum ParamHandling {
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
49         };
50         ///
51         class ParamData {
52         // No parameter may be named "preview", because that is a required
53         // flag for all commands.
54         public:
55                 ///
56                 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
57                           bool ignore = false,
58                           docstring default_value = docstring());
59                 ///
60                 std::string name() const { return name_; }
61                 ///
62                 ParamType type() const { return type_; }
63                 ///
64                 ParamHandling handling() const { return handling_; }
65                 /// whether this is an optional LaTeX argument
66                 bool isOptional() const;
67                 ///
68                 bool ignore() const { return ignore_; }
69                 ///
70                 docstring const & defaultValue() const { return default_value_; }
71                 ///
72                 bool operator==(ParamData const &) const;
73                 ///
74                 bool operator!=(ParamData const & rhs) const
75                         { return !(*this == rhs); }
76         private:
77                 ///
78                 std::string name_;
79                 ///
80                 ParamType type_;
81                 /// do we need special handling on latex output?
82                 ParamHandling handling_;
83                 ///
84                 bool ignore_;
85                 ///
86                 docstring default_value_;
87         };
88
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());
95         ///
96         bool empty() const { return info_.empty(); }
97         ///
98         size_t size() const { return info_.size(); }
99         ///
100         typedef std::vector<ParamData>::const_iterator const_iterator;
101         ///
102         const_iterator const begin() const { return info_.begin(); }
103         ///
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;
108         ///
109         ParamData const & operator[](std::string const & name) const;
110         ///
111         bool operator==(ParamInfo const &) const;
112 private:
113         ///
114         std::vector<ParamData> info_;
115 };
116
117
118 class InsetCommandParams {
119 public:
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);
126         ///
127         std::string insetType() const;
128         ///
129         InsetCode code() const { return insetCode_; }
130         /// Parse the command
131         void read(Lexer &);
132         ///
133         void Read(Lexer &, Buffer const *);
134         ///
135         void write(std::ostream &) const;
136         ///
137         void Write(std::ostream & os, Buffer const * buf) const;
138         /// Build the complete LaTeX command
139         docstring getCommand(OutputParams const &, bool starred = false) 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         /// Determine whether a parameter is set
150         bool hasParam(std::string const & name) const;
151         /// Get the parameter \p name if it is set, \p defaultValue otherwise
152         docstring const & getParamOr(std::string const & name, docstring const & defaultValue) const;
153         /// get parameter \p name
154         /// LyX will assert if name is not a valid parameter.
155         docstring const & operator[](std::string const & name) const;
156         /// set parameter \p name
157         /// LyX will assert if name is not a valid parameter.
158         docstring & operator[](std::string const & name);
159         ///
160         bool preview() const { return preview_; }
161         ///
162         void preview(bool p) { preview_ = p; }
163         /// Clear the values of all parameters
164         void clear();
165         ///
166         static bool isCompatibleCommand(InsetCode code, std::string const & s);
167         ///
168         ParamInfo const & info() const { return info_; }
169         ///
170         docstring prepareCommand(OutputParams const & runparams,
171                 docstring const & command, ParamInfo::ParamHandling handling) const;
172 private:
173         std::string getDefaultCmd(InsetCode code);
174         /// checks whether we need to write an empty optional parameter
175         /// \return true if a non-empty optional parameter follows ci
176         bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
177
178         /// Description of all command properties
179         ParamInfo info_;
180         /// what kind of inset we're the parameters for
181         InsetCode insetCode_;
182         /// The name of this command as it appears in .lyx and .tex files
183         std::string cmdName_;
184         ///
185         // if we need to allow more than one value for a parameter, this
186         // could be made a multimap. it may be that the only thing that
187         // would then need changing is operator[].
188         typedef std::map<std::string, docstring> ParamMap;
189         /// The parameters, by name.
190         ParamMap params_;
191         ///
192         bool preview_;
193         ///
194         friend bool operator==(InsetCommandParams const &,
195                         InsetCommandParams const &);
196 };
197
198 ///
199 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
200 ///
201 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
202
203
204 } // namespace lyx
205
206 #endif