From a8c66e73306e329ccea7b05d887d033358bcdb2f Mon Sep 17 00:00:00 2001 From: RaiKoHoff Date: Thu, 9 Jan 2020 18:10:42 +0100 Subject: [PATCH] + fix: Wrap to Column: respect tab width settings --- src/Edit.c | 49 +++++++++++++++++++------------------------------ src/Helpers.h | 4 ++++ src/Notepad3.c | 3 ++- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index a265fd95f..802bd992b 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -4054,12 +4054,10 @@ void EditRemoveDuplicateLines(HWND hwnd, bool bRemoveEmptyLines) // // EditWrapToColumn() // -void EditWrapToColumn(HWND hwnd, DocPosU nColumn/*,int nTabWidth*/) +void EditWrapToColumn(HWND hwnd, DocPosU nColumn) { - if (Sci_IsMultiOrRectangleSelection()) { - InfoBoxLng(MB_ICONWARNING, NULL, IDS_MUI_SELRECTORMULTI); - return; - } + DocPosU const tabWidth = SciCall_GetTabWidth(); + nColumn = clamppu(nColumn, tabWidth, LONG_LINES_MARKER_LIMIT); DocPosU iCurPos = SciCall_GetCurrentPos(); DocPosU iAnchorPos = SciCall_GetAnchor(); @@ -4105,6 +4103,7 @@ void EditWrapToColumn(HWND hwnd, DocPosU nColumn/*,int nTabWidth*/) #define ISWHITE(wc) (!(wc) || StrChrW(W_WHITESPACE,(wc))) #define ISLINEBREAK(wc) (!(wc) || ((wc) == wszEOL[0]) || ((wc) == wszEOL[1])) #define ISWORDCHAR(wc) (!ISWHITE(wc) && !ISLINEBREAK(wc) && !ISDELIMITER(wc)) + #define ISTAB(wc) ((wc) == L'\t') // -------------------------------------------------------------------------- DocPos iCaretShift = 0; @@ -4119,16 +4118,8 @@ void EditWrapToColumn(HWND hwnd, DocPosU nColumn/*,int nTabWidth*/) // read complete words while (ISWORDCHAR(w) && (iTextW < cchTextW)) { - if (ISLINEBREAK(w)) { - if (w != L'\0') { - pszConvW[cchConvW++] = w; - } - iLineLength = 0; - } - else if (w != L'\0') { - pszConvW[cchConvW++] = w; - ++iLineLength; - } + pszConvW[cchConvW++] = w; + ++iLineLength; w = pszTextW[++iTextW]; } @@ -4146,14 +4137,13 @@ void EditWrapToColumn(HWND hwnd, DocPosU nColumn/*,int nTabWidth*/) pszConvW[cchConvW++] = wszEOL[1]; } if (cchConvW <= iCurPos) { iCaretShift += cchEOL; } - iLineLength = 0; bModified = true; pszConvW[cchConvW++] = w; - ++iLineLength; + iLineLength = ISTAB(w) ? tabWidth : 1; } else { pszConvW[cchConvW++] = w; - ++iLineLength; + iLineLength += ISTAB(w) ? tabWidth : 1; } w = pszTextW[++iTextW]; } @@ -4162,23 +4152,22 @@ void EditWrapToColumn(HWND hwnd, DocPosU nColumn/*,int nTabWidth*/) DocPosU iNextWordLen = 1; DocPosU iNextW = iTextW; WCHAR w2 = pszTextW[iNextW]; - while (ISWORDCHAR(w2) && ((iLineLength + iNextWordLen) < nColumn) && (iNextW < cchTextW)) { + while (ISWORDCHAR(w2) && (iNextW < cchTextW)) { w2 = pszTextW[++iNextW]; ++iNextWordLen; } - if ((iLineLength + iNextWordLen + 1) >= nColumn) { - pszConvW[cchConvW++] = wszEOL[0]; - if (cchEOL > 1) { - pszConvW[cchConvW++] = wszEOL[1]; - } - if (cchConvW <= iCurPos) { iCaretShift += cchEOL; } - iLineLength = 0; - bModified = true; - } - if (w != L'\0') { + if ((iLineLength + iNextWordLen) >= nColumn) { + pszConvW[cchConvW++] = wszEOL[0]; + if (cchEOL > 1) { + pszConvW[cchConvW++] = wszEOL[1]; + } + if (cchConvW <= iCurPos) { iCaretShift += cchEOL; } + iLineLength = 0; + bModified = true; + } pszConvW[cchConvW++] = w; - ++iLineLength; + iLineLength += ISTAB(w) ? tabWidth : 1; } } diff --git a/src/Helpers.h b/src/Helpers.h index 59786f668..1f79d9723 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -134,6 +134,10 @@ inline DocPos clampp(DocPos x, DocPos lower, DocPos upper) { return (x < lower) ? lower : ((x > upper) ? upper : x); } +inline DocPosU clamppu(DocPosU x, DocPosU lower, DocPosU upper) { + return (x < lower) ? lower : ((x > upper) ? upper : x); +} + // Is the character an octal digit? inline bool IsDigitA(const CHAR ch) { return ((ch >= '0') && (ch <= '9')); } inline bool IsDigitW(const WCHAR wch) { return ((wch >= L'0') && (wch <= L'9')); } diff --git a/src/Notepad3.c b/src/Notepad3.c index 1138c5170..dbe74134d 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -4453,7 +4453,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) UINT uWrpCol = Globals.iWrapCol; if (ColumnWrapDlg(hwnd, IDD_MUI_COLUMNWRAP, &uWrpCol)) { - Globals.iWrapCol = clampi((int)uWrpCol, 1, LONG_LINES_MARKER_LIMIT); + Globals.iWrapCol = clampi((int)uWrpCol, SciCall_GetTabWidth(), LONG_LINES_MARKER_LIMIT); BeginWaitCursor(NULL); EditWrapToColumn(Globals.hwndEdit, Globals.iWrapCol); EndWaitCursor(); @@ -5203,6 +5203,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) _iLongLinesLimit = clampi(_iLongLinesLimit, 0, LONG_LINES_MARKER_LIMIT); Globals.fvCurFile.iLongLinesLimit = _iLongLinesLimit; Settings.LongLinesLimit = _iLongLinesLimit; + Globals.iWrapCol = _iLongLinesLimit; } Settings.MarkLongLines = true; Style_SetLongLineEdge(Globals.hwndEdit, Settings.LongLinesLimit);