]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiDocument.cpp
Use QMessageBox for toggleWarning if possible
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
index 6f87dff11f5582b646359b9652f89c3d1d55ac0a..1748ddcaaa85327eb52f2730e94637c21dcc8e5b 100644 (file)
 #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"
 #include "FuncRequest.h"
-#include "HSpace.h"
 #include "IndicesList.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
@@ -49,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 "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>
@@ -134,6 +137,12 @@ char const * backref_opts_gui[] =
 };
 
 
+char const * lst_packages[] =
+{
+       "Listings", "Minted", ""
+};
+
+
 vector<string> engine_types_;
 vector<pair<string, QString> > pagestyles;
 
@@ -176,7 +185,7 @@ public:
        }
 };
 
-}
+} // namespace
 
 namespace frontend {
 namespace {
@@ -249,18 +258,19 @@ class ModuleSelectionManager : public GuiSelectionManager
 {
 public:
        ///
-       ModuleSelectionManager(
-               QTreeView * availableLV,
-               QListView * selectedLV,
-               QPushButton * addPB,
-               QPushButton * delPB,
-               QPushButton * upPB,
-               QPushButton * downPB,
-               GuiIdListModel * availableModel,
-               GuiIdListModel * selectedModel,
-               GuiDocument const * container)
-       : GuiSelectionManager(availableLV, selectedLV, addPB, delPB,
-                               upPB, downPB, availableModel, selectedModel), container_(container)
+       ModuleSelectionManager(QObject * parent,
+                              QTreeView * availableLV,
+                              QListView * selectedLV,
+                              QPushButton * addPB,
+                              QPushButton * delPB,
+                              QPushButton * upPB,
+                              QPushButton * downPB,
+                              GuiIdListModel * availableModel,
+                              GuiIdListModel * selectedModel,
+                              GuiDocument const * container)
+               : GuiSelectionManager(parent, availableLV, selectedLV, addPB, delPB,
+                                     upPB, downPB, availableModel, selectedModel),
+                 container_(container)
                {}
        ///
        void updateProvidedModules(LayoutModuleList const & pm)
@@ -449,15 +459,44 @@ void ModuleSelectionManager::updateDelPB()
 //
 /////////////////////////////////////////////////////////////////////
 
-PreambleModule::PreambleModule() : current_id_(0)
+PreambleModule::PreambleModule(QWidget * parent)
+       : UiWidget<Ui::PreambleUi>(parent), 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()));
+       connect(findLE, SIGNAL(textEdited(const QString &)), this, SLOT(checkFindButton()));
+       connect(findButtonPB, SIGNAL(clicked()), this, SLOT(findText()));
+       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());
+       }
 }
 
 
@@ -493,7 +532,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());
 }
 
 
@@ -514,8 +553,11 @@ void PreambleModule::closeEvent(QCloseEvent * e)
 /////////////////////////////////////////////////////////////////////
 
 
-LocalLayout::LocalLayout() : current_id_(0), validated_(false)
+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()));
@@ -539,11 +581,21 @@ void LocalLayout::update(BufferParams const & params, BufferId id)
 
 void LocalLayout::apply(BufferParams & params)
 {
-       string const layout = fromqstr(locallayoutTE->document()->toPlainText());
+       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();
+}
+
+
 void LocalLayout::textChanged()
 {
        static const QString message =
@@ -555,15 +607,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();
        }
 }
@@ -573,44 +624,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();
@@ -635,7 +694,8 @@ 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);
 
@@ -656,7 +716,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // text layout
-       textLayoutModule = new UiWidget<Ui::TextLayoutUi>;
+       textLayoutModule = new UiWidget<Ui::TextLayoutUi>(this);
        connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
@@ -729,7 +789,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // master/child handling
-       masterChildModule = new UiWidget<Ui::MasterChildUi>;
+       masterChildModule = new UiWidget<Ui::MasterChildUi>(this);
 
        connect(masterChildModule->childrenTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
                this, SLOT(includeonlyClicked(QTreeWidgetItem *, int)));
@@ -750,8 +810,8 @@ GuiDocument::GuiDocument(GuiView & lv)
        masterChildModule->childrenTW->resizeColumnToContents(2);
 
 
-       // Format
-       outputModule = new UiWidget<Ui::OutputUi>;
+       // Formats
+       outputModule = new UiWidget<Ui::OutputUi>(this);
 
        connect(outputModule->defaultFormatCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
@@ -764,6 +824,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)),
@@ -780,7 +842,7 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
 
        // fonts
-       fontModule = new FontModule;
+       fontModule = new FontModule(this);
        connect(fontModule->osFontsCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
        connect(fontModule->osFontsCB, SIGNAL(toggled(bool)),
@@ -815,6 +877,8 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(fontModule->microtypeCB, SIGNAL(clicked()),
                this, SLOT(change_adaptor()));
+       connect(fontModule->dashesCB, SIGNAL(clicked()),
+               this, SLOT(change_adaptor()));
        connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)),
                this, SLOT(change_adaptor()));
        connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)),
@@ -855,7 +919,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // page layout
-       pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
+       pageLayoutModule = new UiWidget<Ui::PageLayoutUi>(this);
        connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
                this, SLOT(papersizeChanged(int)));
        connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
@@ -935,7 +999,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // margins
-       marginsModule = new UiWidget<Ui::MarginsUi>;
+       marginsModule = new UiWidget<Ui::MarginsUi>(this);
        connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
                this, SLOT(setCustomMargins(bool)));
        connect(marginsModule->marginCB, SIGNAL(clicked()),
@@ -1008,7 +1072,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // language & quote
-       langModule = new UiWidget<Ui::LanguageUi>;
+       langModule = new UiWidget<Ui::LanguageUi>(this);
        connect(langModule->languageCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(langModule->languageCO, SIGNAL(activated(int)),
@@ -1027,6 +1091,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));
@@ -1040,27 +1106,13 @@ GuiDocument::GuiDocument(GuiView & lv)
        // 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()));
+       for (auto const & encvar : encodings) {
+               if (!encvar.unsafe() && !encvar.guiName().empty())
+                       encodinglist.append(qt_(encvar.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(
@@ -1074,7 +1126,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // color
-       colorModule = new UiWidget<Ui::ColorUi>;
+       colorModule = new UiWidget<Ui::ColorUi>(this);
        connect(colorModule->fontColorPB, SIGNAL(clicked()),
                this, SLOT(changeFontColor()));
        connect(colorModule->delFontColorTB, SIGNAL(clicked()),
@@ -1094,7 +1146,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // numbering
-       numberingModule = new UiWidget<Ui::NumberingUi>;
+       numberingModule = new UiWidget<Ui::NumberingUi>(this);
        connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
                this, SLOT(change_adaptor()));
        connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
@@ -1110,40 +1162,64 @@ GuiDocument::GuiDocument(GuiView & lv)
        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()));
+       biblioModule = new UiWidget<Ui::BiblioUi>(this);
+       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->bibunitsCO, SIGNAL(activated(int)),
+               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(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()),
+               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()));
+       biblioModule->citePackageOptionsLE->setValidator(new NoNewLineValidator(
+               biblioModule->citePackageOptionsLE));
 
        // NOTE: we do not provide "custom" here for security reasons!
        biblioModule->bibtexCO->clear();
@@ -1162,7 +1238,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // maths
-       mathsModule = new UiWidget<Ui::MathsUi>;
+       mathsModule = new UiWidget<Ui::MathsUi>(this);
        QStringList headers;
        headers << qt_("Package") << qt_("Load automatically")
                << qt_("Load always") << qt_("Do not load");
@@ -1170,7 +1246,7 @@ 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;
+       int packnum = 0;
        for (map<string, string>::const_iterator it = packages.begin();
             it != packages.end(); ++it) {
                docstring const package = from_ascii(it->first);
@@ -1201,11 +1277,35 @@ 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()));
@@ -1213,7 +1313,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()));
@@ -1227,10 +1327,37 @@ 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>;
+       latexModule = new UiWidget<Ui::LaTeXUi>(this);
        connect(latexModule->optionsLE, SIGNAL(textChanged(QString)),
                this, SLOT(change_adaptor()));
        connect(latexModule->defaultOptionsCB, SIGNAL(clicked()),
@@ -1295,7 +1422,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // branches
-       branchesModule = new GuiBranches;
+       branchesModule = new GuiBranches(this);
        connect(branchesModule, SIGNAL(changed()),
                this, SLOT(change_adaptor()));
        connect(branchesModule, SIGNAL(renameBranches(docstring const &, docstring const &)),
@@ -1305,32 +1432,34 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // preamble
-       preambleModule = new PreambleModule;
+       preambleModule = new PreambleModule(this);
        connect(preambleModule, SIGNAL(changed()),
                this, SLOT(change_adaptor()));
 
-       localLayout = new LocalLayout;
+       localLayout = new LocalLayout(this);
        connect(localLayout, SIGNAL(changed()),
                this, SLOT(change_adaptor()));
 
 
        // bullets
-       bulletsModule = new BulletsModule;
+       bulletsModule = new BulletsModule(this);
        connect(bulletsModule, SIGNAL(changed()),
                this, SLOT(change_adaptor()));
 
 
        // Modules
-       modulesModule = new UiWidget<Ui::ModulesUi>;
+       modulesModule = new UiWidget<Ui::ModulesUi>(this);
        modulesModule->availableLV->header()->setVisible(false);
        setSectionResizeMode(modulesModule->availableLV->header(), QHeaderView::ResizeToContents);
        modulesModule->availableLV->header()->setStretchLastSection(false);
        selectionManager =
-               new ModuleSelectionManager(modulesModule->availableLV,
-                       modulesModule->selectedLV,
-                       modulesModule->addPB, modulesModule->deletePB,
-                       modulesModule->upPB, modulesModule->downPB,
-                       availableModel(), selectedModel(), this);
+               new ModuleSelectionManager(this, modulesModule->availableLV,
+                                          modulesModule->selectedLV,
+                                          modulesModule->addPB,
+                                          modulesModule->deletePB,
+                                          modulesModule->upPB,
+                                          modulesModule->downPB,
+                                          availableModel(), selectedModel(), this);
        connect(selectionManager, SIGNAL(updateHook()),
                this, SLOT(updateModuleInfo()));
        connect(selectionManager, SIGNAL(selectionChanged()),
@@ -1338,7 +1467,7 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // PDF support
-       pdfSupportModule = new UiWidget<Ui::PDFSupportUi>;
+       pdfSupportModule = new UiWidget<Ui::PDFSupportUi>(this);
        connect(pdfSupportModule->use_hyperrefGB, SIGNAL(toggled(bool)),
                this, SLOT(change_adaptor()));
        connect(pdfSupportModule->titleLE, SIGNAL(textChanged(QString)),
@@ -1394,18 +1523,25 @@ GuiDocument::GuiDocument(GuiView & lv)
 
 
        // listings
-       listingsModule = new UiWidget<Ui::ListingsSettingsUi>;
+       listingsModule = new UiWidget<Ui::ListingsSettingsUi>(this);
        connect(listingsModule->listingsED, SIGNAL(textChanged()),
                this, SLOT(change_adaptor()));
        connect(listingsModule->bypassCB, SIGNAL(clicked()),
                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"));
@@ -1427,7 +1563,7 @@ GuiDocument::GuiDocument(GuiView & lv)
        docPS->addPanel(listingsModule, N_("Listings[[inset]]"));
        docPS->addPanel(bulletsModule, N_("Bullets"));
        docPS->addPanel(branchesModule, N_("Branches"));
-       docPS->addPanel(outputModule, N_("Format"));
+       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
@@ -1438,6 +1574,13 @@ GuiDocument::GuiDocument(GuiView & lv)
 }
 
 
+void GuiDocument::onBufferViewChanged()
+{
+       if (isVisibleView())
+               initialiseParams("");
+}
+
+
 void GuiDocument::saveDefaultClicked()
 {
        saveDocDefault();
@@ -1457,6 +1600,35 @@ 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::includeonlyClicked(QTreeWidgetItem * item, int)
 {
        if (item == 0)
@@ -1479,21 +1651,14 @@ void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
 
 QString GuiDocument::validateListingsParameters()
 {
-       // use a cache here to avoid repeated validation
-       // of the same parameters
-       // FIXME THREAD
-       static string param_cache;
-       static QString msg_cache;
-
        if (listingsModule->bypassCB->isChecked())
                return QString();
-
+       string const package =
+           lst_packages[listingsModule->packageCO->currentIndex()];
        string params = fromqstr(listingsModule->listingsED->toPlainText());
-       if (params != param_cache) {
-               param_cache = params;
-               msg_cache = toqstr(InsetListingsParams(params).validate());
-       }
-       return msg_cache;
+       InsetListingsParams lstparams(params);
+       lstparams.setMinted(package == "Minted");
+       return toqstr(lstparams.validate());
 }
 
 
@@ -1518,6 +1683,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);
@@ -1561,6 +1742,28 @@ void GuiDocument::enableSkip(bool skip)
                setSkip(textLayoutModule->skipCO->currentIndex());
 }
 
+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);
+       mathsModule->MathIndentLE->setEnabled(enable);
+       mathsModule->MathIndentLengthCO->setEnabled(enable);
+       isValid();
+}
+
 
 void GuiDocument::setMargins()
 {
@@ -1749,6 +1952,38 @@ void GuiDocument::deleteBoxBackgroundColor()
 }
 
 
+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
@@ -1773,10 +2008,7 @@ void GuiDocument::languageChanged(int i)
        }
 
        // set appropriate quotation mark style
-       if (!lang->quoteStyle().empty()) {
-               langModule->quoteStyleCO->setCurrentIndex(
-                       bp_.getQuoteStyle(lang->quoteStyle()));
-       }
+       updateQuoteStyles(true);
 }
 
 
@@ -2267,30 +2499,160 @@ void GuiDocument::biblioChanged()
 }
 
 
-void GuiDocument::bibtexChanged(int n)
+void GuiDocument::checkPossibleCiteEngines()
 {
-       biblioModule->bibtexOptionsLE->setEnabled(
-               biblioModule->bibtexCO->itemData(n).toString() != "default");
+       // 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())
+               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();
@@ -2303,28 +2665,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:
-               case ENGINE_TYPE_DEFAULT:
-                       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);
 }
 
 
@@ -2414,7 +2755,7 @@ void GuiDocument::modulesChanged()
 {
        modulesToParams(bp_);
 
-       if (applyPB->isEnabled() && nonModuleChanged_) {
+       if (applyPB->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."),
@@ -2423,6 +2764,7 @@ void GuiDocument::modulesChanged()
                        applyView();
        }
 
+       modulesChanged_ = true;
        bp_.makeDocumentClass();
        paramsToDialog();
        changed();
@@ -2547,14 +2889,12 @@ void GuiDocument::updateDefaultFormat()
        outputModule->defaultFormatCO->clear();
        outputModule->defaultFormatCO->addItem(qt_("Default"),
                                QVariant(QString("default")));
-       typedef vector<Format const *> Formats;
-       Formats formats = param_copy.exportableFormats(true);
-       sort(formats.begin(), formats.end(), Format::formatSorter);
-       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);
 }
 
@@ -2579,24 +2919,28 @@ 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");
-       if (biblioModule->citeDefaultRB->isChecked()) {
-               bp_.setCiteEngine("basic");
-               bp_.setCiteEngineType(ENGINE_TYPE_DEFAULT);
-       }
-       else
-       if (biblioModule->citeStyleCO->currentIndex())
-               bp_.setCiteEngineType(ENGINE_TYPE_NUMERICAL);
+       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_.setCiteEngineType(ENGINE_TYPE_AUTHORYEAR);
+               bp_.setCiteEngineType(ENGINE_TYPE_DEFAULT);
+
+       bp_.splitbib(biblioModule->bibtopicCB->isChecked());
 
-       bp_.use_bibtopic =
-               biblioModule->bibtopicCB->isChecked();
+       bp_.multibib = fromqstr(biblioModule->bibunitsCO->itemData(
+                               biblioModule->bibunitsCO->currentIndex()).toString());
 
-       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(
@@ -2645,8 +2989,9 @@ 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 langname = langModule->languageCO->itemData(
                langModule->languageCO->currentIndex()).toString();
@@ -2709,20 +3054,57 @@ 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);
        }
+       // if math is indented
+       bp_.is_math_indent = mathsModule->MathIndentCB->isChecked();
+       if (bp_.is_math_indent) {
+               // 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
        if (pageLayoutModule->pagestyleCO->currentIndex() == 0)
@@ -2767,19 +3149,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 {
@@ -2839,16 +3219,26 @@ void GuiDocument::applyView()
 
        // 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();
 
-       // Format
+       // Formats
        bp_.default_output_format = fromqstr(outputModule->defaultFormatCO->itemData(
                outputModule->defaultFormatCO->currentIndex()).toString());
 
        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());
@@ -2899,6 +3289,7 @@ void GuiDocument::applyView()
                fromqstr(fontModule->cjkFontLE->text());
 
        bp_.use_microtype = fontModule->microtypeCB->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;
@@ -2985,8 +3376,9 @@ void GuiDocument::applyView()
        pdf.quoted_options = pdf.quoted_options_check(
                                fromqstr(pdfSupportModule->optionsLE->text()));
 
-       // reset tracker
+       // reset trackers
        nonModuleChanged_ = false;
+       shellescapeChanged_ = false;
 }
 
 
@@ -3006,25 +3398,46 @@ void GuiDocument::paramsToDialog()
        // biblio
        string const cite_engine = bp_.citeEngine().list().front();
 
-       biblioModule->citeDefaultRB->setChecked(
-               cite_engine == "basic");
+       biblioModule->citeEngineCO->setCurrentIndex(
+               biblioModule->citeEngineCO->findData(toqstr(cite_engine)));
 
-       biblioModule->citeJurabibRB->setChecked(
-               cite_engine == "jurabib");
+       updateEngineType(documentClass().opt_enginetype(),
+               bp_.citeEngineType());
 
-       biblioModule->citeNatbibRB->setChecked(
-               cite_engine == "natbib");
+       checkPossibleCiteEngines();
 
        biblioModule->citeStyleCO->setCurrentIndex(
-               bp_.citeEngineType() & ENGINE_TYPE_NUMERICAL);
+               biblioModule->citeStyleCO->findData(bp_.citeEngineType()));
+
+       biblioModule->bibtopicCB->setChecked(bp_.splitbib());
+
+       biblioModule->bibunitsCO->clear();
+       biblioModule->bibunitsCO->addItem(qt_("No"), QString());
+       if (documentClass().hasLaTeXLayout("part"))
+               biblioModule->bibunitsCO->addItem(qt_("per part"), toqstr("part"));
+       if (documentClass().hasLaTeXLayout("chapter"))
+               biblioModule->bibunitsCO->addItem(qt_("per chapter"), toqstr("chapter"));
+       if (documentClass().hasLaTeXLayout("section"))
+               biblioModule->bibunitsCO->addItem(qt_("per section"), toqstr("section"));
+       if (documentClass().hasLaTeXLayout("subsection"))
+               biblioModule->bibunitsCO->addItem(qt_("per subsection"), toqstr("subsection"));
+       biblioModule->bibunitsCO->addItem(qt_("per child document"), toqstr("child"));
+
+       int const mbpos = biblioModule->bibunitsCO->findData(toqstr(bp_.multibib));
+       if (mbpos != -1)
+               biblioModule->bibunitsCO->setCurrentIndex(mbpos);
+       else
+               biblioModule->bibunitsCO->setCurrentIndex(0);
 
-       updateEngineType(documentClass().opt_enginetype(),
-               bp_.citeEngineType());
+       updateEngineDependends();
 
-       biblioModule->bibtopicCB->setChecked(
-               bp_.use_bibtopic);
+       if (isBiblatex()) {
+               updateDefaultBiblio(bp_.biblatex_bibstyle, "bbx");
+               updateDefaultBiblio(bp_.biblatex_citestyle, "cbx");
+       } else
+               updateDefaultBiblio(bp_.defaultBiblioStyle());
 
-       biblioModule->bibtexStyleLE->setText(toqstr(bp_.biblio_style));
+       biblioModule->citePackageOptionsLE->setText(toqstr(bp_.biblio_opts));
 
        string command;
        string options =
@@ -3047,7 +3460,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);
@@ -3057,8 +3470,11 @@ void GuiDocument::paramsToDialog()
                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") {
@@ -3151,6 +3567,31 @@ void GuiDocument::paramsToDialog()
                latexModule->psdriverCO->setCurrentIndex(nitem);
        updateModuleInfo();
 
+       // math
+       mathsModule->MathIndentCB->setChecked(bp_.is_math_indent);
+       if (bp_.is_math_indent) {
+               Length const mathindent = bp_.getMathIndent();
+               int indent = 0;
+               if (!mathindent.empty()) {
+                       lengthToWidgets(mathsModule->MathIndentLE,
+                                       mathsModule->MathIndentLengthCO,
+                                       mathindent, default_unit);
+                       indent = 1;
+               }
+               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();
        for (map<string, string>::const_iterator it = packages.begin();
             it != packages.end(); ++it) {
@@ -3160,17 +3601,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;
                        }
@@ -3200,12 +3644,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);
@@ -3308,6 +3752,10 @@ void GuiDocument::paramsToDialog()
        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
@@ -3379,8 +3827,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->fontScCB->setChecked(bp_.fonts_expert_sc);
        fontModule->fontOsfCB->setChecked(bp_.fonts_old_figures);
@@ -3389,7 +3838,7 @@ 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);
 
@@ -3402,7 +3851,7 @@ void GuiDocument::paramsToDialog()
                fontModule->fontencLE->setText(toqstr(bp_.fontenc));
        }
 
-       // Format
+       // Formats
        // This must be set _after_ fonts since updateDefaultFormat()
        // checks osFontsCB settings.
        // update combobox with formats
@@ -3414,6 +3863,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));
 
@@ -3521,8 +3971,9 @@ void GuiDocument::paramsToDialog()
        // clear changed branches cache
        changedBranches_.clear();
 
-       // reset tracker
+       // reset trackers
        nonModuleChanged_ = false;
+       shellescapeChanged_ = false;
 }
 
 
@@ -3596,7 +4047,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);
@@ -3609,6 +4060,160 @@ void GuiDocument::updateIncludeonlys()
 }
 
 
+bool GuiDocument::isBiblatex() const
+{
+       QString const engine =
+               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";
+}
+
+
+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.
@@ -3635,6 +4240,7 @@ void GuiDocument::useClassDefaults()
        }
        bp_.useClassDefaults();
        paramsToDialog();
+       changed();
 }
 
 
@@ -3666,6 +4272,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()
                );
 }
 
@@ -3772,7 +4386,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();
@@ -3815,10 +4430,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();
@@ -3843,10 +4454,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();
 }
 
 
@@ -4017,7 +4624,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);
        }
 }