Add `narrow-format-all:python' to format narrowed Python buffers
This commit is contained in:
parent
5f4b23340e
commit
deb530f4e9
41
README.org
41
README.org
@ -2907,7 +2907,12 @@ mode independent [[https://github.com/joaotavora/eglot][eglot]] configuration:
|
||||
buffers for use with [[https://github.com/joaotavora/eglot][eglot]]. They are a refactored implementation of the post
|
||||
[[https://www.reddit.com/r/emacs/comments/w4f4u3/using_rustic_eglot_and_orgbabel_for_literate/][Using rustic, eglot, and org-babel with LSP support in Emacs]] for my use with
|
||||
Python.
|
||||
3. Listing [[lst:eglot-maybe-ensure]] starts [[https://github.com/joaotavora/eglot][eglot]] in case of proper programming
|
||||
3. Listing [[lst:narrow-format-all-python]] defines a function to format only the
|
||||
narrowed region of Python =org-src-mode= blocks prepared by means of the code
|
||||
in listing [[lst:setup-python-org-src-mode-for-eglot]]. *Do not use*
|
||||
=format-all-buffer= *on such buffers*, since =format-all-buffer= does not
|
||||
handle the narrowing.
|
||||
4. Listing [[lst:eglot-maybe-ensure]] starts [[https://github.com/joaotavora/eglot][eglot]] in case of proper programming
|
||||
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).
|
||||
@ -2998,6 +3003,40 @@ mode independent [[https://github.com/joaotavora/eglot][eglot]] configuration:
|
||||
(advice-add 'org-edit-src-save :before #'undo-eglot-org-babel-edit-prep))
|
||||
#+end_src
|
||||
|
||||
#+caption[Experimental =narrow-format-all:python=]:
|
||||
#+caption: Experimental =narrow-format-all:python=.
|
||||
#+name: lst:narrow-format-all-python
|
||||
#+begin_src emacs-lisp
|
||||
(when (and (fboundp 'eglot-ensure)
|
||||
(fboundp 'format-all-buffer))
|
||||
(defun narrow-format-all:python ()
|
||||
"Format narrowed Python `org-src-mode' buffers correctly.
|
||||
Use this function instead of `format-all-buffer' on `org-babel-edit-prep:python'
|
||||
prepared buffers."
|
||||
(interactive)
|
||||
(when (eq major-mode 'python-mode)
|
||||
(let ((source-min (point-min))
|
||||
(source-max (point-max))
|
||||
(source (current-buffer))
|
||||
(target (get-buffer-create "*narrow-format-all:python*")))
|
||||
(with-current-buffer target
|
||||
(barf-if-buffer-read-only)
|
||||
(erase-buffer)
|
||||
(save-excursion
|
||||
(insert-buffer-substring-no-properties
|
||||
source source-min source-max))
|
||||
(python-mode)
|
||||
(setq-local format-all-formatters '(("Python" black)))
|
||||
(format-all-buffer)
|
||||
(let ((target-min (point-min))
|
||||
(target-max (point-max)))
|
||||
(with-current-buffer source
|
||||
(erase-buffer)
|
||||
(save-excursion
|
||||
(insert-buffer-substring-no-properties
|
||||
target target-min target-max)))))))))
|
||||
#+end_src
|
||||
|
||||
#+caption[Start =eglot= in case of a proper =dir-local-variables-alist=]:
|
||||
#+caption: Start =eglot= in case of a proper =dir-local-variables-alist=.
|
||||
#+name: lst:eglot-maybe-ensure
|
||||
|
Loading…
x
Reference in New Issue
Block a user