Overhaul the code to let Eglot work with Org Babel source blocks

This commit is contained in:
Gerard Vermeulen 2024-02-23 11:17:46 +01:00
parent 0f1be8c4a2
commit a43104c2eb

View File

@ -4750,11 +4750,14 @@ out of your way. [[info:eglot#Top][Eglot (info)]] is a builtin since Emacs-29.1
listings contribute to a programming language mode independent [[https://github.com/joaotavora/eglot][Eglot]]
configuration:
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
2. Listing [[lst:help-setup-org-src-mode-for-eglot-1]],
[[lst:help-setup-org-src-mode-for-eglot-2]],
[[lst:help-setup-org-src-mode-for-eglot-3]], 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.
Python. Listing [[lst:help-setup-org-src-mode-for-eglot-1]] is bad practice,
because it signals ~user-errors~.
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
@ -4772,19 +4775,19 @@ configuration:
(keymap-set eglot-mode-map "C-c r" 'eglot-rename))
#+end_src
#+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
#+caption[1st help to setup any =org-src-mode= buffers for =eglot=]:
#+caption: 1st help to setup any =org-src-mode= buffers for =eglot=.
#+caption: Bad practice.
#+name: lst:help-setup-org-src-mode-for-eglot-1
#+begin_src emacs-lisp -n :results silent
(with-eval-after-load 'emacs
(defcustom eglot-maybe-ensure-modes '(python-mode)
"Modes where maybe `eglot-ensure' should be or has been called.
This may be in the case of proper directory local variables or in
the case of proper `org-src-mode' buffers.")
;;; Begin: Eglot and Org Babel source blocks
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun eglot-org-babel-edit-prep (info)
;;
;; One should not signal `user-error's in `org-babel-edit-prep:LANG'
;; functions.
(defun eglot-org-babel-edit-prep-user-error (info)
"Try to setup an `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
@ -4792,7 +4795,7 @@ 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))
(let ((point (point))
(body (nth 1 info))
(filename (cdr (assq :tangle (nth 2 info)))))
(when (string= filename "no")
@ -4811,21 +4814,122 @@ the Org-mode source block code before calling `eglot-ensure'."
(goto-char (point-min))
(insert-file-contents filename 'visit nil nil 'replace)
(search-forward body)
;; The next line preserves point in the `org-src-mode' buffer.
(goto-char (+ (match-beginning 0) point -1))
(narrow-to-region (match-beginning 0) (match-end 0))
(goto-char mark)
(eglot-ensure)))
#+end_src
(defun org-babel-edit-prep:python (info)
(eglot-org-babel-edit-prep info)))
#+caption[2nd help to setup any =org-src-mode= buffers for =eglot=]:
#+caption: 2nd help to setup any =org-src-mode= buffers for =eglot=.
#+caption: Best practice.
#+name: lst:help-setup-org-src-mode-for-eglot-2
#+begin_src emacs-lisp -n :results silent
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun eglot-org-babel-edit-prep (info)
"Try to setup an `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'."
(when-let ((ok (bound-and-true-p org-src-mode))
(point (point))
(body (nth 1 info))
(filename (cdr (assq :tangle (nth 2 info)))))
(when (string= filename "no")
(setq ok nil)
(message "Org source block has no tangled file"))
(when ok
(setq filename (expand-file-name filename))
(unless (file-readable-p filename)
(setq ok nil)
(message "Tangled file %s is not readable" filename)))
(when ok
(with-temp-buffer
(insert-file-contents filename 'visit nil nil 'replace)
(unless (search-forward body nil 'noerror)
(setq ok nil)
(message "Org source block does not occur in tangled file %s"
filename))
(when (search-forward body nil 'noerror)
(setq ok nil)
(message
"Org source block occurs twice or more in tangled file %s"
filename))))
(when ok
(goto-char (point-min))
(insert-file-contents filename 'visit nil nil 'replace)
(search-forward body)
;; The next line preserves point in the `org-src-mode' buffer.
(goto-char (+ (match-beginning 0) point -1))
(narrow-to-region (match-beginning 0) (match-end 0))
(eglot-ensure))))
#+end_src
#+caption[3rd help to setup any =org-src-mode= buffers for =eglot=]:
#+caption: 3rd help to setup any =org-src-mode= buffers for =eglot=.
#+caption: Good practice.
#+name: lst:help-setup-org-src-mode-for-eglot-3
#+begin_src emacs-lisp -n :results silent
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun eglot-org-babel-edit-prep-if-let (info)
"Try to setup an `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'."
(if-let ((ok (bound-and-true-p org-src-mode))
(point (point))
(body (nth 1 info))
(filename (cdr (assq :tangle (nth 2 info)))))
(progn
(when (string= filename "no")
(setq ok nil)
(message "Org source block has no tangled file"))
(when ok
(setq filename (expand-file-name filename))
(unless (file-readable-p filename)
(setq ok nil)
(message "Tangled file %s is not readable" filename)))
(when ok
(with-temp-buffer
(insert-file-contents filename 'visit nil nil 'replace)
(unless (search-forward body nil 'noerror)
(setq ok nil)
(message "Org source block does not occur in tangled file %s"
filename))
(when (search-forward body nil 'noerror)
(setq ok nil)
(message
"Org source block occurs twice or more in tangled file %s"
filename))))
(when ok
(goto-char (point-min))
(insert-file-contents filename 'visit nil nil 'replace)
(search-forward body)
;; The next line preserves point in the `org-src-mode' buffer.
(goto-char (+ (match-beginning 0) point -1))
(narrow-to-region (match-beginning 0) (match-end 0))
(eglot-ensure)))
(message "Buffer %s is not `eglot' ready" (buffer-name))))
#+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 -n :results silent
(with-eval-after-load 'emacs
;; https://www.reddit.com/r/emacs/comments/w4f4u3
;; /using_rustic_eglot_and_orgbabel_for_literate/
(defun org-babel-edit-prep:python (info)
(eglot-org-babel-edit-prep info))
(defcustom eglot-maybe-ensure-modes '(python-mode)
"Modes where calling `eglot-ensure' may have occurred.
This may be in the case of proper directory local variables or in
the case of proper `org-src-mode' buffers.")
(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'."
@ -4843,14 +4947,13 @@ This is to advice `org-edit-src-exit' and `org-edit-src-save'."
(delete-region (point) (point-max))))))
(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))
(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 -n :results silent
(with-eval-after-load 'emacs
(defun eglot-maybe-ensure ()
(when (and (apply #'derived-mode-p eglot-maybe-ensure-modes)
(assoc 'eglot-workspace-configuration dir-local-variables-alist))
@ -4859,7 +4962,9 @@ This is to advice `org-edit-src-exit' and `org-edit-src-save'."
;; 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))
(add-hook 'after-change-major-mode-hook #'eglot-maybe-ensure)
;;; End: Eglot and Org Babel source blocks
#+end_src
#+caption[Whiten Black]: