diff --git a/Lib/test/test_tkinter/test_text.py b/Lib/test/test_tkinter/test_text.py index b26956930d3402..f610a3a58990ab 100644 --- a/Lib/test/test_tkinter/test_text.py +++ b/Lib/test/test_tkinter/test_text.py @@ -94,6 +94,34 @@ def test_count(self): self.assertEqual(text.count('1.3', '1.3', 'update', return_ints=True), 0) self.assertEqual(text.count('1.3', '1.3', 'update'), None) +class TextSearchOptionsTest(AbstractTkTest, unittest.TestCase): + def setUp(self): + super().setUp() + self.text = tkinter.Text(self.root) + self.text.pack() + self.text.insert('1.0', + 'This is a test. This is only a test.\n' + 'Another line.\nYet another line.') + + def test_nolinestop(self): + result = self.text.search('line', '1.0', 'end', nolinestop=True, regexp=True) + self.assertEqual(result, '2.8') + + def test_all(self): + result = self.text.search('test', '1.0', 'end', all=True) + self.assertIsInstance(result, str) + indices = result.split() + self.assertGreaterEqual(len(indices), 2) + self.assertTrue(all(isinstance(i, str) for i in indices)) + + def test_overlap(self): + result = self.text.search('test', '1.0', 'end', all=True, overlap=True) + self.assertIsInstance(result, str) + self.assertIn("textindex", result) + + def test_strictlimits(self): + result = self.text.search('test', '1.0', '1.20', strictlimits=True) + self.assertEqual(result, '1.10') if __name__ == "__main__": unittest.main() diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index a693b04870b995..28961aa45d8711 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -4039,8 +4039,10 @@ 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, all=None, + overlap=None, strictlimits=None): """Search PATTERN beginning from INDEX until STOPINDEX. Return the index of the first character of a match or an empty string.""" @@ -4052,6 +4054,10 @@ 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 all: args.append('-all') + if overlap: args.append('-overlap') + if strictlimits: args.append('-strictlimits') if pattern and pattern[0] == '-': args.append('--') args.append(pattern) args.append(index) diff --git a/Misc/NEWS.d/next/Library/2025-03-04-17-19-26.gh-issue-130693.Kv01r8.rst b/Misc/NEWS.d/next/Library/2025-03-04-17-19-26.gh-issue-130693.Kv01r8.rst new file mode 100644 index 00000000000000..1444be954c4807 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-04-17-19-26.gh-issue-130693.Kv01r8.rst @@ -0,0 +1 @@ +Add support for ``-nolinestop``, ``-all``, ``-overlap``, and ``-strictlimits`` options to ``!tkinter.Text.search``