The patch replaces the bool dryrun() in mathed's write stream by a triple enum 
output() that has wsDefault, wsDryrun and wsPreview. Thus, we know whether a 
write stream is used for instant preview, and we can catch and handle encoding 
exceptions gracefully (currently, it does just skip problematic strings, issue 
a warning on the console and produce empty previews, but we can change that, 
if someone has a better idea).

The patch is against branch.

Comments, objections?

Jürgen
Index: src/mathed/InsetMathHull.cpp
===================================================================
--- src/mathed/InsetMathHull.cpp	(Revision 30866)
+++ src/mathed/InsetMathHull.cpp	(Arbeitskopie)
@@ -423,7 +423,7 @@
 		InsetMathGrid::metricsT(mi, dim);
 	} else {
 		odocstringstream os;
-		WriteStream wi(os, false, true, false);
+		WriteStream wi(os, false, true, WriteStream::wsDefault);
 		write(wi);
 		dim.wid = os.str().size();
 		dim.asc = 1;
@@ -438,7 +438,7 @@
 		InsetMathGrid::drawT(pain, x, y);
 	} else {
 		odocstringstream os;
-		WriteStream wi(os, false, true, false);
+		WriteStream wi(os, false, true, WriteStream::wsDefault);
 		write(wi);
 		pain.draw(x, y, os.str().c_str());
 	}
@@ -456,7 +456,7 @@
 	static Encoding const * encoding = 0;
 	if (inset.isBufferValid())
 		encoding = &(inset.buffer().params().encoding());
-	WriteStream wi(ls, false, true, false, encoding);
+	WriteStream wi(ls, false, true, WriteStream::wsPreview, encoding);
 	inset.write(wi);
 	return ls.str();
 }
@@ -1596,7 +1596,7 @@
 void InsetMathHull::write(ostream & os) const
 {
 	odocstringstream oss;
-	WriteStream wi(oss, false, false, false);
+	WriteStream wi(oss, false, false, WriteStream::wsDefault);
 	oss << "Formula ";
 	write(wi);
 	os << to_utf8(oss.str());
@@ -1634,7 +1634,7 @@
 		return tpain.textheight();
 	} else {
 		odocstringstream oss;
-		WriteStream wi(oss, false, true, false, runparams.encoding);
+		WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding);
 		wi << cell(0);
 
 		docstring const str = oss.str();
@@ -1666,7 +1666,7 @@
 		// Workaround for db2latex: db2latex always includes equations with
 		// \ensuremath{} or \begin{display}\end{display}
 		// so we strip LyX' math environment
-		WriteStream wi(ls, false, false, false, runparams.encoding);
+		WriteStream wi(ls, false, false, WriteStream::wsDefault, runparams.encoding);
 		InsetMathGrid::write(wi);
 		ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
 		ms << ETag("alt");
Index: src/mathed/InsetMathString.cpp
===================================================================
--- src/mathed/InsetMathString.cpp	(Revision 30866)
+++ src/mathed/InsetMathString.cpp	(Arbeitskopie)
@@ -16,6 +16,7 @@
 
 #include "Encoding.h"
 
+#include "support/debug.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/textutils.h"
@@ -159,14 +160,22 @@
 			    && isAlphaASCII(command[command.size() - 1]))
 				os.pendingSpace(true);
 		} catch (EncodingException & e) {
-			if (os.dryrun()) {
-				// FIXME: this is OK for View->Source
-				// but math preview will likely fail.
+			switch (os.output()) {
+			case WriteStream::wsDryrun: {
 				os << "<" << _("LyX Warning: ")
 				   << _("uncodable character") << " '";
 				os << docstring(1, e.failed_char);
 				os << "'>";
-			} else {
+				break;
+			}
+			case WriteStream::wsPreview: {
+				LYXERR0("Uncodable character" << " '"
+					<< docstring(1, e.failed_char)
+					<< "'. Preview skipped.");
+				break;
+			}
+			case WriteStream::wsDefault:
+			default:
 				// throw again
 				throw(e);
 			}
Index: src/mathed/InsetFormulaMacro.cpp
===================================================================
--- src/mathed/InsetFormulaMacro.cpp	(Revision 30866)
+++ src/mathed/InsetFormulaMacro.cpp	(Arbeitskopie)
@@ -65,7 +65,7 @@
 void InsetFormulaMacro::write(ostream & os) const
 {
 	os << "FormulaMacro\n";
-	WriteStream wi(os, false, false, false);
+	WriteStream wi(os, false, false, WriteStream::wsDefault);
 	tmpl()->write(wi);
 }
 
@@ -74,8 +74,9 @@
 			     OutputParams const & runparams) const
 {
 	//lyxerr << "InsetFormulaMacro::latex" << endl;
-	WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
-			runparams.encoding);
+	WriteStream wi(os, runparams.moving_arg, true,
+		       runparams.dryrun ? WriteStream::wsDryrun: WriteStream::wsDefault,
+		       runparams.encoding);
 	tmpl()->write(wi);
 	return 2;
 }
@@ -84,7 +85,7 @@
 int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & runparams) const
 {
 	odocstringstream oss;
-	WriteStream wi(oss, false, true, false, runparams.encoding);
+	WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding);
 	tmpl()->write(wi);
 
 	docstring const str = oss.str();
Index: src/mathed/MathStream.cpp
===================================================================
--- src/mathed/MathStream.cpp	(Revision 30866)
+++ src/mathed/MathStream.cpp	(Arbeitskopie)
@@ -107,17 +107,17 @@
 }
 
 
-WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun,
+WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, OutputType output,
 			Encoding const * encoding)
 	: os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
-	  dryrun_(dryrun), pendingspace_(false), pendingbrace_(false),
+	  output_(output), pendingspace_(false), pendingbrace_(false),
 	  textmode_(false), locked_(0), line_(0), encoding_(encoding)
 {}
 
 
 WriteStream::WriteStream(odocstream & os)
 	: os_(os), fragile_(false), firstitem_(false), latex_(false),
-	  dryrun_(false), pendingspace_(false), pendingbrace_(false),
+	  output_(wsDefault), pendingspace_(false), pendingbrace_(false),
 	  textmode_(false), locked_(0), line_(0), encoding_(0)
 {}
 
Index: src/mathed/MathMacroTemplate.cpp
===================================================================
--- src/mathed/MathMacroTemplate.cpp	(Revision 30866)
+++ src/mathed/MathMacroTemplate.cpp	(Arbeitskopie)
@@ -1118,7 +1118,7 @@
 void MathMacroTemplate::write(ostream & os) const
 {
 	odocstringstream oss;
-	WriteStream wi(oss, false, false, false);
+	WriteStream wi(oss, false, false, WriteStream::wsDefault);
 	oss << "FormulaMacro\n";
 	write(wi);
 	os << to_utf8(oss.str());
Index: src/mathed/InsetMathNest.cpp
===================================================================
--- src/mathed/InsetMathNest.cpp	(Revision 30866)
+++ src/mathed/InsetMathNest.cpp	(Arbeitskopie)
@@ -364,8 +364,9 @@
 
 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
 {
-	WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
-			runparams.encoding);
+	WriteStream wi(os, runparams.moving_arg, true,
+		       runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
+		       runparams.encoding);
 	write(wi);
 	return wi.line();
 }
Index: src/mathed/InsetMath.cpp
===================================================================
--- src/mathed/InsetMath.cpp	(Revision 30866)
+++ src/mathed/InsetMath.cpp	(Arbeitskopie)
@@ -47,7 +47,7 @@
 {
 	lyxerr << "---------------------------------------------" << endl;
 	odocstringstream os;
-	WriteStream wi(os, false, true, false);
+	WriteStream wi(os, false, true, WriteStream::wsDefault);
 	write(wi);
 	lyxerr << to_utf8(os.str());
 	lyxerr << "\n---------------------------------------------" << endl;
@@ -136,7 +136,7 @@
 ostream & operator<<(ostream & os, MathAtom const & at)
 {
 	odocstringstream oss;
-	WriteStream wi(oss, false, false, false);
+	WriteStream wi(oss, false, false, WriteStream::wsDefault);
 	at->write(wi);
 	return os << to_utf8(oss.str());
 }
@@ -144,7 +144,7 @@
 
 odocstream & operator<<(odocstream & os, MathAtom const & at)
 {
-	WriteStream wi(os, false, false, false);
+	WriteStream wi(os, false, false, WriteStream::wsDefault);
 	at->write(wi);
 	return os;
 }
Index: src/mathed/MacroTable.cpp
===================================================================
--- src/mathed/MacroTable.cpp	(Revision 30866)
+++ src/mathed/MacroTable.cpp	(Arbeitskopie)
@@ -164,7 +164,7 @@
 	// output template
 	MathMacroTemplate const & tmpl =
 		static_cast<MathMacroTemplate const &>(*inset);
-	WriteStream wi(os, false, true, false);
+	WriteStream wi(os, false, true, WriteStream::wsDefault);
 	tmpl.write(wi, overwriteRedefinition);
 }
 
Index: src/mathed/MathStream.h
===================================================================
--- src/mathed/MathStream.h	(Revision 30866)
+++ src/mathed/MathStream.h	(Arbeitskopie)
@@ -33,7 +33,13 @@
 class WriteStream {
 public:
 	///
-	WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun,
+	enum OutputType {
+		wsDefault,
+		wsDryrun,
+		wsPreview
+	};
+	///
+	WriteStream(odocstream & os, bool fragile, bool latex, OutputType output,
 		Encoding const * encoding = 0);
 	///
 	explicit WriteStream(odocstream & os);
@@ -46,7 +52,7 @@
 	///
 	bool latex() const { return latex_; }
 	///
-	bool dryrun() const { return dryrun_; }
+	OutputType output() const { return output_; }
 	///
 	odocstream & os() { return os_; }
 	///
@@ -80,8 +86,8 @@
 	bool firstitem_;
 	/// are we writing to .tex?
 	int latex_;
-	/// is it for preview?
-	bool dryrun_;
+	/// is it for source preview?
+	OutputType output_;
 	/// do we have a space pending?
 	bool pendingspace_;
 	/// do we have a brace pending?
Index: src/Cursor.cpp
===================================================================
--- src/Cursor.cpp	(Revision 30866)
+++ src/Cursor.cpp	(Arbeitskopie)
@@ -1607,7 +1607,7 @@
 			<< pos() << ' ' << lastpos() <<  " in idx: " << idx()
 		       << " in atom: '";
 		odocstringstream os;
-		WriteStream wi(os, false, true, false);
+		WriteStream wi(os, false, true, WriteStream::wsDefault);
 		inset().asInsetMath()->write(wi);
 		lyxerr << to_utf8(os.str()) << endl;
 		pos() = lastpos();

Reply via email to