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 ;; This formerly worked by temporarily altering face `mode-line',
90 ;; which did not even work right, it seems.
91 `(let (mode-line-format)
94 (defun zone-call (program &optional timeout
)
95 "Call PROGRAM in a zoned way.
96 If PROGRAM is a function, call it, interrupting after the amount
97 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
99 PROGRAM can also be a list of elements, which are interpreted like so:
100 If the element is a function or a list of a function and a number,
101 apply `zone-call' recursively."
102 (cond ((functionp program
)
103 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
106 (mapcar (lambda (elem)
107 (cond ((functionp elem
) (zone-call elem
))
109 (functionp (car elem
))
110 (numberp (cadr elem
)))
111 (apply 'zone-call elem
))
112 (t (error "bad `zone-call' elem: %S" elem
))))
117 "Zone out, completely."
119 (save-window-excursion
120 (let ((f (selected-frame))
121 (outbuf (get-buffer-create "*zone*"))
122 (text (buffer-substring (window-start) (window-end)))
123 (wp (1+ (- (window-point (selected-window))
125 (put 'zone
'orig-buffer
(current-buffer))
126 (put 'zone
'modeline-hidden-level
0)
127 (switch-to-buffer outbuf
)
128 (setq mode-name
"Zone")
130 (setq buffer-undo-list t
132 tab-width
(zone-orig tab-width
)
133 line-spacing
(zone-orig line-spacing
))
135 (untabify (point-min) (point-max))
136 (set-window-start (selected-window) (point-min))
137 (set-window-point (selected-window) wp
)
139 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
140 (ct (and f
(frame-parameter f
'cursor-type
)))
141 (show-trailing-whitespace nil
)
142 (restore (list '(kill-buffer outbuf
))))
144 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
145 (setq restore
(cons '(modify-frame-parameters
146 f
(list (cons 'cursor-type ct
)))
148 ;; Make `restore' a self-disabling one-shot thunk.
149 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
152 (message "Zoning... (%s)" pgm
)
154 ;; If some input is pending, zone says "sorry", which
155 ;; isn't nice; this might happen e.g. when they invoke the
156 ;; game by clicking the menu bar. So discard any pending
157 ;; input before zoning out.
158 (if (input-pending-p)
161 (message "Zoning...sorry"))
164 (while (not (input-pending-p))
165 (message "We were zoning when we wrote %s..." pgm
)
167 (message "...here's hoping we didn't hose your buffer!")
172 (message "Zoning...sorry")))
173 (when restore
(funcall restore
))))))
175 ;;;; Zone when idle, or not.
177 (defun zone-when-idle (secs)
178 "Zone out when Emacs has been idle for SECS seconds."
179 (interactive "nHow long before I start zoning (seconds): ")
180 (if (timerp zone-timer
)
181 (cancel-timer zone-timer
))
182 (setq zone-timer nil
)
184 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
186 (defun zone-leave-me-alone ()
187 "Don't zone out when Emacs is idle."
189 (if (timerp zone-timer
)
190 (cancel-timer zone-timer
))
191 (setq zone-timer nil
)
192 (message "I won't zone out any more"))
197 (defun zone-shift-up ()
199 (e (progn (forward-line 1) (point)))
200 (s (buffer-substring b e
)))
202 (goto-char (point-max))
205 (defun zone-shift-down ()
206 (goto-char (point-max))
208 (e (progn (forward-line -
1) (point)))
209 (s (buffer-substring b e
)))
211 (goto-char (point-min))
214 (defun zone-shift-left ()
218 (setq s
(buffer-substring (point) (1+ (point))))
224 (defun zone-shift-right ()
225 (goto-char (point-max))
230 (setq s
(buffer-substring (1- (point)) (point)))
236 (defun zone-pgm-jitter ()
243 (goto-char (point-min))
244 (while (not (input-pending-p))
245 (funcall (elt ops
(random (length ops
))))
246 (goto-char (point-min))
252 (defun zone-pgm-whack-chars ()
253 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
254 (while (not (input-pending-p))
257 (aset tbl i
(+ 48 (random (- 123 48))))
259 (translate-region (point-min) (point-max) tbl
)
262 (put 'zone-pgm-whack-chars
'wc-tbl
263 (let ((tbl (make-string 128 ?x
))
272 (defun zone-remove-text ()
277 (goto-char (point-min))
279 (if (looking-at "[^(){}\n\t ]")
280 (let ((n (random 5)))
290 (defun zone-pgm-dissolve ()
297 (defun zone-exploding-remove ()
301 (goto-char (point-min))
303 (if (looking-at "[^*\n\t ]")
304 (let ((n (random 5)))
313 (defun zone-pgm-explode ()
314 (zone-exploding-remove)
320 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
321 ;; instances of the same letter have the same case, which produces a
322 ;; less interesting effect than you might imagine.
323 (defun zone-pgm-2nd-putz-with-case ()
324 (let ((tbl (make-string 128 ?x
))
329 (while (not (input-pending-p))
333 (if (zerop (random 5))
336 (setq i
(+ i
(1+ (random 5)))))
340 (if (zerop (random 5))
343 (setq i
(+ i
(1+ (random 5)))))
344 (translate-region (point-min) (point-max) tbl
)
347 (defun zone-pgm-putz-with-case ()
348 (goto-char (point-min))
349 (while (not (input-pending-p))
350 (let ((np (+ 2 (random 5)))
354 (let ((prec (preceding-char))
355 (props (text-properties-at (1- (point)))))
356 (insert (if (zerop (random 2))
359 (set-text-properties (1- (point)) (point) props
))
362 (setq np
(+ np
(1+ (random 5))))))
363 (goto-char (point-min))
369 (defun zone-line-specs ()
372 (goto-char (window-start))
373 (while (< (point) (window-end))
374 (when (looking-at "[\t ]*\\([^\n]+\\)")
375 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
379 (defun zone-pgm-rotate (&optional random-style
)
384 (let* ((beg (car ent
))
386 (amt (if random-style
387 (funcall random-style
)
389 (when (< (- end
(abs amt
)) beg
)
390 (setq amt
(random (- end beg
))))
394 (vector amt beg
(- end
(abs amt
)))
399 amt aamt cut paste txt i ent
)
400 (while (not (input-pending-p))
403 (setq ent
(aref specs i
))
404 (setq amt
(aref ent
0) aamt
(abs amt
))
407 (setq cut
2 paste
1))
408 (goto-char (aref ent cut
))
409 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
411 (goto-char (aref ent paste
))
416 (defun zone-pgm-rotate-LR-lockstep ()
417 (zone-pgm-rotate (lambda () 1)))
419 (defun zone-pgm-rotate-RL-lockstep ()
420 (zone-pgm-rotate (lambda () -
1)))
422 (defun zone-pgm-rotate-LR-variable ()
423 (zone-pgm-rotate (lambda () (1+ (random 3)))))
425 (defun zone-pgm-rotate-RL-variable ()
426 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
431 (defsubst zone-cpos
(pos)
432 (buffer-substring pos
(1+ pos
)))
434 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
435 (delete-char (or del-count
(- count
)))
436 (aset char-as-string
0 new-value
)
437 (dotimes (i count
) (insert char-as-string
)))
439 (defsubst zone-park
/sit-for
(pos seconds
)
442 (prog1 (sit-for seconds
)
445 (defun zone-fret (wbeg pos
)
446 (let* ((case-fold-search nil
)
447 (c-string (zone-cpos pos
))
448 (cw-ceil (ceiling (char-width (aref c-string
0))))
450 ((string-match "[a-z]" c-string
) (upcase c-string
))
451 ((string-match "[A-Z]" c-string
) (downcase c-string
))
452 (t (propertize " " 'display
`(space :width
,cw-ceil
))))))
454 (wait 0.5 (* wait
0.8)))
458 (insert (if (= 0 (% i
2)) hmm c-string
))
459 (zone-park/sit-for wbeg wait
))
460 (delete-char -
1) (insert c-string
)))
462 (defun zone-fill-out-screen (width height
)
463 (let ((start (window-start))
464 (line (make-string width
32)))
466 ;; fill out rectangular ws block
467 (while (progn (end-of-line)
468 (let ((cc (current-column)))
470 (insert (substring line cc
))
471 (delete-char (- width cc
)))
472 (cond ((eobp) (insert "\n") nil
)
473 (t (forward-char 1) t
)))))
474 ;; pad ws past bottom of screen
475 (let ((nl (- height
(count-lines (point-min) (point)))))
477 (setq line
(concat line
"\n"))
485 (defun zone-fall-through-ws (c wbeg wend
)
486 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
487 (spaces (make-string cw-ceil
32))
488 (col (current-column))
491 (while (when (save-excursion
493 (and (= col
(current-column))
494 (setq newpos
(point))
495 (string= spaces
(buffer-substring-no-properties
496 newpos
(+ newpos cw-ceil
)))
497 (setq newpos
(+ newpos
(1- cw-ceil
)))))
502 (when (< (point) wend
)
503 (delete-char cw-ceil
)
506 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
509 (defun zone-pgm-drip (&optional fret-p pancake-p
)
510 (let* ((ww (1- (window-width)))
516 (zone-fill-out-screen ww wh
)
517 (setq wbeg
(window-start)
520 (while (not (input-pending-p))
521 (setq mc
0 wend
(window-end))
522 ;; select non-ws character, but don't miss too much
523 (goto-char (+ wbeg
(random (- wend wbeg
))))
524 (while (looking-at "[ \n\f]")
525 (if (= total
(setq mc
(1+ mc
)))
527 (goto-char (+ wbeg
(random (- wend wbeg
))))))
528 ;; character animation sequence
530 (when fret-p
(zone-fret wbeg p
))
532 (setq c
(zone-cpos p
)
533 fall-p
(zone-fall-through-ws c wbeg wend
)))
534 ;; assuming current-column has not changed...
537 (< (count-lines (point-min) (point))
539 (let ((cw (ceiling (char-width (aref c
0)))))
540 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
541 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
542 (zone-replace-char cw nil c ?_
)))))))
544 (defun zone-pgm-drip-fretfully ()
547 (defun zone-pgm-five-oclock-swan-dive ()
548 (zone-pgm-drip nil t
))
550 (defun zone-pgm-martini-swan-dive ()
553 (defun zone-pgm-rat-race ()
554 (while (not (input-pending-p))
555 (zone-call '((zone-pgm-rotate 10)
556 (zone-pgm-drip-fretfully 15)
559 (goto-char (point-min))
560 (while (re-search-forward " +$" nil t
)
561 (delete-region (match-beginning 0) (match-end 0))))
565 ;;;; paragraph spazzing (for textish modes)
567 (defun zone-pgm-paragraph-spaz ()
568 (if (memq (zone-orig major-mode
)
569 ;; there should be a better way to distinguish textish modes
570 '(text-mode texinfo-mode fundamental-mode
))
571 (let ((fill-column fill-column
)
574 (max-fc (1- (frame-width))))
577 (setq fill-column
(+ fill-column
(- (random 5) 2)))
578 (when (< fill-column fc-min
)
579 (setq fc-min fill-column
))
580 (when (> fill-column max-fc
)
581 (setq fill-column max-fc
))
582 (when (> fill-column fc-max
)
583 (setq fc-max fill-column
))))
584 (message "Zoning... (zone-pgm-rotate)")
588 ;;;; stressing and destressing
590 (defun zone-pgm-stress ()
591 (goto-char (point-min))
593 (while (< (point) (point-max))
596 (setq lines
(cons (buffer-substring p
(point)) lines
))))
598 (zone-hiding-modeline
599 (let ((msg "Zoning... (zone-pgm-stress)"))
600 (while (not (string= msg
""))
601 (message (setq msg
(substring msg
1)))
603 (while (not (input-pending-p))
604 (when (< 50 (random 100))
605 (goto-char (point-max))
607 (let ((kill-whole-line t
))
609 (goto-char (point-min))
610 (insert (nth (random (length lines
)) lines
)))
611 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
614 (defun zone-pgm-stress-destress ()
615 (zone-call 'zone-pgm-stress
25)
616 (zone-hiding-modeline
620 (insert-buffer-substring "*Messages*")
622 (goto-char (point-max))
625 (delete-region (point-min) (window-start))
626 (message "hey why stress out anyway?")
627 (zone-call '((zone-pgm-rotate 30)
628 (zone-pgm-whack-chars 10)
632 ;;;; the lyfe so short the craft so long to lerne --chaucer
634 (defvar zone-pgm-random-life-wait nil
635 "*Seconds to wait between successive `life' generations.
636 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
638 (defun zone-pgm-random-life ()
640 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
641 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
642 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
643 (rtc (- (frame-width) 11))
645 (max (1- (window-end)))
647 (delete-region max
(point-max))
648 (while (and (progn (goto-char min
) (sit-for 0.05))
649 (progn (goto-char (+ min
(random max
)))
650 (or (progn (skip-chars-forward " @\n" max
)
651 (not (= max
(point))))
652 (unless (or (= 0 (skip-chars-backward " @\n" min
))
656 (unless (or (eolp) (eobp))
657 (setq s
(zone-cpos (point))
661 1 s
(cond ((or (> top
(point))
663 (or (> 11 (setq col
(current-column)))
666 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
667 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
672 (while (< top
(point))
675 (setq col
(cons (buffer-substring (point) c
) col
))
678 (let ((life-patterns (vector
679 (if (and col
(search-forward "@" max t
))
680 (cons (make-string (length (car col
)) 32) col
)
681 (list (mapconcat 'identity
682 (make-list (/ (- rtc
11) 15)
684 (make-string 10 32)))))))
685 (life (or zone-pgm-random-life-wait
(random 4)))
694 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
695 ;;; zone.el ends here