Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Lib/idlelib/search.py at main · python/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.4k
Star
77.2k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
cpython
/
Lib
/
idlelib
/
search.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
165 lines (134 loc) · 5.44 KB
main
Breadcrumbs
cpython
/
Lib
/
idlelib
/
search.py
Copy path
Top
File metadata and controls
Code
Blame
165 lines (134 loc) · 5.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Search dialog for Find, Find Again, and Find Selection
functionality.
Inherits from SearchDialogBase for GUI and uses searchengine
to prepare search pattern.
"""
from
tkinter
import
TclError
from
idlelib
import
searchengine
from
idlelib
.
searchbase
import
SearchDialogBase
def
_setup
(
text
):
"""Return the new or existing singleton SearchDialog instance.
The singleton dialog saves user entries and preferences
across instances.
Args:
text: Text widget containing the text to be searched.
"""
root
=
text
.
_root
()
engine
=
searchengine
.
get
(
root
)
if
not
hasattr
(
engine
,
"_searchdialog"
):
engine
.
_searchdialog
=
SearchDialog
(
root
,
engine
)
return
engine
.
_searchdialog
def
find
(
text
):
"""Open the search dialog.
Module-level function to access the singleton SearchDialog
instance and open the dialog. If text is selected, it is
used as the search phrase; otherwise, the previous entry
is used. No search is done with this command.
"""
pat
=
text
.
get
(
"sel.first"
,
"sel.last"
)
return
_setup
(
text
).
open
(
text
,
pat
)
# Open is inherited from SDBase.
def
find_again
(
text
):
"""Repeat the search for the last pattern and preferences.
Module-level function to access the singleton SearchDialog
instance to search again using the user entries and preferences
from the last dialog. If there was no prior search, open the
search dialog; otherwise, perform the search without showing the
dialog.
"""
return
_setup
(
text
).
find_again
(
text
)
def
find_selection
(
text
):
"""Search for the selected pattern in the text.
Module-level function to access the singleton SearchDialog
instance to search using the selected text. With a text
selection, perform the search without displaying the dialog.
Without a selection, use the prior entry as the search phrase
and don't display the dialog. If there has been no prior
search, open the search dialog.
"""
return
_setup
(
text
).
find_selection
(
text
)
class
SearchDialog
(
SearchDialogBase
):
"Dialog for finding a pattern in text."
def
create_widgets
(
self
):
"Create the base search dialog and add a button for Find Next."
SearchDialogBase
.
create_widgets
(
self
)
# TODO - why is this here and not in a create_command_buttons?
self
.
make_button
(
"Find Next"
,
self
.
default_command
,
isdef
=
True
)
def
default_command
(
self
,
event
=
None
):
"Handle the Find Next button as the default command."
if
not
self
.
engine
.
getprog
():
return
self
.
find_again
(
self
.
text
)
def
find_again
(
self
,
text
):
"""Repeat the last search.
If no search was previously run, open a new search dialog. In
this case, no search is done.
If a search was previously run, the search dialog won't be
shown and the options from the previous search (including the
search pattern) will be used to find the next occurrence
of the pattern. Next is relative based on direction.
Position the window to display the located occurrence in the
text.
Return True if the search was successful and False otherwise.
"""
if
not
self
.
engine
.
getpat
():
self
.
open
(
text
)
return
False
if
not
self
.
engine
.
getprog
():
return
False
res
=
self
.
engine
.
search_text
(
text
)
if
res
:
line
,
m
=
res
i
,
j
=
m
.
span
()
first
=
"%d.%d"
%
(
line
,
i
)
last
=
"%d.%d"
%
(
line
,
j
)
try
:
selfirst
=
text
.
index
(
"sel.first"
)
sellast
=
text
.
index
(
"sel.last"
)
if
selfirst
==
first
and
sellast
==
last
:
self
.
bell
()
return
False
except
TclError
:
pass
text
.
tag_remove
(
"sel"
,
"1.0"
,
"end"
)
text
.
tag_add
(
"sel"
,
first
,
last
)
text
.
mark_set
(
"insert"
,
self
.
engine
.
isback
()
and
first
or
last
)
text
.
see
(
"insert"
)
return
True
else
:
self
.
bell
()
return
False
def
find_selection
(
self
,
text
):
"""Search for selected text with previous dialog preferences.
Instead of using the same pattern for searching (as Find
Again does), this first resets the pattern to the currently
selected text. If the selected text isn't changed, then use
the prior search phrase.
"""
pat
=
text
.
get
(
"sel.first"
,
"sel.last"
)
if
pat
:
self
.
engine
.
setcookedpat
(
pat
)
return
self
.
find_again
(
text
)
def
_search_dialog
(
parent
):
# htest #
"Display search test box."
from
tkinter
import
Toplevel
,
Text
from
tkinter
.
ttk
import
Frame
,
Button
top
=
Toplevel
(
parent
)
top
.
title
(
"Test SearchDialog"
)
x
,
y
=
map
(
int
,
parent
.
geometry
().
split
(
'+'
)[
1
:])
top
.
geometry
(
"+%d+%d"
%
(
x
,
y
+
175
))
frame
=
Frame
(
top
)
frame
.
pack
()
text
=
Text
(
frame
,
inactiveselectbackground
=
'gray'
)
text
.
pack
()
text
.
insert
(
"insert"
,
"This is a sample string.
\n
"
*
5
)
def
show_find
():
text
.
tag_add
(
'sel'
,
'1.0'
,
'end'
)
_setup
(
text
).
open
(
text
)
text
.
tag_remove
(
'sel'
,
'1.0'
,
'end'
)
button
=
Button
(
frame
,
text
=
"Search (selection ignored)"
,
command
=
show_find
)
button
.
pack
()
if
__name__
==
'__main__'
:
from
unittest
import
main
main
(
'idlelib.idle_test.test_search'
,
verbosity
=
2
,
exit
=
False
)
from
idlelib
.
idle_test
.
htest
import
run
run
(
_search_dialog
)
You can’t perform that action at this time.