From c35cfda7274993f3d5a48f6f862be987834f99ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Spitzm=C3=BCller?= Date: Fri, 7 Aug 2009 05:24:57 +0000 Subject: [PATCH] Catch encoding exception when performing instant preview on an uncodable char (bug 6116). * MathStream{.cpp, h}: - replace bool dryrun() by enum output that also knows whether the stream is for instant preview * InsetMathHull.cpp: - tell the stream whether we use it for instant preview. * MathString.cpp (write): - gracefully catch encoding exception for instant preview. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/branches/BRANCH_1_6_X@30894 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Cursor.cpp | 2 +- src/mathed/InsetFormulaMacro.cpp | 9 +++++---- src/mathed/InsetMath.cpp | 6 +++--- src/mathed/InsetMathHull.cpp | 12 ++++++------ src/mathed/InsetMathNest.cpp | 5 +++-- src/mathed/InsetMathString.cpp | 20 ++++++++++++++++---- src/mathed/MacroTable.cpp | 2 +- src/mathed/MathMacroTemplate.cpp | 2 +- src/mathed/MathStream.cpp | 6 +++--- src/mathed/MathStream.h | 14 ++++++++++---- status.16x | 3 +++ 11 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index ec2781a5de..e7f1f22f42 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1607,7 +1607,7 @@ void Cursor::normalize() << 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(); diff --git a/src/mathed/InsetFormulaMacro.cpp b/src/mathed/InsetFormulaMacro.cpp index b38f2619cc..b48b6fe34b 100644 --- a/src/mathed/InsetFormulaMacro.cpp +++ b/src/mathed/InsetFormulaMacro.cpp @@ -65,7 +65,7 @@ Inset * InsetFormulaMacro::clone() const 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 @@ int InsetFormulaMacro::latex(odocstream & os, 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::latex(odocstream & os, 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(); diff --git a/src/mathed/InsetMath.cpp b/src/mathed/InsetMath.cpp index fd8994652d..457279a7a2 100644 --- a/src/mathed/InsetMath.cpp +++ b/src/mathed/InsetMath.cpp @@ -47,7 +47,7 @@ void InsetMath::dump() const { 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 @@ HullType InsetMath::getType() const 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 @@ ostream & operator<<(ostream & os, MathAtom const & at) 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; } diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index ecabd691e0..305f39f9cc 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -423,7 +423,7 @@ void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const 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 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const 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 docstring latexString(InsetMathHull const & inset) 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 @@ bool InsetMathHull::searchForward(BufferView * bv, string const & str, 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 @@ int InsetMathHull::plaintext(odocstream & os, OutputParams const & runparams) co 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 @@ int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) cons // 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()), "&", "&"), "<", "<")); ms << ETag("alt"); diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 1687acec1f..44aac80150 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -364,8 +364,9 @@ void InsetMathNest::normalize(NormalStream & os) const 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(); } diff --git a/src/mathed/InsetMathString.cpp b/src/mathed/InsetMathString.cpp index 6e4c99ee70..b0ab513852 100644 --- a/src/mathed/InsetMathString.cpp +++ b/src/mathed/InsetMathString.cpp @@ -11,11 +11,13 @@ #include #include "InsetMathString.h" +#include "MathFactory.h" #include "MathStream.h" #include "MathSupport.h" #include "Encoding.h" +#include "support/debug.h" #include "support/gettext.h" #include "support/lstrings.h" #include "support/textutils.h" @@ -159,14 +161,24 @@ void InsetMathString::write(WriteStream & os) const && 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: { + // indicate the encoding error by a boxed '?' + os << "{\\fboxsep=1pt\\fbox{?}}";; + LYXERR0("Uncodable character" << " '" + << docstring(1, e.failed_char) + << "'"); + break; + } + case WriteStream::wsDefault: + default: // throw again throw(e); } diff --git a/src/mathed/MacroTable.cpp b/src/mathed/MacroTable.cpp index f0de0588f4..59b13c7cd5 100644 --- a/src/mathed/MacroTable.cpp +++ b/src/mathed/MacroTable.cpp @@ -164,7 +164,7 @@ void MacroData::write(odocstream & os, bool overwriteRedefinition) const // output template MathMacroTemplate const & tmpl = static_cast(*inset); - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); tmpl.write(wi, overwriteRedefinition); } diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp index f27c8cdeff..0ca5d36ade 100644 --- a/src/mathed/MathMacroTemplate.cpp +++ b/src/mathed/MathMacroTemplate.cpp @@ -1118,7 +1118,7 @@ void MathMacroTemplate::read(Lexer & lex) 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()); diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index f6100f06cd..349d9d7df4 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -107,17 +107,17 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s) } -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) {} diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index 2ab8f7f713..9b69b62a62 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -33,7 +33,13 @@ class MathData; 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 @@ public: /// bool latex() const { return latex_; } /// - bool dryrun() const { return dryrun_; } + OutputType output() const { return output_; } /// odocstream & os() { return os_; } /// @@ -80,8 +86,8 @@ private: bool firstitem_; /// are we writing to .tex? int latex_; - /// is it for preview? - bool dryrun_; + /// output type (default, source preview, instant preview)? + OutputType output_; /// do we have a space pending? bool pendingspace_; /// do we have a brace pending? diff --git a/status.16x b/status.16x index e3df138fb2..361f1deb10 100644 --- a/status.16x +++ b/status.16x @@ -182,6 +182,9 @@ What's new - Fix a crash when accessing the Navigate menu after an empty inset has been dissolved (bug 6003). + +- Fix a crash when performing instant preview on an uncodable character + (bug 6116). - Fix communication between lyxclient and server, which leads to a crash once CTRL+D is used on the client side (bug 6106).