changeset 108016:6be35f3ece28

tetris.el: Use `define-derived-mode'; fix window selection; doc fixes. * play/tetris.el (tetris, tetris-update-speed-function) (tetris-tty-colors, tetris-x-colors, tetris-move-bottom) (tetris-move-left, tetris-move-right, tetris-rotate-prev) (tetris-rotate-next, tetris-end-game, tetris-start-game) (tetris-pause-game): Fix typos in docstrings. (tetris-mode-map, tetris-null-map): Move initialization into declaration. (tetris-mode): Define with `define-derived-mode'; set show-trailing-whitespace to nil. (tetris): Prefer window already displaying the "*Tetris*" buffer.
author Juanma Barranquero <lekktu@gmail.com>
date Wed, 21 Apr 2010 05:53:42 +0200
parents f3c91f3abae4
children 5eae2b195dc6
files lisp/ChangeLog lisp/play/tetris.el
diffstat 2 files changed, 48 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Tue Apr 20 20:41:01 2010 -0700
+++ b/lisp/ChangeLog	Wed Apr 21 05:53:42 2010 +0200
@@ -1,3 +1,17 @@
+2010-04-21  Juanma Barranquero  <lekktu@gmail.com>
+
+	Use `define-derived-mode'; fix window selection; doc fixes.
+	* play/tetris.el (tetris, tetris-update-speed-function)
+	(tetris-tty-colors, tetris-x-colors, tetris-move-bottom)
+	(tetris-move-left, tetris-move-right, tetris-rotate-prev)
+	(tetris-rotate-next, tetris-end-game, tetris-start-game)
+	(tetris-pause-game): Fix typos in docstrings.
+	(tetris-mode-map, tetris-null-map):
+	Move initialization into declaration.
+	(tetris-mode): Define with `define-derived-mode';
+	set show-trailing-whitespace to nil.
+	(tetris): Prefer window already displaying the "*Tetris*" buffer.
+
 2010-04-21  Karel Klíč  <kklic@redhat.com>
 
 	* files.el (backup-buffer): Handle SELinux context, and return it
--- a/lisp/play/tetris.el	Tue Apr 20 20:41:01 2010 -0700
+++ b/lisp/play/tetris.el	Wed Apr 21 05:53:42 2010 +0200
@@ -35,7 +35,7 @@
 ;; ;;;;;;;;;;;;; customization variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defgroup tetris nil
-  "Play a game of tetris."
+  "Play a game of Tetris."
   :prefix "tetris-"
   :group 'games)
 
@@ -61,10 +61,10 @@
 
 (defcustom tetris-update-speed-function
   'tetris-default-update-speed-function
-  "Function run whenever the Tetris score changes
+  "Function run whenever the Tetris score changes.
 Called with two arguments: (SHAPES ROWS)
-SHAPES is the number of shapes which have been dropped
-ROWS is the number of rows which have been completed
+SHAPES is the number of shapes which have been dropped.
+ROWS is the number of rows which have been completed.
 
 If the return value is a number, it is used as the timer period."
   :group 'tetris
@@ -77,7 +77,7 @@
 
 (defcustom tetris-tty-colors
   [nil "blue" "white" "yellow" "magenta" "cyan" "green" "red"]
-  "Vector of colors of the various shapes in text mode
+  "Vector of colors of the various shapes in text mode.
 Element 0 is ignored."
   :group 'tetris
   :type (let ((names `("Shape 1" "Shape 2" "Shape 3"
@@ -97,7 +97,7 @@
 
 (defcustom tetris-x-colors
   [nil [0 0 1] [0.7 0 1] [1 1 0] [1 0 1] [0 1 1] [0 1 0] [1 0 0]]
-  "Vector of colors of the various shapes
+  "Vector of colors of the various shapes.
 Element 0 is ignored."
   :group 'tetris
   :type 'sexp)
@@ -274,22 +274,22 @@
 ;; ;;;;;;;;;;;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defvar tetris-mode-map
-  (make-sparse-keymap 'tetris-mode-map))
-
-(define-key tetris-mode-map "n"		'tetris-start-game)
-(define-key tetris-mode-map "q"		'tetris-end-game)
-(define-key tetris-mode-map "p"		'tetris-pause-game)
+  (let ((map (make-sparse-keymap 'tetris-mode-map)))
+    (define-key map "n"		'tetris-start-game)
+    (define-key map "q"		'tetris-end-game)
+    (define-key map "p"		'tetris-pause-game)
 
-(define-key tetris-mode-map " "		'tetris-move-bottom)
-(define-key tetris-mode-map [left]	'tetris-move-left)
-(define-key tetris-mode-map [right]	'tetris-move-right)
-(define-key tetris-mode-map [up]	'tetris-rotate-prev)
-(define-key tetris-mode-map [down]	'tetris-rotate-next)
+    (define-key map " "		'tetris-move-bottom)
+    (define-key map [left]	'tetris-move-left)
+    (define-key map [right]	'tetris-move-right)
+    (define-key map [up]	'tetris-rotate-prev)
+    (define-key map [down]	'tetris-rotate-next)
+    map))
 
 (defvar tetris-null-map
-  (make-sparse-keymap 'tetris-null-map))
-
-(define-key tetris-null-map "n"		'tetris-start-game)
+  (let ((map (make-sparse-keymap 'tetris-null-map)))
+    (define-key map "n"		'tetris-start-game)
+    map))
 
 ;; ;;;;;;;;;;;;;;;; game functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
@@ -508,7 +508,7 @@
 	    (tetris-shape-done)))))
 
 (defun tetris-move-bottom ()
-  "Drops the shape to the bottom of the playing area"
+  "Drop the shape to the bottom of the playing area."
   (interactive)
   (if (not tetris-paused)
       (let ((hit nil))
@@ -521,7 +521,7 @@
         (tetris-shape-done))))
 
 (defun tetris-move-left ()
-  "Moves the shape one square to the left"
+  "Move the shape one square to the left."
   (interactive)
   (unless (or (= tetris-pos-x 0)
               tetris-paused)
@@ -532,7 +532,7 @@
     (tetris-draw-shape)))
 
 (defun tetris-move-right ()
-  "Moves the shape one square to the right"
+  "Move the shape one square to the right."
   (interactive)
   (unless (or (= (+ tetris-pos-x (tetris-shape-width))
                  tetris-width)
@@ -544,7 +544,7 @@
     (tetris-draw-shape)))
 
 (defun tetris-rotate-prev ()
-  "Rotates the shape clockwise"
+  "Rotate the shape clockwise."
   (interactive)
   (if (not tetris-paused)
       (progn (tetris-erase-shape)
@@ -554,7 +554,7 @@
              (tetris-draw-shape))))
 
 (defun tetris-rotate-next ()
-  "Rotates the shape anticlockwise"
+  "Rotate the shape anticlockwise."
   (interactive)
   (if (not tetris-paused)
       (progn
@@ -565,14 +565,14 @@
         (tetris-draw-shape))))
 
 (defun tetris-end-game ()
-  "Terminates the current game"
+  "Terminate the current game."
   (interactive)
   (gamegrid-kill-timer)
   (use-local-map tetris-null-map)
   (gamegrid-add-score tetris-score-file tetris-score))
 
 (defun tetris-start-game ()
-  "Starts a new game of Tetris"
+  "Start a new game of Tetris."
   (interactive)
   (tetris-reset-game)
   (use-local-map tetris-mode-map)
@@ -581,7 +581,7 @@
     (gamegrid-start-timer period 'tetris-update-game)))
 
 (defun tetris-pause-game ()
-  "Pauses (or resumes) the current game"
+  "Pause (or resume) the current game."
   (interactive)
   (setq tetris-paused (not tetris-paused))
   (message (and tetris-paused "Game paused (press p to resume)")))
@@ -591,21 +591,13 @@
 
 (put 'tetris-mode 'mode-class 'special)
 
-(defun tetris-mode ()
-  "A mode for playing Tetris.
-
-tetris-mode keybindings:
-   \\{tetris-mode-map}
-"
-  (kill-all-local-variables)
+(define-derived-mode tetris-mode nil "Tetris"
+  "A mode for playing Tetris."
 
   (add-hook 'kill-buffer-hook 'gamegrid-kill-timer nil t)
 
   (use-local-map tetris-null-map)
 
-  (setq major-mode 'tetris-mode)
-  (setq mode-name "Tetris")
-
   (unless (featurep 'emacs)
     (setq mode-popup-menu
 	  '("Tetris Commands"
@@ -617,12 +609,12 @@
 	    ["Resume"		tetris-pause-game
 	     (and (tetris-active-p) tetris-paused)])))
 
+  (setq show-trailing-whitespace nil)
+
   (setq gamegrid-use-glyphs tetris-use-glyphs)
   (setq gamegrid-use-color tetris-use-color)
 
-  (gamegrid-init (tetris-display-options))
-
-  (run-mode-hooks 'tetris-mode-hook))
+  (gamegrid-init (tetris-display-options)))
 
 ;;;###autoload
 (defun tetris ()
@@ -645,6 +637,8 @@
 "
   (interactive)
 
+  (select-window (or (get-buffer-window tetris-buffer-name)
+		     (selected-window)))
   (switch-to-buffer tetris-buffer-name)
   (gamegrid-kill-timer)
   (tetris-mode)