]> code.delx.au - gnu-emacs-elpa/blob - chess-announce.el
*** no comment ***
[gnu-emacs-elpa] / chess-announce.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Scheme to verbally announce moves
4 ;;
5 ;; $Revision$
6
7 (require 'chess-game)
8
9 (defvar chess-announce-names
10 '((?q . "queen")
11 (?k . "king")
12 (?b . "bishop")
13 (?n . "knight")
14 (?r . "rook")
15 (?p . "pawn")))
16
17 (autoload 'festival-start-process "festival")
18 (autoload 'festival-kill-process "festival")
19
20 (defvar chess-announce-functions
21 (if (and (executable-find "festival")
22 (not (featurep 'emacspeak)))
23 (if (fboundp 'festival-say-string)
24 '(festival-start-process festival-say-string festival-kill-process)
25 '(ignore chess-announce-festival ignore))
26 '(ignore message ignore))
27 "These three functions are used to for announcing moves.
28 The first is called one start of the announcer. The second is called
29 with the string to announce each time. The third is called to
30 shutdown the announcer process, if necessary.")
31
32 (defun chess-announce-available-p () t)
33
34 (defun chess-announce-for-game (game)
35 "Announce the opponent's moves in GAME."
36 (funcall (nth 0 chess-announce-functions))
37 (chess-game-add-hook game 'chess-announce-handler))
38
39 (defun chess-announce-handler (game ignore event &rest args)
40 "This display module presents a standard chessboard.
41 See `chess-display-type' for the different kinds of displays."
42 (cond
43 ((eq event 'shutdown)
44 (funcall (nth 2 chess-announce-functions)))
45
46 ((eq event 'move)
47 (let* ((ply (chess-game-ply game (1- (chess-game-index game))))
48 (pos (chess-ply-pos ply)))
49 (unless (eq (chess-game-data game 'my-color)
50 (chess-pos-side-to-move pos))
51 (let* ((source (chess-ply-source ply))
52 (target (chess-ply-target ply))
53 (s-piece (chess-pos-piece pos source))
54 (t-piece (chess-pos-piece pos target))
55 text)
56 (cond
57 ((chess-ply-has-keyword ply :castle)
58 (setq text "kingside castle"))
59 ((chess-ply-has-keyword :long-castle)
60 (setq text "queenside castle"))
61 ((= t-piece ? )
62 (setq text (concat (cdr (assq (downcase s-piece)
63 chess-announce-names))
64 " to "
65 (chess-index-to-coord target))))
66 (t
67 (setq text (concat (cdr (assq (downcase s-piece)
68 chess-announce-names))
69 " takes at "
70 (chess-index-to-coord target)))))
71 (if (chess-ply-has-keyword :check)
72 (setq text (concat text ", check")))
73 (if (chess-ply-has-keyword :checkmate)
74 (setq text (concat text ", checkmate")))
75 (if (chess-ply-has-keyword :stalemate)
76 (setq text (concat text ", stalemate")))
77
78 (funcall (nth 1 chess-announce-functions) text)))))))
79
80 (defun chess-announce-festival (text)
81 "Announce the given text using festival.
82 This is less efficient than festival.el, which should be installed if
83 possible. Debian installs it automatically when you apt-get install
84 festival."
85 (let ((proc (start-process "announce" nil "/usr/bin/festival" "--tts")))
86 (when (and proc (eq (process-status proc) 'run))
87 (process-send-string proc (concat text "\n"))
88 (process-send-eof proc))))
89
90 (provide 'chess-announce)
91
92 ;;; chess-announce.el ends here