]> git.lyx.org Git - lyx.git/blobdiff - src/Layout.cpp
simplify Lexer handling a bit more
[lyx.git] / src / Layout.cpp
index 26740fc2b62f41d5fec4dfe6e853a7c9db75a94f..c3889fd2c4182516bf337c69e059495c9f581362 100644 (file)
 #include "Layout.h"
 #include "TextClass.h"
 #include "Lexer.h"
-#include "debug.h"
+#include "Font.h"
 
+#include "support/debug.h"
 #include "support/lstrings.h"
 
-using std::endl;
-using std::string;
+#include <ostream>
 
-namespace lyx {
-
-using support::subst;
-using support::trim;
+using namespace std;
+using namespace lyx::support;
 
-extern FontInfo lyxRead(Lexer &);
+namespace lyx {
 
 /// Special value of toclevel for layouts that to not belong in a TOC
 const int Layout::NOT_IN_TOC = -1000;
@@ -39,6 +37,7 @@ enum LayoutTags {
        LT_ALIGNPOSSIBLE,
        LT_MARGIN,
        LT_BOTTOMSEP,
+       LT_CATEGORY,
        LT_COMMANDDEPTH,
        LT_COPYSTYLE,
        LT_DEPENDSON,
@@ -83,6 +82,7 @@ enum LayoutTags {
        LT_PARSKIP,
        //LT_PLAIN,
        LT_PREAMBLE,
+       LT_REQUIRES,
        LT_RIGHTMARGIN,
        LT_SPACING,
        LT_TOPSEP,
@@ -134,10 +134,11 @@ Layout::Layout()
 bool Layout::read(Lexer & lexrc, TextClass const & tclass)
 {
        // This table is sorted alphabetically [asierra 30March96]
-       keyword_item layoutTags[] = {
+       LexerKeyword layoutTags[] = {
                { "align",          LT_ALIGN },
                { "alignpossible",  LT_ALIGNPOSSIBLE },
                { "bottomsep",      LT_BOTTOMSEP },
+               { "category",       LT_CATEGORY },
                { "commanddepth",   LT_COMMANDDEPTH },
                { "copystyle",      LT_COPYSTYLE },
                { "dependson",      LT_DEPENDSON },
@@ -180,6 +181,7 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
                { "parskip",        LT_PARSKIP },
                { "passthru",       LT_PASS_THRU },
                { "preamble",       LT_PREAMBLE },
+               { "requires",       LT_REQUIRES },
                { "rightmargin",    LT_RIGHTMARGIN },
                { "spacing",        LT_SPACING },
                { "textfont",       LT_TEXTFONT },
@@ -189,7 +191,7 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
 
        bool error = false;
        bool finished = false;
-       lexrc.pushTable(layoutTags, LT_INTITLE);
+       lexrc.pushTable(layoutTags);
        // parse style section
        while (!finished && lexrc.isOK() && !error) {
                int le = lexrc.lex();
@@ -209,6 +211,11 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
                        finished = true;
                        break;
 
+               case LT_CATEGORY:
+                       if (lexrc.next())
+                               category_ = lexrc.getDocString();
+                       break;
+
                case LT_COPYSTYLE:     // initialize with a known style
                        if (lexrc.next()) {
                                docstring const style = subst(lexrc.getDocString(),
@@ -216,21 +223,17 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
 
                                if (tclass.hasLayout(style)) {
                                        docstring const tmpname = name_;
-                                       this->operator=(*tclass[style]);
+                                       this->operator=(tclass[style]);
                                        name_ = tmpname;
                                } else {
                                        lyxerr << "Cannot copy unknown style `"
                                               << to_utf8(style) << "'\n"
                                               << "All layouts so far:"
                                               << endl;
-                                       TextClass::const_iterator it =
-                                               tclass.begin();
-                                       TextClass::const_iterator end =
-                                               tclass.end();
-                                       for (; it != end; ++it) {
-                                               lyxerr << to_utf8((*it)->name())
-                                                      << endl;
-                                       }
+                                       DocumentClass::const_iterator lit = tclass.begin();
+                                       DocumentClass::const_iterator len = tclass.end();
+                                       for (; lit != len; ++lit)
+                                               lyxerr << to_utf8(lit->name()) << endl;
 
                                        //lexrc.printError("Cannot copy known "
                                        //               "style `$$Token'");
@@ -245,7 +248,7 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
 
                                if (tclass.hasLayout(style)) {
                                        docstring const tmpname = name_;
-                                       this->operator=(*tclass[style]);
+                                       this->operator=(tclass[style]);
                                        name_ = tmpname;
                                        if (obsoleted_by().empty())
                                                obsoleted_by_ = style;
@@ -311,16 +314,16 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
                        break;
 
                case LT_FONT:
-                       font = lyxRead(lexrc);
+                       font = lyxRead(lexrc, font);
                        labelfont = font;
                        break;
 
                case LT_TEXTFONT:
-                       font = lyxRead(lexrc);
+                       font = lyxRead(lexrc, font);
                        break;
 
                case LT_LABELFONT:
-                       labelfont = lyxRead(lexrc);
+                       labelfont = lyxRead(lexrc, labelfont);
                        break;
 
                case LT_NEXTNOINDENT:   // Indent next paragraph?
@@ -485,15 +488,23 @@ bool Layout::read(Lexer & lexrc, TextClass const & tclass)
                case LT_SPACING: // setspace.sty
                        readSpacing(lexrc);
                        break;
+
+               case LT_REQUIRES:
+                       lexrc.eatLine();
+                       vector<string> const req = 
+                               getVectorFromString(lexrc.getString());
+                       requires_.insert(req.begin(), req.end());
+                       break;
+
                }
        }
        lexrc.popTable();
 
-       return error;
+       return !error;
 }
 
 
-enum AlignTags {
+enum {
        AT_BLOCK = 1,
        AT_LEFT,
        AT_RIGHT,
@@ -501,10 +512,9 @@ enum AlignTags {
        AT_LAYOUT
 };
 
-
 void Layout::readAlign(Lexer & lexrc)
 {
-       keyword_item alignTags[] = {
+       LexerKeyword alignTags[] = {
                { "block",  AT_BLOCK },
                { "center", AT_CENTER },
                { "layout", AT_LAYOUT },
@@ -512,7 +522,7 @@ void Layout::readAlign(Lexer & lexrc)
                { "right",  AT_RIGHT }
        };
 
-       PushPopHelper pph(lexrc, alignTags, AT_LAYOUT);
+       PushPopHelper pph(lexrc, alignTags);
        int le = lexrc.lex();
        switch (le) {
        case Lexer::LEX_UNDEF:
@@ -520,7 +530,7 @@ void Layout::readAlign(Lexer & lexrc)
                return;
        default: break;
        };
-       switch (static_cast<AlignTags>(le)) {
+       switch (le) {
        case AT_BLOCK:
                align = LYX_ALIGN_BLOCK;
                break;
@@ -542,7 +552,7 @@ void Layout::readAlign(Lexer & lexrc)
 
 void Layout::readAlignPossible(Lexer & lexrc)
 {
-       keyword_item alignTags[] = {
+       LexerKeyword alignTags[] = {
                { "block",  AT_BLOCK },
                { "center", AT_CENTER },
                { "layout", AT_LAYOUT },
@@ -550,9 +560,9 @@ void Layout::readAlignPossible(Lexer & lexrc)
                { "right",  AT_RIGHT }
        };
 
-       lexrc.pushTable(alignTags, AT_LAYOUT);
+       lexrc.pushTable(alignTags);
        alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
-       int lineno = lexrc.getLineNo();
+       int lineno = lexrc.lineNumber();
        do {
                int le = lexrc.lex();
                switch (le) {
@@ -561,7 +571,7 @@ void Layout::readAlignPossible(Lexer & lexrc)
                        continue;
                default: break;
                };
-               switch (static_cast<AlignTags>(le)) {
+               switch (le) {
                case AT_BLOCK:
                        alignpossible |= LYX_ALIGN_BLOCK;
                        break;
@@ -578,41 +588,41 @@ void Layout::readAlignPossible(Lexer & lexrc)
                        alignpossible |= LYX_ALIGN_LAYOUT;
                        break;
                }
-       } while (lineno == lexrc.getLineNo());
+       } while (lineno == lexrc.lineNumber());
        lexrc.popTable();
 }
 
 
-enum LabelTypeTags {
-       LA_NO_LABEL = 1,
-       LA_MANUAL,
-       LA_TOP_ENVIRONMENT,
-       LA_CENTERED_TOP_ENVIRONMENT,
-       LA_STATIC,
-       LA_SENSITIVE,
-       LA_COUNTER,
-       LA_ENUMERATE,
-       LA_ITEMIZE,
-       LA_BIBLIO
-};
-
-
 void Layout::readLabelType(Lexer & lexrc)
 {
-       keyword_item labelTypeTags[] = {
-       { "bibliography",             LA_BIBLIO },
-       { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
-       { "counter",                  LA_COUNTER },
-       { "enumerate",                LA_ENUMERATE },
-       { "itemize",                  LA_ITEMIZE },
-       { "manual",                   LA_MANUAL },
-       { "no_label",                 LA_NO_LABEL },
-       { "sensitive",                LA_SENSITIVE },
-       { "static",                   LA_STATIC },
-       { "top_environment",          LA_TOP_ENVIRONMENT }
+       enum {
+               LA_NO_LABEL = 1,
+               LA_MANUAL,
+               LA_TOP_ENVIRONMENT,
+               LA_CENTERED_TOP_ENVIRONMENT,
+               LA_STATIC,
+               LA_SENSITIVE,
+               LA_COUNTER,
+               LA_ENUMERATE,
+               LA_ITEMIZE,
+               LA_BIBLIO
+       };
+
+
+       LexerKeyword labelTypeTags[] = {
+               { "bibliography",             LA_BIBLIO },
+               { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
+               { "counter",                  LA_COUNTER },
+               { "enumerate",                LA_ENUMERATE },
+               { "itemize",                  LA_ITEMIZE },
+               { "manual",                   LA_MANUAL },
+               { "no_label",                 LA_NO_LABEL },
+               { "sensitive",                LA_SENSITIVE },
+               { "static",                   LA_STATIC },
+               { "top_environment",          LA_TOP_ENVIRONMENT }
        };
 
-       PushPopHelper pph(lexrc, labelTypeTags, LA_BIBLIO);
+       PushPopHelper pph(lexrc, labelTypeTags);
        int le = lexrc.lex();
        switch (le) {
        case Lexer::LEX_UNDEF:
@@ -620,7 +630,7 @@ void Layout::readLabelType(Lexer & lexrc)
                return;
        default: break;
        }
-       switch (static_cast<LabelTypeTags>(le)) {
+       switch (le) {
        case LA_NO_LABEL:
                labeltype = LABEL_NO_LABEL;
                break;
@@ -655,19 +665,16 @@ void Layout::readLabelType(Lexer & lexrc)
 }
 
 
-static keyword_item endlabelTypeTags[] =
-{
-       { "box",        END_LABEL_BOX },
-       { "filled_box", END_LABEL_FILLED_BOX },
-       { "no_label",   END_LABEL_NO_LABEL },
-       { "static",     END_LABEL_STATIC }
-};
-
-
 void Layout::readEndLabelType(Lexer & lexrc)
 {
-       PushPopHelper pph(lexrc, endlabelTypeTags,
-                         END_LABEL_ENUM_LAST-END_LABEL_ENUM_FIRST+1);
+       static LexerKeyword endlabelTypeTags[] = {
+               { "box",              END_LABEL_BOX },
+               { "filled_box", END_LABEL_FILLED_BOX },
+               { "no_label",     END_LABEL_NO_LABEL },
+               { "static",     END_LABEL_STATIC }
+       };
+
+       PushPopHelper pph(lexrc, endlabelTypeTags);
        int le = lexrc.lex();
        switch (le) {
        case Lexer::LEX_UNDEF:
@@ -689,7 +696,7 @@ void Layout::readEndLabelType(Lexer & lexrc)
 
 void Layout::readMargin(Lexer & lexrc)
 {
-       keyword_item marginTags[] = {
+       LexerKeyword marginTags[] = {
                { "dynamic",           MARGIN_DYNAMIC },
                { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
                { "manual",            MARGIN_MANUAL },
@@ -697,7 +704,7 @@ void Layout::readMargin(Lexer & lexrc)
                { "static",            MARGIN_STATIC }
        };
 
-       PushPopHelper pph(lexrc, marginTags, MARGIN_RIGHT_ADDRESS_BOX);
+       PushPopHelper pph(lexrc, marginTags);
 
        int le = lexrc.lex();
        switch (le) {
@@ -721,7 +728,7 @@ void Layout::readMargin(Lexer & lexrc)
 
 void Layout::readLatexType(Lexer & lexrc)
 {
-       keyword_item latexTypeTags[] = {
+       LexerKeyword latexTypeTags[] = {
                { "bib_environment",  LATEX_BIB_ENVIRONMENT },
                { "command",          LATEX_COMMAND },
                { "environment",      LATEX_ENVIRONMENT },
@@ -730,7 +737,7 @@ void Layout::readLatexType(Lexer & lexrc)
                { "paragraph",        LATEX_PARAGRAPH }
        };
 
-       PushPopHelper pph(lexrc, latexTypeTags, LATEX_LIST_ENVIRONMENT);
+       PushPopHelper pph(lexrc, latexTypeTags);
        int le = lexrc.lex();
        switch (le) {
        case Lexer::LEX_UNDEF:
@@ -752,24 +759,23 @@ void Layout::readLatexType(Lexer & lexrc)
 }
 
 
-enum SpacingTags {
-       ST_SPACING_SINGLE = 1,
-       ST_SPACING_ONEHALF,
-       ST_SPACING_DOUBLE,
-       ST_OTHER
-};
-
-
 void Layout::readSpacing(Lexer & lexrc)
 {
-       keyword_item spacingTags[] = {
+       enum {
+               ST_SPACING_SINGLE = 1,
+               ST_SPACING_ONEHALF,
+               ST_SPACING_DOUBLE,
+               ST_OTHER
+       };
+
+       LexerKeyword spacingTags[] = {
                {"double",  ST_SPACING_DOUBLE },
                {"onehalf", ST_SPACING_ONEHALF },
                {"other",   ST_OTHER },
                {"single",  ST_SPACING_SINGLE }
        };
 
-       PushPopHelper pph(lexrc, spacingTags, ST_OTHER);
+       PushPopHelper pph(lexrc, spacingTags);
        int le = lexrc.lex();
        switch (le) {
        case Lexer::LEX_UNDEF:
@@ -777,7 +783,7 @@ void Layout::readSpacing(Lexer & lexrc)
                return;
        default: break;
        }
-       switch (static_cast<SpacingTags>(le)) {
+       switch (le) {
        case ST_SPACING_SINGLE:
                spacing.set(Spacing::Single);
                break;
@@ -819,6 +825,16 @@ docstring const & Layout::depends_on() const
 }
 
 
+bool Layout::operator==(Layout const & rhs) const
+{
+       // This is enough for the applications we actually make,
+       // at least at the moment. But we could check more.
+       return name() == rhs.name()
+               && latexname() == rhs.latexname()
+               && latextype == rhs.latextype;
+}
+
+
 Layout * Layout::forCaption()
 {
        Layout * lay = new Layout();