X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftex2lyx%2Ftex2lyx.cpp;h=1e56c44bf9d6e460cf0357b8f8cd03eb4a63d03a;hb=3628ceec480c3d8fa9673f80f781eb1153fb9e1f;hp=57790ab12b91e877774ab1b35958fc4f8e717b39;hpb=e4fe294bdc2df4b323390e60ded8804294868614;p=lyx.git diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp index 57790ab12b..1e56c44bf9 100644 --- a/src/tex2lyx/tex2lyx.cpp +++ b/src/tex2lyx/tex2lyx.cpp @@ -839,23 +839,9 @@ namespace { * You must ensure that \p parentFilePathTeX is properly set before calling * this function! */ -bool tex2lyx(idocstream & is, ostream & os, string encoding) +bool tex2lyx(idocstream & is, ostream & os, string const & encoding, + string const & outfiledir) { - // Set a sensible default encoding. - // This is used until an encoding command is found. - // For child documents use the encoding of the master, else ISO8859-1, - // (formerly known by its latex name latin1), since ISO8859-1 does not - // cause an iconv error if the actual encoding is different (bug 7509). - if (encoding.empty()) { - if (preamble.inputencoding() == "auto") - encoding = "ISO8859-1"; - else { - Encoding const * const enc = encodings.fromLyXName( - preamble.inputencoding(), true); - encoding = enc->iconvName(); - } - } - Parser p(is, fixed_encoding ? default_encoding : string()); p.setEncoding(encoding); //p.dump(); @@ -905,7 +891,7 @@ bool tex2lyx(idocstream & is, ostream & os, string encoding) for (; it != end; ++it) preamble.addModule(*it); } - if (!preamble.writeLyXHeader(os, !active_environments.empty())) { + if (!preamble.writeLyXHeader(os, !active_environments.empty(), outfiledir)) { cerr << "Could not write LyX file header." << endl; return false; } @@ -924,11 +910,45 @@ bool tex2lyx(idocstream & is, ostream & os, string encoding) /// convert TeX from \p infilename to LyX and write it to \p os -bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding) +bool tex2lyx(FileName const & infilename, ostream & os, string encoding, + string const & outfiledir) { - ifdocstream is; + // Set a sensible default encoding. + // This is used until an encoding command is found. + // For child documents use the encoding of the master, else try to + // detect it from the preamble, since setting an encoding of an open + // fstream does currently not work on OS X. + // Always start with ISO-8859-1, (formerly known by its latex name + // latin1), since ISO-8859-1 does not cause an iconv error if the + // actual encoding is different (bug 7509). + if (encoding.empty()) { + Encoding const * enc = 0; + if (preamble.inputencoding() == "auto") { + ifdocstream is(setEncoding("ISO-8859-1")); + // forbid buffering on this stream + is.rdbuf()->pubsetbuf(0, 0); + is.open(infilename.toFilesystemEncoding().c_str()); + if (is.good()) { + Parser ep(is, string()); + ep.setEncoding("ISO-8859-1"); + Preamble encodingpreamble; + string const e = encodingpreamble + .parseEncoding(ep, documentclass); + if (!e.empty()) + enc = encodings.fromLyXName(e, true); + } + } else + enc = encodings.fromLyXName( + preamble.inputencoding(), true); + if (enc) + encoding = enc->iconvName(); + else + encoding = "ISO-8859-1"; + } + + ifdocstream is(setEncoding(encoding)); // forbid buffering on this stream - is.rdbuf()->pubsetbuf(0,0); + is.rdbuf()->pubsetbuf(0, 0); is.open(infilename.toFilesystemEncoding().c_str()); if (!is.good()) { cerr << "Could not open input file \"" << infilename @@ -937,7 +957,7 @@ bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding) } string const oldParentFilePath = parentFilePathTeX; parentFilePathTeX = onlyPath(infilename.absFileName()); - bool retval = tex2lyx(is, os, encoding); + bool retval = tex2lyx(is, os, encoding, outfiledir); parentFilePathTeX = oldParentFilePath; return retval; } @@ -970,7 +990,8 @@ bool tex2lyx(string const & infilename, FileName const & outfilename, cerr << "Input file: " << infilename << "\n"; cerr << "Output file: " << outfilename << "\n"; #endif - return tex2lyx(FileName(infilename), os, encoding); + return tex2lyx(FileName(infilename), os, encoding, + outfilename.onlyPath().absFileName() + '/'); } @@ -1122,7 +1143,7 @@ int TeX2LyXApp::run() if (outfilename == "-") { // assume same directory as input file masterFilePathLyX = masterFilePathTeX; - if (tex2lyx(FileName(infilename), cout, default_encoding)) + if (tex2lyx(FileName(infilename), cout, default_encoding, masterFilePathLyX)) return EXIT_SUCCESS; } else { masterFilePathLyX = onlyPath(outfilename); @@ -1157,7 +1178,7 @@ int main(int argc, char * argv[]) lyx::lyxerr.setStream(cerr); - os::init(argc, argv); + os::init(argc, &argv); lyx::TeX2LyXApp app(argc, argv); return app.exec();