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 (restore (list '(kill-buffer outbuf
))))
143 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
144 (setq restore
(cons '(modify-frame-parameters
145 f
(list (cons 'cursor-type ct
)))
147 ;; Make `restore' a self-disabling one-shot thunk.
148 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
151 (message "Zoning... (%s)" pgm
)
153 ;; If some input is pending, zone says "sorry", which
154 ;; isn't nice; this might happen e.g. when they invoke the
155 ;; game by clicking the menu bar. So discard any pending
156 ;; input before zoning out.
157 (if (input-pending-p)
160 (message "Zoning...sorry"))
163 (while (not (input-pending-p))
164 (message "We were zoning when we wrote %s..." pgm
)
166 (message "...here's hoping we didn't hose your buffer!")
171 (message "Zoning...sorry")))
172 (when restore
(funcall restore
))))))
174 ;;;; Zone when idle, or not.
176 (defun zone-when-idle (secs)
177 "Zone out when Emacs has been idle for SECS seconds."
178 (interactive "nHow long before I start zoning (seconds): ")
179 (if (timerp zone-timer
)
180 (cancel-timer zone-timer
))
181 (setq zone-timer nil
)
183 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
185 (defun zone-leave-me-alone ()
186 "Don't zone out when Emacs is idle."
188 (if (timerp zone-timer
)
189 (cancel-timer zone-timer
))
190 (setq zone-timer nil
)
191 (message "I won't zone out any more"))
196 (defun zone-shift-up ()
198 (e (progn (forward-line 1) (point)))
199 (s (buffer-substring b e
)))
201 (goto-char (point-max))
204 (defun zone-shift-down ()
205 (goto-char (point-max))
207 (e (progn (forward-line -
1) (point)))
208 (s (buffer-substring b e
)))
210 (goto-char (point-min))
213 (defun zone-shift-left ()
217 (setq s
(buffer-substring (point) (1+ (point))))
223 (defun zone-shift-right ()
224 (goto-char (point-max))
229 (setq s
(buffer-substring (1- (point)) (point)))
235 (defun zone-pgm-jitter ()
242 (goto-char (point-min))
243 (while (not (input-pending-p))
244 (funcall (elt ops
(random (length ops
))))
245 (goto-char (point-min))
251 (defun zone-pgm-whack-chars ()
252 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
253 (while (not (input-pending-p))
256 (aset tbl i
(+ 48 (random (- 123 48))))
258 (translate-region (point-min) (point-max) tbl
)
261 (put 'zone-pgm-whack-chars
'wc-tbl
262 (let ((tbl (make-string 128 ?x
))
271 (defun zone-remove-text ()
276 (goto-char (point-min))
278 (if (looking-at "[^(){}\n\t ]")
279 (let ((n (random 5)))
289 (defun zone-pgm-dissolve ()
296 (defun zone-exploding-remove ()
300 (goto-char (point-min))
302 (if (looking-at "[^*\n\t ]")
303 (let ((n (random 5)))
312 (defun zone-pgm-explode ()
313 (zone-exploding-remove)
319 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
320 ;; instances of the same letter have the same case, which produces a
321 ;; less interesting effect than you might imagine.
322 (defun zone-pgm-2nd-putz-with-case ()
323 (let ((tbl (make-string 128 ?x
))
328 (while (not (input-pending-p))
332 (if (zerop (random 5))
335 (setq i
(+ i
(1+ (random 5)))))
339 (if (zerop (random 5))
342 (setq i
(+ i
(1+ (random 5)))))
343 (translate-region (point-min) (point-max) tbl
)
346 (defun zone-pgm-putz-with-case ()
347 (goto-char (point-min))
348 (while (not (input-pending-p))
349 (let ((np (+ 2 (random 5)))
353 (let ((prec (preceding-char))
354 (props (text-properties-at (1- (point)))))
355 (insert (if (zerop (random 2))
358 (set-text-properties (1- (point)) (point) props
))
361 (setq np
(+ np
(1+ (random 5))))))
362 (goto-char (point-min))
368 (defun zone-line-specs ()
371 (goto-char (window-start))
372 (while (< (point) (window-end))
373 (when (looking-at "[\t ]*\\([^\n]+\\)")
374 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
378 (defun zone-pgm-rotate (&optional random-style
)
383 (let* ((beg (car ent
))
385 (amt (if random-style
386 (funcall random-style
)
388 (when (< (- end
(abs amt
)) beg
)
389 (setq amt
(random (- end beg
))))
393 (vector amt beg
(- end
(abs amt
)))
398 amt aamt cut paste txt i ent
)
399 (while (not (input-pending-p))
402 (setq ent
(aref specs i
))
403 (setq amt
(aref ent
0) aamt
(abs amt
))
406 (setq cut
2 paste
1))
407 (goto-char (aref ent cut
))
408 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
410 (goto-char (aref ent paste
))
415 (defun zone-pgm-rotate-LR-lockstep ()
416 (zone-pgm-rotate (lambda () 1)))
418 (defun zone-pgm-rotate-RL-lockstep ()
419 (zone-pgm-rotate (lambda () -
1)))
421 (defun zone-pgm-rotate-LR-variable ()
422 (zone-pgm-rotate (lambda () (1+ (random 3)))))
424 (defun zone-pgm-rotate-RL-variable ()
425 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
430 (defsubst zone-cpos
(pos)
431 (buffer-substring pos
(1+ pos
)))
433 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
434 (delete-char (or del-count
(- count
)))
435 (aset char-as-string
0 new-value
)
436 (dotimes (i count
) (insert char-as-string
)))
438 (defsubst zone-park
/sit-for
(pos seconds
)
441 (prog1 (sit-for seconds
)
444 (defun zone-fret (wbeg pos
)
445 (let* ((case-fold-search nil
)
446 (c-string (zone-cpos pos
))
447 (cw-ceil (ceiling (char-width (aref c-string
0))))
449 ((string-match "[a-z]" c-string
) (upcase c-string
))
450 ((string-match "[A-Z]" c-string
) (downcase c-string
))
451 (t (propertize " " 'display
`(space :width
,cw-ceil
))))))
453 (wait 0.5 (* wait
0.8)))
457 (insert (if (= 0 (% i
2)) hmm c-string
))
458 (zone-park/sit-for wbeg wait
))
459 (delete-char -
1) (insert c-string
)))
461 (defun zone-fill-out-screen (width height
)
462 (let ((start (window-start))
463 (line (make-string width
32)))
465 ;; fill out rectangular ws block
466 (while (progn (end-of-line)
467 (let ((cc (current-column)))
469 (insert (substring line cc
))
470 (delete-char (- width cc
)))
471 (cond ((eobp) (insert "\n") nil
)
472 (t (forward-char 1) t
)))))
473 ;; pad ws past bottom of screen
474 (let ((nl (- height
(count-lines (point-min) (point)))))
476 (setq line
(concat line
"\n"))
484 (defun zone-fall-through-ws (c wbeg wend
)
485 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
486 (spaces (make-string cw-ceil
32))
487 (col (current-column))
490 (while (when (save-excursion
492 (and (= col
(current-column))
493 (setq newpos
(point))
494 (string= spaces
(buffer-substring-no-properties
495 newpos
(+ newpos cw-ceil
)))
496 (setq newpos
(+ newpos
(1- cw-ceil
)))))
501 (when (< (point) wend
)
502 (delete-char cw-ceil
)
505 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
508 (defun zone-pgm-drip (&optional fret-p pancake-p
)
509 (let* ((ww (1- (window-width)))
515 (zone-fill-out-screen ww wh
)
516 (setq wbeg
(window-start)
519 (while (not (input-pending-p))
520 (setq mc
0 wend
(window-end))
521 ;; select non-ws character, but don't miss too much
522 (goto-char (+ wbeg
(random (- wend wbeg
))))
523 (while (looking-at "[ \n\f]")
524 (if (= total
(setq mc
(1+ mc
)))
526 (goto-char (+ wbeg
(random (- wend wbeg
))))))
527 ;; character animation sequence
529 (when fret-p
(zone-fret wbeg p
))
531 (setq c
(zone-cpos p
)
532 fall-p
(zone-fall-through-ws c wbeg wend
)))
533 ;; assuming current-column has not changed...
536 (< (count-lines (point-min) (point))
538 (let ((cw (ceiling (char-width (aref c
0)))))
539 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
540 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
541 (zone-replace-char cw nil c ?_
)))))))
543 (defun zone-pgm-drip-fretfully ()
546 (defun zone-pgm-five-oclock-swan-dive ()
547 (zone-pgm-drip nil t
))
549 (defun zone-pgm-martini-swan-dive ()
552 (defun zone-pgm-rat-race ()
553 (while (not (input-pending-p))
554 (zone-call '((zone-pgm-rotate 10)
555 (zone-pgm-drip-fretfully 15)
558 (goto-char (point-min))
559 (while (re-search-forward " +$" nil t
)
560 (delete-region (match-beginning 0) (match-end 0))))
564 ;;;; paragraph spazzing (for textish modes)
566 (defun zone-pgm-paragraph-spaz ()
567 (if (memq (zone-orig major-mode
)
568 ;; there should be a better way to distinguish textish modes
569 '(text-mode texinfo-mode fundamental-mode
))
570 (let ((fill-column fill-column
)
573 (max-fc (1- (frame-width))))
576 (setq fill-column
(+ fill-column
(- (random 5) 2)))
577 (when (< fill-column fc-min
)
578 (setq fc-min fill-column
))
579 (when (> fill-column max-fc
)
580 (setq fill-column max-fc
))
581 (when (> fill-column fc-max
)
582 (setq fc-max fill-column
))))
583 (message "Zoning... (zone-pgm-rotate)")
587 ;;;; stressing and destressing
589 (defun zone-pgm-stress ()
590 (goto-char (point-min))
592 (while (< (point) (point-max))
595 (setq lines
(cons (buffer-substring p
(point)) lines
))))
597 (zone-hiding-modeline
598 (let ((msg "Zoning... (zone-pgm-stress)"))
599 (while (not (string= msg
""))
600 (message (setq msg
(substring msg
1)))
602 (while (not (input-pending-p))
603 (when (< 50 (random 100))
604 (goto-char (point-max))
606 (let ((kill-whole-line t
))
608 (goto-char (point-min))
609 (insert (nth (random (length lines
)) lines
)))
610 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
613 (defun zone-pgm-stress-destress ()
614 (zone-call 'zone-pgm-stress
25)
615 (zone-hiding-modeline
619 (insert-buffer-substring "*Messages*")
621 (goto-char (point-max))
624 (delete-region (point-min) (window-start))
625 (message "hey why stress out anyway?")
626 (zone-call '((zone-pgm-rotate 30)
627 (zone-pgm-whack-chars 10)
631 ;;;; the lyfe so short the craft so long to lerne --chaucer
633 (defvar zone-pgm-random-life-wait nil
634 "*Seconds to wait between successive `life' generations.
635 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
637 (defun zone-pgm-random-life ()
639 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
640 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
641 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
642 (rtc (- (frame-width) 11))
644 (max (1- (window-end)))
646 (delete-region max
(point-max))
647 (while (and (progn (goto-char min
) (sit-for 0.05))
648 (progn (goto-char (+ min
(random max
)))
649 (or (progn (skip-chars-forward " @\n" max
)
650 (not (= max
(point))))
651 (unless (or (= 0 (skip-chars-backward " @\n" min
))
655 (unless (or (eolp) (eobp))
656 (setq s
(zone-cpos (point))
660 1 s
(cond ((or (> top
(point))
662 (or (> 11 (setq col
(current-column)))
665 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
666 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
671 (while (< top
(point))
674 (setq col
(cons (buffer-substring (point) c
) col
))
677 (let ((life-patterns (vector
678 (if (and col
(search-forward "@" max t
))
679 (cons (make-string (length (car col
)) 32) col
)
680 (list (mapconcat 'identity
681 (make-list (/ (- rtc
11) 15)
683 (make-string 10 32)))))))
684 (life (or zone-pgm-random-life-wait
(random 4)))
692 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
693 ;;; zone.el ends here