请大家认真看Elisp官方手册。
ielm
: elisp交互shell
;; global variables
;; 整个系统都能访问
;; C-h v 访问
(setq x '(a b))
;; variables never change
nil
t
;; local variables
;; let* local variables互相可以引用
(let* ((y 1)
(z y))
(list y z))
⇒ (1 1)
;; void variable
;; dynamic scoping
;; boundp: 测试当前变量在环境下有无值
(boundp 'abracadabra) ; Still globally void.
⇒ nil
(setq abracadabra 5) ; Make it globally nonvoid.
⇒ 5
(boundp 'abracadabra)
⇒ t
;; define global variables
;; defvar or defconst
;; 提供初始值、文档
;; C-h v
(defvar bar 23
"The normal weight of a bar.")
⇒ bar
(defconst float-pi 3.141592653589793 "The value of Pi.")
⇒ float-pi
;; 定义一个复杂对象,实践
(defvar my-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-a" 'my-command)
…
map)
docstring)
;; 访问变量
;; When a symbol is evaluated, it is treated as a variable.
(setq x 1)
(symbol-value 'x)
(eval 'x)
(+ x 1) ; =>2
;; set value
;; setq
(setq x (1+ 2))
⇒ 3
(let ((x 5))
(setq x 6) ; The local binding of x is set.
x)
⇒ 6
(setq x 10 ; Notice that x is set before
y (1+ x)) ; the value of y is computed.
⇒ 11
;; set 比较绕,先不考虑
make-local-variable
声明variable为local buffer variable。
之后在当前buffer里面怎么艹都和全局没关联了。
note:接在define-minor-mode
定义中不错。
;; In buffer ‘b1’:
(setq foo 5) ; Affects all buffers.
⇒ 5
(make-local-variable 'foo) ; Now it is local in ‘b1’.
⇒ foo
foo ; That did not change
⇒ 5 ; the value.
(setq foo 6) ; Change the value
⇒ 6 ; in ‘b1’.
foo
⇒ 6
;; In buffer ‘b2’, the value hasn’t changed.
(with-current-buffer "b2"
foo)
⇒ 5
make-variable-buffer-local var
让所有buffer内的var都成为buffer local。
warning:这个功能慎用,太猛了,容易误伤。