如何在不写入磁盘的情况下将 emacs 缓冲区的内容发送到 gcc?

rah*_*hmu 5 emacs gcc

我经常运行一段一次性代码,我对保存在我的磁盘上并不是特别感兴趣,所以我经常在 emacs 中的一个缓冲区中工作,该缓冲区没有附加到我的系统中的任何文件。

当使用像 Python、Scheme 或 Lua 这样的解释型(我有点不愿意使用这个术语)语言时,每个各自的主要模式(常见的)都提供了在编辑器中运行 REPL/解释器和执行内容的方法直接缓冲。(通常带有一个被调用的函数send-buffer或类似的东西)。

但是,在使用 C 时,我找不到类似的功能。我发现自己被迫将文件保存到磁盘,以便将gcc文件名作为参数。

有没有办法将 emacs 缓冲区的内容发送到 gcc 而不将文件写入磁盘?

小智 4

使用C-x h( mark-whole-buffer),然后使用'M-|( shell-command-on-region),然后使用上面 @Kotte 的建议来运行gcc -x c -o tmp_prog - && tmp_prog

或者这是一个执行此操作的 elisp 函数:

(defun compile-and-run-buffer()
  "Compiles the code in the buffer using gcc, run the exe and remove it."
  (interactive)
  (shell-command-on-region
   (point-min)
   (point-max)
   "gcc -x c -o emacs_tmp - && ./emacs_tmp && rm emacs_tmp"))
Run Code Online (Sandbox Code Playgroud)