blob: 46e70e12248473154ffc5b46094d818b2d8ac034 [file] [log] [blame]
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00001class History:
Guido van Rossum504b0bf1999-01-02 21:28:54 +00002
Guido van Rossum17090411999-06-02 12:06:47 +00003 def __init__(self, text, output_sep = "\n"):
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00004 self.text = text
5 self.history = []
6 self.history_prefix = None
7 self.history_pointer = None
Guido van Rossum17090411999-06-02 12:06:47 +00008 self.output_sep = output_sep
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00009 text.bind("<<history-previous>>", self.history_prev)
10 text.bind("<<history-next>>", self.history_next)
11
12 def history_next(self, event):
13 self.history_do(0)
14 return "break"
15
16 def history_prev(self, event):
17 self.history_do(1)
18 return "break"
19
Guido van Rossum17090411999-06-02 12:06:47 +000020 def _get_source(self, start, end):
21 # Get source code from start index to end index. Lines in the
22 # text control may be separated by sys.ps2 .
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000023 lines = self.text.get(start, end).split(self.output_sep)
24 return "\n".join(lines)
Guido van Rossum17090411999-06-02 12:06:47 +000025
26 def _put_source(self, where, source):
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000027 output = self.output_sep.join(source.split("\n"))
Guido van Rossum17090411999-06-02 12:06:47 +000028 self.text.insert(where, output)
29
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000030 def history_do(self, reverse):
31 nhist = len(self.history)
32 pointer = self.history_pointer
33 prefix = self.history_prefix
34 if pointer is not None and prefix is not None:
35 if self.text.compare("insert", "!=", "end-1c") or \
Guido van Rossum17090411999-06-02 12:06:47 +000036 self._get_source("iomark", "end-1c") != self.history[pointer]:
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000037 pointer = prefix = None
38 if pointer is None or prefix is None:
Guido van Rossum17090411999-06-02 12:06:47 +000039 prefix = self._get_source("iomark", "end-1c")
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000040 if reverse:
41 pointer = nhist
42 else:
43 pointer = -1
44 nprefix = len(prefix)
45 while 1:
46 if reverse:
47 pointer = pointer - 1
48 else:
49 pointer = pointer + 1
50 if pointer < 0 or pointer >= nhist:
51 self.text.bell()
Guido van Rossum17090411999-06-02 12:06:47 +000052 if self._get_source("iomark", "end-1c") != prefix:
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000053 self.text.delete("iomark", "end-1c")
Guido van Rossum17090411999-06-02 12:06:47 +000054 self._put_source("iomark", prefix)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000055 pointer = prefix = None
56 break
57 item = self.history[pointer]
58 if item[:nprefix] == prefix and len(item) > nprefix:
59 self.text.delete("iomark", "end-1c")
Guido van Rossum17090411999-06-02 12:06:47 +000060 self._put_source("iomark", item)
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000061 break
62 self.text.mark_set("insert", "end-1c")
63 self.text.see("insert")
64 self.text.tag_remove("sel", "1.0", "end")
65 self.history_pointer = pointer
66 self.history_prefix = prefix
67
68 def history_store(self, source):
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000069 source = source.strip()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000070 if len(source) > 2:
Guido van Rossum17090411999-06-02 12:06:47 +000071 # avoid duplicates
72 try:
73 self.history.remove(source)
74 except ValueError:
75 pass
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000076 self.history.append(source)
77 self.history_pointer = None
78 self.history_prefix = None
79
80 def recall(self, s):
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000081 s = s.strip()
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +000082 self.text.tag_remove("sel", "1.0", "end")
83 self.text.delete("iomark", "end-1c")
84 self.text.mark_set("insert", "end-1c")
85 self.text.insert("insert", s)
86 self.text.see("insert")