Takumf

lesson-3.org

Jan 8th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. #+STARTUP: indent
  2. #+STARTUP: hidestars
  3. #+STARTUP: oddeven
  4.  
  5. * Lets get started with the magic!
  6. ** What is this Emacs Lisp (Elisp) anyway?
  7. Emacs is a customizable text editor, I hope you got the gist of it at this
  8. point and start to feel comfortable using it. Or at least, begin to wrap
  9. your head around.
  10. In the meantime, I requested from you to use commands inside the mini-
  11. buffer.
  12. What it was in essence, the execution of small commands from Emacs internal
  13. programming language. Small parts, but this is a natural starting point.
  14. This is actually the main power behind Emacs, all the text related tasks
  15. can be automated if you can only see the way to do it.
  16. *** Is it only about text?
  17. Actually, no. Elisp is perfectly capable of numerical computation and
  18. any other programming task you want. It might just not perform to the
  19. desired level.
  20. **** So what is the point?
  21. Languages made with single purpose behind them have the edge in task
  22. they were design for in mind, by person on people with particular need.
  23. In case of Emacs this purpose was automation of text-based tasks.
  24. It will not get to numerical speed of Fortran (Engineering language,
  25. one of the oldest still in use).
  26. It will not get the versatility of Python, Perl or Ruby out of the box.
  27. What it will do, is allowing you to program nearly all boring tasks
  28. that relate to manipulation on text. As a GM or player you are likely
  29. not interested above both points relating to Fortran and Python/Ruby/
  30. Perl. You will however see the merit of generating notes, NPCs, names,
  31. maps and many other things without the need to use internet or other
  32. external programs.
  33. ** OK, I'm kinda convinced. How do I do it?
  34. Open the file linked in the original post. Do it with split-sceen so that
  35. you will be able to track description here and source code simultaneously.
  36. Pretty neat feature after all.
  37. Before going further, please open mini-buffer and type:
  38.  
  39. linum-mode
  40.  
  41. if it was not enabled by defaults.
  42. *** Line 1
  43. All lines that start with ; are considered comments. They are not read
  44. while computer checks the program. General rule of thumb states:
  45. the more ; before comment the more important it is.
  46. *** Lines 3-5
  47. This is the very first function definition. It starts with defun,
  48. signing the fact that what is inside parentheses is, in fact, a function
  49. that you want to create.
  50. **** defun syntax
  51. (defun name-of-function (optional-arguments)
  52. "Description on purpose and usage of function."
  53. body of function)
  54.  
  55. What can get tricky is the parenthesis syntax. It works from innermost
  56. expression outward. Here are some examples:
  57.  
  58. (+ 1 2 3) => 6
  59. (- 3 (+ 2 1)) => (- 3 3) => 0
  60.  
  61. If you don't want your function to take any arguments, leave the
  62. optional-parameters as empty pair of parentheses: () just like in
  63. the example.
  64.  
  65. If your function does not take any parameters, don't add anything
  66. while executing it! (some-function) and (some-function ()) are totally
  67. different things!
  68.  
  69. However, I encourage you to later execute (d6 '()) and (d6 ()) in
  70. any way you like. Read the error messages, make sure you get the
  71. gist of the way Elisp wants to communicate problems. Insanely important
  72. skill, but even trained programmers can mess this part up.
  73.  
  74. **** How can I execute functions?
  75. Before executing anything you have to evaluate the function. To do so
  76. go to the end of the function definition (right after the closing
  77. parentheses) and hit:
  78.  
  79. C-x C-e
  80.  
  81. If everything is OK, the only thing that will happen should be the
  82. name of the function printed in the mini-buffer.
  83.  
  84. To execute it, there are two most popular ways:
  85. ***** Within the file
  86. Type somewhere in the file
  87.  
  88. (name-of-function optional-parameter-or-parameters)
  89.  
  90. and hit C-x C-e again, right after the closing paren.
  91. ***** Outside of the file
  92. Hit M-: (Left Alt and :), you will be prompted for name of function
  93. you want to execute. Type:
  94.  
  95. (name-of-function optional-parameters)
  96.  
  97. hit enter and watch the results.
  98. **** How does (d6) work?
  99. It first generates a random number. Function random takes one positive
  100. integer as the parameter and return an integer from 0 to parameter - 1
  101.  
  102. Example, possible outputs of (random 4) are:
  103. 0, 1, 2, 3
  104.  
  105. So to make d4 out of it, we have to add 1. Hence, in d6 function we
  106. first generate a number from 0 up to and including 5, then add 1 to
  107. the result. Try it out, make fabled d13 or other wacky dice.
  108. *** Lines 7-9
  109. Similar. We simply add outputs from calling d6 three times.
  110. Yes, +,-,* and / (add, subtract, multiply and divide) can take any number
  111. of inputs.
  112. *** Line 12
  113. This is how we define global variable. There are some things to note:
  114.  
  115. Asterisks in the name are not required, but they are a convention that
  116. should be respected. Other programmers will find it as a good marker
  117. if they would like to change or review the code themselves.
  118.  
  119. There is one peculiar thing after *stat-names*, sometimes hard to spot.
  120. In Elisp (and other Lisps, it is a whole family of programming languages
  121. that have other applications), it is a way to denote lists. *stat-names*
  122. is not just a value, it is a container with values. In this case, list
  123. of ability names.
  124. *** Lines 14-17
  125. Just as in documentation string, we will roll all statistics. 3d6 in
  126. order, grognard style.
  127. **** How does it work?
  128. We are using something that is a type of loop. Loops in programming
  129. are the means to iterate a particular procedure. In case of Elisp,
  130. one of the most basic and easiest was to do so is dolist loop.
  131. ***** dolist syntax
  132. (dolist (index list-you-iterate-through)
  133. what-you-want-to-do-with-index)
  134.  
  135. Index is simply a short version, a type of 'joker' that simply
  136. represents element of the list.
  137. ***** insert syntax
  138. (insert any-number-of-things-you-want-to-print-to-file)
  139.  
  140. As above. It simply inserts its arguments into the file where
  141. it is executed.
  142. ****** What is this "\t" and "\n"?
  143. These are specific types of characters that mean to most modern
  144. programming languages respectively a tabulator and new line.
  145. There are more, but these two are most common and important.
  146. ***** numbert-to-string syntax
  147. Honestly, works just as advertised. Takes a number and makes it into
  148. the string. For computers "0" and 0 are NOT the same things.
  149. *** Lines 19-23
  150. **** switch-to-buffer-other-window syntax
  151. (switch-to-buffer-other-window syntax "name-of-buffer-to-switch-to")
  152.  
  153. This is going to open new buffer window via split-screens of any
  154. name you want put as parameter.
  155. Further operations will be done in that buffer.
  156. **** erase-buffer
  157. Here it is commented out, so it will not be executed. If you remove
  158. ; from the source code it will, just as name suggests, erase all of
  159. the contents of the buffer you provided as parameter.
  160. **** We invocate generate-stats to print in buffer.
  161. **** We insert the new line, to separate results of generate-stats
  162. ** Do I must evaluate everything all the time?
  163. Hell no! If you already have an Elisp program you can just go to the
  164. mini-buffer and type
  165.  
  166. eval-buffer
  167.  
  168. And whole file will be evaluated. You can now call functions you defined
  169. any time you want.
  170. ** Is it all? All that writing and for what?
  171. Are you kidding me? This is only an introduction and most bare-bones
  172. generator possible. Here is a list of stuff we will add:
  173.  
  174. Ability score modifiers
  175.  
  176. HP, Attack, Equipment and other value tracker
  177.  
  178. Ability to keep the needed stuff about character to roll for stuff
  179.  
  180. Expanding this point to keep data on whole party and antagonists
  181.  
  182. Background generator
  183.  
  184. Skills
  185.  
  186. Possibility to advance the character
  187.  
  188. Adding option to simply generate from templates
  189.  
  190. Bunch of minions with already rolled initiative
  191.  
  192. NPCs with all of the above perks
  193.  
  194. Honestly, start adding your ideas to this list
  195.  
  196. Assert automatically if generated character is valid according to rules
  197.  
  198. ** I'm not really convinced. There are already programs to do that!
  199. Yes, but are there available for all systems? Plus you are now learning
  200. an important and versatile skill by doing something that, ultimately,
  201. will allow you to make programs on your own. With a text editor alone.
  202.  
  203. Less of a perk if you can program already, but I doubt you went through
  204. this tutorial knowing (E)lisp but not knowing Emacs or any previous idea
  205. how to make character generator. If I am mistaken, prove me wrong.
  206.  
  207. Ultimately, think about it like that: You are not learning how to use
  208. the editor, it is a toolbox. You can construct on your own program that
  209. will fit all your needs!
  210.  
  211. Imagine having your own program, that integrates (N)PC generator, random
  212. tables database that can be 'rolled' with a single command, time-line,
  213. plot notes and all other whatnot you use (or want to use) in your games.
  214. ** Is it worth the effort?
  215. I'm biased, but I think the answer is 'yes'. Provided you are actually
  216. interested. Even if you don't want to continue, you have basics of Emacs,
  217. rudimentary skill in org-mode and learned the bare minimum of Elisp to
  218. to write your own very small and basic programs if there will ever any
  219. need to make them.
  220. * How can I continue or get help?
  221. At any time, you can hit the following combination:
  222.  
  223. C-h f
  224.  
  225. and ask for definition of any function.
  226. * Are there more commands? Where can I find them?
  227. ** Get more commands
  228. Just to get the gist of amount of available commands, return to your
  229. Elisp source file, go to the bottom and hit ANY letter. Done? OK, now
  230. hit
  231.  
  232. C-M-i (Left Control + Left Alt + i)
  233.  
  234. and watch at the amount of functions available. This is auto-completion
  235. tool. You can go to the buffer that opened and browse them. You can
  236. ask for definitions (as above) or choose one you would pick anyway.
  237.  
  238. Try to rewrite this program on your own, but instead of typing functions,
  239. ask for auto-completion each time after typing 2-4 characters by yourself.
  240. * Nice, whats next?
  241. Next lessons are going to be either about more advanced features of org-mode
  242. or further augmenting the Basic d20 generator from today's example. If you
  243. want one more then another, comment in original thread.
Advertisement
Add Comment
Please, Sign In to add comment