blob: d3d1e6982bfb2e976b9051bfad827e162dee67aa [file] [log] [blame]
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00001"""
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04002An auto-completion window for IDLE, used by the autocomplete extension
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00003"""
mlouielu778b4842017-06-14 23:13:19 +08004import platform
5
Georg Brandl14fc4272008-05-17 18:39:55 +00006from tkinter import *
Victor Stinner6900f162020-04-30 03:28:51 +02007from tkinter.ttk import Scrollbar
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -04008
Terry Jan Reedy12131232019-08-04 19:48:52 -04009from idlelib.autocomplete import FILES, ATTRS
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040010from idlelib.multicall import MC_SHIFT
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000011
12HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>"
mlouielu778b4842017-06-14 23:13:19 +080013HIDE_FOCUS_OUT_SEQUENCE = "<FocusOut>"
14HIDE_SEQUENCES = (HIDE_FOCUS_OUT_SEQUENCE, "<ButtonPress>")
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000015KEYPRESS_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keypress>>"
16# We need to bind event beyond <Key> so that the function will be called
17# before the default specific IDLE function
Thomas Wouterscf297e42007-02-23 15:07:44 +000018KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>", "<Key-Tab>",
19 "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>",
JohnnyNajera232689b2019-12-10 01:22:16 +020020 "<Key-Prior>", "<Key-Next>", "<Key-Escape>")
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000021KEYRELEASE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keyrelease>>"
22KEYRELEASE_SEQUENCE = "<KeyRelease>"
Thomas Wouterscf297e42007-02-23 15:07:44 +000023LISTUPDATE_SEQUENCE = "<B1-ButtonRelease>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000024WINCONFIG_SEQUENCE = "<Configure>"
Thomas Wouterscf297e42007-02-23 15:07:44 +000025DOUBLECLICK_SEQUENCE = "<B1-Double-ButtonRelease>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000026
27class AutoCompleteWindow:
28
Tal Einatb43cc312021-05-03 05:27:38 +030029 def __init__(self, widget, tags):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000030 # The widget (Text) on which we place the AutoCompleteWindow
31 self.widget = widget
Tal Einatb43cc312021-05-03 05:27:38 +030032 # Tags to mark inserted text with
33 self.tags = tags
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000034 # The widgets we create
35 self.autocompletewindow = self.listbox = self.scrollbar = None
36 # The default foreground and background of a selection. Saved because
37 # they are changed to the regular colors of list items when the
38 # completion start is not a prefix of the selected completion
39 self.origselforeground = self.origselbackground = None
40 # The list of completions
41 self.completions = None
42 # A list with more completions, or None
43 self.morecompletions = None
Terry Jan Reedy12131232019-08-04 19:48:52 -040044 # The completion mode, either autocomplete.ATTRS or .FILES.
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000045 self.mode = None
46 # The current completion start, on the text box (a string)
47 self.start = None
48 # The index of the start of the completion
49 self.startindex = None
50 # The last typed start, used so that when the selection changes,
51 # the new start will be as close as possible to the last typed one.
52 self.lasttypedstart = None
53 # Do we have an indication that the user wants the completion window
54 # (for example, he clicked the list)
55 self.userwantswindow = None
56 # event ids
Tal Einat71662dc2019-08-14 20:06:06 +030057 self.hideid = self.keypressid = self.listupdateid = \
58 self.winconfigid = self.keyreleaseid = self.doubleclickid = None
Thomas Wouterscf297e42007-02-23 15:07:44 +000059 # Flag set if last keypress was a tab
60 self.lastkey_was_tab = False
Tal Einat71662dc2019-08-14 20:06:06 +030061 # Flag set to avoid recursive <Configure> callback invocations.
62 self.is_configuring = False
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000063
64 def _change_start(self, newstart):
Christian Heimes81ee3ef2008-05-04 22:42:01 +000065 min_len = min(len(self.start), len(newstart))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000066 i = 0
Christian Heimes81ee3ef2008-05-04 22:42:01 +000067 while i < min_len and self.start[i] == newstart[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000068 i += 1
69 if i < len(self.start):
70 self.widget.delete("%s+%dc" % (self.startindex, i),
71 "%s+%dc" % (self.startindex, len(self.start)))
72 if i < len(newstart):
73 self.widget.insert("%s+%dc" % (self.startindex, i),
Tal Einatb43cc312021-05-03 05:27:38 +030074 newstart[i:],
75 self.tags)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000076 self.start = newstart
77
78 def _binary_search(self, s):
79 """Find the first index in self.completions where completions[i] is
Terry Jan Reedy12131232019-08-04 19:48:52 -040080 greater or equal to s, or the last index if there is no such.
81 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000082 i = 0; j = len(self.completions)
83 while j > i:
84 m = (i + j) // 2
85 if self.completions[m] >= s:
86 j = m
87 else:
88 i = m + 1
89 return min(i, len(self.completions)-1)
90
91 def _complete_string(self, s):
92 """Assuming that s is the prefix of a string in self.completions,
93 return the longest string which is a prefix of all the strings which
Terry Jan Reedy12131232019-08-04 19:48:52 -040094 s is a prefix of them. If s is not a prefix of a string, return s.
95 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000096 first = self._binary_search(s)
97 if self.completions[first][:len(s)] != s:
98 # There is not even one completion which s is a prefix of.
99 return s
100 # Find the end of the range of completions where s is a prefix of.
101 i = first + 1
102 j = len(self.completions)
103 while j > i:
104 m = (i + j) // 2
105 if self.completions[m][:len(s)] != s:
106 j = m
107 else:
108 i = m + 1
109 last = i-1
110
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000111 if first == last: # only one possible completion
112 return self.completions[first]
113
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000114 # We should return the maximum prefix of first and last
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000115 first_comp = self.completions[first]
116 last_comp = self.completions[last]
117 min_len = min(len(first_comp), len(last_comp))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000118 i = len(s)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000119 while i < min_len and first_comp[i] == last_comp[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000120 i += 1
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000121 return first_comp[:i]
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000122
123 def _selection_changed(self):
Terry Jan Reedy12131232019-08-04 19:48:52 -0400124 """Call when the selection of the Listbox has changed.
125
126 Updates the Listbox display and calls _change_start.
127 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000128 cursel = int(self.listbox.curselection()[0])
129
130 self.listbox.see(cursel)
131
132 lts = self.lasttypedstart
133 selstart = self.completions[cursel]
134 if self._binary_search(lts) == cursel:
135 newstart = lts
136 else:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000137 min_len = min(len(lts), len(selstart))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000138 i = 0
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000139 while i < min_len and lts[i] == selstart[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000140 i += 1
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000141 newstart = selstart[:i]
142 self._change_start(newstart)
143
144 if self.completions[cursel][:len(self.start)] == self.start:
145 # start is a prefix of the selected completion
146 self.listbox.configure(selectbackground=self.origselbackground,
147 selectforeground=self.origselforeground)
148 else:
149 self.listbox.configure(selectbackground=self.listbox.cget("bg"),
150 selectforeground=self.listbox.cget("fg"))
151 # If there are more completions, show them, and call me again.
152 if self.morecompletions:
153 self.completions = self.morecompletions
154 self.morecompletions = None
155 self.listbox.delete(0, END)
156 for item in self.completions:
157 self.listbox.insert(END, item)
158 self.listbox.select_set(self._binary_search(self.start))
159 self._selection_changed()
160
161 def show_window(self, comp_lists, index, complete, mode, userWantsWin):
162 """Show the autocomplete list, bind events.
Terry Jan Reedy12131232019-08-04 19:48:52 -0400163
164 If complete is True, complete the text, and if there is exactly
165 one matching completion, don't open a list.
166 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000167 # Handle the start we already have
168 self.completions, self.morecompletions = comp_lists
169 self.mode = mode
170 self.startindex = self.widget.index(index)
171 self.start = self.widget.get(self.startindex, "insert")
172 if complete:
173 completed = self._complete_string(self.start)
Serhiy Storchakadd4754e2013-09-11 22:46:27 +0300174 start = self.start
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000175 self._change_start(completed)
176 i = self._binary_search(completed)
177 if self.completions[i] == completed and \
178 (i == len(self.completions)-1 or
179 self.completions[i+1][:len(completed)] != completed):
180 # There is exactly one matching completion
Serhiy Storchakadd4754e2013-09-11 22:46:27 +0300181 return completed == start
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000182 self.userwantswindow = userWantsWin
183 self.lasttypedstart = self.start
184
185 # Put widgets in place
186 self.autocompletewindow = acw = Toplevel(self.widget)
187 # Put it in a position so that it is not seen.
188 acw.wm_geometry("+10000+10000")
189 # Make it float
190 acw.wm_overrideredirect(1)
191 try:
192 # This command is only needed and available on Tk >= 8.4.0 for OSX
193 # Without it, call tips intrude on the typing process by grabbing
194 # the focus.
195 acw.tk.call("::tk::unsupported::MacWindowStyle", "style", acw._w,
196 "help", "noActivates")
197 except TclError:
198 pass
199 self.scrollbar = scrollbar = Scrollbar(acw, orient=VERTICAL)
200 self.listbox = listbox = Listbox(acw, yscrollcommand=scrollbar.set,
Terry Jan Reedy491ef532019-03-10 20:18:40 -0400201 exportselection=False)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000202 for item in self.completions:
203 listbox.insert(END, item)
204 self.origselforeground = listbox.cget("selectforeground")
205 self.origselbackground = listbox.cget("selectbackground")
206 scrollbar.config(command=listbox.yview)
207 scrollbar.pack(side=RIGHT, fill=Y)
208 listbox.pack(side=LEFT, fill=BOTH, expand=True)
Terry Jan Reedyd2134c72015-09-26 20:03:57 -0400209 acw.lift() # work around bug in Tk 8.5.18+ (issue #24570)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000210
211 # Initialize the listbox selection
212 self.listbox.select_set(self._binary_search(self.start))
213 self._selection_changed()
214
215 # bind events
mlouielu778b4842017-06-14 23:13:19 +0800216 self.hideaid = acw.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
217 self.hidewid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
218 acw.event_add(HIDE_VIRTUAL_EVENT_NAME, HIDE_FOCUS_OUT_SEQUENCE)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000219 for seq in HIDE_SEQUENCES:
220 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
mlouielu778b4842017-06-14 23:13:19 +0800221
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000222 self.keypressid = self.widget.bind(KEYPRESS_VIRTUAL_EVENT_NAME,
223 self.keypress_event)
224 for seq in KEYPRESS_SEQUENCES:
225 self.widget.event_add(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
226 self.keyreleaseid = self.widget.bind(KEYRELEASE_VIRTUAL_EVENT_NAME,
227 self.keyrelease_event)
228 self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE)
229 self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE,
Thomas Wouterscf297e42007-02-23 15:07:44 +0000230 self.listselect_event)
Tal Einat71662dc2019-08-14 20:06:06 +0300231 self.is_configuring = False
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000232 self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
233 self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
234 self.doubleclick_event)
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400235 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000236
237 def winconfig_event(self, event):
Tal Einat71662dc2019-08-14 20:06:06 +0300238 if self.is_configuring:
239 # Avoid running on recursive <Configure> callback invocations.
240 return
241
242 self.is_configuring = True
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000243 if not self.is_active():
244 return
245 # Position the completion list window
Thomas Wouterscf297e42007-02-23 15:07:44 +0000246 text = self.widget
247 text.see(self.startindex)
248 x, y, cx, cy = text.bbox(self.startindex)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000249 acw = self.autocompletewindow
Tal Einat71662dc2019-08-14 20:06:06 +0300250 acw.update()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000251 acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
252 text_width, text_height = text.winfo_width(), text.winfo_height()
253 new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
254 new_y = text.winfo_rooty() + y
255 if (text_height - (y + cy) >= acw_height # enough height below
256 or y < acw_height): # not enough height above
257 # place acw below current line
258 new_y += cy
259 else:
260 # place acw above current line
261 new_y -= acw_height
262 acw.wm_geometry("+%d+%d" % (new_x, new_y))
JohnnyNajerabbc41622019-12-10 02:30:01 +0200263 acw.update_idletasks()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000264
mlouielu778b4842017-06-14 23:13:19 +0800265 if platform.system().startswith('Windows'):
Terry Jan Reedy33c74202018-06-20 22:49:55 -0400266 # See issue 15786. When on Windows platform, Tk will misbehave
mlouielu778b4842017-06-14 23:13:19 +0800267 # to call winconfig_event multiple times, we need to prevent this,
268 # otherwise mouse button double click will not be able to used.
269 acw.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
270 self.winconfigid = None
271
Tal Einat71662dc2019-08-14 20:06:06 +0300272 self.is_configuring = False
273
mlouielu778b4842017-06-14 23:13:19 +0800274 def _hide_event_check(self):
275 if not self.autocompletewindow:
276 return
277
278 try:
279 if not self.autocompletewindow.focus_get():
280 self.hide_window()
281 except KeyError:
282 # See issue 734176, when user click on menu, acw.focus_get()
283 # will get KeyError.
Terry Jan Reedyc665dfd2016-07-24 23:01:28 -0400284 self.hide_window()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000285
mlouielu778b4842017-06-14 23:13:19 +0800286 def hide_event(self, event):
287 # Hide autocomplete list if it exists and does not have focus or
288 # mouse click on widget / text area.
289 if self.is_active():
290 if event.type == EventType.FocusOut:
Terry Jan Reedy33c74202018-06-20 22:49:55 -0400291 # On Windows platform, it will need to delay the check for
mlouielu778b4842017-06-14 23:13:19 +0800292 # acw.focus_get() when click on acw, otherwise it will return
293 # None and close the window
294 self.widget.after(1, self._hide_event_check)
295 elif event.type == EventType.ButtonPress:
296 # ButtonPress event only bind to self.widget
297 self.hide_window()
298
Thomas Wouterscf297e42007-02-23 15:07:44 +0000299 def listselect_event(self, event):
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400300 if self.is_active():
301 self.userwantswindow = True
302 cursel = int(self.listbox.curselection()[0])
303 self._change_start(self.completions[cursel])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000304
305 def doubleclick_event(self, event):
306 # Put the selected completion in the text, and close the list
307 cursel = int(self.listbox.curselection()[0])
308 self._change_start(self.completions[cursel])
309 self.hide_window()
310
311 def keypress_event(self, event):
312 if not self.is_active():
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400313 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000314 keysym = event.keysym
315 if hasattr(event, "mc_state"):
316 state = event.mc_state
317 else:
318 state = 0
Thomas Wouterscf297e42007-02-23 15:07:44 +0000319 if keysym != "Tab":
320 self.lastkey_was_tab = False
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000321 if (len(keysym) == 1 or keysym in ("underscore", "BackSpace")
Terry Jan Reedy12131232019-08-04 19:48:52 -0400322 or (self.mode == FILES and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000323 ("period", "minus"))) \
324 and not (state & ~MC_SHIFT):
325 # Normal editing of text
326 if len(keysym) == 1:
327 self._change_start(self.start + keysym)
328 elif keysym == "underscore":
329 self._change_start(self.start + '_')
330 elif keysym == "period":
331 self._change_start(self.start + '.')
332 elif keysym == "minus":
333 self._change_start(self.start + '-')
334 else:
335 # keysym == "BackSpace"
336 if len(self.start) == 0:
337 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400338 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000339 self._change_start(self.start[:-1])
340 self.lasttypedstart = self.start
341 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
342 self.listbox.select_set(self._binary_search(self.start))
343 self._selection_changed()
344 return "break"
345
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000346 elif keysym == "Return":
terryjreedy32fd8742017-06-14 15:43:15 -0400347 self.complete()
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000348 self.hide_window()
terryjreedy32fd8742017-06-14 15:43:15 -0400349 return 'break'
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000350
Terry Jan Reedy12131232019-08-04 19:48:52 -0400351 elif (self.mode == ATTRS and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000352 ("period", "space", "parenleft", "parenright", "bracketleft",
353 "bracketright")) or \
Terry Jan Reedy12131232019-08-04 19:48:52 -0400354 (self.mode == FILES and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000355 ("slash", "backslash", "quotedbl", "apostrophe")) \
356 and not (state & ~MC_SHIFT):
357 # If start is a prefix of the selection, but is not '' when
358 # completing file names, put the whole
359 # selected completion. Anyway, close the list.
360 cursel = int(self.listbox.curselection()[0])
361 if self.completions[cursel][:len(self.start)] == self.start \
Terry Jan Reedy12131232019-08-04 19:48:52 -0400362 and (self.mode == ATTRS or self.start):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000363 self._change_start(self.completions[cursel])
364 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400365 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000366
367 elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \
368 not state:
369 # Move the selection in the listbox
370 self.userwantswindow = True
371 cursel = int(self.listbox.curselection()[0])
372 if keysym == "Home":
373 newsel = 0
374 elif keysym == "End":
375 newsel = len(self.completions)-1
376 elif keysym in ("Prior", "Next"):
377 jump = self.listbox.nearest(self.listbox.winfo_height()) - \
378 self.listbox.nearest(0)
379 if keysym == "Prior":
380 newsel = max(0, cursel-jump)
381 else:
382 assert keysym == "Next"
383 newsel = min(len(self.completions)-1, cursel+jump)
384 elif keysym == "Up":
385 newsel = max(0, cursel-1)
386 else:
387 assert keysym == "Down"
388 newsel = min(len(self.completions)-1, cursel+1)
389 self.listbox.select_clear(cursel)
390 self.listbox.select_set(newsel)
391 self._selection_changed()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000392 self._change_start(self.completions[newsel])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000393 return "break"
394
395 elif (keysym == "Tab" and not state):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000396 if self.lastkey_was_tab:
397 # two tabs in a row; insert current selection and close acw
398 cursel = int(self.listbox.curselection()[0])
399 self._change_start(self.completions[cursel])
400 self.hide_window()
401 return "break"
402 else:
403 # first tab; let AutoComplete handle the completion
404 self.userwantswindow = True
405 self.lastkey_was_tab = True
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400406 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000407
Guido van Rossum89da5d72006-08-22 00:21:25 +0000408 elif any(s in keysym for s in ("Shift", "Control", "Alt",
409 "Meta", "Command", "Option")):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000410 # A modifier key, so ignore
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400411 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000412
Martin v. Löwis97aa21b2012-06-03 12:26:09 +0200413 elif event.char and event.char >= ' ':
Martin v. Löwis862d13a2012-06-03 11:55:32 +0200414 # Regular character with a non-length-1 keycode
415 self._change_start(self.start + event.char)
416 self.lasttypedstart = self.start
417 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
418 self.listbox.select_set(self._binary_search(self.start))
419 self._selection_changed()
420 return "break"
421
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000422 else:
423 # Unknown event, close the window and let it through.
424 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400425 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000426
427 def keyrelease_event(self, event):
428 if not self.is_active():
429 return
430 if self.widget.index("insert") != \
431 self.widget.index("%s+%dc" % (self.startindex, len(self.start))):
432 # If we didn't catch an event which moved the insert, close window
433 self.hide_window()
434
435 def is_active(self):
436 return self.autocompletewindow is not None
437
438 def complete(self):
439 self._change_start(self._complete_string(self.start))
440 # The selection doesn't change.
441
442 def hide_window(self):
443 if not self.is_active():
444 return
445
446 # unbind events
mlouielu778b4842017-06-14 23:13:19 +0800447 self.autocompletewindow.event_delete(HIDE_VIRTUAL_EVENT_NAME,
448 HIDE_FOCUS_OUT_SEQUENCE)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000449 for seq in HIDE_SEQUENCES:
450 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
mlouielu778b4842017-06-14 23:13:19 +0800451
452 self.autocompletewindow.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideaid)
453 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hidewid)
454 self.hideaid = None
455 self.hidewid = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000456 for seq in KEYPRESS_SEQUENCES:
457 self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
458 self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid)
459 self.keypressid = None
460 self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME,
461 KEYRELEASE_SEQUENCE)
462 self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid)
463 self.keyreleaseid = None
464 self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid)
465 self.listupdateid = None
mlouielu778b4842017-06-14 23:13:19 +0800466 if self.winconfigid:
467 self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
468 self.winconfigid = None
469
470 # Re-focusOn frame.text (See issue #15786)
471 self.widget.focus_set()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000472
473 # destroy widgets
474 self.scrollbar.destroy()
475 self.scrollbar = None
476 self.listbox.destroy()
477 self.listbox = None
478 self.autocompletewindow.destroy()
479 self.autocompletewindow = None
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400480
481
482if __name__ == '__main__':
483 from unittest import main
484 main('idlelib.idle_test.test_autocomplete_w', verbosity=2, exit=False)
485
486# TODO: autocomplete/w htest here