Neovim 有个 API,通过 Unix domain socket + Msgpack-RPC 调用,试试从 Emacs 控制:
建立 Socket 连接:
(make-network-process
:name "nvim"
:buffer "*nvim*"
:service (car (file-expand-wildcards (concat temporary-file-directory "nvim*/*")))
:family 'local
:coding 'binary
:filter (lambda (proc output)
(message "=> %S" (msgpack-decode (vconcat output)))))
比如发送 :quit
命令退出 Neovim:
(process-send-string
(get-process "nvim")
(msgpack-encode [0 1 "nvim_command" ["quit"]]))
上面 [0 1 "nvim_command" ["quit"]]
是 Msgpack-RPC 规定格式 [type, msgid, method, params]
,nvim_command
是 Neovim 的 API,quit
是 Vim 命令。
好像还没人给 Emacs Lisp 实现 Msgpack,暂时用命令行工具 GitHub - ludocode/msgpack-tools: Command-line tools for converting between MessagePack and JSON / msgpack.org[UNIX Shell] 代替:
(defun msgpack-encode (object)
(with-temp-buffer
(set-buffer-multibyte nil)
(let ((coding-system-for-read 'binary)
(coding-system-for-write 'binary))
(call-process-region (json-encode object) nil "json2msgpack" nil t)
(buffer-string))))
(defun msgpack-decode (data)
(with-temp-buffer
(set-buffer-multibyte nil)
(let ((coding-system-for-read 'binary)
(coding-system-for-write 'binary))
(call-process-region
(if (vectorp data)
(apply #'unibyte-string (append data nil))
data)
nil "msgpack2json" nil t)
(goto-char (point-min))
(json-read))))