]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
Basic support for natbib & jurabib options
[features.git] / src / frontends / qt4 / GuiDocument.cpp
index f5e96ad7316126bc45275737f6fe88e9321e8ef4..20fbd02cf91735f76440a90eb78fd1619bb8d9e6 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "GuiDocument.h"
 
+#include "CategorizedCombo.h"
 #include "GuiApplication.h"
 #include "GuiBranches.h"
 #include "GuiIndices.h"
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "BufferView.h"
+#include "CiteEnginesList.h"
 #include "Color.h"
 #include "ColorCache.h"
+#include "Converter.h"
+#include "Cursor.h"
 #include "Encoding.h"
 #include "FloatPlacement.h"
 #include "Format.h"
@@ -40,6 +44,7 @@
 #include "LaTeXFeatures.h"
 #include "LaTeXFonts.h"
 #include "Layout.h"
+#include "LayoutEnums.h"
 #include "LayoutModuleList.h"
 #include "LyXRC.h"
 #include "ModuleList.h"
@@ -48,6 +53,8 @@
 #include "qt_helpers.h"
 #include "Spacing.h"
 #include "TextClass.h"
+#include "Undo.h"
+#include "VSpace.h"
 
 #include "insets/InsetListingsParams.h"
 
@@ -67,6 +74,7 @@
 #include <QCloseEvent>
 #include <QFontDatabase>
 #include <QScrollBar>
+#include <QTextBoundaryFinder>
 #include <QTextCursor>
 
 #include <sstream>
@@ -128,46 +136,13 @@ char const * backref_opts_gui[] =
 };
 
 
-char const * packages_gui[][4] =
-{
-       {"amsmath",
-        N_("&Use amsmath package automatically"),
-        N_("Use ams&math package"),
-        N_("The LaTeX package amsmath is only used if AMS formula types or symbols from the AMS math toolbars are inserted into formulas")},
-       {"amssymb",
-        N_("&Use amssymb package automatically"),
-        N_("Use amssymb package"),
-        N_("The LaTeX package amssymb is only used if symbols from the AMS math toolbars are inserted into formulas")},
-       {"esint",
-        N_("Use esint package &automatically"),
-        N_("Use &esint package"),
-        N_("The LaTeX package esint is only used if special integral symbols are inserted into formulas")},
-       {"mathdots",
-        N_("Use math&dots package automatically"),
-        N_("Use mathdo&ts package"),
-        N_("The LaTeX package mathdots is only used if the command \\iddots is inserted into formulas")},
-       {"mathtools",
-        N_("Use mathtools package automatically"),
-        N_("Use mathtools package"),
-        N_("The LaTeX package mathtools is only used if some mathematical relations are inserted into formulas")},
-       {"mhchem",
-        N_("Use mhchem &package automatically"),
-        N_("Use mh&chem package"),
-        N_("The LaTeX package mhchem is only used if either the command \\ce or \\cf is inserted into formulas")},
-       {"undertilde",
-        N_("Use u&ndertilde package automatically"),
-        N_("Use undertilde pac&kage"),
-        N_("The LaTeX package undertilde is only used if you use the math frame decoration 'utilde'")},
-       {"", "", "", ""}
-};
-
-
 vector<string> engine_types_;
 vector<pair<string, QString> > pagestyles;
 
 QMap<QString, QString> rmfonts_;
 QMap<QString, QString> sffonts_;
 QMap<QString, QString> ttfonts_;
+QMap<QString, QString> mathfonts_;
 
 
 } // anonymous namespace
@@ -180,6 +155,7 @@ RGBColor set_fontcolor;
 bool is_fontcolor;
 RGBColor set_notefontcolor;
 RGBColor set_boxbgcolor;
+bool forced_fontspec_activation;
 
 namespace {
 // used when sorting the textclass list.
@@ -338,7 +314,7 @@ void ModuleSelectionManager::updateAddPB()
        string const modname = getAvailableModel()->getIDString(idx.row());
 
        bool const enable =
-               container_->params().moduleCanBeAdded(modname);
+               container_->params().layoutModuleCanBeAdded(modname);
        addPB->setEnabled(enable);
 }
 
@@ -479,7 +455,10 @@ PreambleModule::PreambleModule() : current_id_(0)
 {
        // This is not a memory leak. The object will be destroyed
        // with this.
-       (void) new LaTeXHighlighter(preambleTE->document());
+       // @ is letter in the LyX user preamble
+       (void) new LaTeXHighlighter(preambleTE->document(), true);
+       preambleTE->setFont(guiApp->typewriterSystemFont());
+       preambleTE->setWordWrapMode(QTextOption::NoWrap);
        setFocusProxy(preambleTE);
        connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
 }
@@ -517,7 +496,7 @@ void PreambleModule::update(BufferParams const & params, BufferId id)
 
 void PreambleModule::apply(BufferParams & params)
 {
-       params.preamble = fromqstr(preambleTE->document()->toPlainText());
+       params.preamble = qstring_to_ucs4(preambleTE->document()->toPlainText());
 }
 
 
@@ -548,7 +527,7 @@ LocalLayout::LocalLayout() : current_id_(0), validated_(false)
 
 void LocalLayout::update(BufferParams const & params, BufferId id)
 {
-       QString layout = toqstr(params.local_layout);
+       QString layout = toqstr(params.getLocalLayout(false));
        // Nothing to do if the params and preamble are unchanged.
        if (id == current_id_
                && layout == locallayoutTE->document()->toPlainText())
@@ -563,8 +542,18 @@ void LocalLayout::update(BufferParams const & params, BufferId id)
 
 void LocalLayout::apply(BufferParams & params)
 {
-       string const layout = fromqstr(locallayoutTE->document()->toPlainText());
-       params.local_layout = layout;
+       docstring const layout =
+               qstring_to_ucs4(locallayoutTE->document()->toPlainText());
+       params.setLocalLayout(layout, false);
+}
+
+
+void LocalLayout::hideConvert()
+{
+       convertPB->setEnabled(false);
+       convertLB->setText("");
+       convertPB->hide();
+       convertLB->hide();
 }
 
 
@@ -579,15 +568,14 @@ void LocalLayout::textChanged()
                validated_ = true;
                validatePB->setEnabled(false);
                validLB->setText("");
-               convertPB->hide();
-               convertLB->hide();
+               hideConvert();
                changed();
        } else if (!validatePB->isEnabled()) {
                // if that's already enabled, we shouldn't need to do anything.
                validated_ = false;
                validLB->setText(message);
                validatePB->setEnabled(true);
-               convertPB->setEnabled(false);
+               hideConvert();
                changed();
        }
 }
@@ -597,44 +585,52 @@ void LocalLayout::convert() {
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
        string const newlayout = TextClass::convert(layout);
-       LYXERR0(newlayout);
-       if (newlayout.empty()) {
-               Alert::error(_("Conversion Failed!"),
-                     _("Failed to convert local layout to current format."));
-       } else {
+       if (!newlayout.empty())
                locallayoutTE->setPlainText(toqstr(newlayout));
-       }
        validate();
 }
 
 
 void LocalLayout::convertPressed() {
        convert();
+       hideConvert();
        changed();
 }
 
 
 void LocalLayout::validate() {
-       static const QString valid = qt_("Layout is valid!");
-       static const QString vtext =
-               toqstr("<p style=\"font-weight: bold; \">")
-                 + valid + toqstr("</p>");
-       static const QString invalid = qt_("Layout is invalid!");
-       static const QString ivtext =
-               toqstr("<p style=\"color: #c00000; font-weight: bold; \">")
-                 + invalid + toqstr("</p>");
-
+       // Bold text
+       static const QString vpar("<p style=\"font-weight: bold;\">%1</p>");
+       // Flashy red bold text
+       static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; \">"
+                                  "%1</p>");
        string const layout =
                fromqstr(locallayoutTE->document()->toPlainText().trimmed());
        if (!layout.empty()) {
                TextClass::ReturnValues const ret = TextClass::validate(layout);
                validated_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT);
                validatePB->setEnabled(false);
-               validLB->setText(validated_ ? vtext : ivtext);
+               validLB->setText(validated_ ? vpar.arg(qt_("Layout is valid!"))
+                                           : ivpar.arg(qt_("Layout is invalid!")));
                if (ret == TextClass::OK_OLDFORMAT) {
                        convertPB->show();
-                       convertPB->setEnabled(true);
-                       convertLB->setText(qt_("Convert to current format"));
+                       // Testing conversion to LYXFILE_LAYOUT_FORMAT at this point
+                       // already.
+                       if (TextClass::convert(layout).empty()) {
+                               // Conversion failed. If LAYOUT_FORMAT > LYXFILE_LAYOUT_FORMAT,
+                               // then maybe the layout is still valid, but its format is more
+                               // recent than LYXFILE_LAYOUT_FORMAT. However, if LAYOUT_FORMAT
+                               // == LYXFILE_LAYOUT_FORMAT then something is definitely wrong.
+                               convertPB->setEnabled(false);
+                               const QString text = (LAYOUT_FORMAT == LYXFILE_LAYOUT_FORMAT)
+                                       ? ivpar.arg(qt_("Conversion to current format impossible!"))
+                                       : vpar.arg(qt_("Conversion to current stable format "
+                                                      "impossible."));
+                               convertLB->setText(text);
+                       } else {
+                               convertPB->setEnabled(true);
+                               convertLB->setText(qt_("Convert to current format"));
+                       }
                        convertLB->show();
                } else {
                        convertPB->hide();
@@ -658,7 +654,8 @@ void LocalLayout::validatePressed() {
 
 
 GuiDocument::GuiDocument(GuiView & lv)
-       : GuiDialog(lv, "document", qt_("Document Settings"))
+       : GuiDialog(lv, "document", qt_("Document Settings")),
+         biblioChanged_(false), nonModuleChanged_(false)
 {
        setupUi(this);
 
@@ -727,9 +724,9 @@ GuiDocument::GuiDocument(GuiView & lv)
 
        textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
                textLayoutModule->lspacingLE));
-       textLayoutModule->indentLE->setValidator(unsignedLengthValidator(
+       textLayoutModule->indentLE->setValidator(new LengthValidator(
                textLayoutModule->indentLE));
-       textLayoutModule->skipLE->setValidator(unsignedGlueLengthValidator(
+       textLayoutModule->skipLE->setValidator(new LengthValidator(
                textLayoutModule->skipLE));
 
        textLayoutModule->indentCO->addItem(qt_("Default"));
@@ -773,7 +770,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        masterChildModule->childrenTW->resizeColumnToContents(2);
 
 
-       // output
+       // Formats
        outputModule = new UiWidget<Ui::OutputUi>;
 
        connect(outputModule->defaultFormatCO, SIGNAL(activated(int)),
@@ -799,8 +796,11 @@ GuiDocument::GuiDocument(GuiView & lv)
        outputModule->synccustomCB->setValidator(new NoNewLineValidator(
                outputModule->synccustomCB));
 
+       connect(outputModule->saveTransientPropertiesCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+
        // fonts
-       fontModule = new UiWidget<Ui::FontUi>;
+       fontModule = new FontModule;
        connect(fontModule->osFontsCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(fontModule->osFontsCB, SIGNAL(toggled(bool)),
@@ -817,6 +817,10 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
                this, SLOT(ttChanged(int)));
+       connect(fontModule->fontsMathCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(fontModule->fontsMathCO, SIGNAL(activated(int)),
+               this, SLOT(mathFontChanged(int)));
        connect(fontModule->fontsDefaultCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(fontModule->fontencCO, SIGNAL(activated(int)),
@@ -829,14 +833,20 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(fontModule->cjkFontLE, SIGNAL(textChanged(const QString &)),
                this, SLOT(change_adaptor()));
+       connect(fontModule->microtypeCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
        connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)),
                this, SLOT(change_adaptor()));
        connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)),
                this, SLOT(change_adaptor()));
        connect(fontModule->fontScCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(fontModule->fontScCB, SIGNAL(toggled(bool)),
+               this, SLOT(fontScToggled(bool)));
        connect(fontModule->fontOsfCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(fontModule->fontOsfCB, SIGNAL(toggled(bool)),
+               this, SLOT(fontOsfToggled(bool)));
 
        fontModule->fontencLE->setValidator(new NoNewLineValidator(
                fontModule->fontencLE));
@@ -982,21 +992,21 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(marginsModule->columnsepUnit, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
-       marginsModule->topLE->setValidator(unsignedLengthValidator(
+       marginsModule->topLE->setValidator(new LengthValidator(
                marginsModule->topLE));
-       marginsModule->bottomLE->setValidator(unsignedLengthValidator(
+       marginsModule->bottomLE->setValidator(new LengthValidator(
                marginsModule->bottomLE));
-       marginsModule->innerLE->setValidator(unsignedLengthValidator(
+       marginsModule->innerLE->setValidator(new LengthValidator(
                marginsModule->innerLE));
-       marginsModule->outerLE->setValidator(unsignedLengthValidator(
+       marginsModule->outerLE->setValidator(new LengthValidator(
                marginsModule->outerLE));
-       marginsModule->headsepLE->setValidator(unsignedLengthValidator(
+       marginsModule->headsepLE->setValidator(new LengthValidator(
                marginsModule->headsepLE));
-       marginsModule->headheightLE->setValidator(unsignedLengthValidator(
+       marginsModule->headheightLE->setValidator(new LengthValidator(
                marginsModule->headheightLE));
-       marginsModule->footskipLE->setValidator(unsignedLengthValidator(
+       marginsModule->footskipLE->setValidator(new LengthValidator(
                marginsModule->footskipLE));
-       marginsModule->columnsepLE->setValidator(unsignedLengthValidator(
+       marginsModule->columnsepLE->setValidator(new LengthValidator(
                marginsModule->columnsepLE));
 
        bc().addCheckedLineEdit(marginsModule->topLE,
@@ -1037,6 +1047,8 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(langModule->languagePackageCO, SIGNAL(currentIndexChanged(int)),
                this, SLOT(languagePackageChanged(int)));
+       connect(langModule->dynamicQuotesCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
 
        langModule->languagePackageLE->setValidator(new NoNewLineValidator(
                langModule->languagePackageLE));
@@ -1053,23 +1065,11 @@ GuiDocument::GuiDocument(GuiView & lv)
        Encodings::const_iterator it = encodings.begin();
        Encodings::const_iterator const end = encodings.end();
        for (; it != end; ++it)
-               encodinglist.append(qt_(it->guiName()));
+               if (!it->unsafe())
+                       encodinglist.append(qt_(it->guiName()));
        encodinglist.sort();
        langModule->encodingCO->addItems(encodinglist);
 
-       langModule->quoteStyleCO->addItem(
-               qt_("``text''"),InsetQuotes::EnglishQuotes);
-       langModule->quoteStyleCO->addItem(
-               qt_("''text''"), InsetQuotes::SwedishQuotes);
-       langModule->quoteStyleCO->addItem
-               (qt_(",,text``"), InsetQuotes::GermanQuotes);
-       langModule->quoteStyleCO->addItem(
-               qt_(",,text''"), InsetQuotes::PolishQuotes);
-       langModule->quoteStyleCO->addItem(
-               qt_("<<text>>"), InsetQuotes::FrenchQuotes);
-       langModule->quoteStyleCO->addItem(
-               qt_(">>text<<"), InsetQuotes::DanishQuotes);
-
        langModule->languagePackageCO->addItem(
                qt_("Default"), toqstr("default"));
        langModule->languagePackageCO->addItem(
@@ -1116,44 +1116,59 @@ GuiDocument::GuiDocument(GuiView & lv)
        numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
        numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
        numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
-       numberingModule->tocTW->header()->setResizeMode(QHeaderView::ResizeToContents);
-
+       setSectionResizeMode(numberingModule->tocTW->header(), QHeaderView::ResizeToContents);
 
        // biblio
        biblioModule = new UiWidget<Ui::BiblioUi>;
-       connect(biblioModule->citeDefaultRB, SIGNAL(toggled(bool)),
-               this, SLOT(setNumerical(bool)));
-       connect(biblioModule->citeJurabibRB, SIGNAL(toggled(bool)),
-               this, SLOT(setAuthorYear(bool)));
-       connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
-               biblioModule->citationStyleL, SLOT(setEnabled(bool)));
-       connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
-               biblioModule->citeStyleCO, SLOT(setEnabled(bool)));
-       connect(biblioModule->citeDefaultRB, SIGNAL(clicked()),
-               this, SLOT(biblioChanged()));
-       connect(biblioModule->citeNatbibRB, SIGNAL(clicked()),
-               this, SLOT(biblioChanged()));
+       connect(biblioModule->citeEngineCO, SIGNAL(activated(int)),
+               this, SLOT(citeEngineChanged(int)));
        connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
-               this, SLOT(biblioChanged()));
-       connect(biblioModule->citeJurabibRB, SIGNAL(clicked()),
-               this, SLOT(biblioChanged()));
+               this, SLOT(citeStyleChanged()));
        connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
                this, SLOT(biblioChanged()));
        connect(biblioModule->bibtexCO, SIGNAL(activated(int)),
                this, SLOT(bibtexChanged(int)));
        connect(biblioModule->bibtexOptionsLE, SIGNAL(textChanged(QString)),
                this, SLOT(biblioChanged()));
-       connect(biblioModule->bibtexStyleLE, SIGNAL(textChanged(QString)),
+       connect(biblioModule->citePackageOptionsLE, SIGNAL(textChanged(QString)),
+               this, SLOT(biblioChanged()));
+       connect(biblioModule->defaultBiblioCO, SIGNAL(activated(int)),
+               this, SLOT(biblioChanged()));
+       connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
+               this, SLOT(biblioChanged()));
+       connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
+               this, SLOT(updateResetDefaultBiblio()));
+       connect(biblioModule->biblatexBbxCO, SIGNAL(activated(int)),
+               this, SLOT(biblioChanged()));
+       connect(biblioModule->biblatexBbxCO, SIGNAL(editTextChanged(QString)),
+               this, SLOT(updateResetDefaultBiblio()));
+       connect(biblioModule->biblatexCbxCO, SIGNAL(activated(int)),
                this, SLOT(biblioChanged()));
+       connect(biblioModule->biblatexCbxCO, SIGNAL(editTextChanged(QString)),
+               this, SLOT(updateResetDefaultBiblio()));
+       connect(biblioModule->rescanBibliosPB, SIGNAL(clicked()),
+               this, SLOT(rescanBibFiles()));
+       connect(biblioModule->resetDefaultBiblioPB, SIGNAL(clicked()),
+               this, SLOT(resetDefaultBibfile()));
+       connect(biblioModule->resetCbxPB, SIGNAL(clicked()),
+               this, SLOT(resetDefaultCbxBibfile()));
+       connect(biblioModule->resetBbxPB, SIGNAL(clicked()),
+               this, SLOT(resetDefaultBbxBibfile()));
+       connect(biblioModule->matchBbxPB, SIGNAL(clicked()),
+               this, SLOT(matchBiblatexStyles()));
+
+       biblioModule->citeEngineCO->clear();
+       for (LyXCiteEngine const & cet : theCiteEnginesList) {
+               biblioModule->citeEngineCO->addItem(qt_(cet.getName()), toqstr(cet.getID()));
+               int const i = biblioModule->citeEngineCO->findData(toqstr(cet.getID()));
+               biblioModule->citeEngineCO->setItemData(i, qt_(cet.getDescription()),
+                                                       Qt::ToolTipRole);
+       }
 
        biblioModule->bibtexOptionsLE->setValidator(new NoNewLineValidator(
                biblioModule->bibtexOptionsLE));
-       biblioModule->bibtexStyleLE->setValidator(new NoNewLineValidator(
-               biblioModule->bibtexStyleLE));
-
-       biblioModule->citeStyleCO->addItem(qt_("Author-year"));
-       biblioModule->citeStyleCO->addItem(qt_("Numerical"));
-       biblioModule->citeStyleCO->setCurrentIndex(0);
+       biblioModule->defaultBiblioCO->lineEdit()->setValidator(new NoNewLineValidator(
+               biblioModule->defaultBiblioCO->lineEdit()));
 
        // NOTE: we do not provide "custom" here for security reasons!
        biblioModule->bibtexCO->clear();
@@ -1172,49 +1187,71 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // maths
-       // FIXME This UI has problems:
-       //       1) It is not generic, packages_gui needs to be changed for each new package
-       //       2) Two checkboxes have 4 states, but one is invalid (both pressed)
-       //       3) The auto cb is not disabled if the use cb is checked
        mathsModule = new UiWidget<Ui::MathsUi>;
-       vector<string> const & packages = BufferParams::auto_packages();
-        for (size_t i = 0; i < packages.size(); ++i) {
-               // Use the order of BufferParams::auto_packages() for easier
-               // access in applyView() and paramsToDialog()
-               int n = 0;
-               for (n = 0; packages_gui[n][0][0]; n++)
-                       if (packages_gui[n][0] == packages[i])
-                               break;
-               // If this fires somebody changed
-               // BufferParams::auto_packages() without adjusting packages_gui
-               LASSERT(packages_gui[n][0][0], /**/);
-               QString autoText = qt_(packages_gui[n][1]);
-               QString alwaysText = qt_(packages_gui[n][2]);
-               QString autoTooltip = qt_(packages_gui[n][3]);
+       QStringList headers;
+       headers << qt_("Package") << qt_("Load automatically")
+               << qt_("Load always") << qt_("Do not load");
+       mathsModule->packagesTW->setHorizontalHeaderLabels(headers);
+       setSectionResizeMode(mathsModule->packagesTW->horizontalHeader(), QHeaderView::Stretch);
+       map<string, string> const & packages = BufferParams::auto_packages();
+       mathsModule->packagesTW->setRowCount(packages.size());
+       int i = 0;
+       for (map<string, string>::const_iterator it = packages.begin();
+            it != packages.end(); ++it) {
+               docstring const package = from_ascii(it->first);
+               QString autoTooltip = qt_(it->second);
                QString alwaysTooltip;
-               if (packages[i] == "amsmath")
+               if (package == "amsmath")
                        alwaysTooltip =
                                qt_("The AMS LaTeX packages are always used");
                else
                        alwaysTooltip = toqstr(bformat(
                                _("The LaTeX package %1$s is always used"),
-                               from_ascii(packages[i])));
-               QCheckBox * autoCB = new QCheckBox(autoText, mathsModule);
-               QCheckBox * alwaysCB = new QCheckBox(alwaysText, mathsModule);
-               mathsModule->gridLayout->addWidget(autoCB, 2 * i, 0);
-               mathsModule->gridLayout->addWidget(alwaysCB, 2 * i + 1, 0);
-               autoCB->setToolTip(autoTooltip);
-               alwaysCB->setToolTip(alwaysTooltip);
-               connect(autoCB, SIGNAL(toggled(bool)),
-                       alwaysCB, SLOT(setDisabled(bool)));
-               connect(autoCB, SIGNAL(clicked()),
+                               package));
+               QString neverTooltip;
+               if (package == "amsmath")
+                       neverTooltip =
+                               qt_("The AMS LaTeX packages are never used");
+               else
+                       neverTooltip = toqstr(bformat(
+                               _("The LaTeX package %1$s is never used"),
+                               package));
+               QRadioButton * autoRB = new QRadioButton(mathsModule);
+               QRadioButton * alwaysRB = new QRadioButton(mathsModule);
+               QRadioButton * neverRB = new QRadioButton(mathsModule);
+               QButtonGroup * packageGroup = new QButtonGroup(mathsModule);
+               packageGroup->addButton(autoRB);
+               packageGroup->addButton(alwaysRB);
+               packageGroup->addButton(neverRB);
+               autoRB->setToolTip(autoTooltip);
+               alwaysRB->setToolTip(alwaysTooltip);
+               neverRB->setToolTip(neverTooltip);
+               QTableWidgetItem * pack = new QTableWidgetItem(toqstr(package));
+               mathsModule->packagesTW->setItem(i, 0, pack);
+               mathsModule->packagesTW->setCellWidget(i, 1, autoRB);
+               mathsModule->packagesTW->setCellWidget(i, 2, alwaysRB);
+               mathsModule->packagesTW->setCellWidget(i, 3, neverRB);
+
+               connect(autoRB, SIGNAL(clicked()),
+                       this, SLOT(change_adaptor()));
+               connect(alwaysRB, SIGNAL(clicked()),
                        this, SLOT(change_adaptor()));
-               connect(alwaysCB, SIGNAL(clicked()),
+               connect(neverRB, SIGNAL(clicked()),
                        this, SLOT(change_adaptor()));
+               ++i;
        }
-       QSpacerItem * spacer = new QSpacerItem(20, 20, QSizePolicy::Minimum,
-                                              QSizePolicy::Expanding);
-       mathsModule->gridLayout->addItem(spacer, 2 * packages.size(), 0);
+       connect(mathsModule->allPackagesAutoPB, SIGNAL(clicked()),
+               this, SLOT(allPackagesAuto()));
+       connect(mathsModule->allPackagesAlwaysPB, SIGNAL(clicked()),
+               this, SLOT(allPackagesAlways()));
+       connect(mathsModule->allPackagesNotPB, SIGNAL(clicked()),
+               this, SLOT(allPackagesNot()));
+       connect(mathsModule->allPackagesAutoPB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->allPackagesAlwaysPB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->allPackagesNotPB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
 
 
        // latex class
@@ -1226,7 +1263,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(latexModule->psdriverCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(latexModule->classCO, SIGNAL(activated(int)),
-               this, SLOT(classChanged()));
+               this, SLOT(classChanged_adaptor()));
        connect(latexModule->classCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(latexModule->layoutPB, SIGNAL(clicked()),
@@ -1255,7 +1292,6 @@ GuiDocument::GuiDocument(GuiView & lv)
                latexModule->psdriverCO->addItem(enc);
        }
        // latex classes
-       latexModule->classCO->setModel(&classes_model_);
        LayoutFileList const & bcl = LayoutFileList::get();
        vector<LayoutFileIndex> classList = bcl.classList();
        sort(classList.begin(), classList.end(), less_textclass_avail_desc());
@@ -1264,10 +1300,22 @@ GuiDocument::GuiDocument(GuiView & lv)
        vector<LayoutFileIndex>::const_iterator cen = classList.end();
        for (int i = 0; cit != cen; ++cit, ++i) {
                LayoutFile const & tc = bcl[*cit];
-               docstring item = (tc.isTeXClassAvailable()) ?
-                       from_utf8(tc.description()) :
-                       bformat(_("Unavailable: %1$s"), from_utf8(tc.description()));
-               classes_model_.insertRow(i, toqstr(item), *cit);
+               bool const available = tc.isTeXClassAvailable();
+               docstring const guiname = translateIfPossible(from_utf8(tc.description()));
+               // tooltip sensu "KOMA-Script Article [Class 'scrartcl']"
+               QString tooltip = toqstr(bformat(_("%1$s [Class '%2$s']"), guiname, from_utf8(tc.latexname())));
+               if (!available) {
+                       docstring const output_type = (tc.outputType() == lyx::DOCBOOK) ? _("DocBook") : _("LaTeX");
+                       tooltip += '\n' + toqstr(bformat(_("Class not found by LyX. "
+                                                          "Please check if you have the matching %1$s class "
+                                                          "and all required packages (%2$s) installed."),
+                                                        output_type, from_utf8(tc.prerequisites(", "))));
+               }
+               latexModule->classCO->addItemSort(toqstr(tc.name()),
+                                                 toqstr(guiname),
+                                                 toqstr(translateIfPossible(from_utf8(tc.category()))),
+                                                 tooltip,
+                                                 true, true, true, available);
        }
 
 
@@ -1300,7 +1348,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        // Modules
        modulesModule = new UiWidget<Ui::ModulesUi>;
        modulesModule->availableLV->header()->setVisible(false);
-       modulesModule->availableLV->header()->setResizeMode(QHeaderView::ResizeToContents);
+       setSectionResizeMode(modulesModule->availableLV->header(), QHeaderView::ResizeToContents);
        modulesModule->availableLV->header()->setStretchLastSection(false);
        selectionManager =
                new ModuleSelectionManager(modulesModule->availableLV,
@@ -1310,8 +1358,6 @@ GuiDocument::GuiDocument(GuiView & lv)
                        availableModel(), selectedModel(), this);
        connect(selectionManager, SIGNAL(updateHook()),
                this, SLOT(updateModuleInfo()));
-       connect(selectionManager, SIGNAL(updateHook()),
-               this, SLOT(change_adaptor()));
        connect(selectionManager, SIGNAL(selectionChanged()),
                this, SLOT(modulesChanged()));
 
@@ -1387,28 +1433,28 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // add the panels
-       docPS->addPanel(latexModule, qt_("Document Class"));
-       docPS->addPanel(masterChildModule, qt_("Child Documents"));
-       docPS->addPanel(modulesModule, qt_("Modules"));
-       docPS->addPanel(localLayout, qt_("Local Layout"));
-       docPS->addPanel(fontModule, qt_("Fonts"));
-       docPS->addPanel(textLayoutModule, qt_("Text Layout"));
-       docPS->addPanel(pageLayoutModule, qt_("Page Layout"));
-       docPS->addPanel(marginsModule, qt_("Page Margins"));
-       docPS->addPanel(langModule, qt_("Language"));
-       docPS->addPanel(colorModule, qt_("Colors"));
-       docPS->addPanel(numberingModule, qt_("Numbering & TOC"));
-       docPS->addPanel(biblioModule, qt_("Bibliography"));
-       docPS->addPanel(indicesModule, qt_("Indexes"));
-       docPS->addPanel(pdfSupportModule, qt_("PDF Properties"));
-       docPS->addPanel(mathsModule, qt_("Math Options"));
-       docPS->addPanel(floatModule, qt_("Float Placement"));
-       docPS->addPanel(listingsModule, qt_("Listings[[inset]]"));
-       docPS->addPanel(bulletsModule, qt_("Bullets"));
-       docPS->addPanel(branchesModule, qt_("Branches"));
-       docPS->addPanel(outputModule, qt_("Output"));
-       docPS->addPanel(preambleModule, qt_("LaTeX Preamble"));
-       docPS->setCurrentPanel(qt_("Document Class"));
+       docPS->addPanel(latexModule, N_("Document Class"));
+       docPS->addPanel(masterChildModule, N_("Child Documents"));
+       docPS->addPanel(modulesModule, N_("Modules"));
+       docPS->addPanel(localLayout, N_("Local Layout"));
+       docPS->addPanel(fontModule, N_("Fonts"));
+       docPS->addPanel(textLayoutModule, N_("Text Layout"));
+       docPS->addPanel(pageLayoutModule, N_("Page Layout"));
+       docPS->addPanel(marginsModule, N_("Page Margins"));
+       docPS->addPanel(langModule, N_("Language"));
+       docPS->addPanel(colorModule, N_("Colors"));
+       docPS->addPanel(numberingModule, N_("Numbering & TOC"));
+       docPS->addPanel(biblioModule, N_("Bibliography"));
+       docPS->addPanel(indicesModule, N_("Indexes"));
+       docPS->addPanel(pdfSupportModule, N_("PDF Properties"));
+       docPS->addPanel(mathsModule, N_("Math Options"));
+       docPS->addPanel(floatModule, N_("Float Placement"));
+       docPS->addPanel(listingsModule, N_("Listings[[inset]]"));
+       docPS->addPanel(bulletsModule, N_("Bullets"));
+       docPS->addPanel(branchesModule, N_("Branches"));
+       docPS->addPanel(outputModule, N_("Formats[[output]]"));
+       docPS->addPanel(preambleModule, N_("LaTeX Preamble"));
+       docPS->setCurrentPanel("Document Class");
 // FIXME: hack to work around resizing bug in Qt >= 4.2
 // bug verified with Qt 4.2.{0-3} (JSpitzm)
 #if QT_VERSION >= 0x040200
@@ -1417,6 +1463,13 @@ GuiDocument::GuiDocument(GuiView & lv)
 }
 
 
+void GuiDocument::onBufferViewChanged()
+{
+       if (isVisibleView())
+               initialiseParams("");
+}
+
+
 void GuiDocument::saveDefaultClicked()
 {
        saveDocDefault();
@@ -1431,6 +1484,7 @@ void GuiDocument::useDefaultsClicked()
 
 void GuiDocument::change_adaptor()
 {
+       nonModuleChanged_ = true;
        changed();
 }
 
@@ -1451,31 +1505,22 @@ void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
                includeonlys_.push_back(child);
 
        updateIncludeonlys();
-       changed();
+       change_adaptor();
 }
 
 
 QString GuiDocument::validateListingsParameters()
 {
-       // use a cache here to avoid repeated validation
-       // of the same parameters
-       static string param_cache;
-       static QString msg_cache;
-
        if (listingsModule->bypassCB->isChecked())
                return QString();
-
        string params = fromqstr(listingsModule->listingsED->toPlainText());
-       if (params != param_cache) {
-               param_cache = params;
-               msg_cache = toqstr(InsetListingsParams(params).validate());
-       }
-       return msg_cache;
+       return toqstr(InsetListingsParams(params).validate());
 }
 
 
 void GuiDocument::setListingsMessage()
 {
+       // FIXME THREAD
        static bool isOK = true;
        QString msg = validateListingsParameters();
        if (msg.isEmpty()) {
@@ -1485,7 +1530,7 @@ void GuiDocument::setListingsMessage()
                // listingsTB->setTextColor("black");
                listingsModule->listingsTB->setPlainText(
                        qt_("Input listings parameters below. "
-                "Enter ? for a list of parameters."));
+                           "Enter ? for a list of parameters."));
        } else {
                isOK = false;
                // listingsTB->setTextColor("red");
@@ -1628,7 +1673,7 @@ void GuiDocument::changeBackgroundColor()
        // save color
        set_backgroundcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_backgroundcolor = true;
-       changed();
+       change_adaptor();
 }
 
 
@@ -1641,7 +1686,7 @@ void GuiDocument::deleteBackgroundColor()
        // save default color (white)
        set_backgroundcolor = rgbFromHexName("#ffffff");
        is_backgroundcolor = false;
-       changed();
+       change_adaptor();
 }
 
 
@@ -1658,7 +1703,7 @@ void GuiDocument::changeFontColor()
        // save color
        set_fontcolor = rgbFromHexName(fromqstr(newColor.name()));
        is_fontcolor = true;
-       changed();
+       change_adaptor();
 }
 
 
@@ -1671,7 +1716,7 @@ void GuiDocument::deleteFontColor()
        // save default color (black)
        set_fontcolor = rgbFromHexName("#000000");
        is_fontcolor = false;
-       changed();
+       change_adaptor();
 }
 
 
@@ -1686,7 +1731,7 @@ void GuiDocument::changeNoteFontColor()
                colorButtonStyleSheet(newColor));
        // save color
        set_notefontcolor = rgbFromHexName(fromqstr(newColor.name()));
-       changed();
+       change_adaptor();
 }
 
 
@@ -1696,7 +1741,7 @@ void GuiDocument::deleteNoteFontColor()
        theApp()->getRgbColor(Color_greyedouttext, set_notefontcolor);
        colorModule->noteFontColorPB->setStyleSheet(
                colorButtonStyleSheet(rgb2qcolor(set_notefontcolor)));
-       changed();
+       change_adaptor();
 }
 
 
@@ -1711,7 +1756,7 @@ void GuiDocument::changeBoxBackgroundColor()
                colorButtonStyleSheet(newColor));
        // save color
        set_boxbgcolor = rgbFromHexName(fromqstr(newColor.name()));
-       changed();
+       change_adaptor();
 }
 
 
@@ -1721,33 +1766,85 @@ void GuiDocument::deleteBoxBackgroundColor()
        theApp()->getRgbColor(Color_shadedbg, set_boxbgcolor);
        colorModule->boxBackgroundPB->setStyleSheet(
                colorButtonStyleSheet(rgb2qcolor(set_boxbgcolor)));
-       changed();
+       change_adaptor();
+}
+
+
+void GuiDocument::updateQuoteStyles(bool const set)
+{
+       Language const * lang = lyx::languages.getLanguage(
+               fromqstr(langModule->languageCO->itemData(
+                       langModule->languageCO->currentIndex()).toString()));
+
+       InsetQuotesParams::QuoteStyle def = bp_.getQuoteStyle(lang->quoteStyle());
+
+       langModule->quoteStyleCO->clear();
+
+       bool has_default = false;
+       for (int i = 0; i < quoteparams.stylescount(); ++i) {
+               InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::QuoteStyle(i);
+               if (qs == InsetQuotesParams::DynamicQuotes)
+                       continue;
+               bool const langdef = (qs == def);
+               if (langdef) {
+                       // add the default style on top
+                       langModule->quoteStyleCO->insertItem(0,
+                               toqstr(quoteparams.getGuiLabel(qs, langdef)), qs);
+                       has_default = true;
+               }
+               else
+                       langModule->quoteStyleCO->addItem(
+                               toqstr(quoteparams.getGuiLabel(qs, langdef)), qs);
+       }
+       if (set && has_default)
+               // (re)set to the default style
+               langModule->quoteStyleCO->setCurrentIndex(0);
 }
 
 
 void GuiDocument::languageChanged(int i)
 {
-       // some languages only work with polyglossia/XeTeX
+       // some languages only work with polyglossia
        Language const * lang = lyx::languages.getLanguage(
                fromqstr(langModule->languageCO->itemData(i).toString()));
        if (lang->babel().empty() && !lang->polyglossia().empty()) {
+                       // If we force to switch fontspec on, store
+                       // current state (#8717)
+                       if (fontModule->osFontsCB->isEnabled())
+                               forced_fontspec_activation =
+                                       !fontModule->osFontsCB->isChecked();
                        fontModule->osFontsCB->setChecked(true);
                        fontModule->osFontsCB->setEnabled(false);
        }
-       else
+       else {
                fontModule->osFontsCB->setEnabled(true);
+               // If we have forced to switch fontspec on,
+               // restore previous state (#8717)
+               if (forced_fontspec_activation)
+                       fontModule->osFontsCB->setChecked(false);
+               forced_fontspec_activation = false;
+       }
 
        // set appropriate quotation mark style
-       if (!lang->quoteStyle().empty()) {
-               langModule->quoteStyleCO->setCurrentIndex(
-                       bp_.getQuoteStyle(lang->quoteStyle()));
-       }
+       updateQuoteStyles(true);
 }
 
 
 void GuiDocument::osFontsChanged(bool nontexfonts)
 {
        bool const tex_fonts = !nontexfonts;
+       // store current fonts
+       QString const font_roman = fontModule->fontsRomanCO->itemData(
+                       fontModule->fontsRomanCO->currentIndex()).toString();
+       QString const font_sans = fontModule->fontsSansCO->itemData(
+                       fontModule->fontsSansCO->currentIndex()).toString();
+       QString const font_typewriter = fontModule->fontsTypewriterCO->itemData(
+                       fontModule->fontsTypewriterCO->currentIndex()).toString();
+       QString const font_math = fontModule->fontsMathCO->itemData(
+                       fontModule->fontsMathCO->currentIndex()).toString();
+       int const font_sf_scale = fontModule->scaleSansSB->value();
+       int const font_tt_scale = fontModule->scaleTypewriterSB->value();
+
        updateFontlist();
        // store default format
        QString const dformat = outputModule->defaultFormatCO->itemData(
@@ -1759,6 +1856,28 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        if (index == -1)
                index = 0;
        outputModule->defaultFormatCO->setCurrentIndex(index);
+
+       // try to restore fonts which were selected two toggles ago
+       index = fontModule->fontsRomanCO->findData(fontModule->font_roman);
+       if (index != -1)
+               fontModule->fontsRomanCO->setCurrentIndex(index);
+       index = fontModule->fontsSansCO->findData(fontModule->font_sans);
+       if (index != -1)
+               fontModule->fontsSansCO->setCurrentIndex(index);
+       index = fontModule->fontsTypewriterCO->findData(fontModule->font_typewriter);
+       if (index != -1)
+               fontModule->fontsTypewriterCO->setCurrentIndex(index);
+       index = fontModule->fontsMathCO->findData(fontModule->font_math);
+       if (index != -1)
+               fontModule->fontsMathCO->setCurrentIndex(index);
+       // save fonts for next next toggle
+       fontModule->font_roman = font_roman;
+       fontModule->font_sans = font_sans;
+       fontModule->font_typewriter = font_typewriter;
+       fontModule->font_math = font_math;
+       fontModule->font_sf_scale = font_sf_scale;
+       fontModule->font_tt_scale = font_tt_scale;
+
        langModule->encodingCO->setEnabled(tex_fonts &&
                !langModule->defaultencodingRB->isChecked());
        langModule->defaultencodingRB->setEnabled(tex_fonts);
@@ -1776,7 +1895,35 @@ void GuiDocument::osFontsChanged(bool nontexfonts)
        if (!tex_fonts)
                fontModule->fontencLE->setEnabled(false);
        else
-               fontencChanged(fontModule->fontencCO->currentIndex()); 
+               fontencChanged(fontModule->fontencCO->currentIndex());
+}
+
+
+void GuiDocument::mathFontChanged(int)
+{
+       updateFontOptions();
+}
+
+
+void GuiDocument::fontOsfToggled(bool state)
+{
+       if (fontModule->osFontsCB->isChecked())
+               return;
+       QString font = fontModule->fontsRomanCO->itemData(
+                       fontModule->fontsRomanCO->currentIndex()).toString();
+       if (hasMonolithicExpertSet(font))
+               fontModule->fontScCB->setChecked(state);
+}
+
+
+void GuiDocument::fontScToggled(bool state)
+{
+       if (fontModule->osFontsCB->isChecked())
+               return;
+       QString font = fontModule->fontsRomanCO->itemData(
+                       fontModule->fontsRomanCO->currentIndex()).toString();
+       if (hasMonolithicExpertSet(font))
+               fontModule->fontOsfCB->setChecked(state);
 }
 
 
@@ -1801,6 +1948,7 @@ void GuiDocument::updateFontOptions()
                                fontModule->fontsRomanCO->currentIndex()).toString();
        fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
+       updateMathFonts(font);
 }
 
 
@@ -1837,7 +1985,14 @@ bool GuiDocument::completeFontset() const
        return (fontModule->fontsSansCO->itemData(
                        fontModule->fontsSansCO->currentIndex()).toString() == "default"
                && fontModule->fontsSansCO->itemData(
-                       fontModule->fontsSansCO->currentIndex()).toString() == "default");
+                       fontModule->fontsTypewriterCO->currentIndex()).toString() == "default");
+}
+
+
+bool GuiDocument::noMathFont() const
+{
+       return (fontModule->fontsMathCO->itemData(
+               fontModule->fontsMathCO->currentIndex()).toString() == "default");
 }
 
 
@@ -1855,7 +2010,7 @@ void GuiDocument::updateTexFonts()
                }
                docstring const family = lf.family();
                docstring guiname = translateIfPossible(lf.guiname());
-               if (!lf.available(ot1()))
+               if (!lf.available(ot1(), noMathFont()))
                        guiname += _(" (not installed)");
                if (family == "rm")
                        rmfonts_.insert(toqstr(guiname), toqstr(it->first));
@@ -1863,6 +2018,8 @@ void GuiDocument::updateTexFonts()
                        sffonts_.insert(toqstr(guiname), toqstr(it->first));
                else if (family == "tt")
                        ttfonts_.insert(toqstr(guiname), toqstr(it->first));
+               else if (family == "math")
+                       mathfonts_.insert(toqstr(guiname), toqstr(it->first));
        }
 }
 
@@ -1872,12 +2029,18 @@ void GuiDocument::updateFontlist()
        fontModule->fontsRomanCO->clear();
        fontModule->fontsSansCO->clear();
        fontModule->fontsTypewriterCO->clear();
+       fontModule->fontsMathCO->clear();
 
-       // With XeTeX, we have access to all system fonts, but not the LaTeX fonts
+       // With fontspec (XeTeX, LuaTeX), we have access to all system fonts, but not the LaTeX fonts
        if (fontModule->osFontsCB->isChecked()) {
                fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
                fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
                fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
+               QString unimath = qt_("Non-TeX Fonts Default");
+               if (!LaTeXFeatures::isAvailable("unicode-math"))
+                       unimath += qt_(" (not available)");
+               fontModule->fontsMathCO->addItem(qt_("Class Default (TeX Fonts)"), QString("auto"));
+               fontModule->fontsMathCO->addItem(unimath, QString("default"));
 
                QFontDatabase fontdb;
                QStringList families(fontdb.families());
@@ -1898,20 +2061,28 @@ void GuiDocument::updateFontlist()
                fontModule->fontsRomanCO->addItem(rmi.key(), rmi.value());
                ++rmi;
        }
-       
+
        fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
        QMap<QString, QString>::const_iterator sfi = sffonts_.constBegin();
        while (sfi != sffonts_.constEnd()) {
                fontModule->fontsSansCO->addItem(sfi.key(), sfi.value());
                ++sfi;
        }
-       
+
        fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
        QMap<QString, QString>::const_iterator tti = ttfonts_.constBegin();
        while (tti != ttfonts_.constEnd()) {
                fontModule->fontsTypewriterCO->addItem(tti.key(), tti.value());
                ++tti;
        }
+
+       fontModule->fontsMathCO->addItem(qt_("Automatic"), QString("auto"));
+       fontModule->fontsMathCO->addItem(qt_("Class Default"), QString("default"));
+       QMap<QString, QString>::const_iterator mmi = mathfonts_.constBegin();
+       while (mmi != mathfonts_.constEnd()) {
+               fontModule->fontsMathCO->addItem(mmi.key(), mmi.value());
+               ++mmi;
+       }
 }
 
 
@@ -1925,6 +2096,24 @@ void GuiDocument::fontencChanged(int item)
 }
 
 
+void GuiDocument::updateMathFonts(QString const & rm)
+{
+       if (fontModule->osFontsCB->isChecked())
+               return;
+       QString const math =
+               fontModule->fontsMathCO->itemData(fontModule->fontsMathCO->currentIndex()).toString();
+       int const i = fontModule->fontsMathCO->findData("default");
+       if (providesNoMath(rm) && i == -1)
+               fontModule->fontsMathCO->insertItem(1, qt_("Class Default"), QString("default"));
+       else if (!providesNoMath(rm) && i != -1) {
+               int const c = fontModule->fontsMathCO->currentIndex();
+               fontModule->fontsMathCO->removeItem(i);
+               if (c == i)
+                       fontModule->fontsMathCO->setCurrentIndex(0);
+       }
+}
+
+
 void GuiDocument::romanChanged(int item)
 {
        if (fontModule->osFontsCB->isChecked())
@@ -1933,6 +2122,7 @@ void GuiDocument::romanChanged(int item)
                fontModule->fontsRomanCO->itemData(item).toString();
        fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
+       updateMathFonts(font);
 }
 
 
@@ -2006,9 +2196,9 @@ void GuiDocument::browseLayout()
 
        int const ret = Alert::prompt(_("Local layout file"),
                _("The layout file you have selected is a local layout\n"
-                 "file, not one in the system or user directory. Your\n"
-                 "document may not work with this layout if you do not\n"
-                 "keep the layout file in the document directory."),
+                 "file, not one in the system or user directory.\n"
+                 "Your document will not work with this layout if you\n"
+                 "move the layout file to a different directory."),
                  1, 1, _("&Set Layout"), _("&Cancel"));
        if (ret == 1)
                return;
@@ -2017,9 +2207,9 @@ void GuiDocument::browseLayout()
        LayoutFileList & bcl = LayoutFileList::get();
        string classname = layoutFile.onlyFileName();
        // this will update an existing layout if that layout has been loaded before.
-       LayoutFileIndex name = bcl.addLocalLayout(
+       LayoutFileIndex name = support::onlyFileName(bcl.addLocalLayout(
                classname.substr(0, classname.size() - 7),
-               layoutFile.onlyPath().absFileName());
+               layoutFile.onlyPath().absFileName()));
 
        if (name.empty()) {
                Alert::error(_("Error"),
@@ -2027,17 +2217,26 @@ void GuiDocument::browseLayout()
                return;
        }
 
+       const_cast<Buffer &>(buffer()).setLayoutPos(layoutFile.onlyPath().absFileName());
+
        // do not trigger classChanged if there is no change.
        if (latexModule->classCO->currentText() == toqstr(name))
                return;
 
        // add to combo box
-       int idx = latexModule->classCO->findText(toqstr(name));
-       if (idx == -1) {
-               classes_model_.insertRow(0, toqstr(name), name);
-               latexModule->classCO->setCurrentIndex(0);
-       } else
-               latexModule->classCO->setCurrentIndex(idx);
+       bool const avail = latexModule->classCO->set(toqstr(name));
+       if (!avail) {
+               LayoutFile const & tc = bcl[name];
+               docstring const guiname = translateIfPossible(from_utf8(tc.description()));
+               // tooltip sensu "KOMA-Script Article [Class 'scrartcl']"
+               QString tooltip = toqstr(bformat(_("%1$s [Class '%2$s']"), guiname, from_utf8(tc.latexname())));
+               tooltip += '\n' + qt_("This is a local layout file.");
+               latexModule->classCO->addItemSort(toqstr(tc.name()), toqstr(guiname),
+                                                 toqstr(translateIfPossible(from_utf8(tc.category()))),
+                                                 tooltip,
+                                                 true, true, true, true);
+               latexModule->classCO->set(toqstr(name));
+       }
 
        classChanged();
 }
@@ -2058,37 +2257,27 @@ void GuiDocument::browseMaster()
 }
 
 
+void GuiDocument::classChanged_adaptor()
+{
+       const_cast<Buffer &>(buffer()).setLayoutPos(string());
+       classChanged();
+}
+
+
 void GuiDocument::classChanged()
 {
        int idx = latexModule->classCO->currentIndex();
        if (idx < 0)
                return;
-       string const classname = classes_model_.getIDString(idx);
-
-       // check whether the selected modules have changed.
-       bool modules_changed = false;
-       unsigned int const srows = selectedModel()->rowCount();
-       if (srows != bp_.getModules().size())
-               modules_changed = true;
-       else {
-               list<string>::const_iterator mit = bp_.getModules().begin();
-               list<string>::const_iterator men = bp_.getModules().end();
-               for (unsigned int i = 0; i < srows && mit != men; ++i, ++mit)
-                       if (selectedModel()->getIDString(i) != *mit) {
-                               modules_changed = true;
-                               break;
-                       }
-       }
+       string const classname = fromqstr(latexModule->classCO->getData(idx));
 
-       if (modules_changed || lyxrc.auto_reset_options) {
-               if (applyPB->isEnabled()) {
-                       int const ret = Alert::prompt(_("Unapplied changes"),
-                                       _("Some changes in the dialog were not yet applied.\n"
-                                       "If you do not apply now, they will be lost after this action."),
-                                       1, 1, _("&Apply"), _("&Dismiss"));
-                       if (ret == 0)
-                               applyView();
-               }
+       if (applyPB->isEnabled()) {
+               int const ret = Alert::prompt(_("Unapplied changes"),
+                               _("Some changes in the dialog were not yet applied.\n"
+                               "If you do not apply now, they will be lost after this action."),
+                               1, 1, _("&Apply"), _("&Dismiss"));
+               if (ret == 0)
+                       applyView();
        }
 
        // We load the TextClass as soon as it is selected. This is
@@ -2124,34 +2313,142 @@ void GuiDocument::languagePackageChanged(int i)
 void GuiDocument::biblioChanged()
 {
        biblioChanged_ = true;
-       changed();
+       change_adaptor();
 }
 
 
-void GuiDocument::bibtexChanged(int n)
+void GuiDocument::rescanBibFiles()
 {
-       biblioModule->bibtexOptionsLE->setEnabled(
-               biblioModule->bibtexCO->itemData(n).toString() != "default");
+       if (isBiblatex())
+               rescanTexStyles("bbx cbx");
+       else
+               rescanTexStyles("bst");
+}
+
+
+void GuiDocument::resetDefaultBibfile(string const & which)
+{
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString();
+
+       CiteEngineType const cet =
+               CiteEngineType(biblioModule->citeStyleCO->itemData(
+                                                         biblioModule->citeStyleCO->currentIndex()).toInt());
+
+       updateDefaultBiblio(theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet), which);
+}
+
+
+void GuiDocument::resetDefaultBbxBibfile()
+{
+       resetDefaultBibfile("bbx");
+}
+
+
+void GuiDocument::resetDefaultCbxBibfile()
+{
+       resetDefaultBibfile("cbx");
+}
+
+
+void GuiDocument::citeEngineChanged(int n)
+{
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(n).toString();
+
+       vector<string> const engs =
+               theCiteEnginesList[fromqstr(engine)]->getEngineType();
+
+       updateCiteStyles(engs);
+       updateEngineDependends();
+       resetDefaultBibfile();
        biblioChanged();
 }
 
 
-void GuiDocument::setAuthorYear(bool authoryear)
+void GuiDocument::updateEngineDependends()
+{
+       bool const biblatex = isBiblatex();
+
+       // These are only useful with BibTeX
+       biblioModule->defaultBiblioCO->setEnabled(!biblatex);
+       biblioModule->bibtexStyleLA->setEnabled(!biblatex);
+       biblioModule->resetDefaultBiblioPB->setEnabled(!biblatex);
+       biblioModule->bibtopicCB->setEnabled(!biblatex);
+
+       // These are only useful with Biblatex
+       biblioModule->biblatexBbxCO->setEnabled(biblatex);
+       biblioModule->biblatexBbxLA->setEnabled(biblatex);
+       biblioModule->biblatexCbxCO->setEnabled(biblatex);
+       biblioModule->biblatexCbxLA->setEnabled(biblatex);
+       biblioModule->resetBbxPB->setEnabled(biblatex);
+       biblioModule->resetCbxPB->setEnabled(biblatex);
+       biblioModule->matchBbxPB->setEnabled(biblatex);
+
+       // These are useful with biblatex, jurabib and natbib
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString();
+       LyXCiteEngine const * ce = theCiteEnginesList[fromqstr(engine)];
+
+       bool const citepack = ce->requires("biblatex.sty") || ce->requires("jurabib.sty")
+                       || ce->requires("natbib.sty");
+       biblioModule->citePackageOptionsLE->setEnabled(citepack);
+       biblioModule->citePackageOptionsL->setEnabled(citepack);
+}
+
+
+void GuiDocument::citeStyleChanged()
 {
-       if (authoryear)
-               biblioModule->citeStyleCO->setCurrentIndex(0);
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString();
+       QString const currentDef = isBiblatex() ?
+               biblioModule->biblatexBbxCO->currentText()
+               : biblioModule->defaultBiblioCO->currentText();
+       if (theCiteEnginesList[fromqstr(engine)]->isDefaultBiblio(fromqstr(currentDef)))
+               resetDefaultBibfile();
+
        biblioChanged();
 }
 
 
-void GuiDocument::setNumerical(bool numerical)
+void GuiDocument::bibtexChanged(int n)
 {
-       if (numerical)
-               biblioModule->citeStyleCO->setCurrentIndex(1);
+       biblioModule->bibtexOptionsLE->setEnabled(
+               biblioModule->bibtexCO->itemData(n).toString() != "default");
        biblioChanged();
 }
 
 
+void GuiDocument::updateCiteStyles(vector<string> const & engs, CiteEngineType const & sel)
+{
+       biblioModule->citeStyleCO->clear();
+
+       vector<string>::const_iterator it  = engs.begin();
+       vector<string>::const_iterator end = engs.end();
+       for (; it != end; ++it) {
+               if (*it == "default")
+                       biblioModule->citeStyleCO->addItem(qt_("Basic numerical"),
+                                                          ENGINE_TYPE_DEFAULT);
+               else if (*it == "authoryear")
+                       biblioModule->citeStyleCO->addItem(qt_("Author-year"),
+                                                          ENGINE_TYPE_AUTHORYEAR);
+               else if (*it == "numerical")
+                       biblioModule->citeStyleCO->addItem(qt_("Author-number"),
+                                                          ENGINE_TYPE_NUMERICAL);
+       }
+       int i = biblioModule->citeStyleCO->findData(sel);
+       if (biblioModule->citeStyleCO->findData(sel) == -1)
+               i = 0;
+       biblioModule->citeStyleCO->setCurrentIndex(i);
+
+       biblioModule->citationStyleL->setEnabled(engs.size() > 1);
+       biblioModule->citeStyleCO->setEnabled(engs.size() > 1);
+}
+
+
 void GuiDocument::updateEngineType(string const & items, CiteEngineType const & sel)
 {
        engine_types_.clear();
@@ -2164,27 +2461,7 @@ void GuiDocument::updateEngineType(string const & items, CiteEngineType const &
                engine_types_.push_back(style);
        }
 
-       switch (sel) {
-               case ENGINE_TYPE_AUTHORYEAR:
-                       biblioModule->citeStyleCO->setCurrentIndex(0);
-                       break;
-               case ENGINE_TYPE_NUMERICAL:
-                       biblioModule->citeStyleCO->setCurrentIndex(1);
-                       break;
-       }
-
-       biblioModule->citationStyleL->setEnabled(nn > 1);
-       biblioModule->citeStyleCO->setEnabled(nn > 1);
-
-       if (nn != 1)
-               return;
-
-       // If the textclass allows only one of authoryear or numerical,
-       // we have no choice but to force that engine type.
-       if (engine_types_[0] == "authoryear")
-               biblioModule->citeStyleCO->setCurrentIndex(0);
-       else
-               biblioModule->citeStyleCO->setCurrentIndex(1);
+       updateCiteStyles(engine_types_, sel);
 }
 
 
@@ -2273,8 +2550,19 @@ void GuiDocument::modulesToParams(BufferParams & bp)
 void GuiDocument::modulesChanged()
 {
        modulesToParams(bp_);
+
+       if (applyPB->isEnabled() && nonModuleChanged_) {
+               int const ret = Alert::prompt(_("Unapplied changes"),
+                               _("Some changes in the dialog were not yet applied.\n"
+                               "If you do not apply now, they will be lost after this action."),
+                               1, 1, _("&Apply"), _("&Dismiss"));
+               if (ret == 0)
+                       applyView();
+       }
+
        bp_.makeDocumentClass();
        paramsToDialog();
+       changed();
 }
 
 
@@ -2366,7 +2654,7 @@ void GuiDocument::updateNumbering()
        DocumentClass::const_iterator len = tclass.end();
        for (; lit != len; ++lit) {
                int const toclevel = lit->toclevel;
-               if (toclevel != Layout::NOT_IN_TOC && lit->labeltype == LABEL_COUNTER) {
+               if (toclevel != Layout::NOT_IN_TOC && !lit->counter.empty()) {
                        item = new QTreeWidgetItem(numberingModule->tocTW);
                        item->setText(0, toqstr(translateIfPossible(lit->name())));
                        item->setText(1, (toclevel <= depth) ? yes : no);
@@ -2388,21 +2676,20 @@ void GuiDocument::updateDefaultFormat()
        param_copy.useNonTeXFonts = fontModule->osFontsCB->isChecked();
        int const idx = latexModule->classCO->currentIndex();
        if (idx >= 0) {
-               string const classname = classes_model_.getIDString(idx);
+               string const classname = fromqstr(latexModule->classCO->getData(idx));
                param_copy.setBaseClass(classname);
-               param_copy.makeDocumentClass();
+               param_copy.makeDocumentClass(true);
        }
        outputModule->defaultFormatCO->blockSignals(true);
        outputModule->defaultFormatCO->clear();
        outputModule->defaultFormatCO->addItem(qt_("Default"),
                                QVariant(QString("default")));
-       typedef vector<Format const *> Formats;
-       Formats formats = param_copy.exportableFormats(true);
-       Formats::const_iterator cit = formats.begin();
-       Formats::const_iterator end = formats.end();
-       for (; cit != end; ++cit)
-               outputModule->defaultFormatCO->addItem(qt_((*cit)->prettyname()),
-                               QVariant(toqstr((*cit)->name())));
+       FormatList const & formats =
+                               param_copy.exportableFormats(true);
+       for (Format const * f : formats)
+               outputModule->defaultFormatCO->addItem
+                       (toqstr(translateIfPossible(f->prettyname())),
+                        QVariant(toqstr(f->name())));
        outputModule->defaultFormatCO->blockSignals(false);
 }
 
@@ -2427,22 +2714,26 @@ void GuiDocument::applyView()
        bp_.use_refstyle  = latexModule->refstyleCB->isChecked();
 
        // biblio
-       if (biblioModule->citeNatbibRB->isChecked())
-               bp_.setCiteEngine("natbib");
-       else if (biblioModule->citeJurabibRB->isChecked())
-               bp_.setCiteEngine("jurabib");
+       string const engine =
+               fromqstr(biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString());
+       bp_.setCiteEngine(engine);
+
+       CiteEngineType const style = CiteEngineType(biblioModule->citeStyleCO->itemData(
+               biblioModule->citeStyleCO->currentIndex()).toInt());
+       if (theCiteEnginesList[engine]->hasEngineType(style))
+               bp_.setCiteEngineType(style);
        else
-               bp_.setCiteEngine("basic");
-
-       if (biblioModule->citeStyleCO->currentIndex())
-               bp_.setCiteEngineType(ENGINE_TYPE_NUMERICAL);
-       else
-               bp_.setCiteEngineType(ENGINE_TYPE_AUTHORYEAR);
+               bp_.setCiteEngineType(ENGINE_TYPE_DEFAULT);
 
        bp_.use_bibtopic =
                biblioModule->bibtopicCB->isChecked();
 
-       bp_.biblio_style = fromqstr(biblioModule->bibtexStyleLE->text());
+       bp_.setDefaultBiblioStyle(fromqstr(biblioModule->defaultBiblioCO->currentText()));
+
+       bp_.biblatex_bibstyle = fromqstr(biblioModule->biblatexBbxCO->currentText());
+       bp_.biblatex_citestyle = fromqstr(biblioModule->biblatexCbxCO->currentText());
+       bp_.biblio_opts = fromqstr(biblioModule->citePackageOptionsLE->text());
 
        string const bibtex_command =
                fromqstr(biblioModule->bibtexCO->itemData(
@@ -2476,8 +2767,9 @@ void GuiDocument::applyView()
                        Encodings::const_iterator const end = encodings.end();
                        bool found = false;
                        for (; it != end; ++it) {
-                               if (qt_(it->guiName()) == enc_gui) {
-                                       bp_.inputenc = it->latexName();
+                               if (qt_(it->guiName()) == enc_gui &&
+                                   !it->unsafe()) {
+                                       bp_.inputenc = it->name();
                                        found = true;
                                        break;
                                }
@@ -2490,12 +2782,20 @@ void GuiDocument::applyView()
                }
        }
 
-       bp_.quotes_language = (InsetQuotes::QuoteLanguage) langModule->quoteStyleCO->itemData(
+       bp_.quotes_style = (InsetQuotesParams::QuoteStyle) langModule->quoteStyleCO->itemData(
                langModule->quoteStyleCO->currentIndex()).toInt();
+       bp_.dynamic_quotes = langModule->dynamicQuotesCB->isChecked();
 
-       QString const lang = langModule->languageCO->itemData(
+       QString const langname = langModule->languageCO->itemData(
                langModule->languageCO->currentIndex()).toString();
-       bp_.language = lyx::languages.getLanguage(fromqstr(lang));
+       Language const * newlang = lyx::languages.getLanguage(fromqstr(langname));
+       Cursor & cur = const_cast<BufferView *>(bufferview())->cursor();
+       // If current cursor language was the document language, then update it too.
+       if (cur.current_font.language() == bp_.language) {
+               cur.current_font.setLanguage(newlang);
+               cur.real_current_font.setLanguage(newlang);
+       }
+       bp_.language = newlang;
 
        QString const pack = langModule->languagePackageCO->itemData(
                langModule->languagePackageCO->currentIndex()).toString();
@@ -2532,7 +2832,7 @@ void GuiDocument::applyView()
        // text layout
        int idx = latexModule->classCO->currentIndex();
        if (idx >= 0) {
-               string const classname = classes_model_.getIDString(idx);
+               string const classname = fromqstr(latexModule->classCO->getData(idx));
                bp_.setBaseClass(classname);
        }
 
@@ -2540,20 +2840,26 @@ void GuiDocument::applyView()
        modulesToParams(bp_);
 
        // Math
-       vector<string> const & packages = BufferParams::auto_packages();
-        for (size_t n = 0; n < packages.size(); ++n) {
-               QCheckBox * autoCB = static_cast<QCheckBox *>(
-                       mathsModule->gridLayout->itemAtPosition(2 * n, 0)->widget());
-               if (autoCB->isChecked())
-                       bp_.use_package(packages[n], BufferParams::package_auto);
-               else {
-                       QCheckBox * alwaysCB = static_cast<QCheckBox *>(
-                               mathsModule->gridLayout->itemAtPosition(2 * n + 1, 0)->widget());
-                       if (alwaysCB->isChecked())
-                               bp_.use_package(packages[n], BufferParams::package_on);
-                       else
-                               bp_.use_package(packages[n], BufferParams::package_off);
+       map<string, string> const & packages = BufferParams::auto_packages();
+       for (map<string, string>::const_iterator it = packages.begin();
+            it != packages.end(); ++it) {
+               QTableWidgetItem * item = mathsModule->packagesTW->findItems(toqstr(it->first), Qt::MatchExactly)[0];
+               if (!item)
+                       continue;
+               int row = mathsModule->packagesTW->row(item);
+               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
+               if (rb->isChecked()) {
+                       bp_.use_package(it->first, BufferParams::package_auto);
+                       continue;
+               }
+               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
+               if (rb->isChecked()) {
+                       bp_.use_package(it->first, BufferParams::package_on);
+                       continue;
                }
+               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
+               if (rb->isChecked())
+                       bp_.use_package(it->first, BufferParams::package_off);
        }
 
        // Page Layout
@@ -2674,7 +2980,7 @@ void GuiDocument::applyView()
        bp_.listings_params =
                InsetListingsParams(fromqstr(listingsModule->listingsED->toPlainText())).params();
 
-       // output
+       // Formats
        bp_.default_output_format = fromqstr(outputModule->defaultFormatCO->itemData(
                outputModule->defaultFormatCO->currentIndex()).toString());
 
@@ -2682,7 +2988,7 @@ void GuiDocument::applyView()
        bp_.useNonTeXFonts = nontexfonts;
 
        bp_.output_sync = outputModule->outputsyncCB->isChecked();
-       
+
        bp_.output_sync_macro = fromqstr(outputModule->synccustomCB->currentText());
 
        int mathfmt = outputModule->mathoutCB->currentIndex();
@@ -2694,19 +3000,31 @@ void GuiDocument::applyView()
        bp_.html_be_strict = outputModule->strictCB->isChecked();
        bp_.html_css_as_file = outputModule->cssCB->isChecked();
        bp_.html_math_img_scale = outputModule->mathimgSB->value();
+       bp_.display_pixel_ratio = theGuiApp()->pixelRatio();
+
+       bp_.save_transient_properties =
+               outputModule->saveTransientPropertiesCB->isChecked();
 
        // fonts
-       bp_.fonts_roman =
+       bp_.fonts_roman[nontexfonts] =
                fromqstr(fontModule->fontsRomanCO->
                        itemData(fontModule->fontsRomanCO->currentIndex()).toString());
+       bp_.fonts_roman[!nontexfonts] = fromqstr(fontModule->font_roman);
 
-       bp_.fonts_sans =
+       bp_.fonts_sans[nontexfonts] =
                fromqstr(fontModule->fontsSansCO->
                        itemData(fontModule->fontsSansCO->currentIndex()).toString());
+       bp_.fonts_sans[!nontexfonts] = fromqstr(fontModule->font_sans);
 
-       bp_.fonts_typewriter =
+       bp_.fonts_typewriter[nontexfonts] =
                fromqstr(fontModule->fontsTypewriterCO->
                        itemData(fontModule->fontsTypewriterCO->currentIndex()).toString());
+       bp_.fonts_typewriter[!nontexfonts] = fromqstr(fontModule->font_typewriter);
+
+       bp_.fonts_math[nontexfonts] =
+               fromqstr(fontModule->fontsMathCO->
+                       itemData(fontModule->fontsMathCO->currentIndex()).toString());
+       bp_.fonts_math[!nontexfonts] = fromqstr(fontModule->font_math);
 
        QString const fontenc =
                fontModule->fontencCO->itemData(fontModule->fontencCO->currentIndex()).toString();
@@ -2718,9 +3036,13 @@ void GuiDocument::applyView()
        bp_.fonts_cjk =
                fromqstr(fontModule->cjkFontLE->text());
 
-       bp_.fonts_sans_scale = fontModule->scaleSansSB->value();
+       bp_.use_microtype = fontModule->microtypeCB->isChecked();
+
+       bp_.fonts_sans_scale[nontexfonts] = fontModule->scaleSansSB->value();
+       bp_.fonts_sans_scale[!nontexfonts] = fontModule->font_sf_scale;
 
-       bp_.fonts_typewriter_scale = fontModule->scaleTypewriterSB->value();
+       bp_.fonts_typewriter_scale[nontexfonts] = fontModule->scaleTypewriterSB->value();
+       bp_.fonts_typewriter_scale[!nontexfonts] = fontModule->font_tt_scale;
 
        bp_.fonts_expert_sc = fontModule->fontScCB->isChecked();
 
@@ -2800,6 +3122,9 @@ void GuiDocument::applyView()
                pdf.pagemode.clear();
        pdf.quoted_options = pdf.quoted_options_check(
                                fromqstr(pdfSupportModule->optionsLE->text()));
+
+       // reset tracker
+       nonModuleChanged_ = false;
 }
 
 
@@ -2819,25 +3144,27 @@ void GuiDocument::paramsToDialog()
        // biblio
        string const cite_engine = bp_.citeEngine().list().front();
 
-       biblioModule->citeDefaultRB->setChecked(
-               cite_engine == "basic");
-
-       biblioModule->citeJurabibRB->setChecked(
-               cite_engine == "jurabib");
-
-       biblioModule->citeNatbibRB->setChecked(
-               cite_engine == "natbib");
-
-       biblioModule->citeStyleCO->setCurrentIndex(
-               bp_.citeEngineType() == ENGINE_TYPE_NUMERICAL);
+       biblioModule->citeEngineCO->setCurrentIndex(
+               biblioModule->citeEngineCO->findData(toqstr(cite_engine)));
 
        updateEngineType(documentClass().opt_enginetype(),
                bp_.citeEngineType());
 
+       biblioModule->citeStyleCO->setCurrentIndex(
+               biblioModule->citeStyleCO->findData(bp_.citeEngineType()));
+
        biblioModule->bibtopicCB->setChecked(
                bp_.use_bibtopic);
 
-       biblioModule->bibtexStyleLE->setText(toqstr(bp_.biblio_style));
+       updateEngineDependends();
+
+       if (isBiblatex()) {
+               updateDefaultBiblio(bp_.biblatex_bibstyle, "bbx");
+               updateDefaultBiblio(bp_.biblatex_citestyle, "cbx");
+       } else
+               updateDefaultBiblio(bp_.defaultBiblioStyle());
+
+       biblioModule->citePackageOptionsLE->setText(toqstr(bp_.biblio_opts));
 
        string command;
        string options =
@@ -2860,15 +3187,21 @@ void GuiDocument::paramsToDialog()
        biblioChanged_ = false;
 
        // indices
-       indicesModule->update(bp_);
+       // We may be called when there is no Buffer, e.g., when 
+       // the last view has just been closed.
+       bool const isReadOnly = isBufferAvailable() ? buffer().isReadonly() : false;
+       indicesModule->update(bp_, isReadOnly);
 
        // language & quotes
        int const pos = langModule->languageCO->findData(toqstr(
                bp_.language->lang()));
        langModule->languageCO->setCurrentIndex(pos);
 
+       updateQuoteStyles();
+
        langModule->quoteStyleCO->setCurrentIndex(
-               bp_.quotes_language);
+               langModule->quoteStyleCO->findData(bp_.quotes_style));
+       langModule->dynamicQuotesCB->setChecked(bp_.dynamic_quotes);
 
        bool default_enc = true;
        if (bp_.inputenc != "auto") {
@@ -2880,7 +3213,8 @@ void GuiDocument::paramsToDialog()
                        Encodings::const_iterator it = encodings.begin();
                        Encodings::const_iterator const end = encodings.end();
                        for (; it != end; ++it) {
-                               if (it->latexName() == bp_.inputenc) {
+                               if (it->name() == bp_.inputenc &&
+                                   !it->unsafe()) {
                                        enc_gui = it->guiName();
                                        break;
                                }
@@ -2960,14 +3294,30 @@ void GuiDocument::paramsToDialog()
                latexModule->psdriverCO->setCurrentIndex(nitem);
        updateModuleInfo();
 
-       vector<string> const & packages = BufferParams::auto_packages();
-        for (size_t n = 0; n < packages.size(); ++n) {
-               QCheckBox * alwaysCB = static_cast<QCheckBox *>(
-                       mathsModule->gridLayout->itemAtPosition(2 * n + 1, 0)->widget());
-               alwaysCB->setChecked(bp_.use_package(packages[n]) == BufferParams::package_on);
-               QCheckBox * autoCB = static_cast<QCheckBox *>(
-                       mathsModule->gridLayout->itemAtPosition(2 * n, 0)->widget());
-               autoCB->setChecked(bp_.use_package(packages[n]) == BufferParams::package_auto);
+       map<string, string> const & packages = BufferParams::auto_packages();
+       for (map<string, string>::const_iterator it = packages.begin();
+            it != packages.end(); ++it) {
+               QTableWidgetItem * item = mathsModule->packagesTW->findItems(toqstr(it->first), Qt::MatchExactly)[0];
+               if (!item)
+                       continue;
+               int row = mathsModule->packagesTW->row(item);
+               switch (bp_.use_package(it->first)) {
+                       case BufferParams::package_off: {
+                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
+                               rb->setChecked(true);
+                               break;
+                       }
+                       case BufferParams::package_on: {
+                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
+                               rb->setChecked(true);
+                               break;
+                       }
+                       case BufferParams::package_auto: {
+                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
+                               rb->setChecked(true);
+                               break;
+                       }
+               }
        }
 
        switch (bp_.spacing().getSpace()) {
@@ -3081,11 +3431,11 @@ void GuiDocument::paramsToDialog()
        if (!bufferview() || !buffer().hasChildren()) {
                masterChildModule->childrenTW->clear();
                includeonlys_.clear();
-               docPS->showPanel(qt_("Child Documents"), false);
-               if (docPS->isCurrentPanel(qt_("Child Documents")))
-                       docPS->setCurrentPanel(qt_("Document Class"));
+               docPS->showPanel("Child Documents", false);
+               if (docPS->isCurrentPanel("Child Documents"))
+                       docPS->setCurrentPanel("Document Class");
        } else {
-               docPS->showPanel(qt_("Child Documents"), true);
+               docPS->showPanel("Child Documents", true);
                masterChildModule->setEnabled(true);
                includeonlys_ = bp_.getIncludedChildren();
                updateIncludeonlys();
@@ -3103,38 +3453,56 @@ void GuiDocument::paramsToDialog()
        listingsModule->listingsED->setPlainText(toqstr(lstparams));
 
        // Fonts
+       // some languages only work with polyglossia/XeTeX
+       Language const * lang = lyx::languages.getLanguage(
+               fromqstr(langModule->languageCO->itemData(
+                       langModule->languageCO->currentIndex()).toString()));
+       bool const need_fontspec =
+               lang->babel().empty() && !lang->polyglossia().empty();
        bool const os_fonts_available =
                bp_.baseClass()->outputType() == lyx::LATEX
                && LaTeXFeatures::isAvailable("fontspec");
-       fontModule->osFontsCB->setEnabled(os_fonts_available);
+       fontModule->osFontsCB->setEnabled(os_fonts_available && !need_fontspec);
        fontModule->osFontsCB->setChecked(
-               os_fonts_available && bp_.useNonTeXFonts);
+               (os_fonts_available && bp_.useNonTeXFonts) || need_fontspec);
        updateFontsize(documentClass().opt_fontsize(),
                        bp_.fontsize);
 
-       QString font = toqstr(bp_.fonts_roman);
+       QString font = toqstr(bp_.fontsRoman());
        int rpos = fontModule->fontsRomanCO->findData(font);
        if (rpos == -1) {
                rpos = fontModule->fontsRomanCO->count();
                fontModule->fontsRomanCO->addItem(font + qt_(" (not installed)"), font);
        }
        fontModule->fontsRomanCO->setCurrentIndex(rpos);
+       fontModule->font_roman = toqstr(bp_.fonts_roman[!bp_.useNonTeXFonts]);
 
-       font = toqstr(bp_.fonts_sans);
+       font = toqstr(bp_.fontsSans());
        int spos = fontModule->fontsSansCO->findData(font);
        if (spos == -1) {
                spos = fontModule->fontsSansCO->count();
                fontModule->fontsSansCO->addItem(font + qt_(" (not installed)"), font);
        }
        fontModule->fontsSansCO->setCurrentIndex(spos);
+       fontModule->font_sans = toqstr(bp_.fonts_sans[!bp_.useNonTeXFonts]);
 
-       font = toqstr(bp_.fonts_typewriter);
+       font = toqstr(bp_.fontsTypewriter());
        int tpos = fontModule->fontsTypewriterCO->findData(font);
        if (tpos == -1) {
                tpos = fontModule->fontsTypewriterCO->count();
                fontModule->fontsTypewriterCO->addItem(font + qt_(" (not installed)"), font);
        }
        fontModule->fontsTypewriterCO->setCurrentIndex(tpos);
+       fontModule->font_typewriter = toqstr(bp_.fonts_typewriter[!bp_.useNonTeXFonts]);
+
+       font = toqstr(bp_.fontsMath());
+       int mpos = fontModule->fontsMathCO->findData(font);
+       if (mpos == -1) {
+               mpos = fontModule->fontsMathCO->count();
+               fontModule->fontsMathCO->addItem(font + qt_(" (not installed)"), font);
+       }
+       fontModule->fontsMathCO->setCurrentIndex(mpos);
+       fontModule->font_math = toqstr(bp_.fonts_math[!bp_.useNonTeXFonts]);
 
        if (bp_.useNonTeXFonts && os_fonts_available) {
                fontModule->fontencLA->setEnabled(false);
@@ -3154,11 +3522,15 @@ void GuiDocument::paramsToDialog()
                        toqstr(bp_.fonts_cjk));
        else
                fontModule->cjkFontLE->setText(QString());
+       
+       fontModule->microtypeCB->setChecked(bp_.use_microtype);
 
        fontModule->fontScCB->setChecked(bp_.fonts_expert_sc);
        fontModule->fontOsfCB->setChecked(bp_.fonts_old_figures);
-       fontModule->scaleSansSB->setValue(bp_.fonts_sans_scale);
-       fontModule->scaleTypewriterSB->setValue(bp_.fonts_typewriter_scale);
+       fontModule->scaleSansSB->setValue(bp_.fontsSansScale());
+       fontModule->font_sf_scale = bp_.fonts_sans_scale[!bp_.useNonTeXFonts];
+       fontModule->scaleTypewriterSB->setValue(bp_.fontsTypewriterScale());
+       fontModule->font_tt_scale = bp_.fonts_typewriter_scale[!bp_.useNonTeXFonts];
 
        int nn = findToken(GuiDocument::fontfamilies, bp_.fonts_default_family);
        if (nn >= 0)
@@ -3173,7 +3545,7 @@ void GuiDocument::paramsToDialog()
                fontModule->fontencLE->setText(toqstr(bp_.fontenc));
        }
 
-       // Output
+       // Formats
        // This must be set _after_ fonts since updateDefaultFormat()
        // checks osFontsCB settings.
        // update combobox with formats
@@ -3193,6 +3565,9 @@ void GuiDocument::paramsToDialog()
        outputModule->strictCB->setChecked(bp_.html_be_strict);
        outputModule->cssCB->setChecked(bp_.html_css_as_file);
 
+       outputModule->saveTransientPropertiesCB
+               ->setChecked(bp_.save_transient_properties);
+
        // paper
        bool const extern_geometry =
                documentClass().provides("geometry");
@@ -3288,6 +3663,9 @@ void GuiDocument::paramsToDialog()
 
        // clear changed branches cache
        changedBranches_.clear();
+
+       // reset tracker
+       nonModuleChanged_ = false;
 }
 
 
@@ -3302,12 +3680,16 @@ void GuiDocument::saveDocDefault()
 void GuiDocument::updateAvailableModules()
 {
        modules_av_model_.clear();
-       list<modInfoStruct> const & modInfoList = getModuleInfo();
-       list<modInfoStruct>::const_iterator mit = modInfoList.begin();
-       list<modInfoStruct>::const_iterator men = modInfoList.end();
-       for (int i = 0; mit != men; ++mit, ++i)
-               modules_av_model_.insertRow(i, mit->name, mit->id,
-                               mit->description);
+       list<modInfoStruct> modInfoList = getModuleInfo();
+       // Sort names according to the locale
+       modInfoList.sort([](modInfoStruct const & a, modInfoStruct const & b) {
+                       return 0 < b.name.localeAwareCompare(a.name);
+               });
+       int i = 0;
+       for (modInfoStruct const & m : modInfoList) {
+               modules_av_model_.insertRow(i, m.name, m.id, m.description);
+               ++i;
+       }
 }
 
 
@@ -3315,11 +3697,11 @@ void GuiDocument::updateSelectedModules()
 {
        modules_sel_model_.clear();
        list<modInfoStruct> const selModList = getSelectedModules();
-       list<modInfoStruct>::const_iterator mit = selModList.begin();
-       list<modInfoStruct>::const_iterator men = selModList.end();
-       for (int i = 0; mit != men; ++mit, ++i)
-               modules_sel_model_.insertRow(i, mit->name, mit->id,
-                               mit->description);
+       int i = 0;
+       for (modInfoStruct const & m : selModList) {
+               modules_sel_model_.insertRow(i, m.name, m.id, m.description);
+               ++i;
+       }
 }
 
 
@@ -3338,14 +3720,13 @@ void GuiDocument::updateIncludeonlys()
                masterChildModule->childrenTW->setEnabled(true);
                masterChildModule->maintainAuxCB->setEnabled(true);
        }
-       QTreeWidgetItem * item = 0;
        ListOfBuffers children = buffer().getChildren();
        ListOfBuffers::const_iterator it  = children.begin();
        ListOfBuffers::const_iterator end = children.end();
        bool has_unincluded = false;
        bool all_unincluded = true;
        for (; it != end; ++it) {
-               item = new QTreeWidgetItem(masterChildModule->childrenTW);
+               QTreeWidgetItem * item = new QTreeWidgetItem(masterChildModule->childrenTW);
                // FIXME Unicode
                string const name =
                        to_utf8(makeRelPath(from_utf8((*it)->fileName().absFileName()),
@@ -3371,6 +3752,154 @@ void GuiDocument::updateIncludeonlys()
 }
 
 
+bool GuiDocument::isBiblatex() const
+{
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString();
+
+       return theCiteEnginesList[fromqstr(engine)]->getCiteFramework() == "biblatex";
+}
+
+
+void GuiDocument::updateDefaultBiblio(string const & style,
+                                     string const & which)
+{
+       QString const bibstyle = toqstr(style);
+       biblioModule->defaultBiblioCO->clear();
+
+       int item_nr = -1;
+
+       if (isBiblatex()) {
+               if (which != "cbx") {
+                       // First the bbx styles
+                       biblioModule->biblatexBbxCO->clear();
+                       QStringList str = texFileList("bbxFiles.lst");
+                       // test whether we have a valid list, otherwise run rescan
+                       if (str.isEmpty()) {
+                               rescanTexStyles("bbx");
+                               str = texFileList("bbxFiles.lst");
+                       }
+                       for (int i = 0; i != str.size(); ++i)
+                               str[i] = onlyFileName(str[i]);
+                       // sort on filename only (no path)
+                       str.sort();
+
+                       for (int i = 0; i != str.count(); ++i) {
+                               QString item = changeExtension(str[i], "");
+                               if (item == bibstyle)
+                                       item_nr = i;
+                               biblioModule->biblatexBbxCO->addItem(item);
+                       }
+
+                       if (item_nr == -1 && !bibstyle.isEmpty()) {
+                               biblioModule->biblatexBbxCO->addItem(bibstyle);
+                               item_nr = biblioModule->biblatexBbxCO->count() - 1;
+                       }
+
+                       if (item_nr != -1)
+                               biblioModule->biblatexBbxCO->setCurrentIndex(item_nr);
+                       else
+                               biblioModule->biblatexBbxCO->clearEditText();
+               }
+
+               if (which != "bbx") {
+                       // now the cbx styles
+                       biblioModule->biblatexCbxCO->clear();
+                       QStringList str = texFileList("cbxFiles.lst");
+                       // test whether we have a valid list, otherwise run rescan
+                       if (str.isEmpty()) {
+                               rescanTexStyles("cbx");
+                               str = texFileList("cbxFiles.lst");
+                       }
+                       for (int i = 0; i != str.size(); ++i)
+                               str[i] = onlyFileName(str[i]);
+                       // sort on filename only (no path)
+                       str.sort();
+
+                       for (int i = 0; i != str.count(); ++i) {
+                               QString item = changeExtension(str[i], "");
+                               if (item == bibstyle)
+                                       item_nr = i;
+                               biblioModule->biblatexCbxCO->addItem(item);
+                       }
+
+                       if (item_nr == -1 && !bibstyle.isEmpty()) {
+                               biblioModule->biblatexCbxCO->addItem(bibstyle);
+                               item_nr = biblioModule->biblatexCbxCO->count() - 1;
+                       }
+
+                       if (item_nr != -1)
+                               biblioModule->biblatexCbxCO->setCurrentIndex(item_nr);
+                       else
+                               biblioModule->biblatexCbxCO->clearEditText();
+               }
+       } else {// BibTeX
+               biblioModule->biblatexBbxCO->clear();
+               biblioModule->biblatexCbxCO->clear();
+               QStringList str = texFileList("bstFiles.lst");
+               // test whether we have a valid list, otherwise run rescan
+               if (str.isEmpty()) {
+                       rescanTexStyles("bst");
+                       str = texFileList("bstFiles.lst");
+               }
+               for (int i = 0; i != str.size(); ++i)
+                       str[i] = onlyFileName(str[i]);
+               // sort on filename only (no path)
+               str.sort();
+
+               for (int i = 0; i != str.count(); ++i) {
+                       QString item = changeExtension(str[i], "");
+                       if (item == bibstyle)
+                               item_nr = i;
+                       biblioModule->defaultBiblioCO->addItem(item);
+               }
+
+               if (item_nr == -1 && !bibstyle.isEmpty()) {
+                       biblioModule->defaultBiblioCO->addItem(bibstyle);
+                       item_nr = biblioModule->defaultBiblioCO->count() - 1;
+               }
+
+               if (item_nr != -1)
+                       biblioModule->defaultBiblioCO->setCurrentIndex(item_nr);
+               else
+                       biblioModule->defaultBiblioCO->clearEditText();
+       }
+
+       updateResetDefaultBiblio();
+}
+
+
+void GuiDocument::updateResetDefaultBiblio()
+{
+       QString const engine =
+               biblioModule->citeEngineCO->itemData(
+                               biblioModule->citeEngineCO->currentIndex()).toString();
+       CiteEngineType const cet =
+               CiteEngineType(biblioModule->citeStyleCO->itemData(
+                                                         biblioModule->citeStyleCO->currentIndex()).toInt());
+
+       string const defbib = theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet);
+       if (isBiblatex()) {
+               QString const bbx = biblioModule->biblatexBbxCO->currentText();
+               QString const cbx = biblioModule->biblatexCbxCO->currentText();
+               biblioModule->resetCbxPB->setEnabled(defbib != fromqstr(cbx));
+               biblioModule->resetBbxPB->setEnabled(defbib != fromqstr(bbx));
+               biblioModule->matchBbxPB->setEnabled(bbx != cbx && !cbx.isEmpty()
+                       && biblioModule->biblatexBbxCO->findText(cbx) != -1);
+       } else
+               biblioModule->resetDefaultBiblioPB->setEnabled(
+                       defbib != fromqstr(biblioModule->defaultBiblioCO->currentText()));
+}
+
+
+void GuiDocument::matchBiblatexStyles()
+{
+       updateDefaultBiblio(fromqstr(biblioModule->biblatexCbxCO->currentText()), "bbx");
+       biblioChanged();
+}
+
+
 void GuiDocument::updateContents()
 {
        // Nothing to do here as the document settings is not cursor dependant.
@@ -3390,7 +3919,7 @@ void GuiDocument::useClassDefaults()
        }
 
        int idx = latexModule->classCO->currentIndex();
-       string const classname = classes_model_.getIDString(idx);
+       string const classname = fromqstr(latexModule->classCO->getData(idx));
        if (!bp_.setBaseClass(classname)) {
                Alert::error(_("Error"), _("Unable to set document class."));
                return;
@@ -3402,12 +3931,9 @@ void GuiDocument::useClassDefaults()
 
 void GuiDocument::setLayoutComboByIDString(string const & idString)
 {
-       int idx = classes_model_.findIDString(idString);
-       if (idx < 0)
+       if (!latexModule->classCO->set(toqstr(idString)))
                Alert::warning(_("Can't set layout!"),
                        bformat(_("Unable to set layout for ID: %1$s"), from_utf8(idString)));
-       else
-               latexModule->classCO->setCurrentIndex(idx);
 }
 
 
@@ -3485,20 +4011,18 @@ list<GuiDocument::modInfoStruct> const & GuiDocument::getModuleInfo()
 
 
 list<GuiDocument::modInfoStruct> const
-               GuiDocument::makeModuleInfo(LayoutModuleList const & mods)
+GuiDocument::makeModuleInfo(LayoutModuleList const & mods)
 {
-       LayoutModuleList::const_iterator it =  mods.begin();
-       LayoutModuleList::const_iterator end = mods.end();
        list<modInfoStruct> mInfo;
-       for (; it != end; ++it) {
+       for (string const & name : mods) {
                modInfoStruct m;
-               m.id = *it;
-               LyXModule const * const mod = theModuleList[*it];
+               LyXModule const * const mod = theModuleList[name];
                if (mod)
-                       // FIXME Unicode
-                       m.name = toqstr(translateIfPossible(from_utf8(mod->getName())));
-               else
-                       m.name = toqstr(*it) + toqstr(" (") + qt_("Not Found") + toqstr(")");
+                       m = modInfo(*mod);
+               else {
+                       m.id = name;
+                       m.name = toqstr(name + " (") + qt_("Not Found") + toqstr(")");
+               }
                mInfo.push_back(m);
        }
        return mInfo;
@@ -3524,11 +4048,11 @@ DocumentClass const & GuiDocument::documentClass() const
 
 
 static void dispatch_bufferparams(Dialog const & dialog,
-       BufferParams const & bp, FuncCode lfun)
+       BufferParams const & bp, FuncCode lfun, Buffer const * buf)
 {
        ostringstream ss;
        ss << "\\begin_header\n";
-       bp.writeFile(ss);
+       bp.writeFile(ss, buf);
        ss << "\\end_header\n";
        dialog.dispatch(FuncRequest(lfun, ss.str()));
 }
@@ -3536,12 +4060,17 @@ static void dispatch_bufferparams(Dialog const & dialog,
 
 void GuiDocument::dispatchParams()
 {
+       // We need a non-const buffer object.
+       Buffer & buf = const_cast<BufferView *>(bufferview())->buffer();
+       // There may be several undo records; group them (bug #8998)
+       buf.undo().beginUndoGroup();
+
        // This must come first so that a language change is correctly noticed
        setLanguage();
 
        // Apply the BufferParams. Note that this will set the base class
        // and then update the buffer's layout.
-       dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY);
+       dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY, &buffer());
 
        if (!params().master.empty()) {
                FileName const master_file = support::makeAbsPath(params().master,
@@ -3605,6 +4134,10 @@ void GuiDocument::dispatchParams()
        // If we used an LFUN, we would not need these two lines:
        BufferView * bv = const_cast<BufferView *>(bufferview());
        bv->processUpdateFlags(Update::Force | Update::FitCursor);
+
+       // Don't forget to close the group. Note that it is important
+       // to check that there is no early return in the method.
+       buf.undo().endUndoGroup();
 }
 
 
@@ -3621,7 +4154,7 @@ void GuiDocument::setLanguage() const
 
 void GuiDocument::saveAsDefault() const
 {
-       dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
+       dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT, &buffer());
 }
 
 
@@ -3632,7 +4165,9 @@ bool GuiDocument::providesOSF(QString const & font) const
                // have OSF support. But how?
                return true;
        return theLaTeXFonts().getLaTeXFont(
-                               qstring_to_ucs4(font)).providesOSF(ot1(), completeFontset());
+                               qstring_to_ucs4(font)).providesOSF(ot1(),
+                                                                  completeFontset(),
+                                                                  noMathFont());
 }
 
 
@@ -3641,7 +4176,9 @@ bool GuiDocument::providesSC(QString const & font) const
        if (fontModule->osFontsCB->isChecked())
                return false;
        return theLaTeXFonts().getLaTeXFont(
-                               qstring_to_ucs4(font)).providesSC(ot1(), completeFontset());
+                               qstring_to_ucs4(font)).providesSC(ot1(),
+                                                                 completeFontset(),
+                                                                 noMathFont());
 }
 
 
@@ -3650,31 +4187,62 @@ bool GuiDocument::providesScale(QString const & font) const
        if (fontModule->osFontsCB->isChecked())
                return true;
        return theLaTeXFonts().getLaTeXFont(
-                               qstring_to_ucs4(font)).providesScale(ot1(), completeFontset());
+                               qstring_to_ucs4(font)).providesScale(ot1(),
+                                                                    completeFontset(),
+                                                                    noMathFont());
+}
+
+
+bool GuiDocument::providesNoMath(QString const & font) const
+{
+       if (fontModule->osFontsCB->isChecked())
+               return false;
+       return theLaTeXFonts().getLaTeXFont(
+                               qstring_to_ucs4(font)).providesNoMath(ot1(),
+                                                                     completeFontset());
+}
+
+
+bool GuiDocument::hasMonolithicExpertSet(QString const & font) const
+{
+       if (fontModule->osFontsCB->isChecked())
+               return false;
+       return theLaTeXFonts().getLaTeXFont(
+                               qstring_to_ucs4(font)).hasMonolithicExpertSet(ot1(),
+                                                                             completeFontset(),
+                                                                             noMathFont());
+}
+
+
+//static
+GuiDocument::modInfoStruct GuiDocument::modInfo(LyXModule const & mod)
+{
+       // FIXME Unicode: docstrings would be better for these parameters but this
+       // change requires a lot of others
+       modInfoStruct m;
+       m.id = mod.getID();
+       m.name = toqstr(translateIfPossible(from_utf8(mod.getName())));
+       QString desc = toqstr(translateIfPossible(from_utf8(mod.getDescription())));
+       // Find the first sentence of the description
+       QTextBoundaryFinder bf(QTextBoundaryFinder::Sentence, desc);
+       int pos = bf.toNextBoundary();
+       if (pos > 0)
+               desc.truncate(pos);
+       QString modulename = QString(qt_("(Module name: %1)")).arg(toqstr(m.id));
+       // Tooltip is the desc followed by the module name
+       m.description = QString("%1<i>%2</i>")
+               .arg(desc.isEmpty() ? QString() : QString("<p>%1</p>").arg(desc),
+                    modulename);
+       return m;
 }
 
 
 void GuiDocument::loadModuleInfo()
 {
        moduleNames_.clear();
-       LyXModuleList::const_iterator it  = theModuleList.begin();
-       LyXModuleList::const_iterator end = theModuleList.end();
-       for (; it != end; ++it) {
-               modInfoStruct m;
-               m.id = it->getID();
-               // FIXME Unicode
-               m.name = toqstr(translateIfPossible(from_utf8(it->getName())));
-               // this is supposed to give us the first sentence of the description
-               // FIXME Unicode
-               QString desc =
-                       toqstr(translateIfPossible(from_utf8(it->getDescription())));
-               int const pos = desc.indexOf(".");
-               if (pos > 0)
-                       desc.truncate(pos + 1);
-               m.description = desc;
-               if (it->category().substr(0, 8) != "Citation")
-                       moduleNames_.push_back(m);
-       }
+       for (LyXModule const & mod : theModuleList)
+               if (mod.category().substr(0, 8) != "Citation")
+                       moduleNames_.push_back(modInfo(mod));
 }
 
 
@@ -3719,6 +4287,33 @@ void GuiDocument::executeBranchRenaming() const
 }
 
 
+void GuiDocument::allPackagesAuto()
+{
+       allPackages(1);
+}
+
+
+void GuiDocument::allPackagesAlways()
+{
+       allPackages(2);
+}
+
+
+void GuiDocument::allPackagesNot()
+{
+       allPackages(3);
+}
+
+
+void GuiDocument::allPackages(int col)
+{
+       for (int row = 0; row < mathsModule->packagesTW->rowCount(); ++row) {
+               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, col);
+               rb->setChecked(true);
+       }
+}
+
+
 Dialog * createGuiDocument(GuiView & lv) { return new GuiDocument(lv); }