1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Victor Zandy <zandy@cs.wisc.edu>
7 ;; Maintainer: Thien-Thi Nguyen <ttn@gnu.org>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Don't zone out in front of Emacs! Try M-x zone.
31 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
33 ;; Bored by the zone pyrotechnics? Write your own! Add it to
34 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
36 ;; WARNING: Not appropriate for Emacs sessions over modems or
37 ;; computers as slow as mine.
39 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
40 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
41 ;; Luigi Panzeri, John Paul Wallington.
47 (eval-when-compile (require 'cl))
49 (defvar zone-timer nil
50 "The timer we use to decide when to zone out, or nil if none.")
52 (defvar zone-timeout nil
53 "*Seconds to timeout the zoning.
54 If nil, don't interrupt for about 1^26 seconds.")
56 ;; Vector of functions that zone out. `zone' will execute one of
57 ;; these functions, randomly chosen. The chosen function is invoked
58 ;; in the *zone* buffer, which contains the text of the selected
59 ;; window. If the function loops, it *must* periodically check and
60 ;; halt if `input-pending-p' is t (because quitting is disabled when
61 ;; Emacs idle timers are run).
62 (defvar zone-programs [
64 zone-pgm-putz-with-case
69 zone-pgm-rotate-LR-lockstep
70 zone-pgm-rotate-RL-lockstep
71 zone-pgm-rotate-LR-variable
72 zone-pgm-rotate-RL-variable
74 zone-pgm-drip-fretfully
75 zone-pgm-five-oclock-swan-dive
76 zone-pgm-martini-swan-dive
77 zone-pgm-paragraph-spaz
79 zone-pgm-stress-destress
83 (defmacro zone-orig (&rest body)
84 `(with-current-buffer (get 'zone 'orig-buffer)
87 (defmacro zone-hiding-modeline (&rest body)
88 `(let (bg mode-line-fg mode-line-bg mode-line-box)
91 (when (and (= 0 (get 'zone 'modeline-hidden-level))
93 (setq bg (face-background 'default)
94 mode-line-box (face-attribute 'mode-line :box)
95 mode-line-fg (face-attribute 'mode-line :foreground)
96 mode-line-bg (face-attribute 'mode-line :background))
97 (set-face-attribute 'mode-line nil
101 (put 'zone 'modeline-hidden-level
102 (1+ (get 'zone 'modeline-hidden-level)))
104 (put 'zone 'modeline-hidden-level
105 (1- (get 'zone 'modeline-hidden-level)))
106 (when (and (> 1 (get 'zone 'modeline-hidden-level))
108 (set-face-attribute 'mode-line nil
109 :foreground mode-line-fg
110 :background mode-line-bg
111 :box mode-line-box)))))
113 (defun zone-call (program &optional timeout)
114 "Call PROGRAM in a zoned way.
115 If PROGRAM is a function, call it, interrupting after the amount
116 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
118 PROGRAM can also be a list of elements, which are interpreted like so:
119 If the element is a function or a list of a function and a number,
120 apply `zone-call' recursively."
121 (cond ((functionp program)
122 (with-timeout ((or timeout zone-timeout (ash 1 26)))
125 (mapcar (lambda (elem)
126 (cond ((functionp elem) (zone-call elem))
128 (functionp (car elem))
129 (numberp (cadr elem)))
130 (apply 'zone-call elem))
131 (t (error "bad `zone-call' elem: %S" elem))))
136 "Zone out, completely."
138 (save-window-excursion
139 (let ((f (selected-frame))
140 (outbuf (get-buffer-create "*zone*"))
141 (text (buffer-substring (window-start) (window-end)))
142 (wp (1+ (- (window-point (selected-window))
144 (put 'zone 'orig-buffer (current-buffer))
145 (put 'zone 'modeline-hidden-level 0)
146 (switch-to-buffer outbuf)
147 (setq mode-name "Zone")
149 (setq buffer-undo-list t
151 tab-width (zone-orig tab-width)
152 line-spacing (zone-orig line-spacing))
154 (untabify (point-min) (point-max))
155 (set-window-start (selected-window) (point-min))
156 (set-window-point (selected-window) wp)
158 (let ((pgm (elt zone-programs (random (length zone-programs))))
159 (ct (and f (frame-parameter f 'cursor-type)))
160 (restore (list '(kill-buffer outbuf))))
162 (modify-frame-parameters f '((cursor-type . (bar . 0))))
163 (setq restore (cons '(modify-frame-parameters
164 f (list (cons 'cursor-type ct)))
166 ;; Make `restore' a self-disabling one-shot thunk.
167 (setq restore `(lambda () ,@restore (setq restore nil)))
170 (message "Zoning... (%s)" pgm)
172 ;; If some input is pending, zone says "sorry", which
173 ;; isn't nice; this might happen e.g. when they invoke the
174 ;; game by clicking the menu bar. So discard any pending
175 ;; input before zoning out.
176 (if (input-pending-p)
179 (message "Zoning...sorry"))
182 (while (not (input-pending-p))
183 (message "We were zoning when we wrote %s..." pgm)
185 (message "...here's hoping we didn't hose your buffer!")
190 (message "Zoning...sorry")))
191 (when restore (funcall restore))))))
193 ;;;; Zone when idle, or not.
195 (defun zone-when-idle (secs)
196 "Zone out when Emacs has been idle for SECS seconds."
197 (interactive "nHow long before I start zoning (seconds): ")
198 (if (timerp zone-timer)
199 (cancel-timer zone-timer))
200 (setq zone-timer nil)
202 (setq zone-timer (run-with-idle-timer secs t 'zone))))
204 (defun zone-leave-me-alone ()
205 "Don't zone out when Emacs is idle."
207 (if (timerp zone-timer)
208 (cancel-timer zone-timer))
209 (setq zone-timer nil)
210 (message "I won't zone out any more"))
215 (defun zone-shift-up ()
217 (e (progn (forward-line 1) (point)))
218 (s (buffer-substring b e)))
220 (goto-char (point-max))
223 (defun zone-shift-down ()
224 (goto-char (point-max))
226 (e (progn (forward-line -1) (point)))
227 (s (buffer-substring b e)))
229 (goto-char (point-min))
232 (defun zone-shift-left ()
236 (setq s (buffer-substring (point) (1+ (point))))
242 (defun zone-shift-right ()
243 (goto-char (point-max))
248 (setq s (buffer-substring (1- (point)) (point)))
254 (defun zone-pgm-jitter ()
261 (goto-char (point-min))
262 (while (not (input-pending-p))
263 (funcall (elt ops (random (length ops))))
264 (goto-char (point-min))
270 (defun zone-pgm-whack-chars ()
271 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
272 (while (not (input-pending-p))
275 (aset tbl i (+ 48 (random (- 123 48))))
277 (translate-region (point-min) (point-max) tbl)
280 (put 'zone-pgm-whack-chars 'wc-tbl
281 (let ((tbl (make-string 128 ?x))
290 (defun zone-remove-text ()
295 (goto-char (point-min))
297 (if (looking-at "[^(){}\n\t ]")
298 (let ((n (random 5)))
308 (defun zone-pgm-dissolve ()
315 (defun zone-exploding-remove ()
319 (goto-char (point-min))
321 (if (looking-at "[^*\n\t ]")
322 (let ((n (random 5)))
331 (defun zone-pgm-explode ()
332 (zone-exploding-remove)
338 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
339 ;; instances of the same letter have the same case, which produces a
340 ;; less interesting effect than you might imagine.
341 (defun zone-pgm-2nd-putz-with-case ()
342 (let ((tbl (make-string 128 ?x))
347 (while (not (input-pending-p))
351 (if (zerop (random 5))
354 (setq i (+ i (1+ (random 5)))))
358 (if (zerop (random 5))
361 (setq i (+ i (1+ (random 5)))))
362 (translate-region (point-min) (point-max) tbl)
365 (defun zone-pgm-putz-with-case ()
366 (goto-char (point-min))
367 (while (not (input-pending-p))
368 (let ((np (+ 2 (random 5)))
372 (let ((prec (preceding-char))
373 (props (text-properties-at (1- (point)))))
374 (insert (if (zerop (random 2))
377 (set-text-properties (1- (point)) (point) props))
380 (setq np (+ np (1+ (random 5))))))
381 (goto-char (point-min))
387 (defun zone-line-specs ()
390 (goto-char (window-start))
391 (while (< (point) (window-end))
392 (when (looking-at "[\t ]*\\([^\n]+\\)")
393 (setq ret (cons (cons (match-beginning 1) (match-end 1)) ret)))
397 (defun zone-pgm-rotate (&optional random-style)
401 (mapcar (lambda (ent)
402 (let* ((beg (car ent))
404 (amt (if random-style
405 (funcall random-style)
407 (when (< (- end (abs amt)) beg)
408 (setq amt (random (- end beg))))
412 (vector amt beg (- end (abs amt)))
417 amt aamt cut paste txt i ent)
418 (while (not (input-pending-p))
421 (setq ent (aref specs i))
422 (setq amt (aref ent 0) aamt (abs amt))
425 (setq cut 2 paste 1))
426 (goto-char (aref ent cut))
427 (setq txt (buffer-substring (point) (+ (point) aamt)))
429 (goto-char (aref ent paste))
434 (defun zone-pgm-rotate-LR-lockstep ()
435 (zone-pgm-rotate (lambda () 1)))
437 (defun zone-pgm-rotate-RL-lockstep ()
438 (zone-pgm-rotate (lambda () -1)))
440 (defun zone-pgm-rotate-LR-variable ()
441 (zone-pgm-rotate (lambda () (1+ (random 3)))))
443 (defun zone-pgm-rotate-RL-variable ()
444 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
449 (defsubst zone-cpos (pos)
450 (buffer-substring pos (1+ pos)))
452 (defsubst zone-replace-char (count del-count char-as-string new-value)
453 (delete-char (or del-count (- count)))
454 (aset char-as-string 0 new-value)
455 (dotimes (i count) (insert char-as-string)))
457 (defsubst zone-park/sit-for (pos seconds)
460 (prog1 (sit-for seconds)
463 (defun zone-fret (wbeg pos)
464 (let* ((case-fold-search nil)
465 (c-string (zone-cpos pos))
466 (cw-ceil (ceiling (char-width (aref c-string 0))))
468 ((string-match "[a-z]" c-string) (upcase c-string))
469 ((string-match "[A-Z]" c-string) (downcase c-string))
470 (t (propertize " " 'display `(space :width ,cw-ceil))))))
472 (wait 0.5 (* wait 0.8)))
476 (insert (if (= 0 (% i 2)) hmm c-string))
477 (zone-park/sit-for wbeg wait))
478 (delete-char -1) (insert c-string)))
480 (defun zone-fill-out-screen (width height)
481 (let ((start (window-start))
482 (line (make-string width 32)))
484 ;; fill out rectangular ws block
485 (while (progn (end-of-line)
486 (let ((cc (current-column)))
488 (insert (substring line cc))
489 (delete-char (- width cc)))
490 (cond ((eobp) (insert "\n") nil)
491 (t (forward-char 1) t)))))
492 ;; pad ws past bottom of screen
493 (let ((nl (- height (count-lines (point-min) (point)))))
495 (setq line (concat line "\n"))
503 (defun zone-fall-through-ws (c wbeg wend)
504 (let* ((cw-ceil (ceiling (char-width (aref c 0))))
505 (spaces (make-string cw-ceil 32))
506 (col (current-column))
509 (while (when (save-excursion
511 (and (= col (current-column))
512 (setq newpos (point))
513 (string= spaces (buffer-substring-no-properties
514 newpos (+ newpos cw-ceil)))
515 (setq newpos (+ newpos (1- cw-ceil)))))
520 (when (< (point) wend)
521 (delete-char cw-ceil)
524 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
527 (defun zone-pgm-drip (&optional fret-p pancake-p)
528 (let* ((ww (1- (window-width)))
534 (zone-fill-out-screen ww wh)
535 (setq wbeg (window-start)
538 (while (not (input-pending-p))
539 (setq mc 0 wend (window-end))
540 ;; select non-ws character, but don't miss too much
541 (goto-char (+ wbeg (random (- wend wbeg))))
542 (while (looking-at "[ \n\f]")
543 (if (= total (setq mc (1+ mc)))
545 (goto-char (+ wbeg (random (- wend wbeg))))))
546 ;; character animation sequence
548 (when fret-p (zone-fret wbeg p))
550 (setq c (zone-cpos p)
551 fall-p (zone-fall-through-ws c wbeg wend)))
552 ;; assuming current-column has not changed...
555 (< (count-lines (point-min) (point))
557 (let ((cw (ceiling (char-width (aref c 0)))))
558 (zone-replace-char cw 1 c ?@) (zone-park/sit-for wbeg 0.137)
559 (zone-replace-char cw nil c ?*) (zone-park/sit-for wbeg 0.137)
560 (zone-replace-char cw nil c ?_)))))))
562 (defun zone-pgm-drip-fretfully ()
565 (defun zone-pgm-five-oclock-swan-dive ()
566 (zone-pgm-drip nil t))
568 (defun zone-pgm-martini-swan-dive ()
572 ;;;; paragraph spazzing (for textish modes)
574 (defun zone-pgm-paragraph-spaz ()
575 (if (memq (zone-orig major-mode)
576 ;; there should be a better way to distinguish textish modes
577 '(text-mode texinfo-mode fundamental-mode))
578 (let ((fill-column fill-column)
581 (max-fc (1- (frame-width))))
584 (setq fill-column (+ fill-column (- (random 5) 2)))
585 (when (< fill-column fc-min)
586 (setq fc-min fill-column))
587 (when (> fill-column max-fc)
588 (setq fill-column max-fc))
589 (when (> fill-column fc-max)
590 (setq fc-max fill-column))))
591 (message "Zoning... (zone-pgm-rotate)")
595 ;;;; stressing and destressing
597 (defun zone-pgm-stress ()
598 (goto-char (point-min))
600 (while (< (point) (point-max))
603 (setq lines (cons (buffer-substring p (point)) lines))))
605 (zone-hiding-modeline
606 (let ((msg "Zoning... (zone-pgm-stress)"))
607 (while (not (string= msg ""))
608 (message (setq msg (substring msg 1)))
610 (while (not (input-pending-p))
611 (when (< 50 (random 100))
612 (goto-char (point-max))
614 (let ((kill-whole-line t))
616 (goto-char (point-min))
617 (insert (nth (random (length lines)) lines)))
618 (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
621 (defun zone-pgm-stress-destress ()
622 (zone-call 'zone-pgm-stress 25)
623 (zone-hiding-modeline
627 (insert-buffer-substring "*Messages*")
629 (goto-char (point-max))
632 (delete-region (point-min) (window-start))
633 (message "hey why stress out anyway?")
634 (zone-call '((zone-pgm-rotate 30)
635 (zone-pgm-whack-chars 10)
639 ;;;; the lyfe so short the craft so long to lerne --chaucer
641 (defvar zone-pgm-random-life-wait nil
642 "*Seconds to wait between successive `life' generations.
643 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
645 (defun zone-pgm-random-life ()
647 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
648 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
649 (bot (progn (goto-char (window-end)) (forward-line -7) (point)))
650 (rtc (- (frame-width) 11))
652 (max (1- (window-end)))
654 (delete-region max (point-max))
655 (while (and (progn (goto-char min) (sit-for 0.05))
656 (progn (goto-char (+ min (random max)))
657 (or (progn (skip-chars-forward " @\n" max)
658 (not (= max (point))))
659 (unless (or (= 0 (skip-chars-backward " @\n" min))
663 (unless (or (eolp) (eobp))
664 (setq s (zone-cpos (point))
668 1 s (cond ((or (> top (point))
670 (or (> 11 (setq col (current-column)))
673 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
674 ((and (<= ?A c) (>= ?Z c)) ?*)
679 (while (< top (point))
682 (setq col (cons (buffer-substring (point) c) col))
685 (let ((life-patterns (vector
686 (if (and col (search-forward "@" max t))
687 (cons (make-string (length (car col)) 32) col)
688 (list (mapconcat 'identity
689 (make-list (/ (- rtc 11) 15)
691 (make-string 10 32)))))))
692 (life (or zone-pgm-random-life-wait (random 4)))
699 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
700 ;;; zone.el ends here