2018-10-23 21:46:32 -05:00
|
|
|
;;; 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
|
2018-11-16 00:16:00 -06:00
|
|
|
;; Version: 0.6.0
|
2018-10-23 21:46:32 -05:00
|
|
|
|
|
|
|
;; 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:
|
|
|
|
|
2018-11-18 22:52:02 -06:00
|
|
|
;; Support methods for integration with Python.
|
2018-10-23 21:46:32 -05:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2018-10-24 02:16:24 -05:00
|
|
|
(require 'jupyter-repl)
|
2018-10-23 21:46:32 -05:00
|
|
|
(require 'jupyter-org-client)
|
|
|
|
|
|
|
|
(declare-function org-babel-python-table-or-string "ob-python")
|
|
|
|
|
2018-10-27 02:49:13 -05:00
|
|
|
(cl-defmethod jupyter-handle-error :after ((client jupyter-repl-client)
|
|
|
|
req ename _evalue _traceback
|
2018-11-18 11:45:00 -06:00
|
|
|
&context (jupyter-lang python)
|
|
|
|
(major-mode jupyter-repl-mode))
|
2018-10-27 02:49:13 -05:00
|
|
|
"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) ? ))))))))
|
|
|
|
|
2018-11-09 22:59:24 -06:00
|
|
|
(cl-defmethod jupyter-insert :around ((msg cons)
|
|
|
|
&context (jupyter-lang python)
|
|
|
|
&rest _)
|
2018-10-30 18:23:00 -05:00
|
|
|
"Fontify docstrings after inserting inspect messages."
|
2018-11-09 22:59:24 -06:00
|
|
|
(let ((mime (cl-call-next-method)))
|
|
|
|
(prog1 mime
|
|
|
|
(cond
|
|
|
|
((and (eq mime :text/plain)
|
|
|
|
(eq (jupyter-message-type msg) :inspect-reply))
|
2018-11-14 02:21:40 -06:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(when (re-search-forward "^Docstring:" nil t)
|
|
|
|
(jupyter-fontify-region-according-to-mode
|
|
|
|
#'rst-mode (1+ (point))
|
2018-11-19 08:43:02 -06:00
|
|
|
(or (and (re-search-forward "^\\(File\\|Type\\):" nil t)
|
2018-11-14 02:21:40 -06:00
|
|
|
(line-beginning-position))
|
|
|
|
(point-max))))))
|
2018-11-09 22:59:24 -06:00
|
|
|
(t nil)))))
|
2018-10-30 18:23:00 -05:00
|
|
|
|
2018-10-24 02:16:24 -05:00
|
|
|
(cl-defmethod jupyter-load-file-code (file &context (jupyter-lang python))
|
|
|
|
(concat "%run " file))
|
|
|
|
|
2018-11-18 22:49:52 -06:00
|
|
|
;;; `jupyter-org'
|
|
|
|
|
2018-11-10 14:26:00 -06:00
|
|
|
(cl-defmethod jupyter-org-result ((_mime (eql :text/plain))
|
|
|
|
&context (jupyter-lang python)
|
|
|
|
&rest _)
|
|
|
|
(let ((result (cl-call-next-method)))
|
|
|
|
(cond
|
2018-11-18 22:11:12 -06:00
|
|
|
((stringp result)
|
|
|
|
(org-babel-python-table-or-string result))
|
2018-11-10 14:26:00 -06:00
|
|
|
(t result))))
|
2018-10-23 21:46:32 -05:00
|
|
|
|
2018-11-18 22:49:52 -06:00
|
|
|
(cl-defmethod jupyter-org-error-location (&context (jupyter-lang python))
|
|
|
|
(and (or (save-excursion (re-search-forward "^----> \\([0-9]+\\)" nil t))
|
|
|
|
(re-search-forward "^[\t ]*File.+line \\([0-9]+\\)$" nil t))
|
|
|
|
(string-to-number (match-string 1))))
|
|
|
|
|
2018-10-23 21:46:32 -05:00
|
|
|
(provide 'jupyter-python)
|
|
|
|
|
|
|
|
;;; jupyter-python.el ends here
|