1 ;;; animate.el --- make text dance
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Maintainer: Richard Stallman <rms@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; (animate-string STRING VPOS &optional HPOS)
28 ;; makes the string STRING appear starting at VPOS, HPOS
29 ;; by having each letter swoop into place from random starting position.
31 ;; animate-birthday-present was the first application of this program.
35 ;;; STRING is the string to be displayed,
36 ;;; and DEST-X, DEST-Y say where on the screen
39 ;;; This function returns a list describing
40 ;;; all the characters and the paths they should take.
41 ;;; Each element has the form
42 ;;; (CHAR START-Y START-X DEST-Y DEST-X).
44 ;;; The start position of each character is chosen randomly.
45 ;;; The destination is chosen to put it in the right place
46 ;;; in the string when the whole string finally reaches its
47 ;;; specified position.
49 (defun animate-initialize (string vpos hpos
)
50 (let ((characters nil
))
51 (dotimes (i (length string
))
53 (cons (list (aref string i
)
54 ;; Random starting positions.
55 (random (window-height))
56 (random (1- (window-width)))
57 ;; All the chars should end up
58 ;; on the specified line.
60 ;; The Ith character in the string
61 ;; needs to end up I positions later.
66 ;;; Display the characters in CHARACTERS,
67 ;;; each one FRACTION of the way from its start to its destination.
68 ;;; If FRACTION is 0, the characters appear in their starting positions.
69 ;;; If FRACTION is 1, the characters appear in their destinations.
71 (defun animate-step (characters fraction
)
72 (let ((remains (- 1 fraction
)))
73 (dolist (item characters
)
74 (let ((vpos (+ (* remains
(nth 1 item
))
75 (* fraction
(nth 3 item
))))
76 (hpos (+ (* remains
(nth 2 item
))
77 (* fraction
(nth 4 item
)))))
78 (animate-place-char (car item
) vpos hpos
)))))
80 ;;; Place the character CHAR at position VPOS, HPOS in the current buffer.
81 (defun animate-place-char (char vpos hpos
)
82 (goto-char (window-start))
86 (if (= (forward-line 1) 1)
89 (move-to-column (floor hpos
) t
)
90 (unless (eolp) (delete-char 1))
93 (defvar animate-n-steps
10
94 "Number of steps to use `animate-string'.")
97 (defun animate-string (string vpos
&optional hpos
)
98 "Display STRING starting at position VPOS, HPOS, using animation.
99 The characters start at randomly chosen places,
100 and all slide in parallel to their final positions,
101 passing through `animate-n-steps' positions before the final ones.
102 If HPOS is nil (or omitted), center the string horizontally
103 in the current window."
105 (animate-initialize string vpos
107 ;; HPOS unspecified, so compute
108 ;; it so as to center the string.
109 (max 0 (/ (- (window-width) (length string
)) 2))))))
110 (dotimes (i animate-n-steps
)
111 ;; Bind buffer-undo-list so it will be unchanged when we are done.
112 ;; (We're going to undo all our changes anyway.)
113 (let (buffer-undo-list
115 ;; Display the characters at the Ith position.
116 ;; This inserts them in the buffer.
117 (animate-step characters
(/ i
1.0 animate-n-steps
))
118 ;; Make sure buffer is displayed starting at the beginning.
119 (set-window-start nil
1)
120 ;; Display it, and wait just a little while.
122 ;; Now undo the changes we made in the buffer.
123 (setq list-to-undo buffer-undo-list
)
125 (let ((undo-in-progress t
))
126 (setq list-to-undo
(primitive-undo 1 list-to-undo
))))))
127 ;; Insert the characters in their final positions.
128 (animate-step characters
1)
129 ;; Put the cursor at the end of the text on the line.
131 ;; Redisplay so they appear on the screen there.
133 ;; This is so that the undo command, used afterwards,
134 ;; will undo the "animate" calls one by one.
138 (defun animate-sequence (list-of-strings space
)
139 "Display strings from LIST-OF-STRING with animation in a new buffer.
140 Strings will be separated from each other by SPACE lines."
141 (let ((vpos (/ (- (window-height)
142 1 ;; For the mode-line
143 (* (1- (length list-of-strings
)) space
)
144 (length list-of-strings
))
146 (switch-to-buffer (get-buffer-create "*Animation*"))
149 (setq indent-tabs-mode nil
)
150 (while list-of-strings
151 (animate-string (car list-of-strings
) vpos
)
152 (setq vpos
(+ vpos space
1))
153 (setq list-of-strings
(cdr list-of-strings
)))))
156 (defun animate-birthday-present (&optional name
)
157 "Display one's birthday present in a new buffer.
158 You can specify the one's name by NAME; the default value is \"Sarah\"."
159 (interactive (list (read-string "Name (default Sarah): "
161 ;; Make a suitable buffer to display the birthday present in.
162 (switch-to-buffer (get-buffer-create (format "*%s*" name
)))
164 ;; Display the empty buffer.
166 ;; Make sure indentation does not use tabs.
167 ;; They would confuse things.
168 (setq indent-tabs-mode nil
)
170 (animate-string "Happy Birthday," 6)
171 (animate-string (format "%s" name
) 7)
175 (animate-string "You are my sunshine," 10 30)
177 (animate-string "My only sunshine." 11 30)
179 (animate-string "I'm awful sad that" 12 30)
181 (animate-string "You've moved away." 13 30)
183 (animate-string "Let's talk together" 15 30)
185 (animate-string "And love more deeply." 16 30)
187 (animate-string "Please bring back" 17 30)
188 (animate-string "my sunshine" 18 34)
189 (animate-string "to stay!" 19 34))
191 ;;; arch-tag: 275289a3-6ac4-41da-b527-a1147045392f
192 ;;; animate.el ends here