Take this patch with better code documentation instead.

regards Uwe
Index: preamble.cpp
===================================================================
--- preamble.cpp	(revision 21925)
+++ preamble.cpp	(working copy)
@@ -3,7 +3,8 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
+ * \author Uwe Stöhr
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -72,7 +73,7 @@
 string h_textclass               = "article";
 string h_options                 = string();
 string h_language                = "english";
-string h_inputencoding           = "latin1";
+string h_inputencoding           = "auto";
 string h_fontscheme              = "default";
 string h_graphics                = "default";
 string h_paperfontsize           = "default";
@@ -100,14 +101,26 @@
 	if (opts.empty())
 		return;
 
+	// trim spaces around the options because this is valid LaTeX:
+	// "12pt, bulgarian"
+	// otherwise the option would be " bulgarian" (with a leading space)
+	// and thus not be found in the next routines
+	vector<string>::iterator it;
+	for (it = opts.begin(); it != opts.end(); ++it)
+		*it = trim(*it, " ");
+
+	// the last language option is the document language (for babel and LyX)
+	// the last size option is the document font size
+	vector<string>::iterator position = opts.begin();
 	for (; *what; ++what) {
-		vector<string>::iterator it = find(opts.begin(), opts.end(), *what);
-		if (it != opts.end()) {
-			//cerr << "### found option '" << *what << "'\n";
+		it = find(opts.begin(), opts.end(), *what);
+		if (it >= position && it != opts.end()) {
 			target = *what;
+			position = it;
+		}
+		// remove found options from the list
+		if (it != opts.end())
 			opts.erase(it);
-			return;
-		}
 	}
 }
 
@@ -173,6 +186,7 @@
 {
 	vector<string> options = split_options(opts);
 	add_package(name, options);
+	size_t pos;
 
 	//cerr << "handle_package: '" << name << "'\n";
 	if (name == "ae")
@@ -188,7 +202,10 @@
 	else if (name == "fontenc")
 		; // ignore this
 	else if (name == "inputenc") {
-		h_inputencoding = opts;
+		// only set when there is not more than one inputenc option
+		// therefore check for the "," character
+		if ((pos = opts.find(",", 0)) == string::npos)
+			h_inputencoding = opts;
 		options.clear();
 	} else if (name == "makeidx")
 		; // ignore this
@@ -237,14 +254,15 @@
 
 void end_preamble(ostream & os, TextClass const & /*textclass*/)
 {
-	os << "#LyX file created by  tex2lyx 0.1.2\n"
+	os << "#LyX file created by  tex2lyx 0.1.3\n"
 	   << "\\lyxformat 245\n"
 	   << "\\begin_document\n"
 	   << "\\begin_header\n"
-	   << "\\textclass " << h_textclass << "\n"
-	   << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
+	   << "\\textclass " << h_textclass << "\n";
+	if (!h_preamble.str().empty())
+		os << "\\begin_preamble\n" << h_preamble.str() << "\n\\end_preamble\n";
 	if (!h_options.empty())
-	   os << "\\options " << h_options << "\n";
+		os << "\\options " << h_options << "\n";
 	os << "\\language " << h_language << "\n"
 	   << "\\inputencoding " << h_inputencoding << "\n"
 	   << "\\fontscheme " << h_fontscheme << "\n"
Index: text.cpp
===================================================================
--- text.cpp	(revision 21925)
+++ text.cpp	(working copy)
@@ -5,6 +5,7 @@
  *
  * \author André Pönitz
  * \author Jean-Marc Lasgouttes
+ * \author Uwe Stöhr
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -1125,6 +1126,8 @@
 		Context & context)
 {
 	LayoutPtr newlayout;
+	// store the current selectlanguage to be used after \foreignlanguage
+	string selectlang;
 	// Store the latest bibliographystyle (needed for bibtex inset)
 	string bibliographystyle;
 	bool const use_natbib = used_packages.find("natbib") != used_packages.end();
@@ -2051,6 +2054,27 @@
 			eat_whitespace(p, os, context, false);
 		}
 
+		else if (t.cs() == "selectlanguage") {
+			context.check_layout(os);
+			// save the language for the case that a \foreignlanguage is used 
+			selectlang = subst(p.verbatim_item(), "\n", " ");
+			os << "\\lang " << selectlang << "\n";
+			
+		}
+
+		else if (t.cs() == "foreignlanguage") {
+			context.check_layout(os);
+			os << "\n\\lang " << subst(p.verbatim_item(), "\n", " ") << "\n";
+			os << subst(p.verbatim_item(), "\n", " ");
+			// set back to last selectlanguage
+			os << "\n\\lang " << selectlang << "\n";
+		}
+
+		else if (t.cs() == "inputencoding")
+			// write nothing because this is done by LyX using the "\lang"
+			// information given by selectlanguage and foreignlanguage
+			subst(p.verbatim_item(), "\n", " ");
+		
 		else if (t.cs() == "LyX" || t.cs() == "TeX"
 			 || t.cs() == "LaTeX") {
 			context.check_layout(os);

Reply via email to