1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000, 2001, 2004 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 2, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Don't zone out in front of Emacs! Try M-x zone.
30 ;; If it eventually irritates you, try M-x zone-leave-me-alone.
32 ;; Bored by the zone pyrotechnics? Write your own! Add it to
33 ;; `zone-programs'. See `zone-call' for higher-ordered zoning.
35 ;; WARNING: Not appropriate for Emacs sessions over modems or
36 ;; computers as slow as mine.
38 ;; THANKS: Christopher Mayer, Scott Flinchbaugh, Rachel Kalmar,
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
75 zone-pgm-paragraph-spaz
77 zone-pgm-stress-destress
80 (defmacro zone-orig
(&rest body
)
81 `(with-current-buffer (get 'zone
'orig-buffer
)
84 (defmacro zone-hiding-modeline
(&rest body
)
85 `(let (bg mode-line-fg mode-line-bg mode-line-box
)
88 (when (and (= 0 (get 'zone
'modeline-hidden-level
))
90 (setq bg
(face-background 'default
)
91 mode-line-box
(face-attribute 'mode-line
:box
)
92 mode-line-fg
(face-attribute 'mode-line
:foreground
)
93 mode-line-bg
(face-attribute 'mode-line
:background
))
94 (set-face-attribute 'mode-line nil
98 (put 'zone
'modeline-hidden-level
99 (1+ (get 'zone
'modeline-hidden-level
)))
101 (put 'zone
'modeline-hidden-level
102 (1- (get 'zone
'modeline-hidden-level
)))
103 (when (and (> 1 (get 'zone
'modeline-hidden-level
))
105 (set-face-attribute 'mode-line nil
106 :foreground mode-line-fg
107 :background mode-line-bg
108 :box mode-line-box
)))))
110 (defun zone-call (program &optional timeout
)
111 "Call PROGRAM in a zoned way.
112 If PROGRAM is a function, call it, interrupting after the amount
113 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
115 PROGRAM can also be a list of elements, which are interpreted like so:
116 If the element is a function or a list of a function and a number,
117 apply `zone-call' recursively."
118 (cond ((functionp program
)
119 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
122 (mapcar (lambda (elem)
123 (cond ((functionp elem
) (zone-call elem
))
125 (functionp (car elem
))
126 (numberp (cadr elem
)))
127 (apply 'zone-call elem
))
128 (t (error "bad `zone-call' elem: %S" elem
))))
133 "Zone out, completely."
135 (let ((f (selected-frame))
136 (outbuf (get-buffer-create "*zone*"))
137 (text (buffer-substring (window-start) (window-end)))
138 (wp (1+ (- (window-point (selected-window))
140 (put 'zone
'orig-buffer
(current-buffer))
141 (put 'zone
'modeline-hidden-level
0)
143 (setq mode-name
"Zone")
146 (switch-to-buffer outbuf
)
147 (setq buffer-undo-list t
)
148 (untabify (point-min) (point-max))
149 (set-window-start (selected-window) (point-min))
150 (set-window-point (selected-window) wp
)
152 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
153 (ct (and f
(frame-parameter f
'cursor-type
))))
154 (when ct
(modify-frame-parameters f
'((cursor-type .
(bar .
0)))))
157 (message "Zoning... (%s)" pgm
)
159 ;; If some input is pending, zone says "sorry", which
160 ;; isn't nice; this might happen e.g. when they invoke the
161 ;; game by clicking the menu bar. So discard any pending
162 ;; input before zoning out.
163 (if (input-pending-p)
166 (message "Zoning...sorry"))
168 (while (not (input-pending-p))
169 (message (format "We were zoning when we wrote %s..." pgm
))
171 (message "...here's hoping we didn't hose your buffer!")
173 (quit (ding) (message "Zoning...sorry")))
174 (when ct
(modify-frame-parameters f
(list (cons 'cursor-type ct
)))))
175 (kill-buffer outbuf
)))
177 ;;;; Zone when idle, or not.
179 (defun zone-when-idle (secs)
180 "Zone out when Emacs has been idle for SECS seconds."
181 (interactive "nHow long before I start zoning (seconds): ")
182 (if (timerp zone-timer
)
183 (cancel-timer zone-timer
))
184 (setq zone-timer nil
)
186 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
188 (defun zone-leave-me-alone ()
189 "Don't zone out when Emacs is idle."
191 (if (timerp zone-timer
)
192 (cancel-timer zone-timer
))
193 (setq zone-timer nil
)
194 (message "I won't zone out any more"))
199 (defun zone-shift-up ()
203 (if (looking-at "\n") (1+ (point)) (point))))
204 (s (buffer-substring b e
)))
206 (goto-char (point-max))
209 (defun zone-shift-down ()
210 (goto-char (point-max))
216 (if (looking-at "\n") (1+ (point)) (point))))
217 (s (buffer-substring b e
)))
219 (goto-char (point-min))
222 (defun zone-shift-left ()
225 (let ((c (following-char)))
231 (defun zone-shift-right ()
235 (let ((c (preceding-char)))
236 (delete-backward-char 1)
241 (defun zone-pgm-jitter ()
255 (goto-char (point-min))
256 (while (not (input-pending-p))
257 (funcall (elt ops
(random (length ops
))))
258 (goto-char (point-min))
262 ;;;; zone-pgm-whack-chars
264 (defun zone-pgm-whack-chars ()
265 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
266 (while (not (input-pending-p))
269 (aset tbl i
(+ 48 (random (- 123 48))))
271 (translate-region (point-min) (point-max) tbl
)
274 (put 'zone-pgm-whack-chars
'wc-tbl
275 (let ((tbl (make-string 128 ?x
))
282 ;;;; zone-pgm-dissolve
284 (defun zone-remove-text ()
289 (goto-char (point-min))
291 (if (looking-at "[^(){}\n\t ]")
292 (let ((n (random 5)))
302 (defun zone-pgm-dissolve ()
307 ;;;; zone-pgm-explode
309 (defun zone-exploding-remove ()
313 (goto-char (point-min))
315 (if (looking-at "[^*\n\t ]")
316 (let ((n (random 5)))
325 (defun zone-pgm-explode ()
326 (zone-exploding-remove)
330 ;;;; zone-pgm-putz-with-case
332 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
333 ;; instances of the same letter have the same case, which produces a
334 ;; less interesting effect than you might imagine.
335 (defun zone-pgm-2nd-putz-with-case ()
336 (let ((tbl (make-string 128 ?x
))
341 (while (not (input-pending-p))
345 (if (zerop (random 5))
348 (setq i
(+ i
(1+ (random 5)))))
352 (if (zerop (random 5))
355 (setq i
(+ i
(1+ (random 5)))))
356 (translate-region (point-min) (point-max) tbl
)
359 (defun zone-pgm-putz-with-case ()
360 (goto-char (point-min))
361 (while (not (input-pending-p))
362 (let ((np (+ 2 (random 5)))
366 (let ((prec (preceding-char))
367 (props (text-properties-at (1- (point)))))
368 (insert (if (zerop (random 2))
371 (set-text-properties (1- (point)) (point) props
))
374 (setq np
(+ np
(1+ (random 5))))))
375 (goto-char (point-min))
381 (defun zone-line-specs ()
384 (goto-char (window-start))
385 (while (< (point) (window-end))
386 (when (looking-at "[\t ]*\\([^\n]+\\)")
387 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
391 (defun zone-pgm-rotate (&optional random-style
)
395 (mapcar (lambda (ent)
396 (let* ((beg (car ent
))
398 (amt (if random-style
399 (funcall random-style
)
401 (when (< (- end
(abs amt
)) beg
)
402 (setq amt
(random (- end beg
))))
406 (vector amt beg
(- end
(abs amt
)))
411 amt aamt cut paste txt i ent
)
412 (while (not (input-pending-p))
415 (setq ent
(aref specs i
))
416 (setq amt
(aref ent
0) aamt
(abs amt
))
419 (setq cut
2 paste
1))
420 (goto-char (aref ent cut
))
421 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
423 (goto-char (aref ent paste
))
428 (defun zone-pgm-rotate-LR-lockstep ()
429 (zone-pgm-rotate (lambda () 1)))
431 (defun zone-pgm-rotate-RL-lockstep ()
432 (zone-pgm-rotate (lambda () -
1)))
434 (defun zone-pgm-rotate-LR-variable ()
435 (zone-pgm-rotate (lambda () (1+ (random 3)))))
437 (defun zone-pgm-rotate-RL-variable ()
438 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
443 (defun zone-cpos (pos)
444 (buffer-substring pos
(1+ pos
)))
446 (defun zone-fret (pos)
447 (let* ((case-fold-search nil
)
448 (c-string (zone-cpos pos
))
450 ((string-match "[a-z]" c-string
) (upcase c-string
))
451 ((string-match "[A-Z]" c-string
) (downcase c-string
))
454 (wait 0.5 (* wait
0.8)))
458 (insert (if (= 0 (% i
2)) hmm c-string
))
460 (delete-char -
1) (insert c-string
)))
462 (defun zone-fall-through-ws (c col wend
)
463 (let ((fall-p nil
) ; todo: move outward
465 (o (point)) ; for terminals w/o cursor hiding
473 (insert (if (< (point) wend
) c
" "))
479 (sit-for (setq wait
(* wait
0.8))))
480 (setq p
(1- (point))))
483 (defun zone-pgm-drip (&optional fret-p pancake-p
)
484 (let* ((ww (1- (window-width)))
489 (goto-char (point-min))
490 ;; fill out rectangular ws block
493 (let ((cc (current-column)))
495 (insert (make-string (- ww cc
) ?
))
496 (delete-char (- ww cc
))))
499 ;; pad ws past bottom of screen
500 (let ((nl (- wh
(count-lines (point-min) (point)))))
502 (let ((line (concat (make-string (1- ww
) ?
) "\n")))
507 (while (not (input-pending-p))
508 (goto-char (point-min))
510 (let ((wbeg (window-start))
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 p
))
523 (setq fall-p
(zone-fall-through-ws
524 (zone-cpos p
) (current-column) wend
))))
525 ;; assuming current-column has not changed...
528 (< (count-lines (point-min) (point))
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 ()
552 ;;;; zone-pgm-paragraph-spaz
554 (defun zone-pgm-paragraph-spaz ()
555 (if (memq (zone-orig major-mode
) '(text-mode fundamental-mode
))
556 (let ((fill-column fill-column
)
559 (max-fc (1- (frame-width))))
562 (setq fill-column
(+ fill-column
(- (random 5) 2)))
563 (when (< fill-column fc-min
)
564 (setq fc-min fill-column
))
565 (when (> fill-column max-fc
)
566 (setq fill-column max-fc
))
567 (when (> fill-column fc-max
)
568 (setq fc-max fill-column
))))
569 (message "Zoning... (zone-pgm-rotate)")
575 (defun zone-pgm-stress ()
576 (goto-char (point-min))
578 (while (< (point) (point-max))
581 (setq lines
(cons (buffer-substring p
(point)) lines
))))
583 (zone-hiding-modeline
584 (let ((msg "Zoning... (zone-pgm-stress)"))
585 (while (not (string= msg
""))
586 (message (setq msg
(substring msg
1)))
588 (while (not (input-pending-p))
589 (when (< 50 (random 100))
590 (goto-char (point-max))
592 (let ((kill-whole-line t
))
594 (goto-char (point-min))
595 (insert (nth (random (length lines
)) lines
)))
596 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
600 ;;;; zone-pgm-stress-destress
602 (defun zone-pgm-stress-destress ()
603 (zone-call 'zone-pgm-stress
25)
604 (zone-hiding-modeline
608 (insert-buffer "*Messages*")
610 (goto-char (point-max))
613 (delete-region (point-min) (window-start))
614 (message "hey why stress out anyway?")
615 (zone-call '((zone-pgm-rotate 30)
616 (zone-pgm-whack-chars 10)
623 ;;; arch-tag: 7092503d-74a9-4325-a55c-a026ede58cea
624 ;;; zone.el ends here