From da31945be0bda5af706bbc1062ce701146283fc3 Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Fri, 26 Mar 2021 00:08:50 +0100 Subject: [PATCH] + fix: TinyExpr regresion inserting newline after equal sign --- language/common_res.h | 2 +- src/Notepad3.c | 28 ++++++++++++++++++---------- src/Notepad3.rc | 2 +- src/tinyexpr/tinyexpr.h | 3 +++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/language/common_res.h b/language/common_res.h index ed694a4d7..1d1ecec9a 100644 --- a/language/common_res.h +++ b/language/common_res.h @@ -443,7 +443,7 @@ #define CMD_CHECK_INDENTATION 20058 #define CMD_ARROW_UP 20059 #define CMD_ARROW_DOWN 20060 -#define CMD_ENTER_RTURN 20061 +#define CMD_ENTER_RETURN 20061 #define IDS_MUI_MENU_LANGUAGE 34500 #define IDS_MUI_LANG_EN_US 34501 diff --git a/src/Notepad3.c b/src/Notepad3.c index d1d147b57..6ad1f8f61 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -2066,29 +2066,36 @@ static bool _EvalTinyExpr(bool qmark) return false; } - DocPos const posCur = SciCall_GetCurrentPos(); - DocPos const posBegin = qmark ? SciCall_PositionBefore(posCur) : posCur; + DocPos const posSelStart = SciCall_GetSelectionStart(); + DocPos const posBegin = qmark ? SciCall_PositionBefore(posSelStart) : posSelStart; + DocPos posBefore = SciCall_PositionBefore(posBegin); char chBefore = SciCall_GetCharAt(posBefore); + while (IsBlankCharA(chBefore) && (posBefore > 0)) { posBefore = SciCall_PositionBefore(posBefore); chBefore = SciCall_GetCharAt(posBefore); } if (chBefore == '=') { // got "=?" or ENTER : evaluate expression trigger - int const lineLen = (int)SciCall_LineLength(SciCall_LineFromPosition(posCur)) + 1; + + int const lineLen = (int)SciCall_LineLength(SciCall_LineFromPosition(posSelStart)) + 1; char *lineBuf = (char *)AllocMem(lineLen, HEAP_ZERO_MEMORY); WCHAR *lineBufW = (WCHAR *)AllocMem(lineLen * sizeof(WCHAR), HEAP_ZERO_MEMORY); if (lineBuf && lineBufW) { - DocPos const iLnCaretPos = SciCall_GetCurLine((lineLen - 1), lineBuf); - lineBuf[iLnCaretPos - (posCur - posBefore)] = '\0'; // exclude "=?" + if (posSelStart < SciCall_GetCurrentPos()) { + SciCall_SwapMainAnchorCaret(); + } + DocPos const iLnCaretPos = SciCall_GetCurLine((lineLen - 1), lineBuf); + lineBuf[iLnCaretPos - (posSelStart - posBefore)] = '\0'; // exclude "=?" char const defchar = (char)0x24; MultiByteToWideChar(Encoding_SciCP, 0, lineBuf, -1, lineBufW, lineLen); - WideCharToMultiByte(1252, (WC_COMPOSITECHECK | WC_DISCARDNS), lineBufW, -1, lineBuf, lineLen, &defchar, NULL); - StrDelChrA(lineBuf, chr_currency); + WideCharToMultiByte(te_cp(), (WC_COMPOSITECHECK | WC_DISCARDNS), lineBufW, -1, lineBuf, lineLen, &defchar, NULL); FreeMem(lineBufW); + // canonicalize fetched line + StrDelChrA(lineBuf, chr_currency); const char *pBegin = lineBuf; while (IsBlankCharA(*pBegin)) { ++pBegin; @@ -2099,7 +2106,7 @@ static bool _EvalTinyExpr(bool qmark) while (*pBegin && exprErr) { dExprEval = te_interp(pBegin, &exprErr); // proceed to next possible expression - while (exprErr && !te_is_op(pBegin++)) {} + while (*pBegin && exprErr && !te_is_op(pBegin++)) {} } FreeMem(lineBuf); @@ -2111,7 +2118,8 @@ static bool _EvalTinyExpr(bool qmark) } else { StringCchPrintfA(chExpr, COUNTOF(chExpr), "%.7G", dExprEval); } - SciCall_SetSel(posBegin, posCur); + SciCall_ReplaceSel(""); + SciCall_SetSel(posBegin, posSelStart); SciCall_ReplaceSel(chExpr); return true; } @@ -6015,7 +6023,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) } break; - case CMD_ENTER_RTURN: { + case CMD_ENTER_RETURN: { _EvalTinyExpr(false); SciCall_NewLine(); } diff --git a/src/Notepad3.rc b/src/Notepad3.rc index 2c483e174..fc43dd39a 100644 --- a/src/Notepad3.rc +++ b/src/Notepad3.rc @@ -369,7 +369,7 @@ BEGIN VK_OEM_PLUS, CMD_FOLDJUMPDOWN, VIRTKEY, ALT, NOINVERT VK_OEM_PLUS, IDM_VIEW_ZOOMIN, VIRTKEY, CONTROL, NOINVERT VK_OEM_PLUS, CMD_INCREASENUM, VIRTKEY, CONTROL, ALT, NOINVERT - VK_RETURN, CMD_ENTER_RTURN, VIRTKEY, NOINVERT + VK_RETURN, CMD_ENTER_RETURN, VIRTKEY, NOINVERT VK_RETURN, CMD_INSERTNEWLINE, VIRTKEY, CONTROL, NOINVERT VK_RETURN, CMD_SHIFTCTRLENTER, VIRTKEY, SHIFT, CONTROL, NOINVERT VK_RETURN, IDM_EDIT_COMPLETEWORD, VIRTKEY, CONTROL, ALT, NOINVERT diff --git a/src/tinyexpr/tinyexpr.h b/src/tinyexpr/tinyexpr.h index fad777a67..63649dad6 100644 --- a/src/tinyexpr/tinyexpr.h +++ b/src/tinyexpr/tinyexpr.h @@ -105,6 +105,9 @@ void te_print(const te_expr *n); /* This is safe to call on NULL pointers. */ void te_free(te_expr *n); +/* ANSI codepage of te operators */ +inline unsigned te_cp() { return 1252U; } + /* check for operator or special character. */ inline int te_is_op(const char* const expr) { if (!expr)