summaryrefslogtreecommitdiff
path: root/chess-scid.el
blob: 0813b90ba16603add0f1c71df9812edf5415c327 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; A game database that uses SCID for storage/retrieval
;;
;; The advantage is that it's much faster than PGN, and far, far more
;; compact.
;;

(defvar chess-scid-process)

(make-variable-buffer-local 'chess-scid-process)

(defun chess-scid-get-result (command)
  (let ((here (point-max)))
    (process-send-string chess-scid-process command)
    (accept-process-output chess-scid-process)
    (goto-char (point-max))
    (while (memq (char-before) '(?  ?\t ?\n ?\r ?\%))
      (backward-char 1))
    (buffer-substring here (point))))

(defun chess-scid-handler (event &rest args)
  (cond
   ((eq event 'open)
    (if (file-readable-p (concat (car args) ".sg3"))
	(let* ((buffer (generate-new-buffer " *chess-scid*"))
	       (proc (start-process "*chess-scid*" buffer
				    (executable-find "tcscid"))))
	  (if (and proc (eq (process-status proc) 'run))
	      (with-current-buffer buffer
		(accept-process-output proc)
		(setq chess-scid-process proc)
		(if (= 1 (string-to-int
			  (chess-scid-get-result
			   (format "sc_base open %s\n"
				   (expand-file-name (car args))))))
		    buffer
		  (kill-process proc)
		  (kill-buffer buffer)
		  nil))
	    (kill-buffer buffer)
	    nil))))

   ((eq event 'close)
    (process-send-string chess-scid-process "sc_base close\nexit\n")
    (while (eq (process-status chess-scid-process) 'run)
      (sit-for 0 250)))

   ((eq event 'count)
    (string-to-int (chess-scid-get-result "sc_base numGames\n")))

   ((eq event 'read)
    (let ((here (point-max)) game)
      (process-send-string chess-scid-process
			   (format "sc_game load %d\nsc_game pgn\n"
				   (car args)))
      (accept-process-output chess-scid-process)
      (goto-char here)
      (when (setq game (chess-pgn-to-game))
	(chess-game-set-data game 'database (current-buffer))
	(chess-game-set-data game 'database-index (car args))
	(chess-game-set-data game 'database-count
			     (chess-scid-handler 'count))
	game)))

   ((eq event 'write)
    (chess-scid-handler 'replace (car args) 0))

   ((eq event 'replace)
    (let ((index (or (cadr args)
		     (chess-game-data (car args) 'database-index))))
      (process-send-string
       chess-scid-process
       (format "sc_game import \"%s\"\n"
	       (with-temp-buffer
		 (chess-pgn-insert-plies (car args) 1
					 (chess-game-plies (car args)))
		 (insert (or (chess-game-tag (car args) "Result") "*"))
		 (buffer-string))))
      (dolist (tag (chess-game-tags (car args)))
	;; jww (2002-05-01): how do I set extra tags?
	(unless (string= (car tag) "TimeControl")
	  (process-send-string
	   chess-scid-process
	   (concat "sc_game tags set "
		   (cond
		    ((string= (car tag) "Event")     "-event")
		    ((string= (car tag) "Site")      "-site")
		    ((string= (car tag) "Date")      "-date")
		    ((string= (car tag) "Round")     "-round")
		    ((string= (car tag) "White")     "-white")
		    ((string= (car tag) "WhiteElo")  "-whiteElo")
		    ((string= (car tag) "Black")     "-black")
		    ((string= (car tag) "BlackElo")  "-blackElo")
		    ((string= (car tag) "Result")    "-result")
		    ((string= (car tag) "ECO")       "-eco")
		    ((string= (car tag) "EventDate") "-eventdate")
		    ((string= (car tag) "Extra")     "-extra"))
		   " \"" (cdr tag) "\"\n"))))
      (process-send-string chess-scid-process
			   (format "sc_game save %d\n" index))))))

(provide 'chess-scid)

;;; chess-scid.el ends here