>>>>> "José" == José Matos <[EMAIL PROTECTED]> writes:

José> On Tuesday 20 March 2007 9:23:56 am Jean-Marc Lasgouttes wrote:
>>  One solution would be to define a isNeeded() for this use case,
>> and still use isRequired in the few cases where this makes sense.

José>   The patch seems quite interesting indeed. I think that it is
José> not yet ready to beta 2 but I would to see it (and to help
José> testing it) for a later release.

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).

JMarc 

Index: src/LaTeXFeatures.C
===================================================================
--- src/LaTeXFeatures.C	(révision 17488)
+++ src/LaTeXFeatures.C	(copie de travail)
@@ -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	(révision 17488)
+++ src/LaTeXFeatures.h	(copie de travail)
@@ -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	(révision 17488)
+++ src/buffer.C	(copie de travail)
@@ -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/lyxtextclass.C
===================================================================
--- src/lyxtextclass.C	(révision 17488)
+++ src/lyxtextclass.C	(copie de travail)
@@ -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	(révision 17488)
+++ src/lyxtextclass.h	(copie de travail)
@@ -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);
 

Reply via email to