Skip to content

Commit

Permalink
store wrap mode for individual documents, but since those are only sa…
Browse files Browse the repository at this point in the history
…ved if "load session on start" is active, also save wrap mode for file extensions

closes #392
  • Loading branch information
stefankueng committed Oct 19, 2024
1 parent eb53fb4 commit f2c3e99
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
27 changes: 26 additions & 1 deletion src/Commands/CmdLineWrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<int>(Scintilla().WrapMode()));
if (!ext.empty())
{
auto key = L"wrapmode_" + ext;
CIniSettings::Instance().SetInt64(L"View", key.c_str(), static_cast<int>(Scintilla().WrapMode()));
}
InvalidateUICommand(UI_INVALIDATIONS_PROPERTY, &UI_PKEY_BooleanValue);
Scintilla().SetHScrollBar(Scintilla().WrapMode() == Scintilla::Wrap::None);
GetModActiveDocument().m_wrapMode = Scintilla().WrapMode();
Expand All @@ -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<Scintilla::Wrap>(wrapMode));
else
Scintilla().SetWrapMode(static_cast<Scintilla::Wrap>(Scintilla::Wrap::None));
}
else
Scintilla().SetWrapMode(static_cast<Scintilla::Wrap>(Scintilla::Wrap::None));
}
InvalidateUICommand(UI_INVALIDATIONS_PROPERTY, &UI_PKEY_BooleanValue);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Commands/CmdSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(doc.m_tabSpace));
settings.SetInt64(sessionSection(), CStringUtils::Format(L"readdir%d", saveIndex).c_str(), static_cast<int>(doc.m_readDir));
settings.SetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", saveIndex).c_str(), static_cast<int>(doc.m_wrapMode));
if (doc.m_wrapMode)
settings.SetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", saveIndex).c_str(), static_cast<int>(*doc.m_wrapMode));
doc.m_path = docOrigPath;
++saveIndex;
}
Expand Down Expand Up @@ -291,8 +292,10 @@ void CCmdSessionLoad::RestoreSavedSession() const
pos.m_lastStyleLine = static_cast<size_t>(settings.GetInt64(sessionSection(), CStringUtils::Format(L"laststyleline%d", fileNum).c_str(), 0));
doc.m_tabSpace = static_cast<TabSpace>(settings.GetInt64(sessionSection(), CStringUtils::Format(L"tabspace%d", fileNum).c_str(), 0));
doc.m_readDir = static_cast<Scintilla::Bidirectional>(settings.GetInt64(sessionSection(), CStringUtils::Format(L"readdir%d", fileNum).c_str(), 0));
doc.m_wrapMode = static_cast<Scintilla::Wrap>(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<Scintilla::Wrap>(settings.GetInt64(sessionSection(), CStringUtils::Format(L"wrapmode%d", fileNum).c_str(), -1));
if (static_cast<int>(*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)
{
Expand Down
47 changes: 24 additions & 23 deletions src/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../ext/scintilla/include/ILoader.h"

#include <functional>
#include <optional>

using Document = Scintilla::IDocumentEditable*;

Expand Down Expand Up @@ -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;
Expand All @@ -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<void()> 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<Scintilla::Wrap> m_wrapMode;
std::function<void()> m_saveCallback;
HANDLE m_aliveMutex;

private:
std::string m_language;
Expand Down

0 comments on commit f2c3e99

Please sign in to comment.