summaryrefslogtreecommitdiff
path: root/chess-autosave.el
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2002-05-01 05:39:41 +0000
committerJohn Wiegley <johnw@newartisans.com>2002-05-01 05:39:41 +0000
commit652c8b322bf058c35eb48b5373976f22e3e3276f (patch)
tree327151f018a3db90e696bd8943506ffe942e9fd0 /chess-autosave.el
parent4197d08b7f37f9c1155559897d750ad1f53af241 (diff)
*** no comment ***
Diffstat (limited to 'chess-autosave.el')
-rw-r--r--chess-autosave.el84
1 files changed, 54 insertions, 30 deletions
diff --git a/chess-autosave.el b/chess-autosave.el
index 9ab935c..9296785 100644
--- a/chess-autosave.el
+++ b/chess-autosave.el
@@ -26,8 +26,7 @@
(if (file-readable-p chess-autosave-file)
(if (y-or-n-p (chess-string 'chess-read-autosave))
(progn
- (chess-game-copy-game game (chess-autosave-read
- chess-autosave-file))
+ (chess-autosave-read game chess-autosave-file)
(delete-file chess-autosave-file))
(if (y-or-n-p (chess-string 'chess-delete-autosave))
(delete-file chess-autosave-file)
@@ -48,41 +47,66 @@
(if (file-readable-p chess-autosave-file)
(delete-file chess-autosave-file)))))
+(defun chess-prin1-ply (ply)
+ (insert "([")
+ (let ((pos (chess-ply-pos ply)))
+ (dotimes (i 74)
+ (prin1 (aref pos i) (current-buffer))
+ (insert ? )))
+ (insert "nil]")
+ (let ((changes (chess-ply-changes ply)))
+ (if changes
+ (insert ? ))
+ (while changes
+ (if (eq (car changes) :next-pos)
+ (setq changes (cddr changes))
+ (prin1 (car changes) (current-buffer))
+ (if (cdr changes)
+ (insert ? ))
+ (setq changes (cdr changes)))))
+ (insert ")"))
+
(defun chess-autosave-write (game file)
"Write a chess GAME to FILE as raw Lisp."
(with-current-buffer (find-file-noselect file t)
- (erase-buffer)
- (insert "(nil ")
- (prin1 (chess-game-tags game) (current-buffer))
- (insert " nil (")
- (dolist (ply (chess-game-plies game))
- (insert "([")
- (let ((pos (chess-ply-pos ply)))
- (dotimes (i 74)
- (prin1 (aref pos i) (current-buffer))
- (unless (= i 73)
- (insert ? ))))
- (insert ?\])
- (let ((changes (chess-ply-changes ply)))
- (if changes
- (insert ? ))
- (while changes
- (if (eq (car changes) :next-pos)
- (setq changes (cddr changes))
- (prin1 (car changes) (current-buffer))
- (if (cdr changes)
- (insert ? ))
- (setq changes (cdr changes)))))
- (insert ") "))
- (insert "))")
- (basic-save-buffer)
- (message nil)))
+ (let ((index (chess-game-index game)))
+ (if (or (= 1 index) (eobp))
+ (progn
+ (erase-buffer)
+ (prin1 (chess-game-tags game)
+ (current-buffer))
+ (insert "\n(\n;;## ply 0\n"))
+ (goto-char (point-max))
+ (re-search-backward "^;;## ply")
+ (forward-line)
+ (delete-region (point) (point-max)))
+ (chess-prin1-ply (chess-game-ply game (1- index)))
+ (insert (format "\n;;## ply %d\n" index))
+ (chess-prin1-ply (chess-game-ply game))
+ (insert ")\n")
+ (basic-save-buffer)
+ (message nil))))
-(defun chess-autosave-read (file)
+(defun chess-autosave-read (game file)
"Read a chess game as raw Lisp from FILE."
(with-current-buffer (find-file-noselect file t)
(goto-char (point-min))
- (read (current-buffer))))
+ (chess-game-set-tags game (read (current-buffer)))
+
+ (let* ((plies (read (current-buffer)))
+ (game-plies plies)
+ prev-ply)
+ (while plies
+ (if prev-ply
+ (chess-pos-set-preceding-ply (chess-ply-pos (car plies))
+ prev-ply))
+ (if (cdr plies)
+ (chess-ply-set-keyword (car plies) :next-pos
+ (chess-ply-pos (cadr plies))))
+ (setq prev-ply (car plies)
+ plies (cdr plies)))
+
+ (chess-game-set-plies game game-plies))))
(provide 'chess-autosave)