X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/acaf905b1130aae80fa59d2c861ffd4c8eb75486..97d44922da3c22b3973f95892bfa2ee4afc0ceac:/lisp/play/life.el diff --git a/lisp/play/life.el b/lisp/play/life.el index e9133f8486..5bf8a9b57b 100644 --- a/lisp/play/life.el +++ b/lisp/play/life.el @@ -1,9 +1,9 @@ ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs -;; Copyright (C) 1988, 2001-2012 Free Software Foundation, Inc. +;; Copyright (C) 1988, 2001-2015 Free Software Foundation, Inc. ;; Author: Kyle Jones -;; Maintainer: FSF +;; Maintainer: emacs-devel@gnu.org ;; Keywords: games ;; This file is part of GNU Emacs. @@ -111,9 +111,6 @@ ;; Sadly, mode-line-format won't display numbers. (defvar life-generation-string nil) -(defvar life-initialized nil - "Non-nil if `life' has been run at least once.") - ;;;###autoload (defun life (&optional sleeptime) "Run Conway's Life simulation. @@ -121,40 +118,36 @@ 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)) + (let ((inhibit-quit t) + (inhibit-read-only 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) +(define-derived-mode life-mode special-mode "Life" + "Major mode for the buffer of `life'." + (setq-local case-fold-search nil) + (setq-local truncate-lines t) + (setq-local show-trailing-whitespace nil) + (setq-local life-current-generation 0) + (setq-local life-generation-string "0") + (setq-local mode-line-buffer-identification '("Life: generation " + life-generation-string)) + (setq-local fill-column (1- (window-width))) + (setq-local life-window-start 1) + (buffer-disable-undo)) (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 - show-trailing-whitespace nil - 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)) - ;; stuff in the random pattern + (switch-to-buffer (get-buffer-create "*Life*") t) + (erase-buffer) + (life-mode) + ;; stuff in the random pattern + (let ((inhibit-read-only t)) (life-insert-random-pattern) ;; make sure (life-life-char) is used throughout (goto-char (point-min)) @@ -162,18 +155,18 @@ generations (this defaults to 1)." (replace-match (life-life-string) t t)) ;; center the pattern horizontally (goto-char (point-min)) - (setq n (/ (- fill-column (line-end-position)) 2)) - (while (not (eobp)) - (indent-to n) - (forward-line)) + (let ((n (/ (- fill-column (line-end-position)) 2))) + (while (not (eobp)) + (indent-to n) + (forward-line))) ;; center the pattern vertically - (setq n (/ (- (1- (window-height)) - (count-lines (point-min) (point-max))) - 2)) - (goto-char (point-min)) - (newline n) - (goto-char (point-max)) - (newline n) + (let ((n (/ (- (1- (window-height)) + (count-lines (point-min) (point-max))) + 2))) + (goto-char (point-min)) + (newline n) + (goto-char (point-max)) + (newline n)) ;; pad lines out to fill-column (goto-char (point-min)) (while (not (eobp)) @@ -296,8 +289,7 @@ generations (this defaults to 1)." (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") +(define-error 'life-extinct "All life has perished" 'quit) ;FIXME: quit really? (provide 'life)