Refactor trying to make Python org-src-mode buffers work with eglot

This commit is contained in:
Gerard Vermeulen 2022-07-27 07:00:37 +02:00
parent 2b8030a9f7
commit f0d722d7f1

View File

@ -2910,17 +2910,19 @@ Listing [[lst:configure-writegood-mode]] configures [[https://github.com/bnbeckw
:END:
[[https://github.com/joaotavora/eglot#readme][Emacs polyGLOT (eglot)]] is an Emacs language-server-protocol client that stays
out of the way. Three listings provide a programming language mode independent
[[https://github.com/joaotavora/eglot][eglot]] configuration:
out of the way. The following listings contribute to a programming language
mode independent [[https://github.com/joaotavora/eglot][eglot]] configuration:
1. Listing [[lst:ensure-eglot-installation]] ensures installation of [[https://github.com/joaotavora/eglot][eglot]] with
minimal configuration.
2. Listing [[lst:eglot-maybe-ensure]] starts [[https://github.com/joaotavora/eglot][eglot]] in case of proper programming
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: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
[[info:emacs#Directory
Variables][.dir-locals.el]] file in the root directory of any project using proper
file [[info:emacs#Directory Variables][.dir-locals.el]] in the root directory of any project using proper
programming modes).
3. Listing [[lst:setup-python-org-src-mode-for-eglot]] tries to prepare Python
=org-src-mode= buffers for use with [[https://github.com/joaotavora/eglot][eglot]].
#+caption[Ensure =eglot= installation]:
#+caption: Ensure =eglot= installation.
@ -2936,9 +2938,9 @@ out of the way. Three listings provide a programming language mode independent
(define-key eglot-mode-map (kbd "C-c r") 'eglot-rename)))
#+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
#+caption[Help to setup any =org-src-mode= buffers for =eglot=]:
#+caption: Help to setup any =org-src-mode= buffers for =eglot=.
#+name: lst:help-setup-org-src-mode-for-eglot
#+begin_src emacs-lisp
(when (fboundp 'eglot-ensure)
(defcustom eglot-maybe-ensure-modes '(python-mode)
@ -2946,30 +2948,16 @@ out of the way. Three listings provide a programming language mode independent
This may be in the case of proper directory local variables or in
the case of proper `org-src-mode' buffers.")
(defun eglot-maybe-ensure ()
(when (and (apply #'derived-mode-p eglot-maybe-ensure-modes)
(assoc 'eglot-workspace-configuration dir-local-variables-alist))
(eglot-ensure)))
;; The two hooks `after-change-major-mode-hook' and
;; `hack-local-variables-hook' are OK, but language mode hooks like
;; `python-mode-hook' are not.
(add-hook 'after-change-major-mode-hook #'eglot-maybe-ensure))
#+end_src
#+caption[Setup Python =org-src-mode= buffers for =eglot=]:
#+caption: Setup Python =org-src-mode= buffers for =eglot=.
#+name: lst:setup-python-org-src-mode-for-eglot
#+begin_src emacs-lisp
(when (fboundp 'eglot-ensure)
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun org-babel-edit-prep:python (info)
"Try to setup the `org-mode-src' buffer to make `eglot-ensure' succeed.
(defun eglot-org-babel-edit-prep (info)
"Try to setup and `org-mode-src' buffer to make `eglot-ensure' succeed.
INFO has a form similar to the return value of
`org-babel-get-src-block-info'. Try to load the tangled file
into the `org-src-mode' buffer as well as to narrow the region to
the Org-mode source block code before calling `eglot-ensure'."
(unless (bound-and-true-p org-src-mode)
(user-error "Buffer %s is no `org-src-mode' buffer" (buffer-name)))
(let ((mark (point))
(body (nth 1 info))
(filename (expand-file-name (cdr (assq :tangle (nth 2 info))))))
@ -2989,14 +2977,22 @@ out of the way. Three listings provide a programming language mode independent
(min-point (search-backward body)))
(narrow-to-region min-point max-point))
(goto-char mark)
(eglot-ensure)))
(eglot-ensure))))
#+end_src
#+caption[Setup Python =org-src-mode= buffers for =eglot=]:
#+caption: Setup Python =org-src-mode= buffers for =eglot=.
#+name: lst:setup-python-org-src-mode-for-eglot
#+begin_src emacs-lisp
(when (fboundp 'eglot-ensure)
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun undo-eglot-org-babel-edit-prep()
"Undo the `eglot' setup by deleting the text hidden by narrowing.
This is to advice `org-edit-src-exit' and `org-edit-src-save'."
(when (and (apply #'derived-mode-p eglot-maybe-ensure-modes)
(bound-and-true-p org-src-mode)
(buffer-file-name))
(when (and (bound-and-true-p org-src-mode)
(buffer-file-name)
(apply #'derived-mode-p eglot-maybe-ensure-modes))
(save-excursion
(goto-char (point-min))
(save-restriction
@ -3007,10 +3003,29 @@ out of the way. Three listings provide a programming language mode independent
(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[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
#+begin_src emacs-lisp
(when (fboundp 'eglot-ensure)
(defun eglot-maybe-ensure ()
(when (and (apply #'derived-mode-p eglot-maybe-ensure-modes)
(assoc 'eglot-workspace-configuration dir-local-variables-alist))
(eglot-ensure)))
;; The two hooks `after-change-major-mode-hook' and
;; `hack-local-variables-hook' are OK, but language mode hooks like
;; `python-mode-hook' are not.
(add-hook 'after-change-major-mode-hook #'eglot-maybe-ensure))
#+end_src
** [[https://github.com/lassik/emacs-format-all-the-code#readme][Format-all]]
:PROPERTIES:
:CUSTOM_ID: sec:format-all