2001-02-13 17:08:51 +00:00
|
|
|
// -*- C++ -*-
|
2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-28 20:44:46 +00:00
|
|
|
* \file MacroTable.h
|
2003-08-19 13:00:56 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2007-04-17 16:58:59 +00:00
|
|
|
* \author André Pönitz
|
2007-11-01 11:13:07 +00:00
|
|
|
* \author Stefan Schimanski
|
2003-08-19 13:00:56 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
#ifndef MATH_MACROTABLE_H
|
|
|
|
#define MATH_MACROTABLE_H
|
2001-02-13 17:08:51 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
#include "DocIterator.h"
|
|
|
|
|
2007-04-17 16:52:43 +00:00
|
|
|
#include "support/docstring.h"
|
2007-12-21 20:42:46 +00:00
|
|
|
#include "support/types.h"
|
2007-04-17 16:52:43 +00:00
|
|
|
|
2007-04-17 16:35:13 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
class Buffer;
|
2007-04-26 16:05:57 +00:00
|
|
|
class MathData;
|
2007-12-21 20:42:46 +00:00
|
|
|
class MathMacroTemplate;
|
2007-11-01 11:13:07 +00:00
|
|
|
class Paragraph;
|
2001-03-01 14:07:43 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
|
|
enum MacroType {
|
|
|
|
MacroTypeNewcommand,
|
|
|
|
MacroTypeDef
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
///
|
|
|
|
class MacroData {
|
2001-02-13 17:08:51 +00:00
|
|
|
public:
|
2007-12-21 20:42:46 +00:00
|
|
|
/// Constructor to make STL containers happy
|
2004-04-13 06:27:29 +00:00
|
|
|
MacroData();
|
2007-12-21 20:42:46 +00:00
|
|
|
/// 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(MathMacroTemplate const & macro);
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
docstring const & definition() const { updateData(); return definition_; }
|
2004-04-13 06:27:29 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
docstring const & display() const { updateData(); return display_; }
|
2007-11-01 11:13:07 +00:00
|
|
|
/// arity including optional arguments (if there is any)
|
2007-12-21 20:42:46 +00:00
|
|
|
size_t numargs() const { updateData(); return numargs_; }
|
2007-11-06 20:38:35 +00:00
|
|
|
/// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
|
|
|
|
/// optional argument
|
2007-04-26 16:05:57 +00:00
|
|
|
void expand(std::vector<MathData> const & from, MathData & to) const;
|
2007-11-01 11:13:07 +00:00
|
|
|
/// number of optional arguments
|
2007-11-01 15:36:27 +00:00
|
|
|
size_t optionals() const;
|
2007-11-01 11:13:07 +00:00
|
|
|
///
|
|
|
|
std::vector<docstring> const & defaults() const;
|
2006-11-13 17:35:18 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
std::string const & requires() const { updateData(); return requires_; }
|
2006-11-13 17:35:18 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
std::string & requires() { updateData(); return requires_; }
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
|
|
/// lock while being drawn to avoid recursions
|
2007-04-17 16:52:43 +00:00
|
|
|
int lock() const { return ++lockCount_; }
|
|
|
|
/// is it being drawn?
|
|
|
|
bool locked() const { return lockCount_ != 0; }
|
2007-11-01 11:13:07 +00:00
|
|
|
///
|
2007-11-06 20:38:35 +00:00
|
|
|
void unlock() const;
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
bool redefinition() const { return redefinition_; }
|
|
|
|
///
|
|
|
|
void setRedefinition(bool redefined) { redefinition_ = redefined; }
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
MacroType type() const { return type_; }
|
|
|
|
///
|
|
|
|
MacroType & type() { return type_; }
|
2007-11-01 11:13:07 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
/// output as TeX macro, only works for lazy MacroData!!!
|
|
|
|
void write(odocstream & os, bool overwriteRedefinition) const;
|
|
|
|
|
2007-05-24 16:29:40 +00:00
|
|
|
///
|
|
|
|
bool operator==(MacroData const & x) const {
|
2007-12-21 20:42:46 +00:00
|
|
|
updateData();
|
|
|
|
x.updateData();
|
2007-11-01 11:13:07 +00:00
|
|
|
return definition_ == x.definition_
|
|
|
|
&& numargs_ == x.numargs_
|
|
|
|
&& display_ == x.display_
|
|
|
|
&& requires_ == x.requires_
|
|
|
|
&& optionals_ == x.optionals_
|
|
|
|
&& defaults_ == x.defaults_;
|
2007-05-24 16:29:40 +00:00
|
|
|
}
|
|
|
|
///
|
|
|
|
bool operator!=(MacroData const & x) const { return !operator==(x); }
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
private:
|
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
void queryData(MathMacroTemplate const & macro) const;
|
2004-04-13 06:27:29 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
void updateData() const;
|
2001-03-01 14:07:43 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
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_;
|
2006-11-13 17:35:18 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
mutable bool queried_;
|
2007-04-17 16:52:43 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
mutable docstring definition_;
|
|
|
|
///
|
|
|
|
mutable size_t numargs_;
|
|
|
|
///
|
|
|
|
mutable docstring display_;
|
|
|
|
///
|
|
|
|
mutable std::string requires_;
|
|
|
|
///
|
|
|
|
mutable size_t optionals_;
|
|
|
|
///
|
|
|
|
mutable std::vector<docstring> defaults_;
|
2007-11-01 11:13:07 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
mutable int lockCount_;
|
2007-11-01 11:13:07 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
mutable bool redefinition_;
|
2007-11-01 11:13:07 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
mutable MacroType type_;
|
2004-04-13 06:27:29 +00:00
|
|
|
};
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
/// 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.
|
|
|
|
*
|
|
|
|
**/
|
2006-10-22 10:15:23 +00:00
|
|
|
class MacroTable : public std::map<docstring, MacroData>
|
2004-04-13 13:54:58 +00:00
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
public:
|
2006-11-13 17:35:18 +00:00
|
|
|
/// Parse full "\\def..." or "\\newcommand..." or ...
|
|
|
|
void insert(docstring const & definition, std::string const &);
|
2004-04-13 06:27:29 +00:00
|
|
|
/// Insert pre-digested macro definition
|
2006-10-22 10:15:23 +00:00
|
|
|
void insert(docstring const & name, MacroData const & data);
|
2001-03-01 14:07:43 +00:00
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
MacroData const * get(docstring const & name) const;
|
2002-03-27 11:02:56 +00:00
|
|
|
///
|
2004-04-13 06:27:29 +00:00
|
|
|
void dump();
|
|
|
|
|
|
|
|
/// the global list
|
|
|
|
static MacroTable & globalMacros();
|
2001-02-13 17:08:51 +00:00
|
|
|
};
|
2002-07-12 14:24:47 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
/// 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:
|
2007-12-21 20:42:46 +00:00
|
|
|
/// construct context for the insets at pos
|
|
|
|
MacroContext(Buffer const & buf, DocIterator const & pos);
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
|
|
/// Lookup macro
|
2007-12-21 20:42:46 +00:00
|
|
|
MacroData const * get(docstring const & name) const;
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
///
|
|
|
|
Buffer const & buf_;
|
|
|
|
///
|
2007-12-21 20:42:46 +00:00
|
|
|
DocIterator const & pos_;
|
2007-11-01 11:13:07 +00:00
|
|
|
};
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
#endif
|