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,27 +1045,28 @@ insertion into the buffer."
;;;;; Append stream result ;;;;; Append stream result
(defun jupyter-org--append-stream-result (result keep-newline) (defun jupyter-org--append-stream-result (result)
(if (eq (char-after (line-beginning-position)) ?:) (let ((keep-newline (get-text-property (point) 'jupyter-stream-newline)))
(jupyter-org--append-to-fixed-width result keep-newline) (if (eq (char-after (line-beginning-position)) ?:)
;; Append at the end of the current example-block. In this case (jupyter-org--append-to-fixed-width result keep-newline)
;; POS is the end of the last line of contents. ;; Append at the end of the current example-block. In this case
;; ;; POS is the end of the last line of contents.
;; ... ;;
;; foo| ;; ...
;; #+END_EXAMPLE ;; foo|
;; ;; #+END_EXAMPLE
;; Delete the newline that will be re-inserted by the call to ;;
;; `org-element-normalize-string'. ;; Delete the newline that will be re-inserted by the call to
(delete-char 1) ;; `org-element-normalize-string'.
(insert (concat (when keep-newline "\n") (delete-char 1)
(org-element-normalize-string result))))) (insert (concat (when keep-newline "\n")
(org-element-normalize-string result))))))
;;;; Value results ;;;; Value results
;;;;; Append value result ;;;;; Append value result
(defun jupyter-org--append-value-result (context result) (defun jupyter-org--insert-result (context result)
(insert (org-element-interpret-data (insert (org-element-interpret-data
(jupyter-org--wrap-result-maybe (jupyter-org--wrap-result-maybe
context (if (jupyter-org--stream-result-p result) context (if (jupyter-org--stream-result-p result)
@ -1079,15 +1080,11 @@ insertion into the buffer."
;;;; Add/append results ;;;; Add/append results
(defun jupyter-org--delete-element-maybe (element) (defun jupyter-org--delete-unwrapped-result (element)
"Delete ELEMENT from the buffer. "Delete ELEMENT from the buffer.
If ELEMENT represents a previous result it, along with the result If ELEMENT represents a previous result it, along with the result
about to be inserted, will be wrapped in a drawer." about to be inserted, will be wrapped in a drawer."
(cl-case (org-element-type element) (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 ;; The `org-element-contents' of a table is nil which interferes with how
;; `org-element-table-interpreter' works when calling ;; `org-element-table-interpreter' works when calling
;; `org-element-interpret-data' so set the contents and delete ELEMENT from the ;; `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 (cond
(stream-append-pos (stream-append-pos
(goto-char stream-append-pos) (goto-char stream-append-pos)
(let ((keep-newline (get-text-property (jupyter-org--append-stream-result result))
(point) 'jupyter-stream-newline)))
(jupyter-org--append-stream-result result keep-newline)))
(t (t
(if (eq (org-element-type context) 'drawer) (cl-case (org-element-type context)
(goto-char (jupyter-org-element-contents-end context)) ;; Go to the end of the drawer to insert the new result.
(jupyter-org--delete-element-maybe context)) (drawer
(jupyter-org--append-value-result context result) (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 jupyter-org-toggle-latex
(when (memq (org-element-type result) (when (memq (org-element-type result)
'(latex-fragment latex-environment)) '(latex-fragment latex-environment))