]> code.delx.au - gnu-emacs-elpa/blob - chess-database.el
Release 2.0.4
[gnu-emacs-elpa] / chess-database.el
1 ;;; chess-database.el --- Basic code for manipulating game databases
2
3 ;; Copyright (C) 2002, 2004, 2008 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Keywords: data, games
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Code:
23
24 (require 'chess-message)
25
26 (defgroup chess-database nil
27 "Generic interface to chess database modules."
28 :group 'chess)
29
30 (defcustom chess-database-modules '(chess-scid chess-file)
31 "List of database modules to try when `chess-database-open' is called."
32 :type '(repeat (symbol :tag "Module"))
33 :group 'chess-database)
34
35 (defvar chess-database-handler nil)
36
37 (make-variable-buffer-local 'chess-database-handler)
38
39 (chess-message-catalog 'english
40 '((no-such-database . "There is no such chess database module '%s'")))
41
42 (defun chess-database-do-open (module file)
43 "Returns the opened database object, or nil."
44 (when (require module nil t)
45 (let* ((name (symbol-name module))
46 (handler (intern-soft (concat name "-handler"))))
47 (unless handler
48 (chess-error 'no-such-database name))
49 (let ((buffer (funcall handler 'open file)))
50 (when buffer
51 (with-current-buffer buffer
52 (setq chess-database-handler handler)
53 (add-hook 'kill-buffer-hook 'chess-database-close nil t)
54 (add-hook 'after-revert-hook 'chess-database-rescan nil t)
55 (current-buffer)))))))
56
57 (defun chess-database-open (file &optional module)
58 "Returns the opened database object, or nil."
59 (if module
60 (chess-database-do-open module file)
61 (let (result)
62 (setq module chess-database-modules)
63 (while module
64 (if (setq result (chess-database-do-open (car module) file))
65 (setq module nil)
66 (setq module (cdr module))))
67 result)))
68
69 (defsubst chess-database-command (database event &rest args)
70 (with-current-buffer database
71 (apply chess-database-handler event args)))
72
73 (defun chess-database-close (&optional database)
74 (let ((buf (or database (current-buffer))))
75 (when (buffer-live-p buf)
76 (with-current-buffer buf
77 (remove-hook 'kill-buffer-hook 'chess-database-close t))
78 (chess-database-save buf)
79 (chess-database-command buf 'close)
80 (kill-buffer buf))))
81
82 (defun chess-database-save (database)
83 (chess-database-command database 'save))
84
85 (defun chess-database-rescan (&optional database)
86 (chess-database-command database 'rescan))
87
88 (defun chess-database-count (database)
89 (chess-database-command database 'count))
90
91 (defun chess-database-read-only-p (database)
92 "Return non-nil if DATABASE is read only."
93 (chess-database-command database 'read-only-p))
94
95 (defun chess-database-filename (database)
96 "Return the filename of an already opened DATABASE."
97 (chess-database-command database 'filename))
98
99 (defun chess-database-read (database index)
100 "Return from DATABASE the chess game object at INDEX."
101 (chess-database-command database 'read index))
102
103 (defun chess-database-write (database game)
104 (chess-database-command database 'write game))
105
106 (defun chess-database-replace (database game &optional index)
107 (chess-database-command database 'replace game index))
108
109 (defun chess-database-query (database &rest terms)
110 "Run a query on DATABASE.
111 TERMS is partly dependent on the chess-database module in use.
112 chess-scid:
113 tree-search GAME: Perform a tree search on the last position of GAME."
114 (apply 'chess-database-command database 'query terms))
115
116 (provide 'chess-database)
117
118 ;;; chess-database.el ends here