]> code.delx.au - gnu-emacs-elpa/blob - chess-database.el
*** no comment ***
[gnu-emacs-elpa] / chess-database.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Basic code for manipulating game databases
4 ;;
5 ;; $Revision$
6
7 (defvar chess-database-event-handler nil)
8
9 (make-variable-buffer-local 'chess-database-event-handler)
10
11 (defmacro chess-with-current-buffer (buffer &rest body)
12 `(let ((buf ,buffer))
13 (if buf
14 (with-current-buffer buf
15 ,@body)
16 ,@body)))
17
18 (chess-message-catalog 'english
19 '((no-such-style . "There is no such chess database module '%s'")))
20
21 (defun chess-database-open (module file)
22 "Returns the opened database object, or nil."
23 (let* ((name (symbol-name module))
24 (handler (intern-soft (concat name "-handler")))
25 buffer)
26 (unless handler
27 (chess-error 'no-such-database name))
28 (when (setq buffer (funcall handler 'open file))
29 (with-current-buffer buffer
30 (setq chess-database-event-handler handler)
31 (add-hook 'kill-buffer-hook 'chess-database-close nil t)
32 (add-hook 'after-revert-hook 'chess-database-rescan nil t)
33 (current-buffer)))))
34
35 (defsubst chess-database-command (database event &rest args)
36 (chess-with-current-buffer database
37 (apply 'chess-database-event-handler nil (current-buffer)
38 event args)))
39
40 (defun chess-database-close (database)
41 (let ((buf (or database (current-buffer))))
42 (when (buffer-live-p buf)
43 (chess-database-command buf 'save)
44 (chess-database-command buf 'close)
45 (with-current-buffer buf
46 (remove-hook 'kill-buffer-hook 'chess-database-quit t))
47 (kill-buffer buf))))
48
49 (defun chess-database-save (database)
50 (chess-database-command database 'save))
51
52 (defun chess-database-rescan (&optional database)
53 (chess-database-command database 'rescan))
54
55 (defun chess-database-count (database)
56 (chess-database-command database 'count))
57
58 (defun chess-database-read (database index)
59 (chess-database-command database 'read index))
60
61 (defun chess-database-write (database game)
62 (chess-database-command database 'write game))
63
64 (defun chess-database-replace (database game &optional index)
65 (chess-database-command database 'replace game index))
66
67 (defun chess-database-query (database &rest terms)
68 (chess-database-command database 'query terms))
69
70 (defun chess-database-event-handler (game database event &rest args)
71 (if (eq event 'shutdown)
72 (chess-database-close database)
73 (chess-with-current-buffer database
74 (apply chess-database-event-handler event args))))
75
76 (provide 'chess-database)
77
78 ;;; chess-database.el ends here