This time with the diff....

---

In thinking about Paul Rubin's new math modules, it occurred to me that it would be nice to have some conditionals in the layout files. Attached is a patch that adds IfStyle and IfCounter tags. The effect of these is to act as normal Style and Counter tags do, if the style or counter already exists; otherwise, the contents is read and discarded, as if it were a big comment block.

So one can do things like this:

IfStyle Axiom
    ...
End

and thereby modify the style, but only if it exists. This will be useful in modifying math environments: In some cases, we do not know exactly what styles we have.

I think this will be sufficient for actual needs. But it's an obvious question whether we should do something more general. I'd also appreciate comments on the way I've done this.

By the way: Shouldn't Counters::newCounter() check whether the counter already exists before it creates a new one?

Richard

Index: src/TextClass.cpp
===================================================================
--- src/TextClass.cpp   (revision 30873)
+++ src/TextClass.cpp   (working copy)
@@ -62,7 +62,7 @@
 };
 
 // Keep the changes documented in the Customization manual. 
-int const FORMAT = 16;
+int const FORMAT = 17;
 
 
 bool layout2layout(FileName const & filename, FileName const & tempfile)
@@ -164,6 +164,7 @@
        TC_OUTPUTFORMAT,
        TC_INPUT,
        TC_STYLE,
+       TC_IFSTYLE,
        TC_DEFAULTSTYLE,
        TC_INSETLAYOUT,
        TC_NOSTYLE,
@@ -182,6 +183,7 @@
        TC_RIGHTMARGIN,
        TC_FLOAT,
        TC_COUNTER,
+       TC_IFCOUNTER,
        TC_NOFLOAT,
        TC_TITLELATEXNAME,
        TC_TITLELATEXTYPE,
@@ -209,6 +211,8 @@
                { "float",             TC_FLOAT },
                { "format",            TC_FORMAT },
                { "htmlpreamble",      TC_HTMLPREAMBLE },
+               { "ifcounter",         TC_IFCOUNTER },
+               { "ifstyle",           TC_IFSTYLE },
                { "input",             TC_INPUT },
                { "insetlayout",       TC_INSETLAYOUT },
                { "leftmargin",        TC_LEFTMARGIN },
@@ -352,6 +356,10 @@
                        break;
                }
 
+               // used below to track whether we are in an IfStyle or 
IfCounter tag.
+               bool ifstyle    = false;
+               bool ifcounter  = false;
+
                switch (static_cast<TextClassTags>(le)) {
 
                case TC_FORMAT:
@@ -404,6 +412,9 @@
                        }
                        break;
 
+               case TC_IFSTYLE:
+                       ifstyle = true;
+                       // fall through
                case TC_STYLE: {
                        if (!lexrc.next()) {
                                lexrc.printError("No name given for style: 
`$$Token'.");
@@ -423,7 +434,7 @@
                        } else if (hasLayout(name)) {
                                Layout & lay = operator[](name);
                                error = !readStyle(lexrc, lay);
-                       } else {
+                       } else if (!ifstyle) {
                                Layout layout;
                                layout.setName(name);
                                error = !readStyle(lexrc, layout);
@@ -436,6 +447,15 @@
                                        defaultlayout_ = name;
                                }
                        }
+                       else {
+                               // scan the rest and discard it
+                               Layout lay;
+                               readStyle(lexrc, lay);
+                               error = false;
+                       }
+
+                       // reset flag
+                       ifstyle = false;
                        break;
                }
 
@@ -605,6 +625,8 @@
                        readFloat(lexrc);
                        break;
 
+               case TC_IFCOUNTER:
+                       ifcounter = true;
                case TC_COUNTER:
                        if (lexrc.next()) {
                                docstring const name = lexrc.getDocString();
@@ -617,12 +639,14 @@
                                        // and discard it.
                                        c.read(lexrc);
                                } else
-                                       error = !counters_.read(lexrc, name);
+                                       error = !counters_.read(lexrc, name, 
!ifcounter);
                        }
                        else {
                                lexrc.printError("No name given for style: 
`$$Token'.");
                                error = true;
                        }
+                       // reset flag
+                       ifcounter = false;
                        break;
 
                case TC_TITLELATEXTYPE:
Index: src/Counters.cpp
===================================================================
--- src/Counters.cpp    (revision 30873)
+++ src/Counters.cpp    (working copy)
@@ -171,18 +171,20 @@
 }
 
 
-bool Counters::read(Lexer & lex, docstring const & name)
+bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
 {
        if (hasCounter(name)) {
                LYXERR(Debug::TCLASS, "Reading existing counter " << 
to_utf8(name));
                return counterList_[name].read(lex);
        }
+
        LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
        Counter cnt;
        bool success = cnt.read(lex);
-       if (success)
+       // if makenew is false, we will just discard what we read
+       if (success && makenew)
                counterList_[name] = cnt;
-       else
+       else if (!success)
                LYXERR0("Error reading counter `" << name << "'!");
        return success;
 }
Index: src/Counters.h
===================================================================
--- src/Counters.h      (revision 30873)
+++ src/Counters.h      (working copy)
@@ -99,8 +99,10 @@
        /// Checks whether the given counter exists.
        bool hasCounter(docstring const & c) const;
        /// reads the counter name
+       /// \param makeNew whether to make a new counter if one 
+       ///        doesn't already exist
        /// \return true on success
-       bool read(Lexer & lex, docstring const & name);
+       bool read(Lexer & lex, docstring const & name, bool makenew);
        ///
        void set(docstring const & ctr, int val);
        ///
Index: lib/scripts/layout2layout.py
===================================================================
--- lib/scripts/layout2layout.py        (revision 30873)
+++ lib/scripts/layout2layout.py        (working copy)
@@ -67,7 +67,7 @@
 # Do not forget to document format change in Customization
 # Manual (section "Declaring a new text class").
 
-currentFormat = 16
+currentFormat = 17
 
 
 def usage(prog_name):
@@ -256,7 +256,7 @@
             continue
 
         # This just involved new features, not any changes to old ones
-        if format == 14 or format == 15:
+        if format == 14 or format == 15 or format == 16:
           i += 1
           continue
 

Reply via email to