自定义函数
(defun multi-by-seven (number) "multi number by seven" (interactive "p") (message "result is %d" (* 7 number)))
- defun 是定义函数的关键字
- multi-by-sever 是函数的名字
- (number) 是函数的参数,必须有括号
- “multi number by seven”是函数的注释,当用C-h f,输入函数名字后,注释会显示出来
- (interactive "p") 说明是和emacs交互的函数, 参数p的做用是,告诉emacs,这个函数的参数number是须要用C-u来设定
- (message "result is %d" (* 7 number))是函数的实体。message的做用是,把result is %d显示到用户信息行
用法
1,C-u后,输入一个数字(这个数字就是传递个函数的参数),并键入M-x multi-by-seven,回车express
若是在C-u后,输入的数字是3,则在用户信息行,显示:result is 21.函数
若是在C-u后,没有输入任何数字,则取默认值4,因此显示:result is 28.测试
2,M后,输入一个数字(这个数字就是传递个函数的参数),并键入M-x multi-by-seven,回车this
若是在M后,输入的数字是5,则在用户信息行,显示:result is 35spa
let 表达式
let表达式模板:(let varlist body ...)code
(let ((var1 value1)get
(var2 value2))emacs
(it
;bodyio
))
(let ((zebra 'str1) (tiger 'str2) ) (message "one %s %s" zebra tiger)) ;执行结果:one str1 str2 (let ((birch 3) pine fir (oak "some")) (message "Here are %d variables with %s, %s, and %s value" birch pine fir oak)) ;执行结果:Here are 3 variables with nil, nil, and some value
if 表达式
nil为false,其他都为true
(if true-or-false-test action-to-carry-out-if-test-is-true) (if (> 5 4) ;if-part (message "5 is greater than 4!")) ;then-part
(defun iftest (str) "this is if test code" (if (equal str 'da) (message "this is %s" str)) ) (iftest 'da) (iftest 'da1)
if-else 表达式
nil为false,其他都为true
(if true-or-false-test action-to-carry-out-if-test-is-true action-to-carry-out-if-test-is-false) (if (> 4 5) ;if-part (message "5 is greater than 4!") ;then-part (message "4 is not greater than 5!")) ;else-part
(defun iftest (str) "this is if test code" (if (equal str 'da) (message "this is %s" str) (message "this is not %s" str)) ) (iftest 'da) (iftest 'da1)
nil
nil有2种意思,第一种为(),第二种为false
(if () 'true 'false) ;结果为false
(if nil 'true 'false) ;结果为false
(if 4 'true 'false) ;结果为tre
(if (buffer-size) 'true 'false) ;结果为tre
or 函数
or函数能够有不少参数,它逐一对每个参数求值,并返回第一个其值不是nil的参数的值。一旦遇到其值不是nil的参数后,后面的参数就不会被求值。
(defun simple-inser-buffer (buffer) "simple insert buffer" (interactive "*binsert buffer:") (or (bufferp buffer) (setq buffer (get-buffer buffer)))
and 函数
and函数能够有不少参数,它逐一对每个参数求值,一旦遇到其值是nil的参数后,后面的参数就不会被求值。
save-excursion 函数
这个函数将当前的位点(point)和标记(mark)保存起来,若是在函数体里面,改变了位点和标记的值,函数执行结束后,把位点和标记恢复成函数执行前的值。这个函数的主要目的是使用户避免位点和标记的没必要要的移动。
(save-excursion first-expression-in-body second-expression-in-body third-expression-in-body ... last-expression-in-body)
save-excursion常常出如今一个let表达式中
(let varlist (save-excursion body...))
标记
标记(mark)是缓冲区中的另一个位置。它的值能够用一个命令(C-SPACE(set-mark-command))来设置。若是设置了一个标记,能够用命令C-x C-x(exehange-point-and-mark)使光标从位点跳到标记处,并将光标当初所处的位置设置成当前的标记。另外,标记也是存放在标记环中的,能够屡次用C-u C-SPACE命令来使光标跳到被保存的标记处(和M-y的用法同样)
interactive
- "p" 前缀参数,经过C-u传入
- “P” 未加工的前缀参数,使用前须要调用函数(prefix-numeric-value arg),arg是参数
- “B” 传入缓冲区的名字给参数,能够是不存在的缓冲区。
- “r” 传入域(region)的开始位置和终了位置给参数
- “¥n" 分割参数的做用
- "*" 若是要编辑的是只读缓冲区,会显示错误消息到回显区。可是测试后,发现不加也会出错误信息。
- ”b” 要求输入一个缓冲区的名字给参数,必须是存在的缓冲区才行