1998-10-10 20:48:31 +02:00
|
|
|
import string
|
|
|
|
import re
|
|
|
|
|
1999-01-02 22:28:54 +01:00
|
|
|
###$ event <<expand-word>>
|
|
|
|
###$ win <Alt-slash>
|
|
|
|
###$ unix <Alt-slash>
|
|
|
|
|
1998-10-10 20:48:31 +02:00
|
|
|
class AutoExpand:
|
1999-01-02 22:28:54 +01:00
|
|
|
|
|
|
|
keydefs = {
|
|
|
|
'<<expand-word>>': ['<Alt-slash>'],
|
|
|
|
}
|
|
|
|
|
1999-01-04 17:32:21 +01:00
|
|
|
unix_keydefs = {
|
|
|
|
'<<expand-word>>': ['<Meta-slash>'],
|
|
|
|
}
|
|
|
|
|
1999-01-02 22:28:54 +01:00
|
|
|
menudefs = [
|
|
|
|
('edit', [
|
|
|
|
('E_xpand word', '<<expand-word>>'),
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
|
1998-10-10 20:48:31 +02:00
|
|
|
wordchars = string.letters + string.digits + "_"
|
|
|
|
|
1999-01-02 22:28:54 +01:00
|
|
|
def __init__(self, editwin):
|
|
|
|
self.text = editwin.text
|
|
|
|
self.text.wordlist = None # XXX what is this?
|
1998-10-10 20:48:31 +02:00
|
|
|
self.state = None
|
1999-01-02 22:28:54 +01:00
|
|
|
|
|
|
|
def expand_word_event(self, event):
|
1998-10-10 20:48:31 +02:00
|
|
|
curinsert = self.text.index("insert")
|
|
|
|
curline = self.text.get("insert linestart", "insert lineend")
|
|
|
|
if not self.state:
|
|
|
|
words = self.getwords()
|
|
|
|
index = 0
|
|
|
|
else:
|
|
|
|
words, index, insert, line = self.state
|
|
|
|
if insert != curinsert or line != curline:
|
|
|
|
words = self.getwords()
|
|
|
|
index = 0
|
|
|
|
if not words:
|
|
|
|
self.text.bell()
|
|
|
|
return "break"
|
|
|
|
word = self.getprevword()
|
|
|
|
self.text.delete("insert - %d chars" % len(word), "insert")
|
|
|
|
newword = words[index]
|
|
|
|
index = (index + 1) % len(words)
|
|
|
|
if index == 0:
|
|
|
|
self.text.bell() # Warn we cycled around
|
|
|
|
self.text.insert("insert", newword)
|
|
|
|
curinsert = self.text.index("insert")
|
|
|
|
curline = self.text.get("insert linestart", "insert lineend")
|
|
|
|
self.state = words, index, curinsert, curline
|
|
|
|
return "break"
|
1999-01-02 22:28:54 +01:00
|
|
|
|
1998-10-10 20:48:31 +02:00
|
|
|
def getwords(self):
|
|
|
|
word = self.getprevword()
|
|
|
|
if not word:
|
|
|
|
return []
|
|
|
|
before = self.text.get("1.0", "insert wordstart")
|
|
|
|
wbefore = re.findall(r"\b" + word + r"\w+\b", before)
|
|
|
|
del before
|
|
|
|
after = self.text.get("insert wordend", "end")
|
|
|
|
wafter = re.findall(r"\b" + word + r"\w+\b", after)
|
|
|
|
del after
|
|
|
|
if not wbefore and not wafter:
|
|
|
|
return []
|
|
|
|
words = []
|
|
|
|
dict = {}
|
|
|
|
# search backwards through words before
|
|
|
|
wbefore.reverse()
|
|
|
|
for w in wbefore:
|
|
|
|
if dict.get(w):
|
|
|
|
continue
|
|
|
|
words.append(w)
|
|
|
|
dict[w] = w
|
|
|
|
# search onwards through words after
|
|
|
|
for w in wafter:
|
|
|
|
if dict.get(w):
|
|
|
|
continue
|
|
|
|
words.append(w)
|
|
|
|
dict[w] = w
|
|
|
|
words.append(word)
|
|
|
|
return words
|
1999-01-02 22:28:54 +01:00
|
|
|
|
1998-10-10 20:48:31 +02:00
|
|
|
def getprevword(self):
|
|
|
|
line = self.text.get("insert linestart", "insert")
|
|
|
|
i = len(line)
|
|
|
|
while i > 0 and line[i-1] in self.wordchars:
|
|
|
|
i = i-1
|
|
|
|
return line[i:]
|