Merge branch 'master' of git.lyx.org:lyx

This commit is contained in:
Juergen Spitzmueller 2012-10-06 12:10:39 +02:00
commit 2801f40ad2
15 changed files with 213 additions and 85 deletions

View File

@ -113,6 +113,9 @@ void Context::begin_layout(ostream & os, Layout const * const & l)
} }
// FIXME: This is not enough for things like // FIXME: This is not enough for things like
// \\Huge par1 \\par par2 // \\Huge par1 \\par par2
// FIXME: If the document language is not english this outputs a
// superflous language change. Fortunately this is only file format
// bloat an does not change the TeX export of LyX.
output_font_change(os, normalfont, font); output_font_change(os, normalfont, font);
} }

View File

@ -552,7 +552,7 @@ void Parser::tokenize_one()
push_back(Token(s, catSpace)); push_back(Token(s, catSpace));
break; break;
} }
case catNewline: { case catNewline: {
++lineno_; ++lineno_;
docstring s(1, getNewline(is_, c)); docstring s(1, getNewline(is_, c));
@ -565,7 +565,7 @@ void Parser::tokenize_one()
push_back(Token(s, catNewline)); push_back(Token(s, catNewline));
break; break;
} }
case catComment: { case catComment: {
// We don't treat "%\n" combinations here specially because // We don't treat "%\n" combinations here specially because
// we want to preserve them in the preamble // we want to preserve them in the preamble
@ -581,7 +581,7 @@ void Parser::tokenize_one()
push_back(Token(s, catComment)); push_back(Token(s, catComment));
break; break;
} }
case catEscape: { case catEscape: {
is_.get(c); is_.get(c);
if (!is_) { if (!is_) {
@ -599,12 +599,12 @@ void Parser::tokenize_one()
} }
break; break;
} }
case catIgnore: { case catIgnore: {
cerr << "ignoring a char: " << c << "\n"; cerr << "ignoring a char: " << c << "\n";
break; break;
} }
default: default:
push_back(Token(docstring(1, c), catcode(c))); push_back(Token(docstring(1, c), catcode(c)));
} }

View File

@ -44,9 +44,8 @@ Preamble preamble;
namespace { namespace {
// "chinese-simplified", "chinese-traditional", "japanese-cjk", "korean" // CJK languages are handled in text.cpp, polyglossia languages are listed
// cannot be supported because it is impossible to determine the correct document // further down.
// language if CJK is used.
/** /**
* known babel language names (including synonyms) * known babel language names (including synonyms)
* not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai * not in standard babel: arabic, arabtex, armenian, belarusian, serbian-latin, thai
@ -88,6 +87,9 @@ const char * const known_coded_languages[] = {"french", "afrikaans", "albanian",
"uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh", "uppersorbian", "uppersorbian", "english", "english", "vietnamese", "welsh",
0}; 0};
/// languages with danish quotes (.lyx names)
const char * const known_danish_quotes_languages[] = {"danish", 0};
/// languages with english quotes (.lyx names) /// languages with english quotes (.lyx names)
const char * const known_english_quotes_languages[] = {"american", "australian", const char * const known_english_quotes_languages[] = {"american", "australian",
"bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english", "bahasa", "bahasam", "brazilian", "canadian", "chinese-simplified", "english",
@ -446,6 +448,7 @@ Preamble::Preamble() : one_language(true), title_layout_found(false)
h_font_roman = "default"; h_font_roman = "default";
h_font_sans = "default"; h_font_sans = "default";
h_font_typewriter = "default"; h_font_typewriter = "default";
h_font_math = "auto";
h_font_default_family = "default"; h_font_default_family = "default";
h_use_non_tex_fonts = "false"; h_use_non_tex_fonts = "false";
h_font_sc = "false"; h_font_sc = "false";
@ -506,7 +509,7 @@ Preamble::Preamble() : one_language(true), title_layout_found(false)
h_use_indices = "false"; h_use_indices = "false";
h_use_geometry = "false"; h_use_geometry = "false";
h_use_default_options = "false"; h_use_default_options = "false";
h_use_hyperref = "0"; h_use_hyperref = "false";
h_use_refstyle = "0"; h_use_refstyle = "0";
h_use_packages["amsmath"] = "1"; h_use_packages["amsmath"] = "1";
h_use_packages["amssymb"] = "0"; h_use_packages["amssymb"] = "0";
@ -522,7 +525,7 @@ void Preamble::handle_hyperref(vector<string> & options)
{ {
// FIXME swallow inputencoding changes that might surround the // FIXME swallow inputencoding changes that might surround the
// hyperref setup if it was written by LyX // hyperref setup if it was written by LyX
h_use_hyperref = "1"; h_use_hyperref = "true";
// swallow "unicode=true", since LyX does always write that // swallow "unicode=true", since LyX does always write that
vector<string>::iterator it = vector<string>::iterator it =
find(options.begin(), options.end(), "unicode=true"); find(options.begin(), options.end(), "unicode=true");
@ -638,6 +641,7 @@ void Preamble::handle_package(Parser &p, string const & name,
vector<string> options = split_options(opts); vector<string> options = split_options(opts);
add_package(name, options); add_package(name, options);
string scale; string scale;
char const * const * where = 0;
if (is_known(name, known_xetex_packages)) { if (is_known(name, known_xetex_packages)) {
xetex = true; xetex = true;
@ -648,10 +652,8 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
// roman fonts // roman fonts
if (is_known(name, known_roman_fonts)) { if (is_known(name, known_roman_fonts))
h_font_roman = name; h_font_roman = name;
p.skip_spaces();
}
if (name == "fourier") { if (name == "fourier") {
h_font_roman = "utopia"; h_font_roman = "utopia";
@ -754,9 +756,6 @@ void Preamble::handle_package(Parser &p, string const & name,
} }
else if (name == "CJK") { else if (name == "CJK") {
// It is impossible to determine the document language if CJK is used.
// All we can do is to notify the user that he has to set this by himself.
have_CJK = true;
// set the encoding to "auto" because it might be set to "default" by the babel handling // set the encoding to "auto" because it might be set to "default" by the babel handling
// and this would not be correct for CJK // and this would not be correct for CJK
if (h_inputencoding == "default") if (h_inputencoding == "default")
@ -816,10 +815,16 @@ void Preamble::handle_package(Parser &p, string const & name,
else if (is_known(name, known_lyx_packages) && options.empty()) { else if (is_known(name, known_lyx_packages) && options.empty()) {
if (name == "splitidx") if (name == "splitidx")
h_use_indices = "true"; h_use_indices = "true";
if (!in_lyx_preamble) if (!in_lyx_preamble) {
h_preamble << package_beg_sep << name h_preamble << package_beg_sep << name
<< package_mid_sep << "\\usepackage{" << package_mid_sep << "\\usepackage{"
<< name << "}\n" << package_end_sep; << name << '}';
if (p.next_token().cat() == catNewline ||
(p.next_token().cat() == catSpace &&
p.next_next_token().cat() == catNewline))
h_preamble << '\n';
h_preamble << package_end_sep;
}
} }
else if (name == "geometry") else if (name == "geometry")
@ -828,8 +833,8 @@ void Preamble::handle_package(Parser &p, string const & name,
else if (name == "subfig") else if (name == "subfig")
; // ignore this FIXME: Use the package separator mechanism instead ; // ignore this FIXME: Use the package separator mechanism instead
else if (is_known(name, known_languages)) else if ((where = is_known(name, known_languages)))
h_language = name; h_language = known_coded_languages[where - known_languages];
else if (name == "natbib") { else if (name == "natbib") {
h_biblio_style = "plainnat"; h_biblio_style = "plainnat";
@ -859,12 +864,16 @@ void Preamble::handle_package(Parser &p, string const & name,
else if (!in_lyx_preamble) { else if (!in_lyx_preamble) {
if (options.empty()) if (options.empty())
h_preamble << "\\usepackage{" << name << "}\n"; h_preamble << "\\usepackage{" << name << '}';
else { else {
h_preamble << "\\usepackage[" << opts << "]{" h_preamble << "\\usepackage[" << opts << "]{"
<< name << "}\n"; << name << '}';
options.clear(); options.clear();
} }
if (p.next_token().cat() == catNewline ||
(p.next_token().cat() == catSpace &&
p.next_next_token().cat() == catNewline))
h_preamble << '\n';
} }
// We need to do something with the options... // We need to do something with the options...
@ -905,7 +914,7 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
// http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage // http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
// (quotes for kazakh and interlingua are unknown) // (quotes for kazakh and interlingua are unknown)
// danish // danish
if (h_language == "danish") if (is_known(h_language, known_danish_quotes_languages))
h_quotes_language = "danish"; h_quotes_language = "danish";
// french // french
else if (is_known(h_language, known_french_quotes_languages)) else if (is_known(h_language, known_french_quotes_languages))
@ -982,6 +991,7 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
<< "\\font_roman " << h_font_roman << "\n" << "\\font_roman " << h_font_roman << "\n"
<< "\\font_sans " << h_font_sans << "\n" << "\\font_sans " << h_font_sans << "\n"
<< "\\font_typewriter " << h_font_typewriter << "\n" << "\\font_typewriter " << h_font_typewriter << "\n"
<< "\\font_math " << h_font_math << "\n"
<< "\\font_default_family " << h_font_default_family << "\n" << "\\font_default_family " << h_font_default_family << "\n"
<< "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n" << "\\use_non_tex_fonts " << h_use_non_tex_fonts << "\n"
<< "\\font_sc " << h_font_sc << "\n" << "\\font_sc " << h_font_sc << "\n"
@ -1000,7 +1010,7 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc)
os << "\\paperfontsize " << h_paperfontsize << "\n" os << "\\paperfontsize " << h_paperfontsize << "\n"
<< "\\spacing " << h_spacing << "\n" << "\\spacing " << h_spacing << "\n"
<< "\\use_hyperref " << h_use_hyperref << '\n'; << "\\use_hyperref " << h_use_hyperref << '\n';
if (h_use_hyperref == "1") { if (h_use_hyperref == "true") {
if (!h_pdf_title.empty()) if (!h_pdf_title.empty())
os << "\\pdf_title \"" << h_pdf_title << "\"\n"; os << "\\pdf_title \"" << h_pdf_title << "\"\n";
if (!h_pdf_author.empty()) if (!h_pdf_author.empty())
@ -1634,6 +1644,16 @@ void Preamble::parse(Parser & p, string const & forceclass,
ss << tc.sides(); ss << tc.sides();
h_papersides = ss.str(); h_papersides = ss.str();
} }
// If the CJK package is used we cannot set the document language from
// the babel options. Instead, we guess which language is used most
// and set this one.
default_language = h_language;
if (is_full_document && auto_packages.find("CJK") != auto_packages.end()) {
p.pushPosition();
h_language = guessLanguage(p, default_language);
p.popPosition();
}
} }

View File

@ -38,8 +38,10 @@ public:
std::string inputencoding() const { return h_inputencoding; } std::string inputencoding() const { return h_inputencoding; }
/// ///
std::string notefontcolor() const { return h_notefontcolor; } std::string notefontcolor() const { return h_notefontcolor; }
/// /// The document language
std::string language() const { return h_language; } std::string docLanguage() const { return h_language; }
/// The language of text which is not explicitly marked
std::string defaultLanguage() const { return default_language; }
/// ///
std::string use_indices() const { return h_use_indices; } std::string use_indices() const { return h_use_indices; }
/// ///
@ -89,6 +91,8 @@ private:
/// needed to handle encodings with babel /// needed to handle encodings with babel
bool one_language; bool one_language;
/// the main non-CJK language
std::string default_language;
/// was at least one title layout found? /// was at least one title layout found?
bool title_layout_found; bool title_layout_found;
@ -105,6 +109,7 @@ private:
std::string h_float_placement; std::string h_float_placement;
std::string h_fontcolor; std::string h_fontcolor;
std::string h_fontencoding; std::string h_fontencoding;
std::string h_font_math;
std::string h_font_roman; std::string h_font_roman;
std::string h_font_sans; std::string h_font_sans;
std::string h_font_typewriter; std::string h_font_typewriter;

View File

@ -62,6 +62,20 @@ Format LaTeX feature LyX feature
411 support for polyglossia \language_package (the cases of no package, of babel and of custom package is supported) 411 support for polyglossia \language_package (the cases of no package, of babel and of custom package is supported)
415 undertilde.sty fonts 415 undertilde.sty fonts
427 tipa.sty \begin{IPA}, \textipa InsetTIPA 427 tipa.sty \begin{IPA}, \textipa InsetTIPA
434 libertine-type1, libertine.sty, \font_osf, \font_roman
libertine-legacy.sty
435 txtt LaTeX font \font_typewriter
436 mathdesign.sty \font_roman
437 TeX Gyre LaTeX fonts \font_*
438 \tone{51}, \tone{15}, \tone{45}, InsetTIPA
\tone{12}, \tone{454}, \toptiebar,
\bottomtiebar
439 MinionPro.sty \font_roman, \font_osf
440 MinionPro.sty, eulervm.sty \font_math
442 newtxmath.sty, \font_math
443 unicode-math.sty InsetMath*
444 biolinum-type1.sty, libertine.sty, \font_osf, \font_sans
libertine-legacy.sty
General General

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -23,6 +23,7 @@
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -36,7 +37,7 @@
\index_command default \index_command default
\paperfontsize default \paperfontsize default
\spacing single \spacing single
\use_hyperref 0 \use_hyperref false
\papersize default \papersize default
\use_geometry false \use_geometry false
\use_package amsmath 1 \use_package amsmath 1
@ -55,6 +56,10 @@
\suppress_date false \suppress_date false
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -12,6 +12,7 @@
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -25,7 +26,7 @@
\index_command default \index_command default
\paperfontsize 12 \paperfontsize 12
\spacing onehalf \spacing onehalf
\use_hyperref 0 \use_hyperref false
\papersize a4paper \papersize a4paper
\use_geometry false \use_geometry false
\use_package amsmath 2 \use_package amsmath 2
@ -44,6 +45,10 @@
\suppress_date false \suppress_date false
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -23,6 +23,7 @@
\font_roman Linux Libertine O \font_roman Linux Libertine O
\font_sans Linux Biolinum O \font_sans Linux Biolinum O
\font_typewriter Linux Biolinum O \font_typewriter Linux Biolinum O
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts true \use_non_tex_fonts true
\font_sc false \font_sc false
@ -36,7 +37,7 @@
\index_command default \index_command default
\paperfontsize default \paperfontsize default
\spacing single \spacing single
\use_hyperref 0 \use_hyperref false
\papersize default \papersize default
\use_geometry false \use_geometry false
\use_package amsmath 1 \use_package amsmath 1
@ -55,6 +56,10 @@
\suppress_date false \suppress_date false
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent
@ -74,12 +79,14 @@
\begin_layout Part \begin_layout Part
\lang british
df df
\end_layout \end_layout
\begin_layout Standard \begin_layout Standard
\lang british
1 English 1 English
\lang ancientgreek \lang ancientgreek

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -47,6 +47,7 @@
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -60,7 +61,7 @@
\index_command default \index_command default
\paperfontsize 12 \paperfontsize 12
\spacing other 1.2 \spacing other 1.2
\use_hyperref 0 \use_hyperref false
\papersize b4paper \papersize b4paper
\use_geometry true \use_geometry true
\use_package amsmath 2 \use_package amsmath 2
@ -82,6 +83,10 @@
\notefontcolor #0000ff \notefontcolor #0000ff
\backgroundcolor #ff5500 \backgroundcolor #ff5500
\boxbgcolor #ffff00 \boxbgcolor #ffff00
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 3cm \leftmargin 3cm
\topmargin 1cm \topmargin 1cm
\rightmargin 4cm \rightmargin 4cm

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -18,6 +18,7 @@
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -31,7 +32,7 @@
\index_command default \index_command default
\paperfontsize 12 \paperfontsize 12
\spacing onehalf \spacing onehalf
\use_hyperref 0 \use_hyperref false
\papersize a4paper \papersize a4paper
\use_geometry false \use_geometry false
\use_package amsmath 2 \use_package amsmath 2
@ -50,6 +51,10 @@
\suppress_date false \suppress_date false
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -51,6 +51,7 @@ logicalmkup
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -65,7 +66,7 @@ logicalmkup
\index_command default \index_command default
\paperfontsize default \paperfontsize default
\spacing single \spacing single
\use_hyperref 0 \use_hyperref false
\papersize legalpaper \papersize legalpaper
\use_geometry false \use_geometry false
\use_package amsmath 1 \use_package amsmath 1
@ -84,6 +85,10 @@ logicalmkup
\suppress_date true \suppress_date true
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent

View File

@ -1,5 +1,5 @@
#LyX file created by tex2lyx 2.1.0svn #LyX file created by tex2lyx 2.1.0dev
\lyxformat 433 \lyxformat 444
\begin_document \begin_document
\begin_header \begin_header
\textclass article \textclass article
@ -23,6 +23,7 @@
\font_roman default \font_roman default
\font_sans default \font_sans default
\font_typewriter default \font_typewriter default
\font_math auto
\font_default_family default \font_default_family default
\use_non_tex_fonts false \use_non_tex_fonts false
\font_sc false \font_sc false
@ -36,7 +37,7 @@
\index_command default \index_command default
\paperfontsize 11 \paperfontsize 11
\spacing single \spacing single
\use_hyperref 0 \use_hyperref false
\papersize default \papersize default
\use_geometry false \use_geometry false
\use_package amsmath 1 \use_package amsmath 1
@ -55,6 +56,10 @@
\suppress_date false \suppress_date false
\justification true \justification true
\use_refstyle 0 \use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3 \secnumdepth 3
\tocdepth 3 \tocdepth 3
\paragraph_separation indent \paragraph_separation indent

View File

@ -332,7 +332,6 @@ bool checkModule(string const & name, bool command)
bool noweb_mode = false; bool noweb_mode = false;
bool pdflatex = false; bool pdflatex = false;
bool xetex = false; bool xetex = false;
bool have_CJK = false;
bool is_nonCJKJapanese = false; bool is_nonCJKJapanese = false;
bool roundtrip = false; bool roundtrip = false;
@ -704,7 +703,7 @@ bool tex2lyx(idocstream & is, ostream & os, string encoding)
stringstream ss; stringstream ss;
// store the document language in the context to be able to handle the // store the document language in the context to be able to handle the
// commands like \foreignlanguage and \textenglish etc. // commands like \foreignlanguage and \textenglish etc.
context.font.language = preamble.language(); context.font.language = preamble.defaultLanguage();
// parse the main text // parse the main text
parse_text(p, ss, FLAG_END, true, context); parse_text(p, ss, FLAG_END, true, context);
if (Context::empty) if (Context::empty)

View File

@ -67,6 +67,10 @@ void parse_text_in_inset(Parser & p, std::ostream & os, unsigned flags,
bool outer, Context const & context, bool outer, Context const & context,
InsetLayout const * layout = 0); InsetLayout const * layout = 0);
/// Guess document language from \p p if CJK is used.
/// \p lang is used for all non-CJK contents.
std::string guessLanguage(Parser & p, std::string const & lang);
/// in math.cpp /// in math.cpp
void parse_math(Parser & p, std::ostream & os, unsigned flags, mode_type mode); void parse_math(Parser & p, std::ostream & os, unsigned flags, mode_type mode);
@ -166,8 +170,6 @@ extern bool noweb_mode;
extern bool pdflatex; extern bool pdflatex;
/// Did we recognize any xetex-only construct? /// Did we recognize any xetex-only construct?
extern bool xetex; extern bool xetex;
/// Do we have CJK?
extern bool have_CJK;
/// Do we have non-CJK Japanese? /// Do we have non-CJK Japanese?
extern bool is_nonCJKJapanese; extern bool is_nonCJKJapanese;
/// LyX format that is created by tex2lyx /// LyX format that is created by tex2lyx

View File

@ -130,17 +130,9 @@ const char * const supported_CJK_encodings[] = {
* the same as supported_CJK_encodings with their corresponding LyX language name * the same as supported_CJK_encodings with their corresponding LyX language name
* please keep this in sync with supported_CJK_encodings line by line! * please keep this in sync with supported_CJK_encodings line by line!
*/ */
const char * const coded_supported_CJK_encodings[] = { const char * const supported_CJK_languages[] = {
"japanese-cjk", "korean", "chinese-simplified", "chinese-traditional", 0}; "japanese-cjk", "korean", "chinese-simplified", "chinese-traditional", 0};
string CJK2lyx(string const & encoding)
{
char const * const * where = is_known(encoding, supported_CJK_encodings);
if (where)
return coded_supported_CJK_encodings[where - supported_CJK_encodings];
return encoding;
}
/*! /*!
* natbib commands. * natbib commands.
* The starred forms are also known except for "citefullauthor", * The starred forms are also known except for "citefullauthor",
@ -1440,8 +1432,9 @@ void parse_environment(Parser & p, ostream & os, bool outer,
// LyX doesn't support the second argument so if // LyX doesn't support the second argument so if
// this is used we need to output everything as ERT // this is used we need to output everything as ERT
string const mapping = p.getArg('{', '}'); string const mapping = p.getArg('{', '}');
if ((!mapping.empty() && mapping != " ") char const * const * const where =
|| (!is_known(encoding, supported_CJK_encodings))) { is_known(encoding, supported_CJK_encodings);
if ((!mapping.empty() && mapping != " ") || !where) {
parent_context.check_layout(os); parent_context.check_layout(os);
handle_ert(os, "\\begin{" + name + "}{" + encoding + "}{" + mapping + "}", handle_ert(os, "\\begin{" + name + "}{" + encoding + "}{" + mapping + "}",
parent_context); parent_context);
@ -1459,7 +1452,8 @@ void parse_environment(Parser & p, ostream & os, bool outer,
handle_ert(os, "\\end{" + name + "}", handle_ert(os, "\\end{" + name + "}",
parent_context); parent_context);
} else { } else {
string const lang = CJK2lyx(encoding); string const lang =
supported_CJK_languages[where - supported_CJK_encodings];
// store the language because we must reset it at the end // store the language because we must reset it at the end
string const lang_old = parent_context.font.language; string const lang_old = parent_context.font.language;
parent_context.font.language = lang; parent_context.font.language = lang;
@ -2142,24 +2136,6 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
while (p.good()) { while (p.good()) {
Token const & t = p.get_token(); Token const & t = p.get_token();
// it is impossible to determine the correct document language if CJK is used.
// Therefore write a note at the beginning of the document
if (have_CJK) {
context.check_layout(os);
begin_inset(os, "Note Note\n");
os << "status open\n\\begin_layout Plain Layout\n"
<< "\\series bold\n"
<< "Important information:\n"
<< "\\end_layout\n\n"
<< "\\begin_layout Plain Layout\n"
<< "This document contains text in Chinese, Japanese or Korean.\n"
<< " It was therefore impossible for tex2lyx to set the correct document langue for your document."
<< " Please set the language manually in the document settings.\n"
<< "\\end_layout\n";
end_inset(os);
have_CJK = false;
}
// it is impossible to determine the correct encoding for non-CJK Japanese. // it is impossible to determine the correct encoding for non-CJK Japanese.
// Therefore write a note at the beginning of the document // Therefore write a note at the beginning of the document
if (is_nonCJKJapanese) { if (is_nonCJKJapanese) {
@ -2739,8 +2715,7 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
context.check_layout(os); context.check_layout(os);
p.skip_spaces(); p.skip_spaces();
begin_inset(os, "Caption\n"); begin_inset(os, "Caption\n");
Context newcontext(true, context.textclass); Context newcontext(true, context.textclass, 0, 0, context.font);
newcontext.font = context.font;
newcontext.check_layout(os); newcontext.check_layout(os);
if (p.next_token().cat() != catEscape && if (p.next_token().cat() != catEscape &&
p.next_token().character() == '[') { p.next_token().character() == '[') {
@ -2790,8 +2765,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
os << "\n\\begin_layout Plain Layout"; os << "\n\\begin_layout Plain Layout";
p.skip_spaces(); p.skip_spaces();
begin_inset(os, "Caption\n"); begin_inset(os, "Caption\n");
Context newcontext(true, context.textclass); Context newcontext(true, context.textclass,
newcontext.font = context.font; 0, 0, context.font);
newcontext.check_layout(os); newcontext.check_layout(os);
os << caption << "\n"; os << caption << "\n";
newcontext.check_end_layout(os); newcontext.check_end_layout(os);
@ -4427,6 +4402,79 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
} }
} }
string guessLanguage(Parser & p, string const & lang)
{
typedef std::map<std::string, size_t> LangMap;
// map from language names to number of characters
LangMap used;
used[lang] = 0;
for (char const * const * i = supported_CJK_languages; *i; i++)
used[string(*i)] = 0;
while (p.good()) {
Token const t = p.get_token();
// comments are not counted for any language
if (t.cat() == catComment)
continue;
// commands are not counted as well, but we need to detect
// \begin{CJK} and switch encoding if needed
if (t.cat() == catEscape) {
if (t.cs() == "inputencoding") {
string const enc = subst(p.verbatim_item(), "\n", " ");
p.setEncoding(enc);
continue;
}
if (t.cs() != "begin")
continue;
} else {
// Non-CJK content is counted for lang.
// We do not care about the real language here:
// If we have more non-CJK contents than CJK contents,
// we simply use the language that was specified as
// babel main language.
used[lang] += t.asInput().length();
continue;
}
// Now we are starting an environment
p.pushPosition();
string const name = p.getArg('{', '}');
if (name != "CJK") {
p.popPosition();
continue;
}
// It is a CJK environment
p.popPosition();
/* name = */ p.getArg('{', '}');
string const encoding = p.getArg('{', '}');
/* mapping = */ p.getArg('{', '}');
string const encoding_old = p.getEncoding();
char const * const * const where =
is_known(encoding, supported_CJK_encodings);
if (where)
p.setEncoding(encoding);
else
p.setEncoding("utf8");
string const text = p.verbatimEnvironment("CJK");
p.setEncoding(encoding_old);
p.skip_spaces();
if (!where) {
// ignore contents in unknown CJK encoding
continue;
}
// the language of the text
string const cjk =
supported_CJK_languages[where - supported_CJK_encodings];
used[cjk] += text.length();
}
LangMap::const_iterator use = used.begin();
for (LangMap::const_iterator it = used.begin(); it != used.end(); ++it) {
if (it->second > use->second)
use = it;
}
return use->first;
}
// }]) // }])