emacs-jupyter/jupyter-python.el
Nathaniel Nicandro a819067e6c Implement the jupyter-org-result method
This is in replacement of the `jupyter-org-prepare-result` and
`jupyter-org-transform-result`. A single method with both primary and :around
methods is sufficient.

* jupyter-org-client.el (jupyter-org-mime-types): New variable.
(jupyter-org-prepare-and-add-result): Remove.
(jupyter-handle-execute-result, jupyter-handle-display-data):
Do the work previously done by `jupyter-org-prepare-and-add-result`.
Abstract out the python handling to a `jupyter-org-result` method.
(jupyter-org--image-result): Add PARAMS argument.
Move setup done previously in `jupyter-org-prepare-result` to this function.
(jupyter-org-prepare-result): Remove.
Replace with calls to the `jupyter-org-result' method.
(jupyter-org-result): New method.
(text/html) Remove special handling of <img> tags, this was done due to old
versions of the Julia kernel.
(jupyter-org-transform-result): Remove.
Replace with a `jupyter-org-result` :around method.
Remove all callers.

* jupyter-python.el (jupyter-org-transform-result): Remove.
Replace with a `jupyter-org-result` :around method.

* jupyter-tests.el (jupyter-org-result): Add tests.

org-result
2018-11-13 17:46:07 -06:00

86 lines
3.2 KiB
EmacsLisp

;;; jupyter-python.el --- Jupyter support for python -*- lexical-binding: t -*-
;; Copyright (C) 2018 Nathaniel Nicandro
;; Author: Nathaniel Nicandro <nathanielnicandro@gmail.com>
;; Created: 23 Oct 2018
;; Version: 0.0.1
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(require 'jupyter-repl)
(require 'jupyter-org-client)
(declare-function org-babel-python-table-or-string "ob-python")
(cl-defmethod jupyter-handle-error :after ((client jupyter-repl-client)
req ename _evalue _traceback
&context (jupyter-lang python))
"Add spacing between the first occurance of ENAME and \"Traceback\".
Do this only when the traceback of REQ was inserted into the REPL
buffer."
(unless (eq (jupyter-message-parent-type
(jupyter-request-last-message req))
:comm-msg)
(jupyter-with-repl-buffer client
(save-excursion
(jupyter-repl-goto-cell req)
(goto-char (jupyter-repl-cell-code-end-position))
(when (and (search-forward ename nil t)
(looking-at "Traceback"))
(let ((len (- fill-column
jupyter-repl-prompt-margin-width
(- (point) (line-beginning-position))
(- (line-end-position) (point)))))
(jupyter-repl-insert
(make-string (if (> len 4) len 4) ? ))))))))
(cl-defmethod jupyter-insert :around ((msg cons)
&context (jupyter-lang python)
&rest _)
"Fontify docstrings after inserting inspect messages."
(let ((mime (cl-call-next-method)))
(prog1 mime
(cond
((and (eq mime :text/plain)
(eq (jupyter-message-type msg) :inspect-reply))
(when (search-forward "Docstring:" nil t)
(jupyter-fontify-region-according-to-mode
#'rst-mode (1+ (point)) (point-max))))
(t nil)))))
(cl-defmethod jupyter-load-file-code (file &context (jupyter-lang python))
(concat "%run " file))
(cl-defmethod jupyter-org-result ((_mime (eql :text/plain))
&context (jupyter-lang python)
&rest _)
(let ((result (cl-call-next-method)))
(cond
((and (equal (car result) "scalar")
(stringp (cdr result)))
(cons "scalar" (org-babel-python-table-or-string (cdr result))))
(t result))))
(provide 'jupyter-python)
;;; jupyter-python.el ends here