1 ;;; animate.el --- make text dance
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 ;; (animate-string STRING VPOS &optional HPOS)
25 ;; makes the string STRING appear starting at VPOS, HPOS
26 ;; by having each letter swoop into place from random starting position.
28 ;; animate-birthday-present was the first application of this program.
32 ;;; STRING is the string to be displayed,
33 ;;; and DEST-X, DEST-Y say where on the screen
36 ;;; This function returns a list describing
37 ;;; all the characters and the paths they should take.
38 ;;; Each element has the form
39 ;;; (CHAR START-Y START-X DEST-Y DEST-X).
41 ;;; The start position of each character is chosen randomly.
42 ;;; The destination is chosen to put it in the right place
43 ;;; in the string when the whole string finally reaches its
44 ;;; specified position.
46 (defun animate-initialize (string vpos hpos
)
47 (let ((characters nil
))
48 (dotimes (i (length string
))
50 (cons (list (aref string i
)
51 ;; Random starting positions.
52 (random (window-height))
53 (random (1- (window-width)))
54 ;; All the chars should end up
55 ;; on the specified line.
57 ;; The Ith character in the string
58 ;; needs to end up I positions later.
63 ;;; Display the characters in CHARACTERS,
64 ;;; each one FRACTION of the way from its start to its destination.
65 ;;; If FRACTION is 0, the characters appear in their starting positions.
66 ;;; If FRACTION is 1, the characters appear in their destinations.
68 (defun animate-step (characters fraction
)
69 (let ((remains (- 1 fraction
)))
70 (dolist (item characters
)
71 (let ((vpos (+ (* remains
(nth 1 item
))
72 (* fraction
(nth 3 item
))))
73 (hpos (+ (* remains
(nth 2 item
))
74 (* fraction
(nth 4 item
)))))
75 (animate-place-char (car item
) vpos hpos
)))))
77 ;;; Place the character CHAR at position VPOS, HPOS in the current buffer.
78 (defun animate-place-char (char vpos hpos
)
79 (goto-char (window-start))
80 (let ((next-line-add-newlines t
))
84 (move-to-column (floor hpos
) t
)
85 (unless (eolp) (delete-char 1))
88 (defvar animate-n-steps
10
89 "Number of steps to use `animate-string'.")
92 (defun animate-string (string vpos
&optional hpos
)
93 "Display STRING starting at position VPOS, HPOS, using animation.
94 The characters start at randomly chosen places,
95 and all slide in parallel to their final positions,
96 passing through `animate-n-steps' positions before the final ones.
97 If HPOS is nil (or omitted), center the string horizontally
98 in the current window."
100 (animate-initialize string vpos
102 ;; HPOS unspecified, so compute
103 ;; it so as to center the string.
104 (/ (- (window-width) (length string
)) 2)))))
105 (dotimes (i animate-n-steps
)
106 ;; Bind buffer-undo-list so it will be unchanged when we are done.
107 ;; (We're going to undo all our changes anyway.)
108 (let (buffer-undo-list
110 ;; Display the characters at the Ith position.
111 ;; This inserts them in the buffer.
112 (animate-step characters
(/ i
1.0 animate-n-steps
))
113 ;; Make sure buffer is displayed starting at the beginning.
114 (set-window-start nil
1)
115 ;; Display it, and wait just a little while.
117 ;; Now undo the changes we made in the buffer.
118 (setq list-to-undo buffer-undo-list
)
120 (let ((undo-in-progress t
))
121 (setq list-to-undo
(primitive-undo 1 list-to-undo
))))))
122 ;; Insert the characters in their final positions.
123 (animate-step characters
1)
124 ;; Put the cursor at the end of the text on the line.
126 ;; Redisplay so they appear on the screen there.
128 ;; This is so that the undo command, used afterwards,
129 ;; will undo the "animate" calls one by one.
133 (defun animate-birthday-present ()
134 "Display Sarah's birthday present."
136 ;; Make a suitable buffer to display the birthday present in.
137 (switch-to-buffer (get-buffer-create "Sarah"))
139 ;; Display the empty buffer.
141 ;; Make sure indentation does not use tabs.
142 ;; They would confuse things.
143 (setq indent-tabs-mode nil
)
145 (animate-string "Happy Birthday," 6)
146 (animate-string "Sarah" 7)
150 (animate-string "You are my sunshine," 10 30)
152 (animate-string "My only sunshine." 11 30)
154 (animate-string "I'm awful sad that" 12 30)
156 (animate-string "You've moved away." 13 30)
158 (animate-string "Let's talk together" 15 30)
160 (animate-string "And love more deeply." 16 30)
162 (animate-string "Please bring back" 17 30)
163 (animate-string "my sunshine" 18 34)
164 (animate-string "to stay!" 19 34))
166 ;;; animate.el ends here