blob: 7059054281818aeaab531b7a515fc52a941918c1 [file] [log] [blame]
Terry Jan Reedy6e66cb02014-06-04 20:50:49 -04001'''Complete the current word before the cursor with words in the editor.
2
3Each menu selection or shortcut key selection replaces the word with a
4different word with the same prefix. The search for matches begins
5before the target and moves toward the top of the editor. It then starts
6after the cursor and moves down. It then returns to the original word and
7the cycle starts again.
8
9Changing the current text line or leaving the cursor in a different
10place before requesting the next selection causes AutoExpand to reset
11its state.
12
13This is an extension file and there is only one instance of AutoExpand.
14'''
David Scherer7aced172000-08-15 01:13:23 +000015import string
16import re
17
18###$ event <<expand-word>>
19###$ win <Alt-slash>
20###$ unix <Alt-slash>
21
22class AutoExpand:
23
David Scherer7aced172000-08-15 01:13:23 +000024 menudefs = [
25 ('edit', [
Kurt B. Kaiser1061e722003-01-04 01:43:53 +000026 ('E_xpand Word', '<<expand-word>>'),
David Scherer7aced172000-08-15 01:13:23 +000027 ]),
28 ]
29
Kurt B. Kaiser87807a62002-09-15 20:50:02 +000030 wordchars = string.ascii_letters + string.digits + "_"
David Scherer7aced172000-08-15 01:13:23 +000031
32 def __init__(self, editwin):
33 self.text = editwin.text
David Scherer7aced172000-08-15 01:13:23 +000034 self.state = None
35
36 def expand_word_event(self, event):
Terry Jan Reedy6e66cb02014-06-04 20:50:49 -040037 "Replace the current word with the next expansion."
David Scherer7aced172000-08-15 01:13:23 +000038 curinsert = self.text.index("insert")
39 curline = self.text.get("insert linestart", "insert lineend")
40 if not self.state:
41 words = self.getwords()
42 index = 0
43 else:
44 words, index, insert, line = self.state
45 if insert != curinsert or line != curline:
46 words = self.getwords()
47 index = 0
48 if not words:
49 self.text.bell()
50 return "break"
51 word = self.getprevword()
52 self.text.delete("insert - %d chars" % len(word), "insert")
53 newword = words[index]
54 index = (index + 1) % len(words)
55 if index == 0:
56 self.text.bell() # Warn we cycled around
57 self.text.insert("insert", newword)
58 curinsert = self.text.index("insert")
59 curline = self.text.get("insert linestart", "insert lineend")
60 self.state = words, index, curinsert, curline
61 return "break"
62
63 def getwords(self):
Terry Jan Reedy6e66cb02014-06-04 20:50:49 -040064 "Return a list of words that match the prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000065 word = self.getprevword()
66 if not word:
67 return []
68 before = self.text.get("1.0", "insert wordstart")
69 wbefore = re.findall(r"\b" + word + r"\w+\b", before)
70 del before
71 after = self.text.get("insert wordend", "end")
72 wafter = re.findall(r"\b" + word + r"\w+\b", after)
73 del after
74 if not wbefore and not wafter:
75 return []
76 words = []
77 dict = {}
78 # search backwards through words before
79 wbefore.reverse()
80 for w in wbefore:
81 if dict.get(w):
82 continue
83 words.append(w)
84 dict[w] = w
85 # search onwards through words after
86 for w in wafter:
87 if dict.get(w):
88 continue
89 words.append(w)
90 dict[w] = w
91 words.append(word)
92 return words
93
94 def getprevword(self):
Terry Jan Reedy6e66cb02014-06-04 20:50:49 -040095 "Return the word prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000096 line = self.text.get("insert linestart", "insert")
97 i = len(line)
98 while i > 0 and line[i-1] in self.wordchars:
99 i = i-1
100 return line[i:]
Terry Jan Reedy6e66cb02014-06-04 20:50:49 -0400101
102if __name__ == '__main__':
103 import unittest
104 unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)