楼上只贴代码不说话……因为按键需要绑定到一个command / interactive function,而主帖里my/find-file返回的是它的body的最后一个表达式,(find-file filename)的值,显然不是一个function。
这样也可以:
; lexical binding才能用higher order function,才能用closure存住一个变量
(setq lexical-binding t)
; 返回一个lambda
(defun test/find-file (test/file)
(lambda () (interactive) (find-file test/file)))
; dynamically set 这个变量,可以看出closure是不是真的生效了
(setq test/file "~")
(setq test/generated (test/find-file "~/.bashrc"))
(funcall test/generated)
; =>打开了.bashrc这个文件
; 改回默认,别把emacs搞坏了
(setq lexical-binding nil)
然后我又发现生成test/generated和调用它用
(defun test/generated () (test/find-file "~/.bashrc"))
(funcall (test/generated))
也是可以的。[第999次理解lexical/dynamic scope和symbol]
PS上面两个setq和defun出来的test/generated可以共存,因为: