summaryrefslogtreecommitdiff
path: root/chess-database.el
blob: 68c318a2ae8d7a92265d584e98dcfdb99b2284d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Basic code for manipulating game databases
;;
;; $Revision$

(defvar chess-database-handler nil)

(make-variable-buffer-local 'chess-database-handler)

(chess-message-catalog 'english
  '((no-such-database . "There is no such chess database module '%s'")))

(defun chess-database-open (module file)
  "Returns the opened database object, or nil."
  (let* ((name (symbol-name module))
	 (handler (intern-soft (concat name "-handler")))
	 buffer)
    (unless handler
      (chess-error 'no-such-database name))
    (when (setq buffer (funcall handler 'open file))
      (with-current-buffer buffer
	(setq chess-database-handler handler)
	(add-hook 'kill-buffer-hook 'chess-database-close nil t)
	(add-hook 'after-revert-hook 'chess-database-rescan nil t)
	(current-buffer)))))

(defsubst chess-database-command (database event &rest args)
  (with-current-buffer database
    (apply chess-database-handler event args)))

(defun chess-database-close (&optional database)
  (let ((buf (or database (current-buffer))))
    (when (buffer-live-p buf)
      (with-current-buffer buf
	(remove-hook 'kill-buffer-hook 'chess-database-close t))
      (chess-database-command buf 'save)
      (chess-database-command buf 'close)
      (kill-buffer buf))))

(defun chess-database-save (database)
  (chess-database-command database 'save))

(defun chess-database-rescan (&optional database)
  (chess-database-command database 'rescan))

(defun chess-database-count (database)
  (chess-database-command database 'count))

(defun chess-database-read (database index)
  (chess-database-command database 'read index))

(defun chess-database-write (database game)
  (chess-database-command database 'write game))

(defun chess-database-replace (database game &optional index)
  (chess-database-command database 'replace game index))

(defun chess-database-query (database &rest terms)
  (chess-database-command database 'query terms))

(provide 'chess-database)

;;; chess-database.el ends here