From f2c3e99871f410d33271cbb4a80739107570d48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=BCng?= Date: Sat, 19 Oct 2024 10:47:22 +0200 Subject: [PATCH] store wrap mode for individual documents, but since those are only saved if "load session on start" is active, also save wrap mode for file extensions closes #392 --- src/Commands/CmdLineWrap.h | 27 ++++++++++++++++++++- src/Commands/CmdSession.cpp | 9 ++++--- src/Document.h | 47 +++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/Commands/CmdLineWrap.h b/src/Commands/CmdLineWrap.h index 27ecdc85..66153a05 100644 --- a/src/Commands/CmdLineWrap.h +++ b/src/Commands/CmdLineWrap.h @@ -18,6 +18,7 @@ #pragma once #include "ICommand.h" #include "BowPadUI.h" +#include "../../ext/sktoolslib/PathUtils.h" constexpr Scintilla::WrapVisualFlag operator|(Scintilla::WrapVisualFlag a, Scintilla::WrapVisualFlag b) noexcept { @@ -36,8 +37,14 @@ class CCmdLineWrap : public ICommand bool Execute() override { + auto ext = CPathUtils::GetFileExtension(GetActiveDocument().m_path); Scintilla().SetWrapMode(Scintilla().WrapMode() != Scintilla::Wrap::None ? Scintilla::Wrap::None : Scintilla::Wrap::Word); CIniSettings::Instance().SetInt64(L"View", L"wrapmode", static_cast(Scintilla().WrapMode())); + if (!ext.empty()) + { + auto key = L"wrapmode_" + ext; + CIniSettings::Instance().SetInt64(L"View", key.c_str(), static_cast(Scintilla().WrapMode())); + } InvalidateUICommand(UI_INVALIDATIONS_PROPERTY, &UI_PKEY_BooleanValue); Scintilla().SetHScrollBar(Scintilla().WrapMode() == Scintilla::Wrap::None); GetModActiveDocument().m_wrapMode = Scintilla().WrapMode(); @@ -55,7 +62,25 @@ class CCmdLineWrap : public ICommand { if (ptbHdr->hdr.code == TCN_SELCHANGE) { - Scintilla().SetWrapMode(GetActiveDocument().m_wrapMode); + const auto& activeDocument = GetActiveDocument(); + if (activeDocument.m_wrapMode) + Scintilla().SetWrapMode(*GetActiveDocument().m_wrapMode); + else + { + // check if we have a wrap mode set for the file extension of the active document + auto ext = CPathUtils::GetFileExtension(activeDocument.m_path); + if (!ext.empty()) + { + auto key = L"wrapmode_" + ext; + auto wrapMode = CIniSettings::Instance().GetInt64(L"View", key.c_str(), -1); + if (wrapMode >= 0) + Scintilla().SetWrapMode(static_cast(wrapMode)); + else + Scintilla().SetWrapMode(static_cast(Scintilla::Wrap::None)); + } + else + Scintilla().SetWrapMode(static_cast(Scintilla::Wrap::None)); + } InvalidateUICommand(UI_INVALIDATIONS_PROPERTY, &UI_PKEY_BooleanValue); } } diff --git a/src/Commands/CmdSession.cpp b/src/Commands/CmdSession.cpp index 101830e6..ec74ee6c 100644 --- a/src/Commands/CmdSession.cpp +++ b/src/Commands/CmdSession.cpp @@ -197,7 +197,8 @@ void CCmdSessionLoad::OnClose() settings.SetString(sessionSection(), CStringUtils::Format(L"path%d", saveIndex).c_str(), doc.m_path.c_str()); settings.SetInt64(sessionSection(), CStringUtils::Format(L"tabspace%d", saveIndex).c_str(), static_cast(doc.m_tabSpace)); settings.SetInt64(sessionSection(), CStringUtils::Format(L"readdir%d", saveIndex).c_str(), static_cast(doc.m_readDir)); - settings.SetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", saveIndex).c_str(), static_cast(doc.m_wrapMode)); + if (doc.m_wrapMode) + settings.SetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", saveIndex).c_str(), static_cast(*doc.m_wrapMode)); doc.m_path = docOrigPath; ++saveIndex; } @@ -291,8 +292,10 @@ void CCmdSessionLoad::RestoreSavedSession() const pos.m_lastStyleLine = static_cast(settings.GetInt64(sessionSection(), CStringUtils::Format(L"laststyleline%d", fileNum).c_str(), 0)); doc.m_tabSpace = static_cast(settings.GetInt64(sessionSection(), CStringUtils::Format(L"tabspace%d", fileNum).c_str(), 0)); doc.m_readDir = static_cast(settings.GetInt64(sessionSection(), CStringUtils::Format(L"readdir%d", fileNum).c_str(), 0)); - doc.m_wrapMode = static_cast(settings.GetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", fileNum).c_str(), 0)); - auto folds = settings.GetString(sessionSection(), CStringUtils::Format(L"foldlines%d", fileNum).c_str(), nullptr); + doc.m_wrapMode = static_cast(settings.GetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", fileNum).c_str(), -1)); + if (static_cast(*doc.m_wrapMode) < 0) + doc.m_wrapMode = std::nullopt; + auto folds = settings.GetString(sessionSection(), CStringUtils::Format(L"foldlines%d", fileNum).c_str(), nullptr); pos.m_lineStateVector.clear(); if (folds) { diff --git a/src/Document.h b/src/Document.h index 0a2e8599..aa3d4c7d 100644 --- a/src/Document.h +++ b/src/Document.h @@ -20,6 +20,7 @@ #include "../ext/scintilla/include/ILoader.h" #include +#include using Document = Scintilla::IDocumentEditable*; @@ -105,7 +106,7 @@ class CDocument , m_bDoSaveAs(false) , m_tabSpace(TabSpace::Default) , m_readDir(Scintilla::Bidirectional::Disabled) - , m_wrapMode(Scintilla::Wrap::None) + , m_wrapMode(std::nullopt) , m_aliveMutex(nullptr) { m_lastWriteTime.dwHighDateTime = 0; @@ -125,29 +126,29 @@ class CDocument { return m_language; } - void SetLanguage(const std::string& lang); + void SetLanguage(const std::string& lang); - Document m_document; - std::wstring m_path; - int m_encoding; - int m_encodingSaving; - EOLFormat m_format; - bool m_bHasBOM; - bool m_bHasBOMSaving; - bool m_bTrimBeforeSave; - bool m_bEnsureNewlineAtEnd; - bool m_bIsDirty; - bool m_bNeedsSaving; - bool m_bIsReadonly; - bool m_bIsWriteProtected; - bool m_bDoSaveAs; ///< even if m_path is set, always ask where to save - FILETIME m_lastWriteTime; - CPosData m_position; - TabSpace m_tabSpace; - Scintilla::Bidirectional m_readDir; - Scintilla::Wrap m_wrapMode; - std::function m_saveCallback; - HANDLE m_aliveMutex; + Document m_document; + std::wstring m_path; + int m_encoding; + int m_encodingSaving; + EOLFormat m_format; + bool m_bHasBOM; + bool m_bHasBOMSaving; + bool m_bTrimBeforeSave; + bool m_bEnsureNewlineAtEnd; + bool m_bIsDirty; + bool m_bNeedsSaving; + bool m_bIsReadonly; + bool m_bIsWriteProtected; + bool m_bDoSaveAs; ///< even if m_path is set, always ask where to save + FILETIME m_lastWriteTime; + CPosData m_position; + TabSpace m_tabSpace; + Scintilla::Bidirectional m_readDir; + std::optional m_wrapMode; + std::function m_saveCallback; + HANDLE m_aliveMutex; private: std::string m_language;