3434(declare-function org-babel-python-table-or-string " ob-python" (results))
3535(declare-function org-babel-jupyter-initiate-session " ob-jupyter" (&optional session params))
3636(declare-function org-babel-jupyter-src-block-session " ob-jupyter" ())
37+ (declare-function org-babel-jupyter-session-initiated-p " ob-jupyter" (params))
3738(declare-function org-babel-jupyter-language-p " ob-jupyter" (lang))
3839(declare-function org-element-context " org-element" (&optional element))
3940(declare-function org-element-create " org-element" (type &optional props &rest children))
5556(declare-function org-inside-LaTeX-fragment-p " org" ())
5657(declare-function org-toggle-latex-fragment " org" (&optional arg))
5758
59+ (defcustom jupyter-org-auto-connect t
60+ " Automatically establish a connection to a src-block session.
61+ If this variable is non-nil, then a connection to a src-block
62+ session is automatically established under certain conditions,
63+ e.g. during auto-completion. Otherwise there would have to be an
64+ available connection already if this variable is nil for features
65+ like auto-completion to work.
66+
67+ When this variable is nil, you can establish a connection to a
68+ session by, for example, executing a src-block."
69+ :group 'ob-jupyter
70+ :type 'boolean )
71+
5872(defcustom jupyter-org-resource-directory " ./.ob-jupyter/"
5973 " Directory used to store automatically generated image files.
6074See `jupyter-org-image-file-name' ."
@@ -417,7 +431,8 @@ returned."
417431
418432(defmacro jupyter-org-with-src-block-client (&rest body )
419433 " Evaluate BODY with `jupyter-current-client' set to the session's client.
420- A client is initialized if necessary.
434+ A client is initialized if needed when `jupyter-org-auto-connect'
435+ is non-nil.
421436
422437If `point' is not inside the code of a Jupyter source block, BODY
423438is not evaluated and nil is returned. Return the result of BODY
@@ -427,17 +442,21 @@ In addition to evaluating BODY with an active Jupyter client set,
427442the `syntax-table' will be set to that of the REPL buffers."
428443 (declare (debug (body)))
429444 (let ((params (make-symbol " params" ))
430- (syntax (make-symbol " syntax" )))
445+ (key (make-symbol " key" ))
446+ (syntax (make-symbol " syntax" ))
447+ (buffer (make-symbol " buffer" )))
431448 `(jupyter-org-when-in-src-block
432- (let* ((, params (car jupyter-org--src-block-cache))
433- (jupyter-current-client
434- (buffer-local-value 'jupyter-current-client
435- (org-babel-jupyter-initiate-session
436- (alist-get :session , params ) , params )))
437- (, syntax (jupyter-kernel-language-syntax-table
438- jupyter-current-client)))
439- (with-syntax-table , syntax
440- ,@body )))))
449+ (let ((, params (car jupyter-org--src-block-cache)))
450+ (when (or jupyter-org-auto-connect
451+ (org-babel-jupyter-session-initiated-p , params ))
452+ (let* ((, buffer (org-babel-jupyter-initiate-session
453+ (alist-get :session , params ) , params ))
454+ (jupyter-current-client
455+ (buffer-local-value 'jupyter-current-client , buffer ))
456+ (, syntax (jupyter-kernel-language-syntax-table
457+ jupyter-current-client)))
458+ (with-syntax-table , syntax
459+ ,@body )))))))
441460
442461(cl-defmethod jupyter-code-context ((_type (eql inspect))
443462 &context (major-mode org-mode))
0 commit comments