Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: IdleHistory.py
"Implement Idle Shell history mechanism with History class"
[0] Fix | Delete
[1] Fix | Delete
from idlelib.configHandler import idleConf
[2] Fix | Delete
[3] Fix | Delete
class History:
[4] Fix | Delete
''' Implement Idle Shell history mechanism.
[5] Fix | Delete
[6] Fix | Delete
store - Store source statement (called from PyShell.resetoutput).
[7] Fix | Delete
fetch - Fetch stored statement matching prefix already entered.
[8] Fix | Delete
history_next - Bound to <<history-next>> event (default Alt-N).
[9] Fix | Delete
history_prev - Bound to <<history-prev>> event (default Alt-P).
[10] Fix | Delete
'''
[11] Fix | Delete
def __init__(self, text):
[12] Fix | Delete
'''Initialize data attributes and bind event methods.
[13] Fix | Delete
[14] Fix | Delete
.text - Idle wrapper of tk Text widget, with .bell().
[15] Fix | Delete
.history - source statements, possibly with multiple lines.
[16] Fix | Delete
.prefix - source already entered at prompt; filters history list.
[17] Fix | Delete
.pointer - index into history.
[18] Fix | Delete
.cyclic - wrap around history list (or not).
[19] Fix | Delete
'''
[20] Fix | Delete
self.text = text
[21] Fix | Delete
self.history = []
[22] Fix | Delete
self.prefix = None
[23] Fix | Delete
self.pointer = None
[24] Fix | Delete
self.cyclic = idleConf.GetOption("main", "History", "cyclic", 1, "bool")
[25] Fix | Delete
text.bind("<<history-previous>>", self.history_prev)
[26] Fix | Delete
text.bind("<<history-next>>", self.history_next)
[27] Fix | Delete
[28] Fix | Delete
def history_next(self, event):
[29] Fix | Delete
"Fetch later statement; start with ealiest if cyclic."
[30] Fix | Delete
self.fetch(reverse=False)
[31] Fix | Delete
return "break"
[32] Fix | Delete
[33] Fix | Delete
def history_prev(self, event):
[34] Fix | Delete
"Fetch earlier statement; start with most recent."
[35] Fix | Delete
self.fetch(reverse=True)
[36] Fix | Delete
return "break"
[37] Fix | Delete
[38] Fix | Delete
def fetch(self, reverse):
[39] Fix | Delete
'''Fetch statememt and replace current line in text widget.
[40] Fix | Delete
[41] Fix | Delete
Set prefix and pointer as needed for successive fetches.
[42] Fix | Delete
Reset them to None, None when returning to the start line.
[43] Fix | Delete
Sound bell when return to start line or cannot leave a line
[44] Fix | Delete
because cyclic is False.
[45] Fix | Delete
'''
[46] Fix | Delete
nhist = len(self.history)
[47] Fix | Delete
pointer = self.pointer
[48] Fix | Delete
prefix = self.prefix
[49] Fix | Delete
if pointer is not None and prefix is not None:
[50] Fix | Delete
if self.text.compare("insert", "!=", "end-1c") or \
[51] Fix | Delete
self.text.get("iomark", "end-1c") != self.history[pointer]:
[52] Fix | Delete
pointer = prefix = None
[53] Fix | Delete
self.text.mark_set("insert", "end-1c") # != after cursor move
[54] Fix | Delete
if pointer is None or prefix is None:
[55] Fix | Delete
prefix = self.text.get("iomark", "end-1c")
[56] Fix | Delete
if reverse:
[57] Fix | Delete
pointer = nhist # will be decremented
[58] Fix | Delete
else:
[59] Fix | Delete
if self.cyclic:
[60] Fix | Delete
pointer = -1 # will be incremented
[61] Fix | Delete
else: # abort history_next
[62] Fix | Delete
self.text.bell()
[63] Fix | Delete
return
[64] Fix | Delete
nprefix = len(prefix)
[65] Fix | Delete
while 1:
[66] Fix | Delete
pointer += -1 if reverse else 1
[67] Fix | Delete
if pointer < 0 or pointer >= nhist:
[68] Fix | Delete
self.text.bell()
[69] Fix | Delete
if not self.cyclic and pointer < 0: # abort history_prev
[70] Fix | Delete
return
[71] Fix | Delete
else:
[72] Fix | Delete
if self.text.get("iomark", "end-1c") != prefix:
[73] Fix | Delete
self.text.delete("iomark", "end-1c")
[74] Fix | Delete
self.text.insert("iomark", prefix)
[75] Fix | Delete
pointer = prefix = None
[76] Fix | Delete
break
[77] Fix | Delete
item = self.history[pointer]
[78] Fix | Delete
if item[:nprefix] == prefix and len(item) > nprefix:
[79] Fix | Delete
self.text.delete("iomark", "end-1c")
[80] Fix | Delete
self.text.insert("iomark", item)
[81] Fix | Delete
break
[82] Fix | Delete
self.text.see("insert")
[83] Fix | Delete
self.text.tag_remove("sel", "1.0", "end")
[84] Fix | Delete
self.pointer = pointer
[85] Fix | Delete
self.prefix = prefix
[86] Fix | Delete
[87] Fix | Delete
def store(self, source):
[88] Fix | Delete
"Store Shell input statement into history list."
[89] Fix | Delete
source = source.strip()
[90] Fix | Delete
if len(source) > 2:
[91] Fix | Delete
# avoid duplicates
[92] Fix | Delete
try:
[93] Fix | Delete
self.history.remove(source)
[94] Fix | Delete
except ValueError:
[95] Fix | Delete
pass
[96] Fix | Delete
self.history.append(source)
[97] Fix | Delete
self.pointer = None
[98] Fix | Delete
self.prefix = None
[99] Fix | Delete
[100] Fix | Delete
if __name__ == "__main__":
[101] Fix | Delete
from unittest import main
[102] Fix | Delete
main('idlelib.idle_test.test_idlehistory', verbosity=2, exit=False)
[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