jpayne@68: "Implement Idle Shell history mechanism with History class" jpayne@68: jpayne@68: from idlelib.config import idleConf jpayne@68: jpayne@68: jpayne@68: class History: jpayne@68: ''' Implement Idle Shell history mechanism. jpayne@68: jpayne@68: store - Store source statement (called from pyshell.resetoutput). jpayne@68: fetch - Fetch stored statement matching prefix already entered. jpayne@68: history_next - Bound to <> event (default Alt-N). jpayne@68: history_prev - Bound to <> event (default Alt-P). jpayne@68: ''' jpayne@68: def __init__(self, text): jpayne@68: '''Initialize data attributes and bind event methods. jpayne@68: jpayne@68: .text - Idle wrapper of tk Text widget, with .bell(). jpayne@68: .history - source statements, possibly with multiple lines. jpayne@68: .prefix - source already entered at prompt; filters history list. jpayne@68: .pointer - index into history. jpayne@68: .cyclic - wrap around history list (or not). jpayne@68: ''' jpayne@68: self.text = text jpayne@68: self.history = [] jpayne@68: self.prefix = None jpayne@68: self.pointer = None jpayne@68: self.cyclic = idleConf.GetOption("main", "History", "cyclic", 1, "bool") jpayne@68: text.bind("<>", self.history_prev) jpayne@68: text.bind("<>", self.history_next) jpayne@68: jpayne@68: def history_next(self, event): jpayne@68: "Fetch later statement; start with ealiest if cyclic." jpayne@68: self.fetch(reverse=False) jpayne@68: return "break" jpayne@68: jpayne@68: def history_prev(self, event): jpayne@68: "Fetch earlier statement; start with most recent." jpayne@68: self.fetch(reverse=True) jpayne@68: return "break" jpayne@68: jpayne@68: def fetch(self, reverse): jpayne@68: '''Fetch statement and replace current line in text widget. jpayne@68: jpayne@68: Set prefix and pointer as needed for successive fetches. jpayne@68: Reset them to None, None when returning to the start line. jpayne@68: Sound bell when return to start line or cannot leave a line jpayne@68: because cyclic is False. jpayne@68: ''' jpayne@68: nhist = len(self.history) jpayne@68: pointer = self.pointer jpayne@68: prefix = self.prefix jpayne@68: if pointer is not None and prefix is not None: jpayne@68: if self.text.compare("insert", "!=", "end-1c") or \ jpayne@68: self.text.get("iomark", "end-1c") != self.history[pointer]: jpayne@68: pointer = prefix = None jpayne@68: self.text.mark_set("insert", "end-1c") # != after cursor move jpayne@68: if pointer is None or prefix is None: jpayne@68: prefix = self.text.get("iomark", "end-1c") jpayne@68: if reverse: jpayne@68: pointer = nhist # will be decremented jpayne@68: else: jpayne@68: if self.cyclic: jpayne@68: pointer = -1 # will be incremented jpayne@68: else: # abort history_next jpayne@68: self.text.bell() jpayne@68: return jpayne@68: nprefix = len(prefix) jpayne@68: while 1: jpayne@68: pointer += -1 if reverse else 1 jpayne@68: if pointer < 0 or pointer >= nhist: jpayne@68: self.text.bell() jpayne@68: if not self.cyclic and pointer < 0: # abort history_prev jpayne@68: return jpayne@68: else: jpayne@68: if self.text.get("iomark", "end-1c") != prefix: jpayne@68: self.text.delete("iomark", "end-1c") jpayne@68: self.text.insert("iomark", prefix) jpayne@68: pointer = prefix = None jpayne@68: break jpayne@68: item = self.history[pointer] jpayne@68: if item[:nprefix] == prefix and len(item) > nprefix: jpayne@68: self.text.delete("iomark", "end-1c") jpayne@68: self.text.insert("iomark", item) jpayne@68: break jpayne@68: self.text.see("insert") jpayne@68: self.text.tag_remove("sel", "1.0", "end") jpayne@68: self.pointer = pointer jpayne@68: self.prefix = prefix jpayne@68: jpayne@68: def store(self, source): jpayne@68: "Store Shell input statement into history list." jpayne@68: source = source.strip() jpayne@68: if len(source) > 2: jpayne@68: # avoid duplicates jpayne@68: try: jpayne@68: self.history.remove(source) jpayne@68: except ValueError: jpayne@68: pass jpayne@68: self.history.append(source) jpayne@68: self.pointer = None jpayne@68: self.prefix = None jpayne@68: jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: from unittest import main jpayne@68: main('idlelib.idle_test.test_history', verbosity=2, exit=False)