26 |
;;; |
;;; |
27 |
;;; Maintainer: Thien-Thi Nguyen |
;;; Maintainer: Thien-Thi Nguyen |
28 |
;;; |
;;; |
29 |
;;; Rel:standalone-gnugo-el-2-2-6 |
;;; Rel:standalone-gnugo-el-2-2-7 |
30 |
;;; |
;;; |
31 |
;;; Description: Run GNU Go in a buffer. |
;;; Description: Run GNU Go in a buffer. |
32 |
|
|
47 |
;; both komi and handicap. |
;; both komi and handicap. |
48 |
;; |
;; |
49 |
;; To play a stone, move the cursor to the desired vertice and type `SPC' or |
;; To play a stone, move the cursor to the desired vertice and type `SPC' or |
50 |
;; `RET'; to pass, `P' (note: uppercase); to quit, `q'. To undo, place the |
;; `RET'; to pass, `P' (note: uppercase); to quit, `q'; to undo one of your |
51 |
;; cursor on a stone that you played and type `U' (note: uppercase). Other |
;; moves (as well as a possibly intervening move by GNU Go), `u'. To undo |
52 |
;; keybindings are described in the `gnugo-board-mode' documentation, which |
;; back through an arbitrary stone that you played, place the cursor on a |
53 |
;; you may view with the command `describe-mode' (normally `C-h m') in that |
;; stone and type `U' (note: uppercase). Other keybindings are described in |
54 |
;; buffer. The buffer name shows the last move and who is currently to play. |
;; the `gnugo-board-mode' documentation, which you may view with the command |
55 |
;; Capture counts and other info are shown on the mode line immediately |
;; `describe-mode' (normally `C-h m') in that buffer. The buffer name shows |
56 |
;; following the major mode name. |
;; the last move and who is currently to play. Capture counts and other info |
57 |
|
;; are shown on the mode line immediately following the major mode name. |
58 |
;; |
;; |
59 |
;; While GNU Go is pondering its next move, certain commands that rely on its |
;; While GNU Go is pondering its next move, certain commands that rely on its |
60 |
;; assistence will result in a "still waiting" error. Do not be alarmed; that |
;; assistence will result in a "still waiting" error. Do not be alarmed; that |
148 |
;; multiple independent buffers/games |
;; multiple independent buffers/games |
149 |
;; XPM set can be changed on the fly (global and/or local) |
;; XPM set can be changed on the fly (global and/or local) |
150 |
;; font-locking for "X", "O", "[xo]" |
;; font-locking for "X", "O", "[xo]" |
151 |
;; undo by count or by board position |
;; undo by count, by board position, or by "pairs" |
152 |
;; |
;; |
153 |
;; |
;; |
154 |
;; History Predicted |
;; History Predicted |
161 |
;; For users who are not elisp programmers, you can look forward to gradual |
;; For users who are not elisp programmers, you can look forward to gradual |
162 |
;; refinement in 2.x, splitting into gnugo.el and sgf.el in 3.x, and then |
;; refinement in 2.x, splitting into gnugo.el and sgf.el in 3.x, and then |
163 |
;; eventual merging into GNU Emacs for 4.x (if RMS gives it the thumbs-up). |
;; eventual merging into GNU Emacs for 4.x (if RMS gives it the thumbs-up). |
164 |
;; If it is not accepted into Emacs at that time, it will remain in GNU Go. |
;; If it is not accepted into Emacs at that time, a new maintainer will be |
165 |
|
;; sought. In any case, it will no longer be bundled w/ ttn-pers-elisp. |
166 |
|
|
167 |
;;; Code: |
;;; Code: |
168 |
|
|
172 |
;;;--------------------------------------------------------------------------- |
;;;--------------------------------------------------------------------------- |
173 |
;;; Political arts |
;;; Political arts |
174 |
|
|
175 |
(defconst gnugo-version "2.2.6" |
(defconst gnugo-version "2.2.7" |
176 |
"Version of gnugo.el currently loaded. |
"Version of gnugo.el currently loaded. |
177 |
Note that more than two dots in the value indicates \"pre-release\", |
Note that more than two dots in the value indicates \"pre-release\", |
178 |
or \"alpha\" or \"hackers-invited-all-else-beware\"; use at your own risk! |
or \"alpha\" or \"hackers-invited-all-else-beware\"; use at your own risk! |
967 |
To start a game try M-x gnugo." |
To start a game try M-x gnugo." |
968 |
(interactive) |
(interactive) |
969 |
(gnugo-gate t) |
(gnugo-gate t) |
970 |
|
(let ((accept (cdr (gnugo-synchronous-send/return |
971 |
|
(format "play %s PASS" (gnugo-get :user-color)))))) |
972 |
|
(unless (= ?= (aref accept 0)) |
973 |
|
(error accept))) |
974 |
(let ((donep (gnugo-push-move t "PASS")) |
(let ((donep (gnugo-push-move t "PASS")) |
975 |
(buf (current-buffer))) |
(buf (current-buffer))) |
976 |
(let (inhibit-gnugo-refresh) |
(let (inhibit-gnugo-refresh) |
1161 |
(aft-newline-appreciated '(:C :B :W :SZ :PB :PW)) ;;; license |
(aft-newline-appreciated '(:C :B :W :SZ :PB :PW)) ;;; license |
1162 |
(sz (gnugo-get :board-size)) |
(sz (gnugo-get :board-size)) |
1163 |
(tree (gnugo-get :sgf-tree))) |
(tree (gnugo-get :sgf-tree))) |
1164 |
(find-file filename) |
(with-temp-buffer |
1165 |
(erase-buffer) |
(insert "(") |
1166 |
(insert "(") |
(dolist (node (reverse tree)) |
1167 |
(dolist (node (reverse tree)) |
(insert ";") |
1168 |
(insert ";") |
(dolist (prop (reverse node)) |
1169 |
(dolist (prop (reverse node)) |
(let ((name (car prop)) |
1170 |
(let ((name (car prop)) |
(v (cdr prop))) |
1171 |
(v (cdr prop))) |
(insert |
1172 |
(insert |
(if (memq name bef-newline-appreciated) "\n" "") |
1173 |
(if (memq name bef-newline-appreciated) "\n" "") |
(substring (symbol-name name) 1) |
1174 |
(substring (symbol-name name) 1) |
"[" (format "%s" v) "]" |
1175 |
"[" (format "%s" v) "]" |
(if (memq name aft-newline-appreciated) "\n" ""))))) |
1176 |
(if (memq name aft-newline-appreciated) "\n" ""))))) |
(insert ")\n") |
1177 |
(insert ")\n") |
(write-file filename)))) |
|
(save-buffer) |
|
|
(kill-buffer nil))) |
|
1178 |
|
|
1179 |
(defun gnugo-read-sgf-file (filename) |
(defun gnugo-read-sgf-file (filename) |
1180 |
"Load a game tree from FILENAME, a file in SGF format." |
"Load a game tree from FILENAME, a file in SGF format." |
1181 |
(interactive "fSGF file to load: ") |
(interactive "fSGF file to load: ") |
1182 |
(gnugo-command (format "loadsgf %s" (expand-file-name filename)))) |
(gnugo-command (format "loadsgf %s" (expand-file-name filename)))) |
1183 |
|
|
|
(defun gnugo-undo () |
|
|
"Undo one move. Interchange the colors of the two players." |
|
|
(interactive) |
|
|
(gnugo-gate) |
|
|
(gnugo-synchronous-send/return "undo") |
|
|
(gnugo-put :move-history (cdr (gnugo-get :move-history))) |
|
|
(gnugo-put :sgf-tree (cdr (gnugo-get :sgf-tree))) |
|
|
(gnugo-put :user-color (gnugo-get :last-mover)) |
|
|
(gnugo-put :gnugo-color (gnugo-other (gnugo-get :last-mover))) |
|
|
(gnugo-put :last-mover (gnugo-get :gnugo-color)) |
|
|
(gnugo-merge-showboard-results) |
|
|
(gnugo-refresh t) |
|
|
(gnugo-goto-pos (car (cdr (gnugo-get :move-history)))) |
|
|
) |
|
|
|
|
1184 |
(defun gnugo-magic-undo (spec) |
(defun gnugo-magic-undo (spec) |
1185 |
"Undo moves on the GNUGO Board, based on SPEC, a string. |
"Undo moves on the GNUGO Board, based on SPEC, a string. |
1186 |
If SPEC has the form of a board position (begins with a letter), |
If SPEC has the form of a board position (begins with a letter), |
1190 |
moves from the history, signaling an error if the history is exhausted |
moves from the history, signaling an error if the history is exhausted |
1191 |
before finishing. Otherwise, signal \"bad spec\" error. |
before finishing. Otherwise, signal \"bad spec\" error. |
1192 |
|
|
1193 |
Refresh the board for each move undone. Warp the cursor to the next |
Refresh the board for each move undone. If (in the case where SPEC is |
|
to the next-to-last move (which will be the next move undone if |
|
|
the command is executed again). If (in the case where SPEC is |
|
1194 |
a number) after finishing, the color to play is not the user's color, |
a number) after finishing, the color to play is not the user's color, |
1195 |
schedule a move by GNU Go." |
schedule a move by GNU Go." |
1196 |
(gnugo-gate) |
(gnugo-gate) |
1227 |
(gnugo-refresh) ; this |
(gnugo-refresh) ; this |
1228 |
(decf n) ; is |
(decf n) ; is |
1229 |
(sit-for 0))) ; eye candy |
(sit-for 0))) ; eye candy |
1230 |
(gnugo-refresh t) |
(let* ((ulastp (string= (gnugo-get :last-mover) (gnugo-get :user-color))) |
1231 |
(gnugo-goto-pos (car (cdr (gnugo-get :move-history)))) |
(ubpos (funcall (if ulastp 'car 'cadr) (gnugo-get :move-history)))) |
1232 |
(when (string= (gnugo-get :last-mover) (gnugo-get :user-color)) |
(gnugo-put :last-user-bpos (if (and ubpos (not (string= "PASS" ubpos))) |
1233 |
(gnugo-get-move (gnugo-get :gnugo-color)))) |
ubpos |
1234 |
|
(gnugo-get :center-position))) |
1235 |
|
(gnugo-refresh t) |
1236 |
|
(when ulastp |
1237 |
|
(gnugo-get-move (gnugo-get :gnugo-color))))) |
1238 |
|
|
1239 |
|
(defun gnugo-undo-two-moves () |
1240 |
|
"Undo a pair of moves (GNU Go's and yours). |
1241 |
|
However, if you are the last mover, undo only one move. |
1242 |
|
Regardless, after undoing, it is your turn to play again." |
1243 |
|
(interactive) |
1244 |
|
(gnugo-gate) |
1245 |
|
(gnugo-magic-undo (if (string= (gnugo-get :user-color) |
1246 |
|
(gnugo-get :last-mover)) |
1247 |
|
"1" |
1248 |
|
"2"))) |
1249 |
|
|
1250 |
(defun gnugo-display-final-score () |
(defun gnugo-display-final-score () |
1251 |
"Display final score and other info in another buffer (when game over). |
"Display final score and other info in another buffer (when game over). |
1420 |
(let ((hook |
(let ((hook |
1421 |
;; do not elide this binding; `run-hooks' needs it |
;; do not elide this binding; `run-hooks' needs it |
1422 |
(plist-get spec :post-hook))) |
(plist-get spec :post-hook))) |
1423 |
(run-hooks 'hook)))) |
(run-hooks 'hook)))))))) |
|
(gnugo-refresh t))))) |
|
1424 |
|
|
1425 |
;;;--------------------------------------------------------------------------- |
;;;--------------------------------------------------------------------------- |
1426 |
;;; Major mode for interacting with a GNUGO subprocess |
;;; Major mode for interacting with a GNUGO subprocess |
1439 |
|
|
1440 |
R Resign. |
R Resign. |
1441 |
|
|
1442 |
u Undo one move. |
u Run `gnugo-undo-two-moves'. |
1443 |
|
|
1444 |
U Pass to `gnugo-magic-undo' either the board position |
U Pass to `gnugo-magic-undo' either the board position |
1445 |
at point (if no prefix arg), or the prefix arg converted |
at point (if no prefix arg), or the prefix arg converted |
1446 |
to a number. E.g., to undo 16 moves: `C-u C-u U' (see |
to a number. E.g., to undo 16 moves: `C-u C-u U' (see |
1447 |
`universal-argument'); to undo 42 moves: `M-4 M-2 U'. |
`universal-argument'); to undo 42 moves: `M-4 M-2 U'. |
1448 |
|
|
1449 |
C-l Run `gnugo-refresh' to redraw the board. |
C-l Run `gnugo-refresh'. |
1450 |
|
|
1451 |
_ or M-_ Bury the Board buffer (when the boss is near). |
_ or M-_ Bury the Board buffer (when the boss is near). |
1452 |
|
|
1464 |
|
|
1465 |
! Run `gnugo-estimate-score'. |
! Run `gnugo-estimate-score'. |
1466 |
|
|
1467 |
: or ; Run `gnugo-command' (for GTP commands to GNU Go). For |
: or ; Run `gnugo-command' (for GTP commands to GNU Go). |
|
example, `showboard' will draw the board in ascii with |
|
|
a coordinate grid, useful for reference. |
|
1468 |
|
|
1469 |
= Display board position under point (if valid). |
= Display board position under point (if valid). |
1470 |
|
|
1639 |
(gnugo-goto-pos (format "A%d" half)) |
(gnugo-goto-pos (format "A%d" half)) |
1640 |
(forward-char (* 2 (1- half))) |
(forward-char (* 2 (1- half))) |
1641 |
(gnugo-put :last-user-bpos |
(gnugo-put :last-user-bpos |
1642 |
(get-text-property (point) 'gnugo-position))) |
(gnugo-put :center-position |
1643 |
|
(get-text-property (point) 'gnugo-position)))) |
1644 |
;; first move |
;; first move |
1645 |
(gnugo-put :game-start-time (current-time)) |
(gnugo-put :game-start-time (current-time)) |
1646 |
(let ((g (gnugo-get :gnugo-color)) |
(let ((g (gnugo-get :gnugo-color)) |
1673 |
(message "(not quitting)")))) |
(message "(not quitting)")))) |
1674 |
("Q" . (lambda () (interactive) |
("Q" . (lambda () (interactive) |
1675 |
(kill-buffer nil))) |
(kill-buffer nil))) |
|
("u" . gnugo-undo) |
|
1676 |
("U" . (lambda (x) (interactive "P") |
("U" . (lambda (x) (interactive "P") |
1677 |
(gnugo-magic-undo |
(gnugo-magic-undo |
1678 |
(cond ((numberp x) (number-to-string x)) |
(cond ((numberp x) (number-to-string x)) |
1679 |
((consp x) (number-to-string (car x))) |
((consp x) (number-to-string (car x))) |
1680 |
(t (gnugo-position)))))) |
(t (gnugo-position)))))) |
1681 |
("\C-l" . (gnugo-refresh t)) |
("u" . gnugo-undo-two-moves) |
1682 |
|
("\C-l" . gnugo-refresh) |
1683 |
("\M-_" . bury-buffer) |
("\M-_" . bury-buffer) |
1684 |
("_" . bury-buffer) |
("_" . bury-buffer) |
1685 |
("h" . (lambda () (interactive) |
("h" . (lambda () (interactive) |