]> code.delx.au - gnu-emacs-elpa/blob - chess-database.el
Moved some code around to follow better Lisp style.
[gnu-emacs-elpa] / chess-database.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; Basic code for manipulating game databases
4 ;;
5
6 (require 'chess-message)
7
8 (defgroup chess-database nil
9 "Generic interface to chess database modules."
10 :group 'chess)
11
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)
16
17 (defvar chess-database-handler nil)
18
19 (make-variable-buffer-local 'chess-database-handler)
20
21 (chess-message-catalog 'english
22 '((no-such-database . "There is no such chess database module '%s'")))
23
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"))))
28 (unless handler
29 (chess-error 'no-such-database name))
30 (let ((buffer (funcall handler 'open file)))
31 (when buffer
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)
36 (current-buffer))))))
37
38 (defun chess-database-open (file &optional module)
39 "Returns the opened database object, or nil."
40 (if module
41 (chess-database-do-open module file)
42 (let (result)
43 (setq module chess-database-modules)
44 (while module
45 (if (and (require (car module) nil t)
46 (setq result (chess-database-do-open (car module) file)))
47 (setq module nil)
48 (setq module (cdr module))))
49 result)))
50
51 (defsubst chess-database-command (database event &rest args)
52 (with-current-buffer database
53 (apply chess-database-handler event args)))
54
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)
62 (kill-buffer buf))))
63
64 (defun chess-database-save (database)
65 (chess-database-command database 'save))
66
67 (defun chess-database-rescan (&optional database)
68 (chess-database-command database 'rescan))
69
70 (defun chess-database-count (database)
71 (chess-database-command database 'count))
72
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))
76
77 (defun chess-database-filename (database)
78 "Return the filename of an already opened DATABASE."
79 (chess-database-command database 'filename))
80
81 (defun chess-database-read (database index)
82 "Return from DATABASE the chess game object at INDEX."
83 (chess-database-command database 'read index))
84
85 (defun chess-database-write (database game)
86 (chess-database-command database 'write game))
87
88 (defun chess-database-replace (database game &optional index)
89 (chess-database-command database 'replace game index))
90
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.
94 chess-scid:
95 tree-search GAME: Perform a tree search on the last position of GAME."
96 (apply 'chess-database-command database 'query terms))
97
98 (provide 'chess-database)
99
100 ;;; chess-database.el ends here