summaryrefslogtreecommitdiff
path: root/chess-file.el
blob: ddfeac72c7a641adec3360713a80e1e5b9bf6a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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)
      (forward-char 1))
    (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))
	  game))))

   ((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