]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MacroTable.h
g-brief loads babel internally. So don't load it ourselves.
[lyx.git] / src / mathed / MacroTable.h
index 7bc220a7e2408171451b5cfc1c271b9bceae87c1..6c17d634709bfe7079ee06c805156914f9103046 100644 (file)
@@ -5,6 +5,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author André Pönitz
+ * \author Stefan Schimanski
  *
  * Full author contact details are available in file CREDITS.
  */
 #ifndef MATH_MACROTABLE_H
 #define MATH_MACROTABLE_H
 
-#include "support/docstring.h"
+#include "DocIterator.h"
 
-#include <boost/assert.hpp>
+#include "support/docstring.h"
 
 #include <map>
+#include <set>
 #include <vector>
 
 namespace lyx {
 
+class Buffer;
 class MathData;
+class InsetMathMacroTemplate;
+class latexkeys;
+
+enum MacroType {
+       MacroTypeNewcommand,
+       MacroTypeNewcommandx,
+       MacroTypeDef
+};
 
 ///
 class MacroData {
 public:
+       /// Constructor to make STL containers happy
+       explicit MacroData(Buffer const * buf = 0);
+       /// Create lazy MacroData which only queries the macro template when needed
+       MacroData(Buffer const * buf, DocIterator const & pos);
+       /// Create non-lazy MacroData which directly queries the macro template
+       MacroData(Buffer const * buf, InsetMathMacroTemplate const & macro);
+
        ///
-       MacroData();
+       docstring const & definition() const { updateData(); return definition_; }
        ///
-       MacroData(docstring const & def, int nargs, docstring const & disp, std::string const &);
+       docstring const & display() const { updateData(); return display_; }
+       /// arity including optional arguments (if there is any)
+       size_t numargs() const { updateData(); return numargs_; }
+       /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
+       /// optional argument
+       /// \return whether anything was expanded
+       bool expand(std::vector<MathData> const & from, MathData & to) const;
+       /// number of optional arguments
+       size_t optionals() const;
        ///
-       docstring def() const { return def_; }
+       std::vector<docstring> const & defaults() const;
        ///
-       docstring disp() const { return disp_; }
+       std::string const required() const;
        ///
-       int numargs() const { return numargs_; }
-       /// replace #1,#2,... by given MathAtom 0,1,..
-       void expand(std::vector<MathData> const & from, MathData & to) const;
+       bool hidden() const;
        ///
-       std::string requires() const { return requires_; }
+       docstring const xmlname() const;
        ///
-       std::string & requires() { return requires_; }
-       /// lock while being drawn
+       char const * MathMLtype() const;
+       ///
+       latexkeys const * symbol() const { return sym_; }
+       ///
+       void setSymbol(latexkeys const * sym) { sym_ = sym; }
+       ///
+       DocIterator const & pos() const { return pos_; }
+
+       /// lock while being drawn to avoid recursions
        int lock() const { return ++lockCount_; }
        /// is it being drawn?
        bool locked() const { return lockCount_ != 0; }
+       ///
+       void unlock() const;
+
        ///
-       void unlock() const { --lockCount_; BOOST_ASSERT(lockCount_ >= 0); }    
+       bool redefinition() const { updateData(); return redefinition_; }
+
+       ///
+       MacroType type() const { updateData(); return type_; }
+
+       /// output as TeX macro, only works for lazy MacroData!!!
+       int write(odocstream & os, bool overwriteRedefinition) const;
+
+       ///
+       bool operator==(MacroData const & x) const {
+               updateData();
+               x.updateData();
+               return definition_ == x.definition_
+                       && numargs_ == x.numargs_
+                       && display_ == x.display_
+                       && sym_ == x.sym_
+                       && optionals_ == x.optionals_
+                       && defaults_ == x.defaults_;
+       }
+       ///
+       bool operator!=(MacroData const & x) const { return !operator==(x); }
 
 private:
        ///
-       docstring def_;
+       void queryData(InsetMathMacroTemplate const & macro) const;
+       ///
+       void updateData() const;
+       ///
+       Buffer const * buffer_;
+       /// The position of the definition in the buffer.
+       /// There is no guarantee it stays valid if the buffer
+       /// changes. But it (normally) exists only until the
+       /// next Buffer::updateMacros call where new MacroData
+       /// objects are created for each macro definition.
+       /// In the worst case, it is invalidated and the MacroData
+       /// returns its defaults values and the user sees unfolded
+       /// macros.
+       mutable DocIterator pos_;
+       ///
+       mutable bool queried_ = false;
+       ///
+       mutable docstring definition_;
        ///
-       int numargs_;
+       mutable size_t numargs_ = 0;
        ///
-       docstring disp_;
+       mutable docstring display_;
        ///
-       std::string requires_;
+       latexkeys const * sym_ = nullptr;
        ///
-       mutable int lockCount_;
+       mutable size_t optionals_ = 0;
+       ///
+       mutable std::vector<docstring> defaults_;
+       ///
+       mutable int lockCount_ = 0;
+       ///
+       mutable bool redefinition_ = false;
+       ///
+       mutable MacroType type_ = MacroTypeNewcommand;
 };
 
 
-// This contains a table of "global" macros that are always accessible,
-// either because they implement a feature of standard LaTeX or some
-// hack to display certain contents nicely.
+///
+class MacroNameSet : public std::set<docstring> {};
+///
+class MacroSet : public std::set<MacroData const *> {};
+
 
+/// A lookup table of macro definitions.
+/**
+ * This contains a table of "global" macros that are always accessible,
+ * either because they implement a feature of standard LaTeX or some
+ * hack to display certain contents nicely.
+ *
+ **/
 class MacroTable : public std::map<docstring, MacroData>
 {
 public:
        /// Parse full "\\def..." or "\\newcommand..." or ...
-       void insert(docstring const & definition, std::string const &);
+       iterator insert(Buffer * buf, docstring const & definition);
        /// Insert pre-digested macro definition
-       void insert(docstring const & name, MacroData const & data);
-       /// Do we have a macro by that name?
-       bool has(docstring const & name) const;
+       iterator insert(docstring const & name, MacroData const & data);
        ///
-       MacroData const & get(docstring const & name) const;
+       MacroData const * get(docstring const & name) const;
        ///
        void dump();
+       ///
+       void getMacroNames(std::set<docstring> & names, bool gethidden) const;
 
        /// the global list
        static MacroTable & globalMacros();
-       /// the local list hack
-       //static MacroTable & localMacros();
 };
 
 
+/// A context to lookup macros at a certain position in a buffer.
+/**
+ * The MacroContext is used during metrics calculation to resolve
+ * macro instances according to the position of them in the buffer
+ * document. Only macro definition in front of the macro instance
+ * are visible and are resolved.
+ *
+ **/
+class MacroContext {
+public:
+       /// construct context for the insets at pos
+       MacroContext(Buffer const * buf, DocIterator const & pos);
+
+       /// Lookup macro
+       MacroData const * get(docstring const & name) const;
+
+private:
+       ///
+       Buffer const * buf_;
+       ///
+       DocIterator const & pos_;
+};
+
 } // namespace lyx
 
 #endif