jupyter-org-insert-sync-results -> jupyter-org-sync-results

Change the behavior of `jupyter-org-sync-results` to return the org formatted
result string and modify the result parameters in `org-babel-execute:jupyter`
to add the "raw" result parameter so that the result string is directly
inserted into the buffer.

This is to work towards better support for in-lined code blocks.
This commit is contained in:
Nathaniel Nicandro 2018-11-30 23:36:04 -06:00
parent c5ebd7bcbd
commit c6efda58b1
No known key found for this signature in database
GPG key ID: C34814B309DD06B8
2 changed files with 51 additions and 18 deletions

View file

@ -927,37 +927,69 @@ request."
(jupyter-org--clear-request-id req))
(jupyter-org--append-result req result))))
(defun jupyter-org--coalesce-stream-results (results)
"Return RESULTS with all contiguous stream results concatenated.
All stream results are then turned into fixed-width or
example-block elements."
(let ((head (nreverse
(cl-reduce
(lambda (a b)
(if (and (jupyter-org--stream-result-p b)
(jupyter-org--stream-result-p (car a)))
(setcar a (concat (car a) b))
(push b a))
a)
results
:initial-value nil))))
(prog1 head
(setq results head)
(while results
(when (jupyter-org--stream-result-p (car results))
(setcar results (jupyter-org-scalar (car results))))
(pop results)))))
;;; org-babel functions
;; These are meant to be called by `org-babel-execute:jupyter'
(defun jupyter-org--restore-message-function ()
(advice-remove 'message #'ignore)
(remove-hook 'pre-command-hook #'jupyter-org--restore-message-function))
(defun jupyter-org--ignore-message-until-command ()
(advice-add 'message :override #'ignore)
(add-hook 'pre-command-hook #'jupyter-org--restore-message-function))
(defun jupyter-org-insert-async-id (req)
"Insert the ID of REQ.
Meant to be used as the return value of
`org-babel-execute:jupyter'."
(prog1 nil
(advice-add 'message :override #'ignore)
(jupyter-org--ignore-message-until-command)
(setq org-babel-after-execute-hook
(let ((hook org-babel-after-execute-hook))
(lambda ()
(advice-remove 'message #'ignore)
(jupyter-org--restore-message-function)
(unwind-protect
(jupyter-org--add-result
req (jupyter-org-scalar (jupyter-org-request-id req)))
(setq org-babel-after-execute-hook hook)
(run-hooks 'org-babel-after-execute-hook)))))))
(defun jupyter-org-insert-sync-results (req)
"Insert the results of REQ.
Meant to be used as the return value of `org-babel-execute:jupyter'."
(prog1 nil
(advice-add 'message :override #'ignore)
(setq org-babel-after-execute-hook
(let ((hook org-babel-after-execute-hook))
(lambda ()
(advice-remove 'message #'ignore)
(unwind-protect
(cl-loop
for result in (nreverse (jupyter-org-request-results req))
do (jupyter-org--add-result req result))
(setq org-babel-after-execute-hook hook)
(run-hooks 'org-babel-after-execute-hook)))))))
(defun jupyter-org-sync-results (req)
"Return the result string in org syntax for the results of REQ.
Meant to be used as the return value of
`org-babel-execute:jupyter'."
(let ((results (jupyter-org--coalesce-stream-results
(nreverse (jupyter-org-request-results req)))))
(when results
(let* ((params (jupyter-org-request-block-params req))
(result-params (alist-get :result-params params)))
(org-element-interpret-data
(if (or (and (= (length results) 1)
(jupyter-org-babel-result-p (car results)))
(member "raw" result-params))
(car results)
(apply #'jupyter-org-results-drawer results)))))))
(provide 'jupyter-org-client)

View file

@ -236,7 +236,8 @@ the PARAMS alist."
(jupyter-org-insert-async-id req))
(t
(jupyter-wait-until-idle req most-positive-fixnum)
(jupyter-org-insert-sync-results req)))))
(nconc (alist-get :result-params params) (list "raw"))
(jupyter-org-sync-results req)))))
(defun org-babel-jupyter-make-language-alias (kernel lang)
"Simimilar to `org-babel-make-language-alias' but for Jupyter src-blocks.