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 MathMacro : public InsetMathNest {
27 /// A macro can be built from an existing template
28 MathMacro(Buffer * buf, docstring const & name);
30 MathMacro(MathMacro const &);
32 MathMacro & operator=(MathMacro const &);
36 virtual MathMacro * asMacro() { return this; }
38 virtual MathMacro const * asMacro() const { return this; }
40 marker_type marker() const;
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;
45 void beforeMetrics() const;
47 void afterMetrics() const;
49 void beforeDraw(PainterInfo const &) const;
51 void afterDraw(PainterInfo const &) const;
54 void metrics(MetricsInfo & mi, Dimension & dim) const;
55 /// was the macro in edit mode when computing metrics?
56 bool editMetrics(BufferView const * bv) const;
58 void draw(PainterInfo & pi, int x, int y) const;
59 /// draw selection background
60 void drawSelection(PainterInfo & pi, int x, int y) const;
62 int kerning(BufferView const * bv) const;
63 /// get cursor position
64 void cursorPos(BufferView const & bv, CursorSlice const & sl,
65 bool boundary, int & x, int & y) const;
67 void edit(Cursor & cur, bool front, EntryDirection entry_from);
69 Inset * editXY(Cursor & cur, int x, int y);
71 /// target pos when we enter the inset while moving forward
72 bool idxFirst(Cursor &) const;
73 /// target pos when we enter the inset while moving backwards
74 bool idxLast(Cursor &) const;
77 virtual bool notifyCursorLeaves(Cursor const & old, Cursor & cur);
79 /// Remove cell (starting from 0)
80 void removeArgument(pos_type pos);
81 /// Insert empty cell (starting from 0)
82 void insertArgument(pos_type pos);
85 void validate(LaTeXFeatures &) const;
87 mode_type currentMode() const;
90 void write(WriteStream & os) const;
92 void normalize(NormalStream & os) const;
94 void maple(MapleStream &) const;
96 void maxima(MaximaStream &) const;
98 void mathematica(MathematicaStream &) const;
100 void mathmlize(MathStream &) const;
102 void htmlize(HtmlStream &) const;
104 void octave(OctaveStream &) const;
106 void infoize(odocstream &) const;
108 void infoize2(odocstream &) const;
110 /// fold the macro in the next metrics call
111 void fold(Cursor & cur);
112 /// unfold the macro in the next metrics call
113 void unfold(Cursor & cur);
114 /// will it be folded or unfolded in the next metric call?
119 DISPLAY_INTERACTIVE_INIT,
125 DisplayMode displayMode() const;
128 bool extraBraces() const;
131 docstring name() const;
132 /// FIXME: Often dangling.
133 MacroData const * macro() const;
135 docstring macroName() const;
136 /// Level of nesting in macros (including this one)
139 bool validName() const;
141 size_t arity() const;
144 size_t optionals() const;
146 void setOptionals(int n);
147 /// Return the maximal number of arguments the macro is greedy for.
148 size_t appetite() const;
150 InsetCode lyxCode() const { return MATH_MACRO_CODE; }
151 /// This is not used for display; however whether it is mathrel determines
152 /// how to split equations intelligently.
153 MathClass mathClass() const; //override
156 friend class MathData;
157 friend class ArgumentProxy;
160 /// update the display mode (should only be called after detaching arguments)
161 void setDisplayMode(DisplayMode mode, int appetite = -1);
162 /// compute the next display mode
163 DisplayMode computeDisplayMode() const;
164 /// update macro definition
165 void updateMacro(MacroContext const & mc);
166 /// check if macro definition changed, argument changed etc. and adapt
167 void updateRepresentation(Cursor * cur, MacroContext const & mc,
168 UpdateType, int nesting);
169 /// empty macro, put arguments into args, possibly strip arity-attachedArgsNum_ empty ones.
170 /// Includes the optional arguments.
171 void detachArguments(std::vector<MathData> & args, bool strip);
172 /// attach arguments (maybe less than arity at the end of an MathData),
173 /// including the optional ones (even if it can be empty here)
174 void attachArguments(std::vector<MathData> const & args, size_t arity, int optionals);
177 /// Math mode for output and display. UNDECIDED for user macros: they could
179 mode_type modeToEnsure() const;
180 /// This function is needed for now because of two shortfalls of the current
181 /// implementation: the macro() pointer is often dangling, in which case we
182 /// fall back to a backup copy, and the macro is not known at inset
183 /// creation, in which case we fall back to the global macro with this name.
184 MacroData const * macroBackup() const;
186 virtual Inset * clone() const;
188 bool editMode(BufferView const * bv) const;
194 /// update lock to avoid loops
196 friend class UpdateLocker;
200 bool completionSupported(Cursor const &) const;
202 bool inlineCompletionSupported(Cursor const & cur) const;
204 bool automaticInlineCompletion() const;
206 bool automaticPopupCompletion() const;
208 CompletionList const * createCompletionList(Cursor const & cur) const;
210 docstring completionPrefix(Cursor const & cur) const;
212 bool insertCompletion(Cursor & cur, docstring const & s, bool finished);
214 void completionPosAndDim(Cursor const &, int & x, int & y, Dimension & dim) const;