Clean up using eglot' in org-babel' code and documentation

This commit is contained in:
Gerard Vermeulen 2023-07-29 17:20:22 +02:00
parent 3b33e7d1d5
commit 55a68271f4

View File

@ -4629,20 +4629,13 @@ Listing [[lst:configure-writegood-mode]] configures [[https://github.com/bnbeckw
out of your way. [[info:eglot#Top][Eglot (info)]] is a builtin since Emacs-29.1. The following
listings contribute to a programming language mode independent [[https://github.com/joaotavora/eglot][Eglot]]
configuration:
1. Listing [[lst:minimal-eglot-setup][minimal Eglot setup]] ensures installation of [[https://github.com/joaotavora/eglot][Eglot]], shows how to get
debug information from the [[https://github.com/python-lsp/python-lsp-server][Python LSP server]], and adds key bindings to
=eglot-mode-keymap=.
1. Listing [[lst:minimal-eglot-setup][minimal Eglot setup]] adds key bindings to =eglot-mode-keymap=.
2. Listing [[lst:help-setup-org-src-mode-for-eglot]] and
[[lst:setup-python-org-src-mode-for-eglot]] try to prepare any =org-src-mode=
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: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
3. 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).
@ -4651,16 +4644,6 @@ configuration:
#+caption: Ensure =eglot= installation with minimal setup.
#+name: lst:minimal-eglot-setup
#+begin_src emacs-lisp -n :results silent
(with-eval-after-load 'emacs
;; Replace `nil' with `t' for debugging.
(when nil
(when (require 'eglot nil t)
(setq eglot-server-programs
`(((python-ts-mod python-mode)
. ,(eglot-alternatives
'(("pylsp" "-vv")
("ruff-lsp")))))))))
(with-eval-after-load 'eglot
(keymap-set eglot-mode-map "C-c n" 'flymake-goto-next-error)
(keymap-set eglot-mode-map "C-c p" 'flymake-goto-prev-error)
@ -4709,7 +4692,10 @@ the Org-mode source block code before calling `eglot-ensure'."
(min-point (search-backward body)))
(narrow-to-region min-point max-point))
(goto-char mark)
(eglot-ensure))))
(eglot-ensure)))
(defun org-babel-edit-prep:python (info)
(eglot-org-babel-edit-prep info)))
#+end_src
#+caption[Setup Python =org-src-mode= buffers for =eglot=]:
@ -4735,46 +4721,10 @@ This is to advice `org-edit-src-exit' and `org-edit-src-save'."
(widen)
(delete-region (point) (point-max))))))
(defun org-babel-edit-prep:python (info)
(eglot-org-babel-edit-prep info))
(advice-add 'org-edit-src-exit :before #'undo-eglot-org-babel-edit-prep)
(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 -n :results silent
(when (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
@ -4810,9 +4760,7 @@ Listing [[lst:configure-format-all]]:
;; https://ianyepan.github.io/posts/format-all/
;; https://jamesaimonetti.com/posts/formatting-tangled-output-in-org-mode/
(when (ensure-package-installation 'format-all)
;; (with-eval-after-load 'prog-mode
;; (when (autoload 'format-all-ensure-formatter "format-all")
;; (add-hook 'prog-mode-hook #'format-all-ensure-formatter)))
;; `format-all' defines `format-all-buffer' as an autoloaded command.
(with-eval-after-load 'ob-tangle
(add-hook
'org-babel-post-tangle-hook