网上的 smart-compile.el 文件我用过了。 嗯,我想要的效果是 按下快捷键 emacs 直接运行 命令 编译。然后 显示 结果。 看完结果之后。输入 enter 会自动关闭窗口
给出一段我自己写的c/c++一键编译/运行/出结果/输入enter等待一秒关闭窗口的代码仅供参考
写得不怎么好(因为我后来都用cmake了:joy:),欢迎提供改进方案
用了dying-mode
(defun myself-cc-mode-hook ()
(set (make-local-variable 'my-exec-command)
(let ((myfile (file-name-sans-extension buffer-file-name)))
(format "%s;echo Press any key to continue;read -n"
myfile)))
(set (make-local-variable 'my-gdb-command)
(let ((myfile (file-name-sans-extension buffer-file-name)))
(format "gdb -i=mi %s"
myfile)))
(global-set-key [f9] 'myself-compile)
(global-set-key [f8] 'myself-exec)
(global-set-key (kbd "S-<f8>") 'myself-gdb))
(defun myself-c-mode-hook ()
(if (file-exists-p "Makefile")
(set (make-local-variable 'my-compile-command)
"make -k")
(set (make-local-variable 'my-compile-command)
;; emulate make's .c.o implicit pattern rule, but with
;; different defaults for the CC, CPPFLAGS, and CFLAGS
;; variables:
;; $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
(let ((myfile (file-name-nondirectory buffer-file-name)))
(format "%s -o %s %s %s"
(or (getenv "CC") "gcc")
(file-name-sans-extension myfile)
;; (or (getenv "CPPFLAGS") "-DDEBUG=9")
(or (getenv "CFLAGS") "-Wall -g")
myfile))))
;; (set (make-local-variable 'my-exec-command)
;; (let ((myfile (file-name-sans-extension buffer-file-name)))
;; (format "%s;echo Press any key to continue;read -n"
;; myfile)))
)
(defun myself-c++-mode-hook ()
(if (file-exists-p "Makefile")
(set (make-local-variable 'my-compile-command)
"make -k")
(set (make-local-variable 'my-compile-command)
(let ((myfile (file-name-nondirectory buffer-file-name)))
(format "%s -o %s %s %s"
(or (getenv "CC") "g++")
(file-name-sans-extension myfile)
(or (getenv "CFLAGS") "-Wall -g -std=c++11")
myfile))) )
;; (set (make-local-variable 'my-exec-command)
;; (let ((myfile (file-name-sans-extension buffer-file-name)))
;; (format "%s;echo Press any key to continue;read -n"
;; myfile)))
)
(defun myself-exec ()
(interactive)
(progn
(let* ((buf-name (generate-new-buffer-name "result"))
(buf (get-buffer-create buf-name)))
(async-shell-command my-exec-command buf)
(other-window 1)
(let ((proc (get-buffer-process buf)))
(if (and proc (null (eq (process-status proc) 'exit)))
(set-process-sentinel proc '(lambda (proc event)
(if (eq (process-status proc) 'exit)
(with-current-buffer (process-buffer proc) (dying-mode 't)))))
(delete-window)))))
)
(defun myself-compile ()
(interactive)
(save-buffer)
(compile my-compile-command))
(defun myself-gdb ()
(interactive)
(gdb my-gdb-command))