1 | n/a | "Implement Idle Shell history mechanism with History class" |
---|
2 | n/a | |
---|
3 | n/a | from idlelib.config import idleConf |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | class History: |
---|
7 | n/a | ''' Implement Idle Shell history mechanism. |
---|
8 | n/a | |
---|
9 | n/a | store - Store source statement (called from pyshell.resetoutput). |
---|
10 | n/a | fetch - Fetch stored statement matching prefix already entered. |
---|
11 | n/a | history_next - Bound to <<history-next>> event (default Alt-N). |
---|
12 | n/a | history_prev - Bound to <<history-prev>> event (default Alt-P). |
---|
13 | n/a | ''' |
---|
14 | n/a | def __init__(self, text): |
---|
15 | n/a | '''Initialize data attributes and bind event methods. |
---|
16 | n/a | |
---|
17 | n/a | .text - Idle wrapper of tk Text widget, with .bell(). |
---|
18 | n/a | .history - source statements, possibly with multiple lines. |
---|
19 | n/a | .prefix - source already entered at prompt; filters history list. |
---|
20 | n/a | .pointer - index into history. |
---|
21 | n/a | .cyclic - wrap around history list (or not). |
---|
22 | n/a | ''' |
---|
23 | n/a | self.text = text |
---|
24 | n/a | self.history = [] |
---|
25 | n/a | self.prefix = None |
---|
26 | n/a | self.pointer = None |
---|
27 | n/a | self.cyclic = idleConf.GetOption("main", "History", "cyclic", 1, "bool") |
---|
28 | n/a | text.bind("<<history-previous>>", self.history_prev) |
---|
29 | n/a | text.bind("<<history-next>>", self.history_next) |
---|
30 | n/a | |
---|
31 | n/a | def history_next(self, event): |
---|
32 | n/a | "Fetch later statement; start with ealiest if cyclic." |
---|
33 | n/a | self.fetch(reverse=False) |
---|
34 | n/a | return "break" |
---|
35 | n/a | |
---|
36 | n/a | def history_prev(self, event): |
---|
37 | n/a | "Fetch earlier statement; start with most recent." |
---|
38 | n/a | self.fetch(reverse=True) |
---|
39 | n/a | return "break" |
---|
40 | n/a | |
---|
41 | n/a | def fetch(self, reverse): |
---|
42 | n/a | '''Fetch statememt and replace current line in text widget. |
---|
43 | n/a | |
---|
44 | n/a | Set prefix and pointer as needed for successive fetches. |
---|
45 | n/a | Reset them to None, None when returning to the start line. |
---|
46 | n/a | Sound bell when return to start line or cannot leave a line |
---|
47 | n/a | because cyclic is False. |
---|
48 | n/a | ''' |
---|
49 | n/a | nhist = len(self.history) |
---|
50 | n/a | pointer = self.pointer |
---|
51 | n/a | prefix = self.prefix |
---|
52 | n/a | if pointer is not None and prefix is not None: |
---|
53 | n/a | if self.text.compare("insert", "!=", "end-1c") or \ |
---|
54 | n/a | self.text.get("iomark", "end-1c") != self.history[pointer]: |
---|
55 | n/a | pointer = prefix = None |
---|
56 | n/a | self.text.mark_set("insert", "end-1c") # != after cursor move |
---|
57 | n/a | if pointer is None or prefix is None: |
---|
58 | n/a | prefix = self.text.get("iomark", "end-1c") |
---|
59 | n/a | if reverse: |
---|
60 | n/a | pointer = nhist # will be decremented |
---|
61 | n/a | else: |
---|
62 | n/a | if self.cyclic: |
---|
63 | n/a | pointer = -1 # will be incremented |
---|
64 | n/a | else: # abort history_next |
---|
65 | n/a | self.text.bell() |
---|
66 | n/a | return |
---|
67 | n/a | nprefix = len(prefix) |
---|
68 | n/a | while 1: |
---|
69 | n/a | pointer += -1 if reverse else 1 |
---|
70 | n/a | if pointer < 0 or pointer >= nhist: |
---|
71 | n/a | self.text.bell() |
---|
72 | n/a | if not self.cyclic and pointer < 0: # abort history_prev |
---|
73 | n/a | return |
---|
74 | n/a | else: |
---|
75 | n/a | if self.text.get("iomark", "end-1c") != prefix: |
---|
76 | n/a | self.text.delete("iomark", "end-1c") |
---|
77 | n/a | self.text.insert("iomark", prefix) |
---|
78 | n/a | pointer = prefix = None |
---|
79 | n/a | break |
---|
80 | n/a | item = self.history[pointer] |
---|
81 | n/a | if item[:nprefix] == prefix and len(item) > nprefix: |
---|
82 | n/a | self.text.delete("iomark", "end-1c") |
---|
83 | n/a | self.text.insert("iomark", item) |
---|
84 | n/a | break |
---|
85 | n/a | self.text.see("insert") |
---|
86 | n/a | self.text.tag_remove("sel", "1.0", "end") |
---|
87 | n/a | self.pointer = pointer |
---|
88 | n/a | self.prefix = prefix |
---|
89 | n/a | |
---|
90 | n/a | def store(self, source): |
---|
91 | n/a | "Store Shell input statement into history list." |
---|
92 | n/a | source = source.strip() |
---|
93 | n/a | if len(source) > 2: |
---|
94 | n/a | # avoid duplicates |
---|
95 | n/a | try: |
---|
96 | n/a | self.history.remove(source) |
---|
97 | n/a | except ValueError: |
---|
98 | n/a | pass |
---|
99 | n/a | self.history.append(source) |
---|
100 | n/a | self.pointer = None |
---|
101 | n/a | self.prefix = None |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | if __name__ == "__main__": |
---|
105 | n/a | from unittest import main |
---|
106 | n/a | main('idlelib.idle_test.test_history', verbosity=2, exit=False) |
---|