1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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
78 zone-pgm-paragraph-spaz
80 zone-pgm-stress-destress
84 (defmacro zone-orig
(&rest body
)
85 `(with-current-buffer (get 'zone
'orig-buffer
)
88 (defmacro zone-hiding-modeline
(&rest body
)
89 `(let (bg mode-line-fg mode-line-bg mode-line-box
)
92 (when (and (= 0 (get 'zone
'modeline-hidden-level
))
94 (setq bg
(face-background 'default
)
95 mode-line-box
(face-attribute 'mode-line
:box
)
96 mode-line-fg
(face-attribute 'mode-line
:foreground
)
97 mode-line-bg
(face-attribute 'mode-line
:background
))
98 (set-face-attribute 'mode-line nil
102 (put 'zone
'modeline-hidden-level
103 (1+ (get 'zone
'modeline-hidden-level
)))
105 (put 'zone
'modeline-hidden-level
106 (1- (get 'zone
'modeline-hidden-level
)))
107 (when (and (> 1 (get 'zone
'modeline-hidden-level
))
109 (set-face-attribute 'mode-line nil
110 :foreground mode-line-fg
111 :background mode-line-bg
112 :box mode-line-box
)))))
114 (defun zone-call (program &optional timeout
)
115 "Call PROGRAM in a zoned way.
116 If PROGRAM is a function, call it, interrupting after the amount
117 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
119 PROGRAM can also be a list of elements, which are interpreted like so:
120 If the element is a function or a list of a function and a number,
121 apply `zone-call' recursively."
122 (cond ((functionp program
)
123 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
126 (mapcar (lambda (elem)
127 (cond ((functionp elem
) (zone-call elem
))
129 (functionp (car elem
))
130 (numberp (cadr elem
)))
131 (apply 'zone-call elem
))
132 (t (error "bad `zone-call' elem: %S" elem
))))
137 "Zone out, completely."
139 (save-window-excursion
140 (let ((f (selected-frame))
141 (outbuf (get-buffer-create "*zone*"))
142 (text (buffer-substring (window-start) (window-end)))
143 (wp (1+ (- (window-point (selected-window))
145 (put 'zone
'orig-buffer
(current-buffer))
146 (put 'zone
'modeline-hidden-level
0)
147 (switch-to-buffer outbuf
)
148 (setq mode-name
"Zone")
150 (setq buffer-undo-list t
152 tab-width
(zone-orig tab-width
)
153 line-spacing
(zone-orig line-spacing
))
155 (untabify (point-min) (point-max))
156 (set-window-start (selected-window) (point-min))
157 (set-window-point (selected-window) wp
)
159 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
160 (ct (and f
(frame-parameter f
'cursor-type
)))
161 (restore (list '(kill-buffer outbuf
))))
163 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
164 (setq restore
(cons '(modify-frame-parameters
165 f
(list (cons 'cursor-type ct
)))
167 ;; Make `restore' a self-disabling one-shot thunk.
168 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
171 (message "Zoning... (%s)" pgm
)
173 ;; If some input is pending, zone says "sorry", which
174 ;; isn't nice; this might happen e.g. when they invoke the
175 ;; game by clicking the menu bar. So discard any pending
176 ;; input before zoning out.
177 (if (input-pending-p)
180 (message "Zoning...sorry"))
183 (while (not (input-pending-p))
184 (message "We were zoning when we wrote %s..." pgm
)
186 (message "...here's hoping we didn't hose your buffer!")
191 (message "Zoning...sorry")))
192 (when restore
(funcall restore
))))))
194 ;;;; Zone when idle, or not.
196 (defun zone-when-idle (secs)
197 "Zone out when Emacs has been idle for SECS seconds."
198 (interactive "nHow long before I start zoning (seconds): ")
199 (if (timerp zone-timer
)
200 (cancel-timer zone-timer
))
201 (setq zone-timer nil
)
203 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
205 (defun zone-leave-me-alone ()
206 "Don't zone out when Emacs is idle."
208 (if (timerp zone-timer
)
209 (cancel-timer zone-timer
))
210 (setq zone-timer nil
)
211 (message "I won't zone out any more"))
216 (defun zone-shift-up ()
218 (e (progn (forward-line 1) (point)))
219 (s (buffer-substring b e
)))
221 (goto-char (point-max))
224 (defun zone-shift-down ()
225 (goto-char (point-max))
227 (e (progn (forward-line -
1) (point)))
228 (s (buffer-substring b e
)))
230 (goto-char (point-min))
233 (defun zone-shift-left ()
237 (setq s
(buffer-substring (point) (1+ (point))))
243 (defun zone-shift-right ()
244 (goto-char (point-max))
249 (setq s
(buffer-substring (1- (point)) (point)))
255 (defun zone-pgm-jitter ()
262 (goto-char (point-min))
263 (while (not (input-pending-p))
264 (funcall (elt ops
(random (length ops
))))
265 (goto-char (point-min))
271 (defun zone-pgm-whack-chars ()
272 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
273 (while (not (input-pending-p))
276 (aset tbl i
(+ 48 (random (- 123 48))))
278 (translate-region (point-min) (point-max) tbl
)
281 (put 'zone-pgm-whack-chars
'wc-tbl
282 (let ((tbl (make-string 128 ?x
))
291 (defun zone-remove-text ()
296 (goto-char (point-min))
298 (if (looking-at "[^(){}\n\t ]")
299 (let ((n (random 5)))
309 (defun zone-pgm-dissolve ()
316 (defun zone-exploding-remove ()
320 (goto-char (point-min))
322 (if (looking-at "[^*\n\t ]")
323 (let ((n (random 5)))
332 (defun zone-pgm-explode ()
333 (zone-exploding-remove)
339 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
340 ;; instances of the same letter have the same case, which produces a
341 ;; less interesting effect than you might imagine.
342 (defun zone-pgm-2nd-putz-with-case ()
343 (let ((tbl (make-string 128 ?x
))
348 (while (not (input-pending-p))
352 (if (zerop (random 5))
355 (setq i
(+ i
(1+ (random 5)))))
359 (if (zerop (random 5))
362 (setq i
(+ i
(1+ (random 5)))))
363 (translate-region (point-min) (point-max) tbl
)
366 (defun zone-pgm-putz-with-case ()
367 (goto-char (point-min))
368 (while (not (input-pending-p))
369 (let ((np (+ 2 (random 5)))
373 (let ((prec (preceding-char))
374 (props (text-properties-at (1- (point)))))
375 (insert (if (zerop (random 2))
378 (set-text-properties (1- (point)) (point) props
))
381 (setq np
(+ np
(1+ (random 5))))))
382 (goto-char (point-min))
388 (defun zone-line-specs ()
391 (goto-char (window-start))
392 (while (< (point) (window-end))
393 (when (looking-at "[\t ]*\\([^\n]+\\)")
394 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
398 (defun zone-pgm-rotate (&optional random-style
)
402 (mapcar (lambda (ent)
403 (let* ((beg (car ent
))
405 (amt (if random-style
406 (funcall random-style
)
408 (when (< (- end
(abs amt
)) beg
)
409 (setq amt
(random (- end beg
))))
413 (vector amt beg
(- end
(abs amt
)))
418 amt aamt cut paste txt i ent
)
419 (while (not (input-pending-p))
422 (setq ent
(aref specs i
))
423 (setq amt
(aref ent
0) aamt
(abs amt
))
426 (setq cut
2 paste
1))
427 (goto-char (aref ent cut
))
428 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
430 (goto-char (aref ent paste
))
435 (defun zone-pgm-rotate-LR-lockstep ()
436 (zone-pgm-rotate (lambda () 1)))
438 (defun zone-pgm-rotate-RL-lockstep ()
439 (zone-pgm-rotate (lambda () -
1)))
441 (defun zone-pgm-rotate-LR-variable ()
442 (zone-pgm-rotate (lambda () (1+ (random 3)))))
444 (defun zone-pgm-rotate-RL-variable ()
445 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
450 (defsubst zone-cpos
(pos)
451 (buffer-substring pos
(1+ pos
)))
453 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
454 (delete-char (or del-count
(- count
)))
455 (aset char-as-string
0 new-value
)
456 (dotimes (i count
) (insert char-as-string
)))
458 (defsubst zone-park
/sit-for
(pos seconds
)
461 (prog1 (sit-for seconds
)
464 (defun zone-fret (wbeg pos
)
465 (let* ((case-fold-search nil
)
466 (c-string (zone-cpos pos
))
467 (cw-ceil (ceiling (char-width (aref c-string
0))))
469 ((string-match "[a-z]" c-string
) (upcase c-string
))
470 ((string-match "[A-Z]" c-string
) (downcase c-string
))
471 (t (propertize " " 'display
`(space :width
,cw-ceil
))))))
473 (wait 0.5 (* wait
0.8)))
477 (insert (if (= 0 (% i
2)) hmm c-string
))
478 (zone-park/sit-for wbeg wait
))
479 (delete-char -
1) (insert c-string
)))
481 (defun zone-fill-out-screen (width height
)
482 (let ((start (window-start))
483 (line (make-string width
32)))
485 ;; fill out rectangular ws block
486 (while (progn (end-of-line)
487 (let ((cc (current-column)))
489 (insert (substring line cc
))
490 (delete-char (- width cc
)))
491 (cond ((eobp) (insert "\n") nil
)
492 (t (forward-char 1) t
)))))
493 ;; pad ws past bottom of screen
494 (let ((nl (- height
(count-lines (point-min) (point)))))
496 (setq line
(concat line
"\n"))
504 (defun zone-fall-through-ws (c wbeg wend
)
505 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
506 (spaces (make-string cw-ceil
32))
507 (col (current-column))
510 (while (when (save-excursion
512 (and (= col
(current-column))
513 (setq newpos
(point))
514 (string= spaces
(buffer-substring-no-properties
515 newpos
(+ newpos cw-ceil
)))
516 (setq newpos
(+ newpos
(1- cw-ceil
)))))
521 (when (< (point) wend
)
522 (delete-char cw-ceil
)
525 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
528 (defun zone-pgm-drip (&optional fret-p pancake-p
)
529 (let* ((ww (1- (window-width)))
535 (zone-fill-out-screen ww wh
)
536 (setq wbeg
(window-start)
539 (while (not (input-pending-p))
540 (setq mc
0 wend
(window-end))
541 ;; select non-ws character, but don't miss too much
542 (goto-char (+ wbeg
(random (- wend wbeg
))))
543 (while (looking-at "[ \n\f]")
544 (if (= total
(setq mc
(1+ mc
)))
546 (goto-char (+ wbeg
(random (- wend wbeg
))))))
547 ;; character animation sequence
549 (when fret-p
(zone-fret wbeg p
))
551 (setq c
(zone-cpos p
)
552 fall-p
(zone-fall-through-ws c wbeg wend
)))
553 ;; assuming current-column has not changed...
556 (< (count-lines (point-min) (point))
558 (let ((cw (ceiling (char-width (aref c
0)))))
559 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
560 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
561 (zone-replace-char cw nil c ?_
)))))))
563 (defun zone-pgm-drip-fretfully ()
566 (defun zone-pgm-five-oclock-swan-dive ()
567 (zone-pgm-drip nil t
))
569 (defun zone-pgm-martini-swan-dive ()
572 (defun zone-pgm-rat-race ()
573 (while (not (input-pending-p))
574 (zone-call '((zone-pgm-rotate 10)
575 (zone-pgm-drip-fretfully 15)
578 (goto-char (point-min))
579 (while (re-search-forward " +$" nil t
)
580 (delete-region (match-beginning 0) (match-end 0))))
584 ;;;; paragraph spazzing (for textish modes)
586 (defun zone-pgm-paragraph-spaz ()
587 (if (memq (zone-orig major-mode
)
588 ;; there should be a better way to distinguish textish modes
589 '(text-mode texinfo-mode fundamental-mode
))
590 (let ((fill-column fill-column
)
593 (max-fc (1- (frame-width))))
596 (setq fill-column
(+ fill-column
(- (random 5) 2)))
597 (when (< fill-column fc-min
)
598 (setq fc-min fill-column
))
599 (when (> fill-column max-fc
)
600 (setq fill-column max-fc
))
601 (when (> fill-column fc-max
)
602 (setq fc-max fill-column
))))
603 (message "Zoning... (zone-pgm-rotate)")
607 ;;;; stressing and destressing
609 (defun zone-pgm-stress ()
610 (goto-char (point-min))
612 (while (< (point) (point-max))
615 (setq lines
(cons (buffer-substring p
(point)) lines
))))
617 (zone-hiding-modeline
618 (let ((msg "Zoning... (zone-pgm-stress)"))
619 (while (not (string= msg
""))
620 (message (setq msg
(substring msg
1)))
622 (while (not (input-pending-p))
623 (when (< 50 (random 100))
624 (goto-char (point-max))
626 (let ((kill-whole-line t
))
628 (goto-char (point-min))
629 (insert (nth (random (length lines
)) lines
)))
630 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
633 (defun zone-pgm-stress-destress ()
634 (zone-call 'zone-pgm-stress
25)
635 (zone-hiding-modeline
639 (insert-buffer-substring "*Messages*")
641 (goto-char (point-max))
644 (delete-region (point-min) (window-start))
645 (message "hey why stress out anyway?")
646 (zone-call '((zone-pgm-rotate 30)
647 (zone-pgm-whack-chars 10)
651 ;;;; the lyfe so short the craft so long to lerne --chaucer
653 (defvar zone-pgm-random-life-wait nil
654 "*Seconds to wait between successive `life' generations.
655 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
657 (defun zone-pgm-random-life ()
659 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
660 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
661 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
662 (rtc (- (frame-width) 11))
664 (max (1- (window-end)))
666 (delete-region max
(point-max))
667 (while (and (progn (goto-char min
) (sit-for 0.05))
668 (progn (goto-char (+ min
(random max
)))
669 (or (progn (skip-chars-forward " @\n" max
)
670 (not (= max
(point))))
671 (unless (or (= 0 (skip-chars-backward " @\n" min
))
675 (unless (or (eolp) (eobp))
676 (setq s
(zone-cpos (point))
680 1 s
(cond ((or (> top
(point))
682 (or (> 11 (setq col
(current-column)))
685 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
686 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
691 (while (< top
(point))
694 (setq col
(cons (buffer-substring (point) c
) col
))
697 (let ((life-patterns (vector
698 (if (and col
(search-forward "@" max t
))
699 (cons (make-string (length (car col
)) 32) col
)
700 (list (mapconcat 'identity
701 (make-list (/ (- rtc
11) 15)
703 (make-string 10 32)))))))
704 (life (or zone-pgm-random-life-wait
(random 4)))
712 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
713 ;;; zone.el ends here