在Emacs中保存编译 – 执行C ++程序的单一快捷方式?

我一直在想办法有效地保存一个程序,编译它,然后在emacs中运行它。 我只是部分成功了。

我使用smart-compile.el来简化工作( http://emacswiki.org/emacs/smart-compile.el )。 在那里,我编辑了C ++相关部分到下面,以便当我键入Mx smart-compile RET后跟一个RET ,程序编译和运行。

 (defcustom smart-compile-alist '( ;; g++-3 is used instead of g++ as the latter does not ;; work in Windows. Also '&& %n' is added to run the ;; compiled object if the compilation is successful ("\\.[Cc]+[Pp]*\\'" . "g++-3 -O2 -Wall -pedantic -Werror -Wreturn-type %f -lm -o %n && %n") .. 

举一个例子,对于一个sqrt.cpp程序,smart-compile会自动生成下面的编译命令:

 g++-3 -O2 -Wall -pedantic -Werror -Wreturn-type sqrt.cpp -lm -o sqrt && sqrt 

只要我的.cpp没有任何cin语句就可以工作。 对于使用cin语句的代码,控制台窗口会显示用户应该input数据的点。 但我无法input任何内容,编辑状态一直处于停顿状态。

为了使用户input的代码工作,我必须删除&& FILENAME部分,然后在emacs的eshell手动运行eshell

我在Windows中运行emacs 24.3。 我已经安装了Cygwin,并将其bin添加到Windows环境variablespath(这就是为什么g ++ -3编译工作)。

我将不胜感激,如果有人可以指导我如何保存编译运行用户input所需.cpp程序在emacs使用一个命令。 或者至less我该如何修改上面的g ++ – 3命令来编译+运行用户input程序。

谢谢!

Emacs是可编程的,所以如果需要两个步骤的话,你可以编写一个结合它们的命令。 简单代码看起来像这样:

 (defun save-compile-execute () (interactive) (smart-compile 1) ; step 1: compile (let ((exe (smart-compile-string "%n"))) ; step 2: run in *eshell* (with-current-buffer "*eshell*" (goto-char (point-max)) (insert exe) (eshell-send-input)) (switch-to-buffer-other-window "*eshell*"))) 

上面的代码很简单,但它有一个缺陷:它不等待编译完成。 由于smart-compile不支持用于同步编译的标志,因此必须通过暂时挂接compilation-finish-functions ,这会使代码更加复杂:

 (require 'cl) ; for lexical-let (defun do-execute (exe) (with-current-buffer "*eshell*" (goto-char (point-max)) (insert exe) (eshell-send-input)) (switch-to-buffer-other-window "*eshell*")) (defun save-compile-execute () (interactive) (lexical-let ((exe (smart-compile-string "./%n")) finish-callback) ;; when compilation is done, execute the program ;; and remove the callback from ;; compilation-finish-functions (setq finish-callback (lambda (buf msg) (do-execute exe) (setq compilation-finish-functions (delq finish-callback compilation-finish-functions)))) (push finish-callback compilation-finish-functions)) (smart-compile 1)) 

该命令可以使用Mx save-compile-execute RET或通过绑定到一个键来运行。