* vc/diff.el (diff-sentinel): Doc fix (Bug#7682).
[emacs.git] / lisp / play / zone.el
bloba1782377af56e614151cdb576a936b919fdb5faa
1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 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 (defvar zone-timer nil
44 "The timer we use to decide when to zone out, or nil if none.")
46 (defvar zone-timeout nil
47 "*Seconds to timeout the zoning.
48 If nil, don't interrupt for about 1^26 seconds.")
50 ;; Vector of functions that zone out. `zone' will execute one of
51 ;; these functions, randomly chosen. The chosen function is invoked
52 ;; in the *zone* buffer, which contains the text of the selected
53 ;; window. If the function loops, it *must* periodically check and
54 ;; halt if `input-pending-p' is t (because quitting is disabled when
55 ;; Emacs idle timers are run).
56 (defvar zone-programs [
57 zone-pgm-jitter
58 zone-pgm-putz-with-case
59 zone-pgm-dissolve
60 ;; zone-pgm-explode
61 zone-pgm-whack-chars
62 zone-pgm-rotate
63 zone-pgm-rotate-LR-lockstep
64 zone-pgm-rotate-RL-lockstep
65 zone-pgm-rotate-LR-variable
66 zone-pgm-rotate-RL-variable
67 zone-pgm-drip
68 zone-pgm-drip-fretfully
69 zone-pgm-five-oclock-swan-dive
70 zone-pgm-martini-swan-dive
71 zone-pgm-rat-race
72 zone-pgm-paragraph-spaz
73 zone-pgm-stress
74 zone-pgm-stress-destress
75 zone-pgm-random-life
78 (defmacro zone-orig (&rest body)
79 `(with-current-buffer (get 'zone 'orig-buffer)
80 ,@body))
82 (defmacro zone-hiding-modeline (&rest body)
83 ;; This formerly worked by temporarily altering face `mode-line',
84 ;; which did not even work right, it seems.
85 `(let (mode-line-format)
86 ,@body))
88 (defun zone-call (program &optional timeout)
89 "Call PROGRAM in a zoned way.
90 If PROGRAM is a function, call it, interrupting after the amount
91 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
92 if unspecified, q.v.
93 PROGRAM can also be a list of elements, which are interpreted like so:
94 If the element is a function or a list of a function and a number,
95 apply `zone-call' recursively."
96 (cond ((functionp program)
97 (with-timeout ((or timeout zone-timeout (ash 1 26)))
98 (funcall program)))
99 ((listp program)
100 (mapcar (lambda (elem)
101 (cond ((functionp elem) (zone-call elem))
102 ((and (listp elem)
103 (functionp (car elem))
104 (numberp (cadr elem)))
105 (apply 'zone-call elem))
106 (t (error "bad `zone-call' elem: %S" elem))))
107 program))))
109 ;;;###autoload
110 (defun zone ()
111 "Zone out, completely."
112 (interactive)
113 (save-window-excursion
114 (let ((f (selected-frame))
115 (outbuf (get-buffer-create "*zone*"))
116 (text (buffer-substring (window-start) (window-end)))
117 (wp (1+ (- (window-point (selected-window))
118 (window-start)))))
119 (put 'zone 'orig-buffer (current-buffer))
120 (put 'zone 'modeline-hidden-level 0)
121 (switch-to-buffer outbuf)
122 (setq mode-name "Zone")
123 (erase-buffer)
124 (setq buffer-undo-list t
125 truncate-lines t
126 tab-width (zone-orig tab-width)
127 line-spacing (zone-orig line-spacing))
128 (insert text)
129 (untabify (point-min) (point-max))
130 (set-window-start (selected-window) (point-min))
131 (set-window-point (selected-window) wp)
132 (sit-for 0 500)
133 (let ((pgm (elt zone-programs (random (length zone-programs))))
134 (ct (and f (frame-parameter f 'cursor-type)))
135 (show-trailing-whitespace nil)
136 (restore (list '(kill-buffer outbuf))))
137 (when ct
138 (modify-frame-parameters f '((cursor-type . (bar . 0))))
139 (setq restore (cons '(modify-frame-parameters
140 f (list (cons 'cursor-type ct)))
141 restore)))
142 ;; Make `restore' a self-disabling one-shot thunk.
143 (setq restore `(lambda () ,@restore (setq restore nil)))
144 (condition-case nil
145 (progn
146 (message "Zoning... (%s)" pgm)
147 (garbage-collect)
148 ;; If some input is pending, zone says "sorry", which
149 ;; isn't nice; this might happen e.g. when they invoke the
150 ;; game by clicking the menu bar. So discard any pending
151 ;; input before zoning out.
152 (if (input-pending-p)
153 (discard-input))
154 (zone-call pgm)
155 (message "Zoning...sorry"))
156 (error
157 (funcall restore)
158 (while (not (input-pending-p))
159 (message "We were zoning when we wrote %s..." pgm)
160 (sit-for 3)
161 (message "...here's hoping we didn't hose your buffer!")
162 (sit-for 3)))
163 (quit
164 (funcall restore)
165 (ding)
166 (message "Zoning...sorry")))
167 (when restore (funcall restore))))))
169 ;;;; Zone when idle, or not.
171 (defun zone-when-idle (secs)
172 "Zone out when Emacs has been idle for SECS seconds."
173 (interactive "nHow long before I start zoning (seconds): ")
174 (if (timerp zone-timer)
175 (cancel-timer zone-timer))
176 (setq zone-timer nil)
177 (or (<= secs 0)
178 (setq zone-timer (run-with-idle-timer secs t 'zone))))
180 (defun zone-leave-me-alone ()
181 "Don't zone out when Emacs is idle."
182 (interactive)
183 (if (timerp zone-timer)
184 (cancel-timer zone-timer))
185 (setq zone-timer nil)
186 (message "I won't zone out any more"))
189 ;;;; jittering
191 (defun zone-shift-up ()
192 (let* ((b (point))
193 (e (progn (forward-line 1) (point)))
194 (s (buffer-substring b e)))
195 (delete-region b e)
196 (goto-char (point-max))
197 (insert s)))
199 (defun zone-shift-down ()
200 (goto-char (point-max))
201 (let* ((b (point))
202 (e (progn (forward-line -1) (point)))
203 (s (buffer-substring b e)))
204 (delete-region b e)
205 (goto-char (point-min))
206 (insert s)))
208 (defun zone-shift-left ()
209 (let ((inhibit-point-motion-hooks t)
211 (while (not (eobp))
212 (unless (eolp)
213 (setq s (buffer-substring (point) (1+ (point))))
214 (delete-char 1)
215 (end-of-line)
216 (insert s))
217 (ignore-errors (forward-char 1)))))
219 (defun zone-shift-right ()
220 (goto-char (point-max))
221 (let ((inhibit-point-motion-hooks t)
223 (while (not (bobp))
224 (unless (bolp)
225 (setq s (buffer-substring (1- (point)) (point)))
226 (delete-char -1)
227 (beginning-of-line)
228 (insert s))
229 (end-of-line 0))))
231 (defun zone-pgm-jitter ()
232 (let ((ops [
233 zone-shift-left
234 zone-shift-right
235 zone-shift-down
236 zone-shift-up
238 (goto-char (point-min))
239 (while (not (input-pending-p))
240 (funcall (elt ops (random (length ops))))
241 (goto-char (point-min))
242 (sit-for 0 10))))
245 ;;;; whacking chars
247 (defun zone-pgm-whack-chars ()
248 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
249 (while (not (input-pending-p))
250 (let ((i 48))
251 (while (< i 122)
252 (aset tbl i (+ 48 (random (- 123 48))))
253 (setq i (1+ i)))
254 (translate-region (point-min) (point-max) tbl)
255 (sit-for 0 2)))))
257 (put 'zone-pgm-whack-chars 'wc-tbl
258 (let ((tbl (make-string 128 ?x))
259 (i 0))
260 (while (< i 128)
261 (aset tbl i i)
262 (setq i (1+ i)))
263 tbl))
265 ;;;; dissolving
267 (defun zone-remove-text ()
268 (let ((working t))
269 (while working
270 (setq working nil)
271 (save-excursion
272 (goto-char (point-min))
273 (while (not (eobp))
274 (if (looking-at "[^(){}\n\t ]")
275 (let ((n (random 5)))
276 (if (not (= n 0))
277 (progn
278 (setq working t)
279 (forward-char 1))
280 (delete-char 1)
281 (insert " ")))
282 (forward-char 1))))
283 (sit-for 0 2))))
285 (defun zone-pgm-dissolve ()
286 (zone-remove-text)
287 (zone-pgm-jitter))
290 ;;;; exploding
292 (defun zone-exploding-remove ()
293 (let ((i 0))
294 (while (< i 5)
295 (save-excursion
296 (goto-char (point-min))
297 (while (not (eobp))
298 (if (looking-at "[^*\n\t ]")
299 (let ((n (random 5)))
300 (if (not (= n 0))
301 (forward-char 1))
302 (insert " ")))
303 (forward-char 1)))
304 (setq i (1+ i))
305 (sit-for 0 2)))
306 (zone-pgm-jitter))
308 (defun zone-pgm-explode ()
309 (zone-exploding-remove)
310 (zone-pgm-jitter))
313 ;;;; putzing w/ case
315 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
316 ;; instances of the same letter have the same case, which produces a
317 ;; less interesting effect than you might imagine.
318 (defun zone-pgm-2nd-putz-with-case ()
319 (let ((tbl (make-string 128 ?x))
320 (i 0))
321 (while (< i 128)
322 (aset tbl i i)
323 (setq i (1+ i)))
324 (while (not (input-pending-p))
325 (setq i ?a)
326 (while (<= i ?z)
327 (aset tbl i
328 (if (zerop (random 5))
329 (upcase i)
330 (downcase i)))
331 (setq i (+ i (1+ (random 5)))))
332 (setq i ?A)
333 (while (<= i ?z)
334 (aset tbl i
335 (if (zerop (random 5))
336 (downcase i)
337 (upcase i)))
338 (setq i (+ i (1+ (random 5)))))
339 (translate-region (point-min) (point-max) tbl)
340 (sit-for 0 2))))
342 (defun zone-pgm-putz-with-case ()
343 (goto-char (point-min))
344 (while (not (input-pending-p))
345 (let ((np (+ 2 (random 5)))
346 (pm (point-max)))
347 (while (< np pm)
348 (funcall (if (zerop (random 2)) 'upcase-region
349 'downcase-region) (1- np) np)
350 (setq np (+ np (1+ (random 5))))))
351 (goto-char (point-min))
352 (sit-for 0 2)))
355 ;;;; rotating
357 (defun zone-line-specs ()
358 (let ((ok t)
359 ret)
360 (save-excursion
361 (goto-char (window-start))
362 (while (and ok (< (point) (window-end)))
363 (when (looking-at "[\t ]*\\([^\n]+\\)")
364 (setq ret (cons (cons (match-beginning 1) (match-end 1)) ret)))
365 (setq ok (zerop (forward-line 1)))))
366 ret))
368 (defun zone-pgm-rotate (&optional random-style)
369 (let* ((specs (apply
370 'vector
371 (let (res)
372 (mapc (lambda (ent)
373 (let* ((beg (car ent))
374 (end (cdr ent))
375 (amt (if random-style
376 (funcall random-style)
377 (- (random 7) 3))))
378 (when (< (- end (abs amt)) beg)
379 (setq amt (random (- end beg))))
380 (unless (= 0 amt)
381 (setq res
382 (cons
383 (vector amt beg (- end (abs amt)))
384 res)))))
385 (zone-line-specs))
386 res)))
387 (n (length specs))
388 amt aamt cut paste txt i ent)
389 (while (not (input-pending-p))
390 (setq i 0)
391 (while (< i n)
392 (setq ent (aref specs i))
393 (setq amt (aref ent 0) aamt (abs amt))
394 (if (> 0 amt)
395 (setq cut 1 paste 2)
396 (setq cut 2 paste 1))
397 (goto-char (aref ent cut))
398 (setq aamt (min aamt (- (point-max) (point))))
399 (setq txt (buffer-substring (point) (+ (point) aamt)))
400 (delete-char aamt)
401 (goto-char (aref ent paste))
402 (insert txt)
403 (setq i (1+ i)))
404 (sit-for 0.04))))
406 (defun zone-pgm-rotate-LR-lockstep ()
407 (zone-pgm-rotate (lambda () 1)))
409 (defun zone-pgm-rotate-RL-lockstep ()
410 (zone-pgm-rotate (lambda () -1)))
412 (defun zone-pgm-rotate-LR-variable ()
413 (zone-pgm-rotate (lambda () (1+ (random 3)))))
415 (defun zone-pgm-rotate-RL-variable ()
416 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
419 ;;;; dripping
421 (defsubst zone-cpos (pos)
422 (buffer-substring pos (1+ pos)))
424 (defsubst zone-replace-char (count del-count char-as-string new-value)
425 (delete-char (or del-count (- count)))
426 (aset char-as-string 0 new-value)
427 (dotimes (i count) (insert char-as-string)))
429 (defsubst zone-park/sit-for (pos seconds)
430 (let ((p (point)))
431 (goto-char pos)
432 (prog1 (sit-for seconds)
433 (goto-char p))))
435 (defun zone-fret (wbeg pos)
436 (let* ((case-fold-search nil)
437 (c-string (zone-cpos pos))
438 (cw-ceil (ceiling (char-width (aref c-string 0))))
439 (hmm (cond
440 ((string-match "[a-z]" c-string) (upcase c-string))
441 ((string-match "[A-Z]" c-string) (downcase c-string))
442 (t (propertize " " 'display `(space :width ,cw-ceil)))))
443 (wait 0.5))
444 (dotimes (i 20)
445 (goto-char pos)
446 (delete-char 1)
447 (insert (if (= 0 (% i 2)) hmm c-string))
448 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))
449 (delete-char -1) (insert c-string)))
451 (defun zone-fill-out-screen (width height)
452 (let ((start (window-start))
453 (line (make-string width 32))
454 (inhibit-point-motion-hooks t))
455 (goto-char start)
456 ;; fill out rectangular ws block
457 (while (progn (end-of-line)
458 (let ((cc (current-column)))
459 (if (< cc width)
460 (insert (substring line cc))
461 (delete-char (- width cc)))
462 (cond ((eobp) (insert "\n") nil)
463 (t (forward-char 1) t)))))
464 ;; pad ws past bottom of screen
465 (let ((nl (- height (count-lines (point-min) (point)))))
466 (when (> nl 0)
467 (setq line (concat line "\n"))
468 (dotimes (i nl)
469 (insert line))))
470 (goto-char start)
471 (recenter 0)
472 (sit-for 0)))
474 (defun zone-fall-through-ws (c wbeg wend)
475 (let* ((cw-ceil (ceiling (char-width (aref c 0))))
476 (spaces (make-string cw-ceil 32))
477 (col (current-column))
478 (wait 0.15)
479 newpos fall-p)
480 (while (when (save-excursion
481 (and (zerop (forward-line 1))
482 (progn
483 (forward-char col)
484 (= col (current-column)))
485 (setq newpos (point))
486 (string= spaces (buffer-substring-no-properties
487 newpos (+ newpos cw-ceil)))
488 (setq newpos (+ newpos (1- cw-ceil)))))
489 (setq fall-p t)
490 (delete-char 1)
491 (insert spaces)
492 (goto-char newpos)
493 (when (< (point) wend)
494 (delete-char cw-ceil)
495 (insert c)
496 (forward-char -1)
497 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
498 fall-p))
500 (defun zone-pgm-drip (&optional fret-p pancake-p)
501 (let* ((ww (1- (window-width)))
502 (wh (window-height))
503 (mc 0) ; miss count
504 (total (* ww wh))
505 (fall-p nil)
506 wbeg wend c)
507 (zone-fill-out-screen ww wh)
508 (setq wbeg (window-start)
509 wend (window-end))
510 (catch 'done
511 (while (not (input-pending-p))
512 (setq mc 0 wend (window-end))
513 ;; select non-ws character, but don't miss too much
514 (goto-char (+ wbeg (random (- wend wbeg))))
515 (while (looking-at "[ \n\f]")
516 (if (= total (setq mc (1+ mc)))
517 (throw 'done 'sel)
518 (goto-char (+ wbeg (random (- wend wbeg))))))
519 ;; character animation sequence
520 (let ((p (point)))
521 (when fret-p (zone-fret wbeg p))
522 (goto-char p)
523 (setq c (zone-cpos p)
524 fall-p (zone-fall-through-ws c wbeg wend)))
525 ;; assuming current-column has not changed...
526 (when (and pancake-p
527 fall-p
528 (< (count-lines (point-min) (point))
529 wh))
530 (let ((cw (ceiling (char-width (aref c 0)))))
531 (zone-replace-char cw 1 c ?@) (zone-park/sit-for wbeg 0.137)
532 (zone-replace-char cw nil c ?*) (zone-park/sit-for wbeg 0.137)
533 (zone-replace-char cw nil c ?_)))))))
535 (defun zone-pgm-drip-fretfully ()
536 (zone-pgm-drip t))
538 (defun zone-pgm-five-oclock-swan-dive ()
539 (zone-pgm-drip nil t))
541 (defun zone-pgm-martini-swan-dive ()
542 (zone-pgm-drip t t))
544 (defun zone-pgm-rat-race ()
545 (while (not (input-pending-p))
546 (zone-call '((zone-pgm-rotate 10)
547 (zone-pgm-drip-fretfully 15)
548 (zone-pgm-drip 10)
549 ((lambda ()
550 (goto-char (point-min))
551 (while (re-search-forward " +$" nil t)
552 (delete-region (match-beginning 0) (match-end 0))))
553 5)))))
556 ;;;; paragraph spazzing (for textish modes)
558 (defun zone-pgm-paragraph-spaz ()
559 (if (memq (zone-orig major-mode)
560 ;; there should be a better way to distinguish textish modes
561 '(text-mode texinfo-mode fundamental-mode))
562 (let ((fill-column fill-column)
563 (fc-min fill-column)
564 (fc-max fill-column)
565 (max-fc (1- (frame-width))))
566 (while (sit-for 0.1)
567 (fill-paragraph 1)
568 (setq fill-column (+ fill-column (- (random 5) 2)))
569 (when (< fill-column fc-min)
570 (setq fc-min fill-column))
571 (when (> fill-column max-fc)
572 (setq fill-column max-fc))
573 (when (> fill-column fc-max)
574 (setq fc-max fill-column))))
575 (message "Zoning... (zone-pgm-rotate)")
576 (zone-pgm-rotate)))
579 ;;;; stressing and destressing
581 (defun zone-pgm-stress ()
582 (goto-char (point-min))
583 (let ((ok t)
584 lines)
585 (while (and ok (< (point) (point-max)))
586 (let ((p (point)))
587 (setq ok (zerop (forward-line 1))
588 lines (cons (buffer-substring p (point)) lines))))
589 (sit-for 5)
590 (zone-hiding-modeline
591 (let ((msg "Zoning... (zone-pgm-stress)"))
592 (while (not (string= msg ""))
593 (message (setq msg (substring msg 1)))
594 (sit-for 0.05)))
595 (while (not (input-pending-p))
596 (when (< 50 (random 100))
597 (goto-char (point-max))
598 (forward-line -1)
599 (let ((kill-whole-line t))
600 (kill-line))
601 (goto-char (point-min))
602 (insert (nth (random (length lines)) lines)))
603 (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
604 (sit-for 0.1)))))
606 (defun zone-pgm-stress-destress ()
607 (zone-call 'zone-pgm-stress 25)
608 (zone-hiding-modeline
609 (sit-for 3)
610 (erase-buffer)
611 (sit-for 3)
612 (insert-buffer-substring "*Messages*")
613 (message "")
614 (goto-char (point-max))
615 (recenter -1)
616 (sit-for 3)
617 (delete-region (point-min) (window-start))
618 (message "hey why stress out anyway?")
619 (zone-call '((zone-pgm-rotate 30)
620 (zone-pgm-whack-chars 10)
621 zone-pgm-drip))))
624 ;;;; the lyfe so short the craft so long to lerne --chaucer
626 (defvar zone-pgm-random-life-wait nil
627 "*Seconds to wait between successive `life' generations.
628 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
630 (defun zone-pgm-random-life ()
631 (require 'life)
632 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
633 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
634 (bot (progn (goto-char (window-end)) (forward-line -7) (point)))
635 (rtc (- (frame-width) 11))
636 (min (window-start))
637 (max (1- (window-end)))
638 s c col)
639 (delete-region max (point-max))
640 (while (and (progn (goto-char min) (sit-for 0.05))
641 (progn (goto-char (+ min (random max)))
642 (or (progn (skip-chars-forward " @\n" max)
643 (not (= max (point))))
644 (unless (or (= 0 (skip-chars-backward " @\n" min))
645 (= min (point)))
646 (forward-char -1)
647 t))))
648 (unless (or (eolp) (eobp))
649 (setq s (zone-cpos (point))
650 c (aref s 0))
651 (zone-replace-char
652 (char-width c)
653 1 s (cond ((or (> top (point))
654 (< bot (point))
655 (or (> 11 (setq col (current-column)))
656 (< rtc col)))
658 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
659 ((and (<= ?A c) (>= ?Z c)) ?*)
660 (t ?@)))))
661 (sit-for 3)
662 (setq col nil)
663 (goto-char bot)
664 (while (< top (point))
665 (setq c (point))
666 (move-to-column 9)
667 (setq col (cons (buffer-substring (point) c) col))
668 ; (let ((inhibit-point-motion-hooks t))
669 (end-of-line 0);)
670 (forward-char -10))
671 (let ((life-patterns (vector
672 (if (and col (search-forward "@" max t))
673 (cons (make-string (length (car col)) 32) col)
674 (list (mapconcat 'identity
675 (make-list (/ (- rtc 11) 15)
676 (make-string 5 ?@))
677 (make-string 10 32)))))))
678 (life (or zone-pgm-random-life-wait (random 4)))
679 (kill-buffer nil))))
682 (random t)
684 ;;;;;;;;;;;;;;;
685 (provide 'zone)
687 ;;; zone.el ends here