def __init__(self
, text
, output_sep
= "\n"):
self
.history_prefix
= None
self
.history_pointer
= None
self
.output_sep
= output_sep
text
.bind("<<history-previous>>", self
.history_prev
)
text
.bind("<<history-next>>", self
.history_next
)
def history_next(self
, event
):
def history_prev(self
, event
):
def _get_source(self
, start
, end
):
# Get source code from start index to end index. Lines in the
# text control may be separated by sys.ps2 .
lines
= self
.text
.get(start
, end
).split(self
.output_sep
)
def _put_source(self
, where
, source
):
output
= self
.output_sep
.join(source
.split("\n"))
self
.text
.insert(where
, output
)
def history_do(self
, reverse
):
nhist
= len(self
.history
)
pointer
= self
.history_pointer
prefix
= self
.history_prefix
if pointer
is not None and prefix
is not None:
if self
.text
.compare("insert", "!=", "end-1c") or \
self
._get
_source
("iomark", "end-1c") != self
.history
[pointer
]:
if pointer
is None or prefix
is None:
prefix
= self
._get
_source
("iomark", "end-1c")
if pointer
< 0 or pointer
>= nhist
:
if self
._get
_source
("iomark", "end-1c") != prefix
:
self
.text
.delete("iomark", "end-1c")
self
._put
_source
("iomark", prefix
)
item
= self
.history
[pointer
]
if item
[:nprefix
] == prefix
and len(item
) > nprefix
:
self
.text
.delete("iomark", "end-1c")
self
._put
_source
("iomark", item
)
self
.text
.mark_set("insert", "end-1c")
self
.text
.tag_remove("sel", "1.0", "end")
self
.history_pointer
= pointer
self
.history_prefix
= prefix
def history_store(self
, source
):
self
.history
.remove(source
)
self
.history
.append(source
)
self
.history_pointer
= None
self
.history_prefix
= None
self
.text
.tag_remove("sel", "1.0", "end")
self
.text
.delete("iomark", "end-1c")
self
.text
.mark_set("insert", "end-1c")
self
.text
.insert("insert", s
)