Emacs 26 开始新增了一种变量:Connection Local Variable,用来为特定的连接设置特定的变量值,值得说明的是,严格来说, Connection Local Variable 并不是一种变量,而是用来为 buffer 设置变量的一种接口。
定义 Connection Local Variable 的步骤如下:
1、启用 connection local variable
(setq enable-connection-local-variables t)
2、定义一个connection profile
(connection-local-set-profile-variables
'remote-termux
'((org-babel-remote-temporary-directory . "/data/data/com.termux/files/usr/tmp")))
该函数会更新变量 connection-local-profile-alist 的值。
执行完上面语句后就能从变量 connection-local-profile-alist 中看到多了一个 remote-termux 的 profile,以及对应的变量值
3、为特定规则的连接设置 profile
(connection-local-set-profiles
'(:application 'tramp :protocol "ssh" :machine "phone" :user "lujun9972")
'remote-termux)
该函数会更新变量 connection-local-criteria-alist 的值。
执行完上面语句后就能从变量 connection-local-criteria-alist 看到定义的规则和对应 profile
遗憾的是,并不是定义了 Connection Local Variable 后,之前所有网络连接相关的包都会神奇地自动应用 Connection Local Variable 的值。
Emacs为我们提供了两个函数来应用 Connection Local Variable:
1、hack-connection-local-variables-apply criteria
根据指定的标准,将对应条件的 Connection Local Variable 应用到当前 buffer 中
(hack-connection-local-variables-apply '(:application 'tramp :protocol "ssh" :machine "phone" :user "lujun9972"))
这时你再查看 org-babel-remote-temporary-directory 的值,你会发现已经变成了新的值
org-babel-remote-temporary-directory is a variable defined in ‘ob-core.el’.
Its value is ("/data/data/com.termux/files/usr/tmp")
Original value was "/tmp/"
Local in buffer *scratch*; global value is "/tmp/"
Documentation:
Directory to hold temporary files on remote hosts.
You can customize this variable.
[back]
2、with-connection-local-profiles profiles &rest body
在应用 profiles 中的connection local variables 的环境下执行 body 中的内容.
(with-connection-local-profiles '(remote-termux)
(message "%s" org-babel-remote-temporary-directory))
