加载中...

elisp


其中 Y=elisp

源代码下载: learn-emacs-lisp-zh.el

  1. ;; 15分钟学会Emacs Lisp (v0.2a)
  2. ;;(作者:bzghttps://github.com/bzg
  3. ;; 译者:lichenbohttp://douban.com/people/lichenbo)
  4. ;;
  5. ;; 请先阅读Peter Norvig的一篇好文:
  6. ;; http://norvig.com/21-days.html
  7. ;; (译者注:中文版请见http://blog.youxu.info/21-days/)
  8. ;;
  9. ;; 之后安装GNU Emacs 24.3:
  10. ;;
  11. ;; Debian: apt-get install emacs (视具体发行版而定)
  12. ;; MacOSX: http://emacsformacosx.com/emacs-builds/Emacs-24.3-universal-10.6.8.dmg
  13. ;; Windows: http://ftp.gnu.org/gnu/windows/emacs/emacs-24.3-bin-i386.zip
  14. ;;
  15. ;; 更多信息可以在这里找到:
  16. ;; http://www.gnu.org/software/emacs/#Obtaining
  17. ;; 很重要的警告:
  18. ;;
  19. ;; 按照这个教程来学习并不会对你的电脑有任何损坏
  20. ;; 除非你自己在学习的过程中愤怒地把它砸了
  21. ;; 如果出现了这种情况,我不会承担任何责任
  22. ;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;; 打开emacs
  26. ;;
  27. ;; 'q'消除欢迎界面
  28. ;;
  29. ;; 现在请注意窗口底部的那一个灰色长条
  30. ;;
  31. ;; "*scratch*" 是你现在编辑界面的名字。
  32. ;; 这个编辑界面叫做一个"buffer"
  33. ;;
  34. ;; 每当你打开Emacs时,都会默认打开这个scratch buffer
  35. ;; 此时你并没有在编辑任何文件,而是在编辑一个buffer
  36. ;; 之后你可以将这个buffer保存到一个文件中。
  37. ;;
  38. ;; 之后的"Lisp interaction" 则是表明我们可以用的某组命令
  39. ;;
  40. ;; Emacs在每个buffer中都有一组内置的命令
  41. ;; 而当你激活某种特定的模式时,就可以使用相应的命令
  42. ;; 这里我们使用`lisp-interaction-mode',
  43. ;; 这样我们就可以使用内置的Emacs Lisp(以下简称Elisp)命令了。
  44. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45. ;;
  46. ;; 分号是注释开始的标志
  47. ;;
  48. ;; Elisp 是由符号表达式构成的 (即"s-表达式"或"s式"):
  49. (+ 2 2)
  50. ;; 这个s式的意思是 "对2进行加2操作".
  51. ;; s式周围有括号,而且也可以嵌套:
  52. (+ 2 (+ 1 1))
  53. ;; 一个s式可以包含原子符号或者其他s式
  54. ;; 在上面的例子中,1和2是原子符号
  55. ;; (+ 2 (+ 1 1)) 和 (+ 1 1) 是s式.
  56. ;; 在 `lisp-interaction-mode' 中你可以计算s式.
  57. ;; 把光标移到闭括号后,之后按下ctrl+j(以后简写为'C-j')
  58. (+ 3 (+ 1 2))
  59. ;; ^ 光标放到这里
  60. ;; 按下`C-j' 就会输出 6
  61. ;; `C-j' 会在buffer中插入当前运算的结果
  62. ;; 而`C-xC-e' 则会在emacs最底部显示结果,也就是被称作"minibuffer"的区域
  63. ;; 为了避免把我们的buffer填满无用的结果,我们以后会一直用`C-xC-e'
  64. ;; `setq' 可以将一个值赋给一个变量
  65. (setq my-name "Bastien")
  66. ;; `C-xC-e' 输出 "Bastien" (在 mini-buffer 中显示)
  67. ;; `insert' 会在光标处插入字符串:
  68. (insert "Hello!")
  69. ;; `C-xC-e' 输出 "Hello!"
  70. ;; 在这里我们只传给了insert一个参数"Hello!", 但是
  71. ;; 我们也可以传给它更多的参数,比如2个:
  72. (insert "Hello" " world!")
  73. ;; `C-xC-e' 输出 "Hello world!"
  74. ;; 你也可以用变量名来代替字符串
  75. (insert "Hello, I am " my-name)
  76. ;; `C-xC-e' 输出 "Hello, I am Bastien"
  77. ;; 你可以把s式嵌入函数中
  78. (defun hello () (insert "Hello, I am " my-name))
  79. ;; `C-xC-e' 输出 hello
  80. ;; 现在执行这个函数
  81. (hello)
  82. ;; `C-xC-e' 输出 Hello, I am Bastien
  83. ;; 函数中空括号的意思是我们不需要接受任何参数
  84. ;; 但是我们不能一直总是用my-name这个变量
  85. ;; 所以我们现在使我们的函数接受一个叫做"name"的参数
  86. (defun hello (name) (insert "Hello " name))
  87. ;; `C-xC-e' 输出 hello
  88. ;; 现在我们调用这个函数,并且将"you"作为参数传递
  89. (hello "you")
  90. ;; `C-xC-e' 输出 "Hello you"
  91. ;; 成功!
  92. ;; 现在我们可以休息一下
  93. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  94. ;;
  95. ;; 下面我们在新的窗口中新建一个名为 "*test*" 的buffer:
  96. (switch-to-buffer-other-window "*test*")
  97. ;; `C-xC-e' 这时屏幕上会显示两个窗口,而光标此时位于*test* buffer内
  98. ;; 用鼠标单击上面的buffer就会使光标移回。
  99. ;; 或者你可以使用 `C-xo' 使得光标跳到另一个窗口中
  100. ;; 你可以用 `progn'命令将s式结合起来:
  101. (progn
  102. (switch-to-buffer-other-window "*test*")
  103. (hello "you"))
  104. ;; `C-xC-e' 此时屏幕分为两个窗口,并且在*test* buffer中显示"Hello you"
  105. ;; 现在为了简洁,我们需要在每个s式后面都使用`C-xC-e'来执行,后面就不再说明了
  106. ;; 记得可以用过鼠标或者`C-xo'回到*scratch*这个buffer。
  107. ;; 清除当前buffer也是常用操作之一:
  108. (progn
  109. (switch-to-buffer-other-window "*test*")
  110. (erase-buffer)
  111. (hello "there"))
  112. ;; 也可以回到其他的窗口中
  113. (progn
  114. (switch-to-buffer-other-window "*test*")
  115. (erase-buffer)
  116. (hello "you")
  117. (other-window 1))
  118. ;; 你可以用 `let' 将一个值和一个局部变量绑定:
  119. (let ((local-name "you"))
  120. (switch-to-buffer-other-window "*test*")
  121. (erase-buffer)
  122. (hello local-name)
  123. (other-window 1))
  124. ;; 这里我们就不需要使用 `progn' 了, 因为 `let' 也可以将很多s式组合起来。
  125. ;; 格式化字符串的方法:
  126. (format "Hello %s!\n" "visitor")
  127. ;; %s 是字符串占位符,这里被"visitor"替代.
  128. ;; \n 是换行符。
  129. ;; 现在我们用格式化的方法再重写一下我们的函数:
  130. (defun hello (name)
  131. (insert (format "Hello %s!\n" name)))
  132. (hello "you")
  133. ;; 我们再用`let'新建另一个函数:
  134. (defun greeting (name)
  135. (let ((your-name "Bastien"))
  136. (insert (format "Hello %s!\n\nI am %s."
  137. name ; the argument of the function
  138. your-name ; the let-bound variable "Bastien"
  139. ))))
  140. ;; 之后执行:
  141. (greeting "you")
  142. ;; 有些函数可以和用户交互:
  143. (read-from-minibuffer "Enter your name: ")
  144. ;; 这个函数会返回在执行时用户输入的信息
  145. ;; 现在我们让`greeting'函数显示你的名字:
  146. (defun greeting (from-name)
  147. (let ((your-name (read-from-minibuffer "Enter your name: ")))
  148. (insert (format "Hello!\n\nI am %s and you are %s."
  149. from-name ; the argument of the function
  150. your-name ; the let-bound var, entered at prompt
  151. ))))
  152. (greeting "Bastien")
  153. ;; 我们让结果在另一个窗口中显示:
  154. (defun greeting (from-name)
  155. (let ((your-name (read-from-minibuffer "Enter your name: ")))
  156. (switch-to-buffer-other-window "*test*")
  157. (erase-buffer)
  158. (insert (format "Hello %s!\n\nI am %s." your-name from-name))
  159. (other-window 1)))
  160. ;; 测试一下:
  161. (greeting "Bastien")
  162. ;; 第二节结束,休息一下吧。
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;;
  165. ;; 我们将一些名字存到列表中:
  166. (setq list-of-names '("Sarah" "Chloe" "Mathilde"))
  167. ;; 用 `car'来取得第一个名字:
  168. (car list-of-names)
  169. ;; `cdr'取得剩下的名字:
  170. (cdr list-of-names)
  171. ;; 用 `push'把名字添加到列表的开头:
  172. (push "Stephanie" list-of-names)
  173. ;; 注意: `car' `cdr' 并不修改列表本身, 但是 `push' 却会对列表本身进行操作.
  174. ;; 这个区别是很重要的: 有些函数没有任何副作用(比如`car'
  175. ;; 但还有一些却是有的 (比如 `push').
  176. ;; 我们来对`list-of-names'列表中的每一个元素都使用hello函数:
  177. (mapcar 'hello list-of-names)
  178. ;; `greeting' 改进,使的我们能够对`list-of-names'中的所有名字执行:
  179. (defun greeting ()
  180. (switch-to-buffer-other-window "*test*")
  181. (erase-buffer)
  182. (mapcar 'hello list-of-names)
  183. (other-window 1))
  184. (greeting)
  185. ;; 记得我们之前定义的 `hello' 函数吗? 这个函数接受一个参数,名字。
  186. ;; `mapcar' 调用 `hello', 并将`list-of-names'作为参数先后传给`hello'
  187. ;; 现在我们对显示的buffer中的内容进行一些更改:
  188. (defun replace-hello-by-bonjour ()
  189. (switch-to-buffer-other-window "*test*")
  190. (goto-char (point-min))
  191. (while (search-forward "Hello")
  192. (replace-match "Bonjour"))
  193. (other-window 1))
  194. ;; (goto-char (point-min)) 将光标移到buffer的开始
  195. ;; (search-forward "Hello") 查找字符串"Hello"
  196. ;; (while x y) 当x返回某个值时执行y这个s式
  197. ;; 当x返回`nil' (空), 退出循环
  198. (replace-hello-by-bonjour)
  199. ;; 你会看到所有在*test* buffer中出现的"Hello"字样都被换成了"Bonjour"
  200. ;; 你也会得到以下错误提示: "Search failed: Hello".
  201. ;;
  202. ;; 如果要避免这个错误, 你需要告诉 `search-forward' 这个命令是否在
  203. ;; buffer的某个地方停止查找, 并且在什么都没找到时是否应该不给出错误提示
  204. ;; (search-forward "Hello" nil t) 可以达到这个要求:
  205. ;; `nil' 参数的意思是 : 查找并不限于某个范围内
  206. ;; `t' 参数的意思是: 当什么都没找到时,不给出错误提示
  207. ;; 在下面的函数中,我们用到了s式,并且不给出任何错误提示:
  208. (defun hello-to-bonjour ()
  209. (switch-to-buffer-other-window "*test*")
  210. (erase-buffer)
  211. ;; `list-of-names'中的每个名字调用hello
  212. (mapcar 'hello list-of-names)
  213. (goto-char (point-min))
  214. ;; 将"Hello" 替换为"Bonjour"
  215. (while (search-forward "Hello" nil t)
  216. (replace-match "Bonjour"))
  217. (other-window 1))
  218. (hello-to-bonjour)
  219. ;; 给这些名字上个色:
  220. (defun boldify-names ()
  221. (switch-to-buffer-other-window "*test*")
  222. (goto-char (point-min))
  223. (while (re-search-forward "Bonjour \\(.+\\)!" nil t)
  224. (add-text-properties (match-beginning 1)
  225. (match-end 1)
  226. (list 'face 'bold)))
  227. (other-window 1))
  228. ;; 这个函数使用了 `re-search-forward':
  229. ;; 和查找一个字符串不同,你用这个命令可以查找一个模式,即正则表达式
  230. ;; 正则表达式 "Bonjour \\(.+\\)!" 的意思是:
  231. ;; 字符串 "Bonjour ", 之后跟着
  232. ;; 一组 | \\( ... \\) 结构
  233. ;; 任意字符 | . 的含义
  234. ;; 有可能重复的 | + 的含义
  235. ;; 之后跟着 "!" 这个字符串
  236. ;; 准备好了?试试看。
  237. (boldify-names)
  238. ;; `add-text-properties' 可以添加文字属性, 比如文字样式
  239. ;; 好的,我们成功了!
  240. ;; 如果你想对一个变量或者函数有更多的了解:
  241. ;;
  242. ;; C-h v 变量 回车
  243. ;; C-h f 函数 回车
  244. ;;
  245. ;; 阅读Emacs Lisp官方文档:
  246. ;;
  247. ;; C-h i m elisp 回车
  248. ;;
  249. ;; 在线阅读Emacs Lisp文档:
  250. ;; https://www.gnu.org/software/emacs/manual/html_node/eintr/index.html
  251. ;; 感谢以下同学的建议和反馈:
  252. ;; - Wes Hardaker
  253. ;; - notbob
  254. ;; - Kevin Montuori
  255. ;; - Arne Babenhauserheide
  256. ;; - Alan Schmitt

还没有评论.