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