17
17
(dynamic dynamic-drd)))
19
19
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
;; Abstract stuff for fold tags and switches
20
;; Dynamic movements for fold tags and switches
21
21
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23
23
(tm-define (dynamic-context? t)
24
24
(or (toggle-tag? (tree-label t))
25
25
(switch-tag? (tree-label t))))
27
(tm-define (dynamic-first) (noop))
28
(tm-define (dynamic-previous) (noop))
29
(tm-define (dynamic-next) (noop))
30
(tm-define (dynamic-last) (noop))
32
(tm-define (structured-left)
33
(:context dynamic-context?)
36
(tm-define (structured-right)
37
(:context dynamic-context?)
40
(tm-define (structured-up)
41
(:context dynamic-context?)
44
(tm-define (structured-down)
45
(:context dynamic-context?)
48
(tm-define (structured-first)
49
(:context dynamic-context?)
52
(tm-define (structured-last)
53
(:context dynamic-context?)
56
(tm-define (structured-top)
57
(:context dynamic-context?)
60
(tm-define (structured-bottom)
61
(:context dynamic-context?)
64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65
;; Operations on toggle trees
66
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68
(tm-define (toggle-toggle t)
69
(:synopsis "Toggle a fold/unfold")
70
(tree-assign-node! t (ahash-ref toggle-table (tree-label t))))
27
(tm-define (dynamic-extremal t forwards?)
28
(and-with p (tree-outer t)
29
(dynamic-extremal p forwards?)))
31
(tm-define (dynamic-incremental t forwards?)
32
(and-with p (tree-outer t)
33
(dynamic-incremental p forwards?)))
35
(tm-define (dynamic-first)
36
(dynamic-extremal (focus-tree) #f))
37
(tm-define (dynamic-last)
38
(dynamic-extremal (focus-tree) #t))
39
(tm-define (dynamic-previous)
40
(dynamic-incremental (focus-tree) #f))
41
(tm-define (dynamic-next)
42
(dynamic-incremental (focus-tree) #t))
44
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45
;; Abstract stuff for fold tags and switches
46
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48
(tm-define (structured-horizontal t forwards?)
49
(:require (dynamic-context? t))
50
(dynamic-incremental t forwards?))
52
(tm-define (structured-vertical t downwards?)
53
(:require (dynamic-context? t))
54
(dynamic-incremental t downwards?))
56
(tm-define (structured-extremal t forwards?)
57
(:require (dynamic-context? t))
58
(dynamic-extremal t forwards?))
60
(tm-define (structured-incremental t downwards?)
61
(:require (dynamic-context? t))
62
(dynamic-extremal t downwards?))
72
64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82
74
(tm-define (toggle-second-context? t)
83
75
(toggle-second-tag? (tree-label t)))
77
(tm-define (fold-context? t)
78
(or (folded-tag? (tree-label t)) (unfolded-tag? (tree-label t))))
85
80
(tm-define (make-toggle tag)
87
82
(:synopsis "Insert a 'fold' environment")
88
83
(insert-go-to `(,tag (document "") (document "")) (list 0 0)))
92
(:synopsis "Fold at the current cursor position")
96
(:context toggle-second-context?)
97
(with-innermost t toggle-second-context?
99
(tree-go-to t 0 :start)))
103
(:synopsis "Unfold at the current cursor position")
107
(:context toggle-first-context?)
108
(with-innermost t toggle-first-context?
110
(tree-go-to t 1 :start)))
112
(tm-define (mouse-fold)
114
(:synopsis "Fold using the mouse")
117
(tree-go-to t :start)
120
(tm-define (mouse-unfold)
122
(:synopsis "Unfold using the mouse")
125
(tree-go-to t :start)
128
(tm-define (hidden-variant)
129
(:context toggle-first-context?)
132
(tm-define (hidden-variant)
133
(:context toggle-second-context?)
136
(tm-define (dynamic-first)
137
(:context toggle-context?)
140
(tm-define (dynamic-previous)
141
(:context toggle-context?)
144
(tm-define (dynamic-next)
145
(:context toggle-context?)
148
(tm-define (dynamic-last)
149
(:context toggle-context?)
85
(tm-define (alternate-toggle t)
86
(:require (toggle-context? t))
87
(with i (if (toggle-first-context? t) 1 0)
88
(variant-set t (ahash-ref alternate-table (tree-label t)))
89
(tree-go-to t i :start)))
91
(tm-define (dynamic-extremal t forwards?)
92
(:require (toggle-context? t))
93
(with action (if forwards? alternate-unfold alternate-fold)
96
(tm-define (dynamic-incremental t forwards?)
97
(:require (toggle-context? t))
98
(with action (if forwards? alternate-unfold alternate-fold)
152
101
(tm-define (tree-show-hidden t)
153
102
(:require (toggle-context? t))
103
(alternate-toggle t))
156
105
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157
106
;; Operations on switch trees
185
134
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186
;; Routines on innermost switch
135
;; Basic routines on switches
187
136
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189
138
(tm-define (switch-context? t)
190
139
(switch-tag? (tree-label t)))
192
(tm-define (switch-arity)
193
(with t (tree-innermost switch-context?)
194
(and t (tree-arity t))))
196
(tm-define (switch-valid-child? i)
197
(with t (tree-innermost switch-context?)
198
(and t i (>= i 0) (< i (tree-arity t)))))
200
(tm-define (switch-index . args)
201
(:context switch-context?)
202
(and-let* ((i (if (null? args) :current (car args)))
203
(t (tree-innermost switch-context?))
204
(c (tree-down-index t))
205
(l (- (tree-arity t) 1))
206
(v (switch-last-visible t)))
210
((== i :previous) (max 0 (- c 1)))
211
((== i :next) (min l (+ c 1)))
212
((== i :var-previous) (- c 1))
213
((== i :var-next) (+ c 1))
214
((== i :rotate-backward) (if (= c 0) l (- c 1)))
215
((== i :rotate-forward) (if (= c l) 0 (+ c 1)))
220
(tm-define (switch-to i . args)
221
(set! i (switch-index i))
141
(tm-define (switch-valid-child? t i)
142
(and t i (>= i 0) (< i (tree-arity t))))
144
(tm-define (switch-index t . args)
145
(when (switch-context? t)
146
(and-let* ((i (if (null? args) :current (car args)))
147
(c (tree-down-index t))
148
(l (- (tree-arity t) 1))
149
(v (switch-last-visible t)))
153
((== i :previous) (max 0 (- c 1)))
154
((== i :next) (min l (+ c 1)))
155
((== i :var-previous) (- c 1))
156
((== i :var-next) (+ c 1))
157
((== i :rotate-backward) (if (= c 0) l (- c 1)))
158
((== i :rotate-forward) (if (= c l) 0 (+ c 1)))
163
(tm-define (switch-to t i . args)
164
(set! i (switch-index t i))
222
165
(if (null? args) (set! args '(:start)))
223
(when (switch-valid-child? i)
225
(with-innermost t switch-context?
226
(apply tree-go-to (cons* t i 0 args)))))
228
(tm-define (switch-insert-at i)
229
(set! i (if (== i :end) (switch-arity) (switch-index i)))
230
(with-innermost t switch-context?
231
(when (and (>= i 0) (<= i (tree-arity t)))
232
(let* ((empty (if (tree-in? t (big-switch-tag-list)) '(document "") ""))
233
(v (switch-index :visible)))
234
(tree-insert! t i `((shown ,empty)))
235
(if (tree-in? t (alternative-tag-list))
237
(switch-select (+ v 1)))
238
(tree-go-to t i :start)))))
240
(tm-define (switch-remove-at i)
241
(set! i (switch-index i))
242
(with-innermost t switch-context?
243
(when (and (>= i 0) (< i (tree-arity t)) (> (tree-arity t) 1))
244
(let* ((v (switch-index :visible))
245
(l (- (tree-arity t) 2)))
246
(switch-set-range t (max 0 (- i 1)) (min l (+ i 1)) #t)
248
(tree-go-to t (min i l) :start)
249
(if (tree-in? t (alternative-tag-list))
250
(switch-select (min i l))
251
(switch-select (max 0 (- v 1))))))))
166
(when (switch-valid-child? t i)
168
(apply tree-go-to (cons* t i 0 args))))
170
(tm-define (switch-insert-at t i)
171
(set! i (if (== i :end) (tree-arity t) (switch-index t i)))
172
(when (and (>= i 0) (<= i (tree-arity t)))
173
(let* ((empty (if (tree-in? t (big-switch-tag-list)) '(document "") ""))
174
(v (switch-index t :visible)))
175
(tree-insert! t i `((shown ,empty)))
176
(if (tree-in? t (alternative-tag-list))
178
(switch-select t (+ v 1)))
179
(tree-go-to t i :start))))
181
(tm-define (switch-remove-at t i)
182
(set! i (switch-index t i))
183
(when (and (>= i 0) (< i (tree-arity t)) (> (tree-arity t) 1))
184
(let* ((v (switch-index t :visible))
185
(l (- (tree-arity t) 2)))
186
(switch-set-range t (max 0 (- i 1)) (min l (+ i 1)) #t)
188
(tree-go-to t (min i l) :start)
189
(if (tree-in? t (alternative-tag-list))
190
(switch-select t (min i l))
191
(switch-select t (max 0 (- v 1)))))))
253
193
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254
194
;; Specific types of switches
290
226
(insert-go-to `(,tag (shown (document ""))) '(0 0 0 0))
291
227
(insert-go-to `(,tag (shown "")) '(0 0 0))))
293
(tm-define (dynamic-first)
294
(:context switch-context?)
295
(switch-to :first :start))
297
(tm-define (dynamic-previous)
298
(:context switch-context?)
299
(switch-to :previous :end))
301
(tm-define (dynamic-next)
302
(:context switch-context?)
303
(switch-to :next :start))
305
(tm-define (dynamic-last)
306
(:context switch-context?)
307
(switch-to :last :end))
309
(tm-define (structured-insert forwards?)
310
(:context switch-context?)
311
(switch-insert-at (if forwards? :var-next :current)))
313
(tm-define (structured-insert-up)
314
(:context switch-context?)
315
(switch-insert-at :current))
317
(tm-define (structured-insert-down)
318
(:context switch-context?)
319
(switch-insert-at :var-next))
321
(tm-define (structured-remove forwards?)
322
(:context switch-context?)
323
(with-innermost t switch-context?
324
(with i (if forwards? :current :var-previous)
325
(set! i (switch-index i))
326
(cond ((< i 0) (tree-go-to t :start))
327
((and forwards? (= i (- (tree-arity t) 1))) (tree-go-to t :end))
328
(else (switch-remove-at i))))))
330
(tm-define (hidden-variant)
331
(:context switch-context?)
332
(switch-to :rotate-forward))
334
(tm-define (variant-circulate forward?)
335
(:context switch-context?)
336
(with-innermost t switch-context?
337
(let* ((old (tree-label t))
338
(val (big-switch-tag-list))
339
(rot (list-search-rotate val old))
340
(new (if (and forward? (nnull? rot)) (cadr rot) (cAr rot)))
342
(variant-replace old new)
229
(tm-define (dynamic-extremal t forwards?)
230
(:require (switch-context? t))
232
(switch-to t :last :end)
233
(switch-to t :first :start)))
235
(tm-define (dynamic-incremental t forwards?)
236
(:require (switch-context? t))
238
(switch-to t :next :start)
239
(switch-to t :previous :end)))
241
(tm-define (structured-insert-horizontal t forwards?)
242
(:require (switch-context? t))
243
(switch-insert-at t (if forwards? :var-next :current)))
245
(tm-define (structured-insert-vertical t downwards?)
246
(:require (switch-context? t))
247
(structured-insert-horizontal t downwards?))
249
(tm-define (structured-remove-horizontal t forwards?)
250
(:require (switch-context? t))
251
(with i (if forwards? :current :var-previous)
252
(set! i (switch-index t i))
253
(cond ((< i 0) (tree-go-to t :start))
254
((and forwards? (= i (- (tree-arity t) 1))) (tree-go-to t :end))
255
(else (switch-remove-at t i)))))
257
(tm-define (structured-remove-vertical t downwards?)
258
(:require (switch-context? t))
259
(structured-remove-horizontal t downwards?))
261
(tm-define (alternate-toggle t)
262
(:require (switch-context? t))
263
(switch-to t :rotate-forward))
265
(tm-define (variant-circulate t forward?)
266
(:require (switch-context? t))
267
(with i (switch-index t)
268
(variant-circulate-in t (big-switch-tag-list) forward?)
269
(switch-select t i)))
345
271
(tm-define (tree-show-hidden t)
346
272
(:require (switch-context? t))
347
273
(with i (tree-down-index t)
348
274
(if (tree-is? (tree-ref t i) 'hidden)
349
(tree/switch-select t i))))
275
(switch-select t i))))
351
277
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
352
278
;; Analyzing the environments occurring in folds
422
348
(cond ((== mode :var-last)
423
349
(tree-insert-node! t 0 '(traversed)))
424
350
((in? mode '(:unfold :expand :var-expand :last))
351
(alternate-toggle t))
426
352
((and (pair? mode) (== (car mode) :unfold)
427
353
(fold-matching-env? t (cadr mode)))
354
(alternate-toggle t))))
429
355
((toggle-second-context? t)
430
356
(cond ((== mode :var-last)
432
358
(tree-insert-node! t 0 '(traversed)))
433
359
((in? mode '(:fold :compress :var-compress :first))
360
(alternate-toggle t))
435
361
((and (pair? mode) (== (car mode) :fold)
436
362
(fold-matching-env? t (cadr mode)))
363
(alternate-toggle t))))
438
364
((and (== mode :expand) (switch-context? t))
439
365
(switch-set-range t 0 :last #t))
440
366
((and (== mode :compress) (switch-context? t))