Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 1 | class History: |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 3 | def __init__(self, text, output_sep = "\n"): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 4 | self.text = text |
| 5 | self.history = [] |
| 6 | self.history_prefix = None |
| 7 | self.history_pointer = None |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 8 | self.output_sep = output_sep |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 9 | 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 Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 20 | 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örwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 23 | lines = self.text.get(start, end).split(self.output_sep) |
| 24 | return "\n".join(lines) |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 25 | |
| 26 | def _put_source(self, where, source): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 27 | output = self.output_sep.join(source.split("\n")) |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 28 | self.text.insert(where, output) |
| 29 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 30 | 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 Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 36 | self._get_source("iomark", "end-1c") != self.history[pointer]: |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 37 | pointer = prefix = None |
| 38 | if pointer is None or prefix is None: |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 39 | prefix = self._get_source("iomark", "end-1c") |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 40 | 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 Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 52 | if self._get_source("iomark", "end-1c") != prefix: |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 53 | self.text.delete("iomark", "end-1c") |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 54 | self._put_source("iomark", prefix) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 55 | 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 Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 60 | self._put_source("iomark", item) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 61 | 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örwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 69 | source = source.strip() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 70 | if len(source) > 2: |
Guido van Rossum | 1709041 | 1999-06-02 12:06:47 +0000 | [diff] [blame] | 71 | # avoid duplicates |
| 72 | try: |
| 73 | self.history.remove(source) |
| 74 | except ValueError: |
| 75 | pass |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 76 | self.history.append(source) |
| 77 | self.history_pointer = None |
| 78 | self.history_prefix = None |
| 79 | |
| 80 | def recall(self, s): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 81 | s = s.strip() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 82 | 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") |