为 compilation-buffer 增加输入功能
Emacs 的 compile
命令是一个未被充分利用的工具。它允许你运行任意的构建工具并且为多种编程语言提供错误高亮与快速掉转到错误的功能。但是许多的 Emacs 使用者依然习惯于切换到终端下运行 make,lein test 以及 bundle exec。然而 compile
命令有一个限制,那就是 compilation buffer 并不是真正的 shell,因此若正在运行的命令需要用户输入额外信息(即使是简单的 y/n 确认),也没有办法输入这些信息。
幸运的是,为其增加输入功能并不复杂。下面的代码片段定义了两个命令。第一个命令会提示你输入要输入的信息,然后将你的输入附上回车符,一起传递到低层的终端上, 该命令用于提示输入时或在 REPL 循环中。第二个命令只是简单的传递我们按下得键, 主要用于回答 y/n 问题,或者使用 C-d
或 C-j
快速退出 REPL。
(defun endless/send-input (input &optional nl)
"Send INPUT to the current process.
Interactively also sends a terminating newline."
(interactive "MInput: \nd")
(let ((string (concat input (if nl "\n"))))
;; This is just for visual feedback.
(let ((inhibit-read-only t))
(insert-before-markers string))
;; This is the important part.
(process-send-string
(get-buffer-process (current-buffer))
string)))
(defun endless/send-self ()
"Send the pressed key to the current process."
(interactive)
(endless/send-input
(apply #'string
(append (this-command-keys-vector) nil))))
(define-key compilation-mode-map (kbd "C-c i")
#'endless/send-input)
(dolist (key '("\C-d" "\C-j" "y" "n"))
(define-key compilation-mode-map key
#'endless/send-self))
几年来我一直忽略这个缺陷(This is something I’ve run into for years), 但是最终,我还是决定修正它,因为它使得当我的代码中包含 binding.pry(它会产生一个 REPL) 时,无法在 compilation buffer 中运行 Ruby 的 rspec 命令。现在我可以通过先按下 C-c i
来与 REPL 交互,也可以使用 C-d
快速退出这个 REPL。若你也遇到与我一样的情况,你还需要在你的 .pryrc
文件中设置下面选项 .pryrc file。
Pry.config.pager = false if ENV["INSIDE_EMACS"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论