blob: 6b46bee69c95f5eacb2e7c92ddffcf68e55e976f [file] [log] [blame]
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -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 re
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040016import string
David Scherer7aced172000-08-15 01:13:23 +000017
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
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040034 self.bell = self.text.bell
David Scherer7aced172000-08-15 01:13:23 +000035 self.state = None
36
37 def expand_word_event(self, event):
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040038 "Replace the current word with the next expansion."
David Scherer7aced172000-08-15 01:13:23 +000039 curinsert = self.text.index("insert")
40 curline = self.text.get("insert linestart", "insert lineend")
41 if not self.state:
42 words = self.getwords()
43 index = 0
44 else:
45 words, index, insert, line = self.state
46 if insert != curinsert or line != curline:
47 words = self.getwords()
48 index = 0
49 if not words:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040050 self.bell()
David Scherer7aced172000-08-15 01:13:23 +000051 return "break"
52 word = self.getprevword()
53 self.text.delete("insert - %d chars" % len(word), "insert")
54 newword = words[index]
55 index = (index + 1) % len(words)
56 if index == 0:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040057 self.bell() # Warn we cycled around
David Scherer7aced172000-08-15 01:13:23 +000058 self.text.insert("insert", newword)
59 curinsert = self.text.index("insert")
60 curline = self.text.get("insert linestart", "insert lineend")
61 self.state = words, index, curinsert, curline
62 return "break"
63
64 def getwords(self):
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040065 "Return a list of words that match the prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000066 word = self.getprevword()
67 if not word:
68 return []
69 before = self.text.get("1.0", "insert wordstart")
70 wbefore = re.findall(r"\b" + word + r"\w+\b", before)
71 del before
72 after = self.text.get("insert wordend", "end")
73 wafter = re.findall(r"\b" + word + r"\w+\b", after)
74 del after
75 if not wbefore and not wafter:
76 return []
77 words = []
78 dict = {}
79 # search backwards through words before
80 wbefore.reverse()
81 for w in wbefore:
82 if dict.get(w):
83 continue
84 words.append(w)
85 dict[w] = w
86 # search onwards through words after
87 for w in wafter:
88 if dict.get(w):
89 continue
90 words.append(w)
91 dict[w] = w
92 words.append(word)
93 return words
94
95 def getprevword(self):
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -040096 "Return the word prefix before the cursor."
David Scherer7aced172000-08-15 01:13:23 +000097 line = self.text.get("insert linestart", "insert")
98 i = len(line)
99 while i > 0 and line[i-1] in self.wordchars:
100 i = i-1
101 return line[i:]
Terry Jan Reedy13f4aba2014-06-04 20:54:43 -0400102
103if __name__ == '__main__':
104 import unittest
105 unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)