1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 ;; Don't zone out in front of Emacs! Try M-x zone.
29 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
31 ;; Bored by the zone pyrotechnics? Write your own! Add it to
32 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
34 ;; WARNING: Not appropriate for Emacs sessions over modems or
35 ;; computers as slow as mine.
37 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
38 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
39 ;; Luigi Panzeri, John Paul Wallington.
45 (eval-when-compile (require 'cl
))
47 (defvar zone-timer nil
48 "The timer we use to decide when to zone out, or nil if none.")
50 (defvar zone-timeout nil
51 "*Seconds to timeout the zoning.
52 If nil, don't interrupt for about 1^26 seconds.")
54 ;; Vector of functions that zone out. `zone' will execute one of
55 ;; these functions, randomly chosen. The chosen function is invoked
56 ;; in the *zone* buffer, which contains the text of the selected
57 ;; window. If the function loops, it *must* periodically check and
58 ;; halt if `input-pending-p' is t (because quitting is disabled when
59 ;; Emacs idle timers are run).
60 (defvar zone-programs
[
62 zone-pgm-putz-with-case
67 zone-pgm-rotate-LR-lockstep
68 zone-pgm-rotate-RL-lockstep
69 zone-pgm-rotate-LR-variable
70 zone-pgm-rotate-RL-variable
72 zone-pgm-drip-fretfully
73 zone-pgm-five-oclock-swan-dive
74 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 ;; This formerly worked by temporarily altering face `mode-line',
88 ;; which did not even work right, it seems.
89 `(let (mode-line-format)
92 (defun zone-call (program &optional timeout
)
93 "Call PROGRAM in a zoned way.
94 If PROGRAM is a function, call it, interrupting after the amount
95 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
97 PROGRAM can also be a list of elements, which are interpreted like so:
98 If the element is a function or a list of a function and a number,
99 apply `zone-call' recursively."
100 (cond ((functionp program
)
101 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
104 (mapcar (lambda (elem)
105 (cond ((functionp elem
) (zone-call elem
))
107 (functionp (car elem
))
108 (numberp (cadr elem
)))
109 (apply 'zone-call elem
))
110 (t (error "bad `zone-call' elem: %S" elem
))))
115 "Zone out, completely."
117 (save-window-excursion
118 (let ((f (selected-frame))
119 (outbuf (get-buffer-create "*zone*"))
120 (text (buffer-substring (window-start) (window-end)))
121 (wp (1+ (- (window-point (selected-window))
123 (put 'zone
'orig-buffer
(current-buffer))
124 (put 'zone
'modeline-hidden-level
0)
125 (switch-to-buffer outbuf
)
126 (setq mode-name
"Zone")
128 (setq buffer-undo-list t
130 tab-width
(zone-orig tab-width
)
131 line-spacing
(zone-orig line-spacing
))
133 (untabify (point-min) (point-max))
134 (set-window-start (selected-window) (point-min))
135 (set-window-point (selected-window) wp
)
137 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
138 (ct (and f
(frame-parameter f
'cursor-type
)))
139 (show-trailing-whitespace nil
)
140 (restore (list '(kill-buffer outbuf
))))
142 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
143 (setq restore
(cons '(modify-frame-parameters
144 f
(list (cons 'cursor-type ct
)))
146 ;; Make `restore' a self-disabling one-shot thunk.
147 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
150 (message "Zoning... (%s)" pgm
)
152 ;; If some input is pending, zone says "sorry", which
153 ;; isn't nice; this might happen e.g. when they invoke the
154 ;; game by clicking the menu bar. So discard any pending
155 ;; input before zoning out.
156 (if (input-pending-p)
159 (message "Zoning...sorry"))
162 (while (not (input-pending-p))
163 (message "We were zoning when we wrote %s..." pgm
)
165 (message "...here's hoping we didn't hose your buffer!")
170 (message "Zoning...sorry")))
171 (when restore
(funcall restore
))))))
173 ;;;; Zone when idle, or not.
175 (defun zone-when-idle (secs)
176 "Zone out when Emacs has been idle for SECS seconds."
177 (interactive "nHow long before I start zoning (seconds): ")
178 (if (timerp zone-timer
)
179 (cancel-timer zone-timer
))
180 (setq zone-timer nil
)
182 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
184 (defun zone-leave-me-alone ()
185 "Don't zone out when Emacs is idle."
187 (if (timerp zone-timer
)
188 (cancel-timer zone-timer
))
189 (setq zone-timer nil
)
190 (message "I won't zone out any more"))
195 (defun zone-shift-up ()
197 (e (progn (forward-line 1) (point)))
198 (s (buffer-substring b e
)))
200 (goto-char (point-max))
203 (defun zone-shift-down ()
204 (goto-char (point-max))
206 (e (progn (forward-line -
1) (point)))
207 (s (buffer-substring b e
)))
209 (goto-char (point-min))
212 (defun zone-shift-left ()
216 (setq s
(buffer-substring (point) (1+ (point))))
222 (defun zone-shift-right ()
223 (goto-char (point-max))
228 (setq s
(buffer-substring (1- (point)) (point)))
234 (defun zone-pgm-jitter ()
241 (goto-char (point-min))
242 (while (not (input-pending-p))
243 (funcall (elt ops
(random (length ops
))))
244 (goto-char (point-min))
250 (defun zone-pgm-whack-chars ()
251 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
252 (while (not (input-pending-p))
255 (aset tbl i
(+ 48 (random (- 123 48))))
257 (translate-region (point-min) (point-max) tbl
)
260 (put 'zone-pgm-whack-chars
'wc-tbl
261 (let ((tbl (make-string 128 ?x
))
270 (defun zone-remove-text ()
275 (goto-char (point-min))
277 (if (looking-at "[^(){}\n\t ]")
278 (let ((n (random 5)))
288 (defun zone-pgm-dissolve ()
295 (defun zone-exploding-remove ()
299 (goto-char (point-min))
301 (if (looking-at "[^*\n\t ]")
302 (let ((n (random 5)))
311 (defun zone-pgm-explode ()
312 (zone-exploding-remove)
318 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
319 ;; instances of the same letter have the same case, which produces a
320 ;; less interesting effect than you might imagine.
321 (defun zone-pgm-2nd-putz-with-case ()
322 (let ((tbl (make-string 128 ?x
))
327 (while (not (input-pending-p))
331 (if (zerop (random 5))
334 (setq i
(+ i
(1+ (random 5)))))
338 (if (zerop (random 5))
341 (setq i
(+ i
(1+ (random 5)))))
342 (translate-region (point-min) (point-max) tbl
)
345 (defun zone-pgm-putz-with-case ()
346 (goto-char (point-min))
347 (while (not (input-pending-p))
348 (let ((np (+ 2 (random 5)))
352 (let ((prec (preceding-char))
353 (props (text-properties-at (1- (point)))))
354 (insert (if (zerop (random 2))
357 (set-text-properties (1- (point)) (point) props
))
360 (setq np
(+ np
(1+ (random 5))))))
361 (goto-char (point-min))
367 (defun zone-line-specs ()
370 (goto-char (window-start))
371 (while (< (point) (window-end))
372 (when (looking-at "[\t ]*\\([^\n]+\\)")
373 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
377 (defun zone-pgm-rotate (&optional random-style
)
382 (let* ((beg (car ent
))
384 (amt (if random-style
385 (funcall random-style
)
387 (when (< (- end
(abs amt
)) beg
)
388 (setq amt
(random (- end beg
))))
392 (vector amt beg
(- end
(abs amt
)))
397 amt aamt cut paste txt i ent
)
398 (while (not (input-pending-p))
401 (setq ent
(aref specs i
))
402 (setq amt
(aref ent
0) aamt
(abs amt
))
405 (setq cut
2 paste
1))
406 (goto-char (aref ent cut
))
407 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
409 (goto-char (aref ent paste
))
414 (defun zone-pgm-rotate-LR-lockstep ()
415 (zone-pgm-rotate (lambda () 1)))
417 (defun zone-pgm-rotate-RL-lockstep ()
418 (zone-pgm-rotate (lambda () -
1)))
420 (defun zone-pgm-rotate-LR-variable ()
421 (zone-pgm-rotate (lambda () (1+ (random 3)))))
423 (defun zone-pgm-rotate-RL-variable ()
424 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
429 (defsubst zone-cpos
(pos)
430 (buffer-substring pos
(1+ pos
)))
432 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
433 (delete-char (or del-count
(- count
)))
434 (aset char-as-string
0 new-value
)
435 (dotimes (i count
) (insert char-as-string
)))
437 (defsubst zone-park
/sit-for
(pos seconds
)
440 (prog1 (sit-for seconds
)
443 (defun zone-fret (wbeg pos
)
444 (let* ((case-fold-search nil
)
445 (c-string (zone-cpos pos
))
446 (cw-ceil (ceiling (char-width (aref c-string
0))))
448 ((string-match "[a-z]" c-string
) (upcase c-string
))
449 ((string-match "[A-Z]" c-string
) (downcase c-string
))
450 (t (propertize " " 'display
`(space :width
,cw-ceil
))))))
452 (wait 0.5 (* wait
0.8)))
456 (insert (if (= 0 (% i
2)) hmm c-string
))
457 (zone-park/sit-for wbeg wait
))
458 (delete-char -
1) (insert c-string
)))
460 (defun zone-fill-out-screen (width height
)
461 (let ((start (window-start))
462 (line (make-string width
32)))
464 ;; fill out rectangular ws block
465 (while (progn (end-of-line)
466 (let ((cc (current-column)))
468 (insert (substring line cc
))
469 (delete-char (- width cc
)))
470 (cond ((eobp) (insert "\n") nil
)
471 (t (forward-char 1) t
)))))
472 ;; pad ws past bottom of screen
473 (let ((nl (- height
(count-lines (point-min) (point)))))
475 (setq line
(concat line
"\n"))
483 (defun zone-fall-through-ws (c wbeg wend
)
484 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
485 (spaces (make-string cw-ceil
32))
486 (col (current-column))
489 (while (when (save-excursion
491 (and (= col
(current-column))
492 (setq newpos
(point))
493 (string= spaces
(buffer-substring-no-properties
494 newpos
(+ newpos cw-ceil
)))
495 (setq newpos
(+ newpos
(1- cw-ceil
)))))
500 (when (< (point) wend
)
501 (delete-char cw-ceil
)
504 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
507 (defun zone-pgm-drip (&optional fret-p pancake-p
)
508 (let* ((ww (1- (window-width)))
514 (zone-fill-out-screen ww wh
)
515 (setq wbeg
(window-start)
518 (while (not (input-pending-p))
519 (setq mc
0 wend
(window-end))
520 ;; select non-ws character, but don't miss too much
521 (goto-char (+ wbeg
(random (- wend wbeg
))))
522 (while (looking-at "[ \n\f]")
523 (if (= total
(setq mc
(1+ mc
)))
525 (goto-char (+ wbeg
(random (- wend wbeg
))))))
526 ;; character animation sequence
528 (when fret-p
(zone-fret wbeg p
))
530 (setq c
(zone-cpos p
)
531 fall-p
(zone-fall-through-ws c wbeg wend
)))
532 ;; assuming current-column has not changed...
535 (< (count-lines (point-min) (point))
537 (let ((cw (ceiling (char-width (aref c
0)))))
538 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
539 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
540 (zone-replace-char cw nil c ?_
)))))))
542 (defun zone-pgm-drip-fretfully ()
545 (defun zone-pgm-five-oclock-swan-dive ()
546 (zone-pgm-drip nil t
))
548 (defun zone-pgm-martini-swan-dive ()
551 (defun zone-pgm-rat-race ()
552 (while (not (input-pending-p))
553 (zone-call '((zone-pgm-rotate 10)
554 (zone-pgm-drip-fretfully 15)
557 (goto-char (point-min))
558 (while (re-search-forward " +$" nil t
)
559 (delete-region (match-beginning 0) (match-end 0))))
563 ;;;; paragraph spazzing (for textish modes)
565 (defun zone-pgm-paragraph-spaz ()
566 (if (memq (zone-orig major-mode
)
567 ;; there should be a better way to distinguish textish modes
568 '(text-mode texinfo-mode fundamental-mode
))
569 (let ((fill-column fill-column
)
572 (max-fc (1- (frame-width))))
575 (setq fill-column
(+ fill-column
(- (random 5) 2)))
576 (when (< fill-column fc-min
)
577 (setq fc-min fill-column
))
578 (when (> fill-column max-fc
)
579 (setq fill-column max-fc
))
580 (when (> fill-column fc-max
)
581 (setq fc-max fill-column
))))
582 (message "Zoning... (zone-pgm-rotate)")
586 ;;;; stressing and destressing
588 (defun zone-pgm-stress ()
589 (goto-char (point-min))
591 (while (< (point) (point-max))
594 (setq lines
(cons (buffer-substring p
(point)) lines
))))
596 (zone-hiding-modeline
597 (let ((msg "Zoning... (zone-pgm-stress)"))
598 (while (not (string= msg
""))
599 (message (setq msg
(substring msg
1)))
601 (while (not (input-pending-p))
602 (when (< 50 (random 100))
603 (goto-char (point-max))
605 (let ((kill-whole-line t
))
607 (goto-char (point-min))
608 (insert (nth (random (length lines
)) lines
)))
609 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
612 (defun zone-pgm-stress-destress ()
613 (zone-call 'zone-pgm-stress
25)
614 (zone-hiding-modeline
618 (insert-buffer-substring "*Messages*")
620 (goto-char (point-max))
623 (delete-region (point-min) (window-start))
624 (message "hey why stress out anyway?")
625 (zone-call '((zone-pgm-rotate 30)
626 (zone-pgm-whack-chars 10)
630 ;;;; the lyfe so short the craft so long to lerne --chaucer
632 (defvar zone-pgm-random-life-wait nil
633 "*Seconds to wait between successive `life' generations.
634 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
636 (defun zone-pgm-random-life ()
638 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
639 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
640 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
641 (rtc (- (frame-width) 11))
643 (max (1- (window-end)))
645 (delete-region max
(point-max))
646 (while (and (progn (goto-char min
) (sit-for 0.05))
647 (progn (goto-char (+ min
(random max
)))
648 (or (progn (skip-chars-forward " @\n" max
)
649 (not (= max
(point))))
650 (unless (or (= 0 (skip-chars-backward " @\n" min
))
654 (unless (or (eolp) (eobp))
655 (setq s
(zone-cpos (point))
659 1 s
(cond ((or (> top
(point))
661 (or (> 11 (setq col
(current-column)))
664 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
665 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
670 (while (< top
(point))
673 (setq col
(cons (buffer-substring (point) c
) col
))
676 (let ((life-patterns (vector
677 (if (and col
(search-forward "@" max t
))
678 (cons (make-string (length (car col
)) 32) col
)
679 (list (mapconcat 'identity
680 (make-list (/ (- rtc
11) 15)
682 (make-string 10 32)))))))
683 (life (or zone-pgm-random-life-wait
(random 4)))
692 ;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
693 ;;; zone.el ends here