Revert to the previous code.
[emacs.git] / lisp / play / zone.el
blob367c301de445b997d3a5f368676eb1a6f57f4a48
1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Victor Zandy <zandy@cs.wisc.edu>
7 ;; Maintainer: Thien-Thi Nguyen <ttn@gnu.org>
8 ;; Keywords: games
9 ;; Created: June 6, 1998
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Don't zone out in front of Emacs! Try M-x zone.
29 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
31 ;; Bored by the zone pyrotechnics? Write your own! Add it to
32 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
34 ;; WARNING: Not appropriate for Emacs sessions over modems or
35 ;; computers as slow as mine.
37 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
38 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
39 ;; Luigi Panzeri, John Paul Wallington.
41 ;;; Code:
43 (require 'timer)
44 (require 'tabify)
45 (eval-when-compile (require 'cl))
47 (defvar zone-timer nil
48 "The timer we use to decide when to zone out, or nil if none.")
50 (defvar zone-timeout nil
51 "*Seconds to timeout the zoning.
52 If nil, don't interrupt for about 1^26 seconds.")
54 ;; Vector of functions that zone out. `zone' will execute one of
55 ;; these functions, randomly chosen. The chosen function is invoked
56 ;; in the *zone* buffer, which contains the text of the selected
57 ;; window. If the function loops, it *must* periodically check and
58 ;; halt if `input-pending-p' is t (because quitting is disabled when
59 ;; Emacs idle timers are run).
60 (defvar zone-programs [
61 zone-pgm-jitter
62 zone-pgm-putz-with-case
63 zone-pgm-dissolve
64 ;; zone-pgm-explode
65 zone-pgm-whack-chars
66 zone-pgm-rotate
67 zone-pgm-rotate-LR-lockstep
68 zone-pgm-rotate-RL-lockstep
69 zone-pgm-rotate-LR-variable
70 zone-pgm-rotate-RL-variable
71 zone-pgm-drip
72 zone-pgm-drip-fretfully
73 zone-pgm-five-oclock-swan-dive
74 zone-pgm-martini-swan-dive
75 zone-pgm-rat-race
76 zone-pgm-paragraph-spaz
77 zone-pgm-stress
78 zone-pgm-stress-destress
79 zone-pgm-random-life
82 (defmacro zone-orig (&rest body)
83 `(with-current-buffer (get 'zone 'orig-buffer)
84 ,@body))
86 (defmacro zone-hiding-modeline (&rest body)
87 ;; This formerly worked by temporarily altering face `mode-line',
88 ;; which did not even work right, it seems.
89 `(let (mode-line-format)
90 ,@body))
92 (defun zone-call (program &optional timeout)
93 "Call PROGRAM in a zoned way.
94 If PROGRAM is a function, call it, interrupting after the amount
95 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
96 if unspecified, q.v.
97 PROGRAM can also be a list of elements, which are interpreted like so:
98 If the element is a function or a list of a function and a number,
99 apply `zone-call' recursively."
100 (cond ((functionp program)
101 (with-timeout ((or timeout zone-timeout (ash 1 26)))
102 (funcall program)))
103 ((listp program)
104 (mapcar (lambda (elem)
105 (cond ((functionp elem) (zone-call elem))
106 ((and (listp elem)
107 (functionp (car elem))
108 (numberp (cadr elem)))
109 (apply 'zone-call elem))
110 (t (error "bad `zone-call' elem: %S" elem))))
111 program))))
113 ;;;###autoload
114 (defun zone ()
115 "Zone out, completely."
116 (interactive)
117 (save-window-excursion
118 (let ((f (selected-frame))
119 (outbuf (get-buffer-create "*zone*"))
120 (text (buffer-substring (window-start) (window-end)))
121 (wp (1+ (- (window-point (selected-window))
122 (window-start)))))
123 (put 'zone 'orig-buffer (current-buffer))
124 (put 'zone 'modeline-hidden-level 0)
125 (switch-to-buffer outbuf)
126 (setq mode-name "Zone")
127 (erase-buffer)
128 (setq buffer-undo-list t
129 truncate-lines t
130 tab-width (zone-orig tab-width)
131 line-spacing (zone-orig line-spacing))
132 (insert text)
133 (untabify (point-min) (point-max))
134 (set-window-start (selected-window) (point-min))
135 (set-window-point (selected-window) wp)
136 (sit-for 0 500)
137 (let ((pgm (elt zone-programs (random (length zone-programs))))
138 (ct (and f (frame-parameter f 'cursor-type)))
139 (show-trailing-whitespace nil)
140 (restore (list '(kill-buffer outbuf))))
141 (when ct
142 (modify-frame-parameters f '((cursor-type . (bar . 0))))
143 (setq restore (cons '(modify-frame-parameters
144 f (list (cons 'cursor-type ct)))
145 restore)))
146 ;; Make `restore' a self-disabling one-shot thunk.
147 (setq restore `(lambda () ,@restore (setq restore nil)))
148 (condition-case nil
149 (progn
150 (message "Zoning... (%s)" pgm)
151 (garbage-collect)
152 ;; If some input is pending, zone says "sorry", which
153 ;; isn't nice; this might happen e.g. when they invoke the
154 ;; game by clicking the menu bar. So discard any pending
155 ;; input before zoning out.
156 (if (input-pending-p)
157 (discard-input))
158 (zone-call pgm)
159 (message "Zoning...sorry"))
160 (error
161 (funcall restore)
162 (while (not (input-pending-p))
163 (message "We were zoning when we wrote %s..." pgm)
164 (sit-for 3)
165 (message "...here's hoping we didn't hose your buffer!")
166 (sit-for 3)))
167 (quit
168 (funcall restore)
169 (ding)
170 (message "Zoning...sorry")))
171 (when restore (funcall restore))))))
173 ;;;; Zone when idle, or not.
175 (defun zone-when-idle (secs)
176 "Zone out when Emacs has been idle for SECS seconds."
177 (interactive "nHow long before I start zoning (seconds): ")
178 (if (timerp zone-timer)
179 (cancel-timer zone-timer))
180 (setq zone-timer nil)
181 (or (<= secs 0)
182 (setq zone-timer (run-with-idle-timer secs t 'zone))))
184 (defun zone-leave-me-alone ()
185 "Don't zone out when Emacs is idle."
186 (interactive)
187 (if (timerp zone-timer)
188 (cancel-timer zone-timer))
189 (setq zone-timer nil)
190 (message "I won't zone out any more"))
193 ;;;; jittering
195 (defun zone-shift-up ()
196 (let* ((b (point))
197 (e (progn (forward-line 1) (point)))
198 (s (buffer-substring b e)))
199 (delete-region b e)
200 (goto-char (point-max))
201 (insert s)))
203 (defun zone-shift-down ()
204 (goto-char (point-max))
205 (let* ((b (point))
206 (e (progn (forward-line -1) (point)))
207 (s (buffer-substring b e)))
208 (delete-region b e)
209 (goto-char (point-min))
210 (insert s)))
212 (defun zone-shift-left ()
213 (let (s)
214 (while (not (eobp))
215 (unless (eolp)
216 (setq s (buffer-substring (point) (1+ (point))))
217 (delete-char 1)
218 (end-of-line)
219 (insert s))
220 (forward-char 1))))
222 (defun zone-shift-right ()
223 (goto-char (point-max))
224 (end-of-line)
225 (let (s)
226 (while (not (bobp))
227 (unless (bolp)
228 (setq s (buffer-substring (1- (point)) (point)))
229 (delete-char -1)
230 (beginning-of-line)
231 (insert s))
232 (end-of-line 0))))
234 (defun zone-pgm-jitter ()
235 (let ((ops [
236 zone-shift-left
237 zone-shift-right
238 zone-shift-down
239 zone-shift-up
241 (goto-char (point-min))
242 (while (not (input-pending-p))
243 (funcall (elt ops (random (length ops))))
244 (goto-char (point-min))
245 (sit-for 0 10))))
248 ;;;; whacking chars
250 (defun zone-pgm-whack-chars ()
251 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
252 (while (not (input-pending-p))
253 (let ((i 48))
254 (while (< i 122)
255 (aset tbl i (+ 48 (random (- 123 48))))
256 (setq i (1+ i)))
257 (translate-region (point-min) (point-max) tbl)
258 (sit-for 0 2)))))
260 (put 'zone-pgm-whack-chars 'wc-tbl
261 (let ((tbl (make-string 128 ?x))
262 (i 0))
263 (while (< i 128)
264 (aset tbl i i)
265 (setq i (1+ i)))
266 tbl))
268 ;;;; dissolving
270 (defun zone-remove-text ()
271 (let ((working t))
272 (while working
273 (setq working nil)
274 (save-excursion
275 (goto-char (point-min))
276 (while (not (eobp))
277 (if (looking-at "[^(){}\n\t ]")
278 (let ((n (random 5)))
279 (if (not (= n 0))
280 (progn
281 (setq working t)
282 (forward-char 1))
283 (delete-char 1)
284 (insert " ")))
285 (forward-char 1))))
286 (sit-for 0 2))))
288 (defun zone-pgm-dissolve ()
289 (zone-remove-text)
290 (zone-pgm-jitter))
293 ;;;; exploding
295 (defun zone-exploding-remove ()
296 (let ((i 0))
297 (while (< i 5)
298 (save-excursion
299 (goto-char (point-min))
300 (while (not (eobp))
301 (if (looking-at "[^*\n\t ]")
302 (let ((n (random 5)))
303 (if (not (= n 0))
304 (forward-char 1))
305 (insert " ")))
306 (forward-char 1)))
307 (setq i (1+ i))
308 (sit-for 0 2)))
309 (zone-pgm-jitter))
311 (defun zone-pgm-explode ()
312 (zone-exploding-remove)
313 (zone-pgm-jitter))
316 ;;;; putzing w/ case
318 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
319 ;; instances of the same letter have the same case, which produces a
320 ;; less interesting effect than you might imagine.
321 (defun zone-pgm-2nd-putz-with-case ()
322 (let ((tbl (make-string 128 ?x))
323 (i 0))
324 (while (< i 128)
325 (aset tbl i i)
326 (setq i (1+ i)))
327 (while (not (input-pending-p))
328 (setq i ?a)
329 (while (<= i ?z)
330 (aset tbl i
331 (if (zerop (random 5))
332 (upcase i)
333 (downcase i)))
334 (setq i (+ i (1+ (random 5)))))
335 (setq i ?A)
336 (while (<= i ?z)
337 (aset tbl i
338 (if (zerop (random 5))
339 (downcase i)
340 (upcase i)))
341 (setq i (+ i (1+ (random 5)))))
342 (translate-region (point-min) (point-max) tbl)
343 (sit-for 0 2))))
345 (defun zone-pgm-putz-with-case ()
346 (goto-char (point-min))
347 (while (not (input-pending-p))
348 (let ((np (+ 2 (random 5)))
349 (pm (point-max)))
350 (while (< np pm)
351 (goto-char np)
352 (let ((prec (preceding-char))
353 (props (text-properties-at (1- (point)))))
354 (insert (if (zerop (random 2))
355 (upcase prec)
356 (downcase prec)))
357 (set-text-properties (1- (point)) (point) props))
358 (backward-char 2)
359 (delete-char 1)
360 (setq np (+ np (1+ (random 5))))))
361 (goto-char (point-min))
362 (sit-for 0 2)))
365 ;;;; rotating
367 (defun zone-line-specs ()
368 (let (ret)
369 (save-excursion
370 (goto-char (window-start))
371 (while (< (point) (window-end))
372 (when (looking-at "[\t ]*\\([^\n]+\\)")
373 (setq ret (cons (cons (match-beginning 1) (match-end 1)) ret)))
374 (forward-line 1)))
375 ret))
377 (defun zone-pgm-rotate (&optional random-style)
378 (let* ((specs (apply
379 'vector
380 (let (res)
381 (mapc (lambda (ent)
382 (let* ((beg (car ent))
383 (end (cdr ent))
384 (amt (if random-style
385 (funcall random-style)
386 (- (random 7) 3))))
387 (when (< (- end (abs amt)) beg)
388 (setq amt (random (- end beg))))
389 (unless (= 0 amt)
390 (setq res
391 (cons
392 (vector amt beg (- end (abs amt)))
393 res)))))
394 (zone-line-specs))
395 res)))
396 (n (length specs))
397 amt aamt cut paste txt i ent)
398 (while (not (input-pending-p))
399 (setq i 0)
400 (while (< i n)
401 (setq ent (aref specs i))
402 (setq amt (aref ent 0) aamt (abs amt))
403 (if (> 0 amt)
404 (setq cut 1 paste 2)
405 (setq cut 2 paste 1))
406 (goto-char (aref ent cut))
407 (setq txt (buffer-substring (point) (+ (point) aamt)))
408 (delete-char aamt)
409 (goto-char (aref ent paste))
410 (insert txt)
411 (setq i (1+ i)))
412 (sit-for 0.04))))
414 (defun zone-pgm-rotate-LR-lockstep ()
415 (zone-pgm-rotate (lambda () 1)))
417 (defun zone-pgm-rotate-RL-lockstep ()
418 (zone-pgm-rotate (lambda () -1)))
420 (defun zone-pgm-rotate-LR-variable ()
421 (zone-pgm-rotate (lambda () (1+ (random 3)))))
423 (defun zone-pgm-rotate-RL-variable ()
424 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
427 ;;;; dripping
429 (defsubst zone-cpos (pos)
430 (buffer-substring pos (1+ pos)))
432 (defsubst zone-replace-char (count del-count char-as-string new-value)
433 (delete-char (or del-count (- count)))
434 (aset char-as-string 0 new-value)
435 (dotimes (i count) (insert char-as-string)))
437 (defsubst zone-park/sit-for (pos seconds)
438 (let ((p (point)))
439 (goto-char pos)
440 (prog1 (sit-for seconds)
441 (goto-char p))))
443 (defun zone-fret (wbeg pos)
444 (let* ((case-fold-search nil)
445 (c-string (zone-cpos pos))
446 (cw-ceil (ceiling (char-width (aref c-string 0))))
447 (hmm (cond
448 ((string-match "[a-z]" c-string) (upcase c-string))
449 ((string-match "[A-Z]" c-string) (downcase c-string))
450 (t (propertize " " 'display `(space :width ,cw-ceil))))))
451 (do ((i 0 (1+ i))
452 (wait 0.5 (* wait 0.8)))
453 ((= i 20))
454 (goto-char pos)
455 (delete-char 1)
456 (insert (if (= 0 (% i 2)) hmm c-string))
457 (zone-park/sit-for wbeg wait))
458 (delete-char -1) (insert c-string)))
460 (defun zone-fill-out-screen (width height)
461 (let ((start (window-start))
462 (line (make-string width 32)))
463 (goto-char start)
464 ;; fill out rectangular ws block
465 (while (progn (end-of-line)
466 (let ((cc (current-column)))
467 (if (< cc width)
468 (insert (substring line cc))
469 (delete-char (- width cc)))
470 (cond ((eobp) (insert "\n") nil)
471 (t (forward-char 1) t)))))
472 ;; pad ws past bottom of screen
473 (let ((nl (- height (count-lines (point-min) (point)))))
474 (when (> nl 0)
475 (setq line (concat line "\n"))
476 (do ((i 0 (1+ i)))
477 ((= i nl))
478 (insert line))))
479 (goto-char start)
480 (recenter 0)
481 (sit-for 0)))
483 (defun zone-fall-through-ws (c wbeg wend)
484 (let* ((cw-ceil (ceiling (char-width (aref c 0))))
485 (spaces (make-string cw-ceil 32))
486 (col (current-column))
487 (wait 0.15)
488 newpos fall-p)
489 (while (when (save-excursion
490 (forward-line 1)
491 (and (= col (current-column))
492 (setq newpos (point))
493 (string= spaces (buffer-substring-no-properties
494 newpos (+ newpos cw-ceil)))
495 (setq newpos (+ newpos (1- cw-ceil)))))
496 (setq fall-p t)
497 (delete-char 1)
498 (insert spaces)
499 (goto-char newpos)
500 (when (< (point) wend)
501 (delete-char cw-ceil)
502 (insert c)
503 (forward-char -1)
504 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
505 fall-p))
507 (defun zone-pgm-drip (&optional fret-p pancake-p)
508 (let* ((ww (1- (window-width)))
509 (wh (window-height))
510 (mc 0) ; miss count
511 (total (* ww wh))
512 (fall-p nil)
513 wbeg wend c)
514 (zone-fill-out-screen ww wh)
515 (setq wbeg (window-start)
516 wend (window-end))
517 (catch 'done
518 (while (not (input-pending-p))
519 (setq mc 0 wend (window-end))
520 ;; select non-ws character, but don't miss too much
521 (goto-char (+ wbeg (random (- wend wbeg))))
522 (while (looking-at "[ \n\f]")
523 (if (= total (setq mc (1+ mc)))
524 (throw 'done 'sel)
525 (goto-char (+ wbeg (random (- wend wbeg))))))
526 ;; character animation sequence
527 (let ((p (point)))
528 (when fret-p (zone-fret wbeg p))
529 (goto-char p)
530 (setq c (zone-cpos p)
531 fall-p (zone-fall-through-ws c wbeg wend)))
532 ;; assuming current-column has not changed...
533 (when (and pancake-p
534 fall-p
535 (< (count-lines (point-min) (point))
536 wh))
537 (let ((cw (ceiling (char-width (aref c 0)))))
538 (zone-replace-char cw 1 c ?@) (zone-park/sit-for wbeg 0.137)
539 (zone-replace-char cw nil c ?*) (zone-park/sit-for wbeg 0.137)
540 (zone-replace-char cw nil c ?_)))))))
542 (defun zone-pgm-drip-fretfully ()
543 (zone-pgm-drip t))
545 (defun zone-pgm-five-oclock-swan-dive ()
546 (zone-pgm-drip nil t))
548 (defun zone-pgm-martini-swan-dive ()
549 (zone-pgm-drip t t))
551 (defun zone-pgm-rat-race ()
552 (while (not (input-pending-p))
553 (zone-call '((zone-pgm-rotate 10)
554 (zone-pgm-drip-fretfully 15)
555 (zone-pgm-drip 10)
556 ((lambda ()
557 (goto-char (point-min))
558 (while (re-search-forward " +$" nil t)
559 (delete-region (match-beginning 0) (match-end 0))))
560 5)))))
563 ;;;; paragraph spazzing (for textish modes)
565 (defun zone-pgm-paragraph-spaz ()
566 (if (memq (zone-orig major-mode)
567 ;; there should be a better way to distinguish textish modes
568 '(text-mode texinfo-mode fundamental-mode))
569 (let ((fill-column fill-column)
570 (fc-min fill-column)
571 (fc-max fill-column)
572 (max-fc (1- (frame-width))))
573 (while (sit-for 0.1)
574 (fill-paragraph 1)
575 (setq fill-column (+ fill-column (- (random 5) 2)))
576 (when (< fill-column fc-min)
577 (setq fc-min fill-column))
578 (when (> fill-column max-fc)
579 (setq fill-column max-fc))
580 (when (> fill-column fc-max)
581 (setq fc-max fill-column))))
582 (message "Zoning... (zone-pgm-rotate)")
583 (zone-pgm-rotate)))
586 ;;;; stressing and destressing
588 (defun zone-pgm-stress ()
589 (goto-char (point-min))
590 (let (lines)
591 (while (< (point) (point-max))
592 (let ((p (point)))
593 (forward-line 1)
594 (setq lines (cons (buffer-substring p (point)) lines))))
595 (sit-for 5)
596 (zone-hiding-modeline
597 (let ((msg "Zoning... (zone-pgm-stress)"))
598 (while (not (string= msg ""))
599 (message (setq msg (substring msg 1)))
600 (sit-for 0.05)))
601 (while (not (input-pending-p))
602 (when (< 50 (random 100))
603 (goto-char (point-max))
604 (forward-line -1)
605 (let ((kill-whole-line t))
606 (kill-line))
607 (goto-char (point-min))
608 (insert (nth (random (length lines)) lines)))
609 (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
610 (sit-for 0.1)))))
612 (defun zone-pgm-stress-destress ()
613 (zone-call 'zone-pgm-stress 25)
614 (zone-hiding-modeline
615 (sit-for 3)
616 (erase-buffer)
617 (sit-for 3)
618 (insert-buffer-substring "*Messages*")
619 (message "")
620 (goto-char (point-max))
621 (recenter -1)
622 (sit-for 3)
623 (delete-region (point-min) (window-start))
624 (message "hey why stress out anyway?")
625 (zone-call '((zone-pgm-rotate 30)
626 (zone-pgm-whack-chars 10)
627 zone-pgm-drip))))
630 ;;;; the lyfe so short the craft so long to lerne --chaucer
632 (defvar zone-pgm-random-life-wait nil
633 "*Seconds to wait between successive `life' generations.
634 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
636 (defun zone-pgm-random-life ()
637 (require 'life)
638 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
639 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
640 (bot (progn (goto-char (window-end)) (forward-line -7) (point)))
641 (rtc (- (frame-width) 11))
642 (min (window-start))
643 (max (1- (window-end)))
644 s c col)
645 (delete-region max (point-max))
646 (while (and (progn (goto-char min) (sit-for 0.05))
647 (progn (goto-char (+ min (random max)))
648 (or (progn (skip-chars-forward " @\n" max)
649 (not (= max (point))))
650 (unless (or (= 0 (skip-chars-backward " @\n" min))
651 (= min (point)))
652 (forward-char -1)
653 t))))
654 (unless (or (eolp) (eobp))
655 (setq s (zone-cpos (point))
656 c (aref s 0))
657 (zone-replace-char
658 (char-width c)
659 1 s (cond ((or (> top (point))
660 (< bot (point))
661 (or (> 11 (setq col (current-column)))
662 (< rtc col)))
664 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
665 ((and (<= ?A c) (>= ?Z c)) ?*)
666 (t ?@)))))
667 (sit-for 3)
668 (setq col nil)
669 (goto-char bot)
670 (while (< top (point))
671 (setq c (point))
672 (move-to-column 9)
673 (setq col (cons (buffer-substring (point) c) col))
674 (end-of-line 0)
675 (forward-char -10))
676 (let ((life-patterns (vector
677 (if (and col (search-forward "@" max t))
678 (cons (make-string (length (car col)) 32) col)
679 (list (mapconcat 'identity
680 (make-list (/ (- rtc 11) 15)
681 (make-string 5 ?@))
682 (make-string 10 32)))))))
683 (life (or zone-pgm-random-life-wait (random 4)))
684 (kill-buffer nil))))
687 (random t)
689 ;;;;;;;;;;;;;;;
690 (provide 'zone)
692 ;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
693 ;;; zone.el ends here