diff --git a/src/NotepadNext/ApplicationSettings.cpp b/src/NotepadNext/ApplicationSettings.cpp index 6f18558eb..8914b3892 100644 --- a/src/NotepadNext/ApplicationSettings.cpp +++ b/src/NotepadNext/ApplicationSettings.cpp @@ -65,3 +65,4 @@ CREATE_SETTING(Editor, WordWrap, wordWrap, bool, false) CREATE_SETTING(Editor, FontName, fontName, QString, QStringLiteral("Courier New")) CREATE_SETTING(Editor, FontSize, fontSize, int, []() { return qApp->font().pointSize() + 2; }) CREATE_SETTING(Editor, AdditionalWordChars, additionalWordChars, QString, QStringLiteral("")); +CREATE_SETTING(Editor, DefaultEOLMode, defaultEOLMode, QString, QStringLiteral("")) diff --git a/src/NotepadNext/ApplicationSettings.h b/src/NotepadNext/ApplicationSettings.h index 3150b14d9..50d2533b5 100644 --- a/src/NotepadNext/ApplicationSettings.h +++ b/src/NotepadNext/ApplicationSettings.h @@ -107,4 +107,5 @@ class ApplicationSettings : public QSettings DEFINE_SETTING(FontName, fontName, QString); DEFINE_SETTING(FontSize, fontSize, int); DEFINE_SETTING(AdditionalWordChars, additionalWordChars, QString); + DEFINE_SETTING(DefaultEOLMode, defaultEOLMode, QString) }; diff --git a/src/NotepadNext/EditorManager.cpp b/src/NotepadNext/EditorManager.cpp index 1e155af13..bfae40348 100644 --- a/src/NotepadNext/EditorManager.cpp +++ b/src/NotepadNext/EditorManager.cpp @@ -266,8 +266,21 @@ void EditorManager::setupEditor(ScintillaNext *editor) editor->setIndentationGuides(settings->showIndentGuide() ? SC_IV_LOOKBOTH : SC_IV_NONE); editor->setWrapMode(settings->wordWrap() ? SC_WRAP_WORD : SC_WRAP_NONE); - // TODO: make this optional - editor->setEOLMode(detectEOLMode(editor)); + int detectedEOLMode = detectEOLMode(editor); + if (detectedEOLMode == -1) { + if (!settings->defaultEOLMode().isEmpty()) { + const int eol = ScintillaNext::stringToEolMode(settings->defaultEOLMode().toLower()); + + if (eol != -1) + editor->setEOLMode(eol); + else + qWarning("Unexpected DefaultEOLMode: %s", qUtf8Printable(settings->defaultEOLMode())); + } + // else it will just stay whatever EOL mode it was when it was created + } + else { + editor->setEOLMode(detectedEOLMode); + } // Decorators SmartHighlighter *s = new SmartHighlighter(editor); @@ -366,6 +379,6 @@ int EditorManager::detectEOLMode(ScintillaNext *editor) const return SC_EOL_LF; } else { - return editor->eOLMode(); + return -1; } } diff --git a/src/NotepadNext/ScintillaNext.cpp b/src/NotepadNext/ScintillaNext.cpp index 48058d35a..68b92567f 100644 --- a/src/NotepadNext/ScintillaNext.cpp +++ b/src/NotepadNext/ScintillaNext.cpp @@ -97,6 +97,30 @@ ScintillaNext *ScintillaNext::fromFile(const QString &filePath, bool tryToCreate return editor; } +QString ScintillaNext::eolModeToString(int eolMode) +{ + if (eolMode == SC_EOL_CRLF) + return QStringLiteral("crlf"); + else if (eolMode == SC_EOL_CR) + return QStringLiteral("cr"); + else if (eolMode == SC_EOL_LF) + return QStringLiteral("lf"); + else + return QString(); // unknown +} + +int ScintillaNext::stringToEolMode(QString eolMode) +{ + if (eolMode == QStringLiteral("crlf")) + return SC_EOL_CRLF; + else if (eolMode == QStringLiteral("cr")) + return SC_EOL_CR; + else if (eolMode == QStringLiteral("lf")) + return SC_EOL_LF; + else + return -1; +} + int ScintillaNext::allocateIndicator(const QString &name) { return indicatorResources.requestResource(name); diff --git a/src/NotepadNext/ScintillaNext.h b/src/NotepadNext/ScintillaNext.h index 0eab58078..5d3d38bf8 100644 --- a/src/NotepadNext/ScintillaNext.h +++ b/src/NotepadNext/ScintillaNext.h @@ -44,6 +44,8 @@ class ScintillaNext : public ScintillaEdit virtual ~ScintillaNext(); static ScintillaNext *fromFile(const QString &filePath, bool tryToCreate=false); + static QString eolModeToString(int eolMode); + static int stringToEolMode(QString eolMode); int allocateIndicator(const QString &name); diff --git a/src/NotepadNext/dialogs/PreferencesDialog.cpp b/src/NotepadNext/dialogs/PreferencesDialog.cpp index c58fe7cb0..46663b10e 100644 --- a/src/NotepadNext/dialogs/PreferencesDialog.cpp +++ b/src/NotepadNext/dialogs/PreferencesDialog.cpp @@ -21,6 +21,7 @@ #include "NotepadNextApplication.h" #include "TranslationManager.h" #include "ui_PreferencesDialog.h" +#include "ScintillaNext.h" #include @@ -78,6 +79,23 @@ PreferencesDialog::PreferencesDialog(ApplicationSettings *settings, QWidget *par ui->spbDefaultFontSize->setValue(settings->fontSize()); connect(ui->spbDefaultFontSize, QOverload::of(&QSpinBox::valueChanged), settings, &ApplicationSettings::setFontSize); connect(settings, &ApplicationSettings::fontSizeChanged, ui->spbDefaultFontSize, &QSpinBox::setValue); + + ui->comboBoxLineEndings->addItem(tr("System Default"), QString("")); + ui->comboBoxLineEndings->addItem(tr("Windows (CR LF)"), ScintillaNext::eolModeToString(SC_EOL_CRLF)); + ui->comboBoxLineEndings->addItem(tr("Linux (LF)"), ScintillaNext::eolModeToString(SC_EOL_LF)); + ui->comboBoxLineEndings->addItem(tr("Macintosh (CR)"), ScintillaNext::eolModeToString(SC_EOL_CR)); + + // Select the current one + int index = ui->comboBoxLineEndings->findData(settings->defaultEOLMode()); + ui->comboBoxLineEndings->setCurrentIndex(index == -1 ? 0 : index); + + connect(ui->comboBoxLineEndings, QOverload::of(&QComboBox::currentIndexChanged), this, [=](int index) { + settings->setDefaultEOLMode(ui->comboBoxLineEndings->itemData(index).toString()); + }); + connect(settings, &ApplicationSettings::defaultEOLModeChanged, this, [=](QString defaultEOLMode) { + int index = ui->comboBoxLineEndings->findData(defaultEOLMode); + ui->comboBoxLineEndings->setCurrentIndex(index == -1 ? 0 : index); + }); } PreferencesDialog::~PreferencesDialog() diff --git a/src/NotepadNext/dialogs/PreferencesDialog.ui b/src/NotepadNext/dialogs/PreferencesDialog.ui index ee7df5ec5..e9f76f1ce 100644 --- a/src/NotepadNext/dialogs/PreferencesDialog.ui +++ b/src/NotepadNext/dialogs/PreferencesDialog.ui @@ -6,8 +6,8 @@ 0 0 - 838 - 469 + 803 + 564 @@ -15,169 +15,202 @@ - - - - - Show menu bar - - - - - - - Show toolbar - - - - - - - Show status bar - - - - - - - Restore previous session - - - true - - - false - - - - - - Unsaved changes - - - - - - - Temporary files - - - - - - - - - - - - Recenter find/replace dialog when opened + + + true + + + + 0 + 0 + 783 + 512 + + + + + + + + + Show menu bar + + + + + + + Show toolbar + + + + + + + Show status bar + + + + + + + Restore previous session + + + true + + + false + + + + + + Unsaved changes + + + + + + + Temporary files + + + + + + + + + + + + Recenter find/replace dialog when opened + + + + + + + Combine search results + + + + + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + Translation: + + + + + + + + + Exit on last tab closed + + + + + + + + + Default Font + + + + + + Font + + + + + + + + + + Font Size + + + + + + + pt + + + 2 + + + 48 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + Default Line Endings + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + - - - - Combine search results - - - - - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - - Translation: - - - - - - - - - Exit on last tab closed - - - - - - - - - Default Font - - - - - - Font - - - - - - - - - - Font Size - - - - - - - pt - - - 2 - - - 48 - - - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - -