3 * \file InsetMathMacro.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
10 * Full author contact details are available in file CREDITS.
16 #include "InsetMathNest.h"
17 #include "MacroTable.h"
24 /// This class contains the data for a macro.
25 class InsetMathMacro : public InsetMathNest {
27 /// A macro can be built from an existing template
28 InsetMathMacro(Buffer * buf, docstring const & name);
30 InsetMathMacro(InsetMathMacro const &);
32 InsetMathMacro & operator=(InsetMathMacro const &);
36 InsetMathMacro * asMacro() override { return this; }
38 InsetMathMacro const * asMacro() const override { return this; }
40 marker_type marker(BufferView const *) const override;
41 /// If the macro is in normal edit mode, dissolve its contents in
42 /// the row. Otherwise, just insert the inset.
43 bool addToMathRow(MathRow &, MetricsInfo & mi) const override;
45 /// Whether the inset allows \(no)limits
46 bool allowsLimitsChange() const;
47 /// The default limits value
48 Limits defaultLimits(bool display) const override;
49 /// whether the inset has limit-like sub/superscript
50 Limits limits() const override;
51 /// sets types of sub/superscripts
52 void limits(Limits lim) override;
55 void beforeMetrics() const override;
57 void afterMetrics() const override;
59 void beforeDraw(PainterInfo const &) const override;
61 void afterDraw(PainterInfo const &) const override;
64 void metrics(MetricsInfo & mi, Dimension & dim) const override;
65 /// was the macro in edit mode when computing metrics?
66 bool editMetrics(BufferView const * bv) const;
68 void draw(PainterInfo & pi, int x, int y) const override;
70 int kerning(BufferView const * bv) const override;
71 /// get cursor position
72 void cursorPos(BufferView const & bv, CursorSlice const & sl,
73 bool boundary, int & x, int & y) const override;
75 void edit(Cursor & cur, bool front, EntryDirection entry_from) override;
77 Inset * editXY(Cursor & cur, int x, int y) override;
79 /// target pos when we enter the inset while moving forward
80 bool idxFirst(Cursor &) const override;
81 /// target pos when we enter the inset while moving backwards
82 bool idxLast(Cursor &) const override;
85 bool notifyCursorLeaves(Cursor const & old, Cursor & cur) override;
87 /// Remove cell (starting from 0)
88 void removeArgument(pos_type pos);
89 /// Insert empty cell (starting from 0)
90 void insertArgument(pos_type pos);
93 void validate(LaTeXFeatures &) const override;
95 mode_type currentMode() const override;
97 /// Assumes that macros are up-to-date
98 void write(TeXMathStream & os) const override;
100 void normalize(NormalStream & os) const override;
102 void maple(MapleStream &) const override;
104 void maxima(MaximaStream &) const override;
106 void mathematica(MathematicaStream &) const override;
108 void mathmlize(MathMLStream &) const override;
110 void htmlize(HtmlStream &) const override;
112 void octave(OctaveStream &) const override;
114 void infoize(odocstream &) const override;
116 void infoize2(odocstream &) const override;
118 /// fold the macro in the next metrics call
119 void fold(Cursor & cur);
120 /// unfold the macro in the next metrics call
121 void unfold(Cursor & cur);
122 /// will it be folded or unfolded in the next metric call?
127 DISPLAY_INTERACTIVE_INIT,
133 DisplayMode displayMode() const;
136 bool extraBraces() const override;
139 docstring name() const override;
140 /// FIXME: Often dangling.
141 MacroData const * macro() const;
143 docstring macroName() const;
144 /// Level of nesting in macros (including this one)
147 bool validName() const;
149 size_t arity() const;
152 size_t optionals() const;
154 void setOptionals(int n);
155 /// Return the maximal number of arguments the macro is greedy for.
156 size_t appetite() const;
158 InsetCode lyxCode() const override { return MATH_MACRO_CODE; }
159 /// This is not used for display; however whether it is mathrel determines
160 /// how to split equations intelligently.
161 MathClass mathClass() const override;
162 /// Override so as to set Buffer for definition_ member, too.
163 void setBuffer(Buffer &) override;
166 friend class MathData;
167 friend class ArgumentProxy;
170 /// update the display mode (should only be called after detaching arguments)
171 void setDisplayMode(DisplayMode mode, int appetite = -1);
172 /// compute the next display mode
173 DisplayMode computeDisplayMode() const;
174 /// update macro definition
175 void updateMacro(MacroContext const & mc);
176 /// check if macro definition changed, argument changed etc. and adapt
177 void updateRepresentation(Cursor * cur, MacroContext const & mc,
178 UpdateType, int nesting);
179 /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
180 /// Includes the optional arguments.
181 void detachArguments(std::vector<MathData> & args, bool strip);
182 /// attach arguments (maybe less than arity at the end of an MathData),
183 /// including the optional ones (even if it can be empty here)
184 void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
187 /// This function is needed for now because of two shortfalls of the current
188 /// implementation: the macro() pointer is often dangling, in which case we
189 /// fall back to a backup copy, and the macro is not known at inset
190 /// creation, in which case we fall back to the global macro with this name.
191 MacroData const * macroBackup() const;
193 Inset * clone() const override;
195 bool editMode(BufferView const * bv) const;
201 /// update lock to avoid loops
203 friend class UpdateLocker;
207 bool completionSupported(Cursor const &) const override;
209 bool inlineCompletionSupported(Cursor const & cur) const override;
211 bool automaticInlineCompletion() const override;
213 bool automaticPopupCompletion() const override;
215 CompletionList const * createCompletionList(Cursor const & cur) const override;
217 docstring completionPrefix(Cursor const & cur) const override;
219 bool insertCompletion(Cursor & cur, docstring const & s, bool finished) override;
221 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const override;