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