(defconst life-patterns
[("@@@" " @@" "@@@")
("@@@ @@@" "@@ @@ " "@@@ @@@")
("@@@ @@@" "@@ @@" "@@@ @@@")
("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
("@@@@@@@@@@")
(" @@@@@@@@@@ "
" @@@@@@@@@@ "
" @@@@@@@@@@ "
"@@@@@@@@@@ "
"@@@@@@@@@@ ")
("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@")
("@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @")
("@@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@")
("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@"
"@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")]
"Vector of rectangles containing some Life startup patterns.")
(defmacro life-life-char () ?@)
(defmacro life-death-char () (1+ (life-life-char)))
(defmacro life-birth-char () 3)
(defmacro life-void-char () ?\ )
(defmacro life-life-string () (char-to-string (life-life-char)))
(defmacro life-death-string () (char-to-string (life-death-char)))
(defmacro life-birth-string () (char-to-string (life-birth-char)))
(defmacro life-void-string () (char-to-string (life-void-char)))
(defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]"))
(defmacro life-increment (variable) (list 'setq variable (list '1+ variable)))
(defconst life-neighbor-deltas nil)
(defconst life-window-start nil)
(defconst life-current-generation nil)
(defconst life-generation-string nil)
(defvar life-initialized nil
"Non-nil if `life' has been run at least once.")
(defun life (&optional sleeptime)
"Run Conway's Life simulation.
The starting pattern is randomly selected. Prefix arg (optional first
arg non-nil from a program) is the number of seconds to sleep between
generations (this defaults to 1)."
(interactive "p")
(or life-initialized
(random t))
(setq life-initialized t)
(or sleeptime (setq sleeptime 1))
(life-setup)
(catch 'life-exit
(while t
(let ((inhibit-quit t))
(life-display-generation sleeptime)
(life-grim-reaper)
(life-expand-plane-if-needed)
(life-increment-generation)))))
(defalias 'life-mode 'life)
(put 'life-mode 'mode-class 'special)
(defun life-setup ()
(let (n)
(switch-to-buffer (get-buffer-create "*Life*") t)
(erase-buffer)
(kill-all-local-variables)
(setq case-fold-search nil
mode-name "Life"
major-mode 'life-mode
truncate-lines t
life-current-generation 0
life-generation-string "0"
mode-line-buffer-identification '("Life: generation "
life-generation-string)
fill-column (1- (window-width))
life-window-start 1)
(buffer-disable-undo (current-buffer))
(life-insert-random-pattern)
(goto-char (point-min))
(while (re-search-forward (life-not-void-regexp) nil t)
(replace-match (life-life-string) t t))
(goto-char (point-min))
(setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2))
(while (not (eobp))
(indent-to n)
(forward-line))
(setq n (/ (- (1- (window-height))
(count-lines (point-min) (point-max)))
2))
(goto-char (point-min))
(newline n)
(goto-char (point-max))
(newline n)
(goto-char (point-min))
(while (not (eobp))
(end-of-line)
(indent-to fill-column)
(move-to-column fill-column)
(delete-region (point) (progn (end-of-line) (point)))
(forward-line))
(untabify (point-min) (point-max))
(life-expand-plane-if-needed)
(life-compute-neighbor-deltas)))
(defun life-compute-neighbor-deltas ()
(setq life-neighbor-deltas
(list -1 (- fill-column)
(- (1+ fill-column)) (- (+ 2 fill-column))
1 fill-column (1+ fill-column)
(+ 2 fill-column))))
(defun life-insert-random-pattern ()
(insert-rectangle
(elt life-patterns (random (length life-patterns))))
(insert ?\n))
(defun life-increment-generation ()
(life-increment life-current-generation)
(setq life-generation-string (int-to-string life-current-generation)))
(defun life-grim-reaper ()
(set-match-data nil)
(goto-char (point-min))
(let (point char pivot living-neighbors list)
(while (search-forward (life-life-string) nil t)
(setq list life-neighbor-deltas
living-neighbors 0
pivot (1- (point)))
(while list
(setq point (+ pivot (car list))
char (char-after point))
(cond ((eq char (life-void-char))
(subst-char-in-region point (1+ point)
(life-void-char) 1 t))
((< char 3)
(subst-char-in-region point (1+ point) char (1+ char) t))
((< char 9)
(subst-char-in-region point (1+ point) char 9 t))
((>= char (life-life-char))
(life-increment living-neighbors)))
(setq list (cdr list)))
(if (memq living-neighbors '(2 3))
()
(subst-char-in-region pivot (1+ pivot)
(life-life-char) (life-death-char) t))))
(if (null (match-beginning 0))
(life-extinct-quit))
(subst-char-in-region 1 (point-max) 9 (life-void-char) t)
(subst-char-in-region 1 (point-max) 1 (life-void-char) t)
(subst-char-in-region 1 (point-max) 2 (life-void-char) t)
(subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t)
(subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t))
(defun life-expand-plane-if-needed ()
(catch 'done
(goto-char (point-min))
(while (not (eobp))
(cond ((or (eq (following-char) (life-life-char))
(eq (progn (end-of-line) (preceding-char)) (life-life-char)))
(goto-char (point-min))
(while (not (eobp))
(insert (life-void-char))
(end-of-line)
(insert (life-void-char))
(forward-char))
(setq fill-column (+ 2 fill-column))
(scroll-left 1)
(life-compute-neighbor-deltas)
(throw 'done t)))
(forward-line)))
(goto-char (point-min))
(cond ((search-forward (life-life-string)
(+ (point) fill-column fill-column 2) t)
(goto-char (point-min))
(insert-char (life-void-char) fill-column)
(insert ?\n)
(insert-char (life-void-char) fill-column)
(insert ?\n)
(setq life-window-start (+ life-window-start fill-column 1))))
(goto-char (point-max))
(cond ((search-backward (life-life-string)
(- (point) fill-column fill-column 2) t)
(goto-char (point-max))
(insert-char (life-void-char) fill-column)
(insert ?\n)
(insert-char (life-void-char) fill-column)
(insert ?\n)
(setq life-window-start (+ life-window-start fill-column 1)))))
(defun life-display-generation (sleeptime)
(goto-char life-window-start)
(recenter 0)
(or (eq t (sit-for sleeptime))
(throw 'life-exit nil)))
(defun life-extinct-quit ()
(life-display-generation 0)
(signal 'life-extinct nil))
(put 'life-extinct 'error-conditions '(life-extinct quit))
(put 'life-extinct 'error-message "All life has perished")
(provide 'life)