自定義函數
(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」 要求輸入一個緩衝區的名字給參數,必須是存在的緩衝區才行