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