]> git.lyx.org Git - features.git/blobdiff - src/mathed/MathMacroTemplate.h
* Lazy MathData to avoid unneeded interpretation of macro definitions
[features.git] / src / mathed / MathMacroTemplate.h
index c24fd16f80433e8b28ad8de6a64d7d6ae12abf9f..c14bfcb3ebade4af9ebeb024efc737c91634ced1 100644 (file)
@@ -13,9 +13,9 @@
 #ifndef MATH_MACROTEMPLATE_H
 #define MATH_MACROTEMPLATE_H
 
-#include "MathData.h"
-#include "MacroTable.h"
 #include "InsetMathNest.h"
+#include "MacroTable.h"
+#include "MathData.h"
 
 #include "support/types.h"
 
@@ -28,15 +28,14 @@ public:
        ///
        MathMacroTemplate();
        ///
-       MathMacroTemplate(docstring const & name, int nargs,
-               docstring const & type,
-               MathData const & = MathData(),
-               MathData const & = MathData());
+       MathMacroTemplate(docstring const & name, int nargs, int optional, 
+               MacroType type,
+               std::vector<MathData> const & optionalValues = std::vector<MathData>(),
+               MathData const & def = MathData(),
+               MathData const & display = MathData());
        ///
        explicit MathMacroTemplate(const docstring & str);
        ///
-       void edit(Cursor & cur, bool left);
-       ///
        EDITABLE editable() const { return HIGHLY_EDITABLE; }
        ///
        void read(Buffer const &, Lexer & lex);
@@ -44,40 +43,96 @@ public:
        void write(Buffer const &, std::ostream & os) const;
        ///
        void write(WriteStream & os) const;
+       /// Output LaTeX code, but assume that the macro is not definied yet
+       /// if overwriteRedefinition is true
+       void write(WriteStream & os, bool overwriteRedefinition) const;
        ///
        int plaintext(Buffer const &, odocstream &,
-                     OutputParams const &) const;
-
-       /// Number of arguments
-       int numargs() const;
+               OutputParams const &) const;
        ///
-       void numargs(int);
+       bool noFontChange() const { return true; }
+
        ///
        docstring name() const;
        ///
-       MacroData asMacroData() const;
+       void getDefaults(std::vector<docstring> & defaults) const;
+       ///
+       docstring definition() const;
+       ///
+       docstring displayDefinition() const;
+       ///
+       size_t numArgs() const;
+       ///
+       size_t numOptionals() const;
+       ///
+       bool redefinition() const { return redefinition_; }
+       ///
+       MacroType type() const { return type_; }
+
+       /// check name and possible other formal properties
+       bool validMacro() const;
+       ///
+       bool validName() const;
+       /// Remove everything from the name which makes it invalid 
+       /// and return true iff it is valid.
+       bool fixNameAndCheckIfValid();
+
+       /// decide whether its a redefinition
+       void updateToContext(MacroContext const & mc) const;
+
        ///
        void draw(PainterInfo & pi, int x, int y) const;
        ///
-       bool metrics(MetricsInfo & mi, Dimension & dim) const;
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
        /// identifies macro templates
        MathMacroTemplate * asMacroTemplate() { return this; }
        /// identifies macro templates
        MathMacroTemplate const * asMacroTemplate() const { return this; }
        ///
-       Inset::Code lyxCode() const { return MATHMACRO_CODE; }
+       InsetCode lyxCode() const { return MATHMACRO_CODE; }
+
+protected:
+       ///
+       virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
+       /// do we want to handle this event?
+       bool getStatus(Cursor & cur, FuncRequest const & cmd,
+               FuncStatus & status) const;
 
 private:
-       virtual std::auto_ptr<Inset> doClone() const;
-       /// prefix in inset
-       docstring prefix() const;
+       virtual Inset * clone() const;
 
+       /// remove #n with from<=n<=to
+       void removeArguments(Cursor & cur, int from, int to);
+       /// shift every #n with from<=n, i.e. #n -> #(n-by)
+       void shiftArguments(size_t from, int by);
+       ///
+       void insertParameter(Cursor & cur, int pos, bool greedy = false);
        ///
-       int numargs_;
+       void removeParameter(Cursor & cur, int pos, bool greedy = false );
        ///
-       docstring name_;
-       /// newcommand or renewcommand or def
-       docstring type_;
+       void makeOptional(Cursor & cur);
+       ///
+       void makeNonOptional(Cursor & cur);
+       ///
+       idx_type defIdx() const { return optionals_ + 1; }
+       /// index of default value cell of optional parameter (#1 -> n=0)
+       idx_type optIdx(idx_type n) const { return n + 1; }
+       ///
+       idx_type displayIdx() const { return optionals_ + 2; }
+       /// The label with some holes to edit
+       mutable MathData label_;
+       ///
+       mutable int numargs_;
+       ///
+       int optionals_;
+       /// keeps the old optional default value when an 
+       /// optional argument is disabled
+       std::vector<MathData> optionalValues_;
+
+       /// (re)newcommand or def
+       mutable MacroType type_;
+       /// defined before already?
+       mutable bool redefinition_;
 };