Mercurial > emacs
annotate lisp/play/life.el @ 61914:f658c441541a
(resolve_face_name): Add arg SIGNAL_P. Calls changed.
Fix cyclic alias check. If alias loop is detected, signal
circular-list error if SIGNAL_P, and return Qdefault if !SIGNAL_P.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Thu, 28 Apr 2005 13:14:06 +0000 |
parents | a22adc2b72fb |
children | eaa9acd9122c b637c617432f |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1988, 2001, 2004 Free Software Foundation, Inc. |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
17976 | 5 ;; Author: Kyle Jones <kyleuunet.uu.net> |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
6 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
923
diff
changeset
|
7 ;; Keywords: games |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
8 |
53 | 9 ;; This file is part of GNU Emacs. |
10 | |
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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
13 ;; the Free Software Foundation; either version 2, or (at your option) |
53 | 14 ;; any later version. |
15 | |
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. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
53 | 25 |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
26 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
27 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
28 ;; A demonstrator for John Horton Conway's "Life" cellular automaton |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
29 ;; in Emacs Lisp. Picks a random one of a set of interesting Life |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2571
diff
changeset
|
30 ;; patterns and evolves it according to the familiar rules. |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
31 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
32 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
33 |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
34 (defvar life-patterns |
53 | 35 [("@@@" " @@" "@@@") |
36 ("@@@ @@@" "@@ @@ " "@@@ @@@") | |
37 ("@@@ @@@" "@@ @@" "@@@ @@@") | |
38 ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") | |
39 ("@@@@@@@@@@") | |
40 (" @@@@@@@@@@ " | |
41 " @@@@@@@@@@ " | |
42 " @@@@@@@@@@ " | |
43 "@@@@@@@@@@ " | |
44 "@@@@@@@@@@ ") | |
45 ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@") | |
46 ("@ @" "@ @" "@ @" | |
47 "@ @" "@ @" "@ @" | |
48 "@ @" "@ @" "@ @" | |
49 "@ @" "@ @" "@ @" | |
50 "@ @" "@ @" "@ @") | |
51 ("@@ " " @@ " " @@ " | |
52 " @@ " " @@ " " @@ " | |
53 " @@ " " @@ " " @@ " | |
54 " @@ " " @@ " " @@ " | |
55 " @@ " " @@ " " @@ " | |
56 " @@") | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
36750
diff
changeset
|
57 ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@" |
53 | 58 "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")] |
59 "Vector of rectangles containing some Life startup patterns.") | |
60 | |
61 ;; Macros are used macros for manifest constants instead of variables | |
62 ;; because the compiler will convert them to constants, which should | |
63 ;; eval faster than symbols. | |
64 ;; | |
65 ;; Don't change any of the life-* macro constants unless you thoroughly | |
66 ;; understand the `life-grim-reaper' function. | |
923 | 67 |
68 (defmacro life-life-char () ?@) | |
69 (defmacro life-death-char () (1+ (life-life-char))) | |
70 (defmacro life-birth-char () 3) | |
71 (defmacro life-void-char () ?\ ) | |
53 | 72 |
923 | 73 (defmacro life-life-string () (char-to-string (life-life-char))) |
74 (defmacro life-death-string () (char-to-string (life-death-char))) | |
75 (defmacro life-birth-string () (char-to-string (life-birth-char))) | |
76 (defmacro life-void-string () (char-to-string (life-void-char))) | |
77 (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]")) | |
53 | 78 |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
79 (defmacro life-increment (variable) (list 'setq variable (list '1+ variable))) |
923 | 80 |
53 | 81 |
82 ;; list of numbers that tell how many characters to move to get to | |
83 ;; each of a cell's eight neighbors. | |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
84 (defvar life-neighbor-deltas nil) |
53 | 85 |
86 ;; window display always starts here. Easier to deal with than | |
87 ;; (scroll-up) and (scroll-down) when trying to center the display. | |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
88 (defvar life-window-start nil) |
53 | 89 |
90 ;; For mode line | |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
91 (defvar life-current-generation nil) |
53 | 92 ;; Sadly, mode-line-format won't display numbers. |
58252
a22adc2b72fb
Maintainer is now FSF.
Luc Teirlinck <teirllm@auburn.edu>
parents:
52401
diff
changeset
|
93 (defvar life-generation-string nil) |
53 | 94 |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
95 (defvar life-initialized nil |
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
96 "Non-nil if `life' has been run at least once.") |
53 | 97 |
923 | 98 ;;;###autoload |
53 | 99 (defun life (&optional sleeptime) |
100 "Run Conway's Life simulation. | |
215 | 101 The starting pattern is randomly selected. Prefix arg (optional first |
102 arg non-nil from a program) is the number of seconds to sleep between | |
53 | 103 generations (this defaults to 1)." |
104 (interactive "p") | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
105 (or life-initialized |
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
106 (random t)) |
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
107 (setq life-initialized t) |
53 | 108 (or sleeptime (setq sleeptime 1)) |
109 (life-setup) | |
923 | 110 (catch 'life-exit |
111 (while t | |
112 (let ((inhibit-quit t)) | |
36750
83e8e419d94d
(life): Rearrange code so that all calls to
Gerd Moellmann <gerd@gnu.org>
parents:
21166
diff
changeset
|
113 (life-display-generation sleeptime) |
923 | 114 (life-grim-reaper) |
115 (life-expand-plane-if-needed) | |
36750
83e8e419d94d
(life): Rearrange code so that all calls to
Gerd Moellmann <gerd@gnu.org>
parents:
21166
diff
changeset
|
116 (life-increment-generation))))) |
53 | 117 |
2571
b65cf676a09b
All fsets changed to defaliases.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2307
diff
changeset
|
118 (defalias 'life-mode 'life) |
53 | 119 (put 'life-mode 'mode-class 'special) |
120 | |
121 (defun life-setup () | |
122 (let (n) | |
123 (switch-to-buffer (get-buffer-create "*Life*") t) | |
124 (erase-buffer) | |
125 (kill-all-local-variables) | |
126 (setq case-fold-search nil | |
127 mode-name "Life" | |
128 major-mode 'life-mode | |
129 truncate-lines t | |
130 life-current-generation 0 | |
131 life-generation-string "0" | |
132 mode-line-buffer-identification '("Life: generation " | |
133 life-generation-string) | |
134 fill-column (1- (window-width)) | |
135 life-window-start 1) | |
136 (buffer-disable-undo (current-buffer)) | |
137 ;; stuff in the random pattern | |
138 (life-insert-random-pattern) | |
139 ;; make sure (life-life-char) is used throughout | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
140 (goto-char (point-min)) |
53 | 141 (while (re-search-forward (life-not-void-regexp) nil t) |
142 (replace-match (life-life-string) t t)) | |
143 ;; center the pattern horizontally | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
144 (goto-char (point-min)) |
53 | 145 (setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2)) |
146 (while (not (eobp)) | |
147 (indent-to n) | |
148 (forward-line)) | |
149 ;; center the pattern vertically | |
150 (setq n (/ (- (1- (window-height)) | |
151 (count-lines (point-min) (point-max))) | |
152 2)) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
153 (goto-char (point-min)) |
53 | 154 (newline n) |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
155 (goto-char (point-max)) |
53 | 156 (newline n) |
157 ;; pad lines out to fill-column | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
158 (goto-char (point-min)) |
53 | 159 (while (not (eobp)) |
160 (end-of-line) | |
161 (indent-to fill-column) | |
162 (move-to-column fill-column) | |
163 (delete-region (point) (progn (end-of-line) (point))) | |
164 (forward-line)) | |
165 ;; expand tabs to spaces | |
166 (untabify (point-min) (point-max)) | |
167 ;; before starting be sure the automaton has room to grow | |
168 (life-expand-plane-if-needed) | |
169 ;; compute initial neighbor deltas | |
170 (life-compute-neighbor-deltas))) | |
171 | |
172 (defun life-compute-neighbor-deltas () | |
173 (setq life-neighbor-deltas | |
174 (list -1 (- fill-column) | |
175 (- (1+ fill-column)) (- (+ 2 fill-column)) | |
176 1 fill-column (1+ fill-column) | |
177 (+ 2 fill-column)))) | |
178 | |
179 (defun life-insert-random-pattern () | |
180 (insert-rectangle | |
4401
c5e18576d5f1
(life-insert-random-pattern): Simplify (% (abs (random)) N)
Paul Eggert <eggert@twinsun.com>
parents:
3591
diff
changeset
|
181 (elt life-patterns (random (length life-patterns)))) |
53 | 182 (insert ?\n)) |
183 | |
184 (defun life-increment-generation () | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
185 (life-increment life-current-generation) |
53 | 186 (setq life-generation-string (int-to-string life-current-generation))) |
187 | |
188 (defun life-grim-reaper () | |
189 ;; Clear the match information. Later we check to see if it | |
190 ;; is still clear, if so then all the cells have died. | |
21166
acffd9b0ac6d
(life-grim-reaper): store-match-data => set-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
17976
diff
changeset
|
191 (set-match-data nil) |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
192 (goto-char (point-min)) |
53 | 193 ;; For speed declare all local variable outside the loop. |
194 (let (point char pivot living-neighbors list) | |
195 (while (search-forward (life-life-string) nil t) | |
196 (setq list life-neighbor-deltas | |
197 living-neighbors 0 | |
198 pivot (1- (point))) | |
199 (while list | |
200 (setq point (+ pivot (car list)) | |
201 char (char-after point)) | |
202 (cond ((eq char (life-void-char)) | |
203 (subst-char-in-region point (1+ point) | |
204 (life-void-char) 1 t)) | |
205 ((< char 3) | |
206 (subst-char-in-region point (1+ point) char (1+ char) t)) | |
207 ((< char 9) | |
208 (subst-char-in-region point (1+ point) char 9 t)) | |
209 ((>= char (life-life-char)) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
210 (life-increment living-neighbors))) |
53 | 211 (setq list (cdr list))) |
212 (if (memq living-neighbors '(2 3)) | |
213 () | |
214 (subst-char-in-region pivot (1+ pivot) | |
215 (life-life-char) (life-death-char) t)))) | |
216 (if (null (match-beginning 0)) | |
217 (life-extinct-quit)) | |
218 (subst-char-in-region 1 (point-max) 9 (life-void-char) t) | |
219 (subst-char-in-region 1 (point-max) 1 (life-void-char) t) | |
220 (subst-char-in-region 1 (point-max) 2 (life-void-char) t) | |
221 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t) | |
222 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t)) | |
223 | |
224 (defun life-expand-plane-if-needed () | |
225 (catch 'done | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
226 (goto-char (point-min)) |
53 | 227 (while (not (eobp)) |
228 ;; check for life at beginning or end of line. If found at | |
229 ;; either end, expand at both ends, | |
230 (cond ((or (eq (following-char) (life-life-char)) | |
231 (eq (progn (end-of-line) (preceding-char)) (life-life-char))) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
232 (goto-char (point-min)) |
53 | 233 (while (not (eobp)) |
234 (insert (life-void-char)) | |
235 (end-of-line) | |
236 (insert (life-void-char)) | |
237 (forward-char)) | |
238 (setq fill-column (+ 2 fill-column)) | |
239 (scroll-left 1) | |
240 (life-compute-neighbor-deltas) | |
241 (throw 'done t))) | |
242 (forward-line))) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
243 (goto-char (point-min)) |
53 | 244 ;; check for life within the first two lines of the buffer. |
245 ;; If present insert two lifeless lines at the beginning.. | |
246 (cond ((search-forward (life-life-string) | |
247 (+ (point) fill-column fill-column 2) t) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
248 (goto-char (point-min)) |
53 | 249 (insert-char (life-void-char) fill-column) |
250 (insert ?\n) | |
251 (insert-char (life-void-char) fill-column) | |
252 (insert ?\n) | |
253 (setq life-window-start (+ life-window-start fill-column 1)))) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
254 (goto-char (point-max)) |
53 | 255 ;; check for life within the last two lines of the buffer. |
256 ;; If present insert two lifeless lines at the end. | |
257 (cond ((search-backward (life-life-string) | |
258 (- (point) fill-column fill-column 2) t) | |
12948
052c1dfa4efa
(abs): Function definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
4401
diff
changeset
|
259 (goto-char (point-max)) |
53 | 260 (insert-char (life-void-char) fill-column) |
261 (insert ?\n) | |
262 (insert-char (life-void-char) fill-column) | |
263 (insert ?\n) | |
264 (setq life-window-start (+ life-window-start fill-column 1))))) | |
265 | |
266 (defun life-display-generation (sleeptime) | |
267 (goto-char life-window-start) | |
268 (recenter 0) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
36750
diff
changeset
|
269 |
923 | 270 ;; Redisplay; if the user has hit a key, exit the loop. |
271 (or (eq t (sit-for sleeptime)) | |
272 (throw 'life-exit nil))) | |
53 | 273 |
274 (defun life-extinct-quit () | |
275 (life-display-generation 0) | |
276 (signal 'life-extinct nil)) | |
277 | |
278 (put 'life-extinct 'error-conditions '(life-extinct quit)) | |
279 (put 'life-extinct 'error-message "All life has perished") | |
584 | 280 |
281 (provide 'life) | |
282 | |
52401 | 283 ;;; arch-tag: e9373544-755e-42f5-a9a1-4d4c422bb97a |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
284 ;;; life.el ends here |