Restricting Flymake to my projects


My recent move from Flycheck to Flymake has proven to be a solid choice because the Emacs built-in syntax checker has yet to let me down. And since by now I am sure my love for project.el is absolutely obvious, could I miss the opportunity to make these two underappreciated gems shine together?

Honestly, though, the credit here goes all to Protesilaos Stavrou. His Flymake setup made me aware of a neat way to limit the use of Flymake to the places I actually need it.

All I had to do was adapt it to my preferences:

(defun mu-flymake-mode-activate ()
  "Activate `flymake-mode' only in my projects."
  (project--ensure-read-project-list)
  (let ((known-projects (project-known-project-roots))
        (pr (or (locate-dominating-file "." ".git")
                default-directory)))
    (if (and (eq buffer-read-only nil)
             (member pr known-projects))
        (flymake-mode +1)
      (flymake-mode -1))))

I then hooked this little function to prog-mode-hook and text-mode-hook and everything was good to go.

Note that project.el must be required before running mu-flymake-mode-activate, otherwise Emacs will complain about project--ensure-read-project-list not being available.