]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
Merge branch 'master' of git.lyx.org:lyx
[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 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 "OutputParams.h"
20
21 #include "support/docstring.h"
22
23 #include <string>
24 #include <vector>
25 #include <map>
26
27
28 namespace lyx {
29
30 class Lexer;
31
32 class ParamInfo {
33 public:
34         /// Types of parameters
35         enum ParamType {
36                 LATEX_OPTIONAL,    /// normal optional argument
37                 LATEX_REQUIRED,    /// normal required argument
38                 LYX_INTERNAL       /// a parameter used internally by LyX
39         };
40         /// Special handling on output
41         enum ParamHandling {
42                 HANDLING_NONE,    /// no special handling
43                 HANDLING_ESCAPE,  /// escape special characters
44                 HANDLING_LATEXIFY /// transform special characters to LaTeX macros
45         };
46         ///
47         class ParamData {
48         // No parameter may be named "preview", because that is a required
49         // flag for all commands.
50         public:
51                 ///
52                 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE);
53                 ///
54                 std::string name() const { return name_; }
55                 ///
56                 ParamType type() const { return type_; }
57                 ///
58                 ParamHandling handling() const { return handling_; }
59                 /// whether this is an optional LaTeX argument
60                 bool isOptional() const;
61                 ///
62                 bool operator==(ParamData const &) const;
63                 /// 
64                 bool operator!=(ParamData const & rhs) const
65                         { return !(*this == rhs); }
66         private:
67                 ///
68                 std::string name_;
69                 ///
70                 ParamType type_;
71                 /// do we need special handling on latex output?
72                 ParamHandling handling_;
73         };
74
75         /// adds a new parameter
76         void add(std::string const & name, ParamType type,
77                  ParamHandling = HANDLING_NONE);
78         ///
79         bool empty() const { return info_.empty(); }
80         ///
81         size_t size() const { return info_.size(); }
82         ///
83         typedef std::vector<ParamData>::const_iterator const_iterator;
84         ///
85         const_iterator const begin() const { return info_.begin(); }
86         ///
87         const_iterator const end() const { return info_.end(); }
88         /// \return true if name corresponds to a parameter of some sort.
89         /// \return false if the parameter does not exist at all of it it 
90         bool hasParam(std::string const & name) const;
91         ///
92         ParamData const & operator[](std::string const & name) const;
93         ///
94         bool operator==(ParamInfo const &) const;
95 private:
96         ///
97         std::vector<ParamData> info_;
98 };
99
100
101 class InsetCommandParams {
102 public:
103         /// Construct parameters for inset of type \p code.
104         explicit InsetCommandParams(InsetCode code);
105         /// Construct parameters for inset of type \p code with
106         /// command name \p cmdName.
107         explicit InsetCommandParams(InsetCode code,
108                         std::string const & cmdName);
109         ///
110         std::string insetType() const;
111         ///
112         InsetCode code() const { return insetCode_; }
113         ///
114         void read(Lexer &);
115         /// Parse the command
116         ///
117         void write(std::ostream &) const;
118         /// Build the complete LaTeX command
119         docstring getCommand(OutputParams const &) const;
120         /// Return the command name
121         std::string const & getCmdName() const { return cmdName_; }
122         /// Set the name to \p n. This must be a known name. All parameters
123         /// are cleared except those that exist also in the new command.
124         /// What matters here is the parameter name, not position.
125         void setCmdName(std::string const & n);
126         /// FIXME Would be better removed, but is used in BufferView.cpp in 
127         /// ways that make removal hard.
128         docstring getFirstNonOptParam() const;
129         /// get parameter \p name
130         /// LyX will assert if name is not a valid parameter.
131         docstring const & operator[](std::string const & name) const;
132         /// set parameter \p name
133         /// LyX will assert if name is not a valid parameter.
134         docstring & operator[](std::string const & name);
135         ///
136         bool preview() const { return preview_; }
137         ///
138         void preview(bool p) { preview_ = p; }
139         /// Clear the values of all parameters
140         void clear();
141         ///
142         static bool isCompatibleCommand(InsetCode code, std::string const & s);
143         /// 
144         ParamInfo const & info() const { return info_; };
145         ///
146         docstring prepareCommand(OutputParams const & runparams,
147                 docstring const & command, ParamInfo::ParamHandling handling) const;
148 private:
149         std::string getDefaultCmd(InsetCode code);
150         /// checks whether we need to write an empty optional parameter
151         /// \return true if a non-empty optional parameter follows ci
152         bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
153
154         /// Description of all command properties
155         ParamInfo info_;
156         /// what kind of inset we're the parameters for
157         InsetCode insetCode_;
158         /// The name of this command as it appears in .lyx and .tex files
159         std::string cmdName_;
160         ///
161         // if we need to allow more than one value for a parameter, this
162         // could be made a multimap. it may be that the only thing that
163         // would then need changing is operator[].
164         typedef std::map<std::string, docstring> ParamMap;
165         /// The parameters, by name.
166         ParamMap params_;
167         ///
168         bool preview_;
169         ///
170         friend bool operator==(InsetCommandParams const &,
171                         InsetCommandParams const &);
172 };
173
174 ///
175 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
176 ///
177 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
178
179
180 } // namespace lyx
181
182 #endif