Lisp
Table of Contents
1. root
S-express can be atmo or list car : get the first S-express cdr : get the rest list cons : need a S-express and a list : put the first S-express into the second list (null? l) if l is null? (atom? l) if l is atmo? (member? ele list) (rember ele list)
2. command
2.1. execute
| command | explain |
| C-c C-e | result in minibuffer |
| C-j | result under the cursor |
| M-x ielm | in elisp interactive mode |
3. List
(concat "abd" "def")
(+ 34 9)
(substring "0123456789" 2 8)
(setq Emacs 'My-favour)
(message "Am today %s I start to learn emacs %d " "niaho" 09)
;"Am today niaho I start to learn emacs 9 "
(message "He saw %d %s"
(- fill-column 32)
(concat "red "
(substring
"The quick brown foxes jumped." 16 21)
" leaping."))
;"He saw 38 red foxes leaping."
(setq trees '(pine fir oak maple)
herbivores '(gazelle antelope zebra))
| gazelle | antelope | zebra |
4. Evaluation
#+BEGINSRC elisp
(buffer-file-name) (setq a 300) (* 34 67 45 #+ENDSRC)
5. Buffer
(buffer-name) (current-buffer) (other-buffer) (switch-to-buffer (other-buffer)) (buffer-size) (point) (point-min) (point-max) (defun multipy-by-seven (number) "Multipy NUMBER by seven" (* 7 number)) (multipy-by-seven 4) (defun multipy-by-seven (number)) (interactive "p") (message "the result is %d" (* 7 number))
6. let and let *
let define the local variable
(defun circle-area (radix)
(let ((pi 3.1415926)
area)
(setq area (* pi radix radix))
(message "直径为 %.2f 的圆面积是 %.2f" radix area)
))
(circle-area 5)
直径为 5.00 的圆面积是 78.54
let* define local variable, which can be immediately used
(defun circle-eara (radix)
(let* ((pi 3.1415926)
(area (pi*radix*radix))))
(message "直径为 %.2f 的圆面积是 %.2f" radix area))
(circle-area 6)
直径为 6.00 的圆面积是 113.10
7. Special Form
7.0.1. defun
(defun name-of-function (argument-list) "documentation…" body…)
7.0.2. let
(let ((zebra "stripes") (tiger "fierce")) (message "One kind of animal has %s and another is %s." zebra tiger))
7.0.3. if
(defun my-max (a b)
(if ( > a b)
(message "aaaaa")
(message "bbbbb")
))
(my-max 3 0)
(if true-or-false-test action-to-carry-out-if-the-test-returns-true)
7.0.4. if-then
(if true-or-false-test action-to-carry-out-if-the-test-returns-true action-to-carry-out-if-the-test-returns-false)
(if 4 'true 'false)
(if nil 'true 'false)
7.0.5. save-excursion
(message "We are %d characters into this buffer." (save-excursion (- (point) (goto-char (point-min)) (point))))
8. 2-36 arbitrary base
#36re #36r1 #36ra #36ru #36rk
20
9. code
char count
(defun cheap-count-words()
(interactive)
(let ((words 0))
(goto-char (point-min))
(while (forward-word)
(setq words (1+ words)))
(message (format "Words is Buffer : %s" words))
words)
)
10. Emacs lisp programm
10.1. file management
read file :: insert-file-contents and copy file :: write-region
;;read file and print out
(defun read-file-as-lines (file-path)
(with-temp-buffer
(insert-file-contents file-path)
(split-string (buffer-string) "\n" t)))
(read-file-as-lines "~/Desktop/110.py")
;;read file and copy to another
(defun copy-file-to-file (fromfile tofile)
(with-temp-buffer
(insert-file-contents fromfile)
(setq copy-test-path tofile)
(write-region (point-min) (point-max) copy-test-path)
(write-region "new content" nil copy-test-path t)))
(copy-file-to-file "~/Desktop/110.py" "~/Desktop/112.py")
11. Function in orgmode
(+ 45 65)
110
(defun do-some-math(x y)
(* (+ x 20)
(- y 10) ) )
(defun multipy-many (x &rest operands)
(dolist (operand operands)
(when operand
(setq x (* x operand)))) x)
interactive function als commands
(defun my-first-command() (interactive) (message "Hey , it works") )
my-first-command