Elisp

Elisp

Elisp

欲和emacs和谐共处,就必须了解和emacs沟通的语言, Elisp。来一起看看Elisp的世界吧!

Lisp

先了解下lisp , 百度百科lisp词条,可以发现lisp这门语言诞生于1958年,维基中文lisp词条, 现代最著名的通用编程语种是Clojure、Common Lisp和Scheme,还有编辑器衍生的基于Common Lisp的Emacs_Lisp

lisp特性

  • 函数性 一般的程序都是按照顺序执行,lisp这种是函数型语言,通过函数的表达式运行就是结果
  • 递归性
  • 数据和程序的一致性
  • 自动进行存储分配
  • 语法简单

lisp语言相关

https://zh.wikipedia.org/wiki/LISP https://learnxinyminutes.com/docs/zh-cn/elisp-cn/

以elisp为例,C-x b进scratch的buffer,输入表达式,C-x C-e即可执行elisp语句

操作符

elisp由符号表达式组成,s式被()包裹 比如(+ 1 3)代表对1进行+3操作得4 支持嵌套(+ 1 (- 5 2))也是4

赋值
1
  (setq name "Liuliancao")
Liuliancao

全局变量defvar

1
2
3
4
5
6
7
;; use defvar
(defvar *num* '(1 2 3))
(push 1 *num*)
;; use set defines the list with a name num
(set 'num '(1 2 3))
;; contenate
(cons 'c num)
光标处插入字符
1
2
  (insert "I am the cursor~")
  (insert " Hello," name) ;; 支持变量

函数insert功能查询,使用C-h f,输入insert,按RET

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
insert is a built-in function in ‘C source code’.

(insert &rest ARGS)

  Probably introduced at or before Emacs version 1.1.

Insert the arguments, either strings or characters, at point.
Point and after-insertion markers move forward to end up
 after the inserted text.
Any other markers at the point of insertion remain before the text.

If the current buffer is multibyte, unibyte strings are converted
to multibyte for insertion (see ‘string-make-multibyte’).
If the current buffer is unibyte, multibyte strings are converted
to unibyte for insertion (see ‘string-make-unibyte’).

When operating on binary data, it may be necessary to preserve the
original bytes of a unibyte string when inserting it into a multibyte
buffer; to accomplish this, apply ‘string-as-multibyte’ to the string
and insert the result.
函数

使用(defun FUNCTION_NAME (PARAMS1 PARAMS2 …) (S-EXPRESSION))方式创建函数

使用(FUNCTION_NAME)调用函数

1
2
  (defun hello_liuliancao () (insert " Hello, liuliancao."))
  (hello_liuliancao)
1
2
  (defun hello(name1 name2) (insert " Hello, " name1 "," name2))
  (hello "liuliancao" "benben")
emacs里面新建一个叫liuliancao_test的buffer
1
  (switch-to-buffer-other-window "*liuliancao_test*")
使用progn

progn执行所有语句,相当于一个语句块

1
2
3
4
5
  (defun hello (name) (insert "Emacs love you, " name "."))
  (progn
    (switch-to-buffer-other-window "*liuliancao_test*")
    (hello "liuliancao")
    )
清除buffer
1
2
3
4
  (progn
    (insert "test")
    (erase-buffer)
    (insert "hello"))

**

列表list
1
2
3
  (list 'hello (+ 1 1) "times")
  (quote (2 "times"))
  '(2 "times")

上面三种都可以表示列表

针对列表,我们可以进行一些运算

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
;; get first of the list
(car '(3 2 1))
;; get excluded first of the list
(cdr '(3 2 1))
;; contenate two elements, you can also use mediate dot
(cons 3 '(2 1))
'(3 . (2 1))
;; get second elements
(cadr '(3 2 1))
(nth 1 '(3 2 1))
;; is in
(member 'b '(a b c))
(memq '(a) '((a)(z)))
(memq 'a '(a b))
;; add keyword
(defvar sre (list :name "Liuliancao" :tel "123"))
(format "%s" sre)

Elisp的示例代码

小试牛刀

保存所有modifed的buffer

写一个函数,kill掉unmodified buffers

参考文档