PyXR

c:\python24\lib \ idlelib \ AutoExpand.py



0001 import string
0002 import re
0003 
0004 ###$ event <<expand-word>>
0005 ###$ win <Alt-slash>
0006 ###$ unix <Alt-slash>
0007 
0008 class AutoExpand:
0009 
0010     menudefs = [
0011         ('edit', [
0012             ('E_xpand Word', '<<expand-word>>'),
0013          ]),
0014     ]
0015 
0016     wordchars = string.ascii_letters + string.digits + "_"
0017 
0018     def __init__(self, editwin):
0019         self.text = editwin.text
0020         self.state = None
0021 
0022     def expand_word_event(self, event):
0023         curinsert = self.text.index("insert")
0024         curline = self.text.get("insert linestart", "insert lineend")
0025         if not self.state:
0026             words = self.getwords()
0027             index = 0
0028         else:
0029             words, index, insert, line = self.state
0030             if insert != curinsert or line != curline:
0031                 words = self.getwords()
0032                 index = 0
0033         if not words:
0034             self.text.bell()
0035             return "break"
0036         word = self.getprevword()
0037         self.text.delete("insert - %d chars" % len(word), "insert")
0038         newword = words[index]
0039         index = (index + 1) % len(words)
0040         if index == 0:
0041             self.text.bell()            # Warn we cycled around
0042         self.text.insert("insert", newword)
0043         curinsert = self.text.index("insert")
0044         curline = self.text.get("insert linestart", "insert lineend")
0045         self.state = words, index, curinsert, curline
0046         return "break"
0047 
0048     def getwords(self):
0049         word = self.getprevword()
0050         if not word:
0051             return []
0052         before = self.text.get("1.0", "insert wordstart")
0053         wbefore = re.findall(r"\b" + word + r"\w+\b", before)
0054         del before
0055         after = self.text.get("insert wordend", "end")
0056         wafter = re.findall(r"\b" + word + r"\w+\b", after)
0057         del after
0058         if not wbefore and not wafter:
0059             return []
0060         words = []
0061         dict = {}
0062         # search backwards through words before
0063         wbefore.reverse()
0064         for w in wbefore:
0065             if dict.get(w):
0066                 continue
0067             words.append(w)
0068             dict[w] = w
0069         # search onwards through words after
0070         for w in wafter:
0071             if dict.get(w):
0072                 continue
0073             words.append(w)
0074             dict[w] = w
0075         words.append(word)
0076         return words
0077 
0078     def getprevword(self):
0079         line = self.text.get("insert linestart", "insert")
0080         i = len(line)
0081         while i > 0 and line[i-1] in self.wordchars:
0082             i = i-1
0083         return line[i:]
0084 

Generated by PyXR 0.9.4
SourceForge.net Logo