]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
3a69a7895d1387c06e249f3b52d19c3d22607a32
[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 #include "support/docstring.h"
19
20 #include <iosfwd>
21 #include <list>
22 #include <string>
23 #include <vector>
24
25
26 namespace lyx {
27
28 class Lexer;
29
30 // No parameter may be named "preview", because that is a required
31 // flag for all commands.
32 struct CommandInfo {
33         /// Number of parameters
34         size_t n;
35         /// Parameter names. paramnames[n] must be "".
36         char const * const * paramnames;
37         /// Tells whether a parameter is optional
38         bool const * optional;
39 };
40
41 ///
42 struct ICPInfo {
43         ///
44         ICPInfo(std::string const & s, bool b);
45         /// Parameter name.
46         std::string paramName;
47         /// Whether it is optional.
48         bool optional;
49 };
50 ///
51 typedef std::list<ICPInfo> PList;
52 ///
53 class ICPList {
54         public:
55                 ///
56                 PList::const_iterator begin() { return plist_.begin(); }
57                 ///
58                 PList::const_iterator end()   { return plist_.end(); }
59                 ///
60                 void clear() { plist_.clear(); }
61                 ///
62                 void addParam(std::string const & s, bool b = false);
63                 ///
64                 bool hasParam(std::string const & s);
65         private:
66                 ///
67                 PList plist_;
68         
69 };
70
71
72 class InsetCommandParams {
73 public:
74         /// Construct parameters for inset of type \p code.
75         explicit InsetCommandParams(InsetCode code);
76         /// Construct parameters for inset of type \p code with
77         /// command name \p cmdName.
78         explicit InsetCommandParams(InsetCode code,
79                         std::string const & cmdName);
80         ///
81         std::string insetType() const { return insetName(insetCode_); }
82         ///
83         InsetCode code() const { return insetCode_; }
84         ///
85         void read(Lexer &);
86         /// Parse the command
87         ///
88         void write(std::ostream &) const;
89         /// Build the complete LaTeX command
90         docstring const getCommand() const;
91         /// Return the command name
92         std::string const & getCmdName() const { return cmdName_; }
93         /// Set the name to \p n. This must be a known name. All parameters
94         /// are cleared except those that exist also in the new command.
95         /// What matters here is the parameter name, not position.
96         void setCmdName(std::string const & n);
97         /// FIXME Would be better removed, but is used in BufferView.cpp in 
98         /// ways that make removal hard.
99         docstring const getFirstNonOptParam() const;
100         /// get parameter \p name
101         docstring const & operator[](std::string const & name) const;
102         /// set parameter \p name
103         docstring & operator[](std::string const & name);
104         ///
105         bool preview() const { return preview_; }
106         ///
107         void preview(bool p) { preview_ = p; }
108         /// Clear the values of all parameters
109         void clear();
110
111 private:
112         ///
113         /// Get information for inset type \p code.
114         /// Returns 0 if the inset is not known.
115         static CommandInfo const * findInfo(InsetCode code);
116         /// Get information for \p code and command \p cmdName.
117         /// Returns 0 if the combination is not known.
118         /// Don't call this without first making sure the command name is
119         /// acceptable to the inset.
120         static CommandInfo const * findInfo(InsetCode code,
121                                             std::string const & cmdName);
122         ///
123         static bool isCompatibleCommand(InsetCode code, std::string const & s);
124         ///
125         std::string getDefaultCmd(InsetCode);
126         /// Description of all command properties
127         CommandInfo const * info_;
128         /// what kind of inset we're the parameters for
129         InsetCode insetCode_;
130         /// The name of this command as it appears in .lyx and .tex files
131         std::string cmdName_;
132         ///
133         typedef std::vector<docstring> ParamVector;
134         /// The parameters (both optional and required ones). The order is
135         /// the same that is required for LaTeX output. The size of params_
136         /// is always info_->n.
137         ParamVector params_;
138         ///
139         bool preview_;
140         ///
141         friend bool operator==(InsetCommandParams const &,
142                         InsetCommandParams const &);
143 };
144
145
146 ///
147 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
148
149 ///
150 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
151
152
153 } // namespace lyx
154
155 #endif