Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 1 | import string |
| 2 | import re |
| 3 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 4 | ###$ event <<expand-word>> |
| 5 | ###$ win <Alt-slash> |
| 6 | ###$ unix <Alt-slash> |
| 7 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 8 | class AutoExpand: |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 9 | |
| 10 | keydefs = { |
| 11 | '<<expand-word>>': ['<Alt-slash>'], |
| 12 | } |
| 13 | |
Guido van Rossum | 16b91f8 | 1999-01-04 16:32:21 +0000 | [diff] [blame] | 14 | unix_keydefs = { |
Guido van Rossum | b19f1e3 | 2001-01-02 18:28:52 +0000 | [diff] [blame] | 15 | '<<expand-word>>': ['<Meta-slash>', '<Alt-slash>'], |
Guido van Rossum | 16b91f8 | 1999-01-04 16:32:21 +0000 | [diff] [blame] | 16 | } |
| 17 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 18 | menudefs = [ |
| 19 | ('edit', [ |
| 20 | ('E_xpand word', '<<expand-word>>'), |
| 21 | ]), |
| 22 | ] |
| 23 | |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 24 | wordchars = string.ascii_letters + string.digits + "_" |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 26 | def __init__(self, editwin): |
| 27 | self.text = editwin.text |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 28 | self.state = None |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 29 | |
| 30 | def expand_word_event(self, event): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 31 | curinsert = self.text.index("insert") |
| 32 | curline = self.text.get("insert linestart", "insert lineend") |
| 33 | if not self.state: |
| 34 | words = self.getwords() |
| 35 | index = 0 |
| 36 | else: |
| 37 | words, index, insert, line = self.state |
| 38 | if insert != curinsert or line != curline: |
| 39 | words = self.getwords() |
| 40 | index = 0 |
| 41 | if not words: |
| 42 | self.text.bell() |
| 43 | return "break" |
| 44 | word = self.getprevword() |
| 45 | self.text.delete("insert - %d chars" % len(word), "insert") |
| 46 | newword = words[index] |
| 47 | index = (index + 1) % len(words) |
| 48 | if index == 0: |
| 49 | self.text.bell() # Warn we cycled around |
| 50 | self.text.insert("insert", newword) |
| 51 | curinsert = self.text.index("insert") |
| 52 | curline = self.text.get("insert linestart", "insert lineend") |
| 53 | self.state = words, index, curinsert, curline |
| 54 | return "break" |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 56 | def getwords(self): |
| 57 | word = self.getprevword() |
| 58 | if not word: |
| 59 | return [] |
| 60 | before = self.text.get("1.0", "insert wordstart") |
| 61 | wbefore = re.findall(r"\b" + word + r"\w+\b", before) |
| 62 | del before |
| 63 | after = self.text.get("insert wordend", "end") |
| 64 | wafter = re.findall(r"\b" + word + r"\w+\b", after) |
| 65 | del after |
| 66 | if not wbefore and not wafter: |
| 67 | return [] |
| 68 | words = [] |
| 69 | dict = {} |
| 70 | # search backwards through words before |
| 71 | wbefore.reverse() |
| 72 | for w in wbefore: |
| 73 | if dict.get(w): |
| 74 | continue |
| 75 | words.append(w) |
| 76 | dict[w] = w |
| 77 | # search onwards through words after |
| 78 | for w in wafter: |
| 79 | if dict.get(w): |
| 80 | continue |
| 81 | words.append(w) |
| 82 | dict[w] = w |
| 83 | words.append(word) |
| 84 | return words |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 86 | def getprevword(self): |
| 87 | line = self.text.get("insert linestart", "insert") |
| 88 | i = len(line) |
| 89 | while i > 0 and line[i-1] in self.wordchars: |
| 90 | i = i-1 |
| 91 | return line[i:] |