Commit | Line | Data |
---|---|---|
80a677d9 JA |
1 | ;; Conway's `Life' for GNU Emacs |
2 | ;; Copyright (C) 1988 Free Software Foundation, Inc. | |
3 | ;; Contributed by Kyle Jones, talos!kjones@uunet.uu.net | |
4 | ||
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
9 | ;; the Free Software Foundation; either version 1, or (at your option) | |
10 | ;; any later version. | |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
19 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | ||
80a677d9 JA |
21 | (defconst life-patterns |
22 | [("@@@" " @@" "@@@") | |
23 | ("@@@ @@@" "@@ @@ " "@@@ @@@") | |
24 | ("@@@ @@@" "@@ @@" "@@@ @@@") | |
25 | ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") | |
26 | ("@@@@@@@@@@") | |
27 | (" @@@@@@@@@@ " | |
28 | " @@@@@@@@@@ " | |
29 | " @@@@@@@@@@ " | |
30 | "@@@@@@@@@@ " | |
31 | "@@@@@@@@@@ ") | |
32 | ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@") | |
33 | ("@ @" "@ @" "@ @" | |
34 | "@ @" "@ @" "@ @" | |
35 | "@ @" "@ @" "@ @" | |
36 | "@ @" "@ @" "@ @" | |
37 | "@ @" "@ @" "@ @") | |
38 | ("@@ " " @@ " " @@ " | |
39 | " @@ " " @@ " " @@ " | |
40 | " @@ " " @@ " " @@ " | |
41 | " @@ " " @@ " " @@ " | |
42 | " @@ " " @@ " " @@ " | |
43 | " @@") | |
44 | ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@" | |
45 | "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")] | |
46 | "Vector of rectangles containing some Life startup patterns.") | |
47 | ||
48 | ;; Macros are used macros for manifest constants instead of variables | |
49 | ;; because the compiler will convert them to constants, which should | |
50 | ;; eval faster than symbols. | |
51 | ;; | |
52 | ;; The (require) wrapping forces the compiler to eval these macros at | |
53 | ;; compile time. This would not be necessary if we did not use macros | |
54 | ;; inside of macros, which the compiler doesn't seem to check for. | |
55 | ;; | |
56 | ;; Don't change any of the life-* macro constants unless you thoroughly | |
57 | ;; understand the `life-grim-reaper' function. | |
58 | (require | |
59 | (progn | |
60 | (defmacro life-life-char () ?@) | |
61 | (defmacro life-death-char () (1+ (life-life-char))) | |
62 | (defmacro life-birth-char () 3) | |
63 | (defmacro life-void-char () ?\ ) | |
64 | ||
65 | (defmacro life-life-string () (char-to-string (life-life-char))) | |
66 | (defmacro life-death-string () (char-to-string (life-death-char))) | |
67 | (defmacro life-birth-string () (char-to-string (life-birth-char))) | |
68 | (defmacro life-void-string () (char-to-string (life-void-char))) | |
69 | (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]")) | |
70 | ||
71 | ;; try to optimize the (goto-char (point-min)) & (goto-char (point-max)) | |
72 | ;; idioms. This depends on goto-char's not griping if we underrshoot | |
73 | ;; or overshoot beginning or end of buffer. | |
74 | (defmacro goto-beginning-of-buffer () '(goto-char 1)) | |
75 | (defmacro maxint () (lsh (lsh (lognot 0) 1) -1)) | |
76 | (defmacro goto-end-of-buffer () '(goto-char (maxint))) | |
77 | ||
78 | (defmacro increment (variable) (list 'setq variable (list '1+ variable))) | |
79 | 'life)) | |
80 | ||
81 | ;; list of numbers that tell how many characters to move to get to | |
82 | ;; each of a cell's eight neighbors. | |
83 | (defconst life-neighbor-deltas nil) | |
84 | ||
85 | ;; window display always starts here. Easier to deal with than | |
86 | ;; (scroll-up) and (scroll-down) when trying to center the display. | |
87 | (defconst life-window-start nil) | |
88 | ||
89 | ;; For mode line | |
90 | (defconst life-current-generation nil) | |
91 | ;; Sadly, mode-line-format won't display numbers. | |
92 | (defconst life-generation-string nil) | |
93 | ||
94 | (defun abs (n) (if (< n 0) (- n) n)) | |
95 | ||
96 | (defun life (&optional sleeptime) | |
97 | "Run Conway's Life simulation. | |
e3f99b91 JB |
98 | The starting pattern is randomly selected. Prefix arg (optional first |
99 | arg non-nil from a program) is the number of seconds to sleep between | |
80a677d9 JA |
100 | generations (this defaults to 1)." |
101 | (interactive "p") | |
102 | (or sleeptime (setq sleeptime 1)) | |
103 | (life-setup) | |
104 | (life-display-generation sleeptime) | |
105 | (while t | |
106 | (let ((inhibit-quit t)) | |
107 | (life-grim-reaper) | |
108 | (life-expand-plane-if-needed) | |
109 | (life-increment-generation) | |
110 | (life-display-generation sleeptime)))) | |
111 | ||
112 | (fset 'life-mode 'life) | |
113 | (put 'life-mode 'mode-class 'special) | |
114 | ||
115 | (random t) | |
116 | ||
117 | (defun life-setup () | |
118 | (let (n) | |
119 | (switch-to-buffer (get-buffer-create "*Life*") t) | |
120 | (erase-buffer) | |
121 | (kill-all-local-variables) | |
122 | (setq case-fold-search nil | |
123 | mode-name "Life" | |
124 | major-mode 'life-mode | |
125 | truncate-lines t | |
126 | life-current-generation 0 | |
127 | life-generation-string "0" | |
128 | mode-line-buffer-identification '("Life: generation " | |
129 | life-generation-string) | |
130 | fill-column (1- (window-width)) | |
131 | life-window-start 1) | |
132 | (buffer-disable-undo (current-buffer)) | |
133 | ;; stuff in the random pattern | |
134 | (life-insert-random-pattern) | |
135 | ;; make sure (life-life-char) is used throughout | |
136 | (goto-beginning-of-buffer) | |
137 | (while (re-search-forward (life-not-void-regexp) nil t) | |
138 | (replace-match (life-life-string) t t)) | |
139 | ;; center the pattern horizontally | |
140 | (goto-beginning-of-buffer) | |
141 | (setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2)) | |
142 | (while (not (eobp)) | |
143 | (indent-to n) | |
144 | (forward-line)) | |
145 | ;; center the pattern vertically | |
146 | (setq n (/ (- (1- (window-height)) | |
147 | (count-lines (point-min) (point-max))) | |
148 | 2)) | |
149 | (goto-beginning-of-buffer) | |
150 | (newline n) | |
151 | (goto-end-of-buffer) | |
152 | (newline n) | |
153 | ;; pad lines out to fill-column | |
154 | (goto-beginning-of-buffer) | |
155 | (while (not (eobp)) | |
156 | (end-of-line) | |
157 | (indent-to fill-column) | |
158 | (move-to-column fill-column) | |
159 | (delete-region (point) (progn (end-of-line) (point))) | |
160 | (forward-line)) | |
161 | ;; expand tabs to spaces | |
162 | (untabify (point-min) (point-max)) | |
163 | ;; before starting be sure the automaton has room to grow | |
164 | (life-expand-plane-if-needed) | |
165 | ;; compute initial neighbor deltas | |
166 | (life-compute-neighbor-deltas))) | |
167 | ||
168 | (defun life-compute-neighbor-deltas () | |
169 | (setq life-neighbor-deltas | |
170 | (list -1 (- fill-column) | |
171 | (- (1+ fill-column)) (- (+ 2 fill-column)) | |
172 | 1 fill-column (1+ fill-column) | |
173 | (+ 2 fill-column)))) | |
174 | ||
175 | (defun life-insert-random-pattern () | |
176 | (insert-rectangle | |
177 | (elt life-patterns (% (abs (random)) (length life-patterns)))) | |
178 | (insert ?\n)) | |
179 | ||
180 | (defun life-increment-generation () | |
181 | (increment life-current-generation) | |
182 | (setq life-generation-string (int-to-string life-current-generation))) | |
183 | ||
184 | (defun life-grim-reaper () | |
185 | ;; Clear the match information. Later we check to see if it | |
186 | ;; is still clear, if so then all the cells have died. | |
187 | (store-match-data nil) | |
188 | (goto-beginning-of-buffer) | |
189 | ;; For speed declare all local variable outside the loop. | |
190 | (let (point char pivot living-neighbors list) | |
191 | (while (search-forward (life-life-string) nil t) | |
192 | (setq list life-neighbor-deltas | |
193 | living-neighbors 0 | |
194 | pivot (1- (point))) | |
195 | (while list | |
196 | (setq point (+ pivot (car list)) | |
197 | char (char-after point)) | |
198 | (cond ((eq char (life-void-char)) | |
199 | (subst-char-in-region point (1+ point) | |
200 | (life-void-char) 1 t)) | |
201 | ((< char 3) | |
202 | (subst-char-in-region point (1+ point) char (1+ char) t)) | |
203 | ((< char 9) | |
204 | (subst-char-in-region point (1+ point) char 9 t)) | |
205 | ((>= char (life-life-char)) | |
206 | (increment living-neighbors))) | |
207 | (setq list (cdr list))) | |
208 | (if (memq living-neighbors '(2 3)) | |
209 | () | |
210 | (subst-char-in-region pivot (1+ pivot) | |
211 | (life-life-char) (life-death-char) t)))) | |
212 | (if (null (match-beginning 0)) | |
213 | (life-extinct-quit)) | |
214 | (subst-char-in-region 1 (point-max) 9 (life-void-char) t) | |
215 | (subst-char-in-region 1 (point-max) 1 (life-void-char) t) | |
216 | (subst-char-in-region 1 (point-max) 2 (life-void-char) t) | |
217 | (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t) | |
218 | (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t)) | |
219 | ||
220 | (defun life-expand-plane-if-needed () | |
221 | (catch 'done | |
222 | (goto-beginning-of-buffer) | |
223 | (while (not (eobp)) | |
224 | ;; check for life at beginning or end of line. If found at | |
225 | ;; either end, expand at both ends, | |
226 | (cond ((or (eq (following-char) (life-life-char)) | |
227 | (eq (progn (end-of-line) (preceding-char)) (life-life-char))) | |
228 | (goto-beginning-of-buffer) | |
229 | (while (not (eobp)) | |
230 | (insert (life-void-char)) | |
231 | (end-of-line) | |
232 | (insert (life-void-char)) | |
233 | (forward-char)) | |
234 | (setq fill-column (+ 2 fill-column)) | |
235 | (scroll-left 1) | |
236 | (life-compute-neighbor-deltas) | |
237 | (throw 'done t))) | |
238 | (forward-line))) | |
239 | (goto-beginning-of-buffer) | |
240 | ;; check for life within the first two lines of the buffer. | |
241 | ;; If present insert two lifeless lines at the beginning.. | |
242 | (cond ((search-forward (life-life-string) | |
243 | (+ (point) fill-column fill-column 2) t) | |
244 | (goto-beginning-of-buffer) | |
245 | (insert-char (life-void-char) fill-column) | |
246 | (insert ?\n) | |
247 | (insert-char (life-void-char) fill-column) | |
248 | (insert ?\n) | |
249 | (setq life-window-start (+ life-window-start fill-column 1)))) | |
250 | (goto-end-of-buffer) | |
251 | ;; check for life within the last two lines of the buffer. | |
252 | ;; If present insert two lifeless lines at the end. | |
253 | (cond ((search-backward (life-life-string) | |
254 | (- (point) fill-column fill-column 2) t) | |
255 | (goto-end-of-buffer) | |
256 | (insert-char (life-void-char) fill-column) | |
257 | (insert ?\n) | |
258 | (insert-char (life-void-char) fill-column) | |
259 | (insert ?\n) | |
260 | (setq life-window-start (+ life-window-start fill-column 1))))) | |
261 | ||
262 | (defun life-display-generation (sleeptime) | |
263 | (goto-char life-window-start) | |
264 | (recenter 0) | |
265 | (sit-for sleeptime)) | |
266 | ||
267 | (defun life-extinct-quit () | |
268 | (life-display-generation 0) | |
269 | (signal 'life-extinct nil)) | |
270 | ||
271 | (put 'life-extinct 'error-conditions '(life-extinct quit)) | |
272 | (put 'life-extinct 'error-message "All life has perished") | |
49116ac0 JB |
273 | |
274 | (provide 'life) | |
275 |