Am Donnerstag, 22. März 2007 09:41 schrieb Jean-Marc Lasgouttes:

> OK, here is the still untested second iteration. I introduce a helper
> isNeeded, which is equivalent to "isRequired && ! provided" (if you
> have a better name, please tell). With this change, any feature known
> to LyX can be provided by a class. 
> 
> What needs to be done
> 
> - update layout2layout. I need help there pretty please.
> 
> - actually test it. I do not expect any major problem, since I tried
>   to be conservative.
> 
> - document the changes (trivial).

This is the updated patch with layout2layout support. I needed to change 
one other place as well (compile error due to Martin's recent natbib 
change).


Georg
Index: src/LaTeXFeatures.C
===================================================================
--- src/LaTeXFeatures.C	(Revision 17530)
+++ src/LaTeXFeatures.C	(Arbeitskopie)
@@ -152,6 +152,12 @@ bool LaTeXFeatures::isRequired(string co
 }
 
 
+bool LaTeXFeatures::isNeeded(string const & name) const
+{
+	return isRequired(name) && !params_.getLyXTextClass().provides(name);
+}
+
+
 bool LaTeXFeatures::isAvailable(string const & name)
 {
 	if (packages_.empty())
@@ -267,7 +273,7 @@ string const LaTeXFeatures::getPackages(
 	//  packages which we just \usepackage{package}
 	//
 	for (int i = 0; i < nb_simplefeatures; ++i) {
-		if (isRequired(simplefeatures[i]))
+		if (isNeeded(simplefeatures[i]))
 			packages << "\\usepackage{"
 				 << simplefeatures[i] << "}\n";
 	}
@@ -277,8 +283,7 @@ string const LaTeXFeatures::getPackages(
 	// than those above.
 	//
 
-	if (isRequired("amsmath")
-	    && !tclass.provides(LyXTextClass::amsmath)
+	if (isNeeded("amsmath")
 	    && params_.use_amsmath != BufferParams::package_off) {
 		packages << "\\usepackage{amsmath}\n";
 	}
@@ -290,12 +295,12 @@ string const LaTeXFeatures::getPackages(
 	// esint is used, since esint redefines all relevant integral
 	// symbols from wasysym and amsmath.
 	// See http://bugzilla.lyx.org/show_bug.cgi?id=1942
-	if (isRequired("wasysym") && isRequired("esint") &&
+	if (isNeeded("wasysym") && isRequired("esint") &&
 	    params_.use_esint != BufferParams::package_off)
 		packages << "\\usepackage{wasysym}\n";
 
 	// color.sty
-	if (isRequired("color")) {
+	if (isNeeded("color")) {
 		if (params_.graphicsDriver == "default")
 			packages << "\\usepackage{color}\n";
 		else
@@ -306,13 +311,13 @@ string const LaTeXFeatures::getPackages(
 
 	// makeidx.sty
 	if (isRequired("makeidx")) {
-		if (!tclass.provides(LyXTextClass::makeidx))
+		if (!tclass.provides("makeidx"))
 			packages << "\\usepackage{makeidx}\n";
 		packages << "\\makeindex\n";
 	}
 
 	// graphicx.sty
-	if (isRequired("graphicx") && params_.graphicsDriver != "none") {
+	if (isNeeded("graphicx") && params_.graphicsDriver != "none") {
 		if (params_.graphicsDriver == "default")
 			packages << "\\usepackage{graphicx}\n";
 		else
@@ -321,7 +326,7 @@ string const LaTeXFeatures::getPackages(
 				 << "]{graphicx}\n";
 	}
 	// shadecolor for shaded
-	if (isRequired("framed")) {
+	if (isNeeded("framed")) {
 		RGBColor c = RGBColor(lcolor.getX11Name(LColor::shadedbg));
 		packages << "\\definecolor{shadecolor}{rgb}{" 
 			<< c.r/255 << ',' << c.g/255 << ',' << c.b/255 << "}\n";
@@ -332,7 +337,7 @@ string const LaTeXFeatures::getPackages(
 	//}
 
 	// lyxskak.sty --- newer chess support based on skak.sty
-	if (isRequired("chess")) {
+	if (isNeeded("chess")) {
 		packages << "\\usepackage[ps,mover]{lyxskak}\n";
 	}
 
@@ -361,22 +366,22 @@ string const LaTeXFeatures::getPackages(
 	}
 
 	// amssymb.sty
-	if (isRequired("amssymb") || params_.use_amsmath == BufferParams::package_on)
+	if (isNeeded("amssymb") || params_.use_amsmath == BufferParams::package_on)
 		packages << "\\usepackage{amssymb}\n";
 
 	// esint must be after amsmath and wasysym, since it will redeclare
 	// inconsistent integral symbols
-	if (isRequired("esint") && params_.use_esint != BufferParams::package_off)
+	if (isNeeded("esint") && params_.use_esint != BufferParams::package_off)
 		packages << "\\usepackage{esint}\n";
 
 	// url.sty
-	if (isRequired("url") && ! tclass.provides(LyXTextClass::url))
+	if (isNeeded("url"))
 		packages << "\\IfFileExists{url.sty}{\\usepackage{url}}\n"
 			    "                      {\\newcommand{\\url}{\\texttt}}\n";
 
 	// float.sty
 	// natbib.sty
-	if (isRequired("natbib") && ! tclass.provides(LyXTextClass::natbib)) {
+	if (isNeeded("natbib")) {
 		packages << "\\usepackage[";
 		if (params_.cite_engine == biblio::ENGINE_NATBIB_NUMERICAL) {
 			packages << "numbers";
@@ -387,20 +392,20 @@ string const LaTeXFeatures::getPackages(
 	}
 
 	// jurabib -- we need version 0.6 at least.
-	if (isRequired("jurabib")) {
+	if (isNeeded("jurabib")) {
 		packages << "\\usepackage{jurabib}[2004/01/25]\n";
 	}
 
 	// bibtopic -- the dot provides the aux file naming which
 	// LyX can detect.
-	if (isRequired("bibtopic")) {
+	if (isNeeded("bibtopic")) {
 		packages << "\\usepackage[dot]{bibtopic}\n";
 	}
 
-	if (isRequired("xy"))
+	if (isNeeded("xy"))
 		packages << "\\usepackage[all]{xy}\n";
 
-	if (isRequired("nomencl")) {
+	if (isNeeded("nomencl")) {
 		packages << "\\usepackage{nomencl}[2005/09/22]\n"
 			 << "\\makenomenclature\n";
 	}
@@ -421,55 +426,55 @@ string const LaTeXFeatures::getMacros() 
 		macros << *pit << '\n';
 	}
 
-	if (isRequired("LyX"))
+	if (isNeeded("LyX"))
 		macros << lyx_def << '\n';
 
-	if (isRequired("lyxline"))
+	if (isNeeded("lyxline"))
 		macros << lyxline_def << '\n';
 
-	if (isRequired("noun"))
+	if (isNeeded("noun"))
 		macros << noun_def << '\n';
 
-	if (isRequired("lyxarrow"))
+	if (isNeeded("lyxarrow"))
 		macros << lyxarrow_def << '\n';
 
 	// quotes.
-	if (isRequired("quotesinglbase"))
+	if (isNeeded("quotesinglbase"))
 		macros << quotesinglbase_def << '\n';
-	if (isRequired("quotedblbase"))
+	if (isNeeded("quotedblbase"))
 		macros << quotedblbase_def << '\n';
-	if (isRequired("guilsinglleft"))
+	if (isNeeded("guilsinglleft"))
 		macros << guilsinglleft_def << '\n';
-	if (isRequired("guilsinglright"))
+	if (isNeeded("guilsinglright"))
 		macros << guilsinglright_def << '\n';
-	if (isRequired("guillemotleft"))
+	if (isNeeded("guillemotleft"))
 		macros << guillemotleft_def << '\n';
-	if (isRequired("guillemotright"))
+	if (isNeeded("guillemotright"))
 		macros << guillemotright_def << '\n';
 
 	// Math mode
-	if (isRequired("boldsymbol") && !isRequired("amsmath"))
+	if (isNeeded("boldsymbol") && !isNeeded("amsmath"))
 		macros << boldsymbol_def << '\n';
-	if (isRequired("binom") && !isRequired("amsmath"))
+	if (isNeeded("binom") && !isRequired("amsmath"))
 		macros << binom_def << '\n';
-	if (isRequired("mathcircumflex"))
+	if (isNeeded("mathcircumflex"))
 		macros << mathcircumflex_def << '\n';
 
 	// other
-	if (isRequired("ParagraphLeftIndent"))
+	if (isNeeded("ParagraphLeftIndent"))
 		macros << paragraphleftindent_def;
-	if (isRequired("NeedLyXFootnoteCode"))
+	if (isNeeded("NeedLyXFootnoteCode"))
 		macros << floatingfootnote_def;
 
 	// some problems with tex->html converters
-	if (isRequired("NeedTabularnewline"))
+	if (isNeeded("NeedTabularnewline"))
 		macros << tabularnewline_def;
 
 	// greyedout environment (note inset)
-	if (isRequired("lyxgreyedout"))
+	if (isNeeded("lyxgreyedout"))
 		macros << lyxgreyedout_def;
 
-	if (isRequired("lyxdot"))
+	if (isNeeded("lyxdot"))
 		macros << lyxdot_def << '\n';
 
 	// floats
@@ -525,7 +530,7 @@ docstring const LaTeXFeatures::getLyXSGM
 	// Definition of entities used in the document that are LyX related.
 	odocstringstream entities;
 
-	if (isRequired("lyxarrow")) {
+	if (isNeeded("lyxarrow")) {
 		entities << "<!ENTITY lyxarrow \"-&gt;\">" << '\n';
 	}
 
Index: src/LaTeXFeatures.h
===================================================================
--- src/LaTeXFeatures.h	(Revision 17530)
+++ src/LaTeXFeatures.h	(Arbeitskopie)
@@ -73,8 +73,13 @@ public:
 	static void getAvailable();
 	/// Is the (required) package available?
 	static bool isAvailable(std::string const & name);
-	/// Is the package required?
+	/// Has the package been required?
 	bool isRequired(std::string const & name) const;
+	/* Is it necessary to load the package? This is true is
+	   isRequired is true and the feature is not provided by the
+	   textclass.
+	*/
+	bool isNeeded(std::string const & name) const;
 	///
 	void useFloat(std::string const & name);
 	///
Index: src/buffer.C
===================================================================
--- src/buffer.C	(Revision 17530)
+++ src/buffer.C	(Arbeitskopie)
@@ -1195,7 +1195,7 @@ void Buffer::validate(LaTeXFeatures & fe
 
 	// AMS Style is at document level
 	if (params().use_amsmath == BufferParams::package_on
-	    || tclass.provides(LyXTextClass::amsmath))
+	    || tclass.provides("amsmath"))
 		features.require("amsmath");
 	if (params().use_esint == BufferParams::package_on)
 		features.require("esint");
Index: src/bufferparams.C
===================================================================
--- src/bufferparams.C	(Revision 17530)
+++ src/bufferparams.C	(Arbeitskopie)
@@ -1487,7 +1487,7 @@ biblio::CiteEngine_enum BufferParams::ge
 {
 	// FIXME the class should provide the numerical/
 	// authoryear choice
-	if (getLyXTextClass().provides(LyXTextClass::natbib)
+	if (getLyXTextClass().provides("natbib")
 	    && cite_engine != biblio::ENGINE_NATBIB_NUMERICAL)
 		return biblio::ENGINE_NATBIB_AUTHORYEAR;
 	return cite_engine;
Index: src/lyxtextclass.C
===================================================================
--- src/lyxtextclass.C	(Revision 17530)
+++ src/lyxtextclass.C	(Arbeitskopie)
@@ -65,7 +65,7 @@ private:
 };
 
 
-int const FORMAT = 3;
+int const FORMAT = 4;
 
 
 bool layout2layout(FileName const & filename, FileName const & tempfile)
@@ -113,7 +113,6 @@ LyXTextClass::LyXTextClass(string const 
 	defaultfont_ = LyXFont(LyXFont::ALL_SANE);
 	opt_fontsize_ = "10|11|12";
 	opt_pagestyle_ = "empty|plain|headings|fancy";
-	provides_ = nothing;
 	titletype_ = TITLE_COMMAND_AFTER;
 	titlename_ = "maketitle";
 	loaded_ = false;
@@ -158,10 +157,7 @@ enum TextClassTags {
 	TC_TOCDEPTH,
 	TC_CLASSOPTIONS,
 	TC_PREAMBLE,
-	TC_PROVIDESAMSMATH,
-	TC_PROVIDESNATBIB,
-	TC_PROVIDESMAKEIDX,
-	TC_PROVIDESURL,
+	TC_PROVIDES,
 	TC_LEFTMARGIN,
 	TC_RIGHTMARGIN,
 	TC_FLOAT,
@@ -199,10 +195,7 @@ bool LyXTextClass::read(FileName const &
 		{ "outputtype",      TC_OUTPUTTYPE },
 		{ "pagestyle",       TC_PAGESTYLE },
 		{ "preamble",        TC_PREAMBLE },
-		{ "providesamsmath", TC_PROVIDESAMSMATH },
-		{ "providesmakeidx", TC_PROVIDESMAKEIDX },
-		{ "providesnatbib",  TC_PROVIDESNATBIB },
-		{ "providesurl",     TC_PROVIDESURL },
+		{ "provides",        TC_PROVIDES },
 		{ "rightmargin",     TC_RIGHTMARGIN },
 		{ "secnumdepth",     TC_SECNUMDEPTH },
 		{ "sides",           TC_SIDES },
@@ -383,24 +376,9 @@ bool LyXTextClass::read(FileName const &
 			preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
 			break;
 
-		case TC_PROVIDESAMSMATH:
-			if (lexrc.next() && lexrc.getInteger())
-				provides_ |= amsmath;
-			break;
-
-		case TC_PROVIDESNATBIB:
-			if (lexrc.next() && lexrc.getInteger())
-				provides_ |= natbib;
-			break;
-
-		case TC_PROVIDESMAKEIDX:
-			if (lexrc.next() && lexrc.getInteger())
-				provides_ |= makeidx;
-			break;
-
-		case TC_PROVIDESURL:
-			if (lexrc.next() && lexrc.getInteger())
-				provides_ |= url;
+		case TC_PROVIDES:
+			if (lexrc.next())
+				provides_.insert(lexrc.getString());
 			break;
 
 		case TC_LEFTMARGIN:	// left margin type
@@ -1067,9 +1045,9 @@ OutputType LyXTextClass::outputType() co
 }
 
 
-bool LyXTextClass::provides(LyXTextClass::Provides p) const
+bool LyXTextClass::provides(string const & p) const
 {
-	return provides_ & p;
+	return provides_.find(p) != provides_.end();
 }
 
 
Index: src/lyxtextclass.h
===================================================================
--- src/lyxtextclass.h	(Revision 17530)
+++ src/lyxtextclass.h	(Arbeitskopie)
@@ -16,7 +16,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include <vector>
-
+#include <set>
 
 namespace lyx {
 
@@ -126,21 +126,8 @@ public:
 	///
 	docstring const & preamble() const;
 
-	/// Packages that are already loaded by the class
-	enum Provides {
-		///
-		nothing = 0,
-		///
-		amsmath = 1,
-		///
-		makeidx = 2,
-		///
-		url = 4,
-		///
-		natbib = 8
-	};
-	///
-	bool provides(Provides p) const;
+	/// is this feature already provided by the class?
+	bool provides(std::string const & p) const;
 
 	///
 	unsigned int columns() const;
@@ -209,7 +196,7 @@ private:
 	/// preamble text to support layout styles
 	docstring preamble_;
 	/// latex packages loaded by document class.
-	Provides provides_;
+	std::set<std::string> provides_;
 	///
 	unsigned int columns_;
 	///
@@ -261,14 +248,6 @@ private:
 };
 
 
-/// Merge two different provides flags into one bit field record
-inline
-void operator|=(LyXTextClass::Provides & p1, LyXTextClass::Provides p2)
-{
-	p1 = static_cast<LyXTextClass::Provides>(p1 | p2);
-}
-
-
 /// convert page sides option to text 1 or 2
 std::ostream & operator<<(std::ostream & os, LyXTextClass::PageSides p);
 
Index: lib/scripts/layout2layout.py
===================================================================
--- lib/scripts/layout2layout.py	(Revision 17531)
+++ lib/scripts/layout2layout.py	(Arbeitskopie)
@@ -79,6 +79,7 @@ def convert(lines):
     re_CopyStyle = re.compile(r'^(\s*)(CopyStyle)(\s+)(\S+)', re.IGNORECASE)
     re_NoStyle = re.compile(r'^(\s*)(NoStyle)(\s+)(\S+)', re.IGNORECASE)
     re_End = re.compile(r'^(\s*)(End)(\s*)$', re.IGNORECASE)
+    re_Provides = re.compile(r'^(\s*)Provides(\S+)(\s+)(\S+)', re.IGNORECASE)
 
     # counters for sectioning styles (hardcoded in 1.3)
     counters = {"part"          : "\\Roman{part}",
@@ -133,10 +134,10 @@ def convert(lines):
                 match = re_Format.match(lines[i])
                 if match:
                         format = int(match.group(4))
-                        if format == 2:
-                            lines[i] = "Format 3"
+                        if format > 1 and format < 4:
+                            lines[i] = "Format %d" % (format + 1)
                             only_comment = 0
-                        elif format == 3:
+                        elif format == 4:
                                 # nothing to do
                                 return format
                         else:
@@ -153,6 +154,17 @@ def convert(lines):
                 i = i + 1
             continue
 
+        if format == 3:
+            # convert 'providesamsmath x',  'providesmakeidx x',  'providesnatbib x',  'providesurl x' to
+            #         'provides amsmath x', 'provides makeidx x', 'provides natbib x', 'provides url x'
+            # x is either 0 or 1
+            match = re_Provides.match(lines[i])
+            if match:
+                lines[i] = "%sProvides %s%s%s" % (match.group(1), match.group(2),
+                                                  match.group(3), match.group(4))
+            i = i + 1
+            continue
+
         if format == 2:
             caption = []
 
@@ -370,7 +382,7 @@ def main(argv):
     # Do the real work
     lines = read(input)
     format = 1
-    while (format < 3):
+    while (format < 4):
         format = convert(lines)
     write(output, lines)
 

Reply via email to