Home | History | Annotate | Download | only in idlelib
      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 re
     16 import string
     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.bell = self.text.bell
     35         self.state = None
     36 
     37     def expand_word_event(self, event):
     38         "Replace the current word with the next expansion."
     39         curinsert = self.text.index("insert")
     40         curline = self.text.get("insert linestart", "insert lineend")
     41         if not self.state:
     42             words = self.getwords()
     43             index = 0
     44         else:
     45             words, index, insert, line = self.state
     46             if insert != curinsert or line != curline:
     47                 words = self.getwords()
     48                 index = 0
     49         if not words:
     50             self.bell()
     51             return "break"
     52         word = self.getprevword()
     53         self.text.delete("insert - %d chars" % len(word), "insert")
     54         newword = words[index]
     55         index = (index + 1) % len(words)
     56         if index == 0:
     57             self.bell()            # Warn we cycled around
     58         self.text.insert("insert", newword)
     59         curinsert = self.text.index("insert")
     60         curline = self.text.get("insert linestart", "insert lineend")
     61         self.state = words, index, curinsert, curline
     62         return "break"
     63 
     64     def getwords(self):
     65         "Return a list of words that match the prefix before the cursor."
     66         word = self.getprevword()
     67         if not word:
     68             return []
     69         before = self.text.get("1.0", "insert wordstart")
     70         wbefore = re.findall(r"\b" + word + r"\w+\b", before)
     71         del before
     72         after = self.text.get("insert wordend", "end")
     73         wafter = re.findall(r"\b" + word + r"\w+\b", after)
     74         del after
     75         if not wbefore and not wafter:
     76             return []
     77         words = []
     78         dict = {}
     79         # search backwards through words before
     80         wbefore.reverse()
     81         for w in wbefore:
     82             if dict.get(w):
     83                 continue
     84             words.append(w)
     85             dict[w] = w
     86         # search onwards through words after
     87         for w in wafter:
     88             if dict.get(w):
     89                 continue
     90             words.append(w)
     91             dict[w] = w
     92         words.append(word)
     93         return words
     94 
     95     def getprevword(self):
     96         "Return the word prefix before the cursor."
     97         line = self.text.get("insert linestart", "insert")
     98         i = len(line)
     99         while i > 0 and line[i-1] in self.wordchars:
    100             i = i-1
    101         return line[i:]
    102 
    103 if __name__ == '__main__':
    104     import unittest
    105     unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)
    106