1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Basic code for manipulating game databases
6 (require 'chess-message)
8 (defgroup chess-database nil
9 "Generic interface to chess database modules."
12 (defcustom chess-database-modules '(chess-scid chess-file)
13 "List of database modules to try when `chess-database-open' is called."
14 :type '(repeat (symbol :tag "Module"))
15 :group 'chess-database)
17 (defvar chess-database-handler nil)
19 (make-variable-buffer-local 'chess-database-handler)
21 (chess-message-catalog 'english
22 '((no-such-database . "There is no such chess database module '%s'")))
24 (defun chess-database-do-open (module file)
25 "Returns the opened database object, or nil."
26 (let* ((name (symbol-name module))
27 (handler (intern-soft (concat name "-handler"))))
29 (chess-error 'no-such-database name))
30 (let ((buffer (funcall handler 'open file)))
32 (with-current-buffer buffer
33 (setq chess-database-handler handler)
34 (add-hook 'kill-buffer-hook 'chess-database-close nil t)
35 (add-hook 'after-revert-hook 'chess-database-rescan nil t)
38 (defun chess-database-open (file &optional module)
39 "Returns the opened database object, or nil."
41 (chess-database-do-open module file)
43 (setq module chess-database-modules)
45 (if (and (require (car module) nil t)
46 (setq result (chess-database-do-open (car module) file)))
48 (setq module (cdr module))))
51 (defsubst chess-database-command (database event &rest args)
52 (with-current-buffer database
53 (apply chess-database-handler event args)))
55 (defun chess-database-close (&optional database)
56 (let ((buf (or database (current-buffer))))
57 (when (buffer-live-p buf)
58 (with-current-buffer buf
59 (remove-hook 'kill-buffer-hook 'chess-database-close t))
60 (chess-database-save buf)
61 (chess-database-command buf 'close)
64 (defun chess-database-save (database)
65 (chess-database-command database 'save))
67 (defun chess-database-rescan (&optional database)
68 (chess-database-command database 'rescan))
70 (defun chess-database-count (database)
71 (chess-database-command database 'count))
73 (defun chess-database-read-only-p (database)
74 "Return non-nil if DATABASE is read only."
75 (chess-database-command database 'read-only-p))
77 (defun chess-database-filename (database)
78 "Return the filename of an already opened DATABASE."
79 (chess-database-command database 'filename))
81 (defun chess-database-read (database index)
82 "Return from DATABASE the chess game object at INDEX."
83 (chess-database-command database 'read index))
85 (defun chess-database-write (database game)
86 (chess-database-command database 'write game))
88 (defun chess-database-replace (database game &optional index)
89 (chess-database-command database 'replace game index))
91 (defun chess-database-query (database &rest terms)
92 "Run a query on DATABASE.
93 TERMS is partly dependent on the chess-database module in use.
95 tree-search GAME: Perform a tree search on the last position of GAME."
96 (apply 'chess-database-command database 'query terms))
98 (provide 'chess-database)
100 ;;; chess-database.el ends here