gh-130693: Support more options for search in tkinter.Text (GH-130848)

* Add parameters nolinestop and strictlimits in the tkinter.Text.search() method.
* Add the tkinter.Text.search_all() method.
* Add more tests for tkinter.Text.search().
* stopindex is now only ignored if it is None.
This commit is contained in:
R.C.M 2025-11-17 09:42:26 -05:00 committed by GitHub
parent f6dd9c12a8
commit 3d14805947
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 154 additions and 8 deletions

View file

@ -4049,8 +4049,9 @@ def scan_dragto(self, x, y):
self.tk.call(self._w, 'scan', 'dragto', x, y)
def search(self, pattern, index, stopindex=None,
forwards=None, backwards=None, exact=None,
regexp=None, nocase=None, count=None, elide=None):
forwards=None, backwards=None, exact=None,
regexp=None, nocase=None, count=None,
elide=None, *, nolinestop=None, strictlimits=None):
"""Search PATTERN beginning from INDEX until STOPINDEX.
Return the index of the first character of a match or an
empty string."""
@ -4062,12 +4063,39 @@ def search(self, pattern, index, stopindex=None,
if nocase: args.append('-nocase')
if elide: args.append('-elide')
if count: args.append('-count'); args.append(count)
if nolinestop: args.append('-nolinestop')
if strictlimits: args.append('-strictlimits')
if pattern and pattern[0] == '-': args.append('--')
args.append(pattern)
args.append(index)
if stopindex: args.append(stopindex)
if stopindex is not None: args.append(stopindex)
return str(self.tk.call(tuple(args)))
def search_all(self, pattern, index, stopindex=None, *,
forwards=None, backwards=None, exact=None,
regexp=None, nocase=None, count=None,
elide=None, nolinestop=None, overlap=None,
strictlimits=None):
"""Search all occurrences of PATTERN from INDEX to STOPINDEX.
Return a tuple of indices where matches begin."""
args = [self._w, 'search', '-all']
if forwards: args.append('-forwards')
if backwards: args.append('-backwards')
if exact: args.append('-exact')
if regexp: args.append('-regexp')
if nocase: args.append('-nocase')
if elide: args.append('-elide')
if count: args.append('-count'); args.append(count)
if nolinestop: args.append('-nolinestop')
if overlap: args.append('-overlap')
if strictlimits: args.append('-strictlimits')
if pattern and pattern[0] == '-': args.append('--')
args.append(pattern)
args.append(index)
if stopindex is not None: args.append(stopindex)
result = self.tk.call(tuple(args))
return self.tk.splitlist(result)
def see(self, index):
"""Scroll such that the character at INDEX is visible."""
self.tk.call(self._w, 'see', index)