animate.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ;;; animate.el --- make text dance
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: Richard Stallman <rms@gnu.org>
  4. ;; Keywords: games
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; (animate-string STRING VPOS &optional HPOS)
  18. ;; makes the string STRING appear starting at VPOS, HPOS
  19. ;; by having each letter swoop into place from random starting position.
  20. ;; animate-birthday-present was the first application of this program.
  21. ;;; Code:
  22. ;;; STRING is the string to be displayed,
  23. ;;; and DEST-X, DEST-Y say where on the screen
  24. ;;; it should end up.
  25. ;;; This function returns a list describing
  26. ;;; all the characters and the paths they should take.
  27. ;;; Each element has the form
  28. ;;; (CHAR START-Y START-X DEST-Y DEST-X).
  29. ;;; The start position of each character is chosen randomly.
  30. ;;; The destination is chosen to put it in the right place
  31. ;;; in the string when the whole string finally reaches its
  32. ;;; specified position.
  33. (defun animate-initialize (string vpos hpos)
  34. (let ((characters nil))
  35. (dotimes (i (length string))
  36. (setq characters
  37. (cons (list (aref string i)
  38. ;; Random starting positions.
  39. (random (window-height))
  40. (random (1- (window-width)))
  41. ;; All the chars should end up
  42. ;; on the specified line.
  43. vpos
  44. ;; The Ith character in the string
  45. ;; needs to end up I positions later.
  46. (+ hpos i))
  47. characters)))
  48. characters))
  49. ;;; Display the characters in CHARACTERS,
  50. ;;; each one FRACTION of the way from its start to its destination.
  51. ;;; If FRACTION is 0, the characters appear in their starting positions.
  52. ;;; If FRACTION is 1, the characters appear in their destinations.
  53. (defun animate-step (characters fraction)
  54. (let ((remains (- 1 fraction)))
  55. (dolist (item characters)
  56. (let ((vpos (+ (* remains (nth 1 item))
  57. (* fraction (nth 3 item))))
  58. (hpos (+ (* remains (nth 2 item))
  59. (* fraction (nth 4 item)))))
  60. (animate-place-char (car item) vpos hpos)))))
  61. ;;; Place the character CHAR at position VPOS, HPOS in the current buffer.
  62. (defun animate-place-char (char vpos hpos)
  63. (goto-char (window-start))
  64. (let (abbrev-mode)
  65. (dotimes (i vpos)
  66. (end-of-line)
  67. (if (= (forward-line 1) 1)
  68. (insert "\n"))))
  69. (beginning-of-line)
  70. (move-to-column (floor hpos) t)
  71. (unless (eolp) (delete-char 1))
  72. (insert-char char 1))
  73. (defvar animate-n-steps 10
  74. "*Number of steps `animate-string' will place a char before its last position.")
  75. (defvar animation-buffer-name nil
  76. "*String naming the default buffer for animations.
  77. When nil animations displayed in the buffer named *Animation*.")
  78. ;;;###autoload
  79. (defun animate-string (string vpos &optional hpos)
  80. "Display STRING animations starting at position VPOS, HPOS.
  81. The characters start at randomly chosen places,
  82. and all slide in parallel to their final positions,
  83. passing through `animate-n-steps' positions before the final ones.
  84. If HPOS is nil (or omitted), center the string horizontally
  85. in the current window."
  86. (let ((characters
  87. (animate-initialize string vpos
  88. (or hpos
  89. ;; HPOS unspecified, so compute
  90. ;; it so as to center the string.
  91. (max 0 (/ (- (window-width) (length string)) 2)))))
  92. (show-trailing-whitespace nil)
  93. ;; Make sure indentation does not use tabs.
  94. ;; They would confuse things.
  95. (indent-tabs-mode nil))
  96. (dotimes (i animate-n-steps)
  97. ;; Bind buffer-undo-list so it will be unchanged when we are done.
  98. ;; (We're going to undo all our changes anyway.)
  99. (let (buffer-undo-list
  100. list-to-undo)
  101. ;; Display the characters at the Ith position.
  102. ;; This inserts them in the buffer.
  103. (animate-step characters (/ i 1.0 animate-n-steps))
  104. ;; Make sure buffer is displayed starting at the beginning.
  105. (set-window-start nil 1)
  106. ;; Display it, and wait just a little while.
  107. (sit-for .05)
  108. ;; Now undo the changes we made in the buffer.
  109. (setq list-to-undo buffer-undo-list)
  110. (while list-to-undo
  111. (let ((undo-in-progress t))
  112. (setq list-to-undo (primitive-undo 1 list-to-undo))))))
  113. ;; Insert the characters in their final positions.
  114. (animate-step characters 1)
  115. ;; Put the cursor at the end of the text on the line.
  116. (end-of-line)
  117. ;; Redisplay so they appear on the screen there.
  118. (sit-for 0)
  119. ;; This is so that the undo command, used afterwards,
  120. ;; will undo the "animate" calls one by one.
  121. (undo-boundary)))
  122. ;;;###autoload
  123. (defun animate-sequence (list-of-strings space)
  124. "Display animation strings from LIST-OF-STRING with buffer *Animation*.
  125. Strings will be separated from each other by SPACE lines.
  126. When the variable `animation-buffer-name' is non-nil display
  127. animation in the buffer named by variable's value, creating the
  128. buffer if one does not exist."
  129. (let ((vpos (/ (- (window-height)
  130. 1 ;; For the mode-line
  131. (* (1- (length list-of-strings)) space)
  132. (length list-of-strings))
  133. 2)))
  134. (switch-to-buffer (get-buffer-create
  135. (or animation-buffer-name
  136. "*Animation*")))
  137. (erase-buffer)
  138. (sit-for 0)
  139. (while list-of-strings
  140. (animate-string (car list-of-strings) vpos)
  141. (setq vpos (+ vpos space 1))
  142. (setq list-of-strings (cdr list-of-strings)))))
  143. ;;;###autoload
  144. (defun animate-birthday-present (&optional name)
  145. "Return a birthday present in the buffer *Birthday-Present*.
  146. When optional arg NAME is non-nil or called-interactively, prompt for
  147. NAME of birthday present receiver and return a birthday present in
  148. the buffer *Birthday-Present-for-Name*."
  149. (interactive (list (read-string "Birthday present for: "
  150. nil nil)))
  151. ;; Make a suitable buffer to display the birthday present in.
  152. (switch-to-buffer (get-buffer-create
  153. (if name
  154. (concat "*A-Present-for-" (capitalize name) "*")
  155. "*Birthday-Present*")))
  156. (erase-buffer)
  157. ;; Display the empty buffer.
  158. (sit-for 0)
  159. (if name
  160. (animate-string "Happy Birthday," 6)
  161. (animate-string "Happy Birthday" 6))
  162. (when name (animate-string (format "%s" (capitalize name)) 7))
  163. (sit-for 1)
  164. (animate-string "You are my sunshine," 10 30)
  165. (sit-for .5)
  166. (animate-string "My only sunshine." 11 30)
  167. (sit-for .5)
  168. (animate-string "I'm awful sad that" 12 30)
  169. (sit-for .5)
  170. (animate-string "You've moved away." 13 30)
  171. (sit-for .5)
  172. (animate-string "Let's talk together" 15 30)
  173. (sit-for .5)
  174. (animate-string "And love more deeply." 16 30)
  175. (sit-for .5)
  176. (animate-string "Please bring back" 17 30)
  177. (animate-string "my sunshine" 18 34)
  178. (animate-string "to stay!" 19 34))
  179. (random t)
  180. (provide 'animate)
  181. ;;; animate.el ends here