From 9b44dfcc0674666a0c16476dcb99f453afc9be2f Mon Sep 17 00:00:00 2001 From: Gerard Vermeulen Date: Sun, 24 Apr 2022 14:01:06 +0200 Subject: [PATCH] Ensure that blacken-buffer runs only on buffers in python-mode --- README.org | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index b46aa55..1c6d225 100644 --- a/README.org +++ b/README.org @@ -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