Skip to content

Commit 06544c5

Browse files
committed
Light refactoring and suppress an error in one instance
* jupyter-org-client.el (jupyter-org--with-src-block-client): New function. same as what the non-private version did, in addition, use the new NOERROR argument of `org-babel-jupyter-session-initiated-p`. (jupyter-org-with-src-block-client): Move the work to the above so as to reduce clutter of the macro expansion. (jupyter-org--insert-stream): Remove a `setq`. * ob-jupyter.el (org-babel-jupyter-session-key): Add optional argument, NOERROR, that returns nil if a session key could not be formed instead of raising an error. (org-babel-jupyter-session-initiated-p): Add optional argument, same as above. * jupyter-repl.el (jupyter-repl-interaction-mode-line): Use `pcase`.
1 parent 3ead15c commit 06544c5

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

jupyter-org-client.el

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,20 @@ returned."
657657
(when (jupyter-org--same-src-block-p)
658658
,@body)))
659659

660+
(defun jupyter-org--with-src-block-client (thunk)
661+
(jupyter-org-when-in-src-block
662+
(let ((params (car jupyter-org--src-block-cache)))
663+
(when (or jupyter-org-auto-connect
664+
(org-babel-jupyter-session-initiated-p params 'noerror))
665+
(let* ((buffer (org-babel-jupyter-initiate-session
666+
(alist-get :session params) params))
667+
(jupyter-current-client
668+
(buffer-local-value 'jupyter-current-client buffer))
669+
(syntax (jupyter-kernel-language-syntax-table
670+
jupyter-current-client)))
671+
(with-syntax-table syntax
672+
(funcall thunk)))))))
673+
660674
(defmacro jupyter-org-with-src-block-client (&rest body)
661675
"Evaluate BODY with `jupyter-current-client' set to the session's client.
662676
A client is initialized if needed when `jupyter-org-auto-connect'
@@ -670,21 +684,8 @@ when it is evaluated.
670684
In addition to evaluating BODY with an active Jupyter client set,
671685
the `syntax-table' will be set to that of the REPL buffer's."
672686
(declare (debug (body)))
673-
(let ((params (make-symbol "params"))
674-
(syntax (make-symbol "syntax"))
675-
(buffer (make-symbol "buffer")))
676-
`(jupyter-org-when-in-src-block
677-
(let ((,params (car jupyter-org--src-block-cache)))
678-
(when (or jupyter-org-auto-connect
679-
(org-babel-jupyter-session-initiated-p ,params))
680-
(let* ((,buffer (org-babel-jupyter-initiate-session
681-
(alist-get :session ,params) ,params))
682-
(jupyter-current-client
683-
(buffer-local-value 'jupyter-current-client ,buffer))
684-
(,syntax (jupyter-kernel-language-syntax-table
685-
jupyter-current-client)))
686-
(with-syntax-table ,syntax
687-
,@body)))))))
687+
`(jupyter-org--with-src-block-client
688+
(lambda () ,@body)))
688689

689690
(cl-defmethod jupyter-code-context ((_type (eql inspect))
690691
&context (major-mode org-mode))
@@ -1737,10 +1738,12 @@ If INDENTATION is nil, it defaults to `current-indentation'."
17371738
(defun jupyter-org--insert-nonstream (context result)
17381739
(cond
17391740
((jupyter-org--first-result-context-p context)
1740-
(unless (jupyter-org-babel-result-p result)
1741-
;; Wrap the result if it can't be removed by `org-babel'.
1742-
(setq result (jupyter-org-results-drawer result)))
1743-
(insert (org-element-interpret-data result)))
1741+
(insert (org-element-interpret-data
1742+
(if (jupyter-org-babel-result-p result)
1743+
result
1744+
;; Wrap the result if it can't be removed by
1745+
;; `org-babel'.
1746+
(jupyter-org-results-drawer result)))))
17441747
(t
17451748
(let ((elems (jupyter-org--prepare-context context)))
17461749
(insert (org-element-interpret-data

jupyter-repl.el

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,14 +1958,15 @@ Do so only if possible in the `current-buffer'."
19581958
`*' means the kernel is busy, `-' means the kernel is idle and
19591959
the REPL is connected, `x' means the REPL is disconnected
19601960
from the kernel."
1961-
(and (cl-typep jupyter-current-client 'jupyter-repl-client)
1962-
(concat " JuPy["
1963-
(cond
1964-
((not (jupyter-hb-beating-p jupyter-current-client)) "x")
1965-
((equal (jupyter-execution-state jupyter-current-client) "busy")
1966-
"*")
1967-
(t "-"))
1968-
"]")))
1961+
(pcase jupyter-current-client
1962+
((and client (cl-type jupyter-repl-client))
1963+
(concat " JuPy["
1964+
(cond
1965+
((not (jupyter-hb-beating-p client)) "x")
1966+
((equal (jupyter-execution-state client) "busy")
1967+
"*")
1968+
(t "-"))
1969+
"]"))))
19691970

19701971
(defun jupyter-repl-pop-to-buffer ()
19711972
"Switch to the REPL buffer of the `jupyter-current-client'."

ob-jupyter.el

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,22 @@ table for the source block at `point'.")
102102
(advice-member-p
103103
'ob-jupyter (intern (concat "org-babel-execute:" lang)))))
104104

105-
(defun org-babel-jupyter-session-key (params)
105+
(defun org-babel-jupyter-session-key (params &optional noerror)
106106
"Return a string that is the concatenation of the :session and :kernel PARAMS.
107107
PARAMS is the arguments alist as returned by
108108
`org-babel-get-src-block-info'. The returned string can then be
109-
used to identify unique Jupyter Org babel sessions."
109+
used to identify unique Jupyter Org babel sessions.
110+
111+
If NOERROR is non-nil and PARAMS does not have valid information
112+
to identify a session return nil, otherwise raise an error."
110113
;; Take into account a Lisp expression as a session name.
111114
(let ((session (org-babel-read (alist-get :session params)))
112115
(kernel (alist-get :kernel params)))
113-
(unless (and session kernel
114-
(not (equal session "none")))
115-
(error "Need a valid session and a kernel to form a key"))
116-
(concat session "-" kernel)))
116+
(if (and session kernel
117+
(not (equal session "none")))
118+
(concat session "-" kernel)
119+
(unless noerror
120+
(error "Need a valid session and a kernel to form a key")))))
117121

118122
(defun org-babel-jupyter-src-block-session ()
119123
"Return the session key for the current Jupyter source block.
@@ -385,9 +389,13 @@ session."
385389
;; TODO: Would we always want to do this?
386390
(jupyter-server-name-client-kernel client sname)))))))
387391

388-
(defun org-babel-jupyter-session-initiated-p (params)
389-
"Return non-nil if the session corresponding to PARAMS is initiated."
390-
(let ((key (org-babel-jupyter-session-key params)))
392+
(defun org-babel-jupyter-session-initiated-p (params &optional noerror)
393+
"Return non-nil if the session corresponding to PARAMS is initiated.
394+
If NOERROR is nil, raise an error if PARAMS doesn't contain valid
395+
information to identify a session, e.g. the `:session' key is
396+
\"none\". Otherwise, for a non-nil NOERROR and when PARAMS
397+
doesn't contain valid information, return nil."
398+
(when-let* ((key (org-babel-jupyter-session-key params noerror)))
391399
(gethash key org-babel-jupyter-session-clients)))
392400

393401
(defun org-babel-jupyter-initiate-session-by-key (session params)

0 commit comments

Comments
 (0)