Terry Jan Reedy | 6e66cb0 | 2014-06-04 20:50:49 -0400 | [diff] [blame] | 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 | ''' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 15 | import string |
| 16 | import re |
| 17 | |
| 18 | ###$ event <<expand-word>> |
| 19 | ###$ win <Alt-slash> |
| 20 | ###$ unix <Alt-slash> |
| 21 | |
| 22 | class AutoExpand: |
| 23 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 24 | menudefs = [ |
| 25 | ('edit', [ |
Kurt B. Kaiser | 1061e72 | 2003-01-04 01:43:53 +0000 | [diff] [blame] | 26 | ('E_xpand Word', '<<expand-word>>'), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 27 | ]), |
| 28 | ] |
| 29 | |
Kurt B. Kaiser | 87807a6 | 2002-09-15 20:50:02 +0000 | [diff] [blame] | 30 | wordchars = string.ascii_letters + string.digits + "_" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | |
| 32 | def __init__(self, editwin): |
| 33 | self.text = editwin.text |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | self.state = None |
| 35 | |
| 36 | def expand_word_event(self, event): |
Terry Jan Reedy | 6e66cb0 | 2014-06-04 20:50:49 -0400 | [diff] [blame] | 37 | "Replace the current word with the next expansion." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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): |
Terry Jan Reedy | 6e66cb0 | 2014-06-04 20:50:49 -0400 | [diff] [blame] | 64 | "Return a list of words that match the prefix before the cursor." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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): |
Terry Jan Reedy | 6e66cb0 | 2014-06-04 20:50:49 -0400 | [diff] [blame] | 95 | "Return the word prefix before the cursor." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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:] |
Terry Jan Reedy | 6e66cb0 | 2014-06-04 20:50:49 -0400 | [diff] [blame] | 101 | |
| 102 | if __name__ == '__main__': |
| 103 | import unittest |
| 104 | unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2) |