]> git.lyx.org Git - features.git/commitdiff
Use ErrorList to handle encoding error
authorBo Peng <bpeng@lyx.org>
Tue, 18 Dec 2007 17:51:20 +0000 (17:51 +0000)
committerBo Peng <bpeng@lyx.org>
Tue, 18 Dec 2007 17:51:20 +0000 (17:51 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22208 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/Encoding.cpp
src/Encoding.h
src/Paragraph.cpp

index 91f977944bc118dbfba08b2e75aebd6a2dcde646..b67c644bb99ba879a7e416f2ebe6c9f7c2d4fbb0 100644 (file)
@@ -999,19 +999,31 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
                return false;
 
        //TexStream ts(ofs.rdbuf(), &texrow());
-
+       ErrorList & errorList = d->errorLists["Export"];
+       errorList.clear();
        bool failed_export = false;
        try {
                d->texrow.reset();
                writeLaTeXSource(ofs, original_path,
                      runparams, output_preamble, output_body);
        }
+       catch (EncodingException & e) {
+               docstring msg = _("Could not find LaTeX command for character '%'");
+               msg[msg.size() - 2] = e.failed_char;
+               errorList.push_back(ErrorItem(msg, _("Some characters of your document are probably not "
+                               "representable in the chosen encoding.\n"
+                               "Changing the document encoding to utf8 could help."),
+                               e.par_id, e.pos, e.pos + 1));
+               failed_export = true;                   
+       }
        catch (iconv_codecvt_facet_exception & e) {
-               lyxerr << "Caught iconv exception: " << e.what() << endl;
+               errorList.push_back(ErrorItem(_("iconv conversion failed"),
+                       _(e.what()), -1, 0, 0));
                failed_export = true;
        }
        catch (exception const & e) {
-               lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
+               errorList.push_back(ErrorItem(_("conversion failed"),
+                       _(e.what()), -1, 0, 0));
                failed_export = true;
        }
        catch (...) {
@@ -1025,14 +1037,8 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
                lyxerr << "File '" << fname << "' was not closed properly." << endl;
        }
 
-       if (failed_export) {
-               Alert::error(_("Encoding error"),
-                       _("Some characters of your document are probably not "
-                       "representable in the chosen encoding.\n"
-                       "Changing the document encoding to utf8 could help."));
-               return false;
-       }
-       return true;
+       errors("Export");
+       return !failed_export;
 }
 
 
index 248bdae6bcf97bcd8e7d3829036ca8444cb3e881..71ea8c63e49a944691d5c760bbbc2c7cfa372bbd 100644 (file)
@@ -247,6 +247,18 @@ char_type const max_ucs4 = 0x110000;
 } // namespace anon
 
 
+EncodingException::EncodingException(char_type c)
+       : failed_char(c), par_id(0), pos(0)
+{
+}
+
+
+const char * EncodingException::what() const throw()
+{
+       return "Could not find LaTeX command for a character";
+}
+
+
 Encoding::Encoding(string const & n, string const & l, string const & i,
                   bool f, Encoding::Package p)
        : Name_(n), LatexName_(l), iconvName_(i), fixedwidth_(f), package_(p)
@@ -321,10 +333,7 @@ docstring const Encoding::latexChar(char_type c) const
                // c cannot be encoded in this encoding
                CharInfoMap::const_iterator const it = unicodesymbols.find(c);
                if (it == unicodesymbols.end())
-                       lyxerr << "Could not find LaTeX command for character 0x"
-                              << hex << c << dec
-                              << ".\nLaTeX export will fail."
-                              << endl;
+                       throw EncodingException(c);
                else
                        return it->second.command;
        }
index b4e728ea67bcc6757df3640ec902efcf2542c449..b23e50b954a94942a435742063410e117e4cefad 100644 (file)
@@ -14,6 +14,7 @@
 #define ENCODING_H
 
 #include "support/docstring.h"
+#include "support/types.h"
 
 #include <map>
 #include <set>
@@ -24,6 +25,17 @@ namespace support { class FileName; }
 
 class LaTeXFeatures;
 
+class EncodingException : public std::exception {
+public:
+       EncodingException(char_type c);
+       virtual ~EncodingException() throw() {}
+       virtual const char * what() const throw();
+       char_type failed_char;
+       int par_id;
+       pos_type pos;
+};
+
 
 ///
 class Encoding {
index 3b66a68de395f3966f2e0bf8ea039f3c309a927c..c21b7b349bd86dacb97053058132f04eb0400b86 100644 (file)
@@ -2024,9 +2024,17 @@ bool Paragraph::latex(Buffer const & buf,
                                        texrow, rp, running_font,
                                        basefont, outerfont, open_font,
                                        runningChange, *style, i, column);
-               else
-                       d->latexSpecialChar(os, rp, running_font, runningChange,
-                               *style, i, column);
+               else {
+                       try {
+                               d->latexSpecialChar(os, rp, running_font, runningChange,
+                                       *style, i, column);
+                       } catch (EncodingException & e) {
+                               // add location information and throw again.
+                               e.par_id = id();
+                               e.pos = i;
+                               throw(e);
+                       }
+               }
 
                // Set the encoding to that returned from simpleTeXSpecialChars (see
                // comment for encoding member in OutputParams.h)