Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: AutoExpand.py
'''Complete the current word before the cursor with words in the editor.
[0] Fix | Delete
[1] Fix | Delete
Each menu selection or shortcut key selection replaces the word with a
[2] Fix | Delete
different word with the same prefix. The search for matches begins
[3] Fix | Delete
before the target and moves toward the top of the editor. It then starts
[4] Fix | Delete
after the cursor and moves down. It then returns to the original word and
[5] Fix | Delete
the cycle starts again.
[6] Fix | Delete
[7] Fix | Delete
Changing the current text line or leaving the cursor in a different
[8] Fix | Delete
place before requesting the next selection causes AutoExpand to reset
[9] Fix | Delete
its state.
[10] Fix | Delete
[11] Fix | Delete
This is an extension file and there is only one instance of AutoExpand.
[12] Fix | Delete
'''
[13] Fix | Delete
import string
[14] Fix | Delete
import re
[15] Fix | Delete
[16] Fix | Delete
###$ event <<expand-word>>
[17] Fix | Delete
###$ win <Alt-slash>
[18] Fix | Delete
###$ unix <Alt-slash>
[19] Fix | Delete
[20] Fix | Delete
class AutoExpand:
[21] Fix | Delete
[22] Fix | Delete
menudefs = [
[23] Fix | Delete
('edit', [
[24] Fix | Delete
('E_xpand Word', '<<expand-word>>'),
[25] Fix | Delete
]),
[26] Fix | Delete
]
[27] Fix | Delete
[28] Fix | Delete
wordchars = string.ascii_letters + string.digits + "_"
[29] Fix | Delete
[30] Fix | Delete
def __init__(self, editwin):
[31] Fix | Delete
self.text = editwin.text
[32] Fix | Delete
self.state = None
[33] Fix | Delete
[34] Fix | Delete
def expand_word_event(self, event):
[35] Fix | Delete
"Replace the current word with the next expansion."
[36] Fix | Delete
curinsert = self.text.index("insert")
[37] Fix | Delete
curline = self.text.get("insert linestart", "insert lineend")
[38] Fix | Delete
if not self.state:
[39] Fix | Delete
words = self.getwords()
[40] Fix | Delete
index = 0
[41] Fix | Delete
else:
[42] Fix | Delete
words, index, insert, line = self.state
[43] Fix | Delete
if insert != curinsert or line != curline:
[44] Fix | Delete
words = self.getwords()
[45] Fix | Delete
index = 0
[46] Fix | Delete
if not words:
[47] Fix | Delete
self.text.bell()
[48] Fix | Delete
return "break"
[49] Fix | Delete
word = self.getprevword()
[50] Fix | Delete
self.text.delete("insert - %d chars" % len(word), "insert")
[51] Fix | Delete
newword = words[index]
[52] Fix | Delete
index = (index + 1) % len(words)
[53] Fix | Delete
if index == 0:
[54] Fix | Delete
self.text.bell() # Warn we cycled around
[55] Fix | Delete
self.text.insert("insert", newword)
[56] Fix | Delete
curinsert = self.text.index("insert")
[57] Fix | Delete
curline = self.text.get("insert linestart", "insert lineend")
[58] Fix | Delete
self.state = words, index, curinsert, curline
[59] Fix | Delete
return "break"
[60] Fix | Delete
[61] Fix | Delete
def getwords(self):
[62] Fix | Delete
"Return a list of words that match the prefix before the cursor."
[63] Fix | Delete
word = self.getprevword()
[64] Fix | Delete
if not word:
[65] Fix | Delete
return []
[66] Fix | Delete
before = self.text.get("1.0", "insert wordstart")
[67] Fix | Delete
wbefore = re.findall(r"\b" + word + r"\w+\b", before)
[68] Fix | Delete
del before
[69] Fix | Delete
after = self.text.get("insert wordend", "end")
[70] Fix | Delete
wafter = re.findall(r"\b" + word + r"\w+\b", after)
[71] Fix | Delete
del after
[72] Fix | Delete
if not wbefore and not wafter:
[73] Fix | Delete
return []
[74] Fix | Delete
words = []
[75] Fix | Delete
dict = {}
[76] Fix | Delete
# search backwards through words before
[77] Fix | Delete
wbefore.reverse()
[78] Fix | Delete
for w in wbefore:
[79] Fix | Delete
if dict.get(w):
[80] Fix | Delete
continue
[81] Fix | Delete
words.append(w)
[82] Fix | Delete
dict[w] = w
[83] Fix | Delete
# search onwards through words after
[84] Fix | Delete
for w in wafter:
[85] Fix | Delete
if dict.get(w):
[86] Fix | Delete
continue
[87] Fix | Delete
words.append(w)
[88] Fix | Delete
dict[w] = w
[89] Fix | Delete
words.append(word)
[90] Fix | Delete
return words
[91] Fix | Delete
[92] Fix | Delete
def getprevword(self):
[93] Fix | Delete
"Return the word prefix before the cursor."
[94] Fix | Delete
line = self.text.get("insert linestart", "insert")
[95] Fix | Delete
i = len(line)
[96] Fix | Delete
while i > 0 and line[i-1] in self.wordchars:
[97] Fix | Delete
i = i-1
[98] Fix | Delete
return line[i:]
[99] Fix | Delete
[100] Fix | Delete
if __name__ == '__main__':
[101] Fix | Delete
import unittest
[102] Fix | Delete
unittest.main('idlelib.idle_test.test_autoexpand', verbosity=2)
[103] Fix | Delete
[104] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function