34
34
(declare-function org-babel-python-table-or-string " ob-python" (results))
35
35
(declare-function org-babel-jupyter-initiate-session " ob-jupyter" (&optional session params))
36
36
(declare-function org-babel-jupyter-src-block-session " ob-jupyter" ())
37
+ (declare-function org-babel-jupyter-session-initiated-p " ob-jupyter" (params))
37
38
(declare-function org-babel-jupyter-language-p " ob-jupyter" (lang))
38
39
(declare-function org-element-context " org-element" (&optional element))
39
40
(declare-function org-element-create " org-element" (type &optional props &rest children))
55
56
(declare-function org-inside-LaTeX-fragment-p " org" ())
56
57
(declare-function org-toggle-latex-fragment " org" (&optional arg))
57
58
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
+
58
72
(defcustom jupyter-org-resource-directory " ./.ob-jupyter/"
59
73
" Directory used to store automatically generated image files.
60
74
See `jupyter-org-image-file-name' ."
@@ -417,7 +431,8 @@ returned."
417
431
418
432
(defmacro jupyter-org-with-src-block-client (&rest body )
419
433
" 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.
421
436
422
437
If `point' is not inside the code of a Jupyter source block, BODY
423
438
is 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,
427
442
the `syntax-table' will be set to that of the REPL buffers."
428
443
(declare (debug (body)))
429
444
(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" )))
431
448
`(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 )))))))
441
460
442
461
(cl-defmethod jupyter-code-context ((_type (eql inspect))
443
462
&context (major-mode org-mode))
0 commit comments