Debian Codegraph @

cpython27/Lib/idlelib/AutoExpand.py

raw symbols SCIP
1 '''Complete the current word before the cursor with words in the editor.
2
3 Each menu selection or shortcut key selection replaces the word with a
4 different word with the same prefix. The search for matches begins
5 before the target and moves toward the top of the editor. It then starts
6 after the cursor and moves down. It then returns to the original word and
7 the cycle starts again.
8
9 Changing the current text line or leaving the cursor in a different
10 place before requesting the next selection causes AutoExpand to reset
11 its state.
12
13 This is an extension file and there is only one instance of AutoExpand.
14 '''
15 import string
16 import re
17
18 ###$ event <<expand-word>>
19 ###$ win <Alt-slash>
20 ###$ unix <Alt-slash>
21
22 class AutoExpand:
23
24 menudefs = [
25 ('edit', [
26 ('E_xpand Word', '<<expand-word>>'),
27 ]),
28 ]
29
30 wordchars = string.ascii_letters + string.digits + "_"
31
32 def __init__(self, editwin):
33 self.text = editwin.text
34 self.state = None
35
36 def expand_word_event(self, event):
37 "Replace the current word with the next expansion."
38 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):
64 "Return a list of words that match the prefix before the cursor."
65 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):
95 "Return the word prefix before the cursor."
96 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:]
101
102 if __name__ == '__main__':
103 import unittest
104 unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)