用以下代码测了下:
(defvar test-variable "ori")
(setq-mode-local
telega-chat-mode
test-variable "telega")
(setq-mode-local
lisp-interaction-mode
test-variable "lisp-interaction")
(setq-mode-local
emacs-lisp-mode
test-variable "emacs-lisp")
(setq-mode-local
org-mode
test-variable "org")
(setq-mode-local
python-mode
test-variable "python")
最终的结果是 emacs-lisp
, org
和 python
生效,但在 telega 的 chat mode 和 scratch (major mode 是 lisp-interaction) 不行。哪位大佬给讲讲原理?
以下是 setq-mode-local
的源代码:
(defmacro setq-mode-local (mode &rest args)
"Assign new values to variables local in MODE.
MODE must be a major mode symbol.
ARGS is a list (SYM VAL SYM VAL ...).
The symbols SYM are variables; they are literal (not evaluated).
The values VAL are expressions; they are evaluated.
Set each SYM to the value of its VAL, locally in buffers already in
MODE, or in buffers switched to that mode.
Return the value of the last VAL."
(declare (debug (symbolp &rest symbolp form)))
(when args
(let (i ll bl sl tmp sym val)
(setq i 0)
(while args
(setq tmp (make-symbol (format "tmp%d" i))
i (1+ i)
sym (car args)
val (cadr args)
ll (cons (list tmp val) ll)
bl (cons `(cons ',sym ,tmp) bl)
sl (cons `(set (make-local-variable ',sym) ,tmp) sl)
args (cddr args)))
`(let* ,(nreverse ll)
;; Save mode bindings
(mode-local-bind (list ,@bl) '(mode-variable-flag t) ',mode)
;; Assign to local variables in all existing buffers in MODE
(mode-local-map-mode-buffers #'(lambda () ,@sl) ',mode)
;; Return the last value
,tmp)
)))