-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnugo-frolic.el
509 lines (474 loc) · 18.5 KB
/
gnugo-frolic.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
;;; gnugo-frolic.el --- gametree in a buffer -*- lexical-binding: t -*-
;; Copyright (C) 2014-2023 Free Software Foundation, Inc.
;; Author: Thien-Thi Nguyen <[email protected]>
;; Maintainer: [email protected]
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(require 'gnugo)
(require 'ascii-art-to-unicode) ; for `aa2u'
(defvar gnugo-frolic-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map)
(mapc (lambda (pair)
(define-key map (car pair) (cdr pair)))
'(("q" . gnugo-frolic-quit)
("Q" . gnugo-frolic-quit)
("\C-q" . gnugo-frolic-quit)
("C" . gnugo-frolic-quit) ; like ‘View-kill-and-leave’
("\C-b" . gnugo-frolic-backward-branch)
("\C-f" . gnugo-frolic-forward-branch)
("\C-p" . gnugo-frolic-previous-move)
("\C-n" . gnugo-frolic-next-move)
("t" . gnugo-frolic-tip-move)
("j" . gnugo-frolic-exchange-left)
("J" . gnugo-frolic-rotate-left)
("k" . gnugo-frolic-exchange-right)
("K" . gnugo-frolic-rotate-right)
("\C-m" . gnugo-frolic-set-as-main-line)
("\C-\M-p" . gnugo-frolic-prune-branch)
("o" . gnugo-frolic-return-to-origin)))
map)
"Keymap for GNUGO Frolic mode.")
(defvar gnugo-frolic-parent-buffer nil)
(defvar gnugo-frolic-origin nil)
(define-derived-mode gnugo-frolic-mode special-mode "GNUGO Frolic"
"A special mode for manipulating a GNUGO gametree."
(setq truncate-lines t)
(buffer-disable-undo))
(defun gnugo-frolic-quit ()
"Kill GNUGO Frolic buffer and switch to its parent buffer."
(interactive)
(let ((bye (current-buffer)))
(switch-to-buffer (when (buffer-live-p gnugo-frolic-parent-buffer)
gnugo-frolic-parent-buffer))
(kill-buffer bye)))
(defun gnugo-frolic-return-to-origin ()
"Move point to the board's current position."
(interactive)
(if (not gnugo-frolic-origin)
(message "No origin")
(goto-char gnugo-frolic-origin)
(recenter (- (count-lines (line-beginning-position)
(point-max))))))
;;;###autoload
(defun gnugo-frolic-in-the-leaves ()
"Display the game tree in a *GNUGO Frolic* buffer.
This looks something like:
1 B -- E7 E7 E7 E7
2 W -- K10 K10 K10 K10
3 B -- E2 E2 E2 E2
4 W -- J3 J3 J3 J3
5 B -- A6 A6 A6 A6
6 W -- C9 C9 C9 C9
│
├─────┬─────┐
│ │ │
7 B -- H7 !B8 C8 C8
│
├─────┐
│ │
8 W -- D9 D9 D9 E9
9 B -- H8 H8
10 W -- PASS PASS
11 B -- H5 PASS
12 W -- PASS
13 B -- *PASS
with 0, 1, ... N (in this case N is 3) in the header line
to indicate the branches. Branch 0 is the \"main line\".
Point (* in this example) indicates the current position,
\"!\" indicates comment properties (e.g., B8, branch 1),
and moves not actually on the game tree (e.g., E7, branch 3)
are dimmed. Type \\[describe-mode] in that buffer for details."
(interactive)
(let* ((buf (get-buffer-create (concat (gnugo-get :diamond)
"*GNUGO Frolic*")))
(from (or gnugo-frolic-parent-buffer
(current-buffer)))
;; todo: use defface once we finally succumb to ‘customize’
(dimmed-node-face (list :inherit 'default
:foreground "gray50"))
(tree (gnugo-get :sgf-gametree))
(ends (copy-sequence (gnugo--tree-ends tree)))
(mnum (gnugo--tree-mnum tree))
(seen (gnugo--mkht))
(soil (gnugo--mkht))
(width (length ends))
(lanes (number-sequence 0 (1- width)))
(monkey (gnugo-get :monkey))
(as-pos (gnugo--as-pos-func))
(at (car (aref monkey 0)))
(bidx (aref monkey 1))
(valid (cl-map 'vector (lambda (end)
(gethash (car end) mnum))
ends))
(max-move-num (apply 'max (append valid nil)))
(inhibit-read-only t)
finish)
(cl-flet
((on (node)
(gethash node seen))
(emph (s face)
(propertize s 'face face))
(fsi (properties fmt &rest args)
(insert (apply 'propertize
(apply 'format fmt args)
properties))))
;; breathe in
(cl-loop
for bx below width
do (cl-loop
with fork
for node in (aref ends bx)
do (if (setq fork (on node))
(cl-flet
((tip-p (bix)
;; todo: ignore non-"move" nodes
(eq node (car (aref ends bix))))
(link (other)
(cl-pushnew other (gethash node soil))))
(unless (tip-p bx)
(unless (tip-p fork)
(link fork))
(link bx)))
(puthash node bx seen))
until fork))
;; breathe out
(switch-to-buffer buf)
(gnugo-frolic-mode)
(erase-buffer)
(setq header-line-format
(let ((full (concat
(make-string 11 ?\s)
(mapconcat (lambda (n)
(format "%-5s" n))
lanes
" "))))
`((:eval
(funcall
,(lambda ()
(cl-flet
((sp (w) (propertize
" " 'display
`(space :width ,w))))
(concat
(when (eq 'left scroll-bar-mode)
(let ((w (or scroll-bar-width
(frame-parameter
nil 'scroll-bar-width)))
(cw (frame-char-width)))
(sp (if w
(/ w cw)
2))))
(let ((fc (fringe-columns 'left t)))
(unless (zerop fc)
(sp fc)))
(condition-case nil
(substring full (window-hscroll))
(error ""))))))))))
(set (make-local-variable 'gnugo-frolic-parent-buffer) from)
(set (make-local-variable 'gnugo-state)
(buffer-local-value 'gnugo-state from))
(cl-loop
with props
for n ; move number
from max-move-num downto 1
do (setq props (list 'n n))
do
(cl-loop
with (move forks br)
initially (progn
(goto-char (point-min))
(fsi props
"%3d %s -- "
n (aref ["W" "B"] (logand 1 n))))
for bx below width
do (let* ((node (unless (< (aref valid bx) n)
;; todo: ignore non-"move" nodes
(pop (aref ends bx))))
(zow `(bx ,bx ,@props))
(ok (when node
(= bx (on node))))
(comment (when ok
(gnugo-aqr :C node)))
(s (cond ((not node) "")
((not (setq move (gnugo--move-prop node))) "-")
(t (funcall as-pos (cdr move))))))
(when comment
(push comment zow)
(push 'help-echo zow))
(when (and ok (setq br (gethash node soil)))
(push (cons bx (sort br '<))
forks))
(fsi zow
"%c%-5s"
(if comment ?! ?\s)
(cond ((and (eq at node)
(or ok (= bx bidx)))
(when (= bx bidx)
(setq finish (point-marker)))
(emph s (list :inherit 'default
:foreground (frame-parameter
nil 'cursor-color))))
((not ok)
(emph s dimmed-node-face))
(t s))))
finally do
(when (progn (fsi props "\n")
(setq forks (nreverse forks)))
(let* ((margin (make-string 11 ?\s))
(heads (mapcar #'car forks))
(tails (mapcar #'cdr forks)))
(cl-flet*
((spaced (lanes func)
(mapconcat func lanes " "))
;; live to play ~ ~ ()
;; play to learn (+) (-) . o O
;; learn to live --ttn .M. _____U
(dashed (lanes func) ;;; _____ ^^^^
(mapconcat func lanes "-----"))
(cnxn (lanes set)
(spaced lanes (lambda (bx)
(if (memq bx set)
"|"
" "))))
(pad-unless (condition)
(if condition
""
" "))
(edge (set)
(insert margin
(cnxn lanes set)
"\n")))
(edge heads)
(cl-loop
with bef
for ls on forks
do (let* ((one (car ls))
(yes (append
;; "aft" heads
(mapcar 'car (cdr ls))
;; ‘bef’ tails
(apply 'append (mapcar 'cdr bef))))
(ord (sort one '<))
(beg (car ord))
(end (car (last ord))))
(cl-flet
((also (b e) (cnxn (number-sequence b e)
yes)))
(insert
margin
(also 0 (1- beg))
(pad-unless (zerop beg))
(dashed (number-sequence beg end)
(lambda (bx)
(cond ((memq bx ord) "+")
((memq bx yes) "|")
(t "-"))))
(pad-unless (>= end width))
(also (1+ end) (1- width))
"\n"))
(push one bef)))
(edge (apply 'append tails))
(aa2u (line-beginning-position
(- (1+ (length forks))))
(point))))))))
(when finish
(set (make-local-variable 'gnugo-frolic-origin) finish)
(gnugo-frolic-return-to-origin))))
(defun gnugo--awake (how)
;; Valid HOW elements:
;; require-valid-branch
;; (line . numeric)
;; (line . move-string)
;; (omit . [VAR...])
;; Invalid elements blissfully ignored. :-D
(let* ((tree (gnugo-get :sgf-gametree))
(ends (gnugo--tree-ends tree))
(width (length ends))
(monkey (gnugo-get :monkey))
(line (cl-case (gnugo-aqr 'line how)
(numeric
(count-lines (point-min) (line-beginning-position)))
(move-string
(save-excursion
(when (re-search-backward "^ *[0-9]+ [BW]" nil t)
(match-string 0))))
(t nil)))
(col (current-column))
(a (unless (> 10 col)
(let ((try (/ (- col 10)
6)))
(unless (<= width try)
try))))
(rv (list a)))
(when (memq 'require-valid-branch how)
(unless a
(user-error "No branch here")))
(cl-loop
with omit = (gnugo-aqr 'omit how)
for (name . value) in `((line . ,line)
(bidx . ,(aref monkey 1))
(monkey . ,monkey)
(width . ,width)
(ends . ,ends)
(tree . ,tree))
do (unless (memq name omit)
(push value rv)))
rv))
(defmacro gnugo--awakened (how &rest body)
(declare (indent 1))
`(cl-destructuring-bind
,(cl-loop
with omit = (gnugo-aqr 'omit how)
with ls = (list 'a)
for name in '(line bidx monkey
width ends
tree)
do (unless (memq name omit)
(push name ls))
finally return ls)
(gnugo--awake ',how)
,@body))
(defsubst gnugo--move-to-bcol (bidx)
(move-to-column (+ 10 (* 6 bidx))))
(defun gnugo--swiz (direction &optional blunt)
(gnugo--awakened (require-valid-branch
(omit tree)
(line . numeric))
(let* ((b (cond ((numberp blunt)
(unless (and (< -1 blunt)
(< blunt width))
(user-error "No such branch: %s" blunt))
blunt)
(t (mod (+ direction a) width))))
(flit (if blunt (lambda (n)
(cond ((= n a) b)
((= n b) a)
(t n)))
(lambda (n)
(mod (+ direction n) width))))
(was (copy-sequence ends))
(new-bidx (funcall flit bidx)))
(cl-loop
for bx below width
do (aset ends (funcall flit bx)
(aref was bx)))
(unless (= new-bidx bidx)
(aset monkey 1 new-bidx))
(gnugo-frolic-in-the-leaves)
(goto-char (point-min))
(forward-line line)
(gnugo--move-to-bcol b))))
(defun gnugo-frolic-exchange-left ()
"Exchange the current branch with the one to its left."
(interactive)
(gnugo--swiz -1 t))
(defun gnugo-frolic-rotate-left ()
"Rotate all branches left."
(interactive)
(gnugo--swiz -1))
(defun gnugo-frolic-exchange-right ()
"Exchange the current branch with the one to its right."
(interactive)
(gnugo--swiz 1 t))
(defun gnugo-frolic-rotate-right ()
"Rotate all branches right."
(interactive)
(gnugo--swiz 1))
(defun gnugo-frolic-set-as-main-line ()
"Make the current branch the main line."
(interactive)
(gnugo--swiz nil 0))
(defun gnugo-frolic-prune-branch ()
"Remove the current branch from the gametree.
This fails if there is only one branch in the tree.
This fails if the monkey is on the current branch
\(a restriction that will probably be lifted Real Soon Now\)."
(interactive)
(gnugo--awakened (require-valid-branch
(line . move-string))
;; todo: define meaningful eviction semantics; remove restriction
(when (= a bidx)
(user-error "Cannot prune with monkey on branch"))
(when (= 1 width)
(user-error "Cannot prune last remaining branch"))
(let ((new (append ends nil)))
;; Explicit ignorance avoids byte-compiler warning.
(ignore (pop (nthcdr a new)))
(gnugo--set-tree-ends tree new))
(when (< a bidx)
(aset monkey 1 (cl-decf bidx)))
(gnugo-frolic-in-the-leaves)
(when line
(goto-char (point-min))
(search-forward line)
(gnugo--move-to-bcol (min a (- width 2))))))
(defun gnugo--sideways (backwards n)
(gnugo--awakened ((omit tree ends monkey bidx line))
(gnugo--move-to-bcol (mod (if backwards
(- (or a width) n)
(+ (or a -1) n))
width))))
(defun gnugo-frolic-backward-branch (&optional n)
"Move backward N (default 1) branches."
(interactive "p")
(gnugo--sideways t n))
(defun gnugo-frolic-forward-branch (&optional n)
"Move forward N (default 1) branches."
(interactive "p")
(gnugo--sideways nil n))
(defun gnugo--vertical (n direction)
(when (> 0 n)
(setq n (- n)
direction (- direction)))
(gnugo--awakened ((line . numeric)
(omit tree ends width monkey bidx))
(let ((stop (if (> 0 direction)
0
(max 0 (1- (count-lines (point-min)
(point-max))))))
(col (unless a
(current-column))))
(cl-loop
while (not (= line stop))
do (cl-loop
do (progn
(forward-line direction)
(cl-incf line direction))
until (get-text-property (point) 'n))
until (zerop (cl-decf n)))
(if a
(gnugo--move-to-bcol a)
(move-to-column col)))))
(defun gnugo-frolic-previous-move (&optional n)
"Move to the Nth (default 1) previous move."
(interactive "p")
(gnugo--vertical n -1))
(defun gnugo-frolic-next-move (&optional n)
"Move to the Nth (default 1) next move."
(interactive "p")
(gnugo--vertical n 1))
(defun gnugo-frolic-tip-move ()
"Move to the tip of the current branch."
(interactive)
(gnugo--awakened ((omit line bidx monkey width)
require-valid-branch)
(goto-char (point-max))
(let ((mnum (gnugo--tree-mnum tree))
(node (car (aref ends a))))
(re-search-backward (format "^%3d" (gethash node mnum)))
(gnugo--move-to-bcol a))))
;;;---------------------------------------------------------------------------
;;; that's it
(provide 'gnugo-frolic)
;;; gnugo-frolic.el ends here