如何通过 ALT-M 在 emacs 中进行编译?

How to make compilation in emacs through ALT-M?

emacs 的编译过程非常安静,这就是为什么我决定让它更快更方便的原因,我只遇到这个程序(代码串)不能自我更新的问题,也就是说,compile-command(第一次启动后),保持原样,不干预不会改变,我决定加上defin,但我对lisp的无知阻止了我,结果没有成功。

问题: 如何使我的功能正常工作,以便每次新的 ALT-M 单击 compile-command 都是新的。

我尝试做的事情:

(defun x-recompile (compile-command)
(setq compile-command '(concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
                   (if (file-name-sans-extension buffer-file-name)
                  (shell-quote-argument
                   (file-name-sans-extension buffer-file-name)))
                   " "
                    (if buffer-file-name
                    (shell-quote-argument (buffer-file-name))))))

(define-key global-map "\eM" 'compile)
(define-key global-map "\em" 'x-recompile)

初始版本:

(setq compile-command '(concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
                   (if (file-name-sans-extension buffer-file-name)
                  (shell-quote-argument
                   (file-name-sans-extension buffer-file-name)))
                   " "
                    (if buffer-file-name
                    (shell-quote-argument (buffer-file-name)))))
(define-key global-map "\eM" 'compile)
(define-key global-map "\em" 'recompile)

我认为最简单的方法是使用模式挂钩:

(add-hook 'c-mode-hook
  (lambda ()
    (set (make-local-variable 'compile-command)
         (concat "/usr/local/Cellar/gcc/8.3.0/bin/gcc-8 -O2 -Wall -o "
                 (if (file-name-sans-extension buffer-file-name)
                     (shell-quote-argument
                      (file-name-sans-extension buffer-file-name)))
                 " "
                 (if buffer-file-name
                     (shell-quote-argument (buffer-file-name)))))))

这将在您访问该文件时设置 compile-command,然后 compilerecompile 将正常工作。

  1. 将以上代码添加到.emacs
  2. 评估表格
  3. 重新访问文件(杀死缓冲区并再次打开文件)
  4. C-h v compile-command RET 看值
  5. M-x 编译 RET 或任何你将 compile 绑定到