Skip to content

Commit 8da7a58

Browse files
committed
add commands to mark the selected text throughout the document
closes #395
1 parent 205b8d1 commit 8da7a58

File tree

7 files changed

+240
-37
lines changed

7 files changed

+240
-37
lines changed

src/Commands/CmdFindReplace.cpp

Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <regex>
3434
#include <thread>
35+
#include <array>
3536
#include <algorithm>
3637
#include <utility>
3738
#include <memory>
@@ -43,6 +44,8 @@
4344

4445
static std::string g_findString;
4546
std::string g_sHighlightString;
47+
std::array<std::string, 4> g_sCustomHighlightStrings;
48+
std::array<std::string, 4> g_sLastCustomHighlightStrings;
4649
static Scintilla::FindOption g_searchFlags = Scintilla::FindOption::None;
4750
sptr_t g_searchMarkerCount = 0;
4851
static std::string g_lastSelText;
@@ -2898,43 +2901,86 @@ void CCmdFindReplace::ScintillaNotify(SCNotification* pScn)
28982901
g_lastSelText.clear();
28992902
g_searchMarkerCount = 0;
29002903
DocScrollClear(DOCSCROLLTYPE_SEARCHTEXT);
2901-
return;
29022904
}
2903-
2904-
Sci_TextToFind findText = {0};
2905-
findText.chrg.cpMin = static_cast<Sci_PositionCR>(startStylePos);
2906-
findText.chrg.cpMax = static_cast<Sci_PositionCR>(endStylePos);
2907-
findText.lpstrText = g_sHighlightString.c_str();
2908-
while (Scintilla().FindText(g_searchFlags, &findText) >= 0)
2909-
{
2910-
Scintilla().IndicatorFillRange(findText.chrgText.cpMin, findText.chrgText.cpMax - findText.chrgText.cpMin);
2911-
if (findText.chrg.cpMin >= findText.chrgText.cpMax)
2912-
break;
2913-
findText.chrg.cpMin = findText.chrgText.cpMax;
2914-
}
2915-
2916-
if (g_lastSelText.empty() || g_lastSelText.compare(g_sHighlightString) || (g_searchFlags != g_lastSearchFlags))
2905+
else
29172906
{
2918-
DocScrollClear(DOCSCROLLTYPE_SEARCHTEXT);
2919-
g_searchMarkerCount = 0;
2920-
findText.chrg.cpMin = 0;
2921-
findText.chrg.cpMax = static_cast<Sci_PositionCR>(Scintilla().Length());
2922-
findText.lpstrText = g_sHighlightString.c_str();
2907+
Sci_TextToFind findText = {0};
2908+
findText.chrg.cpMin = static_cast<Sci_PositionCR>(startStylePos);
2909+
findText.chrg.cpMax = static_cast<Sci_PositionCR>(endStylePos);
2910+
findText.lpstrText = g_sHighlightString.c_str();
29232911
while (Scintilla().FindText(g_searchFlags, &findText) >= 0)
29242912
{
2925-
size_t line = Scintilla().LineFromPosition(findText.chrgText.cpMin);
2926-
DocScrollAddLineColor(DOCSCROLLTYPE_SEARCHTEXT, line, RGB(200, 200, 0));
2927-
++g_searchMarkerCount;
2913+
Scintilla().IndicatorFillRange(findText.chrgText.cpMin, findText.chrgText.cpMax - findText.chrgText.cpMin);
29282914
if (findText.chrg.cpMin >= findText.chrgText.cpMax)
29292915
break;
29302916
findText.chrg.cpMin = findText.chrgText.cpMax;
29312917
}
2918+
2919+
if (g_lastSelText.empty() || g_lastSelText.compare(g_sHighlightString) || (g_searchFlags != g_lastSearchFlags))
2920+
{
2921+
DocScrollClear(DOCSCROLLTYPE_SEARCHTEXT);
2922+
g_searchMarkerCount = 0;
2923+
findText.chrg.cpMin = 0;
2924+
findText.chrg.cpMax = static_cast<Sci_PositionCR>(Scintilla().Length());
2925+
findText.lpstrText = g_sHighlightString.c_str();
2926+
while (Scintilla().FindText(g_searchFlags, &findText) >= 0)
2927+
{
2928+
size_t line = Scintilla().LineFromPosition(findText.chrgText.cpMin);
2929+
DocScrollAddLineColor(DOCSCROLLTYPE_SEARCHTEXT, line, RGB(200, 200, 0));
2930+
++g_searchMarkerCount;
2931+
if (findText.chrg.cpMin >= findText.chrgText.cpMax)
2932+
break;
2933+
findText.chrg.cpMin = findText.chrgText.cpMax;
2934+
}
2935+
g_lastSelText = g_sHighlightString;
2936+
g_lastSearchFlags = g_searchFlags;
2937+
DocScrollUpdate();
2938+
}
29322939
g_lastSelText = g_sHighlightString;
29332940
g_lastSearchFlags = g_searchFlags;
2934-
DocScrollUpdate();
29352941
}
2936-
g_lastSelText = g_sHighlightString;
2937-
g_lastSearchFlags = g_searchFlags;
2942+
for (uptr_t i = INDIC_CUSTOM_MARK_1; i <= INDIC_CUSTOM_MARK_4; ++i)
2943+
{
2944+
Scintilla().SetIndicatorCurrent(i);
2945+
Scintilla().IndicatorClearRange(startStylePos, len);
2946+
Scintilla().IndicatorClearRange(startStylePos, len - 1);
2947+
2948+
auto sHighlightString = g_sCustomHighlightStrings[i - INDIC_CUSTOM_MARK_1];
2949+
auto sLastHighlightString = g_sLastCustomHighlightStrings[i - INDIC_CUSTOM_MARK_1];
2950+
Sci_TextToFind findText = {0};
2951+
findText.chrg.cpMin = static_cast<Sci_PositionCR>(startStylePos);
2952+
findText.chrg.cpMax = static_cast<Sci_PositionCR>(endStylePos);
2953+
findText.lpstrText = sHighlightString.c_str();
2954+
while (Scintilla().FindText(Scintilla::FindOption::None, &findText) >= 0)
2955+
{
2956+
Scintilla().IndicatorFillRange(findText.chrgText.cpMin, findText.chrgText.cpMax - findText.chrgText.cpMin);
2957+
if (findText.chrg.cpMin >= findText.chrgText.cpMax)
2958+
break;
2959+
findText.chrg.cpMin = findText.chrgText.cpMax;
2960+
}
2961+
2962+
if (sLastHighlightString.empty() || sLastHighlightString.compare(sHighlightString))
2963+
{
2964+
auto docScrollType = DOCSCROLLTYPE_CUSTOMMARK_1 + (i - INDIC_CUSTOM_MARK_1);
2965+
DocScrollClear(docScrollType);
2966+
if (sHighlightString.empty())
2967+
continue;
2968+
findText.chrg.cpMin = 0;
2969+
findText.chrg.cpMax = static_cast<Sci_PositionCR>(Scintilla().Length());
2970+
findText.lpstrText = sHighlightString.c_str();
2971+
while (Scintilla().FindText(Scintilla::FindOption::None, &findText) >= 0)
2972+
{
2973+
size_t line = Scintilla().LineFromPosition(findText.chrgText.cpMin);
2974+
DocScrollAddLineColor(docScrollType, line,
2975+
CTheme::Instance().GetThemeColor(customMarkColors[i - INDIC_CUSTOM_MARK_1]));
2976+
if (findText.chrg.cpMin >= findText.chrgText.cpMax)
2977+
break;
2978+
findText.chrg.cpMin = findText.chrgText.cpMax;
2979+
}
2980+
g_sLastCustomHighlightStrings[i - INDIC_CUSTOM_MARK_1] = sHighlightString;
2981+
DocScrollUpdate();
2982+
}
2983+
}
29382984
}
29392985
}
29402986

@@ -2943,6 +2989,8 @@ void CCmdFindReplace::TabNotify(TBHDR* ptbHdr)
29432989
if (ptbHdr->hdr.code == TCN_SELCHANGE)
29442990
{
29452991
g_lastSelText.clear();
2992+
for (size_t i = 0; i < g_sLastCustomHighlightStrings.size(); ++i)
2993+
g_sLastCustomHighlightStrings[i].clear();
29462994
if (g_pFindReplaceDlg != nullptr)
29472995
{
29482996
bool followTab = IsDlgButtonChecked(*g_pFindReplaceDlg, IDC_SEARCHFOLDERFOLLOWTAB) == BST_CHECKED;
@@ -3139,6 +3187,45 @@ bool CCmdFindFile::Execute()
31393187
return true;
31403188
}
31413189

3190+
bool CCmdCustomMark1::Execute()
3191+
{
3192+
g_sCustomHighlightStrings[0] = GetSelectedText(SelectionHandling::None);
3193+
g_sLastCustomHighlightStrings[0].clear();
3194+
DocScrollUpdate();
3195+
return true;
3196+
}
3197+
bool CCmdCustomMark2::Execute()
3198+
{
3199+
g_sCustomHighlightStrings[1] = GetSelectedText(SelectionHandling::None);
3200+
g_sLastCustomHighlightStrings[1].clear();
3201+
DocScrollUpdate();
3202+
return true;
3203+
}
3204+
bool CCmdCustomMark3::Execute()
3205+
{
3206+
g_sCustomHighlightStrings[2] = GetSelectedText(SelectionHandling::None);
3207+
g_sLastCustomHighlightStrings[2].clear();
3208+
DocScrollUpdate();
3209+
return true;
3210+
}
3211+
bool CCmdCustomMark4::Execute()
3212+
{
3213+
g_sCustomHighlightStrings[3] = GetSelectedText(SelectionHandling::None);
3214+
g_sLastCustomHighlightStrings[3].clear();
3215+
DocScrollUpdate();
3216+
return true;
3217+
}
3218+
bool CCmdCustomMarkClearAll::Execute()
3219+
{
3220+
for (size_t i = 0; i < g_sCustomHighlightStrings.size(); ++i)
3221+
{
3222+
g_sCustomHighlightStrings[i].clear();
3223+
g_sLastCustomHighlightStrings[i].clear();
3224+
}
3225+
DocScrollUpdate();
3226+
return true;
3227+
}
3228+
31423229
void findReplaceFinish()
31433230
{
31443231
g_pFindReplaceDlg.reset();

src/Commands/CmdFindReplace.h

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of BowPad.
22
//
3-
// Copyright (C) 2013-2017, 2019-2023 - Stefan Kueng
3+
// Copyright (C) 2013-2017, 2019-2024 - Stefan Kueng
44
//
55
// This program is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU General Public License as published by
@@ -338,3 +338,79 @@ class CCmdFindFile : public ICommand
338338

339339
UINT GetCmdId() override { return cmdFindFile; }
340340
};
341+
342+
/////////////////////////////////////////////
343+
class CCmdCustomMark1 : public ICommand
344+
{
345+
public:
346+
CCmdCustomMark1(void* obj)
347+
: ICommand(obj)
348+
{
349+
}
350+
351+
~CCmdCustomMark1() override = default;
352+
353+
bool Execute() override;
354+
355+
UINT GetCmdId() override { return cmdCustomMark1; }
356+
};
357+
358+
class CCmdCustomMark2 : public ICommand
359+
{
360+
public:
361+
CCmdCustomMark2(void* obj)
362+
: ICommand(obj)
363+
{
364+
}
365+
366+
~CCmdCustomMark2() override = default;
367+
368+
bool Execute() override;
369+
370+
UINT GetCmdId() override { return cmdCustomMark2; }
371+
};
372+
373+
class CCmdCustomMark3 : public ICommand
374+
{
375+
public:
376+
CCmdCustomMark3(void* obj)
377+
: ICommand(obj)
378+
{
379+
}
380+
381+
~CCmdCustomMark3() override = default;
382+
383+
bool Execute() override;
384+
385+
UINT GetCmdId() override { return cmdCustomMark3; }
386+
};
387+
388+
class CCmdCustomMark4 : public ICommand
389+
{
390+
public:
391+
CCmdCustomMark4(void* obj)
392+
: ICommand(obj)
393+
{
394+
}
395+
396+
~CCmdCustomMark4() override = default;
397+
398+
bool Execute() override;
399+
400+
UINT GetCmdId() override { return cmdCustomMark4; }
401+
};
402+
403+
class CCmdCustomMarkClearAll : public ICommand
404+
{
405+
public:
406+
CCmdCustomMarkClearAll(void* obj)
407+
: ICommand(obj)
408+
{
409+
}
410+
411+
~CCmdCustomMarkClearAll() override = default;
412+
413+
bool Execute() override;
414+
415+
UINT GetCmdId() override { return cmdCustomMarkClearAll; }
416+
};

src/Commands/CommandHandler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ void CCommandHandler::Init(void* obj)
171171
Add<CCmdGotoLine>(obj);
172172
Add<CCmdGotoSymbol>(obj);
173173
Add<CCmdRegexCapture>(obj);
174+
Add<CCmdCustomMark1>(obj);
175+
Add<CCmdCustomMark2>(obj);
176+
Add<CCmdCustomMark3>(obj);
177+
Add<CCmdCustomMark4>(obj);
178+
Add<CCmdCustomMarkClearAll>(obj);
174179

175180
Add<CCmdBookmarks>(obj);
176181
Add<CCmdBookmarkToggle>(obj);

src/DocScroll.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of BowPad.
22
//
3-
// Copyright (C) 2013-2014, 2016-2017, 2020-2021 - Stefan Kueng
3+
// Copyright (C) 2013-2014, 2016-2017, 2020-2021, 2024 - Stefan Kueng
44
//
55
// This program is free software: you can redistribute it and/or modify
66
// it under the terms of the GNU General Public License as published by
@@ -23,7 +23,11 @@
2323
constexpr int DOCSCROLLTYPE_SELTEXT = 1;
2424
constexpr int DOCSCROLLTYPE_BOOKMARK = 2;
2525
constexpr int DOCSCROLLTYPE_SEARCHTEXT = 3;
26-
constexpr int DOCSCROLLTYPE_END = 4;
26+
constexpr int DOCSCROLLTYPE_CUSTOMMARK_1 = 4;
27+
constexpr int DOCSCROLLTYPE_CUSTOMMARK_2 = 5;
28+
constexpr int DOCSCROLLTYPE_CUSTOMMARK_3 = 6;
29+
constexpr int DOCSCROLLTYPE_CUSTOMMARK_4 = 7;
30+
constexpr int DOCSCROLLTYPE_END = 8;
2731

2832
struct LineColor
2933
{

src/ScintillaWnd.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,14 @@ void CScintillaWnd::SetupDefaultStyles() const
12441244
m_scintilla.IndicSetFore(i, CTheme::Instance().GetThemeColor(captureColors[i - INDIC_REGEXCAPTURE]));
12451245
}
12461246

1247+
for (uptr_t i = INDIC_CUSTOM_MARK_1; i <= INDIC_CUSTOM_MARK_4; ++i)
1248+
{
1249+
m_scintilla.IndicSetStyle(i, Scintilla::IndicatorStyle::RoundBox);
1250+
m_scintilla.IndicSetAlpha(i, theme.IsDarkTheme() ? static_cast<Scintilla::Alpha>(100) : static_cast<Scintilla::Alpha>(200));
1251+
m_scintilla.IndicSetUnder(i, true);
1252+
m_scintilla.IndicSetFore(i, theme.GetThemeColor(customMarkColors[i - INDIC_CUSTOM_MARK_1], true));
1253+
}
1254+
12471255
m_scintilla.SetFoldMarginColour(true, theme.GetThemeColor(RGB(240, 240, 240), true));
12481256
m_scintilla.SetFoldMarginHiColour(true, theme.GetThemeColor(RGB(255, 255, 255), true));
12491257

src/ScintillaWnd.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
#include "../ext/scintilla/include/ScintillaTypes.h"
2626
#include "../ext/scintilla/include/ScintillaCall.h"
2727

28+
#include <array>
2829
#include <vector>
29-
#include <unordered_map>
3030

3131
class CPosData;
3232

@@ -41,15 +41,20 @@ class CPosData;
4141
#define INDIC_SNIPPETPOS (INDIC_CONTAINER + 9)
4242
#define INDIC_REGEXCAPTURE (INDIC_CONTAINER + 10)
4343
#define INDIC_REGEXCAPTURE_END (INDIC_CONTAINER + 20)
44+
#define INDIC_CUSTOM_MARK_1 (INDIC_CONTAINER + 21)
45+
#define INDIC_CUSTOM_MARK_2 (INDIC_CONTAINER + 22)
46+
#define INDIC_CUSTOM_MARK_3 (INDIC_CONTAINER + 23)
47+
#define INDIC_CUSTOM_MARK_4 (INDIC_CONTAINER + 24)
48+
constexpr std::array customMarkColors = {RGB(180, 128, 128), RGB(128, 180, 128), RGB(128, 128, 180), RGB(180, 180, 128)};
4449

45-
constexpr int SC_MARGE_LINENUMBER = 0;
46-
constexpr int SC_MARGE_HISTORY = 1;
47-
constexpr int SC_MARGE_SYMBOL = 2;
48-
constexpr int SC_MARGE_FOLDER = 4;
50+
constexpr int SC_MARGE_LINENUMBER = 0;
51+
constexpr int SC_MARGE_HISTORY = 1;
52+
constexpr int SC_MARGE_SYMBOL = 2;
53+
constexpr int SC_MARGE_FOLDER = 4;
4954

50-
constexpr int MARK_BOOKMARK = 20;
55+
constexpr int MARK_BOOKMARK = 20;
5156

52-
constexpr int SCN_BP_MOUSEMSG = 4000;
57+
constexpr int SCN_BP_MOUSEMSG = 4000;
5358

5459
enum class BraceMatch
5560
{

src/res/BowPad.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,14 @@ Hold down the Shift key to show EOLs as well." Keytip="WS">
842842
</Command.LargeImages>
843843
</Command>
844844

845-
<Command Name="cmdContextMap" />
845+
<Command Name="cmdCustomMark1" LabelTitle="with style 1"/>
846+
<Command Name="cmdCustomMark2" LabelTitle="with style 2"/>
847+
<Command Name="cmdCustomMark3" LabelTitle="with style 3"/>
848+
<Command Name="cmdCustomMark4" LabelTitle="with style 4"/>
849+
<Command Name="cmdCustomMarkClearAll" LabelTitle="Clear all styles"/>
850+
<Command Name="cmdMarkGroup" LabelTitle="Style selection"/>
851+
852+
<Command Name="cmdContextMap" />
846853
<Command Name="cmdContextSpellMap" />
847854
<Command Name="cmdTabContextMap" />
848855
<Command Name="cmdSelectTab" Id="10000" />
@@ -1494,6 +1501,17 @@ Hold down the Shift key to show EOLs as well." Keytip="WS">
14941501
<MenuGroup>
14951502
<Button CommandName="cmdOpenSelection" />
14961503
</MenuGroup>
1504+
<MenuGroup>
1505+
<DropDownButton CommandName="cmdMarkGroup">
1506+
<MenuGroup>
1507+
<Button CommandName="cmdCustomMark1"/>
1508+
<Button CommandName="cmdCustomMark2"/>
1509+
<Button CommandName="cmdCustomMark3"/>
1510+
<Button CommandName="cmdCustomMark4"/>
1511+
<Button CommandName="cmdCustomMarkClearAll"/>
1512+
</MenuGroup>
1513+
</DropDownButton>
1514+
</MenuGroup>
14971515
<MenuGroup>
14981516
<DropDownButton CommandName="cmdFoldGroup">
14991517
<MenuGroup>

0 commit comments

Comments
 (0)