jupyter-org-client.el: More refactoring

This commit is contained in:
Nathaniel Nicandro 2019-02-17 23:05:00 -06:00
parent 2119f29def
commit 4c7d7b390b

View file

@ -1045,7 +1045,8 @@ insertion into the buffer."
;;;;; Append stream result
(defun jupyter-org--append-stream-result (result keep-newline)
(defun jupyter-org--append-stream-result (result)
(let ((keep-newline (get-text-property (point) 'jupyter-stream-newline)))
(if (eq (char-after (line-beginning-position)) ?:)
(jupyter-org--append-to-fixed-width result keep-newline)
;; Append at the end of the current example-block. In this case
@ -1059,13 +1060,13 @@ insertion into the buffer."
;; `org-element-normalize-string'.
(delete-char 1)
(insert (concat (when keep-newline "\n")
(org-element-normalize-string result)))))
(org-element-normalize-string result))))))
;;;; Value results
;;;;; Append value result
(defun jupyter-org--append-value-result (context result)
(defun jupyter-org--insert-result (context result)
(insert (org-element-interpret-data
(jupyter-org--wrap-result-maybe
context (if (jupyter-org--stream-result-p result)
@ -1079,15 +1080,11 @@ insertion into the buffer."
;;;; Add/append results
(defun jupyter-org--delete-element-maybe (element)
(defun jupyter-org--delete-unwrapped-result (element)
"Delete ELEMENT from the buffer.
If ELEMENT represents a previous result it, along with the result
about to be inserted, will be wrapped in a drawer."
(cl-case (org-element-type element)
;; Don't delete the current result if its a drawer or if there
;; aren't any results yet, in this case the context is just the
;; #+RESULTS: keyword.
((keyword drawer))
;; The `org-element-contents' of a table is nil which interferes with how
;; `org-element-table-interpreter' works when calling
;; `org-element-interpret-data' so set the contents and delete ELEMENT from the
@ -1117,14 +1114,21 @@ about to be inserted, will be wrapped in a drawer."
(cond
(stream-append-pos
(goto-char stream-append-pos)
(let ((keep-newline (get-text-property
(point) 'jupyter-stream-newline)))
(jupyter-org--append-stream-result result keep-newline)))
(jupyter-org--append-stream-result result))
(t
(if (eq (org-element-type context) 'drawer)
(goto-char (jupyter-org-element-contents-end context))
(jupyter-org--delete-element-maybe context))
(jupyter-org--append-value-result context result)
(cl-case (org-element-type context)
;; Go to the end of the drawer to insert the new result.
(drawer
(goto-char (jupyter-org-element-contents-end context)))
;; Insert the first result. In this case `point' is at the first
;; line after the #+RESULTS keyword.
(keyword nil)
;; Any other context is a previous result. Remove it from the
;; buffer since it, along with the new result will be wrapped in a
;; drawer and re-inserted into the buffer.
(t
(jupyter-org--delete-unwrapped-result context)))
(jupyter-org--insert-result context result)
(when jupyter-org-toggle-latex
(when (memq (org-element-type result)
'(latex-fragment latex-environment))