emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] org-babel-demarcate-block: split using element API


From: gerard . vermeulen
Subject: Re: [PATCH] org-babel-demarcate-block: split using element API
Date: Thu, 22 Feb 2024 16:28:47 +0000


On 21.02.2024 19:19, gerard.vermeulen@posteo.net wrote:
On 21.02.2024 10:40, Ihor Radchenko wrote:
gerard.vermeulen@posteo.net writes:
[...]
May you please provide more details?

This is different of what I saw before:

When splitting python blocks in an org file I got a long list of warnings: Warning (org-element): ‘org-element-at-point’ cannot be used in non-Org buffer #<buffer *Org Src rpasyncio.org[ python ]*> (python-mode)
until it stopped by itself.

I traced this to user-errors in my own `org-babel-edit-prep:python' which I
use to let eglot handle python source blocks.

I rewrote my `org-babel-edit-prep:python' to get rid of user-errors but
then I bumped on the user-error coming from `org-src--make-source-overlay'.

I think that this may be the case for other definitions of `org-babel-edit-prep:LANG'

I copy my `org-babel-edit-edit-prep:python' without user-errors below:

#+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.")

  ;; 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))
               (mark (point))
               (body (nth 1 info))
               (filename (cdr (assq :tangle (nth 2 info)))))
      (when (string= filename "no")
        (message "Org source block has no tangled file")
        (setq ok nil))
      (when ok
        (setq filename (expand-file-name filename))
        (unless (file-readable-p filename)
          (message "Tangled file %s is not readable" filename)
          (setq ok nil)))
      (when ok
        (with-temp-buffer
          (insert-file-contents filename 'visit nil nil 'replace)
          (unless (search-forward body nil 'noerror)
(message "Org source block does not occur in tangled file %s"
                     filename)
            (setq ok nil))
          (when (search-forward body nil 'noerror)
(message "Org source block occurs twice or more in tangled file %s"
                     filename)
            (setq ok nil))))
      (when ok
        (goto-char (point-min))
        (insert-file-contents filename 'visit nil nil 'replace)
        (search-forward body)
        (narrow-to-region (match-beginning 0) (match-end 0))
        (goto-char mark)
        (eglot-ensure))
      ;; (message "Buffer %s is no `org-src-mode' buffer" (buffer-name))
      ))

  (defun org-babel-edit-prep:python (info)
    (eglot-org-babel-edit-prep info)))
#+end_src

Regards -- Gerard



reply via email to

[Prev in Thread] Current Thread [Next in Thread]