1 ;;; zone.el --- idle display hacks
3 ;; Copyright (C) 2000-2013 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 ;; THANKS: Christopher Mayer, Scott Flinchbaugh,
34 ;; Rachel Kalmar, Max Froumentin, Juri Linkov,
35 ;; Luigi Panzeri, John Paul Wallington.
39 (defvar zone-timer nil
40 "The timer we use to decide when to zone out, or nil if none.")
42 (defvar zone-timeout nil
43 "Seconds to timeout the zoning.
44 If nil, don't interrupt for about 1^26 seconds.")
46 ;; Vector of functions that zone out. `zone' will execute one of
47 ;; these functions, randomly chosen. The chosen function is invoked
48 ;; in the *zone* buffer, which contains the text of the selected
49 ;; window. If the function loops, it *must* periodically check and
50 ;; halt if `input-pending-p' is t (because quitting is disabled when
51 ;; Emacs idle timers are run).
52 (defvar zone-programs
[
54 zone-pgm-putz-with-case
59 zone-pgm-rotate-LR-lockstep
60 zone-pgm-rotate-RL-lockstep
61 zone-pgm-rotate-LR-variable
62 zone-pgm-rotate-RL-variable
64 zone-pgm-drip-fretfully
65 zone-pgm-five-oclock-swan-dive
66 zone-pgm-martini-swan-dive
68 zone-pgm-paragraph-spaz
70 zone-pgm-stress-destress
74 (defmacro zone-orig
(&rest body
)
75 `(with-current-buffer (get 'zone
'orig-buffer
)
78 (defmacro zone-hiding-mode-line
(&rest body
)
79 ;; This formerly worked by temporarily altering face `mode-line',
80 ;; which did not even work right, it seems.
81 `(let (mode-line-format)
84 (defun zone-call (program &optional timeout
)
85 "Call PROGRAM in a zoned way.
86 If PROGRAM is a function, call it, interrupting after the amount
87 of time in seconds specified by optional arg TIMEOUT, or `zone-timeout'
89 PROGRAM can also be a list of elements, which are interpreted like so:
90 If the element is a function or a list of a function and a number,
91 apply `zone-call' recursively."
92 (cond ((functionp program
)
93 (with-timeout ((or timeout zone-timeout
(ash 1 26)))
96 (mapcar (lambda (elem)
97 (cond ((functionp elem
) (zone-call elem
))
99 (functionp (car elem
))
100 (numberp (cadr elem
)))
101 (apply 'zone-call elem
))
102 (t (error "bad `zone-call' elem: %S" elem
))))
107 "Zone out, completely."
109 (save-window-excursion
110 (let ((f (selected-frame))
111 (outbuf (get-buffer-create "*zone*"))
112 (text (buffer-substring (window-start) (window-end)))
113 (wp (1+ (- (window-point (selected-window))
115 (put 'zone
'orig-buffer
(current-buffer))
116 (switch-to-buffer outbuf
)
117 (setq mode-name
"Zone")
119 (setq buffer-undo-list t
121 tab-width
(zone-orig tab-width
)
122 line-spacing
(zone-orig line-spacing
))
124 (untabify (point-min) (point-max))
125 (set-window-start (selected-window) (point-min))
126 (set-window-point (selected-window) wp
)
128 (let ((pgm (elt zone-programs
(random (length zone-programs
))))
129 (ct (and f
(frame-parameter f
'cursor-type
)))
130 (show-trailing-whitespace nil
)
131 (restore (list '(kill-buffer outbuf
))))
133 (modify-frame-parameters f
'((cursor-type .
(bar .
0))))
134 (setq restore
(cons '(modify-frame-parameters
135 f
(list (cons 'cursor-type ct
)))
137 ;; Make `restore' a self-disabling one-shot thunk.
138 (setq restore
`(lambda () ,@restore
(setq restore nil
)))
141 (message "Zoning... (%s)" pgm
)
143 ;; If some input is pending, zone says "sorry", which
144 ;; isn't nice; this might happen e.g. when they invoke the
145 ;; game by clicking the menu bar. So discard any pending
146 ;; input before zoning out.
147 (if (input-pending-p)
150 (message "Zoning...sorry"))
153 (while (not (input-pending-p))
154 (message "We were zoning when we wrote %s..." pgm
)
156 (message "...here's hoping we didn't hose your buffer!")
161 (message "Zoning...sorry")))
162 (when restore
(funcall restore
))))))
164 ;;;; Zone when idle, or not.
166 (defun zone-when-idle (secs)
167 "Zone out when Emacs has been idle for SECS seconds."
168 (interactive "nHow long before I start zoning (seconds): ")
169 (if (timerp zone-timer
)
170 (cancel-timer zone-timer
))
171 (setq zone-timer nil
)
173 (setq zone-timer
(run-with-idle-timer secs t
'zone
))))
175 (defun zone-leave-me-alone ()
176 "Don't zone out when Emacs is idle."
178 (if (timerp zone-timer
)
179 (cancel-timer zone-timer
))
180 (setq zone-timer nil
)
181 (message "I won't zone out any more"))
186 (defun zone-shift-up ()
188 (e (progn (forward-line 1) (point)))
189 (s (buffer-substring b e
)))
191 (goto-char (point-max))
194 (defun zone-shift-down ()
195 (goto-char (point-max))
197 (e (progn (forward-line -
1) (point)))
198 (s (buffer-substring b e
)))
200 (goto-char (point-min))
203 (defun zone-shift-left ()
204 (let ((inhibit-point-motion-hooks t
)
208 (setq s
(buffer-substring (point) (1+ (point))))
212 (ignore-errors (forward-char 1)))))
214 (defun zone-shift-right ()
215 (goto-char (point-max))
216 (let ((inhibit-point-motion-hooks t
)
220 (setq s
(buffer-substring (1- (point)) (point)))
226 (defun zone-pgm-jitter ()
233 (goto-char (point-min))
234 (while (not (input-pending-p))
235 (funcall (elt ops
(random (length ops
))))
236 (goto-char (point-min))
242 (defun zone-pgm-whack-chars ()
243 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars
'wc-tbl
))))
244 (while (not (input-pending-p))
247 (aset tbl i
(+ 48 (random (- 123 48))))
249 (translate-region (point-min) (point-max) tbl
)
252 (put 'zone-pgm-whack-chars
'wc-tbl
253 (let ((tbl (make-string 128 ?x
))
262 (defun zone-remove-text ()
267 (goto-char (point-min))
269 (if (looking-at "[^(){}\n\t ]")
270 (let ((n (random 5)))
280 (defun zone-pgm-dissolve ()
287 (defun zone-exploding-remove ()
291 (goto-char (point-min))
293 (if (looking-at "[^*\n\t ]")
294 (let ((n (random 5)))
303 (defun zone-pgm-explode ()
304 (zone-exploding-remove)
310 ;; Faster than `zone-pgm-putz-with-case', but not as good: all
311 ;; instances of the same letter have the same case, which produces a
312 ;; less interesting effect than you might imagine.
313 (defun zone-pgm-2nd-putz-with-case ()
314 (let ((tbl (make-string 128 ?x
))
319 (while (not (input-pending-p))
323 (if (zerop (random 5))
326 (setq i
(+ i
(1+ (random 5)))))
330 (if (zerop (random 5))
333 (setq i
(+ i
(1+ (random 5)))))
334 (translate-region (point-min) (point-max) tbl
)
337 (defun zone-pgm-putz-with-case ()
338 (goto-char (point-min))
339 (while (not (input-pending-p))
340 (let ((np (+ 2 (random 5)))
343 (funcall (if (zerop (random 2)) 'upcase-region
344 'downcase-region
) (1- np
) np
)
345 (setq np
(+ np
(1+ (random 5))))))
346 (goto-char (point-min))
352 (defun zone-line-specs ()
356 (goto-char (window-start))
357 (while (and ok
(< (point) (window-end)))
358 (when (looking-at "[\t ]*\\([^\n]+\\)")
359 (setq ret
(cons (cons (match-beginning 1) (match-end 1)) ret
)))
360 (setq ok
(zerop (forward-line 1)))))
363 (defun zone-pgm-rotate (&optional random-style
)
368 (let* ((beg (car ent
))
370 (amt (if random-style
371 (funcall random-style
)
373 (when (< (- end
(abs amt
)) beg
)
374 (setq amt
(random (- end beg
))))
378 (vector amt beg
(- end
(abs amt
)))
383 amt aamt cut paste txt i ent
)
384 (while (not (input-pending-p))
387 (setq ent
(aref specs i
))
388 (setq amt
(aref ent
0) aamt
(abs amt
))
391 (setq cut
2 paste
1))
392 (goto-char (aref ent cut
))
393 (setq aamt
(min aamt
(- (point-max) (point))))
394 (setq txt
(buffer-substring (point) (+ (point) aamt
)))
396 (goto-char (aref ent paste
))
401 (defun zone-pgm-rotate-LR-lockstep ()
402 (zone-pgm-rotate (lambda () 1)))
404 (defun zone-pgm-rotate-RL-lockstep ()
405 (zone-pgm-rotate (lambda () -
1)))
407 (defun zone-pgm-rotate-LR-variable ()
408 (zone-pgm-rotate (lambda () (1+ (random 3)))))
410 (defun zone-pgm-rotate-RL-variable ()
411 (zone-pgm-rotate (lambda () (1- (- (random 3))))))
416 (defsubst zone-cpos
(pos)
417 (buffer-substring pos
(1+ pos
)))
419 (defsubst zone-replace-char
(count del-count char-as-string new-value
)
420 (delete-char (or del-count
(- count
)))
421 (aset char-as-string
0 new-value
)
422 (dotimes (i count
) (insert char-as-string
)))
424 (defsubst zone-park
/sit-for
(pos seconds
)
427 (prog1 (sit-for seconds
)
430 (defun zone-fret (wbeg pos
)
431 (let* ((case-fold-search nil
)
432 (c-string (zone-cpos pos
))
433 (cw-ceil (ceiling (char-width (aref c-string
0))))
435 ((string-match "[a-z]" c-string
) (upcase c-string
))
436 ((string-match "[A-Z]" c-string
) (downcase c-string
))
437 (t (propertize " " 'display
`(space :width
,cw-ceil
)))))
442 (insert (if (= 0 (% i
2)) hmm c-string
))
443 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))
444 (delete-char -
1) (insert c-string
)))
446 (defun zone-fill-out-screen (width height
)
447 (let ((start (window-start))
448 (line (make-string width
32))
449 (inhibit-point-motion-hooks t
))
451 ;; fill out rectangular ws block
452 (while (progn (end-of-line)
453 (let ((cc (current-column)))
455 (insert (substring line cc
))
456 (delete-char (- width cc
)))
457 (cond ((eobp) (insert "\n") nil
)
458 (t (forward-char 1) t
)))))
459 ;; pad ws past bottom of screen
460 (let ((nl (- height
(count-lines (point-min) (point)))))
462 (setq line
(concat line
"\n"))
469 (defun zone-fall-through-ws (c wbeg wend
)
470 (let* ((cw-ceil (ceiling (char-width (aref c
0))))
471 (spaces (make-string cw-ceil
32))
472 (col (current-column))
475 (while (when (save-excursion
476 (and (zerop (forward-line 1))
479 (= col
(current-column)))
480 (setq newpos
(point))
481 (string= spaces
(buffer-substring-no-properties
482 newpos
(+ newpos cw-ceil
)))
483 (setq newpos
(+ newpos
(1- cw-ceil
)))))
488 (when (< (point) wend
)
489 (delete-char cw-ceil
)
492 (zone-park/sit-for wbeg
(setq wait
(* wait
0.8))))))
495 (defun zone-pgm-drip (&optional fret-p pancake-p
)
496 (let* ((ww (1- (window-width)))
502 (zone-fill-out-screen ww wh
)
503 (setq wbeg
(window-start)
506 (while (not (input-pending-p))
507 (setq mc
0 wend
(window-end))
508 ;; select non-ws character, but don't miss too much
509 (goto-char (+ wbeg
(random (- wend wbeg
))))
510 (while (looking-at "[ \n\f]")
511 (if (= total
(setq mc
(1+ mc
)))
513 (goto-char (+ wbeg
(random (- wend wbeg
))))))
514 ;; character animation sequence
516 (when fret-p
(zone-fret wbeg p
))
518 (setq c
(zone-cpos p
)
519 fall-p
(zone-fall-through-ws c wbeg wend
)))
520 ;; assuming current-column has not changed...
523 (< (count-lines (point-min) (point))
525 (let ((cw (ceiling (char-width (aref c
0)))))
526 (zone-replace-char cw
1 c ?
@) (zone-park/sit-for wbeg
0.137)
527 (zone-replace-char cw nil c ?
*) (zone-park/sit-for wbeg
0.137)
528 (zone-replace-char cw nil c ?_
)))))))
530 (defun zone-pgm-drip-fretfully ()
533 (defun zone-pgm-five-oclock-swan-dive ()
534 (zone-pgm-drip nil t
))
536 (defun zone-pgm-martini-swan-dive ()
539 (defun zone-pgm-rat-race ()
540 (while (not (input-pending-p))
541 (zone-call '((zone-pgm-rotate 10)
542 (zone-pgm-drip-fretfully 15)
545 (goto-char (point-min))
546 (while (re-search-forward " +$" nil t
)
547 (delete-region (match-beginning 0) (match-end 0))))
551 ;;;; paragraph spazzing (for textish modes)
553 (defun zone-pgm-paragraph-spaz ()
554 (if (memq (zone-orig major-mode
)
555 ;; there should be a better way to distinguish textish modes
556 '(text-mode texinfo-mode fundamental-mode
))
557 (let ((fill-column fill-column
)
560 (max-fc (1- (frame-width))))
563 (setq fill-column
(+ fill-column
(- (random 5) 2)))
564 (when (< fill-column fc-min
)
565 (setq fc-min fill-column
))
566 (when (> fill-column max-fc
)
567 (setq fill-column max-fc
))
568 (when (> fill-column fc-max
)
569 (setq fc-max fill-column
))))
570 (message "Zoning... (zone-pgm-rotate)")
574 ;;;; stressing and destressing
576 (defun zone-pgm-stress ()
577 (goto-char (point-min))
580 (while (and ok
(< (point) (point-max)))
582 (setq ok
(zerop (forward-line 1))
583 lines
(cons (buffer-substring p
(point)) lines
))))
585 (zone-hiding-mode-line
586 (let ((msg "Zoning... (zone-pgm-stress)"))
587 (while (not (string= msg
""))
588 (message (setq msg
(substring msg
1)))
590 (while (not (input-pending-p))
591 (when (< 50 (random 100))
592 (goto-char (point-max))
594 (delete-region (point) (line-beginning-position 2))
595 (goto-char (point-min))
596 (insert (nth (random (length lines
)) lines
)))
597 (message (concat (make-string (random (- (frame-width) 5)) ?
) "grrr"))
600 (defun zone-pgm-stress-destress ()
601 (zone-call 'zone-pgm-stress
25)
602 (zone-hiding-mode-line
606 (insert-buffer-substring "*Messages*")
608 (goto-char (point-max))
611 (delete-region (point-min) (window-start))
612 (message "hey why stress out anyway?")
613 (zone-call '((zone-pgm-rotate 30)
614 (zone-pgm-whack-chars 10)
618 ;;;; the lyfe so short the craft so long to lerne --chaucer
620 (defvar zone-pgm-random-life-wait nil
621 "Seconds to wait between successive `life' generations.
622 If nil, `zone-pgm-random-life' chooses a value from 0-3 (inclusive).")
624 (defvar life-patterns
) ; from life.el
626 (defun zone-pgm-random-life ()
628 (zone-fill-out-screen (1- (window-width)) (1- (window-height)))
629 (let ((top (progn (goto-char (window-start)) (forward-line 7) (point)))
630 (bot (progn (goto-char (window-end)) (forward-line -
7) (point)))
631 (rtc (- (frame-width) 11))
633 (max (1- (window-end)))
635 (delete-region max
(point-max))
636 (while (and (progn (goto-char min
) (sit-for 0.05))
637 (progn (goto-char (+ min
(random max
)))
638 (or (progn (skip-chars-forward " @\n" max
)
639 (not (= max
(point))))
640 (unless (or (= 0 (skip-chars-backward " @\n" min
))
644 (unless (or (eolp) (eobp))
645 (setq s
(zone-cpos (point))
649 1 s
(cond ((or (> top
(point))
651 (or (> 11 (setq col
(current-column)))
654 ((and (<= ?a c
) (>= ?z c
)) (+ c
(- ?A ?a
)))
655 ((and (<= ?A c
) (>= ?Z c
)) ?
*)
660 (while (< top
(point))
663 (setq col
(cons (buffer-substring (point) c
) col
))
664 ; (let ((inhibit-point-motion-hooks t))
667 (let ((life-patterns (vector
668 (if (and col
(search-forward "@" max t
))
669 (cons (make-string (length (car col
)) 32) col
)
670 (list (mapconcat 'identity
671 (make-list (/ (- rtc
11) 15)
673 (make-string 10 32)))))))
674 (life (or zone-pgm-random-life-wait
(random 4)))
681 ;;; zone.el ends here