]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
Fix date inset except on windows (bug 9925)
[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         /// Parse the command
117         void read(Lexer &);
118         ///
119         void Read(Lexer &, Buffer const *);
120         ///
121         void write(std::ostream &) const;
122         ///
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);
141         ///
142         bool preview() const { return preview_; }
143         ///
144         void preview(bool p) { preview_ = p; }
145         /// Clear the values of all parameters
146         void clear();
147         ///
148         static bool isCompatibleCommand(InsetCode code, std::string const & s);
149         /// 
150         ParamInfo const & info() const { return info_; }
151         ///
152         docstring prepareCommand(OutputParams const & runparams,
153                 docstring const & command, ParamInfo::ParamHandling handling) const;
154 private:
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;
159
160         /// Description of all command properties
161         ParamInfo info_;
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_;
166         ///
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.
172         ParamMap params_;
173         ///
174         bool preview_;
175         ///
176         friend bool operator==(InsetCommandParams const &,
177                         InsetCommandParams const &);
178 };
179
180 ///
181 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
182 ///
183 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
184
185
186 } // namespace lyx
187
188 #endif