Original link: https://emacstalk.github.io/post/026/
There is a very useful compile module in Emacs, which can be very convenient to compile code, run tests, etc. Unfamiliar readers can refer to:
Compiling and running scripts in Emacs – Mastering Emacs .
What’s more annoying is that
Every time you execute compile, if there are files that have been modified but not yet saved, it will prompt in the minibuffer.
The original intention of doing this is understandable. If the modified file is not saved, the old file will be used for compilation. The problem is that all unsaved files will be prompted, which is a bit excessive. It is best to control them within the project, and don’t prompt for other files.
Fortunately, the compile module provides an option to control which files are prompted: compilation-save-buffers-predicate
.
The implementation is as follows:
(defvar my/compile-project-root nil ) (defvar my/compile-file nil ) (defun my/update-compile-dir (cmd &optional comint) (if-let* ((pr (project-current)) (root (project-root pr))) (setq my/compile-project-root (file-truename root)) (progn (setq my/compile-project-root nil ) (setq my/compile-file (file-truename ( buffer-file-name )))))) (advice-add 'compile :before 'my/update-compile-dir ) (defun my/file-in-project? () "检查当前buffer 是否属于当前项目,如果当前目录不属于任何项目,直接返回`nil' " (if my/compile-project-root (string-prefix-p my/compile-project-root (file-truename ( buffer-file-name ))) ( string-equal my/compile-file (file-truename ( buffer-file-name ))))) (setq compilation-save-buffers-predicate 'my/file-in-project? )
The idea is to record the project to which the current file belongs before executing compile, and then determine whether the current unmodified file is included.
It should be noted that when the current directory of compile does not belong to any project, it will only remind whether the current file needs to be saved.
This article is reprinted from: https://emacstalk.github.io/post/026/
This site is for inclusion only, and the copyright belongs to the original author.