0

I'd like to configure Emacs to play an audible noise when saving my .py files but only if there is a linting error present.

I currently have flycheck linting done on the fly and autopep8 settings applied on saving with the following configuration...

;; Use flycheck rather than flymake with elpy
(when (require 'flycheck nil t)
  (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  (add-hook 'elpy-mode-hook 'flycheck-mode))
(add-hook 'python-mode-hook
          (lambda ()
            (setq flycheck-python-pylint-executable "~/.virtualenvs/dskit/bin/pylint")
            (setq flycheck-pylintrc "~/.emacs.d/settings/.pylintrc")))

;; enable autopep8 formatting on save
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)

...and was thinking that a hook added to 'py-autopep8-enable-on-save might be the way forward, but am still on the starting slopes of the Lisp learning curve and would appreciate any pointers or suggestions.

  • 1
    You'll need some way to tell whether or not there exists >1 errors in the file. I don't see any variables that store the number of linting errors in the file. It looks like python.el and elpy use python-check to call flake8. When run with M-x, it returns an error code. If you can find a way to call that in the background, you could write a function that calls flake8 and, depending on the return code, plays a sound. You'd want to add that to the before-save-hook. py-autopep8-enable-on-save is just a wrapper for that. – Lorem Ipsum Jan 30 '19 at 15:41
  • 1
    It looks like you might be able to use something like this: (call-process "flake8" nil t nil "program.py"). This returns the output from running flake8 program.py. Hopefully that's enough to get you down the right path. – Lorem Ipsum Jan 30 '19 at 15:56
  • Thanks for the pointer, I'll report back when I've had time to play around. – slackline Jan 31 '19 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.