Skip to content

Commit 872eed2

Browse files
committed
Alt+M - replace all substitutions #271
1 parent 1143ca2 commit 872eed2

File tree

5 files changed

+86
-181
lines changed

5 files changed

+86
-181
lines changed

src/Edit.c

Lines changed: 38 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,17 +2823,7 @@ void EditMoveDown(HWND hwnd)
28232823
void EditModifyLines(HWND hwnd, LPCWSTR pwszPrefix, LPCWSTR pwszAppend)
28242824
{
28252825
char mszPrefix1[256 * 3] = "";
2826-
char mszPrefix2[256 * 3] = "";
2827-
BOOL bPrefixNum = FALSE;
2828-
int iPrefixNum = 0;
2829-
int iPrefixNumWidth = 1;
2830-
char *pszPrefixNumPad = "";
28312826
char mszAppend1[256 * 3] = "";
2832-
char mszAppend2[256 * 3] = "";
2833-
BOOL bAppendNum = FALSE;
2834-
int iAppendNum = 0;
2835-
int iAppendNumWidth = 1;
2836-
char *pszAppendNumPad = "";
28372827
int mbcp;
28382828

28392829
int iSelStart = SciCall_GetSelStart();
@@ -2858,8 +2848,6 @@ void EditModifyLines(HWND hwnd, LPCWSTR pwszPrefix, LPCWSTR pwszAppend)
28582848

28592849
if (!n2e_ShowPromptIfSelectionModeIsRectangle(hwnd))
28602850
{
2861-
char *p;
2862-
int i;
28632851

28642852
int iLine;
28652853

@@ -2872,155 +2860,31 @@ void EditModifyLines(HWND hwnd, LPCWSTR pwszPrefix, LPCWSTR pwszAppend)
28722860
iLineEnd--;
28732861
}
28742862

2875-
if (lstrlenA(mszPrefix1))
2876-
{
2877-
2878-
p = mszPrefix1;
2879-
while (!bPrefixNum && (p = StrStrA(p, "$(")))
2880-
{
2881-
2882-
if (StrCmpNA(p, "$(I)", CSTRLEN("$(I)")) == 0)
2883-
{
2884-
*p = 0;
2885-
StrCpyA(mszPrefix2, p + CSTRLEN("$(I)"));
2886-
bPrefixNum = TRUE;
2887-
iPrefixNum = 0;
2888-
for (i = iLineEnd - iLineStart; i >= 10; i = i / 10)
2889-
iPrefixNumWidth++;
2890-
pszPrefixNumPad = "";
2891-
}
2892-
2893-
else if (StrCmpNA(p, "$(0I)", CSTRLEN("$(0I)")) == 0)
2894-
{
2895-
*p = 0;
2896-
StrCpyA(mszPrefix2, p + CSTRLEN("$(0I)"));
2897-
bPrefixNum = TRUE;
2898-
iPrefixNum = 0;
2899-
for (i = iLineEnd - iLineStart; i >= 10; i = i / 10)
2900-
iPrefixNumWidth++;
2901-
pszPrefixNumPad = "0";
2902-
}
2903-
2904-
else if (StrCmpNA(p, "$(N)", CSTRLEN("$(N)")) == 0)
2905-
{
2906-
*p = 0;
2907-
StrCpyA(mszPrefix2, p + CSTRLEN("$(N)"));
2908-
bPrefixNum = TRUE;
2909-
iPrefixNum = 1;
2910-
for (i = iLineEnd - iLineStart + 1; i >= 10; i = i / 10)
2911-
iPrefixNumWidth++;
2912-
pszPrefixNumPad = "";
2913-
}
2914-
else if (StrCmpNA(p, "$(0N)", CSTRLEN("$(0N)")) == 0)
2915-
{
2916-
*p = 0;
2917-
StrCpyA(mszPrefix2, p + CSTRLEN("$(0N)"));
2918-
bPrefixNum = TRUE;
2919-
iPrefixNum = 1;
2920-
for (i = iLineEnd - iLineStart + 1; i >= 10; i = i / 10)
2921-
iPrefixNumWidth++;
2922-
pszPrefixNumPad = "0";
2923-
}
2924-
2925-
else if (StrCmpNA(p, "$(L)", CSTRLEN("$(L)")) == 0)
2926-
{
2927-
*p = 0;
2928-
StrCpyA(mszPrefix2, p + CSTRLEN("$(L)"));
2929-
bPrefixNum = TRUE;
2930-
iPrefixNum = iLineStart + 1;
2931-
for (i = iLineEnd + 1; i >= 10; i = i / 10)
2932-
iPrefixNumWidth++;
2933-
pszPrefixNumPad = "";
2934-
}
2935-
2936-
else if (StrCmpNA(p, "$(0L)", CSTRLEN("$(0L)")) == 0)
2937-
{
2938-
*p = 0;
2939-
StrCpyA(mszPrefix2, p + CSTRLEN("$(0L)"));
2940-
bPrefixNum = TRUE;
2941-
iPrefixNum = iLineStart + 1;
2942-
for (i = iLineEnd + 1; i >= 10; i = i / 10)
2943-
iPrefixNumWidth++;
2944-
pszPrefixNumPad = "0";
2945-
}
2946-
p += CSTRLEN("$(");
2947-
}
2948-
}
2949-
2950-
if (lstrlenA(mszAppend1))
2951-
{
2952-
2953-
p = mszAppend1;
2954-
while (!bAppendNum && (p = StrStrA(p, "$(")))
2955-
{
2956-
2957-
if (StrCmpNA(p, "$(I)", CSTRLEN("$(I)")) == 0)
2958-
{
2959-
*p = 0;
2960-
StrCpyA(mszAppend2, p + CSTRLEN("$(I)"));
2961-
bAppendNum = TRUE;
2962-
iAppendNum = 0;
2963-
for (i = iLineEnd - iLineStart; i >= 10; i = i / 10)
2964-
iAppendNumWidth++;
2965-
pszAppendNumPad = "";
2966-
}
2967-
else if (StrCmpNA(p, "$(0I)", CSTRLEN("$(0I)")) == 0)
2968-
{
2969-
*p = 0;
2970-
StrCpyA(mszAppend2, p + CSTRLEN("$(0I)"));
2971-
bAppendNum = TRUE;
2972-
iAppendNum = 0;
2973-
for (i = iLineEnd - iLineStart; i >= 10; i = i / 10)
2974-
iAppendNumWidth++;
2975-
pszAppendNumPad = "0";
2976-
}
2977-
else if (StrCmpNA(p, "$(N)", CSTRLEN("$(N)")) == 0)
2978-
{
2979-
*p = 0;
2980-
StrCpyA(mszAppend2, p + CSTRLEN("$(N)"));
2981-
bAppendNum = TRUE;
2982-
iAppendNum = 1;
2983-
for (i = iLineEnd - iLineStart + 1; i >= 10; i = i / 10)
2984-
iAppendNumWidth++;
2985-
pszAppendNumPad = "";
2986-
}
2987-
2988-
else if (StrCmpNA(p, "$(0N)", CSTRLEN("$(0N)")) == 0)
2989-
{
2990-
*p = 0;
2991-
StrCpyA(mszAppend2, p + CSTRLEN("$(0N)"));
2992-
bAppendNum = TRUE;
2993-
iAppendNum = 1;
2994-
for (i = iLineEnd - iLineStart + 1; i >= 10; i = i / 10)
2995-
iAppendNumWidth++;
2996-
pszAppendNumPad = "0";
2997-
}
2998-
else if (StrCmpNA(p, "$(L)", CSTRLEN("$(L)")) == 0)
2999-
{
3000-
*p = 0;
3001-
StrCpyA(mszAppend2, p + CSTRLEN("$(L)"));
3002-
bAppendNum = TRUE;
3003-
iAppendNum = iLineStart + 1;
3004-
for (i = iLineEnd + 1; i >= 10; i = i / 10)
3005-
iAppendNumWidth++;
3006-
pszAppendNumPad = "";
3007-
}
3008-
3009-
else if (StrCmpNA(p, "$(0L)", CSTRLEN("$(0L)")) == 0)
3010-
{
3011-
*p = 0;
3012-
StrCpyA(mszAppend2, p + CSTRLEN("$(0L)"));
3013-
bAppendNum = TRUE;
3014-
iAppendNum = iLineStart + 1;
3015-
for (i = iLineEnd + 1; i >= 10; i = i / 10)
3016-
iAppendNumWidth++;
3017-
pszAppendNumPad = "0";
3018-
}
3019-
p += CSTRLEN("$(");
3020-
}
3021-
}
3022-
30232863
SendMessage(hwnd, SCI_BEGINUNDOACTION, 0, 0);
2864+
2865+
// [2e]: Alt+M - replace all substitutions #271
2866+
int iPrefixAbsZeroCounter = 1;
2867+
int iPrefixRelZeroCounter = 1;
2868+
int iPrefixRel0ZeroCounter = 1;
2869+
2870+
for (int i = iLineEnd + 1; i >= 10; i = i / 10)
2871+
iPrefixAbsZeroCounter++;
2872+
for (int i = iLineEnd - iLineStart + 1; i >= 10; i = i / 10)
2873+
iPrefixRelZeroCounter++;
2874+
for (int i = iLineEnd - iLineStart; i >= 10; i = i / 10)
2875+
iPrefixRel0ZeroCounter++;
2876+
2877+
char chPrefixAbsFormat[10], chPrefixAbsZeroFormat[10];
2878+
char chPrefixRelFormat[10], chPrefixRelZeroFormat[10];
2879+
char chPrefixRel0Format[10], chPrefixRel0ZeroFormat[10];
2880+
2881+
wsprintfA(chPrefixAbsFormat, "%%%ii", iPrefixAbsZeroCounter);
2882+
wsprintfA(chPrefixAbsZeroFormat, "%%0%ii", iPrefixAbsZeroCounter);
2883+
wsprintfA(chPrefixRelFormat, "%%%ii", iPrefixRelZeroCounter);
2884+
wsprintfA(chPrefixRelZeroFormat, "%%0%ii", iPrefixRelZeroCounter);
2885+
wsprintfA(chPrefixRel0Format, "%%%ii", iPrefixRel0ZeroCounter);
2886+
wsprintfA(chPrefixRel0ZeroFormat, "%%0%ii", iPrefixRel0ZeroCounter);
2887+
// [/2e]
30242888

30252889
for (iLine = iLineStart; iLine <= iLineEnd; iLine++)
30262890
{
@@ -3030,16 +2894,13 @@ void EditModifyLines(HWND hwnd, LPCWSTR pwszPrefix, LPCWSTR pwszAppend)
30302894
{
30312895
char mszInsert[512 * 3];
30322896
lstrcpyA(mszInsert, mszPrefix1);
3033-
if (bPrefixNum)
3034-
{
3035-
char tchFmt[64];
3036-
char tchNum[64];
3037-
wsprintfA(tchFmt, "%%%s%ii", pszPrefixNumPad, iPrefixNumWidth);
3038-
wsprintfA(tchNum, tchFmt, iPrefixNum);
3039-
lstrcatA(mszInsert, tchNum);
3040-
lstrcatA(mszInsert, mszPrefix2);
3041-
iPrefixNum++;
3042-
}
2897+
2898+
// [2e]: Alt+M - replace all substitutions #271
2899+
n2e_FormatLineText(mszInsert, iLineStart, iLine,
2900+
chPrefixAbsFormat, chPrefixAbsZeroFormat,
2901+
chPrefixRelFormat, chPrefixRelZeroFormat,
2902+
chPrefixRel0Format, chPrefixRel0ZeroFormat);
2903+
30432904
iPos = (int)SendMessage(hwnd, SCI_POSITIONFROMLINE, (WPARAM)iLine, 0);
30442905
SendMessage(hwnd, SCI_SETTARGETSTART, (WPARAM)iPos, 0);
30452906
SendMessage(hwnd, SCI_SETTARGETEND, (WPARAM)iPos, 0);
@@ -3048,19 +2909,15 @@ void EditModifyLines(HWND hwnd, LPCWSTR pwszPrefix, LPCWSTR pwszAppend)
30482909

30492910
if (lstrlen(pwszAppend))
30502911
{
3051-
30522912
char mszInsert[512 * 3];
30532913
lstrcpyA(mszInsert, mszAppend1);
3054-
if (bAppendNum)
3055-
{
3056-
char tchFmt[64];
3057-
char tchNum[64];
3058-
wsprintfA(tchFmt, "%%%s%ii", pszAppendNumPad, iAppendNumWidth);
3059-
wsprintfA(tchNum, tchFmt, iAppendNum);
3060-
lstrcatA(mszInsert, tchNum);
3061-
lstrcatA(mszInsert, mszAppend2);
3062-
iAppendNum++;
3063-
}
2914+
2915+
// [2e]: Alt+M - replace all substitutions #271
2916+
n2e_FormatLineText(mszInsert, iLineStart, iLine,
2917+
chPrefixAbsFormat, chPrefixAbsZeroFormat,
2918+
chPrefixRelFormat, chPrefixRelZeroFormat,
2919+
chPrefixRel0Format, chPrefixRel0ZeroFormat);
2920+
30642921
iPos = (int)SendMessage(hwnd, SCI_GETLINEENDPOSITION, (WPARAM)iLine, 0);
30652922
SendMessage(hwnd, SCI_SETTARGETSTART, (WPARAM)iPos, 0);
30662923
SendMessage(hwnd, SCI_SETTARGETEND, (WPARAM)iPos, 0);

src/Extension/EditHelper.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cassert>
33
#include "CommonUtils.h"
44
#include "Dialogs.h"
5+
#include "EditHelperEx.h"
56
#include "Helpers.h"
67
#include "Edit.h"
78
#include "ExtSelection.h"
@@ -1423,3 +1424,17 @@ BOOL n2e_IsFindReplaceAvailable(LPCEDITFINDREPLACE lpefr)
14231424
return res;
14241425
#endif
14251426
}
1427+
1428+
LPCSTR n2e_FormatLineText(LPSTR buf, const int iLineStart, const int iLineIndex,
1429+
LPCSTR lpPrefixAbsFormat, LPCSTR lpPrefixAbsZeroFormat,
1430+
LPCSTR lpPrefixRelFormat, LPCSTR lpPrefixRelZeroFormat,
1431+
LPCSTR lpPrefixRel0Format, LPCSTR lpPrefixRel0ZeroFormat)
1432+
{
1433+
n2e_ReplaceSubstringFormat(&buf[0], "$(L)", lpPrefixAbsFormat, iLineIndex + 1);
1434+
n2e_ReplaceSubstringFormat(&buf[0], "$(0L)", lpPrefixAbsZeroFormat, iLineIndex + 1);
1435+
n2e_ReplaceSubstringFormat(&buf[0], "$(N)", lpPrefixRelFormat, iLineIndex - iLineStart + 1);
1436+
n2e_ReplaceSubstringFormat(&buf[0], "$(0N)", lpPrefixRelZeroFormat, iLineIndex - iLineStart + 1);
1437+
n2e_ReplaceSubstringFormat(&buf[0], "$(I)", lpPrefixRel0Format, iLineIndex - iLineStart);
1438+
n2e_ReplaceSubstringFormat(&buf[0], "$(0I)", lpPrefixRel0ZeroFormat, iLineIndex - iLineStart);
1439+
return buf;
1440+
}

src/Extension/EditHelper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ BOOL n2e_IsExpressionEvaluationEnabled();
6565
int n2e_GetExpressionTextRange(int* piStart, int* piEnd);
6666

6767
BOOL n2e_IsFindReplaceAvailable(LPCEDITFINDREPLACE lpefr);
68+
69+
LPCSTR n2e_FormatLineText(LPCSTR buf, const int iLineStart, const int iLineIndex,
70+
LPCSTR lpPrefixAbsFormat, LPCSTR lpPrefixAbsZeroFormat,
71+
LPCSTR lpPrefixRelFormat, LPCSTR lpPrefixRelZeroFormat,
72+
LPCSTR lpPrefixRel0Format, LPCSTR lpPrefixRel0ZeroFormat);

src/Extension/EditHelperEx.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,30 @@ extern "C"
187187
{
188188
return (int)Scintilla::UTF8CharLength(ch);
189189
}
190+
191+
void n2e_ReplaceSubstring(LPSTR buf, LPCSTR from, LPCSTR to)
192+
{
193+
std::string str(buf);
194+
const std::string strFrom(from);
195+
const std::string strTo(to);
196+
size_t index = 0;
197+
while (1)
198+
{
199+
index = str.find(strFrom, index);
200+
if (index == std::string::npos)
201+
{
202+
break;
203+
}
204+
str.replace(index, strFrom.length(), strTo);
205+
index += std::min<int>(strFrom.length(), strTo.length());
206+
}
207+
lstrcpyA(buf, str.c_str());
208+
}
209+
210+
void n2e_ReplaceSubstringFormat(LPSTR buf, LPCSTR from, LPCSTR formatTo, const int value)
211+
{
212+
char to[10];
213+
wsprintfA(to, formatTo, value);
214+
n2e_ReplaceSubstring(buf, from, to);
215+
}
190216
}

src/Extension/EditHelperEx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
BOOL n2e_ExplorerCxtMenu(LPCWSTR path, const HWND hwndParent);
99
int n2e_isValidRegex(LPCSTR str);
1010
int n2e_GetUTF8CharLength(const unsigned char ch);
11+
void n2e_ReplaceSubstring(LPSTR buf, LPCSTR from, LPCSTR to);
12+
void n2e_ReplaceSubstringFormat(LPSTR buf, LPCSTR from, LPCSTR formatTo, const int value);
1113

1214
#ifdef __cplusplus
1315
}//end extern "C"

0 commit comments

Comments
 (0)