]> code.delx.au - gnu-emacs-elpa/blob - chess-kibitz.el
reward passed pawns, and make the code a bit faster
[gnu-emacs-elpa] / chess-kibitz.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Implements chess kibitzing, stored as annotations to the game being
4 ;; viewed or played. C-c C-c is used to save a kibitzing comment.
5 ;;
6
7 (defvar chess-kibitz-input-last nil)
8 (defvar chess-kibitz-index nil)
9
10 (make-variable-buffer-local 'chess-kibitz-input-last)
11 (make-variable-buffer-local 'chess-kibitz-index)
12
13 (define-derived-mode chess-kibitz-mode text-mode "Kibitz"
14 "A mode for editing chess annotations."
15 (set-buffer-modified-p nil)
16 (setq chess-kibitz-input-last (copy-marker (point-max) t))
17 (let ((map (current-local-map)))
18 (define-key map [(control ?c) (control ?c)] 'chess-kibitz-save)))
19
20 (defun chess-kibitz-save ()
21 (interactive)
22 (let ((ann (buffer-substring-no-properties chess-kibitz-input-last
23 (point-max))))
24 (chess-game-run-hooks chess-module-game 'kibitz ann)
25 (chess-pos-add-annotation (chess-game-pos chess-kibitz-index) ann))
26 (set-marker chess-kibitz-input-last (point-max))
27 (set-buffer-modified-p nil))
28
29 (defun chess-kibitz-show-annotations (index)
30 (setq chess-kibitz-index index)
31 (erase-buffer)
32 (let ((position (chess-game-pos chess-module-game index))
33 popup)
34 (dolist (ann (chess-pos-annotations position))
35 (when (stringp ann)
36 (insert ann ?\n)
37 (setq popup t)))
38 (if popup
39 (display-buffer (current-buffer)))))
40
41 (defun chess-kibitz-handler (game event &rest args)
42 (cond
43 ((eq event 'initialize)
44 (kill-buffer (current-buffer))
45 (set-buffer (generate-new-buffer "*Annotations*"))
46 (chess-kibitz-mode)
47 t)
48
49 ((eq event 'switch-to-annotations)
50 (switch-to-buffer-other-window (current-buffer)))
51
52 ((eq event 'kibitz)
53 (chess-kibitz-handler 'switch-to-annotations)
54 (save-excursion
55 (goto-char chess-kibitz-input-last)
56 (insert (car args))))
57
58 ((eq event 'set-index)
59 (chess-kibitz-show-annotations (car args)))
60
61 ((memq event '(post-undo move))
62 (chess-kibitz-show-annotations (chess-game-index game)))))
63
64 (provide 'chess-kibitz)
65
66 ;;; chess-kibitz.el ends here