elisp 笔记

李杀网

1.运行elisp code

C - x b 进入*scratch* buffer , scratch作为形容词有打草稿之意。键入下面的代码

(message "hi")

将光标移动到括号后面 C-x C-e,在最下面的mini buffer里面看到一个hi。(执行elisp方法2:C-j ;方法3:先选中要执行的代码,然后M-x eval-region)

可以使用转义字符

(message "Her age is: %d" 16)        ; %d is for number
(message "Her name is: %s" "Vicky")  ; %s is for string
(message "Her mid init is: %c" 86)   ; %c is for character in ascii code
(message "My list is: %S" '(8 2 3))  ; %S is for a list

2.数学运算符

(+ 4 5 1)     ;    ⇒ 10
(- 9 2)       ;    ⇒  7
(- 9 2 3)     ;    ⇒  4
(* 2 3)       ;    ⇒  6
(* 2 3 2)     ;    ⇒ 12
(/ 7 2)       ;    ⇒  3 (Integer part of quotient)
(/ 7 2.0)     ;    ⇒  3.5
(% 7 4)       ;    ⇒  3 (Remainder)
(expt 2 3)    ;    ⇒ 8

如果你希望结果不是取整数的话,需要加上小数位,像上面的

(/ 7 2.0)     ;    ⇒  3.5

下面的都为假

nil
()
'()
(list)

3.数据类型判断

(integerp 3.) ; returns t
(floatp 3.) ; returns nil
(floatp 3.0) ; returns t

t 表示真,nil表示假,相当javascript中的true,false。在elisp中,nil表示空表(),因此除了nil 和 () 为假其他的都为真

4.if else 在elisp 中的表示

(if nil "yes" "no") ; ⇒ "no"
(if () "yes" "no") ; ⇒ "no"
(if '() "yes" "no") ; ⇒ "no"
(if (list) "yes" "no") ; ⇒ "no"
(if t "yes" "no") ; ⇒ "yes"
(if 0 "yes" "no") ; ⇒ "yes"
(if "" "yes" "no") ; ⇒ "yes"
(if [] "yes" "no") ; ⇒ "yes". The [] is vector of 0 elements
5.比较运算符

5.1数值比较

(< 3 4) ; less than
(> 3 4) ; greater than
(<= 3 4) ; less or equal to
(>= 3 4) ; greater or equal to

(= 3 3)   ; ⇒ t
(= 3 3.0) ; ⇒ t

(/= 3 4) ; ⇒ t


(and t nil) ; ⇒ nil
(or t nil) ; ⇒ t

5.2字符串的比较

(string= "this" "this") ; ⇒ t. Case matters.
(string< "a" "b") ; ⇒ t. by lexicographic order.
(string< "B" "b") ; ⇒ t.

5.3equal 比较两个量的数据类型和值

;; testing if two values have the same datatype and value.
(equal "abc" "abc") ; ⇒ t
(equal 3 3) ; ⇒ t
(equal 3.0 3.0) ; ⇒ t
(equal 3 3.0) ; ⇒ nil. Because datatype doesn't match.

;; testing equality of lists
(equal '(3 4 5) '(3 4 5))  ; ⇒ t
(equal '(3 4 5) '(3 4 "5")) ; ⇒ nil

;; testing equality of symbols
(equal 'abc 'abc) ; ⇒ t

/=只用于数值比较,表示不相等。通用的不想等 用下面的记法

(not (= 3 4)) ; ⇒ t
(/= 3 4) ; ⇒ t. “/=” is for comparing numbers only

(not (equal 3 4)) ; ⇒ t. General way to test inequality.

6.全局变量和局部变量

6.1setq 定义全局变量

(setq x 1) ; assign 1 to x
(setq a 3 b 2 c 7) ; assign 3 to a, 2 to b, 7 to c

6.2let 定义局部变量

let 定义变量方式1

(let (a b)
 (setq a 3)
 (setq b 4)
 (+ a b)
) ; returns 7

let 定义变量方式2

(let ((a 3) (b 4))
 (+ a b)
) ; returns 7

if else 中使用块,即类似于

if(exp){

}else{

}

elisp 中用()表示{}

(if (< 3 2) (message "yes") (message "no") )

7.while循环

(setq x 0)

(while (< x 4)
  (princ (format "yay %d." x))
  (setq x (1+ x)))

这里出现了一个新函数 princ 不知道有何用,这段程序没有明白

(let ((x 32))
  (while (< x 127)
    (ucs-insert x)
    (setq x (+ x 1))))

在光标处插入32~127的unicode字符

elisp没有for形式的循环,因为没那个需要,for循环能做的while循环一样能做

8.函数

8.1定义一个函数

(defun myFunction () "testing" (message "Yay!") )

这个函数只能在其它lisp程序中能够调用执行。如果想以交互模式运行,需要加上 (interactive) 

  (defun myFunction2 ()  "testing"  (interactive)  (message "Yay!") )

   定义完成后执行它们 C-x C-e

8.2调用函数

M-x myFunction

M-x myFunction2 ->Yay!

能够执行的只有2。

8.3带参数的函数

(defun myFunction (myArg)
  "Prints the argument"
  (interactive "p")
  (message "Your argument is: %d" myArg)
)

8.4执行带参数的函数,并传入参数7

C-u 7 M -x myFunction

8.5执行带参数的函数,并且以选区作为参数 M-x myFunction

(defun myFunction (myStart myEnd)  "Prints region start and end positions"  (interactive "r")  (message "Region begin at: %d, end at: %d" myStart myEnd))

9.总之,(interactive)让你写的程序能以M-x的形式调用,非常方便调试用。并且可以带参数,指定调用形式

  • (interactive), 不带参数,一交互方式调用
  • (interactive "n"), 数字参数,可以带字符串,看下例. (prompt string can follow right after "n" as part of the string, like this: (interactive "nWhat is your age?").)
  • (interactive "s"), prompt user for a string as argument.
  • (interactive "r"), for commands that takes 2 arguments, the beginning and ending positions of the curr
  • (interactive "r") , 预先传入参数

举例

例子1

(defun myFunction (myArg)  "Prints the argument"  (interactive "nWhat is your age?")  (message "Your argument is: %d" myArg))

执行过程

C-x C-e

M-x myFunction

对比下面的传入参数的方式——下面是预先传入参数

(defun myFunction (myArg)  "Prints the argument"  (interactive "p")  (message  myArg))

C-x C-e

C-u 25 M-x

例子2

(defun myFunction (myArg)  "Prints the argument"  ;; (interactive "nWhat is your age?")  (interactive "sleave?")  (message  myArg))

10.函数模板

最后一个表达式作为返回值,注意区别于其他语言比如javascript需要显示的return

(defun myFunction (arg1 arg2 …)
  "One sentence summary of what this command do.
More detailed documentation here."
  (interactive)
  (let (localVar1 localVar2 …)
    ; do something here …
    ; …
    ; last expression is returned
  )
)
原文地址:https://www.cnblogs.com/wewe/p/2077542.html