From bdf5382a5e39c375a60916893747c412405a0f4d Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Thu, 23 Feb 2023 03:07:51 +0100 Subject: [PATCH] +enh: enhancement for auto-close char-pairs --- src/Notepad3.c | 50 +++++++++++++++++++++++++++++++++++++++++--------- src/SciCall.h | 3 +++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/Notepad3.c b/src/Notepad3.c index 7f82d592e..89d968ef7 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -8358,23 +8358,39 @@ static void _HandleInsertCheck(const SCNotification* const scn) } if (Settings.AutoCloseQuotes) { if (scn->length == 1) { - bool bInserted = true; DocPos len = 0; switch (scn->text[0]) { case '"': - len = _EncloseSelectionBuffer('"', '"'); + if (Sci_GetCurrChar() == '"') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + else { + len = _EncloseSelectionBuffer('"', '"'); + } break; case '\'': - len = _EncloseSelectionBuffer('\'', '\''); + if (Sci_GetCurrChar() == '\'') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + else { + len = _EncloseSelectionBuffer('\'', '\''); + } break; case '`': - len = _EncloseSelectionBuffer('`', '`'); + if (Sci_GetCurrChar() == '`') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + else { + len = _EncloseSelectionBuffer('`', '`'); + } break; default: - bInserted = false; break; } - if (bInserted) { + if (len) { SciCall_ChangeInsertion(len, s_SelectionBuffer); if (len == 2) { PostMessage(Globals.hwndEdit, SCI_CHARLEFT, 0, 0); @@ -8384,7 +8400,6 @@ static void _HandleInsertCheck(const SCNotification* const scn) } if (Settings.AutoCloseBrackets) { if (scn->length == 1) { - bool bInserted = true; DocPos len = 0; switch (scn->text[0]) { case '[': @@ -8396,11 +8411,28 @@ static void _HandleInsertCheck(const SCNotification* const scn) case '(': len = _EncloseSelectionBuffer('(', ')'); break; + case ')': + if (Sci_GetCurrChar() == ')') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + break; + case '}': + if (Sci_GetCurrChar() == '}') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + break; + case ']': + if (Sci_GetCurrChar() == ']') { + SciCall_ChangeInsertion(0, ""); // clear + PostMessage(Globals.hwndEdit, SCI_CHARRIGHT, 0, 0); + } + break; default: - bInserted = false; break; } - if (bInserted) { + if (len) { SciCall_ChangeInsertion(len, s_SelectionBuffer); if (len == 2) { PostMessage(Globals.hwndEdit, SCI_CHARLEFT, 0, 0); diff --git a/src/SciCall.h b/src/SciCall.h index 3bcaf2581..b10675123 100644 --- a/src/SciCall.h +++ b/src/SciCall.h @@ -772,6 +772,9 @@ DeclareSciCallR0(IsSelectionRectangle, SELECTIONISRECTANGLE, bool); #define Sci_GetLineStartPosition(position) SciCall_PositionFromLine(SciCall_LineFromPosition(position)) #define Sci_GetLineEndPosition(position) SciCall_GetLineEndPosition(SciCall_LineFromPosition(position)) +#define Sci_GetCurrChar() SciCall_GetCharAt(SciCall_GetCurrentPos()) +#define Sci_GetNextChar() SciCall_GetCharAt(SciCall_PositionAfter(SciCall_GetCurrentPos())) + // length of line w/o line-end chars (full use SciCall_LineLength() #define Sci_GetNetLineLength(line) (SciCall_GetLineEndPosition(line) - SciCall_PositionFromLine(line))