1+ "Implement Idle Shell history mechanism with History class"
2+
13from idlelib .configHandler import idleConf
24
35class History :
6+ ''' Implement Idle Shell history mechanism.
47
8+ store - Store source statement (called from PyShell.resetoutput).
9+ fetch - Fetch stored statement matching prefix already entered.
10+ history_next - Bound to <<history-next>> event (default Alt-N).
11+ history_prev - Bound to <<history-prev>> event (default Alt-P).
12+ '''
513 def __init__ (self , text , output_sep = "\n " ):
14+ '''Initialize data attributes and bind event methods.
15+
16+ .text - Idle wrapper of tk Text widget, with .bell().
17+ .history - source statements, possibly with multiple lines.
18+ .prefix - source already entered at prompt; filters history list.
19+ .pointer - index into history.
20+ .cyclic - wrap around history list (or not).
21+ '''
622 self .text = text
723 self .history = []
8- self .history_prefix = None
9- self .history_pointer = None
24+ self .prefix = None
25+ self .pointer = None
1026 self .output_sep = output_sep
1127 self .cyclic = idleConf .GetOption ("main" , "History" , "cyclic" , 1 , "bool" )
1228 text .bind ("<<history-previous>>" , self .history_prev )
1329 text .bind ("<<history-next>>" , self .history_next )
1430
1531 def history_next (self , event ):
16- self .history_do (0 )
32+ "Fetch later statement; start with ealiest if cyclic."
33+ self .fetch (reverse = False )
1734 return "break"
1835
1936 def history_prev (self , event ):
20- self .history_do (1 )
37+ "Fetch earlier statement; start with most recent."
38+ self .fetch (reverse = True )
2139 return "break"
2240
2341 def _get_source (self , start , end ):
@@ -30,21 +48,22 @@ def _put_source(self, where, source):
3048 output = self .output_sep .join (source .split ("\n " ))
3149 self .text .insert (where , output )
3250
33- def history_do (self , reverse ):
51+ def fetch (self , reverse ):
52+ "Fetch statememt and enter into text at cursor."
3453 nhist = len (self .history )
35- pointer = self .history_pointer
36- prefix = self .history_prefix
54+ pointer = self .pointer
55+ prefix = self .prefix
3756 if pointer is not None and prefix is not None :
3857 if self .text .compare ("insert" , "!=" , "end-1c" ) or \
3958 self ._get_source ("iomark" , "end-1c" ) != self .history [pointer ]:
4059 pointer = prefix = None
4160 if pointer is None or prefix is None :
4261 prefix = self ._get_source ("iomark" , "end-1c" )
4362 if reverse :
44- pointer = nhist
63+ pointer = nhist # will be decremented
4564 else :
4665 if self .cyclic :
47- pointer = - 1
66+ pointer = - 1 # will be incremented
4867 else :
4968 self .text .bell ()
5069 return
@@ -72,10 +91,11 @@ def history_do(self, reverse):
7291 self .text .mark_set ("insert" , "end-1c" )
7392 self .text .see ("insert" )
7493 self .text .tag_remove ("sel" , "1.0" , "end" )
75- self .history_pointer = pointer
76- self .history_prefix = prefix
94+ self .pointer = pointer
95+ self .prefix = prefix
7796
78- def history_store (self , source ):
97+ def store (self , source ):
98+ "Store Shell input statement into history list."
7999 source = source .strip ()
80100 if len (source ) > 2 :
81101 # avoid duplicates
@@ -84,5 +104,5 @@ def history_store(self, source):
84104 except ValueError :
85105 pass
86106 self .history .append (source )
87- self .history_pointer = None
88- self .history_prefix = None
107+ self .pointer = None
108+ self .prefix = None
0 commit comments