]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathMacroTemplate.h
Do not allow pasting backslashes in macro names
[features.git] / src / mathed / InsetMathMacroTemplate.h
1 // -*- C++ -*-
2 /**
3  * \file math_macrotemplate.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_MACROTEMPLATE_H
14 #define MATH_MACROTEMPLATE_H
15
16 #include "InsetMathNest.h"
17 #include "MacroTable.h"
18 #include "MathData.h"
19
20
21 namespace lyx {
22
23 class OutputParams;
24 class XMLStream;
25
26 /// This class contains the macro definition.
27 class InsetMathMacroTemplate : public InsetMathNest {
28 public:
29         ///
30         explicit InsetMathMacroTemplate(Buffer * buf);
31         ///
32         InsetMathMacroTemplate(Buffer * buf, docstring const & name, int numargs,
33                 int optionals, MacroType type,
34                 std::vector<MathData> const & optionalValues = std::vector<MathData>(),
35                 MathData const & def = MathData(),
36                 MathData const & display = MathData());
37         /// parses from string, returns false if failed
38         bool fromString (const docstring & str);
39         ///
40         bool editable() const override { return true; }
41         ///
42         void edit(Cursor & cur, bool front, EntryDirection entry_from) override;
43         ///
44         bool notifyCursorLeaves(Cursor const & old, Cursor & cur) override;
45         ///
46         void read(Lexer & lex) override;
47         ///
48         void write(std::ostream & os) const override;
49         ///
50         void write(TeXMathStream & os) const override;
51         /// Output LaTeX code, but assume that the macro is not defined yet
52         /// if overwriteRedefinition is true
53         int write(TeXMathStream & os, bool overwriteRedefinition) const;
54         /// Nothing happens. This is simply to suppress the default output.
55         docstring xhtml(XMLStream &, OutputParams const &) const override;
56         ///
57         int plaintext(odocstringstream &, OutputParams const &, size_t) const override;
58         ///
59         bool inheritFont() const override { return false; }
60         ///
61         docstring name() const override;
62         ///
63         void getDefaults(std::vector<docstring> & defaults) const;
64         ///
65         docstring definition() const;
66         ///
67         docstring displayDefinition() const;
68         ///
69         size_t numArgs() const;
70         ///
71         size_t numOptionals() const;
72         ///
73         bool redefinition() const { return redefinition_; }
74         ///
75         MacroType type() const { return type_; }
76
77         /// check name and possible other formal properties
78         bool validMacro() const;
79         ///
80         bool validName() const;
81         /// Remove everything from the name which makes it invalid
82         /// and return true iff it is valid.
83         bool fixNameAndCheckIfValid();
84
85         /// request "external features"
86         void validate(LaTeXFeatures &) const override;
87
88         /// decide whether its a redefinition
89         void updateToContext(MacroContext const & mc);
90
91         ///
92         void draw(PainterInfo & pi, int x, int y) const override;
93         ///
94         void metrics(MetricsInfo & mi, Dimension & dim) const override;
95         /// identifies macro templates
96         InsetMathMacroTemplate * asMacroTemplate() override { return this; }
97         /// identifies macro templates
98         InsetMathMacroTemplate const * asMacroTemplate() const override { return this; }
99         ///
100         InsetCode lyxCode() const override { return MATHMACRO_CODE; }
101         ///
102         void infoize(odocstream & os) const override;
103         ///
104         std::string contextMenuName() const override;
105         ///
106         void addToToc(DocIterator const & di, bool output_active,
107                                   UpdateType utype, TocBackend & backend) const override;
108 protected:
109         ///
110         void doDispatch(Cursor & cur, FuncRequest & cmd) override;
111         /// do we want to handle this event?
112         bool getStatus(Cursor & cur, FuncRequest const & cmd,
113                 FuncStatus & status) const override;
114
115 private:
116         friend class InsetLabelBox;
117         friend class InsetDisplayLabelBox;
118
119         ///
120         Inset * clone() const override;
121
122         /// remove #n with from<=n<=to
123         void removeArguments(Cursor & cur, DocIterator const & inset_pos,
124                 int from, int to);
125         /// shift every #n with from<=n, i.e. #n -> #(n-by)
126         void shiftArguments(size_t from, int by);
127         ///
128         void insertParameter(Cursor & cur, DocIterator const & inset_pos,
129                 int pos, bool greedy = false, bool addarg = true);
130         ///
131         void removeParameter(Cursor & cur, DocIterator const & inset_pos,
132                 int pos, bool greedy = false);
133         ///
134         void makeOptional(Cursor & cur, DocIterator const & inset_pos);
135         ///
136         void makeNonOptional(Cursor & cur, DocIterator const & inset_pos);
137         ///
138         idx_type defIdx() const { return optionals_ + 1; }
139         /// index of default value cell of optional parameter (#1 -> n=0)
140         idx_type optIdx(idx_type n) const { return n + 1; }
141         ///
142         idx_type displayIdx() const { return optionals_ + 2; }
143         ///
144         void updateLook() const;
145         /// look through the macro for #n arguments
146         int maxArgumentInDefinition() const;
147         /// add missing #n arguments up to \c maxArg
148         void insertMissingArguments(int maxArg);
149         /// change the arity
150         void changeArity(Cursor & cur, DocIterator const & inset_pos,
151                 int newNumArg);
152         /// find arguments in definition and adapt the arity accordingly
153         void commitEditChanges(Cursor & cur, DocIterator const & inset_pos);
154         /// The representation of the macro template, with some holes to edit
155         mutable MathData look_;
156         ///
157         mutable int numargs_;
158         ///
159         mutable int argsInLook_;
160         ///
161         int optionals_;
162         /// keeps the old optional default value when an
163         /// optional argument is disabled
164         std::vector<MathData> optionalValues_;
165
166         /// (re)newcommand or def
167         mutable MacroType type_;
168         /// defined before already?
169         bool redefinition_;
170         ///
171         void createLook(int args) const;
172         ///
173         mutable bool lookOutdated_;
174         /// true if in pre-calculations of metrics to get height of boxes
175         mutable bool premetrics_;
176         ///
177         mutable int labelBoxAscent_;
178         ///
179         mutable int labelBoxDescent_;
180         ///
181         bool premetrics() const { return premetrics_; }
182         ///
183         int commonLabelBoxAscent() const { return labelBoxAscent_; }
184         ///
185         int commonLabelBoxDescent() const { return labelBoxDescent_; }
186 };
187
188
189 } // namespace lyx
190
191 #endif