1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 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.
43 (defvar zone-timer nil
44 "The timer we use to decide when to zone out, or nil if none.")
46 (defvar zone-timeout nil
47 "*Seconds to timeout the zoning.
48 If nil, don't interrupt for about 1^26 seconds.")
50 ;; Vector of functions that zone out. `zone' will execute one of
51 ;; these functions, randomly chosen. The chosen function is invoked
52 ;; in the *zone* buffer, which contains the text of the selected
53 ;; window. If the function loops, it *must* periodically check and
54 ;; halt if `input-pending-p' is t (because quitting is disabled when
55 ;; Emacs idle timers are run).
56 (defvar zone-programs
[
58 zone-pgm-putz-with-case
63 zone-pgm-rotate-LR-lockstep
64 zone-pgm-rotate-RL-lockstep
65 zone-pgm-rotate-LR-variable
66 zone-pgm-rotate-RL-variable
68 zone-pgm-drip-fretfully
69 zone-pgm-five-oclock-swan-dive
70 zone-pgm-martini-swan-dive
72 zone-pgm-paragraph-spaz
74 zone-pgm-stress-destress
78 (defmacro zone-orig
(&rest body
)
79 `(with-current-buffer (get 'zone
'orig-buffer
)
82 (defmacro zone-hiding-modeline
(&rest body
)
83 ;; This formerly worked by temporarily altering face `mode-line',
84 ;; which did not even work right, it seems.
85 `(let (mode-line-format)
88 (defun zone-call (program &optional timeout
)
89 "Call PROGRAM in a zoned way.
90 If PROGRAM is a function, call it, interrupting after the amount
91 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
93 PROGRAM can also be a list of elements, which are interpreted like so:
94 If the element is a function or a list of a function and a number,
95 apply `zone-call' recursively."
96 (cond ((functionp program
)
97 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
100 (mapcar (lambda (elem)
101 (cond ((functionp elem
) (zone-call elem
))
103 (functionp (car elem
))
104 (numberp (cadr elem
)))
105 (apply 'zone-call elem
))
106 (t (error "bad `zone-call' elem: %S" elem
))))
111 "Zone out, completely."
113 (save-window-excursion
114 (let ((f (selected-frame))
115 (outbuf (get-buffer-create "*zone*"))
116 (text (buffer-substring (window-start) (window-end)))
117 (wp (1+ (- (window-point (selected-window))
119 (put 'zone
'orig-buffer
(current-buffer))
120 (put 'zone
'modeline-hidden-level
0)
121 (switch-to-buffer outbuf
)
122 (setq mode-name
"Zone")
124 (setq buffer-undo-list t
126 tab-width
(zone-orig tab-width
)
127 line-spacing
(zone-orig line-spacing
))
129 (untabify (point-min) (point-max))
130 (set-window-start (selected-window) (point-min))
131 (set-window-point (selected-window) wp
)
133 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
134 (ct (and f
(frame-parameter f
'cursor-type
)))
135 (show-trailing-whitespace nil
)
136 (restore (list '(kill-buffer outbuf
))))
138 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
139 (setq restore
(cons '(modify-frame-parameters
140 f
(list (cons 'cursor-type ct
)))
142 ;; Make `restore' a self-disabling one-shot thunk.
143 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
146 (message "Zoning... (%s)" pgm
)
148 ;; If some input is pending, zone says "sorry", which
149 ;; isn't nice; this might happen e.g. when they invoke the
150 ;; game by clicking the menu bar. So discard any pending
151 ;; input before zoning out.
152 (if (input-pending-p)
155 (message "Zoning...sorry"))
158 (while (not (input-pending-p))
159 (message "We were zoning when we wrote %s..." pgm
)
161 (message "...here's hoping we didn't hose your buffer!")
166 (message "Zoning...sorry")))
167 (when restore
(funcall restore
))))))
169 ;;;; Zone when idle, or not.
171 (defun zone-when-idle (secs)
172 "Zone out when Emacs has been idle for SECS seconds."
173 (interactive "nHow long before I start zoning (seconds): ")
174 (if (timerp zone-timer
)
175 (cancel-timer zone-timer
))
176 (setq zone-timer nil
)
178 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
180 (defun zone-leave-me-alone ()
181 "Don't zone out when Emacs is idle."
183 (if (timerp zone-timer
)
184 (cancel-timer zone-timer
))
185 (setq zone-timer nil
)
186 (message "I won't zone out any more"))
191 (defun zone-shift-up ()
193 (e (progn (forward-line 1) (point)))
194 (s (buffer-substring b e
)))
196 (goto-char (point-max))
199 (defun zone-shift-down ()
200 (goto-char (point-max))
202 (e (progn (forward-line -
1) (point)))
203 (s (buffer-substring b e
)))
205 (goto-char (point-min))
208 (defun zone-shift-left ()
209 (let ((inhibit-point-motion-hooks t
)
213 (setq s
(buffer-substring (point) (1+ (point))))
217 (ignore-errors (forward-char 1)))))
219 (defun zone-shift-right ()
220 (goto-char (point-max))
221 (let ((inhibit-point-motion-hooks t
)
225 (setq s
(buffer-substring (1- (point)) (point)))
231 (defun zone-pgm-jitter ()
238 (goto-char (point-min))
239 (while (not (input-pending-p))
240 (funcall (elt ops
(random (length ops
))))
241 (goto-char (point-min))
247 (defun zone-pgm-whack-chars ()
248 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
249 (while (not (input-pending-p))
252 (aset tbl i
(+ 48 (random (- 123 48))))
254 (translate-region (point-min) (point-max) tbl
)
257 (put 'zone-pgm-whack-chars
'wc-tbl
258 (let ((tbl (make-string 128 ?x
))
267 (defun zone-remove-text ()
272 (goto-char (point-min))
274 (if (looking-at "[^(){}\n\t ]")
275 (let ((n (random 5)))
285 (defun zone-pgm-dissolve ()
292 (defun zone-exploding-remove ()
296 (goto-char (point-min))
298 (if (looking-at "[^*\n\t ]")
299 (let ((n (random 5)))
308 (defun zone-pgm-explode ()
309 (zone-exploding-remove)
315 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
316 ;; instances of the same letter have the same case, which produces a
317 ;; less interesting effect than you might imagine.
318 (defun zone-pgm-2nd-putz-with-case ()
319 (let ((tbl (make-string 128 ?x
))
324 (while (not (input-pending-p))
328 (if (zerop (random 5))
331 (setq i
(+ i
(1+ (random 5)))))
335 (if (zerop (random 5))
338 (setq i
(+ i
(1+ (random 5)))))
339 (translate-region (point-min) (point-max) tbl
)
342 (defun zone-pgm-putz-with-case ()
343 (goto-char (point-min))
344 (while (not (input-pending-p))
345 (let ((np (+ 2 (random 5)))
348 (funcall (if (zerop (random 2)) 'upcase-region
349 'downcase-region
) (1- np
) np
)
350 (setq np
(+ np
(1+ (random 5))))))
351 (goto-char (point-min))
357 (defun zone-line-specs ()
361 (goto-char (window-start))
362 (while (and ok
(< (point) (window-end)))
363 (when (looking-at "[\t ]*\\([^\n]+\\)")
364 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
365 (setq ok
(zerop (forward-line 1)))))
368 (defun zone-pgm-rotate (&optional random-style
)
373 (let* ((beg (car ent
))
375 (amt (if random-style
376 (funcall random-style
)
378 (when (< (- end
(abs amt
)) beg
)
379 (setq amt
(random (- end beg
))))
383 (vector amt beg
(- end
(abs amt
)))
388 amt aamt cut paste txt i ent
)
389 (while (not (input-pending-p))
392 (setq ent
(aref specs i
))
393 (setq amt
(aref ent
0) aamt
(abs amt
))
396 (setq cut
2 paste
1))
397 (goto-char (aref ent cut
))
398 (setq aamt
(min aamt
(- (point-max) (point))))
399 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
401 (goto-char (aref ent paste
))
406 (defun zone-pgm-rotate-LR-lockstep ()
407 (zone-pgm-rotate (lambda () 1)))
409 (defun zone-pgm-rotate-RL-lockstep ()
410 (zone-pgm-rotate (lambda () -
1)))
412 (defun zone-pgm-rotate-LR-variable ()
413 (zone-pgm-rotate (lambda () (1+ (random 3)))))
415 (defun zone-pgm-rotate-RL-variable ()
416 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
421 (defsubst zone-cpos
(pos)
422 (buffer-substring pos
(1+ pos
)))
424 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
425 (delete-char (or del-count
(- count
)))
426 (aset char-as-string
0 new-value
)
427 (dotimes (i count
) (insert char-as-string
)))
429 (defsubst zone-park
/sit-for
(pos seconds
)
432 (prog1 (sit-for seconds
)
435 (defun zone-fret (wbeg pos
)
436 (let* ((case-fold-search nil
)
437 (c-string (zone-cpos pos
))
438 (cw-ceil (ceiling (char-width (aref c-string
0))))
440 ((string-match "[a-z]" c-string
) (upcase c-string
))
441 ((string-match "[A-Z]" c-string
) (downcase c-string
))
442 (t (propertize " " 'display
`(space :width
,cw-ceil
)))))
447 (insert (if (= 0 (% i
2)) hmm c-string
))
448 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))
449 (delete-char -
1) (insert c-string
)))
451 (defun zone-fill-out-screen (width height
)
452 (let ((start (window-start))
453 (line (make-string width
32))
454 (inhibit-point-motion-hooks t
))
456 ;; fill out rectangular ws block
457 (while (progn (end-of-line)
458 (let ((cc (current-column)))
460 (insert (substring line cc
))
461 (delete-char (- width cc
)))
462 (cond ((eobp) (insert "\n") nil
)
463 (t (forward-char 1) t
)))))
464 ;; pad ws past bottom of screen
465 (let ((nl (- height
(count-lines (point-min) (point)))))
467 (setq line
(concat line
"\n"))
474 (defun zone-fall-through-ws (c wbeg wend
)
475 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
476 (spaces (make-string cw-ceil
32))
477 (col (current-column))
480 (while (when (save-excursion
481 (and (zerop (forward-line 1))
484 (= col
(current-column)))
485 (setq newpos
(point))
486 (string= spaces
(buffer-substring-no-properties
487 newpos
(+ newpos cw-ceil
)))
488 (setq newpos
(+ newpos
(1- cw-ceil
)))))
493 (when (< (point) wend
)
494 (delete-char cw-ceil
)
497 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
500 (defun zone-pgm-drip (&optional fret-p pancake-p
)
501 (let* ((ww (1- (window-width)))
507 (zone-fill-out-screen ww wh
)
508 (setq wbeg
(window-start)
511 (while (not (input-pending-p))
512 (setq mc
0 wend
(window-end))
513 ;; select non-ws character, but don't miss too much
514 (goto-char (+ wbeg
(random (- wend wbeg
))))
515 (while (looking-at "[ \n\f]")
516 (if (= total
(setq mc
(1+ mc
)))
518 (goto-char (+ wbeg
(random (- wend wbeg
))))))
519 ;; character animation sequence
521 (when fret-p
(zone-fret wbeg p
))
523 (setq c
(zone-cpos p
)
524 fall-p
(zone-fall-through-ws c wbeg wend
)))
525 ;; assuming current-column has not changed...
528 (< (count-lines (point-min) (point))
530 (let ((cw (ceiling (char-width (aref c
0)))))
531 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
532 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
533 (zone-replace-char cw nil c ?_
)))))))
535 (defun zone-pgm-drip-fretfully ()
538 (defun zone-pgm-five-oclock-swan-dive ()
539 (zone-pgm-drip nil t
))
541 (defun zone-pgm-martini-swan-dive ()
544 (defun zone-pgm-rat-race ()
545 (while (not (input-pending-p))
546 (zone-call '((zone-pgm-rotate 10)
547 (zone-pgm-drip-fretfully 15)
550 (goto-char (point-min))
551 (while (re-search-forward " +$" nil t
)
552 (delete-region (match-beginning 0) (match-end 0))))
556 ;;;; paragraph spazzing (for textish modes)
558 (defun zone-pgm-paragraph-spaz ()
559 (if (memq (zone-orig major-mode
)
560 ;; there should be a better way to distinguish textish modes
561 '(text-mode texinfo-mode fundamental-mode
))
562 (let ((fill-column fill-column
)
565 (max-fc (1- (frame-width))))
568 (setq fill-column
(+ fill-column
(- (random 5) 2)))
569 (when (< fill-column fc-min
)
570 (setq fc-min fill-column
))
571 (when (> fill-column max-fc
)
572 (setq fill-column max-fc
))
573 (when (> fill-column fc-max
)
574 (setq fc-max fill-column
))))
575 (message "Zoning... (zone-pgm-rotate)")
579 ;;;; stressing and destressing
581 (defun zone-pgm-stress ()
582 (goto-char (point-min))
585 (while (and ok
(< (point) (point-max)))
587 (setq ok
(zerop (forward-line 1))
588 lines
(cons (buffer-substring p
(point)) lines
))))
590 (zone-hiding-modeline
591 (let ((msg "Zoning... (zone-pgm-stress)"))
592 (while (not (string= msg
""))
593 (message (setq msg
(substring msg
1)))
595 (while (not (input-pending-p))
596 (when (< 50 (random 100))
597 (goto-char (point-max))
599 (let ((kill-whole-line t
))
601 (goto-char (point-min))
602 (insert (nth (random (length lines
)) lines
)))
603 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
606 (defun zone-pgm-stress-destress ()
607 (zone-call 'zone-pgm-stress
25)
608 (zone-hiding-modeline
612 (insert-buffer-substring "*Messages*")
614 (goto-char (point-max))
617 (delete-region (point-min) (window-start))
618 (message "hey why stress out anyway?")
619 (zone-call '((zone-pgm-rotate 30)
620 (zone-pgm-whack-chars 10)
624 ;;;; the lyfe so short the craft so long to lerne --chaucer
626 (defvar zone-pgm-random-life-wait nil
627 "*Seconds to wait between successive `life' generations.
628 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
630 (defun zone-pgm-random-life ()
632 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
633 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
634 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
635 (rtc (- (frame-width) 11))
637 (max (1- (window-end)))
639 (delete-region max
(point-max))
640 (while (and (progn (goto-char min
) (sit-for 0.05))
641 (progn (goto-char (+ min
(random max
)))
642 (or (progn (skip-chars-forward " @\n" max
)
643 (not (= max
(point))))
644 (unless (or (= 0 (skip-chars-backward " @\n" min
))
648 (unless (or (eolp) (eobp))
649 (setq s
(zone-cpos (point))
653 1 s
(cond ((or (> top
(point))
655 (or (> 11 (setq col
(current-column)))
658 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
659 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
664 (while (< top
(point))
667 (setq col
(cons (buffer-substring (point) c
) col
))
668 ; (let ((inhibit-point-motion-hooks t))
671 (let ((life-patterns (vector
672 (if (and col
(search-forward "@" max t
))
673 (cons (make-string (length (car col
)) 32) col
)
674 (list (mapconcat 'identity
675 (make-list (/ (- rtc
11) 15)
677 (make-string 10 32)))))))
678 (life (or zone-pgm-random-life-wait
(random 4)))
687 ;;; zone.el ends here