Hi,

When editing python src block using C-c ', the python shell is not set
in Org edit buffer according to the :session header argument of the
block. Consequently, commands such as python-shell-send-region cannot
send python code to the correct python process. To address this, the
attached patch defines org-babel-edit-prep:python in ob-python.el.

In addition, I tried to use org-src-associate-babel-session at first,
but found it doesn't work because it is called when enabling
org-src-mode, where org-src--babel-info is still nil. It seems it has
stopped working since 203bf5870, and I think it is safe to remove it
and related stuffs to avoid confusion.


Best,
Liu Hui
From c8b9c174cf643bd625cedc311d2604e6fc3bb83a Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1...@gmail.com>
Date: Tue, 5 Dec 2023 11:40:38 +0800
Subject: [PATCH] Set Python shell in Org edit buffer

* lisp/ob-python.el (org-babel-edit-prep:python): New function.
---
 lisp/ob-python.el | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 6c568a36d..8ff2c7a1d 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -67,6 +67,13 @@ (defcustom org-babel-python-None-to 'hline
   :package-version '(Org . "8.0")
   :type 'symbol)
 
+(defun org-babel-edit-prep:python (info)
+  "Set Python shell in Org edit buffer according to INFO."
+  (let ((session (cdr (assq :session (nth 2 info)))))
+    (when (and session (not (string= session "none")))
+      (setq-local python-shell-buffer-name
+                  (org-babel-python-without-earmuffs session)))))
+
 (defun org-babel-execute:python (body params)
   "Execute Python BODY according to PARAMS.
 This function is called by `org-babel-execute-src-block'."
-- 
2.25.1

From f3c0d401de55d7ad8873c18658abd1d9f6b49d77 Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1...@gmail.com>
Date: Tue, 5 Dec 2023 11:36:07 +0800
Subject: [PATCH] Remove 'org-src-babel-configure-edit-buffer' and related
 stuffs

The functionality has stopped working since 203bf5870.
org-babel-edit-prep:lang should be used instead.

* lisp/org-src.el (org-src-associate-babel-session):
(org-src-babel-configure-edit-buffer):
* lisp/ob-R.el (org-babel-R-associate-session): Remove.
---
 lisp/ob-R.el    |  8 --------
 lisp/org-src.el | 23 -----------------------
 2 files changed, 31 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index c48b2cdb7..f3cce20b7 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -294,14 +294,6 @@ (defun org-babel-R-initiate-session (session params)
 	       (buffer-name))))
 	  (current-buffer))))))
 
-(defun org-babel-R-associate-session (session)
-  "Associate R code buffer with an R session.
-Make SESSION be the inferior ESS process associated with the
-current code buffer."
-  (setq ess-local-process-name
-	(process-name (get-buffer-process session)))
-  (ess-make-buffer-current))
-
 (defvar org-babel-R-graphics-devices
   '((:bmp "bmp" "filename")
     (:jpg "jpeg" "filename")
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 866ff2dbf..6406b8d29 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -914,29 +914,6 @@ (defun org-src-mode-configure-edit-buffer ()
 
 (add-hook 'org-src-mode-hook #'org-src-mode-configure-edit-buffer)
 
-
-
-;;; Babel related functions
-
-(defun org-src-associate-babel-session (info)
-  "Associate edit buffer with comint session.
-INFO should be a list similar in format to the return value of
-`org-babel-get-src-block-info'."
-  (interactive)
-  (let ((session (cdr (assq :session (nth 2 info)))))
-    (and session (not (string= session "none"))
-	 (org-babel-comint-buffer-livep session)
-	 (let ((f (intern (format "org-babel-%s-associate-session"
-                                  (nth 0 info)))))
-           (and (fboundp f) (funcall f session))))))
-
-(defun org-src-babel-configure-edit-buffer ()
-  "Configure src editing buffer."
-  (when org-src--babel-info
-    (org-src-associate-babel-session org-src--babel-info)))
-
-(add-hook 'org-src-mode-hook #'org-src-babel-configure-edit-buffer)
-
 
 ;;; Public API
 
-- 
2.25.1

Reply via email to