1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
5 ;; Author: Victor Zandy <zandy@cs.wisc.edu>
6 ;; Maintainer: Thien-Thi Nguyen <ttn@gnu.org>
8 ;; Created: June 6, 1998
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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Don't zone out in front of Emacs! Try M-x zone.
30 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
32 ;; Bored by the zone pyrotechnics? Write your own! Add it to
33 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
35 ;; WARNING: Not appropriate for Emacs sessions over modems or
36 ;; computers as slow as mine.
38 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
39 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
40 ;; Luigi Panzeri, John Paul Wallington.
46 (eval-when-compile (require 'cl
))
48 (defvar zone-timer nil
49 "The timer we use to decide when to zone out, or nil if none.")
51 (defvar zone-timeout nil
52 "*Seconds to timeout the zoning.
53 If nil, don't interrupt for about 1^26 seconds.")
55 ;; Vector of functions that zone out. `zone' will execute one of
56 ;; these functions, randomly chosen. The chosen function is invoked
57 ;; in the *zone* buffer, which contains the text of the selected
58 ;; window. If the function loops, it *must* periodically check and
59 ;; halt if `input-pending-p' is t (because quitting is disabled when
60 ;; Emacs idle timers are run).
61 (defvar zone-programs
[
63 zone-pgm-putz-with-case
68 zone-pgm-rotate-LR-lockstep
69 zone-pgm-rotate-RL-lockstep
70 zone-pgm-rotate-LR-variable
71 zone-pgm-rotate-RL-variable
73 zone-pgm-drip-fretfully
74 zone-pgm-five-oclock-swan-dive
75 zone-pgm-martini-swan-dive
76 zone-pgm-paragraph-spaz
78 zone-pgm-stress-destress
82 (defmacro zone-orig
(&rest body
)
83 `(with-current-buffer (get 'zone
'orig-buffer
)
86 (defmacro zone-hiding-modeline
(&rest body
)
87 `(let (bg mode-line-fg mode-line-bg mode-line-box
)
90 (when (and (= 0 (get 'zone
'modeline-hidden-level
))
92 (setq bg
(face-background 'default
)
93 mode-line-box
(face-attribute 'mode-line
:box
)
94 mode-line-fg
(face-attribute 'mode-line
:foreground
)
95 mode-line-bg
(face-attribute 'mode-line
:background
))
96 (set-face-attribute 'mode-line nil
100 (put 'zone
'modeline-hidden-level
101 (1+ (get 'zone
'modeline-hidden-level
)))
103 (put 'zone
'modeline-hidden-level
104 (1- (get 'zone
'modeline-hidden-level
)))
105 (when (and (> 1 (get 'zone
'modeline-hidden-level
))
107 (set-face-attribute 'mode-line nil
108 :foreground mode-line-fg
109 :background mode-line-bg
110 :box mode-line-box
)))))
112 (defun zone-call (program &optional timeout
)
113 "Call PROGRAM in a zoned way.
114 If PROGRAM is a function, call it, interrupting after the amount
115 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
117 PROGRAM can also be a list of elements, which are interpreted like so:
118 If the element is a function or a list of a function and a number,
119 apply `zone-call' recursively."
120 (cond ((functionp program
)
121 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
124 (mapcar (lambda (elem)
125 (cond ((functionp elem
) (zone-call elem
))
127 (functionp (car elem
))
128 (numberp (cadr elem
)))
129 (apply 'zone-call elem
))
130 (t (error "bad `zone-call' elem: %S" elem
))))
135 "Zone out, completely."
137 (let ((f (selected-frame))
138 (outbuf (get-buffer-create "*zone*"))
139 (text (buffer-substring (window-start) (window-end)))
140 (wp (1+ (- (window-point (selected-window))
142 (put 'zone
'orig-buffer
(current-buffer))
143 (put 'zone
'modeline-hidden-level
0)
144 (switch-to-buffer outbuf
)
145 (setq mode-name
"Zone")
147 (setq buffer-undo-list t
149 tab-width
(zone-orig tab-width
)
150 line-spacing
(zone-orig line-spacing
))
152 (untabify (point-min) (point-max))
153 (set-window-start (selected-window) (point-min))
154 (set-window-point (selected-window) wp
)
156 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
157 (ct (and f
(frame-parameter f
'cursor-type
)))
158 (restore (list '(kill-buffer outbuf
))))
160 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
161 (setq restore
(cons '(modify-frame-parameters
162 f
(list (cons 'cursor-type ct
)))
164 ;; Make `restore' a self-disabling one-shot thunk.
165 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
168 (message "Zoning... (%s)" pgm
)
170 ;; If some input is pending, zone says "sorry", which
171 ;; isn't nice; this might happen e.g. when they invoke the
172 ;; game by clicking the menu bar. So discard any pending
173 ;; input before zoning out.
174 (if (input-pending-p)
177 (message "Zoning...sorry"))
180 (while (not (input-pending-p))
181 (message (format "We were zoning when we wrote %s..." pgm
))
183 (message "...here's hoping we didn't hose your buffer!")
188 (message "Zoning...sorry")))
189 (when restore
(funcall restore
)))))
191 ;;;; Zone when idle, or not.
193 (defun zone-when-idle (secs)
194 "Zone out when Emacs has been idle for SECS seconds."
195 (interactive "nHow long before I start zoning (seconds): ")
196 (if (timerp zone-timer
)
197 (cancel-timer zone-timer
))
198 (setq zone-timer nil
)
200 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
202 (defun zone-leave-me-alone ()
203 "Don't zone out when Emacs is idle."
205 (if (timerp zone-timer
)
206 (cancel-timer zone-timer
))
207 (setq zone-timer nil
)
208 (message "I won't zone out any more"))
213 (defun zone-shift-up ()
215 (e (progn (forward-line 1) (point)))
216 (s (buffer-substring b e
)))
218 (goto-char (point-max))
221 (defun zone-shift-down ()
222 (goto-char (point-max))
224 (e (progn (forward-line -
1) (point)))
225 (s (buffer-substring b e
)))
227 (goto-char (point-min))
230 (defun zone-shift-left ()
234 (setq s
(buffer-substring (point) (1+ (point))))
240 (defun zone-shift-right ()
241 (goto-char (point-max))
246 (setq s
(buffer-substring (1- (point)) (point)))
252 (defun zone-pgm-jitter ()
259 (goto-char (point-min))
260 (while (not (input-pending-p))
261 (funcall (elt ops
(random (length ops
))))
262 (goto-char (point-min))
268 (defun zone-pgm-whack-chars ()
269 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
270 (while (not (input-pending-p))
273 (aset tbl i
(+ 48 (random (- 123 48))))
275 (translate-region (point-min) (point-max) tbl
)
278 (put 'zone-pgm-whack-chars
'wc-tbl
279 (let ((tbl (make-string 128 ?x
))
288 (defun zone-remove-text ()
293 (goto-char (point-min))
295 (if (looking-at "[^(){}\n\t ]")
296 (let ((n (random 5)))
306 (defun zone-pgm-dissolve ()
313 (defun zone-exploding-remove ()
317 (goto-char (point-min))
319 (if (looking-at "[^*\n\t ]")
320 (let ((n (random 5)))
329 (defun zone-pgm-explode ()
330 (zone-exploding-remove)
336 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
337 ;; instances of the same letter have the same case, which produces a
338 ;; less interesting effect than you might imagine.
339 (defun zone-pgm-2nd-putz-with-case ()
340 (let ((tbl (make-string 128 ?x
))
345 (while (not (input-pending-p))
349 (if (zerop (random 5))
352 (setq i
(+ i
(1+ (random 5)))))
356 (if (zerop (random 5))
359 (setq i
(+ i
(1+ (random 5)))))
360 (translate-region (point-min) (point-max) tbl
)
363 (defun zone-pgm-putz-with-case ()
364 (goto-char (point-min))
365 (while (not (input-pending-p))
366 (let ((np (+ 2 (random 5)))
370 (let ((prec (preceding-char))
371 (props (text-properties-at (1- (point)))))
372 (insert (if (zerop (random 2))
375 (set-text-properties (1- (point)) (point) props
))
378 (setq np
(+ np
(1+ (random 5))))))
379 (goto-char (point-min))
385 (defun zone-line-specs ()
388 (goto-char (window-start))
389 (while (< (point) (window-end))
390 (when (looking-at "[\t ]*\\([^\n]+\\)")
391 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
395 (defun zone-pgm-rotate (&optional random-style
)
399 (mapcar (lambda (ent)
400 (let* ((beg (car ent
))
402 (amt (if random-style
403 (funcall random-style
)
405 (when (< (- end
(abs amt
)) beg
)
406 (setq amt
(random (- end beg
))))
410 (vector amt beg
(- end
(abs amt
)))
415 amt aamt cut paste txt i ent
)
416 (while (not (input-pending-p))
419 (setq ent
(aref specs i
))
420 (setq amt
(aref ent
0) aamt
(abs amt
))
423 (setq cut
2 paste
1))
424 (goto-char (aref ent cut
))
425 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
427 (goto-char (aref ent paste
))
432 (defun zone-pgm-rotate-LR-lockstep ()
433 (zone-pgm-rotate (lambda () 1)))
435 (defun zone-pgm-rotate-RL-lockstep ()
436 (zone-pgm-rotate (lambda () -
1)))
438 (defun zone-pgm-rotate-LR-variable ()
439 (zone-pgm-rotate (lambda () (1+ (random 3)))))
441 (defun zone-pgm-rotate-RL-variable ()
442 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
447 (defsubst zone-cpos
(pos)
448 (buffer-substring pos
(1+ pos
)))
450 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
451 (delete-char (or del-count
(- count
)))
452 (aset char-as-string
0 new-value
)
453 (dotimes (i count
) (insert char-as-string
)))
455 (defsubst zone-park
/sit-for
(pos seconds
)
458 (prog1 (sit-for seconds
)
461 (defun zone-fret (wbeg pos
)
462 (let* ((case-fold-search nil
)
463 (c-string (zone-cpos pos
))
464 (cw-ceil (ceiling (char-width (aref c-string
0))))
466 ((string-match "[a-z]" c-string
) (upcase c-string
))
467 ((string-match "[A-Z]" c-string
) (downcase c-string
))
468 (t (propertize " " 'display
`(space :width
,cw-ceil
))))))
470 (wait 0.5 (* wait
0.8)))
474 (insert (if (= 0 (% i
2)) hmm c-string
))
475 (zone-park/sit-for wbeg wait
))
476 (delete-char -
1) (insert c-string
)))
478 (defun zone-fill-out-screen (width height
)
479 (let ((start (window-start))
480 (line (make-string width
32)))
482 ;; fill out rectangular ws block
483 (while (progn (end-of-line)
484 (let ((cc (current-column)))
486 (insert (substring line cc
))
487 (delete-char (- width cc
)))
488 (cond ((eobp) (insert "\n") nil
)
489 (t (forward-char 1) t
)))))
490 ;; pad ws past bottom of screen
491 (let ((nl (- height
(count-lines (point-min) (point)))))
493 (setq line
(concat line
"\n"))
501 (defun zone-fall-through-ws (c wbeg wend
)
502 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
503 (spaces (make-string cw-ceil
32))
504 (col (current-column))
507 (while (when (save-excursion
509 (and (= col
(current-column))
510 (setq newpos
(point))
511 (string= spaces
(buffer-substring-no-properties
512 newpos
(+ newpos cw-ceil
)))
513 (setq newpos
(+ newpos
(1- cw-ceil
)))))
518 (when (< (point) wend
)
519 (delete-char cw-ceil
)
522 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
525 (defun zone-pgm-drip (&optional fret-p pancake-p
)
526 (let* ((ww (1- (window-width)))
532 (zone-fill-out-screen ww wh
)
533 (setq wbeg
(window-start)
536 (while (not (input-pending-p))
537 (setq mc
0 wend
(window-end))
538 ;; select non-ws character, but don't miss too much
539 (goto-char (+ wbeg
(random (- wend wbeg
))))
540 (while (looking-at "[ \n\f]")
541 (if (= total
(setq mc
(1+ mc
)))
543 (goto-char (+ wbeg
(random (- wend wbeg
))))))
544 ;; character animation sequence
546 (when fret-p
(zone-fret wbeg p
))
548 (setq c
(zone-cpos p
)
549 fall-p
(zone-fall-through-ws c wbeg wend
)))
550 ;; assuming current-column has not changed...
553 (< (count-lines (point-min) (point))
555 (let ((cw (ceiling (char-width (aref c
0)))))
556 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
557 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
558 (zone-replace-char cw nil c ?_
)))))))
560 (defun zone-pgm-drip-fretfully ()
563 (defun zone-pgm-five-oclock-swan-dive ()
564 (zone-pgm-drip nil t
))
566 (defun zone-pgm-martini-swan-dive ()
570 ;;;; paragraph spazzing (for textish modes)
572 (defun zone-pgm-paragraph-spaz ()
573 (if (memq (zone-orig major-mode
)
574 ;; there should be a better way to distinguish textish modes
575 '(text-mode texinfo-mode fundamental-mode
))
576 (let ((fill-column fill-column
)
579 (max-fc (1- (frame-width))))
582 (setq fill-column
(+ fill-column
(- (random 5) 2)))
583 (when (< fill-column fc-min
)
584 (setq fc-min fill-column
))
585 (when (> fill-column max-fc
)
586 (setq fill-column max-fc
))
587 (when (> fill-column fc-max
)
588 (setq fc-max fill-column
))))
589 (message "Zoning... (zone-pgm-rotate)")
593 ;;;; stressing and destressing
595 (defun zone-pgm-stress ()
596 (goto-char (point-min))
598 (while (< (point) (point-max))
601 (setq lines
(cons (buffer-substring p
(point)) lines
))))
603 (zone-hiding-modeline
604 (let ((msg "Zoning... (zone-pgm-stress)"))
605 (while (not (string= msg
""))
606 (message (setq msg
(substring msg
1)))
608 (while (not (input-pending-p))
609 (when (< 50 (random 100))
610 (goto-char (point-max))
612 (let ((kill-whole-line t
))
614 (goto-char (point-min))
615 (insert (nth (random (length lines
)) lines
)))
616 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
619 (defun zone-pgm-stress-destress ()
620 (zone-call 'zone-pgm-stress
25)
621 (zone-hiding-modeline
625 (insert-buffer "*Messages*")
627 (goto-char (point-max))
630 (delete-region (point-min) (window-start))
631 (message "hey why stress out anyway?")
632 (zone-call '((zone-pgm-rotate 30)
633 (zone-pgm-whack-chars 10)
637 ;;;; the lyfe so short the craft so long to lerne --chaucer
639 (defvar zone-pgm-random-life-wait nil
640 "*Seconds to wait between successive `life' generations.
641 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
643 (defun zone-pgm-random-life ()
645 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
646 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
647 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
648 (rtc (- (frame-width) 11))
650 (max (1- (window-end)))
652 (delete-region max
(point-max))
653 (while (and (progn (goto-char min
) (sit-for 0.05))
654 (progn (goto-char (+ min
(random max
)))
655 (or (progn (skip-chars-forward " @\n" max
)
656 (not (= max
(point))))
657 (unless (or (= 0 (skip-chars-backward " @\n" min
))
661 (unless (or (eolp) (eobp))
662 (setq s
(zone-cpos (point))
666 1 s
(cond ((or (> top
(point))
668 (or (> 11 (setq col
(current-column)))
671 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
672 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
677 (while (< top
(point))
680 (setq col
(cons (buffer-substring (point) c
) col
))
683 (let ((life-patterns (vector
684 (if (and col
(search-forward "@" max t
))
685 (cons (make-string (length (car col
)) 32) col
)
686 (list (mapconcat 'identity
687 (make-list (/ (- rtc
11) 15)
689 (make-string 10 32)))))))
690 (life (or zone-pgm-random-life-wait
(random 4)))
697 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
698 ;;; zone.el ends here