Skip to content

Commit

Permalink
Simplify encoding conversion code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Oct 10, 2024
1 parent 2714853 commit ac3dac2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void EditSetNewText(LPCSTR lpstrText, DWORD cbText, Sci_Line lineCount) noexcept
//
// EditConvertText()
//
bool EditConvertText(UINT cpSource, UINT cpDest, bool bSetSavePoint) noexcept {
bool EditConvertText(UINT cpSource, UINT cpDest) noexcept {
if (cpSource == cpDest) {
return true;
}
Expand Down Expand Up @@ -236,7 +236,7 @@ bool EditConvertText(UINT cpSource, UINT cpDest, bool bSetSavePoint) noexcept {

SciCall_EmptyUndoBuffer();
SciCall_SetUndoCollection(true);
if (length == 0 && bSetSavePoint) {
if (length == 0 && StrIsEmpty(szCurFile)) {
SciCall_SetSavePoint();
}
UpdateLineNumberWidth();
Expand Down
4 changes: 2 additions & 2 deletions src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static inline void EditSetEmptyText() noexcept{
EditSetNewText("", 0, 1);
}

bool EditConvertText(UINT cpSource, UINT cpDest, bool bSetSavePoint) noexcept;
bool EditConvertText(UINT cpSource, UINT cpDest) noexcept;
void EditConvertToLargeMode() noexcept;
void EditReplaceDocument(HANDLE pdoc) noexcept;

Expand Down Expand Up @@ -544,7 +544,7 @@ constexpr bool Encoding_IsUTF8(int iEncoding) noexcept {
}

void Encoding_ReleaseResources() noexcept;
bool EditSetNewEncoding(int iEncoding, int iNewEncoding, BOOL bNoUI, bool bSetSavePoint) noexcept;
bool EditSetNewEncoding(int iEncoding, int iNewEncoding, BOOL bNoUI) noexcept;
void EditOnCodePageChanged(UINT oldCodePage, bool showControlCharacter, EDITFINDREPLACE *lpefr) noexcept;
const char* GetFoldDisplayEllipsis(UINT cpEdit, UINT acp) noexcept;
void Encoding_InitDefaults() noexcept;
Expand Down
10 changes: 4 additions & 6 deletions src/EditEncoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static inline bool IsValidEncoding(const NP2ENCODING *encoding) noexcept {
//
// EditSetNewEncoding()
//
bool EditSetNewEncoding(int iEncoding, int iNewEncoding, BOOL bNoUI, bool bSetSavePoint) noexcept {
bool EditSetNewEncoding(int iEncoding, int iNewEncoding, BOOL bNoUI) noexcept {
if (iEncoding != iNewEncoding) {
if (iEncoding != CPI_DEFAULT && iNewEncoding != CPI_DEFAULT) {
return true;
Expand All @@ -458,15 +458,13 @@ bool EditSetNewEncoding(int iEncoding, int iNewEncoding, BOOL bNoUI, bool bSetSa
const UINT cpDest = (mEncoding[iNewEncoding].uFlags & NCP_DEFAULT) ? iDefaultCodePage : SC_CP_UTF8;

if (SciCall_GetLength() == 0) {
const bool bIsEmptyUndoHistory = !(SciCall_CanUndo() || SciCall_CanRedo());

if (bNoUI || bIsEmptyUndoHistory || InfoBoxWarn(MB_YESNO, L"MsgConv2", IDS_ASK_ENCODING2) == IDYES) {
EditConvertText(cpSrc, cpDest, bSetSavePoint);
if (bNoUI || SciCall_GetUndoActions() == 0 || InfoBoxWarn(MB_YESNO, L"MsgConv2", IDS_ASK_ENCODING2) == IDYES) {
EditConvertText(cpSrc, cpDest);
return true;
}
} else if (bNoUI || InfoBoxWarn(MB_YESNO, L"MsgConv1", IDS_ASK_ENCODING) == IDYES) {
BeginWaitCursor();
EditConvertText(cpSrc, cpDest, false);
EditConvertText(cpSrc, cpDest);
EndWaitCursor();
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Notepad4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2993,7 +2993,7 @@ LRESULT MsgCommand(HWND hwnd, WPARAM wParam, LPARAM lParam) {
iNewEncoding = (mask >> (4*(LOWORD(wParam) - IDM_ENCODING_ANSI))) & 15;
}

if (EditSetNewEncoding(iCurrentEncoding, iNewEncoding, flagSetEncoding, StrIsEmpty(szCurFile))) {
if (EditSetNewEncoding(iCurrentEncoding, iNewEncoding, flagSetEncoding)) {
if (SciCall_GetLength() == 0) {
iCurrentEncoding = iNewEncoding;
if (StrIsEmpty(szCurFile) || Encoding_HasBOM(iNewEncoding) == Encoding_HasBOM(iOriginalEncoding)) {
Expand Down
4 changes: 4 additions & 0 deletions src/SciCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ inline void SciCall_EndUndoAction() noexcept {
SciCall(SCI_ENDUNDOACTION, 0, 0);
}

inline int SciCall_GetUndoActions() noexcept {
return static_cast<int>(SciCall(SCI_GETUNDOACTIONS, 0, 0));
}

// Selection and information

inline Sci_Position SciCall_GetLength() noexcept {
Expand Down
2 changes: 1 addition & 1 deletion src/Styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ void Style_SetLexer(PEDITLEXER pLexNew, BOOL bLexerChanged) noexcept {
}

// change empty file to use scheme default encoding and line ending
if (SciCall_GetLength() == 0 && !(SciCall_CanUndo() || SciCall_CanRedo())) {
if (SciCall_GetLength() == 0 && SciCall_GetUndoActions() == 0) {
EditApplyDefaultEncoding(pLexNew, bLexerChanged & LexerChanged_Override);
}
SciCall_SetLexer(pLexNew->iLexer);
Expand Down

0 comments on commit ac3dac2

Please sign in to comment.