1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Basic code for manipulating game databases
7 (defvar chess-database-event-handler nil)
9 (make-variable-buffer-local 'chess-database-event-handler)
11 (defmacro chess-with-current-buffer (buffer &rest body)
14 (with-current-buffer buf
18 (chess-message-catalog 'english
19 '((no-such-style . "There is no such chess database module '%s'")))
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")))
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)
35 (defsubst chess-database-command (database event &rest args)
36 (chess-with-current-buffer database
37 (apply 'chess-database-event-handler nil (current-buffer)
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))
49 (defun chess-database-save (database)
50 (chess-database-command database 'save))
52 (defun chess-database-rescan (&optional database)
53 (chess-database-command database 'rescan))
55 (defun chess-database-count (database)
56 (chess-database-command database 'count))
58 (defun chess-database-read (database index)
59 (chess-database-command database 'read index))
61 (defun chess-database-write (database game)
62 (chess-database-command database 'write game))
64 (defun chess-database-replace (database game &optional index)
65 (chess-database-command database 'replace game index))
67 (defun chess-database-query (database &rest terms)
68 (chess-database-command database 'query terms))
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))))
76 (provide 'chess-database)
78 ;;; chess-database.el ends here