home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / amiga / vim46bin.lha / vim-4.6 / macros / life / life.mac < prev   
Encoding:
Text File  |  1996-10-13  |  7.0 KB  |  251 lines

  1. " Macros to play Conway's Game of Life in vi
  2. " Version 1.0m: edges wrap
  3. " by Eli-the-Bearded (eli@netusa.net), Sept 1996
  4. " This file may be free distributed so long as these credits remain unchanged.
  5. "
  6. " Modified by Bram Moolenaar (mool@oce.nl), 1996 Sept 10
  7. " - Made it quite a bit faster, but now needs search patterns in the text
  8. " - Changed the order of mappings to top-down.
  9. " - Made "g" run the whole thing, "C" run one generation.
  10. " - Added support for any uppercase character instead of 'X'
  11. "
  12. " Rules:
  13. "   If a germ has 0 or 1 live neighbors it dies of loneliness
  14. "   If a germ has 2 or 3 live neighbors it survives
  15. "   If a germ has 4 to 8 live neighbors it dies of starvation
  16. "   If an empty box has 3 live neighbors a new germ is born
  17. "
  18. "   A new born germ is an "A".    Every generation it gets older: B, C, etc.
  19. "   A germ dies of old age when it reaches "Z".
  20. "
  21. " Notice the rules do not mention edges. This version has the edges wrap
  22. " around. I have an earlier version that offers the option of live edges or
  23. " dead edges. Email me if you are interested. -Eli-
  24. "
  25. " Note: This is slow!  One generation may take up to ten minutes (depends on
  26. " your computer and the vi version).
  27. "
  28. " Quite a lot of the messy stuff is to work around the vi error "Can't yank
  29. " inside global/macro".  Still doesn't work for all versions of vi.
  30. "
  31. " To use these macros:
  32. "
  33. " vi        start vi/vim
  34. "
  35. " :so life.mac    Source this file
  36. "
  37. " g        'g'o!  runs everything until interrupted: "IR".
  38. "
  39. " I        Initialize everything. A board will be drawn at the end
  40. "        of the current buffer. All line references in these macros
  41. "        are relative to the end of the file and playing the game
  42. "        can be done safely with any file as the current buffer.
  43. "
  44. "    Change the left field with spaces and uppercase letters to suit
  45. "    your taste.
  46. "
  47. " C        'C'ompute one generation.
  48. " +        idem, time running one generation.
  49. " R        'R'un 'C'ompute until interrupted.
  50. " 1Gi<nr><Esc>z    Make a number the only thing on the first line and use
  51. "        'z' to time that many generations.
  52. "
  53. " Time to run ten generations on my 100Mhz 486 (FreeBSD):
  54. "   vi      3.7        3 min 54 sec
  55. "   vim   4.5        1 min 45 sec
  56. "   nvi   1.78        1 min 02 sec
  57. "   Elvis 1.8pl4    failed
  58. "   Elvis 2.0        failed
  59. "
  60. "
  61. " And now the macros, more or less in top-down order.
  62. "
  63. "  ----- macros that can be used by the human -----
  64. "
  65. " 'g'o: 'I'nitialize and then 'R'un 'C'ompute recursively (used by the human)
  66. map g IR
  67. "
  68. "
  69. " 'R'un 'C'ompute recursively (used by the human and 'g'o)
  70. map R CV
  71. " work around "tail recursion" problem in vi, "V" == "R".
  72. map V R
  73. "
  74. "
  75. " 'I'nitialize the board (used by the human and 'g'o)
  76. map I G)0)0)0)0)1)0)0)2)0)0)0)0,ok,-11k,-,R,IIN
  77. "
  78. "
  79. " 'C'ompute next generation (used by the human and others)
  80. map C T>>>>>>>>B&
  81. "
  82. "
  83. " Time running one generation (used by the human)
  84. map + <1C<2
  85. "
  86. "
  87. " Time running N generations, where N is the number on the current line.
  88. " (used by the human)
  89. map z ,^,&,*,&<1,*<2
  90. "
  91. "  ----- END of macros that can be used by the human -----
  92. "
  93. "  ----- Initialisation -----
  94. "
  95. map ,- :s/./-/g
  96. map ,o oPut 'X's in the left box, then hit 'C' or 'R'
  97. map ,R 03stop
  98. "
  99. " Write a new line (used by 'I'nitialize board)
  100. map )0 o-                    --....................--....................-
  101. map )1 o-        VIM         --....................--....................-
  102. map )2 o-       LIVES        --....................--....................-
  103. "
  104. "
  105. " Initialisation of the pattern/command to execute for working out a square.
  106. " Pattern is: "#<germ><count>"
  107. " where <germ>   is " " if the current germ is dead, "X" when living.
  108. "       <count>  is the number of living neighbours (including current germ)
  109. "                expressed in X's
  110. "
  111. map ,Il8 O#XXXXXXXXXX .`a22lr 
  112. map ,Id8 o# XXXXXXXX .`a22lr 
  113. map ,Il7 o#XXXXXXXXX .`a22lr 
  114. map ,Id7 o# XXXXXXX .`a22lr 
  115. map ,Il6 o#XXXXXXXX .`a22lr 
  116. map ,Id6 o# XXXXXX .`a22lr 
  117. map ,Il5 o#XXXXXXX .`a22lr 
  118. map ,Id5 o# XXXXX .`a22lr 
  119. map ,Il4 o#XXXXXX .`a22lr 
  120. map ,Id4 o# XXXX .`a22lr 
  121. map ,Il3 o#XXXXX .,a
  122. map ,Id3 o# XXX .`a22lrA
  123. map ,Il2 o#XXXX .,a
  124. map ,Id2 o# XX .`a22lr 
  125. map ,Il1 o#XXX .`a22lr 
  126. map ,Id1 o# X .`a22lr 
  127. map ,Il0 o#XX .`a22lr 
  128. map ,Id0 o#  .`a22lr 
  129. "
  130. " Patterns used to replace a germ with it's next generation
  131. map ,Iaa o=AB =BC =CD =DE =EF =FG =GH =HI =IJ =JK =KL =LM =MN =NO =OP =PQ =QR
  132. map ,Iab o=RS =ST =TU =UV =VW =WX =XY =YZ =Z 
  133. "
  134. " Insert the searched patterns above the board
  135. map ,IIN G?^top
  136. ,Il8,Id8,Il7,Id7,Il6,Id6,Il5,Id5,Il4,Id4,Il3,Id3,Il2,Id2,Il1,Id1,Il0,Id0,Iaa,Iab
  137. "
  138. "  ----- END of Initialisation -----
  139. "
  140. "  ----- Work out one line -----
  141. "
  142. " Work out 'T'op line (used by show next)
  143. map T G,c2k,!9k,@,#j>2k,$j
  144. "
  145. " Work out 'B'ottom line (used by show next)
  146. map B ,%k>,$
  147. "
  148. " Work out a line (used by show next, work out top and bottom lines)
  149. map > 0 LWWWWWWWWWWWWWWWWWW,rj
  150. "
  151. " Refresh board (used by show next)
  152. map & :%s/^\(-[ A-Z]*-\)\(-[ A-Z]*-\)\(-[.]*-\)$/\2\3\3/
  153. "
  154. "
  155. " Work around vi multiple yank/put in a single macro limitation
  156. " (used by work out top and/or bottom line)
  157. map ,$ dd
  158. map ,% "cp
  159. map ,! "byy
  160. map ,@ "cyy
  161. map ,# "bP
  162. map ,c c$
  163. "
  164. "  ----- END of Work out one line -----
  165. "
  166. "  ----- Work out one square -----
  167. "
  168. " The next three work out a square: put all nine chars around the current
  169. " character on the bottom line (the bottom line must be empty when starting).
  170. "
  171. " 'W'ork out a center square (used by work out line)
  172. map W makh,3`ah,3`ajh,3(
  173. "
  174. "
  175. " Work out a 'L'eft square (used by work out line)
  176. map L makf-h,1`ak,2`af-h,1`a,2`ajf-h,1`aj,2(
  177. "
  178. "
  179. " Work out a 'R'ight square (used by work out line)
  180. map ,r makh,2`akF-l,1`ah,2`aF-l,1`ajh,2`ajF-l,1(
  181. "
  182. " 'M'ove a character to the end of the file (used by all work out square
  183. " macros)
  184. "
  185. map ,1 y G$p
  186. map ,2 2y G$p
  187. map ,3 3y G$p
  188. "
  189. "
  190. "  ----- END of Work out one square -----
  191. "
  192. "  ----- Work out one germ -----
  193. "
  194. " Generate an edit command that depends on the number of living in the last
  195. " line, and then run the edit command. (used by work out square).
  196. " Leaves the cursor on the next character to be processed.
  197. "
  198. map ( ,s,i,X0i?^#A 
  199. ,df.l,Y21h
  200. "
  201. " Delete 's'paces (deads);
  202. " The number of remaining characters is the number of living neighbours.
  203. map ,s :.g/ /s///g
  204. "
  205. " Insert current character in the last line
  206. map ,i `ay GP
  207. "
  208. " Replace any uppercase letter with 'X';
  209. map ,X :.g/[A-Z]/s//X/g
  210. "
  211. " Delete and execute the rest of the line
  212. map ,d "qd$@q
  213. "
  214. " Yank and execute the rest of the line
  215. map ,Y "qy$@q
  216. "
  217. " Yank the character under the cursor
  218. map ,j y 
  219. "
  220. " Put the current cut buffer after the cursor 
  221. map ,m p
  222. "
  223. " Delete the character under the cursor 
  224. map ,n x
  225. "
  226. " Replace a character by it's next, A --> B,  B --> C, etc.
  227. map ,a `a,jGi?=,ma
  228. ,dll,j`a21l,ml,nh
  229. "
  230. "  ----- END of Work out one germ -----
  231. "
  232. "  ----- timing macros  -----
  233. "
  234. " Get current date (used by time a generation)
  235. map << :r!date
  236. map <1 G?^top
  237. O<<
  238. map <2 G?^top
  239. k<<
  240. "
  241. "
  242. " Turn number on current line into edit command (used by time N generations)
  243. map ,^ AiC
  244. "
  245. "
  246. " Delete current line and save current line (used by time N generations)
  247. map ,& 0"gd$
  248. "
  249. "
  250. " Run saved line (used by time N generations)
  251. map ,* @g
  252. "
  253. "  ----- END of timing macros  -----
  254. "
  255. " End of the macros.
  256.