diff options
author | Guido van Rossum <guido@python.org> | 1999-06-02 12:06:47 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1999-06-02 12:06:47 (GMT) |
commit | 1709041e73d93b7c6ad3c29a76121b52aeca5c32 (patch) | |
tree | bfbcb49d1a80b28b61256c672f5cad1c23cd038b /Tools/idle | |
parent | 808fa4993e1d65c42d5fd86af4230fd3540ad9a1 (diff) | |
download | cpython-1709041e73d93b7c6ad3c29a76121b52aeca5c32.zip cpython-1709041e73d93b7c6ad3c29a76121b52aeca5c32.tar.gz cpython-1709041e73d93b7c6ad3c29a76121b52aeca5c32.tar.bz2 |
Changes by Mark Hammond: (1) support optional output_sep argument to
the constructor so he can eliminate the sys.ps2 that PythonWin leaves
in the source; (2) remove duplicate history items.
Diffstat (limited to 'Tools/idle')
-rw-r--r-- | Tools/idle/IdleHistory.py | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/Tools/idle/IdleHistory.py b/Tools/idle/IdleHistory.py index 3094173..aa41b73 100644 --- a/Tools/idle/IdleHistory.py +++ b/Tools/idle/IdleHistory.py @@ -2,11 +2,12 @@ import string class History: - def __init__(self, text): + def __init__(self, text, output_sep = "\n"): self.text = text self.history = [] 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) @@ -18,16 +19,26 @@ class History: self.history_do(1) return "break" + 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 = string.split(self.text.get(start, end), self.output_sep) + return string.join(lines, "\n") + + def _put_source(self, where, source): + output = string.join(string.split(source, "\n"), self.output_sep) + 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.text.get("iomark", "end-1c") != self.history[pointer]: + self._get_source("iomark", "end-1c") != self.history[pointer]: pointer = prefix = None if pointer is None or prefix is None: - prefix = self.text.get("iomark", "end-1c") + prefix = self._get_source("iomark", "end-1c") if reverse: pointer = nhist else: @@ -40,15 +51,15 @@ class History: pointer = pointer + 1 if pointer < 0 or pointer >= nhist: self.text.bell() - if self.text.get("iomark", "end-1c") != prefix: + if self._get_source("iomark", "end-1c") != prefix: self.text.delete("iomark", "end-1c") - self.text.insert("iomark", prefix) + self._put_source("iomark", prefix) pointer = prefix = None break item = self.history[pointer] if item[:nprefix] == prefix and len(item) > nprefix: self.text.delete("iomark", "end-1c") - self.text.insert("iomark", item) + self._put_source("iomark", item) break self.text.mark_set("insert", "end-1c") self.text.see("insert") @@ -59,6 +70,11 @@ class History: def history_store(self, source): source = string.strip(source) if len(source) > 2: + # avoid duplicates + try: + self.history.remove(source) + except ValueError: + pass self.history.append(source) self.history_pointer = None self.history_prefix = None |