1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Victor Zandy <zandy@cs.wisc.edu>
6 ;; Maintainer: Thien-Thi Nguyen <ttn@gnu.org>
8 ;; Created: June 6, 1998
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Don't zone out in front of Emacs! Try M-x zone.
28 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
30 ;; Bored by the zone pyrotechnics? Write your own! Add it to
31 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
33 ;; WARNING: Not appropriate for Emacs sessions over modems or
34 ;; computers as slow as mine.
36 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
37 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
38 ;; Luigi Panzeri, John Paul Wallington.
42 (defvar zone-timer nil
43 "The timer we use to decide when to zone out, or nil if none.")
45 (defvar zone-timeout nil
46 "*Seconds to timeout the zoning.
47 If nil, don't interrupt for about 1^26 seconds.")
49 ;; Vector of functions that zone out. `zone' will execute one of
50 ;; these functions, randomly chosen. The chosen function is invoked
51 ;; in the *zone* buffer, which contains the text of the selected
52 ;; window. If the function loops, it *must* periodically check and
53 ;; halt if `input-pending-p' is t (because quitting is disabled when
54 ;; Emacs idle timers are run).
55 (defvar zone-programs [
57 zone-pgm-putz-with-case
62 zone-pgm-rotate-LR-lockstep
63 zone-pgm-rotate-RL-lockstep
64 zone-pgm-rotate-LR-variable
65 zone-pgm-rotate-RL-variable
67 zone-pgm-drip-fretfully
68 zone-pgm-five-oclock-swan-dive
69 zone-pgm-martini-swan-dive
71 zone-pgm-paragraph-spaz
73 zone-pgm-stress-destress
77 (defmacro zone-orig (&rest body)
78 `(with-current-buffer (get 'zone 'orig-buffer)
81 (defmacro zone-hiding-modeline (&rest body)
82 ;; This formerly worked by temporarily altering face `mode-line',
83 ;; which did not even work right, it seems.
84 `(let (mode-line-format)
87 (defun zone-call (program &optional timeout)
88 "Call PROGRAM in a zoned way.
89 If PROGRAM is a function, call it, interrupting after the amount
90 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
92 PROGRAM can also be a list of elements, which are interpreted like so:
93 If the element is a function or a list of a function and a number,
94 apply `zone-call' recursively."
95 (cond ((functionp program)
96 (with-timeout ((or timeout zone-timeout (ash 1 26)))
99 (mapcar (lambda (elem)
100 (cond ((functionp elem) (zone-call elem))
102 (functionp (car elem))
103 (numberp (cadr elem)))
104 (apply 'zone-call elem))
105 (t (error "bad `zone-call' elem: %S" elem))))
110 "Zone out, completely."
112 (save-window-excursion
113 (let ((f (selected-frame))
114 (outbuf (get-buffer-create "*zone*"))
115 (text (buffer-substring (window-start) (window-end)))
116 (wp (1+ (- (window-point (selected-window))
118 (put 'zone 'orig-buffer (current-buffer))
119 (put 'zone 'modeline-hidden-level 0)
120 (switch-to-buffer outbuf)
121 (setq mode-name "Zone")
123 (setq buffer-undo-list t
125 tab-width (zone-orig tab-width)
126 line-spacing (zone-orig line-spacing))
128 (untabify (point-min) (point-max))
129 (set-window-start (selected-window) (point-min))
130 (set-window-point (selected-window) wp)
132 (let ((pgm (elt zone-programs (random (length zone-programs))))
133 (ct (and f (frame-parameter f 'cursor-type)))
134 (show-trailing-whitespace nil)
135 (restore (list '(kill-buffer outbuf))))
137 (modify-frame-parameters f '((cursor-type . (bar . 0))))
138 (setq restore (cons '(modify-frame-parameters
139 f (list (cons 'cursor-type ct)))
141 ;; Make `restore' a self-disabling one-shot thunk.
142 (setq restore `(lambda () ,@restore (setq restore nil)))
145 (message "Zoning... (%s)" pgm)
147 ;; If some input is pending, zone says "sorry", which
148 ;; isn't nice; this might happen e.g. when they invoke the
149 ;; game by clicking the menu bar. So discard any pending
150 ;; input before zoning out.
151 (if (input-pending-p)
154 (message "Zoning...sorry"))
157 (while (not (input-pending-p))
158 (message "We were zoning when we wrote %s..." pgm)
160 (message "...here's hoping we didn't hose your buffer!")
165 (message "Zoning...sorry")))
166 (when restore (funcall restore))))))
168 ;;;; Zone when idle, or not.
170 (defun zone-when-idle (secs)
171 "Zone out when Emacs has been idle for SECS seconds."
172 (interactive "nHow long before I start zoning (seconds): ")
173 (if (timerp zone-timer)
174 (cancel-timer zone-timer))
175 (setq zone-timer nil)
177 (setq zone-timer (run-with-idle-timer secs t 'zone))))
179 (defun zone-leave-me-alone ()
180 "Don't zone out when Emacs is idle."
182 (if (timerp zone-timer)
183 (cancel-timer zone-timer))
184 (setq zone-timer nil)
185 (message "I won't zone out any more"))
190 (defun zone-shift-up ()
192 (e (progn (forward-line 1) (point)))
193 (s (buffer-substring b e)))
195 (goto-char (point-max))
198 (defun zone-shift-down ()
199 (goto-char (point-max))
201 (e (progn (forward-line -1) (point)))
202 (s (buffer-substring b e)))
204 (goto-char (point-min))
207 (defun zone-shift-left ()
208 (let ((inhibit-point-motion-hooks t)
212 (setq s (buffer-substring (point) (1+ (point))))
216 (ignore-errors (forward-char 1)))))
218 (defun zone-shift-right ()
219 (goto-char (point-max))
220 (let ((inhibit-point-motion-hooks t)
224 (setq s (buffer-substring (1- (point)) (point)))
230 (defun zone-pgm-jitter ()
237 (goto-char (point-min))
238 (while (not (input-pending-p))
239 (funcall (elt ops (random (length ops))))
240 (goto-char (point-min))
246 (defun zone-pgm-whack-chars ()
247 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
248 (while (not (input-pending-p))
251 (aset tbl i (+ 48 (random (- 123 48))))
253 (translate-region (point-min) (point-max) tbl)
256 (put 'zone-pgm-whack-chars 'wc-tbl
257 (let ((tbl (make-string 128 ?x))
266 (defun zone-remove-text ()
271 (goto-char (point-min))
273 (if (looking-at "[^(){}\n\t ]")
274 (let ((n (random 5)))
284 (defun zone-pgm-dissolve ()
291 (defun zone-exploding-remove ()
295 (goto-char (point-min))
297 (if (looking-at "[^*\n\t ]")
298 (let ((n (random 5)))
307 (defun zone-pgm-explode ()
308 (zone-exploding-remove)
314 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
315 ;; instances of the same letter have the same case, which produces a
316 ;; less interesting effect than you might imagine.
317 (defun zone-pgm-2nd-putz-with-case ()
318 (let ((tbl (make-string 128 ?x))
323 (while (not (input-pending-p))
327 (if (zerop (random 5))
330 (setq i (+ i (1+ (random 5)))))
334 (if (zerop (random 5))
337 (setq i (+ i (1+ (random 5)))))
338 (translate-region (point-min) (point-max) tbl)
341 (defun zone-pgm-putz-with-case ()
342 (goto-char (point-min))
343 (while (not (input-pending-p))
344 (let ((np (+ 2 (random 5)))
347 (funcall (if (zerop (random 2)) 'upcase-region
348 'downcase-region) (1- np) np)
349 (setq np (+ np (1+ (random 5))))))
350 (goto-char (point-min))
356 (defun zone-line-specs ()
360 (goto-char (window-start))
361 (while (and ok (< (point) (window-end)))
362 (when (looking-at "[\t ]*\\([^\n]+\\)")
363 (setq ret (cons (cons (match-beginning 1) (match-end 1)) ret)))
364 (setq ok (zerop (forward-line 1)))))
367 (defun zone-pgm-rotate (&optional random-style)
372 (let* ((beg (car ent))
374 (amt (if random-style
375 (funcall random-style)
377 (when (< (- end (abs amt)) beg)
378 (setq amt (random (- end beg))))
382 (vector amt beg (- end (abs amt)))
387 amt aamt cut paste txt i ent)
388 (while (not (input-pending-p))
391 (setq ent (aref specs i))
392 (setq amt (aref ent 0) aamt (abs amt))
395 (setq cut 2 paste 1))
396 (goto-char (aref ent cut))
397 (setq aamt (min aamt (- (point-max) (point))))
398 (setq txt (buffer-substring (point) (+ (point) aamt)))
400 (goto-char (aref ent paste))
405 (defun zone-pgm-rotate-LR-lockstep ()
406 (zone-pgm-rotate (lambda () 1)))
408 (defun zone-pgm-rotate-RL-lockstep ()
409 (zone-pgm-rotate (lambda () -1)))
411 (defun zone-pgm-rotate-LR-variable ()
412 (zone-pgm-rotate (lambda () (1+ (random 3)))))
414 (defun zone-pgm-rotate-RL-variable ()
415 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
420 (defsubst zone-cpos (pos)
421 (buffer-substring pos (1+ pos)))
423 (defsubst zone-replace-char (count del-count char-as-string new-value)
424 (delete-char (or del-count (- count)))
425 (aset char-as-string 0 new-value)
426 (dotimes (i count) (insert char-as-string)))
428 (defsubst zone-park/sit-for (pos seconds)
431 (prog1 (sit-for seconds)
434 (defun zone-fret (wbeg pos)
435 (let* ((case-fold-search nil)
436 (c-string (zone-cpos pos))
437 (cw-ceil (ceiling (char-width (aref c-string 0))))
439 ((string-match "[a-z]" c-string) (upcase c-string))
440 ((string-match "[A-Z]" c-string) (downcase c-string))
441 (t (propertize " " 'display `(space :width ,cw-ceil)))))
446 (insert (if (= 0 (% i 2)) hmm c-string))
447 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))
448 (delete-char -1) (insert c-string)))
450 (defun zone-fill-out-screen (width height)
451 (let ((start (window-start))
452 (line (make-string width 32))
453 (inhibit-point-motion-hooks t))
455 ;; fill out rectangular ws block
456 (while (progn (end-of-line)
457 (let ((cc (current-column)))
459 (insert (substring line cc))
460 (delete-char (- width cc)))
461 (cond ((eobp) (insert "\n") nil)
462 (t (forward-char 1) t)))))
463 ;; pad ws past bottom of screen
464 (let ((nl (- height (count-lines (point-min) (point)))))
466 (setq line (concat line "\n"))
473 (defun zone-fall-through-ws (c wbeg wend)
474 (let* ((cw-ceil (ceiling (char-width (aref c 0))))
475 (spaces (make-string cw-ceil 32))
476 (col (current-column))
479 (while (when (save-excursion
480 (and (zerop (forward-line 1))
483 (= col (current-column)))
484 (setq newpos (point))
485 (string= spaces (buffer-substring-no-properties
486 newpos (+ newpos cw-ceil)))
487 (setq newpos (+ newpos (1- cw-ceil)))))
492 (when (< (point) wend)
493 (delete-char cw-ceil)
496 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
499 (defun zone-pgm-drip (&optional fret-p pancake-p)
500 (let* ((ww (1- (window-width)))
506 (zone-fill-out-screen ww wh)
507 (setq wbeg (window-start)
510 (while (not (input-pending-p))
511 (setq mc 0 wend (window-end))
512 ;; select non-ws character, but don't miss too much
513 (goto-char (+ wbeg (random (- wend wbeg))))
514 (while (looking-at "[ \n\f]")
515 (if (= total (setq mc (1+ mc)))
517 (goto-char (+ wbeg (random (- wend wbeg))))))
518 ;; character animation sequence
520 (when fret-p (zone-fret wbeg p))
522 (setq c (zone-cpos p)
523 fall-p (zone-fall-through-ws c wbeg wend)))
524 ;; assuming current-column has not changed...
527 (< (count-lines (point-min) (point))
529 (let ((cw (ceiling (char-width (aref c 0)))))
530 (zone-replace-char cw 1 c ?@) (zone-park/sit-for wbeg 0.137)
531 (zone-replace-char cw nil c ?*) (zone-park/sit-for wbeg 0.137)
532 (zone-replace-char cw nil c ?_)))))))
534 (defun zone-pgm-drip-fretfully ()
537 (defun zone-pgm-five-oclock-swan-dive ()
538 (zone-pgm-drip nil t))
540 (defun zone-pgm-martini-swan-dive ()
543 (defun zone-pgm-rat-race ()
544 (while (not (input-pending-p))
545 (zone-call '((zone-pgm-rotate 10)
546 (zone-pgm-drip-fretfully 15)
549 (goto-char (point-min))
550 (while (re-search-forward " +$" nil t)
551 (delete-region (match-beginning 0) (match-end 0))))
555 ;;;; paragraph spazzing (for textish modes)
557 (defun zone-pgm-paragraph-spaz ()
558 (if (memq (zone-orig major-mode)
559 ;; there should be a better way to distinguish textish modes
560 '(text-mode texinfo-mode fundamental-mode))
561 (let ((fill-column fill-column)
564 (max-fc (1- (frame-width))))
567 (setq fill-column (+ fill-column (- (random 5) 2)))
568 (when (< fill-column fc-min)
569 (setq fc-min fill-column))
570 (when (> fill-column max-fc)
571 (setq fill-column max-fc))
572 (when (> fill-column fc-max)
573 (setq fc-max fill-column))))
574 (message "Zoning... (zone-pgm-rotate)")
578 ;;;; stressing and destressing
580 (defun zone-pgm-stress ()
581 (goto-char (point-min))
584 (while (and ok (< (point) (point-max)))
586 (setq ok (zerop (forward-line 1))
587 lines (cons (buffer-substring p (point)) lines))))
589 (zone-hiding-modeline
590 (let ((msg "Zoning... (zone-pgm-stress)"))
591 (while (not (string= msg ""))
592 (message (setq msg (substring msg 1)))
594 (while (not (input-pending-p))
595 (when (< 50 (random 100))
596 (goto-char (point-max))
598 (let ((kill-whole-line t))
600 (goto-char (point-min))
601 (insert (nth (random (length lines)) lines)))
602 (message (concat (make-string (random (- (frame-width) 5)) ? ) "grrr"))
605 (defun zone-pgm-stress-destress ()
606 (zone-call 'zone-pgm-stress 25)
607 (zone-hiding-modeline
611 (insert-buffer-substring "*Messages*")
613 (goto-char (point-max))
616 (delete-region (point-min) (window-start))
617 (message "hey why stress out anyway?")
618 (zone-call '((zone-pgm-rotate 30)
619 (zone-pgm-whack-chars 10)
623 ;;;; the lyfe so short the craft so long to lerne --chaucer
625 (defvar zone-pgm-random-life-wait nil
626 "*Seconds to wait between successive `life' generations.
627 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
629 (defvar life-patterns) ; from life.el
631 (defun zone-pgm-random-life ()
633 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
634 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
635 (bot (progn (goto-char (window-end)) (forward-line -7) (point)))
636 (rtc (- (frame-width) 11))
638 (max (1- (window-end)))
640 (delete-region max (point-max))
641 (while (and (progn (goto-char min) (sit-for 0.05))
642 (progn (goto-char (+ min (random max)))
643 (or (progn (skip-chars-forward " @\n" max)
644 (not (= max (point))))
645 (unless (or (= 0 (skip-chars-backward " @\n" min))
649 (unless (or (eolp) (eobp))
650 (setq s (zone-cpos (point))
654 1 s (cond ((or (> top (point))
656 (or (> 11 (setq col (current-column)))
659 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
660 ((and (<= ?A c) (>= ?Z c)) ?*)
665 (while (< top (point))
668 (setq col (cons (buffer-substring (point) c) col))
669 ; (let ((inhibit-point-motion-hooks t))
672 (let ((life-patterns (vector
673 (if (and col (search-forward "@" max t))
674 (cons (make-string (length (car col)) 32) col)
675 (list (mapconcat 'identity
676 (make-list (/ (- rtc 11) 15)
678 (make-string 10 32)))))))
679 (life (or zone-pgm-random-life-wait (random 4)))
688 ;;; zone.el ends here