Ensure that blacken-buffer runs only on buffers in python-mode

This commit is contained in:
Gerard Vermeulen 2022-04-24 14:01:06 +02:00
parent e2c6671f96
commit 9b44dfcc06
1 changed files with 14 additions and 1 deletions

View File

@ -2765,12 +2765,25 @@ such a proper [[info:emacs#Directory Variables][.dir-locals.el]] file.
| =eldoc-doc-buffer= | {{{kbd(C-h .)}}} | =eglot-mode-map= |
|---------------------------+------------------+------------------|
Allthough the configuration of [[https://github.com/pythonic-emacs/blacken#readme][blacken]] is not explicit, I use it to format all
Lising [[lst:configure-blacken]] configures [[https://github.com/pythonic-emacs/blacken#readme][blacken]], because I use it to format all
new Python code with the [[https://black.readthedocs.io/en/stable/index.html][Python Black code formatter]]. Listing
[[lst:pyproject-toml-kick-off]] and [[lst:setup-cfg-kick-off]] implement the rules in
[[https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html][using black with other tools]] in order to make [[https://flake8.pycqa.org/en/latest/][flake8]] or [[https://pycodestyle.pycqa.org/en/latest/][pycodestyle]] agree with
[[https://black.readthedocs.io/en/stable/index.html][black's uncompromising style]].
#+caption[Configure =blacken=]:
#+caption: Configure =blacken=.
#+name: lst:configure-blacken
#+begin_src emacs-lisp
(when (fboundp 'blacken-buffer)
(defun advice-derived-python-mode-p (fn &rest args)
(if (derived-mode-p 'python-mode)
(apply fn args)
(message "Refusal to run %S, since buffer is in %S." fn major-mode)))
(advice-add 'blacken-buffer :around #'advice-derived-python-mode-p))
#+end_src
#+caption[Kick starting a =pyproject.toml= file]:
#+caption: Kick starting a =pyproject.toml= file.
#+name: lst:pyproject-toml-kick-off