1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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 (let ((f (selected-frame))
139 (outbuf (get-buffer-create "*zone*"))
140 (text (buffer-substring (window-start) (window-end)))
141 (wp (1+ (- (window-point (selected-window))
143 (put 'zone 'orig-buffer (current-buffer))
144 (put 'zone 'modeline-hidden-level 0)
145 (switch-to-buffer outbuf)
146 (setq mode-name "Zone")
148 (setq buffer-undo-list t
150 tab-width (zone-orig tab-width)
151 line-spacing (zone-orig line-spacing))
153 (untabify (point-min) (point-max))
154 (set-window-start (selected-window) (point-min))
155 (set-window-point (selected-window) wp)
157 (let ((pgm (elt zone-programs (random (length zone-programs))))
158 (ct (and f (frame-parameter f 'cursor-type)))
159 (restore (list '(kill-buffer outbuf))))
161 (modify-frame-parameters f '((cursor-type . (bar . 0))))
162 (setq restore (cons '(modify-frame-parameters
163 f (list (cons 'cursor-type ct)))
165 ;; Make `restore' a self-disabling one-shot thunk.
166 (setq restore `(lambda () ,@restore (setq restore nil)))
169 (message "Zoning... (%s)" pgm)
171 ;; If some input is pending, zone says "sorry", which
172 ;; isn't nice; this might happen e.g. when they invoke the
173 ;; game by clicking the menu bar. So discard any pending
174 ;; input before zoning out.
175 (if (input-pending-p)
178 (message "Zoning...sorry"))
181 (while (not (input-pending-p))
182 (message (format "We were zoning when we wrote %s..." pgm))
184 (message "...here's hoping we didn't hose your buffer!")
189 (message "Zoning...sorry")))
190 (when restore (funcall restore)))))
192 ;;;; Zone when idle, or not.
194 (defun zone-when-idle (secs)
195 "Zone out when Emacs has been idle for SECS seconds."
196 (interactive "nHow long before I start zoning (seconds): ")
197 (if (timerp zone-timer)
198 (cancel-timer zone-timer))
199 (setq zone-timer nil)
201 (setq zone-timer (run-with-idle-timer secs t 'zone))))
203 (defun zone-leave-me-alone ()
204 "Don't zone out when Emacs is idle."
206 (if (timerp zone-timer)
207 (cancel-timer zone-timer))
208 (setq zone-timer nil)
209 (message "I won't zone out any more"))
214 (defun zone-shift-up ()
216 (e (progn (forward-line 1) (point)))
217 (s (buffer-substring b e)))
219 (goto-char (point-max))
222 (defun zone-shift-down ()
223 (goto-char (point-max))
225 (e (progn (forward-line -1) (point)))
226 (s (buffer-substring b e)))
228 (goto-char (point-min))
231 (defun zone-shift-left ()
235 (setq s (buffer-substring (point) (1+ (point))))
241 (defun zone-shift-right ()
242 (goto-char (point-max))
247 (setq s (buffer-substring (1- (point)) (point)))
253 (defun zone-pgm-jitter ()
260 (goto-char (point-min))
261 (while (not (input-pending-p))
262 (funcall (elt ops (random (length ops))))
263 (goto-char (point-min))
269 (defun zone-pgm-whack-chars ()
270 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
271 (while (not (input-pending-p))
274 (aset tbl i (+ 48 (random (- 123 48))))
276 (translate-region (point-min) (point-max) tbl)
279 (put 'zone-pgm-whack-chars 'wc-tbl
280 (let ((tbl (make-string 128 ?x))
289 (defun zone-remove-text ()
294 (goto-char (point-min))
296 (if (looking-at "[^(){}\n\t ]")
297 (let ((n (random 5)))
307 (defun zone-pgm-dissolve ()
314 (defun zone-exploding-remove ()
318 (goto-char (point-min))
320 (if (looking-at "[^*\n\t ]")
321 (let ((n (random 5)))
330 (defun zone-pgm-explode ()
331 (zone-exploding-remove)
337 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
338 ;; instances of the same letter have the same case, which produces a
339 ;; less interesting effect than you might imagine.
340 (defun zone-pgm-2nd-putz-with-case ()
341 (let ((tbl (make-string 128 ?x))
346 (while (not (input-pending-p))
350 (if (zerop (random 5))
353 (setq i (+ i (1+ (random 5)))))
357 (if (zerop (random 5))
360 (setq i (+ i (1+ (random 5)))))
361 (translate-region (point-min) (point-max) tbl)
364 (defun zone-pgm-putz-with-case ()
365 (goto-char (point-min))
366 (while (not (input-pending-p))
367 (let ((np (+ 2 (random 5)))
371 (let ((prec (preceding-char))
372 (props (text-properties-at (1- (point)))))
373 (insert (if (zerop (random 2))
376 (set-text-properties (1- (point)) (point) props))
379 (setq np (+ np (1+ (random 5))))))
380 (goto-char (point-min))
386 (defun zone-line-specs ()
389 (goto-char (window-start))
390 (while (< (point) (window-end))
391 (when (looking-at "[\t ]*\\([^\n]+\\)")
392 (setq ret (cons (cons (match-beginning 1) (match-end 1)) ret)))
396 (defun zone-pgm-rotate (&optional random-style)
400 (mapcar (lambda (ent)
401 (let* ((beg (car ent))
403 (amt (if random-style
404 (funcall random-style)
406 (when (< (- end (abs amt)) beg)
407 (setq amt (random (- end beg))))
411 (vector amt beg (- end (abs amt)))
416 amt aamt cut paste txt i ent)
417 (while (not (input-pending-p))
420 (setq ent (aref specs i))
421 (setq amt (aref ent 0) aamt (abs amt))
424 (setq cut 2 paste 1))
425 (goto-char (aref ent cut))
426 (setq txt (buffer-substring (point) (+ (point) aamt)))
428 (goto-char (aref ent paste))
433 (defun zone-pgm-rotate-LR-lockstep ()
434 (zone-pgm-rotate (lambda () 1)))
436 (defun zone-pgm-rotate-RL-lockstep ()
437 (zone-pgm-rotate (lambda () -1)))
439 (defun zone-pgm-rotate-LR-variable ()
440 (zone-pgm-rotate (lambda () (1+ (random 3)))))
442 (defun zone-pgm-rotate-RL-variable ()
443 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
448 (defsubst zone-cpos (pos)
449 (buffer-substring pos (1+ pos)))
451 (defsubst zone-replace-char (count del-count char-as-string new-value)
452 (delete-char (or del-count (- count)))
453 (aset char-as-string 0 new-value)
454 (dotimes (i count) (insert char-as-string)))
456 (defsubst zone-park/sit-for (pos seconds)
459 (prog1 (sit-for seconds)
462 (defun zone-fret (wbeg pos)
463 (let* ((case-fold-search nil)
464 (c-string (zone-cpos pos))
465 (cw-ceil (ceiling (char-width (aref c-string 0))))
467 ((string-match "[a-z]" c-string) (upcase c-string))
468 ((string-match "[A-Z]" c-string) (downcase c-string))
469 (t (propertize " " 'display `(space :width ,cw-ceil))))))
471 (wait 0.5 (* wait 0.8)))
475 (insert (if (= 0 (% i 2)) hmm c-string))
476 (zone-park/sit-for wbeg wait))
477 (delete-char -1) (insert c-string)))
479 (defun zone-fill-out-screen (width height)
480 (let ((start (window-start))
481 (line (make-string width 32)))
483 ;; fill out rectangular ws block
484 (while (progn (end-of-line)
485 (let ((cc (current-column)))
487 (insert (substring line cc))
488 (delete-char (- width cc)))
489 (cond ((eobp) (insert "\n") nil)
490 (t (forward-char 1) t)))))
491 ;; pad ws past bottom of screen
492 (let ((nl (- height (count-lines (point-min) (point)))))
494 (setq line (concat line "\n"))
502 (defun zone-fall-through-ws (c wbeg wend)
503 (let* ((cw-ceil (ceiling (char-width (aref c 0))))
504 (spaces (make-string cw-ceil 32))
505 (col (current-column))
508 (while (when (save-excursion
510 (and (= col (current-column))
511 (setq newpos (point))
512 (string= spaces (buffer-substring-no-properties
513 newpos (+ newpos cw-ceil)))
514 (setq newpos (+ newpos (1- cw-ceil)))))
519 (when (< (point) wend)
520 (delete-char cw-ceil)
523 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
526 (defun zone-pgm-drip (&optional fret-p pancake-p)
527 (let* ((ww (1- (window-width)))
533 (zone-fill-out-screen ww wh)
534 (setq wbeg (window-start)
537 (while (not (input-pending-p))
538 (setq mc 0 wend (window-end))
539 ;; select non-ws character, but don't miss too much
540 (goto-char (+ wbeg (random (- wend wbeg))))
541 (while (looking-at "[ \n\f]")
542 (if (= total (setq mc (1+ mc)))
544 (goto-char (+ wbeg (random (- wend wbeg))))))
545 ;; character animation sequence
547 (when fret-p (zone-fret wbeg p))
549 (setq c (zone-cpos p)
550 fall-p (zone-fall-through-ws c wbeg wend)))
551 ;; assuming current-column has not changed...
554 (< (count-lines (point-min) (point))
556 (let ((cw (ceiling (char-width (aref c 0)))))
557 (zone-replace-char cw 1 c ?@) (zone-park/sit-for wbeg 0.137)
558 (zone-replace-char cw nil c ?*) (zone-park/sit-for wbeg 0.137)
559 (zone-replace-char cw nil c ?_)))))))
561 (defun zone-pgm-drip-fretfully ()
564 (defun zone-pgm-five-oclock-swan-dive ()
565 (zone-pgm-drip nil t))
567 (defun zone-pgm-martini-swan-dive ()
571 ;;;; paragraph spazzing (for textish modes)
573 (defun zone-pgm-paragraph-spaz ()
574 (if (memq (zone-orig major-mode)
575 ;; there should be a better way to distinguish textish modes
576 '(text-mode texinfo-mode fundamental-mode))
577 (let ((fill-column fill-column)
580 (max-fc (1- (frame-width))))
583 (setq fill-column (+ fill-column (- (random 5) 2)))
584 (when (< fill-column fc-min)
585 (setq fc-min fill-column))
586 (when (> fill-column max-fc)
587 (setq fill-column max-fc))
588 (when (> fill-column fc-max)
589 (setq fc-max fill-column))))
590 (message "Zoning... (zone-pgm-rotate)")
594 ;;;; stressing and destressing
596 (defun zone-pgm-stress ()
597 (goto-char (point-min))
599 (while (< (point) (point-max))
602 (setq lines (cons (buffer-substring p (point)) lines))))
604 (zone-hiding-modeline
605 (let ((msg "Zoning... (zone-pgm-stress)"))
606 (while (not (string= msg ""))
607 (message (setq msg (substring msg 1)))
609 (while (not (input-pending-p))
610 (when (< 50 (random 100))
611 (goto-char (point-max))
613 (let ((kill-whole-line t))
615 (goto-char (point-min))
616 (insert (nth (random (length lines)) lines)))
617 (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
620 (defun zone-pgm-stress-destress ()
621 (zone-call 'zone-pgm-stress 25)
622 (zone-hiding-modeline
626 (insert-buffer-substring "*Messages*")
628 (goto-char (point-max))
631 (delete-region (point-min) (window-start))
632 (message "hey why stress out anyway?")
633 (zone-call '((zone-pgm-rotate 30)
634 (zone-pgm-whack-chars 10)
638 ;;;; the lyfe so short the craft so long to lerne --chaucer
640 (defvar zone-pgm-random-life-wait nil
641 "*Seconds to wait between successive `life' generations.
642 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
644 (defun zone-pgm-random-life ()
646 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
647 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
648 (bot (progn (goto-char (window-end)) (forward-line -7) (point)))
649 (rtc (- (frame-width) 11))
651 (max (1- (window-end)))
653 (delete-region max (point-max))
654 (while (and (progn (goto-char min) (sit-for 0.05))
655 (progn (goto-char (+ min (random max)))
656 (or (progn (skip-chars-forward " @\n" max)
657 (not (= max (point))))
658 (unless (or (= 0 (skip-chars-backward " @\n" min))
662 (unless (or (eolp) (eobp))
663 (setq s (zone-cpos (point))
667 1 s (cond ((or (> top (point))
669 (or (> 11 (setq col (current-column)))
672 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
673 ((and (<= ?A c) (>= ?Z c)) ?*)
678 (while (< top (point))
681 (setq col (cons (buffer-substring (point) c) col))
684 (let ((life-patterns (vector
685 (if (and col (search-forward "@" max t))
686 (cons (make-string (length (car col)) 32) col)
687 (list (mapconcat 'identity
688 (make-list (/ (- rtc 11) 15)
690 (make-string 10 32)))))))
691 (life (or zone-pgm-random-life-wait (random 4)))
698 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
699 ;;; zone.el ends here