]> git.lyx.org Git - lyx.git/blobdiff - src/BufferParams.cpp
Do not overwrite read-only files. We now move the file to the backup directory and...
[lyx.git] / src / BufferParams.cpp
index cbab310859cc3f2eec6d8bba309e675f21da5c01..c09f5bbbad1e8c5849c11dfa8782899736913950 100644 (file)
@@ -534,6 +534,8 @@ VSpace const & BufferParams::getDefSkip() const
 
 void BufferParams::setDefSkip(VSpace const & vs)
 {
+       // DEFSKIP will cause an infinite loop
+       LASSERT(vs.kind() != VSpace::DEFSKIP, return);
        pimpl_->defskip = vs;
 }
 
@@ -545,7 +547,8 @@ string BufferParams::readToken(Lexer & lex, string const & token,
                lex.next();
                string const classname = lex.getString();
                // if there exists a local layout file, ignore the system one
-               // NOTE: in this case, the textclass (.cls file) is assumed to be available.
+               // NOTE: in this case, the textclass (.cls file) is assumed to 
+               // be available.
                string tcp;
                LayoutFileList & bcl = LayoutFileList::get();
                if (tcp.empty() && !filepath.empty())
@@ -554,12 +557,13 @@ string BufferParams::readToken(Lexer & lex, string const & token,
                        setBaseClass(tcp);
                else
                        setBaseClass(classname);
-               // We assume that a tex class exists for local or unknown layouts so this warning
-               // will only be given for system layouts.
+               // We assume that a tex class exists for local or unknown 
+               // layouts so this warning, will only be given for system layouts.
                if (!baseClass()->isTeXClassAvailable()) {
                        docstring const desc = 
                                translateIfPossible(from_utf8(baseClass()->description()));
-                       docstring const prereqs = from_utf8(baseClass()->prerequisites());
+                       docstring const prereqs = 
+                               from_utf8(baseClass()->prerequisites());
                        docstring const msg =
                                bformat(_("The selected document class\n"
                                                 "\t%1$s\n"
@@ -645,11 +649,11 @@ string BufferParams::readToken(Lexer & lex, string const & token,
                pimpl_->indentation = HSpace(indentation);
        } else if (token == "\\defskip") {
                lex.next();
-               string defskip = lex.getString();
-               if (defskip == "defskip")
-                       // this is invalid
-                       defskip = "medskip";
+               string const defskip = lex.getString();
                pimpl_->defskip = VSpace(defskip);
+               if (pimpl_->defskip.kind() == VSpace::DEFSKIP)
+                       // that is invalid
+                       pimpl_->defskip = VSpace(VSpace::MEDSKIP);
        } else if (token == "\\quotes_language") {
                string quotes_lang;
                lex >> quotes_lang;
@@ -755,7 +759,7 @@ string BufferParams::readToken(Lexer & lex, string const & token,
                istringstream ss(lex.getString());
                Author a;
                ss >> a;
-               author_map[a.buffer_id()] = pimpl_->authorlist.record(a);
+               author_map[a.bufferId()] = pimpl_->authorlist.record(a);
        } else if (token == "\\paperorientation") {
                string orient;
                lex >> orient;
@@ -1116,6 +1120,7 @@ void BufferParams::validate(LaTeXFeatures & features) const
                                features.require("ct-none");
                        }
                        break;
+               case OutputParams::LUATEX:
                case OutputParams::PDFLATEX:
                case OutputParams::XETEX:
                        if (xcolorulem) {
@@ -1183,8 +1188,8 @@ void BufferParams::validate(LaTeXFeatures & features) const
                        features.require("color");
        }
 
-       if (useXetex)
-               features.require("xetex");
+       if (features.runparams().flavor == OutputParams::XETEX)
+               features.require("polyglossia");
 
        if (language->lang() == "vietnamese")
                features.require("vietnamese");
@@ -1301,34 +1306,16 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
 
        ostringstream language_options;
        bool const use_babel = features.useBabel() && !tclass.provides("babel");
-       if (use_babel) {
+       bool const use_polyglossia = features.usePolyglossia();
+       bool const global = lyxrc.language_global_options;
+       if (use_babel || (use_polyglossia && global)) {
                language_options << features.getLanguages();
                if (!language->babel().empty()) {
                        if (!language_options.str().empty())
                                language_options << ',';
                        language_options << language->babel();
                }
-               // if Vietnamese is used, babel must directly be loaded
-               // with language options, not in the class options, see
-               // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg129417.html
-               size_t viet = language_options.str().find("vietnam");
-               // viet = string::npos when not found
-               // the same is for all other languages that are not directly supported by
-               // babel, but where LaTeX-packages add babel support.
-               // this is currently the case for Latvian, Lithuanian, Mongolian
-               // and Turkmen
-               size_t latvian = language_options.str().find("latvian");
-               size_t lithu = language_options.str().find("lithuanian");
-               size_t mongo = language_options.str().find("mongolian");
-               size_t turkmen = language_options.str().find("turkmen");
-               // if Japanese is used, babel must directly be loaded
-               // with language options, not in the class options, see
-               // http://www.lyx.org/trac/ticket/4597#c4
-               size_t japan = language_options.str().find("japanese");
-               if (lyxrc.language_global_options && !language_options.str().empty()
-                       && viet == string::npos && japan == string::npos
-                       && latvian == string::npos && lithu == string::npos
-                       && mongo == string::npos && turkmen == string::npos)
+               if (global && !features.needBabelLangOptions())
                        clsoptions << language_options.str() << ',';
        }
 
@@ -1373,9 +1360,9 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
        // set font encoding
        // for arabic_arabi and farsi we also need to load the LAE and
        // LFE encoding
-       // XeTeX works without fontenc
+       // XeTeX and LuaTeX (isFullUnicode() flavor) work without fontenc
        if (font_encoding() != "default" && language->lang() != "japanese"
-           && !useXetex && !tclass.provides("fontenc")) {
+           && !features.runparams().isFullUnicode() && !tclass.provides("fontenc")) {
                size_t fars = language_options.str().find("farsi");
                size_t arab = language_options.str().find("arabic");
                if (language->lang() == "arabic_arabi"
@@ -1736,13 +1723,15 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
        
        // If we use hyperref, jurabib, japanese, or vietnamese, we have to call babel before them.
        if (use_babel
-               && (features.isRequired("jurabib")
-                       || features.isRequired("hyperref")
-                       || features.isRequired("vietnamese")
-                       || features.isRequired("japanese") ) ) {
-                               // FIXME UNICODE
-                               lyxpreamble += from_utf8(babelCall(language_options.str())) + '\n';
-                               lyxpreamble += from_utf8(features.getBabelOptions()) + '\n';
+           && (features.isRequired("jurabib")
+               || features.isRequired("hyperref")
+               || features.isRequired("vietnamese")
+               || features.isRequired("japanese"))) {
+                       // FIXME UNICODE
+                       lyxpreamble += from_utf8(features.getBabelPresettings());
+                       lyxpreamble += from_utf8(babelCall(language_options.str(),
+                                                          features.needBabelLangOptions())) + '\n';
+                       lyxpreamble += from_utf8(features.getBabelPostsettings());
        }
 
        // The optional packages;
@@ -1884,10 +1873,13 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
            && !features.isRequired("vietnamese")
            && !features.isRequired("japanese")) {
                // FIXME UNICODE
-               lyxpreamble += from_utf8(babelCall(language_options.str())) + '\n';
-               lyxpreamble += from_utf8(features.getBabelOptions()) + '\n';
+               lyxpreamble += from_utf8(features.getBabelPresettings());
+               lyxpreamble += from_utf8(babelCall(language_options.str(),
+                                                  features.needBabelLangOptions())) + '\n';
+               lyxpreamble += from_utf8(features.getBabelPostsettings());
        }
 
+       // FIXME Polyglossia?
        docstring const i18npreamble = features.getTClassI18nPreamble(use_babel);
        if (!i18npreamble.empty())
                lyxpreamble += i18npreamble + '\n';
@@ -1901,12 +1893,35 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
        // these packages (xunicode, for that matter) need to be loaded at least
        // after amsmath, amssymb, esint and the other packages that provide 
        // special glyphs
-       if (useXetex) {
+       if (features.runparams().flavor == OutputParams::XETEX) {
                os << "\\usepackage{xunicode}\n";
                texrow.newline();
                os << "\\usepackage{xltxtra}\n";
                texrow.newline();
        }
+       // Polyglossia must be loaded after xltxtra
+       if (use_polyglossia) {
+               // call the package
+               os << "\\usepackage{polyglossia}\n";
+               texrow.newline();
+               // set the main language
+               os << "\\setdefaultlanguage";
+               if (!language->polyglossiaOpts().empty())
+                       os << "[" << from_ascii(language->polyglossiaOpts()) << "]";
+               os << "{" + from_ascii(language->polyglossia()) + "}\n";
+               texrow.newline();
+               // now setup the other languages
+               std::map<std::string, std::string> const polylangs = 
+                       features.getPolyglossiaLanguages();
+               for (std::map<std::string, std::string>::const_iterator mit = polylangs.begin();
+                    mit != polylangs.end() ; ++mit) {
+                       os << "\\setotherlanguage";
+                       if (!mit->second.empty())
+                               os << "[" << from_ascii(mit->second) << "]";
+                       os << "{" << from_ascii(mit->first) << "}\n";
+                       texrow.newline();
+               }
+       }
        return use_babel;
 }
 
@@ -1946,12 +1961,14 @@ DocumentClass const & BufferParams::documentClass() const
 }
 
 
-DocumentClass const * BufferParams::documentClassPtr() const {
+DocumentClass const * BufferParams::documentClassPtr() const
+{
        return doc_class_;
 }
 
 
-void BufferParams::setDocumentClass(DocumentClass const * const tc) {
+void BufferParams::setDocumentClass(DocumentClass const * const tc)
+{
        // evil, but this function is evil
        doc_class_ = const_cast<DocumentClass *>(tc);
 }
@@ -2401,39 +2418,21 @@ string const BufferParams::font_encoding() const
 }
 
 
-string BufferParams::babelCall(string const & lang_opts) const
+string BufferParams::babelCall(string const & lang_opts, bool const langoptions) const
 {
-       string lang_pack = lyxrc.language_package;
-       if (lang_pack != "\\usepackage{babel}")
-               return lang_pack;
-       // suppress the babel call when there is no babel language defined
+       if (lyxrc.language_package_selection == LyXRC::LP_CUSTOM)
+               return lyxrc.language_custom_package;
+       // suppress the babel call if there is no BabelName defined
        // for the document language in the lib/languages file and if no
        // other languages are used (lang_opts is then empty)
        if (lang_opts.empty())
                return string();
-       // If Vietnamese is used, babel must directly be loaded with the
-       // language options, see
-       // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg129417.html
-       size_t viet = lang_opts.find("vietnam");
-       // viet = string::npos when not found
-       // the same is for all other languages that are not directly supported by
-       // babel, but where LaTeX-packages add babel support.
-       // this is currently the case for Latvian, Lithuanian, Mongolian
-       // and Turkmen
-       size_t latvian = lang_opts.find("latvian");
-       size_t lithu = lang_opts.find("lithuanian");
-       size_t mongo = lang_opts.find("mongolian");
-       size_t turkmen = lang_opts.find("turkmen");
-       // If Japanese is used, babel must directly be loaded with the
-       // language options, see
-       // http://www.lyx.org/trac/ticket/4597#c4
-       size_t japan = lang_opts.find("japanese");
-       if (!lyxrc.language_global_options || viet != string::npos
-               || japan != string::npos || latvian != string::npos
-               || lithu != string::npos || mongo != string::npos
-               || turkmen != string::npos)
+       // either a specific language (AsBabelOptions setting in
+       // lib/languages) or the prefs require the languages to
+       // be submitted to babel itself (not the class).
+       if (langoptions)
                return "\\usepackage[" + lang_opts + "]{babel}";
-       return lang_pack;
+       return "\\usepackage{babel}";
 }
 
 
@@ -2458,7 +2457,8 @@ docstring BufferParams::getGraphicsDriver(string const & package) const
 void BufferParams::writeEncodingPreamble(odocstream & os,
                LaTeXFeatures & features, TexRow & texrow) const
 {
-       if (useXetex)
+       // fully unicode-aware backends (such as XeTeX) do not need this
+       if (features.runparams().isFullUnicode())
                return;
        if (inputenc == "auto") {
                string const doc_encoding =
@@ -2574,9 +2574,12 @@ string const BufferParams::loadFonts(string const & rm,
        ostringstream os;
 
        if (xetex) {
-               if (rm != "default")
-                       os << "\\setmainfont[Mapping=tex-text]{"
-                          << parseFontName(rm) << "}\n";
+               if (rm != "default") {
+                       os << "\\setmainfont[Mapping=tex-text";
+                       if (osf)
+                               os << ",Numbers=OldStyle";
+                       os << "]{" << parseFontName(rm) << "}\n";
+               }
                if (sf != "default") {
                        string const sans = parseFontName(sf);
                        if (sfscale != 100)
@@ -2599,8 +2602,6 @@ string const BufferParams::loadFonts(string const & rm,
                                os << "\\setmonofont[Mapping=tex-text]{"
                                   << mono << "}\n";
                }
-               if (osf)
-                       os << "\\defaultfontfeatures{Numbers=OldStyle}\n";
                return os.str();
        }