]> code.delx.au - gnu-emacs-elpa/blob - chess-kibitz.el
* chess-pos.el (chess-pos-search*)
[gnu-emacs-elpa] / chess-kibitz.el
1 ;;; chess-kibitz.el --- Chess kibitzing, stored as annotations
2
3 ;; Copyright (C) 2002, 2014 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Keywords: games
8
9 ;; This is free software; you can redistribute it and/or modify it under
10 ;; the terms of the GNU General Public License as published by the Free
11 ;; Software Foundation; either version 3, or (at your option) any later
12 ;; version.
13 ;;
14 ;; This is distributed in the hope that it will be useful, but WITHOUT
15 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 ;; for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; Implements chess kibitzing, stored as annotations to the game being
25 ;; viewed or played. C-c C-c is used to save a kibitzing comment.
26
27 ;;; Code:
28
29 (require 'chess-game)
30
31 (defvar chess-kibitz-input-last nil)
32 (defvar chess-kibitz-index nil)
33
34 (make-variable-buffer-local 'chess-kibitz-input-last)
35 (make-variable-buffer-local 'chess-kibitz-index)
36
37 (define-derived-mode chess-kibitz-mode text-mode "Kibitz"
38 "A mode for editing chess annotations."
39 (set-buffer-modified-p nil)
40 (setq chess-kibitz-input-last (copy-marker (point-max) t))
41 (let ((map (current-local-map)))
42 (define-key map [(control ?c) (control ?c)] 'chess-kibitz-save)))
43
44 (defvar chess-module-game)
45
46 (defun chess-kibitz-save ()
47 (interactive)
48 (let ((ann (buffer-substring-no-properties chess-kibitz-input-last
49 (point-max))))
50 (chess-game-run-hooks chess-module-game 'kibitz ann)
51 (chess-pos-add-annotation (chess-game-pos chess-kibitz-index) ann))
52 (set-marker chess-kibitz-input-last (point-max))
53 (set-buffer-modified-p nil))
54
55 (defun chess-kibitz-show-annotations (index)
56 (setq chess-kibitz-index index)
57 (erase-buffer)
58 (let ((position (chess-game-pos chess-module-game index))
59 popup)
60 (dolist (ann (chess-pos-annotations position))
61 (when (stringp ann)
62 (insert ann ?\n)
63 (setq popup t)))
64 (if popup
65 (display-buffer (current-buffer)))))
66
67 (defun chess-kibitz-handler (game event &rest args)
68 (cond
69 ((eq event 'initialize)
70 (kill-buffer (current-buffer))
71 (set-buffer (generate-new-buffer "*Annotations*"))
72 (chess-kibitz-mode)
73 t)
74
75 ((eq event 'switch-to-annotations)
76 (switch-to-buffer-other-window (current-buffer)))
77
78 ((eq event 'kibitz)
79 (chess-kibitz-handler 'switch-to-annotations)
80 (save-excursion
81 (goto-char chess-kibitz-input-last)
82 (insert (car args))))
83
84 ((eq event 'set-index)
85 (chess-kibitz-show-annotations (car args)))
86
87 ((memq event '(post-undo move))
88 (chess-kibitz-show-annotations (chess-game-index game)))))
89
90 (provide 'chess-kibitz)
91
92 ;;; chess-kibitz.el ends here