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