Add tools to "whiten" some output of Black

This commit is contained in:
Gerard Vermeulen 2024-01-21 17:30:56 +01:00
parent 0da38f0834
commit 32dc95054c
1 changed files with 48 additions and 0 deletions

View File

@ -4623,6 +4623,8 @@ configuration:
modes and proper directory local variables (meaning in presence of a proper
file [[info:emacs#Directory Variables][.dir-locals.el]] in the root directory of any project using proper
programming modes).
4. Listing [[lst:whiten-black]] defines Emacs Lisp functions to undo (whiten) some
output of [[https://black.readthedocs.io/en/stable/][Black]] after src_emacs-lisp{(org-babel-tangle)}.
#+caption[Ensure =eglot= installation with minimal setup]:
#+caption: Ensure =eglot= installation with minimal setup.
@ -4752,6 +4754,52 @@ This is to advice `org-edit-src-exit' and `org-edit-src-save'."
(add-hook 'after-change-major-mode-hook #'eglot-maybe-ensure))
#+end_src
#+caption[Whiten Black]:
#+caption: Whiten Black
#+name: lst:whiten-black
#+begin_src emacs-lisp -n
(with-eval-after-load 'emacs
(defun black-python-sequence ()
"Try to find a Blackened Python sequence to whiten."
(interactive)
(if (derived-mode-p 'python-mode)
(re-search-forward "[[(][ \t]*$" nil t)
(org-narrow-to-block)
(re-search-forward "[[(][ \t]*$" nil t)
(widen)))
(defun whiten--python-sequence ()
"Whiten a Blackened Python sequence without narrowing.
Call `widen' after an `user-error'."
(if (not (and (looking-at-p "[ \t]*$")))
(user-error "Call `widen': not seeing `new-line'")
(skip-chars-backward " \t\\[\\(")
(if (not (looking-at-p "[ \t]*[[(][ \t]*$"))
(user-error "Call `widen': not seeing `[' or `(' before `new-line'")
(fixup-whitespace)
(skip-chars-forward " \\[\\(")
(kill-line)
(fixup-whitespace)
(move-end-of-line 1)
(while (and (eq ?\, (char-before)))
(kill-line)
(fixup-whitespace)
(move-end-of-line 1))
(save-excursion
(when (memq (char-before) '(?\) ?\]))
(goto-char (1- (point)))
(delete-char -1))))))
(defun whiten-python-sequence ()
"Whiten a Blackened Python sequence. Call `widen' after an `user-error'."
(interactive)
(if (derived-mode-p 'python-mode)
(whiten--python-sequences)
(org-narrow-to-block)
(whiten--python-sequence)
(widen))))
#+end_src
#+caption: Real sessions for ~eval-buffer~ in ~org-src~ buffers.
#+begin_src org -n :tangle eval-buffer-in-org-src-buffers.org
,#+title: Sessions for ~eval-buffer~ in ~org-src~ buffers