]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
index f02cdebb17d1bf2bf3539950c309d163a246497e..d60385b041dd98d5bda834874fb4264347b5851a 100644 (file)
@@ -38,7 +38,6 @@
 #include "FloatPlacement.h"
 #include "Format.h"
 #include "FuncRequest.h"
-#include "HSpace.h"
 #include "IndicesList.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
@@ -51,6 +50,7 @@
 #include "OutputParams.h"
 #include "PDFOptions.h"
 #include "qt_helpers.h"
+#include "Session.h"
 #include "Spacing.h"
 #include "TextClass.h"
 #include "Undo.h"
 #include "insets/InsetListingsParams.h"
 
 #include "support/debug.h"
+#include "support/docstream.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
+#include "support/TempFile.h"
 
 #include "frontends/alert.h"
 
 #include <QAbstractItemModel>
-#include <QHeaderView>
+#include <QButtonGroup>
 #include <QColor>
 #include <QColorDialog>
 #include <QCloseEvent>
 #include <QFontDatabase>
+#include <QHeaderView>
 #include <QScrollBar>
 #include <QTextBoundaryFinder>
 #include <QTextCursor>
@@ -136,6 +139,12 @@ char const * backref_opts_gui[] =
 };
 
 
+char const * lst_packages[] =
+{
+       "Listings", "Minted", ""
+};
+
+
 vector<string> engine_types_;
 vector<pair<string, QString> > pagestyles;
 
@@ -178,7 +187,7 @@ public:
        }
 };
 
-}
+} // namespace
 
 namespace frontend {
 namespace {
@@ -463,6 +472,34 @@ PreambleModule::PreambleModule(QWidget * parent)
        preambleTE->setWordWrapMode(QTextOption::NoWrap);
        setFocusProxy(preambleTE);
        connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
+       connect(findLE, SIGNAL(textEdited(const QString &)), this, SLOT(checkFindButton()));
+       connect(findButtonPB, SIGNAL(clicked()), this, SLOT(findText()));
+       connect(editPB, SIGNAL(clicked()), this, SLOT(editExternal()));
+       connect(findLE, SIGNAL(returnPressed()), this, SLOT(findText()));
+       checkFindButton();
+       // https://stackoverflow.com/questions/13027091/how-to-override-tab-width-in-qt
+       const int tabStop = 4;
+       QFontMetrics metrics(preambleTE->currentFont());
+       preambleTE->setTabStopWidth(tabStop * metrics.width(' '));
+}
+
+
+void PreambleModule::checkFindButton()
+{
+       findButtonPB->setEnabled(!findLE->text().isEmpty());
+}
+
+
+void PreambleModule::findText()
+{
+       bool const found = preambleTE->find(findLE->text());
+       if (!found) {
+               // wrap
+               QTextCursor qtcur = preambleTE->textCursor();
+               qtcur.movePosition(QTextCursor::Start);
+               preambleTE->setTextCursor(qtcur);
+               preambleTE->find(findLE->text());
+       }
 }
 
 
@@ -488,7 +525,7 @@ void PreambleModule::update(BufferParams const & params, BufferId id)
                preamble_coords_[current_id_] = make_pair(0, 0);
        else {
                // Restore saved coords.
-               QTextCursor cur = preambleTE->textCursor();
+               cur = preambleTE->textCursor();
                cur.setPosition(it->second.first);
                preambleTE->setTextCursor(cur);
                preambleTE->verticalScrollBar()->setValue(it->second.second);
@@ -512,6 +549,36 @@ void PreambleModule::closeEvent(QCloseEvent * e)
 }
 
 
+void PreambleModule::editExternal() {
+       if (!current_id_)
+               return;
+
+       if (tempfile_) {
+               preambleTE->setReadOnly(false);
+               FileName const tempfilename = tempfile_->name();
+               docstring const s = tempfilename.fileContents("UTF-8");
+               preambleTE->document()->setPlainText(toqstr(s));
+               tempfile_.reset();
+               editPB->setText(qt_("Edit"));
+               changed();
+               return;
+       }
+
+       string const format =
+               current_id_->params().documentClass().outputFormat();
+       string const ext = theFormats().extension(format);
+       tempfile_.reset(new TempFile("preamble_editXXXXXX." + ext));
+       FileName const tempfilename = tempfile_->name();
+       string const name = tempfilename.toFilesystemEncoding();
+       ofdocstream os(name.c_str());
+       os << qstring_to_ucs4(preambleTE->document()->toPlainText());
+       os.close();
+       preambleTE->setReadOnly(true);
+       theFormats().edit(*current_id_, tempfilename, format);
+       editPB->setText(qt_("End Edit"));
+       changed();
+}
+
 /////////////////////////////////////////////////////////////////////
 //
 // LocalLayout
@@ -522,9 +589,12 @@ void PreambleModule::closeEvent(QCloseEvent * e)
 LocalLayout::LocalLayout(QWidget * parent)
        : UiWidget<Ui::LocalLayoutUi>(parent), current_id_(0), validated_(false)
 {
+       locallayoutTE->setFont(guiApp->typewriterSystemFont());
+       locallayoutTE->setWordWrapMode(QTextOption::NoWrap);
        connect(locallayoutTE, SIGNAL(textChanged()), this, SLOT(textChanged()));
        connect(validatePB, SIGNAL(clicked()), this, SLOT(validatePressed()));
        connect(convertPB, SIGNAL(clicked()), this, SLOT(convertPressed()));
+       connect(editPB, SIGNAL(clicked()), this, SLOT(editExternal()));
 }
 
 
@@ -649,6 +719,38 @@ void LocalLayout::validatePressed() {
 }
 
 
+void LocalLayout::editExternal() {
+       if (!current_id_)
+               return;
+
+       if (tempfile_) {
+               locallayoutTE->setReadOnly(false);
+               FileName const tempfilename = tempfile_->name();
+               docstring const s = tempfilename.fileContents("UTF-8");
+               locallayoutTE->document()->setPlainText(toqstr(s));
+               tempfile_.reset();
+               editPB->setText(qt_("Edit"));
+               changed();
+               return;
+       }
+
+       string const format =
+               current_id_->params().documentClass().outputFormat();
+       string const ext = theFormats().extension(format);
+       tempfile_.reset(new TempFile("preamble_editXXXXXX." + ext));
+       FileName const tempfilename = tempfile_->name();
+       string const name = tempfilename.toFilesystemEncoding();
+       ofdocstream os(name.c_str());
+       os << qstring_to_ucs4(locallayoutTE->document()->toPlainText());
+       os.close();
+       locallayoutTE->setReadOnly(true);
+       theFormats().edit(*current_id_, tempfilename, format);
+       editPB->setText(qt_("End Edit"));
+       validatePB->setEnabled(false);
+       hideConvert();
+       changed();
+}
+
 /////////////////////////////////////////////////////////////////////
 //
 // DocumentDialog
@@ -658,24 +760,23 @@ void LocalLayout::validatePressed() {
 
 GuiDocument::GuiDocument(GuiView & lv)
        : GuiDialog(lv, "document", qt_("Document Settings")),
-         biblioChanged_(false), nonModuleChanged_(false)
+         biblioChanged_(false), nonModuleChanged_(false),
+         modulesChanged_(false), shellescapeChanged_(false)
 {
        setupUi(this);
 
-       connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
-       connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
+       connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
+               this, SLOT(slotButtonBox(QAbstractButton *)));
 
        connect(savePB, SIGNAL(clicked()), this, SLOT(saveDefaultClicked()));
        connect(defaultPB, SIGNAL(clicked()), this, SLOT(useDefaultsClicked()));
 
        // Manage the restore, ok, apply, restore and cancel/close buttons
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
-       bc().setOK(okPB);
-       bc().setApply(applyPB);
-       bc().setCancel(closePB);
-       bc().setRestore(restorePB);
+       bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
+       bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
+       bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
+       bc().setRestore(buttonBox->button(QDialogButtonBox::Reset));
 
 
        // text layout
@@ -725,25 +826,6 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(textLayoutModule->justCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
 
-       connect(textLayoutModule->MathIndentCB, SIGNAL(toggled(bool)),
-               this, SLOT(change_adaptor()));
-       connect(textLayoutModule->MathIndentCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-       connect(textLayoutModule->MathIndentCO, SIGNAL(activated(int)),
-               this, SLOT(setMathIndent(int)));
-       connect(textLayoutModule->MathIndentLE, SIGNAL(textChanged(const QString &)),
-               this, SLOT(change_adaptor()));
-       connect(textLayoutModule->MathIndentLengthCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-
-       
-       textLayoutModule->MathIndentCO->addItem(qt_("Default"));
-       textLayoutModule->MathIndentCO->addItem(qt_("Custom"));
-       textLayoutModule->MathIndentLE->setValidator(new LengthValidator(
-               textLayoutModule->MathIndentLE));
-       // initialize the length validator
-       bc().addCheckedLineEdit(textLayoutModule->MathIndentLE);
-       
        textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
                textLayoutModule->lspacingLE));
        textLayoutModule->indentLE->setValidator(new LengthValidator(
@@ -806,6 +888,8 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(outputModule->mathoutCB, SIGNAL(currentIndexChanged(int)),
                this, SLOT(change_adaptor()));
 
+       connect(outputModule->shellescapeCB, SIGNAL(stateChanged(int)),
+               this, SLOT(shellescapeChanged()));
        connect(outputModule->outputsyncCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(outputModule->synccustomCB, SIGNAL(editTextChanged(QString)),
@@ -821,6 +905,62 @@ GuiDocument::GuiDocument(GuiView & lv)
        connect(outputModule->saveTransientPropertiesCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
 
+
+       // language & quote
+       // this must preceed font, since fonts depend on this
+       langModule = new UiWidget<Ui::LanguageUi>(this);
+       connect(langModule->languageCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->languageCO, SIGNAL(activated(int)),
+               this, SLOT(languageChanged(int)));
+       connect(langModule->defaultencodingRB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(langModule->otherencodingRB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
+       connect(langModule->encodingCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->languagePackageCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(langModule->languagePackageLE, SIGNAL(textChanged(QString)),
+               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));
+
+       QAbstractItemModel * language_model = guiApp->languageModel();
+       // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
+       language_model->sort(0);
+       langModule->languageCO->setModel(language_model);
+       langModule->languageCO->setModelColumn(0);
+
+       // Always put the default encoding in the first position.
+       langModule->encodingCO->addItem(qt_("Language Default (no inputenc)"));
+       QStringList encodinglist;
+       for (auto const & encvar : encodings) {
+               if (!encvar.unsafe() && !encvar.guiName().empty())
+                       encodinglist.append(qt_(encvar.guiName()));
+       }
+       encodinglist.sort();
+       langModule->encodingCO->addItems(encodinglist);
+
+       langModule->languagePackageCO->addItem(
+               qt_("Default"), toqstr("default"));
+       langModule->languagePackageCO->addItem(
+               qt_("Automatic"), toqstr("auto"));
+       langModule->languagePackageCO->addItem(
+               qt_("Always Babel"), toqstr("babel"));
+       langModule->languagePackageCO->addItem(
+               qt_("Custom"), toqstr("custom"));
+       langModule->languagePackageCO->addItem(
+               qt_("None[[language package]]"), toqstr("none"));
+
+
        // fonts
        fontModule = new FontModule(this);
        connect(fontModule->osFontsCB, SIGNAL(clicked()),
@@ -884,9 +1024,9 @@ GuiDocument::GuiDocument(GuiView & lv)
        fontModule->fontsizeCO->addItem(qt_("11"));
        fontModule->fontsizeCO->addItem(qt_("12"));
 
-       fontModule->fontencCO->addItem(qt_("Default"), QString("global"));
+       fontModule->fontencCO->addItem(qt_("Automatic"), QString("auto"));
+       fontModule->fontencCO->addItem(qt_("Class Default"), QString("default"));
        fontModule->fontencCO->addItem(qt_("Custom"), QString("custom"));
-       fontModule->fontencCO->addItem(qt_("None (no fontenc)"), QString("default"));
 
        for (int n = 0; GuiDocument::fontfamilies_gui[n][0]; ++n)
                fontModule->fontsDefaultCO->addItem(
@@ -1051,61 +1191,6 @@ GuiDocument::GuiDocument(GuiView & lv)
                marginsModule->columnsepL);
 
 
-       // language & quote
-       langModule = new UiWidget<Ui::LanguageUi>(this);
-       connect(langModule->languageCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-       connect(langModule->languageCO, SIGNAL(activated(int)),
-               this, SLOT(languageChanged(int)));
-       connect(langModule->defaultencodingRB, SIGNAL(clicked()),
-               this, SLOT(change_adaptor()));
-       connect(langModule->otherencodingRB, SIGNAL(clicked()),
-               this, SLOT(change_adaptor()));
-       connect(langModule->encodingCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-       connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-       connect(langModule->languagePackageCO, SIGNAL(activated(int)),
-               this, SLOT(change_adaptor()));
-       connect(langModule->languagePackageLE, SIGNAL(textChanged(QString)),
-               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));
-
-       QAbstractItemModel * language_model = guiApp->languageModel();
-       // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
-       language_model->sort(0);
-       langModule->languageCO->setModel(language_model);
-       langModule->languageCO->setModelColumn(0);
-
-       // Always put the default encoding in the first position.
-       langModule->encodingCO->addItem(qt_("Language Default (no inputenc)"));
-       QStringList encodinglist;
-       Encodings::const_iterator it = encodings.begin();
-       Encodings::const_iterator const end = encodings.end();
-       for (; it != end; ++it)
-               if (!it->unsafe())
-                       encodinglist.append(qt_(it->guiName()));
-       encodinglist.sort();
-       langModule->encodingCO->addItems(encodinglist);
-
-       langModule->languagePackageCO->addItem(
-               qt_("Default"), toqstr("default"));
-       langModule->languagePackageCO->addItem(
-               qt_("Automatic"), toqstr("auto"));
-       langModule->languagePackageCO->addItem(
-               qt_("Always Babel"), toqstr("babel"));
-       langModule->languagePackageCO->addItem(
-               qt_("Custom"), toqstr("custom"));
-       langModule->languagePackageCO->addItem(
-               qt_("None[[language package]]"), toqstr("none"));
-
-
        // color
        colorModule = new UiWidget<Ui::ColorUi>(this);
        connect(colorModule->fontColorPB, SIGNAL(clicked()),
@@ -1166,10 +1251,14 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(updateResetDefaultBiblio()));
        connect(biblioModule->biblatexBbxCO, SIGNAL(activated(int)),
                this, SLOT(biblioChanged()));
+       connect(biblioModule->biblatexBbxCO, SIGNAL(editTextChanged(QString)),
+               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(biblioChanged()));
        connect(biblioModule->biblatexCbxCO, SIGNAL(editTextChanged(QString)),
                this, SLOT(updateResetDefaultBiblio()));
        connect(biblioModule->rescanBibliosPB, SIGNAL(clicked()),
@@ -1195,13 +1284,14 @@ GuiDocument::GuiDocument(GuiView & lv)
                biblioModule->bibtexOptionsLE));
        biblioModule->defaultBiblioCO->lineEdit()->setValidator(new NoNewLineValidator(
                biblioModule->defaultBiblioCO->lineEdit()));
+       biblioModule->citePackageOptionsLE->setValidator(new NoNewLineValidator(
+               biblioModule->citePackageOptionsLE));
 
        // NOTE: we do not provide "custom" here for security reasons!
        biblioModule->bibtexCO->clear();
        biblioModule->bibtexCO->addItem(qt_("Default"), QString("default"));
-       for (set<string>::const_iterator it = lyxrc.bibtex_alternatives.begin();
-                            it != lyxrc.bibtex_alternatives.end(); ++it) {
-               QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
+       for (auto const & alts : lyxrc.bibtex_alternatives) {
+               QString const command = toqstr(alts).left(toqstr(alts).indexOf(" "));
                biblioModule->bibtexCO->addItem(command, command);
        }
 
@@ -1221,11 +1311,10 @@ GuiDocument::GuiDocument(GuiView & lv)
        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);
+       int packnum = 0;
+       for (auto const & pkgvar : packages) {
+               docstring const package = from_ascii(pkgvar.first);
+               QString autoTooltip = qt_(pkgvar.second);
                QString alwaysTooltip;
                if (package == "amsmath")
                        alwaysTooltip =
@@ -1252,11 +1341,34 @@ GuiDocument::GuiDocument(GuiView & lv)
                autoRB->setToolTip(autoTooltip);
                alwaysRB->setToolTip(alwaysTooltip);
                neverRB->setToolTip(neverTooltip);
+
+               // Pack the buttons in a layout in order to get proper alignment
+               QWidget * autoRBWidget = new QWidget();
+               QHBoxLayout * autoRBLayout = new QHBoxLayout(autoRBWidget);
+               autoRBLayout->addWidget(autoRB);
+               autoRBLayout->setAlignment(Qt::AlignCenter);
+               autoRBLayout->setContentsMargins(0, 0, 0, 0);
+               autoRBWidget->setLayout(autoRBLayout);
+
+               QWidget * alwaysRBWidget = new QWidget();
+               QHBoxLayout * alwaysRBLayout = new QHBoxLayout(alwaysRBWidget);
+               alwaysRBLayout->addWidget(alwaysRB);
+               alwaysRBLayout->setAlignment(Qt::AlignCenter);
+               alwaysRBLayout->setContentsMargins(0, 0, 0, 0);
+               alwaysRBWidget->setLayout(alwaysRBLayout);
+
+               QWidget * neverRBWidget = new QWidget();
+               QHBoxLayout * neverRBLayout = new QHBoxLayout(neverRBWidget);
+               neverRBLayout->addWidget(neverRB);
+               neverRBLayout->setAlignment(Qt::AlignCenter);
+               neverRBLayout->setContentsMargins(0, 0, 0, 0);
+               neverRBWidget->setLayout(neverRBLayout);
+
                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);
+               mathsModule->packagesTW->setItem(packnum, 0, pack);
+               mathsModule->packagesTW->setCellWidget(packnum, 1, autoRBWidget);
+               mathsModule->packagesTW->setCellWidget(packnum, 2, alwaysRBWidget);
+               mathsModule->packagesTW->setCellWidget(packnum, 3, neverRBWidget);
 
                connect(autoRB, SIGNAL(clicked()),
                        this, SLOT(change_adaptor()));
@@ -1264,7 +1376,7 @@ GuiDocument::GuiDocument(GuiView & lv)
                        this, SLOT(change_adaptor()));
                connect(neverRB, SIGNAL(clicked()),
                        this, SLOT(change_adaptor()));
-               ++i;
+               ++packnum;
        }
        connect(mathsModule->allPackagesAutoPB, SIGNAL(clicked()),
                this, SLOT(allPackagesAuto()));
@@ -1278,7 +1390,34 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(mathsModule->allPackagesNotPB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
-       
+       connect(mathsModule->MathNumberingPosCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+
+       connect(mathsModule->MathIndentCB, SIGNAL(toggled(bool)),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->MathIndentCB, SIGNAL(toggled(bool)),
+               this, SLOT(allowMathIndent()));
+       connect(mathsModule->MathIndentCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->MathIndentCO, SIGNAL(activated(int)),
+               this, SLOT(enableMathIndent(int)));
+       connect(mathsModule->MathIndentLE, SIGNAL(textChanged(const QString &)),
+               this, SLOT(change_adaptor()));
+       connect(mathsModule->MathIndentLengthCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+
+
+       mathsModule->MathIndentCO->addItem(qt_("Default"));
+       mathsModule->MathIndentCO->addItem(qt_("Custom"));
+       mathsModule->MathIndentLE->setValidator(new LengthValidator(
+               mathsModule->MathIndentLE));
+       // initialize the length validator
+       bc().addCheckedLineEdit(mathsModule->MathIndentLE);
+       mathsModule->MathNumberingPosCO->addItem(qt_("Left"));
+       mathsModule->MathNumberingPosCO->addItem(qt_("Default"));
+       mathsModule->MathNumberingPosCO->addItem(qt_("Right"));
+       mathsModule->MathNumberingPosCO->setCurrentIndex(1);
+
 
        // latex class
        latexModule = new UiWidget<Ui::LaTeXUi>(this);
@@ -1322,10 +1461,8 @@ GuiDocument::GuiDocument(GuiView & lv)
        vector<LayoutFileIndex> classList = bcl.classList();
        sort(classList.begin(), classList.end(), less_textclass_avail_desc());
 
-       vector<LayoutFileIndex>::const_iterator cit  = classList.begin();
-       vector<LayoutFileIndex>::const_iterator cen = classList.end();
-       for (int i = 0; cit != cen; ++cit, ++i) {
-               LayoutFile const & tc = bcl[*cit];
+       for (auto const & cvar : classList) {
+               LayoutFile const & tc = bcl[cvar];
                bool const available = tc.isTeXClassAvailable();
                docstring const guiname = translateIfPossible(from_utf8(tc.description()));
                // tooltip sensu "KOMA-Script Article [Class 'scrartcl']"
@@ -1454,11 +1591,18 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(listingsModule->bypassCB, SIGNAL(clicked()),
                this, SLOT(setListingsMessage()));
+       connect(listingsModule->packageCO, SIGNAL(activated(int)),
+               this, SLOT(change_adaptor()));
+       connect(listingsModule->packageCO, SIGNAL(activated(int)),
+               this, SLOT(listingsPackageChanged(int)));
        connect(listingsModule->listingsED, SIGNAL(textChanged()),
                this, SLOT(setListingsMessage()));
        listingsModule->listingsTB->setPlainText(
                qt_("Input listings parameters below. Enter ? for a list of parameters."));
 
+       for (int i = 0; lst_packages[i][0]; ++i)
+            listingsModule->packageCO->addItem(lst_packages[i]);
+
 
        // add the panels
        docPS->addPanel(latexModule, N_("Document Class"));
@@ -1476,7 +1620,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        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(floatModule, N_("Float Settings"));
        docPS->addPanel(listingsModule, N_("Listings[[inset]]"));
        docPS->addPanel(bulletsModule, N_("Bullets"));
        docPS->addPanel(branchesModule, N_("Branches"));
@@ -1517,6 +1661,57 @@ void GuiDocument::change_adaptor()
 }
 
 
+void GuiDocument::shellescapeChanged()
+{
+       shellescapeChanged_ = true;
+       changed();
+}
+
+
+void GuiDocument::slotApply()
+{
+       bool only_shellescape_changed = !nonModuleChanged_ && !modulesChanged_;
+       bool wasclean = buffer().isClean();
+       GuiDialog::slotApply();
+       if (wasclean && only_shellescape_changed)
+               buffer().markClean();
+       modulesChanged_ = false;
+}
+
+
+void GuiDocument::slotOK()
+{
+       bool only_shellescape_changed = !nonModuleChanged_ && !modulesChanged_;
+       bool wasclean = buffer().isClean();
+       GuiDialog::slotOK();
+       if (wasclean && only_shellescape_changed)
+               buffer().markClean();
+       modulesChanged_ = false;
+}
+
+
+void GuiDocument::slotButtonBox(QAbstractButton * button)
+{
+       switch (buttonBox->standardButton(button)) {
+       case QDialogButtonBox::Ok:
+               slotOK();
+               break;
+       case QDialogButtonBox::Apply:
+               slotApply();
+               break;
+       case QDialogButtonBox::Cancel:
+               slotClose();
+               break;
+       case QDialogButtonBox::Reset:
+       case QDialogButtonBox::RestoreDefaults:
+               slotRestore();
+               break;
+       default:
+               break;
+       }
+}
+
+
 void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
 {
        if (item == 0)
@@ -1541,8 +1736,12 @@ QString GuiDocument::validateListingsParameters()
 {
        if (listingsModule->bypassCB->isChecked())
                return QString();
+       string const package =
+           lst_packages[listingsModule->packageCO->currentIndex()];
        string params = fromqstr(listingsModule->listingsED->toPlainText());
-       return toqstr(InsetListingsParams(params).validate());
+       InsetListingsParams lstparams(params);
+       lstparams.setMinted(package == "Minted");
+       return toqstr(lstparams.validate());
 }
 
 
@@ -1567,6 +1766,22 @@ void GuiDocument::setListingsMessage()
 }
 
 
+void GuiDocument::listingsPackageChanged(int index)
+{
+        string const package = lst_packages[index];
+        if (package == "Minted" && lyxrc.pygmentize_command.empty()) {
+                Alert::warning(_("Pygments driver command not found!"),
+                    _("The driver command necessary to use the minted package\n"
+                      "(pygmentize) has not been found. Make sure you have\n"
+                      "the python-pygments module installed or, if the driver\n"
+                      "is named differently, to add the following line to the\n"
+                      "document preamble:\n\n"
+                      "\\AtBeginDocument{\\renewcommand{\\MintedPygmentize}{driver}}\n\n"
+                      "where 'driver' is name of the driver command."));
+        }
+}
+
+
 void GuiDocument::setLSpacing(int item)
 {
        textLayoutModule->lspacingLE->setEnabled(item == 3);
@@ -1610,11 +1825,25 @@ void GuiDocument::enableSkip(bool skip)
                setSkip(textLayoutModule->skipCO->currentIndex());
 }
 
-void GuiDocument::setMathIndent(int item)
+void GuiDocument::allowMathIndent() {
+       // only disable when not checked, checked does not always allow enabling
+       if (!mathsModule->MathIndentCB->isChecked()) {
+               mathsModule->MathIndentLE->setEnabled(false);
+               mathsModule->MathIndentLengthCO->setEnabled(false);
+       }
+       if (mathsModule->MathIndentCB->isChecked()
+           && mathsModule->MathIndentCO->currentIndex() == 1) {
+                       mathsModule->MathIndentLE->setEnabled(true);
+                       mathsModule->MathIndentLengthCO->setEnabled(true);
+       }
+       isValid();
+}
+
+void GuiDocument::enableMathIndent(int item)
 {
        bool const enable = (item == 1);
-       textLayoutModule->MathIndentLE->setEnabled(enable);
-       textLayoutModule->MathIndentLengthCO->setEnabled(enable);
+       mathsModule->MathIndentLE->setEnabled(enable);
+       mathsModule->MathIndentLengthCO->setEnabled(enable);
        isValid();
 }
 
@@ -1984,7 +2213,6 @@ void GuiDocument::updateFontOptions()
                                fontModule->fontsRomanCO->currentIndex()).toString();
        fontModule->fontScCB->setEnabled(providesSC(font));
        fontModule->fontOsfCB->setEnabled(providesOSF(font));
-       fontModule->dashesCB->setEnabled(tex_fonts);
        updateMathFonts(font);
 }
 
@@ -2011,8 +2239,13 @@ bool GuiDocument::ot1() const
 {
        QString const fontenc =
                fontModule->fontencCO->itemData(fontModule->fontencCO->currentIndex()).toString();
+       int const i = langModule->languageCO->currentIndex();
+       if (i == -1)
+               return false;
+       QString const langname = langModule->languageCO->itemData(i).toString();
+       Language const * newlang = lyx::languages.getLanguage(fromqstr(langname));
        return (fontenc == "default"
-               || (fontenc == "global" && (lyxrc.fontenc == "default" || lyxrc.fontenc == "OT1"))
+               || (fontenc == "auto" && newlang->fontenc(buffer().params()) == "OT1")
                || (fontenc == "custom" && fontModule->fontencLE->text() == "OT1"));
 }
 
@@ -2218,7 +2451,7 @@ void GuiDocument::updatePagestyle(string const & items, string const & sel)
 
 void GuiDocument::browseLayout()
 {
-       QString const label1 = qt_("Layouts|#o#O");
+       QString const label1 = qt_("Lay&outs");
        QString const dir1 = toqstr(lyxrc.document_path);
        QStringList const filter(qt_("LyX Layout (*.layout)"));
        QString file = browseRelToParent(QString(), bufferFilePath(),
@@ -2287,7 +2520,7 @@ void GuiDocument::browseMaster()
        QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
        QStringList const filter(qt_("LyX Files (*.lyx)"));
        QString file = browseRelToSub(old, docpath, title, filter, false,
-               qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
+               qt_("D&ocuments"), toqstr(lyxrc.document_path));
 
        if (!file.isEmpty())
                latexModule->childDocLE->setText(file);
@@ -2308,7 +2541,7 @@ void GuiDocument::classChanged()
                return;
        string const classname = fromqstr(latexModule->classCO->getData(idx));
 
-       if (applyPB->isEnabled()) {
+       if (buttonBox->button(QDialogButtonBox::Apply)->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."),
@@ -2354,6 +2587,28 @@ void GuiDocument::biblioChanged()
 }
 
 
+void GuiDocument::checkPossibleCiteEngines()
+{
+       // Check if the class provides a specific engine,
+       // and if so, enforce this.
+       string force_engine;
+       if (documentClass().provides("natbib")
+           || documentClass().provides("natbib-internal"))
+               force_engine = "natbib";
+       else if (documentClass().provides("jurabib"))
+               force_engine = "jurabib";
+       else if (documentClass().provides("biblatex"))
+               force_engine = "biblatex";
+       else if (documentClass().provides("biblatex-natbib"))
+               force_engine = "biblatex-natbib";
+
+       if (!force_engine.empty())
+               biblioModule->citeEngineCO->setCurrentIndex(
+                       biblioModule->citeEngineCO->findData(toqstr(force_engine)));
+       biblioModule->citeEngineCO->setEnabled(force_engine.empty());
+}
+
+
 void GuiDocument::rescanBibFiles()
 {
        if (isBiblatex())
@@ -2588,7 +2843,8 @@ void GuiDocument::modulesChanged()
 {
        modulesToParams(bp_);
 
-       if (applyPB->isEnabled() && nonModuleChanged_) {
+       if (buttonBox->button(QDialogButtonBox::Apply)->isEnabled()
+           && (nonModuleChanged_ || shellescapeChanged_)) {
                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."),
@@ -2597,6 +2853,7 @@ void GuiDocument::modulesChanged()
                        applyView();
        }
 
+       modulesChanged_ = true;
        bp_.makeDocumentClass();
        paramsToDialog();
        changed();
@@ -2886,28 +3143,56 @@ void GuiDocument::applyView()
                if (!item)
                        continue;
                int row = mathsModule->packagesTW->row(item);
-               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
+
+               QRadioButton * rb =
+                       (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1)->layout()->itemAt(0)->widget();
                if (rb->isChecked()) {
                        bp_.use_package(it->first, BufferParams::package_auto);
                        continue;
                }
-               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
+               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2)->layout()->itemAt(0)->widget();
                if (rb->isChecked()) {
                        bp_.use_package(it->first, BufferParams::package_on);
                        continue;
                }
-               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
+               rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3)->layout()->itemAt(0)->widget();
                if (rb->isChecked())
                        bp_.use_package(it->first, BufferParams::package_off);
        }
-       bp_.is_math_indent = textLayoutModule->MathIndentCB->isChecked();
        // if math is indented
+       bp_.is_math_indent = mathsModule->MathIndentCB->isChecked();
        if (bp_.is_math_indent) {
-               HSpace MathIndentation = HSpace(
-                               widgetsToLength(textLayoutModule->MathIndentLE,
-                               textLayoutModule->MathIndentLengthCO)
-                               );
-                       bp_.setMathIndentation(MathIndentation);
+               // if formulas are indented
+               switch (mathsModule->MathIndentCO->currentIndex()) {
+               case 0:
+                       bp_.setMathIndent(Length());
+                       break;
+               case 1: {
+                       Length mathindent(widgetsToLength(mathsModule->MathIndentLE,
+                                                         mathsModule->MathIndentLengthCO));
+                       bp_.setMathIndent(mathindent);
+                       break;
+               }
+               default:
+                       // this should never happen
+                       bp_.setMathIndent(Length());
+                       break;
+               }
+       }
+       switch (mathsModule->MathNumberingPosCO->currentIndex()) {
+               case 0:
+                       bp_.math_numbering_side = BufferParams::LEFT;
+                       break;
+               case 1:
+                       bp_.math_numbering_side = BufferParams::DEFAULT;
+                       break;
+               case 2:
+                       bp_.math_numbering_side = BufferParams::RIGHT;
+                       break;
+               default:
+                       // this should never happen
+                       bp_.math_numbering_side = BufferParams::DEFAULT;
+                       break;
        }
 
        // Page Layout
@@ -2953,19 +3238,17 @@ void GuiDocument::applyView()
                bp_.paragraph_separation = BufferParams::ParagraphIndentSeparation;
                switch (textLayoutModule->indentCO->currentIndex()) {
                case 0:
-                       bp_.setIndentation(HSpace(HSpace::DEFAULT));
+                       bp_.setParIndent(Length());
                        break;
-               case 1: {
-                       HSpace indent = HSpace(
-                               widgetsToLength(textLayoutModule->indentLE,
-                               textLayoutModule->indentLengthCO)
-                               );
-                       bp_.setIndentation(indent);
+               case 1: {
+                       Length parindent(widgetsToLength(textLayoutModule->indentLE,
+                                                        textLayoutModule->indentLengthCO));
+                       bp_.setParIndent(parindent);
                        break;
-                       }
+               }
                default:
                        // this should never happen
-                       bp_.setIndentation(HSpace(HSpace::DEFAULT));
+                       bp_.setParIndent(Length());
                        break;
                }
        } else {
@@ -2997,27 +3280,6 @@ void GuiDocument::applyView()
                }
        }
 
-       if (textLayoutModule->MathIndentCB->isChecked()) {
-               // if formulas are indented
-               switch (textLayoutModule->MathIndentCO->currentIndex()) {
-               case 0:
-                       bp_.setMathIndentation(HSpace(HSpace::DEFAULT));
-                       break;
-               case 1: {
-                       HSpace MathIndent = HSpace(
-                               widgetsToLength(textLayoutModule->MathIndentLE,
-                               textLayoutModule->MathIndentLengthCO)
-                               );
-                       bp_.setMathIndentation(MathIndent);
-                       break;
-                       }
-               default:
-                       // this should never happen
-                       bp_.setMathIndentation(HSpace(HSpace::DEFAULT));
-                       break;
-               }
-       }
-
        bp_.options =
                fromqstr(latexModule->optionsLE->text());
 
@@ -3041,11 +3303,14 @@ void GuiDocument::applyView()
        bp_.maintain_unincluded_children =
                masterChildModule->maintainAuxCB->isChecked();
 
-       // Float Placement
-       bp_.float_placement = floatModule->get();
+       // Float Settings
+       bp_.float_placement = floatModule->getPlacement();
+       bp_.float_alignment = floatModule->getAlignment();
 
        // Listings
        // text should have passed validation
+       idx = listingsModule->packageCO->currentIndex();
+       bp_.use_minted = string(lst_packages[idx]) == "Minted";
        bp_.listings_params =
                InsetListingsParams(fromqstr(listingsModule->listingsED->toPlainText())).params();
 
@@ -3056,6 +3321,14 @@ void GuiDocument::applyView()
        bool const nontexfonts = fontModule->osFontsCB->isChecked();
        bp_.useNonTeXFonts = nontexfonts;
 
+       bp_.shell_escape = outputModule->shellescapeCB->isChecked();
+       if (!bp_.shell_escape)
+           theSession().shellescapeFiles().remove(buffer().absFileName());
+       else if (!theSession().shellescapeFiles().find(buffer().absFileName()))
+           theSession().shellescapeFiles().insert(buffer().absFileName());
+       Buffer & buf = const_cast<Buffer &>(buffer());
+       buf.params().shell_escape = bp_.shell_escape;
+
        bp_.output_sync = outputModule->outputsyncCB->isChecked();
 
        bp_.output_sync_macro = fromqstr(outputModule->synccustomCB->currentText());
@@ -3106,7 +3379,7 @@ void GuiDocument::applyView()
                fromqstr(fontModule->cjkFontLE->text());
 
        bp_.use_microtype = fontModule->microtypeCB->isChecked();
-       bp_.use_dash_ligatures = fontModule->dashesCB->isChecked();
+       bp_.use_dash_ligatures = !fontModule->dashesCB->isChecked();
 
        bp_.fonts_sans_scale[nontexfonts] = fontModule->scaleSansSB->value();
        bp_.fonts_sans_scale[!nontexfonts] = fontModule->font_sf_scale;
@@ -3193,8 +3466,9 @@ void GuiDocument::applyView()
        pdf.quoted_options = pdf.quoted_options_check(
                                fromqstr(pdfSupportModule->optionsLE->text()));
 
-       // reset tracker
+       // reset trackers
        nonModuleChanged_ = false;
+       shellescapeChanged_ = false;
 }
 
 
@@ -3212,7 +3486,7 @@ void GuiDocument::paramsToDialog()
        latexModule->refstyleCB->setChecked(bp_.use_refstyle);
 
        // biblio
-       string const cite_engine = bp_.citeEngine().list().front();
+       string const cite_engine = bp_.citeEngine();
 
        biblioModule->citeEngineCO->setCurrentIndex(
                biblioModule->citeEngineCO->findData(toqstr(cite_engine)));
@@ -3220,6 +3494,8 @@ void GuiDocument::paramsToDialog()
        updateEngineType(documentClass().opt_enginetype(),
                bp_.citeEngineType());
 
+       checkPossibleCiteEngines();
+
        biblioModule->citeStyleCO->setCurrentIndex(
                biblioModule->citeStyleCO->findData(bp_.citeEngineType()));
 
@@ -3274,7 +3550,7 @@ void GuiDocument::paramsToDialog()
        biblioChanged_ = false;
 
        // indices
-       // We may be called when there is no Buffer, e.g., when 
+       // 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);
@@ -3382,18 +3658,28 @@ void GuiDocument::paramsToDialog()
        updateModuleInfo();
 
        // math
+       mathsModule->MathIndentCB->setChecked(bp_.is_math_indent);
        if (bp_.is_math_indent) {
-               textLayoutModule->MathIndentCB->setChecked(bp_.is_math_indent);
-               string MathIndentation = bp_.getMathIndentation().asLyXCommand();
-               int MathIndent = 0;
-               if (MathIndentation != "default") {
-                       lengthToWidgets(textLayoutModule->MathIndentLE,
-                       textLayoutModule->MathIndentLengthCO,
-                       MathIndentation, default_unit);
-                       MathIndent = 1;
+               Length const mathindent = bp_.getMathIndent();
+               int indent = 0;
+               if (!mathindent.empty()) {
+                       lengthToWidgets(mathsModule->MathIndentLE,
+                                       mathsModule->MathIndentLengthCO,
+                                       mathindent, default_unit);
+                       indent = 1;
                }
-               textLayoutModule->MathIndentCO->setCurrentIndex(MathIndent);
-               setMathIndent(MathIndent);
+               mathsModule->MathIndentCO->setCurrentIndex(indent);
+               enableMathIndent(indent);
+       }
+       switch(bp_.math_numbering_side) {
+       case BufferParams::LEFT:
+               mathsModule->MathNumberingPosCO->setCurrentIndex(0);
+               break;
+       case BufferParams::DEFAULT:
+               mathsModule->MathNumberingPosCO->setCurrentIndex(1);
+               break;
+       case BufferParams::RIGHT:
+               mathsModule->MathNumberingPosCO->setCurrentIndex(2);
        }
 
        map<string, string> const & packages = BufferParams::auto_packages();
@@ -3405,17 +3691,20 @@ void GuiDocument::paramsToDialog()
                int row = mathsModule->packagesTW->row(item);
                switch (bp_.use_package(it->first)) {
                        case BufferParams::package_off: {
-                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
+                               QRadioButton * rb =
+                                       (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3)->layout()->itemAt(0)->widget();
                                rb->setChecked(true);
                                break;
                        }
                        case BufferParams::package_on: {
-                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
+                               QRadioButton * rb =
+                                       (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2)->layout()->itemAt(0)->widget();
                                rb->setChecked(true);
                                break;
                        }
                        case BufferParams::package_auto: {
-                               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
+                               QRadioButton * rb =
+                                       (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1)->layout()->itemAt(0)->widget();
                                rb->setChecked(true);
                                break;
                        }
@@ -3445,12 +3734,12 @@ void GuiDocument::paramsToDialog()
 
        if (bp_.paragraph_separation == BufferParams::ParagraphIndentSeparation) {
                textLayoutModule->indentRB->setChecked(true);
-               string indentation = bp_.getIndentation().asLyXCommand();
+               string parindent = bp_.getParIndent().asString();
                int indent = 0;
-               if (indentation != "default") {
+               if (!parindent.empty()) {
                        lengthToWidgets(textLayoutModule->indentLE,
-                       textLayoutModule->indentLengthCO,
-                       indentation, default_unit);
+                                       textLayoutModule->indentLengthCO,
+                                       parindent, default_unit);
                        indent = 1;
                }
                textLayoutModule->indentCO->setCurrentIndex(indent);
@@ -3546,13 +3835,18 @@ void GuiDocument::paramsToDialog()
                bp_.maintain_unincluded_children);
 
        // Float Settings
-       floatModule->set(bp_.float_placement);
+       floatModule->setPlacement(bp_.float_placement);
+       floatModule->setAlignment(bp_.float_alignment);
 
        // ListingsSettings
        // break listings_params to multiple lines
        string lstparams =
                InsetListingsParams(bp_.listings_params).separatedParams();
        listingsModule->listingsED->setPlainText(toqstr(lstparams));
+       int nn = findToken(lst_packages, bp_.use_minted ? "Minted" : "Listings");
+       if (nn >= 0)
+               listingsModule->packageCO->setCurrentIndex(nn);
+
 
        // Fonts
        // some languages only work with polyglossia/XeTeX
@@ -3624,9 +3918,9 @@ void GuiDocument::paramsToDialog()
                        toqstr(bp_.fonts_cjk));
        else
                fontModule->cjkFontLE->setText(QString());
-       
+
        fontModule->microtypeCB->setChecked(bp_.use_microtype);
-       fontModule->dashesCB->setChecked(bp_.use_dash_ligatures);
+       fontModule->dashesCB->setChecked(!bp_.use_dash_ligatures);
 
        fontModule->fontScCB->setChecked(bp_.fonts_expert_sc);
        fontModule->fontOsfCB->setChecked(bp_.fonts_old_figures);
@@ -3635,16 +3929,17 @@ void GuiDocument::paramsToDialog()
        fontModule->scaleTypewriterSB->setValue(bp_.fontsTypewriterScale());
        fontModule->font_tt_scale = bp_.fonts_typewriter_scale[!bp_.useNonTeXFonts];
 
-       int nn = findToken(GuiDocument::fontfamilies, bp_.fonts_default_family);
+       nn = findToken(GuiDocument::fontfamilies, bp_.fonts_default_family);
        if (nn >= 0)
                fontModule->fontsDefaultCO->setCurrentIndex(nn);
 
-       if (bp_.fontenc == "global" || bp_.fontenc == "default") {
+       if (bp_.fontenc == "auto" || bp_.fontenc == "default") {
                fontModule->fontencCO->setCurrentIndex(
                        fontModule->fontencCO->findData(toqstr(bp_.fontenc)));
                fontModule->fontencLE->setEnabled(false);
        } else {
-               fontModule->fontencCO->setCurrentIndex(1);
+               fontModule->fontencCO->setCurrentIndex(
+                                       fontModule->fontencCO->findData("custom"));
                fontModule->fontencLE->setText(toqstr(bp_.fontenc));
        }
 
@@ -3660,6 +3955,7 @@ void GuiDocument::paramsToDialog()
                index = 0;
        outputModule->defaultFormatCO->setCurrentIndex(index);
 
+       outputModule->shellescapeCB->setChecked(bp_.shell_escape);
        outputModule->outputsyncCB->setChecked(bp_.output_sync);
        outputModule->synccustomCB->setEditText(toqstr(bp_.output_sync_macro));
 
@@ -3767,8 +4063,9 @@ void GuiDocument::paramsToDialog()
        // clear changed branches cache
        changedBranches_.clear();
 
-       // reset tracker
+       // reset trackers
        nonModuleChanged_ = false;
+       shellescapeChanged_ = false;
 }
 
 
@@ -3842,7 +4139,7 @@ void GuiDocument::updateIncludeonlys()
                        all_unincluded = false;
        }
        // Both if all childs are included and if none is included
-       // is equal to "include all" (i.e., ommit \includeonly).
+       // is equal to "include all" (i.e., omit \includeonly).
        // Thus, reset the GUI.
        if (!has_unincluded || all_unincluded) {
                masterChildModule->includeallRB->setChecked(true);
@@ -3861,6 +4158,12 @@ bool GuiDocument::isBiblatex() const
                biblioModule->citeEngineCO->itemData(
                                biblioModule->citeEngineCO->currentIndex()).toString();
 
+       // this can happen if the cite engine is unknown, which can happen
+       // if one is using a file that came from someone else, etc. in that
+       // case, we crash if we proceed.
+       if (engine.isEmpty())
+           return false;
+
        return theCiteEnginesList[fromqstr(engine)]->getCiteFramework() == "biblatex";
 }
 
@@ -4012,7 +4315,7 @@ void GuiDocument::updateContents()
 
 void GuiDocument::useClassDefaults()
 {
-       if (applyPB->isEnabled()) {
+       if (buttonBox->button(QDialogButtonBox::Apply)->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."),
@@ -4029,6 +4332,7 @@ void GuiDocument::useClassDefaults()
        }
        bp_.useClassDefaults();
        paramsToDialog();
+       changed();
 }
 
 
@@ -4045,6 +4349,8 @@ bool GuiDocument::isValid()
        return
                validateListingsParameters().isEmpty() &&
                localLayout->isValid() &&
+               !localLayout->editing() &&
+               !preambleModule->editing() &&
                (
                        // if we're asking for skips between paragraphs
                        !textLayoutModule->skipRB->isChecked() ||
@@ -4060,6 +4366,14 @@ bool GuiDocument::isValid()
                        textLayoutModule->indentCO->currentIndex() != 1 ||
                        // or else a length has been given
                        !textLayoutModule->indentLE->text().isEmpty()
+               ) &&
+               (
+                       // if we're asking for math indentation
+                       !mathsModule->MathIndentCB->isChecked() ||
+                       // then either we haven't chosen custom
+                       mathsModule->MathIndentCO->currentIndex() != 1 ||
+                       // or else a length has been given
+                       !mathsModule->MathIndentLE->text().isEmpty()
                );
 }
 
@@ -4166,7 +4480,8 @@ 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 handles undo groups automagically
+       UndoGroupHelper ugh(&buf);
 
        // This must come first so that a language change is correctly noticed
        setLanguage();
@@ -4209,10 +4524,6 @@ void GuiDocument::dispatchParams()
                        docstring const str = current_branch + ' ' + from_ascii(x11hexname);
                        dispatch(FuncRequest(LFUN_SET_COLOR, str));
                }
-
-               // Open insets of selected branches, close deselected ones
-               dispatch(FuncRequest(LFUN_INSET_FORALL,
-                       "Branch inset-toggle assign"));
        }
        // rename branches in the document
        executeBranchRenaming();
@@ -4237,10 +4548,6 @@ 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();
 }
 
 
@@ -4411,7 +4718,8 @@ void GuiDocument::allPackagesNot()
 void GuiDocument::allPackages(int col)
 {
        for (int row = 0; row < mathsModule->packagesTW->rowCount(); ++row) {
-               QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, col);
+               QRadioButton * rb =
+                       (QRadioButton*)mathsModule->packagesTW->cellWidget(row, col)->layout()->itemAt(0)->widget();
                rb->setChecked(true);
        }
 }