]> code.delx.au - gnu-emacs-elpa/blob - chess-scid.el
Correctly indent `chess-with-current-buffer' in lisp-mode.
[gnu-emacs-elpa] / chess-scid.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; A game database that uses SCID for storage/retrieval
4 ;;
5 ;; The advantage is that it's much faster than PGN, and far, far more
6 ;; compact.
7 ;;
8
9 (defvar chess-scid-process)
10
11 (make-variable-buffer-local 'chess-scid-process)
12
13 (defsubst chess-scid-send (string)
14 (process-send-string chess-scid-process (concat string "\n")))
15
16 (defun chess-scid-get-result (command)
17 (let ((here (point-max)))
18 (chess-scid-send command)
19 (accept-process-output chess-scid-process)
20 (goto-char (point-max))
21 (while (memq (char-before) '(? ?\t ?\n ?\r ?\%))
22 (backward-char 1))
23 (buffer-substring here (point))))
24
25 (defun chess-scid-handler (event &rest args)
26 (cond
27 ((eq event 'open)
28 (if (file-readable-p (concat (car args) ".sg3"))
29 (let* ((buffer (generate-new-buffer " *chess-scid*"))
30 (proc (start-process "*chess-scid*" buffer
31 (executable-find "tcscid"))))
32 (if (and proc (eq (process-status proc) 'run))
33 (with-current-buffer buffer
34 (accept-process-output proc)
35 (setq chess-scid-process proc)
36 (if (= 1 (string-to-number
37 (chess-scid-get-result
38 (format "sc_base open %s"
39 (expand-file-name (car args))))))
40 buffer
41 (kill-process proc)
42 (kill-buffer buffer)
43 nil))
44 (kill-buffer buffer)
45 nil))))
46
47 ((eq event 'close)
48 (chess-scid-send "sc_base close\nexit")
49 (while (eq (process-status chess-scid-process) 'run)
50 (sit-for 0 250)))
51
52 ((eq event 'read-only-p)
53 (if (zerop (string-to-number (chess-scid-get-result "sc_base isReadOnly")))
54 nil
55 t))
56
57 ((eq event 'filename)
58 (chess-scid-get-result "sc_base filename"))
59
60 ((eq event 'count)
61 (string-to-number (chess-scid-get-result "sc_base numGames")))
62
63 ((eq event 'read)
64 (let ((here (point-max)) game)
65 (process-send-string chess-scid-process
66 (format "sc_game load %d\nsc_game pgn\n"
67 (car args)))
68 (accept-process-output chess-scid-process)
69 (goto-char here)
70 (when (setq game (chess-pgn-to-game))
71 (chess-game-set-data game 'database (current-buffer))
72 (chess-game-set-data game 'database-index (car args))
73 (chess-game-set-data game 'database-count (chess-scid-handler 'count))
74 game)))
75
76 ((and (eq event 'query)
77 (eq (car args) 'tree-search))
78 (if (string=
79 (chess-scid-get-result
80 (format "sc_game import \"%s\""
81 (with-temp-buffer
82 (chess-pgn-insert-plies
83 (cadr args) 1 (chess-game-plies (cadr args)))
84 (insert " " (or (chess-game-tag (cadr args) "Result") "*"))
85 (buffer-string))))
86 "PGN text imported with no errors or warnings.")
87 (let ((here (point-max)))
88 (chess-scid-send "sc_tree search")
89 (accept-process-output chess-scid-process)
90 (goto-char here)
91 (let ((lines (list t)))
92 (while
93 (re-search-forward
94 (concat "\\s-*\\([1-9][0-9]*\\):\\s-+\\([^ ]+\\)\\s-+"
95 "\\([0-9]+\\):\\s-*\\([0-9.]+\\)%\\s-+"
96 "\\([0-9.]+\\)%\\s-+\\([0-9]+\\| \\)\\s-+"
97 "\\([0-9]+\\| \\)\\s-+\\([0-9]+\\| \\)\\s-+"
98 "\\([0-9.]+\\)%")
99 nil t)
100 (let ((move (match-string 2))
101 (freq (string-to-number (match-string 3)))
102 (score (string-to-number (match-string 5)))
103 (avgelo (string-to-number (match-string 6)))
104 (perf (string-to-number (match-string 7)))
105 (avgyear (string-to-number (match-string 8)))
106 (draws (string-to-number (match-string 9))))
107 (nconc lines
108 (list
109 (append
110 (list move freq score draws)
111 (cond
112 ((and (zerop avgyear) (zerop avgelo) (zerop perf))
113 nil)
114 ((and (zerop avgelo) (zerop perf))
115 (list avgyear))
116 (t
117 (list
118 (unless (zerop avgyear) avgyear)
119 (unless (zerop avgelo) avgelo)
120 (unless (zerop perf) perf)))))))))
121 (when (re-search-forward
122 (concat "TOTAL:\\s-+\\([0-9]+\\):100.0%\\s-+"
123 "\\([0-9.]+\\)%\\s-+\\([0-9]+\\| \\)\\s-+"
124 "\\([0-9]+\\| \\)\\s-+\\([0-9]+\\| \\)\\s-+"
125 "\\([0-9.]+\\)%") nil t)
126 (goto-char (point-max))
127 (append
128 (list (string-to-number (match-string 1))
129 (string-to-number (match-string 2))
130 (string-to-number (match-string 6)))
131 (cdr lines)))))
132 (error "Unable to import game")))
133
134 ((eq event 'write)
135 (chess-scid-handler 'replace (car args) 0))
136
137 ((eq event 'replace)
138 (unless (chess-scid-handler 'read-only-p)
139 (let ((index (or (cadr args)
140 (chess-game-data (car args) 'database-index))))
141 (chess-scid-send
142 (format "sc_game import \"%s\""
143 (with-temp-buffer
144 (chess-pgn-insert-plies
145 (car args) 1 (chess-game-plies (car args)))
146 (insert (or (chess-game-tag (car args) "Result") "*"))
147 (buffer-string))))
148 (dolist (tag (chess-game-tags (car args)))
149 ;; jww (2002-05-01): how do I set extra tags?
150 (unless (string= (car tag) "TimeControl")
151 (chess-scid-send
152 (concat "sc_game tags set "
153 (cond
154 ((string= (car tag) "Event") "-event")
155 ((string= (car tag) "Site") "-site")
156 ((string= (car tag) "Date") "-date")
157 ((string= (car tag) "Round") "-round")
158 ((string= (car tag) "White") "-white")
159 ((string= (car tag) "WhiteElo") "-whiteElo")
160 ((string= (car tag) "Black") "-black")
161 ((string= (car tag) "BlackElo") "-blackElo")
162 ((string= (car tag) "Result") "-result")
163 ((string= (car tag) "ECO") "-eco")
164 ((string= (car tag) "EventDate") "-eventdate")
165 ((string= (car tag) "Extra") "-extra"))
166 " \"" (cdr tag) "\""))))
167 (chess-scid-send (format "sc_game save %d" index)))))))
168
169 (provide 'chess-scid)
170
171 ;;; chess-scid.el ends here