Fix bug in recent styled_format change
[emacs.git] / lisp / play / life.el
bloba5a3f1ef054c005473f579ccf0e57bcbf7610623
1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
3 ;; Copyright (C) 1988, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: Kyle Jones <kyleuunet.uu.net>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: games
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; A demonstrator for John Horton Conway's "Life" cellular automaton
27 ;; in Emacs Lisp. Picks a random one of a set of interesting Life
28 ;; patterns and evolves it according to the familiar rules.
30 ;;; Code:
32 (defvar life-patterns
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 "Vector of rectangles containing some Life startup patterns.")
80 ;; Macros are used macros for manifest constants instead of variables
81 ;; because the compiler will convert them to constants, which should
82 ;; eval faster than symbols.
84 ;; Don't change any of the life-* macro constants unless you thoroughly
85 ;; understand the `life-grim-reaper' function.
87 (defmacro life-life-char () ?@)
88 (defmacro life-death-char () (1+ (life-life-char)))
89 (defmacro life-birth-char () 3)
90 (defmacro life-void-char () ?\ )
92 (defmacro life-life-string () (char-to-string (life-life-char)))
93 (defmacro life-death-string () (char-to-string (life-death-char)))
94 (defmacro life-birth-string () (char-to-string (life-birth-char)))
95 (defmacro life-void-string () (char-to-string (life-void-char)))
96 (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]"))
98 (defmacro life-increment (variable) (list 'setq variable (list '1+ variable)))
101 ;; list of numbers that tell how many characters to move to get to
102 ;; each of a cell's eight neighbors.
103 (defvar life-neighbor-deltas nil)
105 ;; window display always starts here. Easier to deal with than
106 ;; (scroll-up) and (scroll-down) when trying to center the display.
107 (defvar life-window-start nil)
109 ;; For mode line
110 (defvar life-current-generation nil)
111 ;; Sadly, mode-line-format won't display numbers.
112 (defvar life-generation-string nil)
114 ;;;###autoload
115 (defun life (&optional sleeptime)
116 "Run Conway's Life simulation.
117 The starting pattern is randomly selected. Prefix arg (optional first
118 arg non-nil from a program) is the number of seconds to sleep between
119 generations (this defaults to 1)."
120 (interactive "p")
121 (or sleeptime (setq sleeptime 1))
122 (life-setup)
123 (catch 'life-exit
124 (while t
125 (let ((inhibit-quit t)
126 (inhibit-read-only t))
127 (life-display-generation sleeptime)
128 (life-grim-reaper)
129 (life-expand-plane-if-needed)
130 (life-increment-generation)))))
132 (define-derived-mode life-mode special-mode "Life"
133 "Major mode for the buffer of `life'."
134 (setq-local case-fold-search nil)
135 (setq-local truncate-lines t)
136 (setq-local show-trailing-whitespace nil)
137 (setq-local life-current-generation 0)
138 (setq-local life-generation-string "0")
139 (setq-local mode-line-buffer-identification '("Life: generation "
140 life-generation-string))
141 (setq-local fill-column (1- (window-width)))
142 (setq-local life-window-start 1)
143 (buffer-disable-undo))
145 (defun life-setup ()
146 (switch-to-buffer (get-buffer-create "*Life*") t)
147 (erase-buffer)
148 (life-mode)
149 ;; stuff in the random pattern
150 (let ((inhibit-read-only t))
151 (life-insert-random-pattern)
152 ;; make sure (life-life-char) is used throughout
153 (goto-char (point-min))
154 (while (re-search-forward (life-not-void-regexp) nil t)
155 (replace-match (life-life-string) t t))
156 ;; center the pattern horizontally
157 (goto-char (point-min))
158 (let ((n (/ (- fill-column (line-end-position)) 2)))
159 (while (not (eobp))
160 (indent-to n)
161 (forward-line)))
162 ;; center the pattern vertically
163 (let ((n (/ (- (1- (window-height))
164 (count-lines (point-min) (point-max)))
165 2)))
166 (goto-char (point-min))
167 (newline n)
168 (goto-char (point-max))
169 (newline n))
170 ;; pad lines out to fill-column
171 (goto-char (point-min))
172 (while (not (eobp))
173 (end-of-line)
174 (indent-to fill-column)
175 (move-to-column fill-column)
176 (delete-region (point) (progn (end-of-line) (point)))
177 (forward-line))
178 ;; expand tabs to spaces
179 (untabify (point-min) (point-max))
180 ;; before starting be sure the automaton has room to grow
181 (life-expand-plane-if-needed)
182 ;; compute initial neighbor deltas
183 (life-compute-neighbor-deltas)))
185 (defun life-compute-neighbor-deltas ()
186 (setq life-neighbor-deltas
187 (list -1 (- fill-column)
188 (- (1+ fill-column)) (- (+ 2 fill-column))
189 1 fill-column (1+ fill-column)
190 (+ 2 fill-column))))
192 (defun life-insert-random-pattern ()
193 (insert-rectangle
194 (elt life-patterns (random (length life-patterns))))
195 (insert ?\n))
197 (defun life-increment-generation ()
198 (life-increment life-current-generation)
199 (setq life-generation-string (int-to-string life-current-generation)))
201 (defun life-grim-reaper ()
202 ;; Clear the match information. Later we check to see if it
203 ;; is still clear, if so then all the cells have died.
204 (set-match-data nil)
205 (goto-char (point-min))
206 ;; For speed declare all local variable outside the loop.
207 (let (point char pivot living-neighbors list)
208 (while (search-forward (life-life-string) nil t)
209 (setq list life-neighbor-deltas
210 living-neighbors 0
211 pivot (1- (point)))
212 (while list
213 (setq point (+ pivot (car list))
214 char (char-after point))
215 (cond ((eq char (life-void-char))
216 (subst-char-in-region point (1+ point)
217 (life-void-char) 1 t))
218 ((< char 3)
219 (subst-char-in-region point (1+ point) char (1+ char) t))
220 ((< char 9)
221 (subst-char-in-region point (1+ point) char 9 t))
222 ((>= char (life-life-char))
223 (life-increment living-neighbors)))
224 (setq list (cdr list)))
225 (if (memq living-neighbors '(2 3))
227 (subst-char-in-region pivot (1+ pivot)
228 (life-life-char) (life-death-char) t))))
229 (if (null (match-beginning 0))
230 (life-extinct-quit))
231 (subst-char-in-region 1 (point-max) 9 (life-void-char) t)
232 (subst-char-in-region 1 (point-max) 1 (life-void-char) t)
233 (subst-char-in-region 1 (point-max) 2 (life-void-char) t)
234 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t)
235 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t))
237 (defun life-expand-plane-if-needed ()
238 (catch 'done
239 (goto-char (point-min))
240 (while (not (eobp))
241 ;; check for life at beginning or end of line. If found at
242 ;; either end, expand at both ends,
243 (cond ((or (eq (following-char) (life-life-char))
244 (eq (progn (end-of-line) (preceding-char)) (life-life-char)))
245 (goto-char (point-min))
246 (while (not (eobp))
247 (insert (life-void-char))
248 (end-of-line)
249 (insert (life-void-char))
250 (forward-char))
251 (setq fill-column (+ 2 fill-column))
252 (scroll-left 1)
253 (life-compute-neighbor-deltas)
254 (throw 'done t)))
255 (forward-line)))
256 (goto-char (point-min))
257 ;; check for life within the first two lines of the buffer.
258 ;; If present insert two lifeless lines at the beginning..
259 (cond ((search-forward (life-life-string)
260 (+ (point) fill-column fill-column 2) t)
261 (goto-char (point-min))
262 (insert-char (life-void-char) fill-column)
263 (insert ?\n)
264 (insert-char (life-void-char) fill-column)
265 (insert ?\n)
266 (setq life-window-start (+ life-window-start fill-column 1))))
267 (goto-char (point-max))
268 ;; check for life within the last two lines of the buffer.
269 ;; If present insert two lifeless lines at the end.
270 (cond ((search-backward (life-life-string)
271 (- (point) fill-column fill-column 2) t)
272 (goto-char (point-max))
273 (insert-char (life-void-char) fill-column)
274 (insert ?\n)
275 (insert-char (life-void-char) fill-column)
276 (insert ?\n)
277 (setq life-window-start (+ life-window-start fill-column 1)))))
279 (defun life-display-generation (sleeptime)
280 (goto-char life-window-start)
281 (recenter 0)
283 ;; Redisplay; if the user has hit a key, exit the loop.
284 (or (and (sit-for sleeptime) (< 0 sleeptime))
285 (not (input-pending-p))
286 (throw 'life-exit nil)))
288 (defun life-extinct-quit ()
289 (life-display-generation 0)
290 (signal 'life-extinct nil))
292 (define-error 'life-extinct "All life has perished" 'quit) ;FIXME: quit really?
294 (provide 'life)
296 ;;; life.el ends here