]> code.delx.au - gnu-emacs-elpa/blob - chess-common.el
Correctly indent `chess-with-current-buffer' in lisp-mode.
[gnu-emacs-elpa] / chess-common.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Define handler functions that are common to the (relatively)
4 ;; standard chess engine communication protocol:
5 ;;
6 ;; http://www.tim-mann.org/xboard/engine-intf.html
7 ;;
8
9 (require 'chess-engine)
10 (require 'chess-message)
11
12 (defvar chess-common-engine-name nil)
13 (defvar chess-common-temp-files nil)
14 (make-variable-buffer-local 'chess-common-engine-name)
15 (make-variable-buffer-local 'chess-common-temp-files)
16
17 (defmacro chess-with-temp-file (&rest body)
18 `(let ((file (make-temp-file "chess")))
19 (with-temp-file file
20 ,@body)
21 (push file chess-common-temp-files)
22 file))
23
24 (put 'chess-with-temp-file 'lisp-indent-function 1)
25
26 (chess-message-catalog 'english
27 '((starting-engine . "Starting chess program '%s'...")
28 (starting-engine-done . "Starting chess program '%s'...done")
29 (could-not-find-engine . "Cannot find %s executable; check `%s'")
30 (draw-offer-declined . "Your draw offer was declined")
31 (illegal-move . "Illegal move")
32 (not-yet-implemented . "This feature is not yet implemented")))
33
34 (defun chess-common-handler (game event &rest args)
35 "Initialize the network chess engine."
36 (cond
37 ((eq event 'initialize)
38 (let* ((name (car args))
39 (path (intern (concat "chess-" name "-path")))
40 proc)
41 (chess-message 'starting-engine name)
42 (unless (boundp path)
43 (chess-error 'could-not-find-engine name path))
44 (setq proc (start-process (concat "chess-" name)
45 (current-buffer) (symbol-value path)))
46 (chess-message 'starting-engine-done name)
47 proc))
48
49 ((eq event 'ready)
50 (chess-game-set-data game 'active t)
51 (chess-game-run-hooks game 'check-autosave))
52
53 ((eq event 'destroy)
54 (let ((proc (get-buffer-process (current-buffer))))
55 (if (and (processp proc)
56 (memq (process-status proc) '(run open)))
57 (chess-engine-send nil "quit\n")))
58
59 (dolist (file chess-common-temp-files)
60 (if (file-exists-p file)
61 (delete-file file)))
62 (setq chess-common-temp-files nil))
63
64 ((eq event 'pass)
65 (chess-engine-send nil "go\n"))
66
67 ((eq event 'draw)
68 (chess-message 'draw-offer-declined))
69
70 ((eq event 'resign)
71 (chess-engine-send nil "resign\n")
72 (chess-game-set-data game 'active nil))
73
74 ((eq event 'new)
75 (chess-engine-send nil "new\n")
76 (chess-engine-set-position nil))
77
78 ((eq event 'force)
79 (chess-error 'not-yet-implemented))
80
81 ((eq event 'undo)
82 (dotimes (i (car args))
83 (chess-engine-send nil "undo\n"))
84 (if (= 1 (mod (car args) 2))
85 (chess-engine-send nil "go\n"))
86
87 ;; prevent us from handling the `undo' event which this triggers
88 (let ((chess-engine-handling-event t))
89 (chess-game-undo game (car args))))
90
91 ((eq event 'flag-fell)
92 (chess-game-set-data game 'active nil)
93 (let ((chess-game-inhibit-events t))
94 (chess-game-end game :flag-fell)))
95
96 ((eq event 'move)
97 (when (= 1 (chess-game-index game))
98 (chess-game-set-tag game "White" chess-full-name)
99 (chess-game-set-tag game "Black" chess-engine-opponent-name))
100
101 (chess-engine-send nil (concat (chess-ply-to-algebraic (car args))
102 "\n"))
103 (if (chess-game-over-p game)
104 (chess-game-set-data game 'active nil)))))
105
106 (provide 'chess-common)
107
108 ;;; chess-common.el ends here