Skip to content

Commit

Permalink
PoC of showing all matches during a "find"
Browse files Browse the repository at this point in the history
  • Loading branch information
tangledhelix committed Jan 5, 2025
1 parent a78562f commit d1db679
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/guiguts/maintext.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ class HighlightTag(StrEnum):
ALIGNCOL = auto()
CURSOR_LINE = auto()
COLUMN_RULER = auto()
SEARCH = auto()


class HighlightColors:
Expand Down Expand Up @@ -350,6 +351,21 @@ class HighlightColors:
"Dark": {"bg": "#324F78", "fg": "white"},
}

SEARCH = {
"Light": {
"bg": "#f0f0f0",
"fg": "#a8a8a8",
"relief": "ridge",
"borderwidth": 2,
},
"Dark": {
"bg": "#0f0f0f",
"fg": "#8a8a8a",
"relief": "ridge",
"borderwidth": 2,
},
}


class TextPeer(tk.Text):
"""A peer of maintext's text widget.
Expand Down Expand Up @@ -2834,6 +2850,12 @@ def _highlight_configure_tag(
background=tag_colors[theme]["bg"],
foreground=tag_colors[theme]["fg"],
)
if "relief" in tag_colors[theme] and "borderwidth" in tag_colors[theme]:
self.tag_configure(
tag_name,
relief=tag_colors[theme]["relief"],
borderwidth=tag_colors[theme]["borderwidth"],
)

def highlight_selection(
self,
Expand Down Expand Up @@ -2861,8 +2883,13 @@ def highlight_selection(
tag_name, match.rowcol.index(), match.rowcol.index() + "+1c"
)

def remove_search_highlights(self) -> None:
"""Remove highlights for search"""
self.tag_remove(HighlightTag.SEARCH, "1.0", tk.END)

def remove_highlights(self) -> None:
"""Remove active highlights."""
self.remove_search_highlights()
self.tag_remove(HighlightTag.QUOTEMARK, "1.0", tk.END)

def highlight_quotemarks(self, pat: str) -> None:
Expand Down Expand Up @@ -3239,6 +3266,7 @@ def highlight_configure_tags(self, first_run: bool = False) -> None:
(HighlightTag.QUOTEMARK, HighlightColors.QUOTEMARK),
# "sel" is for active selections - don't override the default color
("sel", None),
(HighlightTag.SEARCH, HighlightColors.SEARCH),
(HighlightTag.SPOTLIGHT, HighlightColors.SPOTLIGHT),
(HighlightTag.PAREN, HighlightColors.PAREN),
(HighlightTag.CURLY_BRACKET, HighlightColors.CURLY_BRACKET),
Expand Down
13 changes: 12 additions & 1 deletion src/guiguts/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import regex as re

from guiguts.checkers import CheckerDialog
from guiguts.maintext import maintext, TclRegexCompileError, FindMatch
from guiguts.maintext import maintext, TclRegexCompileError, FindMatch, HighlightTag
from guiguts.preferences import preferences, PersistentBoolean, PrefKey
from guiguts.utilities import sound_bell, IndexRowCol, IndexRange, sing_plur
from guiguts.widgets import (
Expand Down Expand Up @@ -292,6 +292,9 @@ def set_first_last() -> None:
self.config_width()
self.allow_geometry_save()

# Handle tag cleanup when search panel is closed
self.top_frame.bind("<Destroy>", lambda _event: maintext().remove_search_highlights())

def show_multi_replace(self, show: bool, resize: bool = True) -> None:
"""Show or hide the multi-replace buttons.
Expand Down Expand Up @@ -365,7 +368,15 @@ def search_clicked(
start_rowcol = get_search_start(backwards)
stop_rowcol = maintext().start() if backwards else maintext().end()
message = ""
maintext().remove_search_highlights()
try:
matchez = self.count_clicked()
for matchz in matchez:
maintext().tag_add(
HighlightTag.SEARCH,
matchz.rowcol.index(),
f"{matchz.rowcol.index()}+{matchz.count}c",
)
_do_find_next(
search_string, backwards, IndexRange(start_rowcol, stop_rowcol)
)
Expand Down

0 comments on commit d1db679

Please # to comment.