2015-05-05 21:22:36 -05:00
|
|
|
|
|
2012-06-07 15:56:38 +02:00
|
|
|
|
;;; ein-traceback.el --- Traceback module
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2012- Takafumi Arakaki
|
|
|
|
|
|
2012-07-01 20:18:05 +02:00
|
|
|
|
;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
|
2012-06-07 15:56:38 +02:00
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; ein-traceback.el 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 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; ein-traceback.el 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 ein-traceback.el. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
(require 'eieio)
|
|
|
|
|
(require 'ewoc)
|
|
|
|
|
(require 'ansi-color)
|
|
|
|
|
|
2012-08-28 15:26:32 +02:00
|
|
|
|
(require 'ein-core)
|
2018-10-11 16:53:02 -04:00
|
|
|
|
(require 'ein-shared-output)
|
2012-06-07 15:56:38 +02:00
|
|
|
|
|
|
|
|
|
(defclass ein:traceback ()
|
|
|
|
|
((tb-data :initarg :tb-data :type list)
|
2018-01-10 09:14:58 -06:00
|
|
|
|
(notebook :initarg :source-notebook ;; :type ein:$notebook
|
2018-02-28 06:32:14 -06:00
|
|
|
|
:accessor ein:traceback-notebook)
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(buffer-name :initarg :buffer-name :type string)
|
|
|
|
|
(buffer :initarg :buffer :type buffer)
|
|
|
|
|
(ewoc :initarg :ewoc :type ewoc)))
|
|
|
|
|
|
2012-08-18 22:52:14 +02:00
|
|
|
|
(ein:deflocal ein:%traceback% nil
|
2012-06-07 15:56:38 +02:00
|
|
|
|
"Buffer local variable to store an instance of `ein:traceback'.")
|
|
|
|
|
|
2012-08-14 20:39:05 +02:00
|
|
|
|
(defvar ein:tb-buffer-name-template "*ein:tb %s/%s*")
|
|
|
|
|
|
2015-05-05 21:22:36 -05:00
|
|
|
|
(defun ein:tb-new (buffer-name notebook)
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(make-instance 'ein:traceback
|
|
|
|
|
:buffer-name buffer-name
|
|
|
|
|
:source-notebook notebook))
|
2012-06-07 15:56:38 +02:00
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-get-buffer ((traceback ein:traceback))
|
2012-06-07 16:08:34 +02:00
|
|
|
|
(unless (and (slot-boundp traceback :buffer)
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(buffer-live-p (slot-value traceback 'buffer)))
|
|
|
|
|
(let ((buf (get-buffer-create (slot-value traceback 'buffer-name))))
|
|
|
|
|
(setf (slot-value traceback 'buffer) buf)))
|
|
|
|
|
(slot-value traceback 'buffer))
|
2012-06-07 15:56:38 +02:00
|
|
|
|
|
|
|
|
|
(defun ein:tb-pp (ewoc-data)
|
|
|
|
|
(insert (ansi-color-apply ewoc-data)))
|
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-render ((traceback ein:traceback) tb-data)
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(with-current-buffer (ein:tb-get-buffer traceback)
|
2012-08-18 22:52:14 +02:00
|
|
|
|
(setq ein:%traceback% traceback)
|
2012-06-07 16:08:34 +02:00
|
|
|
|
(setq buffer-read-only t)
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(let ((inhibit-read-only t)
|
2012-08-16 17:03:53 +02:00
|
|
|
|
(ewoc (ein:ewoc-create #'ein:tb-pp)))
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(erase-buffer)
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(setf (slot-value traceback 'ewoc) ewoc)
|
|
|
|
|
(setf (slot-value traceback 'tb-data) tb-data)
|
2012-06-07 16:08:34 +02:00
|
|
|
|
(mapc (lambda (data) (ewoc-enter-last ewoc data)) tb-data))
|
2012-06-07 16:38:01 +02:00
|
|
|
|
(ein:traceback-mode)))
|
2012-06-07 15:56:38 +02:00
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-popup ((traceback ein:traceback) tb-data)
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(ein:tb-render traceback tb-data)
|
|
|
|
|
(pop-to-buffer (ein:tb-get-buffer traceback)))
|
|
|
|
|
|
2012-09-01 20:51:55 +02:00
|
|
|
|
;;;###autoload
|
2012-08-14 20:39:05 +02:00
|
|
|
|
(defun ein:tb-show ()
|
2012-09-01 21:32:20 +02:00
|
|
|
|
"Show full traceback in traceback viewer."
|
2012-08-14 20:39:05 +02:00
|
|
|
|
(interactive)
|
|
|
|
|
(unless
|
|
|
|
|
(ein:and-let* ((tb-data (ein:get-traceback-data))
|
2018-01-10 09:14:58 -06:00
|
|
|
|
(url-or-port (or (ein:get-url-or-port)
|
|
|
|
|
(ein:get-url-or-port--shared-output)))
|
|
|
|
|
(kernel (or (ein:get-kernel)
|
|
|
|
|
(ein:get-kernel--shared-output)))
|
2012-08-15 20:54:22 +02:00
|
|
|
|
(kr-id (ein:kernel-id kernel))
|
2012-08-14 20:39:05 +02:00
|
|
|
|
(tb-name (format ein:tb-buffer-name-template
|
2012-08-15 20:54:22 +02:00
|
|
|
|
url-or-port kr-id)))
|
2018-01-10 09:14:58 -06:00
|
|
|
|
(ein:tb-popup (ein:tb-new tb-name (ein:get-notebook)) tb-data)
|
2012-08-14 20:39:05 +02:00
|
|
|
|
t)
|
|
|
|
|
(error "No traceback is available.")))
|
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-range-of-node-at-point ((traceback ein:traceback))
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(let* ((ewoc (slot-value traceback 'ewoc))
|
2012-06-07 18:20:25 +02:00
|
|
|
|
(ewoc-node (ewoc-locate ewoc))
|
|
|
|
|
(beg (ewoc-location ewoc-node))
|
2012-06-07 18:42:45 +02:00
|
|
|
|
(end (ein:aand (ewoc-next ewoc ewoc-node) (ewoc-location it))))
|
|
|
|
|
(list beg end)))
|
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-file-path-at-point ((traceback ein:traceback))
|
2012-06-07 18:42:45 +02:00
|
|
|
|
(destructuring-bind (beg end)
|
|
|
|
|
(ein:tb-range-of-node-at-point traceback)
|
2012-12-06 23:16:17 +01:00
|
|
|
|
(let* ((file-tail
|
|
|
|
|
(if (>= emacs-major-version 24)
|
|
|
|
|
(next-single-property-change beg 'font-lock-face nil end)
|
|
|
|
|
;; For Emacs 23.x:
|
|
|
|
|
(next-single-property-change beg 'face nil end)))
|
2012-06-07 18:42:45 +02:00
|
|
|
|
(file (when file-tail
|
|
|
|
|
(buffer-substring-no-properties beg file-tail))))
|
|
|
|
|
(if (string-match "\\.pyc$" file)
|
|
|
|
|
(concat (file-name-sans-extension file) ".py")
|
|
|
|
|
file))))
|
2012-06-07 18:20:25 +02:00
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-file-lineno-at-point ((traceback ein:traceback))
|
2012-06-07 18:42:45 +02:00
|
|
|
|
(destructuring-bind (beg end)
|
|
|
|
|
(ein:tb-range-of-node-at-point traceback)
|
2012-06-07 18:20:25 +02:00
|
|
|
|
(when (save-excursion
|
|
|
|
|
(goto-char beg)
|
|
|
|
|
(search-forward-regexp "^[-]+> \\([0-9]+\\)" end t))
|
|
|
|
|
(string-to-number (match-string 1)))))
|
|
|
|
|
|
2017-07-12 14:38:04 -05:00
|
|
|
|
(defmethod ein:tb-jump-to-source-at-point ((traceback ein:traceback)
|
2012-06-07 19:04:32 +02:00
|
|
|
|
&optional select)
|
2012-06-07 18:20:25 +02:00
|
|
|
|
(let ((file (ein:tb-file-path-at-point traceback))
|
|
|
|
|
(lineno (ein:tb-file-lineno-at-point traceback)))
|
2015-05-05 21:22:36 -05:00
|
|
|
|
(if (string-match "<ipython-input-\\([0-9]+\\)-.*" file)
|
|
|
|
|
(let* ((cellnum (string-to-number (match-string 1 file)))
|
2017-03-16 16:55:10 -05:00
|
|
|
|
(nb (slot-value traceback 'notebook))
|
2015-05-05 21:22:36 -05:00
|
|
|
|
(ws (first (ein:$notebook-worksheets nb)))
|
|
|
|
|
(cells (ein:worksheet-get-cells ws))
|
2017-04-07 08:18:41 -05:00
|
|
|
|
(it (cl-find cellnum cells :key #'(lambda (x)
|
2015-05-12 15:12:33 -05:00
|
|
|
|
(if (same-class-p x 'ein:codecell)
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(slot-value x 'input-prompt-number))))))
|
2015-05-12 15:12:33 -05:00
|
|
|
|
(if it
|
|
|
|
|
(progn
|
|
|
|
|
(pop-to-buffer (ein:notebook-buffer nb))
|
2015-05-26 15:57:50 +02:00
|
|
|
|
(ein:cell-goto-line it lineno))))
|
2018-02-28 06:32:14 -06:00
|
|
|
|
(let ((url-or-port (ein:$notebook-url-or-port (ein:traceback-notebook traceback))))
|
|
|
|
|
(cond
|
|
|
|
|
((numberp url-or-port) (ein:tb-jtsap--local file lineno select))
|
|
|
|
|
((string-match "localhost" url-or-port) (ein:tb-jtsap--local file lineno select))
|
|
|
|
|
((string-match "127.0.0.1" url-or-port) (ein:tb-jtsap--local file lineno select))
|
|
|
|
|
(t (ein:tb-jtsap--remote url-or-port file lineno select)))))))
|
|
|
|
|
|
|
|
|
|
(defun ein:tb-jtsap--local (file lineno select)
|
|
|
|
|
(assert (file-exists-p file) nil "File %s does not exist." file)
|
|
|
|
|
(let ((buf (find-file-noselect file))
|
|
|
|
|
(scroll (lambda ()
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(forward-line (1- lineno)))))
|
|
|
|
|
(if select
|
|
|
|
|
(progn (pop-to-buffer buf)
|
|
|
|
|
(funcall scroll))
|
|
|
|
|
(with-selected-window (display-buffer buf)
|
|
|
|
|
(funcall scroll)))))
|
|
|
|
|
|
|
|
|
|
(defun ein:tb-jtsap--remote (uri path lineno select)
|
|
|
|
|
(let* ((uri (url-generic-parse-url uri))
|
|
|
|
|
(host-path (concat "/" (url-host uri)
|
|
|
|
|
":" path)))
|
|
|
|
|
(ein:tb-jtsap--local host-path lineno select)))
|
2012-06-07 19:04:32 +02:00
|
|
|
|
|
|
|
|
|
(defun ein:tb-jump-to-source-at-point-command (&optional select)
|
|
|
|
|
(interactive "P")
|
2012-08-18 22:52:14 +02:00
|
|
|
|
(ein:tb-jump-to-source-at-point ein:%traceback% select))
|
2012-06-07 16:38:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ein:traceback-mode
|
|
|
|
|
|
|
|
|
|
(defun ein:tb-prev-item ()
|
|
|
|
|
(interactive)
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(ewoc-goto-prev (slot-value ein:%traceback% 'ewoc) 1))
|
2012-06-07 16:38:01 +02:00
|
|
|
|
|
|
|
|
|
(defun ein:tb-next-item ()
|
|
|
|
|
(interactive)
|
2017-03-16 16:42:03 -05:00
|
|
|
|
(ewoc-goto-next (slot-value ein:%traceback% 'ewoc) 1))
|
2012-06-07 16:38:01 +02:00
|
|
|
|
|
2016-09-13 09:32:31 -04:00
|
|
|
|
(defvar ein:traceback-mode-map
|
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
|
(define-key map (kbd "RET") 'ein:tb-jump-to-source-at-point-command)
|
|
|
|
|
(define-key map "p" 'ein:tb-prev-item)
|
|
|
|
|
(define-key map "n" 'ein:tb-next-item)
|
|
|
|
|
map)
|
|
|
|
|
"Keymap for ein:traceback-mode.")
|
2012-06-07 16:38:01 +02:00
|
|
|
|
|
2016-09-19 09:02:35 -05:00
|
|
|
|
(define-derived-mode ein:traceback-mode special-mode "ein:tb"
|
2012-06-07 16:38:01 +02:00
|
|
|
|
(font-lock-mode))
|
|
|
|
|
|
2012-06-11 19:14:09 +02:00
|
|
|
|
(add-hook 'ein:traceback-mode-hook 'ein:truncate-lines-on)
|
|
|
|
|
|
2012-06-07 15:56:38 +02:00
|
|
|
|
(provide 'ein-traceback)
|
|
|
|
|
|
|
|
|
|
;;; ein-traceback.el ends here
|