Preserve point when doing untabify
[emacs.git] / lisp / play / gamegrid.el
blobe245e70a55c7c36cb67d36b1ab60825016dc06a7
1 ;;; gamegrid.el --- library for implementing grid-based games on Emacs
3 ;; Copyright (C) 1997-1998, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Glynn Clements <glynn@sensei.co.uk>
6 ;; Version: 1.02
7 ;; Created: 1997-08-13
8 ;; Keywords: games
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile
30 (require 'cl))
32 ;; ;;;;;;;;;;;;; buffer-local variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 (defvar gamegrid-use-glyphs t
35 "Non-nil means use glyphs when available.")
37 (defvar gamegrid-use-color t
38 "Non-nil means use color when available.")
40 (defvar gamegrid-font "-*-courier-medium-r-*-*-*-140-100-75-*-*-iso8859-*"
41 "Name of the font used in X mode.")
43 (defvar gamegrid-face nil
44 "Indicates the face to use as a default.")
45 (make-variable-buffer-local 'gamegrid-face)
47 (defvar gamegrid-display-options nil)
49 (defvar gamegrid-buffer-width 0)
50 (defvar gamegrid-buffer-height 0)
51 (defvar gamegrid-blank 0)
53 (defvar gamegrid-timer nil)
55 (defvar gamegrid-display-mode nil)
57 (defvar gamegrid-display-table)
59 (defvar gamegrid-face-table nil)
61 (defvar gamegrid-buffer-start 1)
63 (defvar gamegrid-score-file-length 50
64 "Number of high scores to keep.")
66 (defvar gamegrid-user-score-file-directory
67 (locate-user-emacs-file "games/")
68 "A directory for game scores which can't be shared.
69 If Emacs was built without support for shared game scores, then this
70 directory will be used.")
72 (make-variable-buffer-local 'gamegrid-use-glyphs)
73 (make-variable-buffer-local 'gamegrid-use-color)
74 (make-variable-buffer-local 'gamegrid-font)
75 (make-variable-buffer-local 'gamegrid-display-options)
76 (make-variable-buffer-local 'gamegrid-buffer-width)
77 (make-variable-buffer-local 'gamegrid-buffer-height)
78 (make-variable-buffer-local 'gamegrid-blank)
79 (make-variable-buffer-local 'gamegrid-timer)
80 (make-variable-buffer-local 'gamegrid-display-mode)
81 (make-variable-buffer-local 'gamegrid-display-table)
82 (make-variable-buffer-local 'gamegrid-face-table)
83 (make-variable-buffer-local 'gamegrid-buffer-start)
84 (make-variable-buffer-local 'gamegrid-score-file-length)
86 ;; ;;;;;;;;;;;;; global variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 (defvar gamegrid-grid-x-face nil)
89 (defvar gamegrid-mono-x-face nil)
90 (defvar gamegrid-mono-tty-face nil)
92 ;; ;;;;;;;;;;;;; constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 (defconst gamegrid-glyph-height 16)
96 (defconst gamegrid-xpm "\
97 /* XPM */
98 static char *noname[] = {
99 /* width height ncolors chars_per_pixel */
100 \"16 16 3 1\",
101 /* colors */
102 \"+ s col1\",
103 \". s col2\",
104 \"- s col3\",
105 /* pixels */
106 \"---------------+\",
107 \"--------------++\",
108 \"--............++\",
109 \"--............++\",
110 \"--............++\",
111 \"--............++\",
112 \"--............++\",
113 \"--............++\",
114 \"--............++\",
115 \"--............++\",
116 \"--............++\",
117 \"--............++\",
118 \"--............++\",
119 \"--............++\",
120 \"-+++++++++++++++\",
121 \"++++++++++++++++\"
124 "XPM format image used for each square")
126 (defvar gamegrid-xbm "\
127 /* gamegrid XBM */
128 #define gamegrid_width 16
129 #define gamegrid_height 16
130 static unsigned char gamegrid_bits[] = {
131 0xff, 0xff, 0xff, 0x7f, 0xff, 0x3f, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
132 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a, 0x57, 0x15, 0xaf, 0x0a,
133 0x57, 0x15, 0x07, 0x00, 0x03, 0x00, 0x01, 0x00 };"
134 "XBM format image used for each square.")
136 ;; ;;;;;;;;;;;;;;;; miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 (defsubst gamegrid-characterp (arg)
139 (if (fboundp 'characterp)
140 (characterp arg)
141 (integerp arg)))
143 (defsubst gamegrid-event-x (event)
144 (if (fboundp 'event-x)
145 (event-x event)
146 (car (posn-col-row (event-end event)))))
148 (defsubst gamegrid-event-y (event)
149 (if (fboundp 'event-y)
150 (event-y event)
151 (cdr (posn-col-row (event-end event)))))
153 ;; ;;;;;;;;;;;;; display functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 (defun gamegrid-color (color shade)
156 (let* ((v (floor (* shade 255)))
157 (r (* v (aref color 0)))
158 (g (* v (aref color 1)))
159 (b (* v (aref color 2))))
160 (format "#%02x%02x%02x" r g b)))
162 (defun gamegrid-set-font (face)
163 (if gamegrid-font
164 (condition-case nil
165 (set-face-font face gamegrid-font)
166 (error nil))))
168 (defun gamegrid-setup-face (face color)
169 (set-face-foreground face color)
170 (set-face-background face color)
171 (gamegrid-set-font face)
172 (condition-case nil
173 (set-face-background-pixmap face [nothing]);; XEmacs
174 (error nil))
175 (condition-case nil
176 (set-face-background-pixmap face nil);; Emacs
177 (error nil)))
179 (defun gamegrid-make-mono-tty-face ()
180 (let ((face (make-face 'gamegrid-mono-tty-face)))
181 (set-face-inverse-video-p face t)
182 face))
184 (defun gamegrid-make-color-tty-face (color)
185 (let* ((color-str (if (symbolp color) (symbol-value color) color))
186 (name (intern (format "gamegrid-color-tty-face-%s" color-str)))
187 (face (make-face name)))
188 (gamegrid-setup-face face color-str)
189 face))
191 (defun gamegrid-make-grid-x-face ()
192 (let ((face (make-face 'gamegrid-x-border-face)))
193 (gamegrid-set-font face)
194 face))
196 (defun gamegrid-make-mono-x-face ()
197 (let ((face (make-face 'gamegrid-mono-x-face))
198 (color (face-foreground 'default)))
199 (if (null color)
200 (setq color
201 (cdr-safe (assq 'foreground-color (frame-parameters)))))
202 (gamegrid-setup-face face color)
203 face))
205 (defun gamegrid-make-color-x-face (color)
206 (let* ((hex (gamegrid-color color 1.0))
207 (name (intern (format "gamegrid-color-x-face-%s" hex)))
208 (face (make-face name)))
209 (gamegrid-setup-face face hex)
210 face))
212 (defun gamegrid-make-face (data-spec-list color-spec-list)
213 (let ((data (gamegrid-match-spec-list data-spec-list))
214 (color (gamegrid-match-spec-list color-spec-list)))
215 (case data
216 (color-x
217 (gamegrid-make-color-x-face color))
218 (grid-x
219 (unless gamegrid-grid-x-face
220 (setq gamegrid-grid-x-face (gamegrid-make-grid-x-face)))
221 gamegrid-grid-x-face)
222 (mono-x
223 (unless gamegrid-mono-x-face
224 (setq gamegrid-mono-x-face (gamegrid-make-mono-x-face)))
225 gamegrid-mono-x-face)
226 (color-tty
227 (gamegrid-make-color-tty-face color))
228 (mono-tty
229 (unless gamegrid-mono-tty-face
230 (setq gamegrid-mono-tty-face (gamegrid-make-mono-tty-face)))
231 gamegrid-mono-tty-face))))
233 (defun gamegrid-colorize-glyph (color)
234 (find-image `((:type xpm :data ,gamegrid-xpm
235 :ascent center
236 :color-symbols
237 (("col1" . ,(gamegrid-color color 0.6))
238 ("col2" . ,(gamegrid-color color 0.8))
239 ("col3" . ,(gamegrid-color color 1.0))))
240 (:type xbm :data ,gamegrid-xbm
241 :ascent center
242 :foreground ,(gamegrid-color color 1.0)
243 :background ,(gamegrid-color color 0.5)))))
245 (defun gamegrid-match-spec (spec)
246 (let ((locale (car spec))
247 (value (cadr spec)))
248 (and (or (eq locale t)
249 (and (listp locale)
250 (memq gamegrid-display-mode locale))
251 (and (symbolp locale)
252 (eq gamegrid-display-mode locale)))
253 value)))
255 (defun gamegrid-match-spec-list (spec-list)
256 (and spec-list
257 (or (gamegrid-match-spec (car spec-list))
258 (gamegrid-match-spec-list (cdr spec-list)))))
260 (defun gamegrid-make-glyph (data-spec-list color-spec-list)
261 (let ((data (gamegrid-match-spec-list data-spec-list))
262 (color (gamegrid-match-spec-list color-spec-list)))
263 (cond ((gamegrid-characterp data)
264 (vector data))
265 ((eq data 'colorize)
266 (gamegrid-colorize-glyph color))
267 ((listp data)
268 (find-image data)) ;untested!
269 ((vectorp data)
270 (gamegrid-make-image-from-vector data)))))
272 (defun gamegrid-make-image-from-vector (vect)
273 "Convert an XEmacs style \"glyph\" to an image-spec."
274 (let ((l (list 'image :type)))
275 (dotimes (n (length vect))
276 (setf l (nconc l (list (aref vect n)))))
277 (nconc l (list :ascent 'center))))
279 (defun gamegrid-display-type ()
280 (cond ((and gamegrid-use-glyphs
281 (display-images-p))
282 'glyph)
283 ((and gamegrid-use-color
284 (display-graphic-p)
285 (display-color-p))
286 'color-x)
287 ((display-graphic-p)
288 'mono-x)
289 ((and gamegrid-use-color
290 (display-color-p))
291 'color-tty)
292 ((display-multi-font-p) ;???
293 'mono-tty)
295 'emacs-tty)))
297 (defun gamegrid-set-display-table ()
298 (if (featurep 'xemacs)
299 (add-spec-to-specifier current-display-table
300 gamegrid-display-table
301 (current-buffer)
303 'remove-locale)
304 (setq buffer-display-table gamegrid-display-table)))
306 (declare-function image-size "image.c" (spec &optional pixels frame))
308 (defun gamegrid-setup-default-font ()
309 (setq gamegrid-face
310 (copy-face 'default
311 (intern (concat "gamegrid-face-" (buffer-name)))))
312 (when (eq gamegrid-display-mode 'glyph)
313 (let ((max-height nil))
314 (loop for c from 0 to 255 do
315 (let ((glyph (aref gamegrid-display-table c)))
316 (when (and (listp glyph) (eq (car glyph) 'image))
317 (let ((height (cdr (image-size glyph))))
318 (if (or (null max-height)
319 (< max-height height))
320 (setq max-height height))))))
321 (when (and max-height (< max-height 1))
322 (let ((default-font-height (face-attribute 'default :height))
323 (resy (/ (display-pixel-height) (/ (display-mm-height) 25.4)))
324 point-size pixel-size)
325 (setq point-size (/ (* (float default-font-height) max-height) 10)
326 pixel-size (floor (* resy (/ point-size 72.27)))
327 point-size (* (/ pixel-size resy) 72.27))
328 (face-spec-set gamegrid-face
329 `((t :height ,(floor (* point-size 10))))))))))
331 (defun gamegrid-initialize-display ()
332 (setq gamegrid-display-mode (gamegrid-display-type))
333 (setq gamegrid-display-table (make-display-table))
334 (setq gamegrid-face-table (make-vector 256 nil))
335 (loop for c from 0 to 255 do
336 (let* ((spec (aref gamegrid-display-options c))
337 (glyph (gamegrid-make-glyph (car spec) (caddr spec)))
338 (face (gamegrid-make-face (cadr spec) (caddr spec))))
339 (aset gamegrid-face-table c face)
340 (aset gamegrid-display-table c glyph)))
341 (gamegrid-setup-default-font)
342 (gamegrid-set-display-table)
343 (setq cursor-type nil))
346 (defun gamegrid-set-face (c)
347 (if (eq gamegrid-display-mode 'glyph)
348 (add-text-properties (1- (point)) (point)
349 (list 'display (list (aref gamegrid-display-table c))))
350 (put-text-property (1- (point))
351 (point)
352 'face
353 (aref gamegrid-face-table c))))
355 (defun gamegrid-cell-offset (x y)
356 (+ gamegrid-buffer-start
357 (* (1+ gamegrid-buffer-width) y)
360 ;; ;;;;;;;;;;;;;;;; grid functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
362 (defun gamegrid-get-cell (x y)
363 (char-after (gamegrid-cell-offset x y)))
365 (defun gamegrid-set-cell (x y c)
366 (save-excursion
367 (let ((buffer-read-only nil))
368 (goto-char (gamegrid-cell-offset x y))
369 (delete-char 1)
370 (insert-char c 1)
371 (gamegrid-set-face c))))
373 (defun gamegrid-init-buffer (width height blank)
374 (setq gamegrid-buffer-width width
375 gamegrid-buffer-height height)
376 (let ((line (concat
377 (make-string width blank)
378 "\n"))
379 (buffer-read-only nil))
380 (erase-buffer)
381 (setq gamegrid-buffer-start (point))
382 (dotimes (i height)
383 (insert line))
384 ;; Adjust the height of the default face to the height of the
385 ;; images. Unlike XEmacs, Emacs doesn't allow to make the default
386 ;; face buffer-local; so we do this with an overlay.
387 (when (eq gamegrid-display-mode 'glyph)
388 (overlay-put (make-overlay (point-min) (point-max))
389 'face gamegrid-face))
390 (goto-char (point-min))))
392 (defun gamegrid-init (options)
393 (setq buffer-read-only t
394 truncate-lines t
395 line-spacing 0
396 gamegrid-display-options options)
397 (buffer-disable-undo (current-buffer))
398 (gamegrid-initialize-display))
400 ;; ;;;;;;;;;;;;;;;; timer functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
402 (defun gamegrid-start-timer (period func)
403 (setq gamegrid-timer
404 (if (featurep 'xemacs)
405 (start-itimer "Gamegrid"
406 func
407 period
408 period
411 (current-buffer))
412 (run-with-timer period
413 period
414 func
415 (current-buffer)))))
417 (defun gamegrid-set-timer (delay)
418 (if gamegrid-timer
419 (if (fboundp 'set-itimer-restart)
420 (set-itimer-restart gamegrid-timer delay)
421 (timer-set-time gamegrid-timer
422 (list (aref gamegrid-timer 1)
423 (aref gamegrid-timer 2)
424 (aref gamegrid-timer 3))
425 delay))))
427 (defun gamegrid-kill-timer ()
428 (if gamegrid-timer
429 (if (featurep 'xemacs)
430 (delete-itimer gamegrid-timer)
431 (cancel-timer gamegrid-timer)))
432 (setq gamegrid-timer nil))
434 ;; ;;;;;;;;;;;;;;; high score functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436 (defun gamegrid-add-score (file score)
437 "Add the current score to the high score file.
439 On POSIX systems there may be a shared game directory for all users in
440 which the scorefiles are kept. On such systems Emacs doesn't create
441 the score file FILE in this directory, if it doesn't already exist.
442 In this case Emacs searches for FILE in the directory specified by
443 `gamegrid-user-score-file-directory' and creates it there, if
444 necessary.
446 To add the score file for a game to the system wide shared game
447 directory, create the file with the shell command \"touch\" in this
448 directory and make sure that it is owned by the correct user and
449 group. You probably need special user privileges to do this.
451 On non-POSIX systems Emacs searches for FILE in the directory
452 specified by the variable `temporary-file-directory'. If necessary,
453 FILE is created there."
454 (case system-type
455 ((ms-dos windows-nt)
456 (gamegrid-add-score-insecure file score))
458 (gamegrid-add-score-with-update-game-score file score))))
461 ;; On POSIX systems there are four cases to distinguish:
463 ;; 1. FILE is an absolute filename. Then it should be a file in
464 ;; temporary file directory. This is the way,
465 ;; `gamegrid-add-score' was supposed to be used in the past and
466 ;; is covered here for backward-compatibility.
468 ;; 2. The helper program "update-game-score" is setuid and the
469 ;; file FILE does already exist in a system wide shared game
470 ;; directory. This should be the normal case on POSIX systems,
471 ;; if the game was installed system wide. Use
472 ;; "update-game-score" to add the score to the file in the
473 ;; shared game directory.
475 ;; 3. "update-game-score" is setuid, but the file FILE does *not*
476 ;; exist in the system wide shared game directory. Use
477 ;; `gamegrid-add-score-insecure' to create--if necessary--and
478 ;; update FILE. This is for the case that a user has installed
479 ;; a game on her own.
481 ;; 4. "update-game-score" is not setuid. Use it to create/update
482 ;; FILE in the user's home directory. There is presumably no
483 ;; shared game directory.
485 (defvar gamegrid-shared-game-dir)
487 (defun gamegrid-add-score-with-update-game-score (file score)
488 (let ((gamegrid-shared-game-dir
489 (not (zerop (logand (file-modes
490 (expand-file-name "update-game-score"
491 exec-directory))
492 #o4000)))))
493 (cond ((file-name-absolute-p file)
494 (gamegrid-add-score-insecure file score))
495 ((and gamegrid-shared-game-dir
496 (file-exists-p (expand-file-name file shared-game-score-directory)))
497 ;; Use the setuid "update-game-score" program to update a
498 ;; system-wide score file.
499 (gamegrid-add-score-with-update-game-score-1 file
500 (expand-file-name file shared-game-score-directory) score))
501 ;; Else: Add the score to a score file in the user's home
502 ;; directory.
503 (gamegrid-shared-game-dir
504 ;; If `gamegrid-shared-game-dir' is non-nil, then
505 ;; "update-gamescore" program is setuid, so don't use it.
506 (unless (file-exists-p
507 (directory-file-name gamegrid-user-score-file-directory))
508 (make-directory gamegrid-user-score-file-directory t))
509 (gamegrid-add-score-insecure file score
510 gamegrid-user-score-file-directory))
511 (t (let ((f (expand-file-name
512 gamegrid-user-score-file-directory)))
513 (when (file-writable-p f)
514 (unless (eq (car-safe (file-attributes f))
516 (make-directory f))
517 (setq f (expand-file-name file f))
518 (unless (file-exists-p f)
519 (write-region "" nil f nil 'silent nil 'excl)))
520 (gamegrid-add-score-with-update-game-score-1 file f score))))))
522 (defun gamegrid-add-score-with-update-game-score-1 (file target score)
523 (let ((default-directory "/")
524 (errbuf (generate-new-buffer " *update-game-score loss*"))
525 (marker-string (concat
526 (user-full-name)
527 " <"
528 (cond ((fboundp 'user-mail-address)
529 (user-mail-address))
530 ((boundp 'user-mail-address)
531 user-mail-address)
532 (t ""))
533 "> "
534 (current-time-string))))
535 ;; This can be called from a timer, so enable local quits.
536 (with-local-quit
537 (apply
538 'call-process
539 (append
540 (list
541 (expand-file-name "update-game-score" exec-directory)
542 nil errbuf nil
543 "-m" (int-to-string gamegrid-score-file-length)
544 "-d" (if gamegrid-shared-game-dir
545 (expand-file-name shared-game-score-directory)
546 (file-name-directory target))
547 file
548 (int-to-string score)
549 marker-string))))
550 (if (buffer-modified-p errbuf)
551 (progn
552 (display-buffer errbuf)
553 (error "Failed to update game score file"))
554 (kill-buffer errbuf))
555 (let ((buf (find-buffer-visiting target)))
556 (save-excursion
557 (if buf
558 (progn
559 (switch-to-buffer buf)
560 (revert-buffer nil t nil)
561 (display-buffer buf))
562 (find-file-read-only target))
563 (goto-char (point-min))
564 (search-forward (concat (int-to-string score)
565 " " (user-login-name) " "
566 marker-string))
567 (beginning-of-line)))))
569 (defun gamegrid-add-score-insecure (file score &optional directory)
570 (save-excursion
571 (setq file (expand-file-name file (or directory
572 temporary-file-directory)))
573 (find-file-other-window file)
574 (setq buffer-read-only nil)
575 (goto-char (point-max))
576 (insert (format "%05d\t%s\t%s <%s>\n"
577 score
578 (current-time-string)
579 (user-full-name)
580 (cond ((fboundp 'user-mail-address)
581 (user-mail-address))
582 ((boundp 'user-mail-address)
583 user-mail-address)
584 (t ""))))
585 (sort-fields 1 (point-min) (point-max))
586 (reverse-region (point-min) (point-max))
587 (goto-char (point-min))
588 (forward-line gamegrid-score-file-length)
589 (delete-region (point) (point-max))
590 (setq buffer-read-only t)
591 (save-buffer)))
594 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
596 (provide 'gamegrid)
598 ;;; gamegrid.el ends here