Add `remember-notes' function to store random notes across Emacs
[emacs.git] / lisp / play / zone.el
blob1724ebdf1980af6617864171f9f9e24e216c1b8e
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>
7 ;; Keywords: games
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/>.
25 ;;; Commentary:
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.
37 ;;; Code:
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 [
53 zone-pgm-jitter
54 zone-pgm-putz-with-case
55 zone-pgm-dissolve
56 ;; zone-pgm-explode
57 zone-pgm-whack-chars
58 zone-pgm-rotate
59 zone-pgm-rotate-LR-lockstep
60 zone-pgm-rotate-RL-lockstep
61 zone-pgm-rotate-LR-variable
62 zone-pgm-rotate-RL-variable
63 zone-pgm-drip
64 zone-pgm-drip-fretfully
65 zone-pgm-five-oclock-swan-dive
66 zone-pgm-martini-swan-dive
67 zone-pgm-rat-race
68 zone-pgm-paragraph-spaz
69 zone-pgm-stress
70 zone-pgm-stress-destress
71 zone-pgm-random-life
74 (defmacro zone-orig (&rest body)
75 `(with-current-buffer (get 'zone 'orig-buffer)
76 ,@body))
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)
82 ,@body))
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'
88 if unspecified, q.v.
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)))
94 (funcall program)))
95 ((listp program)
96 (mapcar (lambda (elem)
97 (cond ((functionp elem) (zone-call elem))
98 ((and (listp elem)
99 (functionp (car elem))
100 (numberp (cadr elem)))
101 (apply 'zone-call elem))
102 (t (error "bad `zone-call' elem: %S" elem))))
103 program))))
105 ;;;###autoload
106 (defun zone ()
107 "Zone out, completely."
108 (interactive)
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))
114 (window-start)))))
115 (put 'zone 'orig-buffer (current-buffer))
116 (switch-to-buffer outbuf)
117 (setq mode-name "Zone")
118 (erase-buffer)
119 (setq buffer-undo-list t
120 truncate-lines t
121 tab-width (zone-orig tab-width)
122 line-spacing (zone-orig line-spacing))
123 (insert text)
124 (untabify (point-min) (point-max))
125 (set-window-start (selected-window) (point-min))
126 (set-window-point (selected-window) wp)
127 (sit-for 0 500)
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))))
132 (when ct
133 (modify-frame-parameters f '((cursor-type . (bar . 0))))
134 (setq restore (cons '(modify-frame-parameters
135 f (list (cons 'cursor-type ct)))
136 restore)))
137 ;; Make `restore' a self-disabling one-shot thunk.
138 (setq restore `(lambda () ,@restore (setq restore nil)))
139 (condition-case nil
140 (progn
141 (message "Zoning... (%s)" pgm)
142 (garbage-collect)
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)
148 (discard-input))
149 (zone-call pgm)
150 (message "Zoning...sorry"))
151 (error
152 (funcall restore)
153 (while (not (input-pending-p))
154 (message "We were zoning when we wrote %s..." pgm)
155 (sit-for 3)
156 (message "...here's hoping we didn't hose your buffer!")
157 (sit-for 3)))
158 (quit
159 (funcall restore)
160 (ding)
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)
172 (or (<= secs 0)
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."
177 (interactive)
178 (if (timerp zone-timer)
179 (cancel-timer zone-timer))
180 (setq zone-timer nil)
181 (message "I won't zone out any more"))
184 ;;;; jittering
186 (defun zone-shift-up ()
187 (let* ((b (point))
188 (e (progn (forward-line 1) (point)))
189 (s (buffer-substring b e)))
190 (delete-region b e)
191 (goto-char (point-max))
192 (insert s)))
194 (defun zone-shift-down ()
195 (goto-char (point-max))
196 (let* ((b (point))
197 (e (progn (forward-line -1) (point)))
198 (s (buffer-substring b e)))
199 (delete-region b e)
200 (goto-char (point-min))
201 (insert s)))
203 (defun zone-shift-left ()
204 (let ((inhibit-point-motion-hooks t)
206 (while (not (eobp))
207 (unless (eolp)
208 (setq s (buffer-substring (point) (1+ (point))))
209 (delete-char 1)
210 (end-of-line)
211 (insert s))
212 (ignore-errors (forward-char 1)))))
214 (defun zone-shift-right ()
215 (goto-char (point-max))
216 (let ((inhibit-point-motion-hooks t)
218 (while (not (bobp))
219 (unless (bolp)
220 (setq s (buffer-substring (1- (point)) (point)))
221 (delete-char -1)
222 (beginning-of-line)
223 (insert s))
224 (end-of-line 0))))
226 (defun zone-pgm-jitter ()
227 (let ((ops [
228 zone-shift-left
229 zone-shift-right
230 zone-shift-down
231 zone-shift-up
233 (goto-char (point-min))
234 (while (not (input-pending-p))
235 (funcall (elt ops (random (length ops))))
236 (goto-char (point-min))
237 (sit-for 0 10))))
240 ;;;; whacking chars
242 (defun zone-pgm-whack-chars ()
243 (let ((tbl (copy-sequence (get 'zone-pgm-whack-chars 'wc-tbl))))
244 (while (not (input-pending-p))
245 (let ((i 48))
246 (while (< i 122)
247 (aset tbl i (+ 48 (random (- 123 48))))
248 (setq i (1+ i)))
249 (translate-region (point-min) (point-max) tbl)
250 (sit-for 0 2)))))
252 (put 'zone-pgm-whack-chars 'wc-tbl
253 (let ((tbl (make-string 128 ?x))
254 (i 0))
255 (while (< i 128)
256 (aset tbl i i)
257 (setq i (1+ i)))
258 tbl))
260 ;;;; dissolving
262 (defun zone-remove-text ()
263 (let ((working t))
264 (while working
265 (setq working nil)
266 (save-excursion
267 (goto-char (point-min))
268 (while (not (eobp))
269 (if (looking-at "[^(){}\n\t ]")
270 (let ((n (random 5)))
271 (if (not (= n 0))
272 (progn
273 (setq working t)
274 (forward-char 1))
275 (delete-char 1)
276 (insert " ")))
277 (forward-char 1))))
278 (sit-for 0 2))))
280 (defun zone-pgm-dissolve ()
281 (zone-remove-text)
282 (zone-pgm-jitter))
285 ;;;; exploding
287 (defun zone-exploding-remove ()
288 (let ((i 0))
289 (while (< i 5)
290 (save-excursion
291 (goto-char (point-min))
292 (while (not (eobp))
293 (if (looking-at "[^*\n\t ]")
294 (let ((n (random 5)))
295 (if (not (= n 0))
296 (forward-char 1))
297 (insert " ")))
298 (forward-char 1)))
299 (setq i (1+ i))
300 (sit-for 0 2)))
301 (zone-pgm-jitter))
303 (defun zone-pgm-explode ()
304 (zone-exploding-remove)
305 (zone-pgm-jitter))
308 ;;;; putzing w/ case
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))
315 (i 0))
316 (while (< i 128)
317 (aset tbl i i)
318 (setq i (1+ i)))
319 (while (not (input-pending-p))
320 (setq i ?a)
321 (while (<= i ?z)
322 (aset tbl i
323 (if (zerop (random 5))
324 (upcase i)
325 (downcase i)))
326 (setq i (+ i (1+ (random 5)))))
327 (setq i ?A)
328 (while (<= i ?z)
329 (aset tbl i
330 (if (zerop (random 5))
331 (downcase i)
332 (upcase i)))
333 (setq i (+ i (1+ (random 5)))))
334 (translate-region (point-min) (point-max) tbl)
335 (sit-for 0 2))))
337 (defun zone-pgm-putz-with-case ()
338 (goto-char (point-min))
339 (while (not (input-pending-p))
340 (let ((np (+ 2 (random 5)))
341 (pm (point-max)))
342 (while (< np pm)
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))
347 (sit-for 0 2)))
350 ;;;; rotating
352 (defun zone-line-specs ()
353 (let ((ok t)
354 ret)
355 (save-excursion
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)))))
361 ret))
363 (defun zone-pgm-rotate (&optional random-style)
364 (let* ((specs (apply
365 'vector
366 (let (res)
367 (mapc (lambda (ent)
368 (let* ((beg (car ent))
369 (end (cdr ent))
370 (amt (if random-style
371 (funcall random-style)
372 (- (random 7) 3))))
373 (when (< (- end (abs amt)) beg)
374 (setq amt (random (- end beg))))
375 (unless (= 0 amt)
376 (setq res
377 (cons
378 (vector amt beg (- end (abs amt)))
379 res)))))
380 (zone-line-specs))
381 res)))
382 (n (length specs))
383 amt aamt cut paste txt i ent)
384 (while (not (input-pending-p))
385 (setq i 0)
386 (while (< i n)
387 (setq ent (aref specs i))
388 (setq amt (aref ent 0) aamt (abs amt))
389 (if (> 0 amt)
390 (setq cut 1 paste 2)
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)))
395 (delete-char aamt)
396 (goto-char (aref ent paste))
397 (insert txt)
398 (setq i (1+ i)))
399 (sit-for 0.04))))
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))))))
414 ;;;; dripping
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)
425 (let ((p (point)))
426 (goto-char pos)
427 (prog1 (sit-for seconds)
428 (goto-char p))))
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))))
434 (hmm (cond
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)))))
438 (wait 0.5))
439 (dotimes (i 20)
440 (goto-char pos)
441 (delete-char 1)
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))
450 (goto-char start)
451 ;; fill out rectangular ws block
452 (while (progn (end-of-line)
453 (let ((cc (current-column)))
454 (if (< cc width)
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)))))
461 (when (> nl 0)
462 (setq line (concat line "\n"))
463 (dotimes (i nl)
464 (insert line))))
465 (goto-char start)
466 (recenter 0)
467 (sit-for 0)))
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))
473 (wait 0.15)
474 newpos fall-p)
475 (while (when (save-excursion
476 (and (zerop (forward-line 1))
477 (progn
478 (forward-char col)
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)))))
484 (setq fall-p t)
485 (delete-char 1)
486 (insert spaces)
487 (goto-char newpos)
488 (when (< (point) wend)
489 (delete-char cw-ceil)
490 (insert c)
491 (forward-char -1)
492 (zone-park/sit-for wbeg (setq wait (* wait 0.8))))))
493 fall-p))
495 (defun zone-pgm-drip (&optional fret-p pancake-p)
496 (let* ((ww (1- (window-width)))
497 (wh (window-height))
498 (mc 0) ; miss count
499 (total (* ww wh))
500 (fall-p nil)
501 wbeg wend c)
502 (zone-fill-out-screen ww wh)
503 (setq wbeg (window-start)
504 wend (window-end))
505 (catch 'done
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)))
512 (throw 'done 'sel)
513 (goto-char (+ wbeg (random (- wend wbeg))))))
514 ;; character animation sequence
515 (let ((p (point)))
516 (when fret-p (zone-fret wbeg p))
517 (goto-char p)
518 (setq c (zone-cpos p)
519 fall-p (zone-fall-through-ws c wbeg wend)))
520 ;; assuming current-column has not changed...
521 (when (and pancake-p
522 fall-p
523 (< (count-lines (point-min) (point))
524 wh))
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 ()
531 (zone-pgm-drip t))
533 (defun zone-pgm-five-oclock-swan-dive ()
534 (zone-pgm-drip nil t))
536 (defun zone-pgm-martini-swan-dive ()
537 (zone-pgm-drip t t))
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)
543 (zone-pgm-drip 10)
544 ((lambda ()
545 (goto-char (point-min))
546 (while (re-search-forward " +$" nil t)
547 (delete-region (match-beginning 0) (match-end 0))))
548 5)))))
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)
558 (fc-min fill-column)
559 (fc-max fill-column)
560 (max-fc (1- (frame-width))))
561 (while (sit-for 0.1)
562 (fill-paragraph 1)
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)")
571 (zone-pgm-rotate)))
574 ;;;; stressing and destressing
576 (defun zone-pgm-stress ()
577 (goto-char (point-min))
578 (let ((ok t)
579 lines)
580 (while (and ok (< (point) (point-max)))
581 (let ((p (point)))
582 (setq ok (zerop (forward-line 1))
583 lines (cons (buffer-substring p (point)) lines))))
584 (sit-for 5)
585 (zone-hiding-mode-line
586 (let ((msg "Zoning... (zone-pgm-stress)"))
587 (while (not (string= msg ""))
588 (message (setq msg (substring msg 1)))
589 (sit-for 0.05)))
590 (while (not (input-pending-p))
591 (when (< 50 (random 100))
592 (goto-char (point-max))
593 (forward-line -1)
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"))
598 (sit-for 0.1)))))
600 (defun zone-pgm-stress-destress ()
601 (zone-call 'zone-pgm-stress 25)
602 (zone-hiding-mode-line
603 (sit-for 3)
604 (erase-buffer)
605 (sit-for 3)
606 (insert-buffer-substring "*Messages*")
607 (message "")
608 (goto-char (point-max))
609 (recenter -1)
610 (sit-for 3)
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)
615 zone-pgm-drip))))
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 ()
627 (require '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))
632 (min (window-start))
633 (max (1- (window-end)))
634 s c col)
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))
641 (= min (point)))
642 (forward-char -1)
643 t))))
644 (unless (or (eolp) (eobp))
645 (setq s (zone-cpos (point))
646 c (aref s 0))
647 (zone-replace-char
648 (char-width c)
649 1 s (cond ((or (> top (point))
650 (< bot (point))
651 (or (> 11 (setq col (current-column)))
652 (< rtc col)))
654 ((and (<= ?a c) (>= ?z c)) (+ c (- ?A ?a)))
655 ((and (<= ?A c) (>= ?Z c)) ?*)
656 (t ?@)))))
657 (sit-for 3)
658 (setq col nil)
659 (goto-char bot)
660 (while (< top (point))
661 (setq c (point))
662 (move-to-column 9)
663 (setq col (cons (buffer-substring (point) c) col))
664 ; (let ((inhibit-point-motion-hooks t))
665 (end-of-line 0);)
666 (forward-char -10))
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)
672 (make-string 5 ?@))
673 (make-string 10 32)))))))
674 (life (or zone-pgm-random-life-wait (random 4)))
675 (kill-buffer nil))))
678 ;;;;;;;;;;;;;;;
679 (provide 'zone)
681 ;;; zone.el ends here