1 prototypes define: #NavigationHistory
4 #pastVisits -> ExtensibleSequence new.
5 "Remembered visits in sequential, historical order."
6 #futureVisits -> ExtensibleSequence new.
7 "Remembered visits when navigation proceeds backwards."
9 "All visits this NavigationHistory has ever touched."
10 #rememberTest -> [| :_ | True].
11 "Answers whether a particular object, when visited, should be remembered."
14 nh@(NavigationHistory traits) new
16 resend `>> [clear. rememberTest := [| :_ | True]. ]
19 nh@(NavigationHistory traits) clear
22 nh allVisits := nh allVisits new.
27 nh@(NavigationHistory traits) clearFuture
29 nh futureVisits := nh futureVisits new.
32 nh@(NavigationHistory traits) clearPast
34 nh pastVisits := nh pastVisits new.
37 nh@(NavigationHistory traits) hasFuture
39 nh futureVisits isNotEmpty
42 nh@(NavigationHistory traits) hasPast
44 nh pastVisits isNotEmpty
47 nh@(NavigationHistory traits) shouldRemember: obj
49 (nh rememberTest applyWith: obj) not
52 nh@(NavigationHistory traits) remember: obj
54 nh allVisits remove: obj ifAbsent: [].
55 nh allVisits add: obj.
58 nh@(NavigationHistory traits) next
59 "Answer the closest visit from the future. Fail if the future is empty."
64 nh@(NavigationHistory traits) previous
65 "Answer the closest visit from the past. Fail if the past is empty."
70 nh@(NavigationHistory traits) do: block
72 (nh shouldRemember: nh here) ifFalse: [block applyWith: nh here].
73 nh allVisits do: block
76 nh@(NavigationHistory traits) goBack
79 [(nh shouldRemember: nh here) ifTrue:
80 [nh futureVisits addFirst: nh here].
81 nh here := nh pastVisits removeLast].
84 nh@(NavigationHistory traits) goBackTo: obj
86 [nh here = obj \/ [nh hasPast not]] whileFalse: [nh goBack]
89 nh@(NavigationHistory traits) goForward
92 [(nh shouldRemember: nh here) ifTrue:
93 [nh pastVisits addLast: nh here].
94 nh here := nh futureVisits removeFirst].
97 nh@(NavigationHistory traits) goForwardTo: obj
99 [nh here = obj \/ [nh hasFuture not]] whileFalse: [nh goForward]
102 nh@(NavigationHistory traits) visit: obj
103 "Make the argument the current object of the history.
104 The prior current object becomes the last object of the 'past' queue.
105 The 'future' queue is erased, unless the visit is the first future element."
107 nh here = obj ifFalse:
108 [nh here isNotNil /\ [nh shouldRemember: nh here] ifTrue:
109 [nh pastVisits addLast: here].
111 (nh shouldRemember: obj) ifTrue: [nh remember: obj].
113 [nh futureVisits first = nh here
114 ifTrue: [nh futureVisits removeFirst]
115 ifFalse: [nh clearFuture]].
119 nh@(NavigationHistory traits) hasVisited: obj
121 nh allVisits includes: obj
124 prototypes define: #Navigator
125 "This is a very basic object navigator, for playing around and ease of use
126 at the command line."
128 #history -> (NavigationHistory new `>> [remember: here. ]).
129 "The previous objects inspected into; the series of contexts followed."
130 #namespace -> (Namespace clone `>> [addDelegate: lobby. ])
131 "The namespace in which expressions are evaluated."}.
133 i@(Navigator traits) newOn: obj
134 [i clone `setting: #{#history} to: {i history new `>> [remember: obj. ]}].
136 _@(Namespace traits) inspect: obj
138 i := (Navigator newOn: obj).
139 here inform: 'You are in a twisty little maze of objects, all alike.'.
143 i@(Navigator traits) help &target: r
145 (r ifNil: [Console]) writer
146 ; 'The Navigator is an extension to the evaluator which allows "travelling" and keeps a navigable travel-history.\n'
147 ; 'here - Answers the current location.\n'
148 ; 'go: address - Visits the object at the slot-name or index of \'here\'.\n'
149 ; 'slots - Shows the current slots here.\n'
150 ; 'indexedSlots - Shows the current array slots here, if any.\n'
151 ; 'parents - Shows the current parent slots here.\n'
152 ; 'visit: obj - Visits the mentioned object.\n'
153 ; 'history - Shows the history of visited objects.'
154 ; 'back - Goes back one step in the history.\n'
155 ; 'back: N - Goes back N steps along the history.\n'
156 ; 'backToStart - Goes back to the start of the history.\n'
157 ; 'navigator - Answers the navigator itself.\n'
158 ; 'close - Exits the Navigator.\n'
159 ; 'help - Prints this helpful message.\n'.
162 i@(Navigator traits) last
165 i@(Navigator traits) here
168 i@(Navigator traits) slots
171 i@(Navigator traits) indexedSlots
174 (i here is: Sequence)
178 [| :each :index | s ; index printString ; ': ' ; each printString ; '.\n'].
183 i@(Navigator traits) parents
184 [i here _map delegates].
186 i@(Navigator traits) visit: obj
188 i history visit: obj.
192 i@(Navigator traits) visit: _@Nil
193 ['Nil: nothing done.'].
195 i@(Navigator traits) go: slotName@(Symbol traits)
196 [i visit: (i here atSlotNamed: slotName)].
198 i@(Navigator traits) go: slotName
199 [i go: slotName intern].
201 i@(Navigator traits) go: index@(Integer traits)
202 [i visit: (i here at: index)].
204 i@(Navigator traits) back
205 "Moves the last context off the history Stack and makes it it."
210 i@(Navigator traits) back: n
211 "Moves the last n contexts off the history Stack and makes the last one here."
213 n timesRepeat: [i history goBack]
216 i@(Navigator traits) backToStart
217 "Clears the history stack and returns the navigator to the original object
220 i history goBackTo: i history pastVisits first.
223 i@(Navigator traits) close
224 "Sever all the connections that make the Navigator work, for memory safety,
225 and return the Navigator."