summaryrefslogtreecommitdiff
path: root/chess-file.el
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2002-04-14 21:18:56 +0000
committerJohn Wiegley <johnw@newartisans.com>2002-04-14 21:18:56 +0000
commitb3d24ad95aaa81040fc2f4eab468e9aceee37bd3 (patch)
tree85895895d66fccc6c0c0b4059884cfcf6bb4ca34 /chess-file.el
parentd5e9f0a3c531104936728c5a599dde77c67319b0 (diff)
*** no comment ***
Diffstat (limited to 'chess-file.el')
-rw-r--r--chess-file.el69
1 files changed, 69 insertions, 0 deletions
diff --git a/chess-file.el b/chess-file.el
new file mode 100644
index 0000000..9667c06
--- /dev/null
+++ b/chess-file.el
@@ -0,0 +1,69 @@
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;
+;; A game database that stores PGN format games in a single file.
+;;
+;; This is basically what you expect from a file ending in .pgn.
+;;
+;; $Revision$
+
+(defvar chess-file-locations)
+
+(make-variable-buffer-local 'chess-file-locations)
+
+(defun chess-file-handler (event &rest args)
+ (cond
+ ((eq event 'open)
+ (with-current-buffer (find-file-noselect (car args))
+ (chess-file-handler 'rescan)
+ (current-buffer)))
+
+ ((eq event 'rescan)
+ (goto-char (point-min))
+ (setq chess-file-locations nil)
+ (while (search-forward "[Event" nil t)
+ (goto-char (match-beginning 0))
+ (push (point) chess-file-locations))
+ (setq chess-file-locations (nreverse chess-file-locations)))
+
+ ((eq event 'save)
+ (save-buffer))
+
+ ((eq event 'count)
+ (length chess-file-locations))
+
+ ((eq event 'read)
+ (let ((index (car args)) game)
+ (when (and (>= index 0)
+ (< index (chess-file-handler 'count)))
+ (goto-char (nth index chess-file-locations))
+ (when (setq game (chess-pgn-to-game))
+ (chess-game-set-data game 'database (current-buffer))
+ (chess-game-set-data game 'database-index index)
+ (chess-game-set-data game 'database-count
+ (chess-file-handler 'count))))))
+
+ ((eq event 'write)
+ (goto-char (point-max))
+ (while (memq (char-before) '(? ?\t ?\n ?\r))
+ (delete-backward-char 1))
+ (insert ?\n ?\n)
+ (push (point) chess-file-locations)
+ (chess-game-to-pgn (car args))
+ (1- (chess-file-handler 'count)))
+
+ ((eq event 'replace)
+ (let ((index (or (cadr args)
+ (chess-game-data (car args) 'database-index)))
+ (count (chess-file-handler 'count)))
+ (when (and (>= index 0)
+ (< index count))
+ (goto-char (nth index chess-file-locations))
+ (delete-region (point) (if (= (1+ index) count)
+ (point-max)
+ (nth (1+ index) chess-file-locations)))
+ (chess-game-to-pgn (car args))
+ (insert ?\n))))))
+
+(provide 'chess-file)
+
+;;; chess-file.el ends here