From 652c8b322bf058c35eb48b5373976f22e3e3276f Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 1 May 2002 05:39:41 +0000 Subject: *** no comment *** --- chess-autosave.el | 84 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 30 deletions(-) (limited to 'chess-autosave.el') 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) -- cgit v1.2.3