-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathein-ipdb.el
126 lines (103 loc) · 4.93 KB
/
ein-ipdb.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; ein-ipdb.el --- Support ipython debugger (ipdb) -*- lexical-binding:t -*-
;; Copyright (C) 2015 - John Miller
;; Author: John Miller <millejoh at mac.com>
;; This file is NOT part of GNU Emacs.
;; ein-ipdb.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-ipdb.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-kernel.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'cl-lib)
(defvar *ein:ipdb-sessions* (make-hash-table)
"Kernel Id to ein:$ipdb-session.")
(declare-function ein:kernel--get-msg "ein-kernel")
(cl-defstruct ein:$ipdb-session buffer kernel prompt notebook)
(defun ein:ipdb-get-session (kernel)
(gethash (ein:$kernel-kernel-id kernel) *ein:ipdb-sessions*))
(defun ein:ipdb-start-session (kernel prompt notebook)
(let* ((buffer (get-buffer-create
(format "*ipdb: %s*"
(ein:$kernel-kernel-id kernel))))
(session (make-ein:$ipdb-session :buffer buffer
:kernel kernel
:prompt prompt
:notebook notebook)))
(puthash (ein:$kernel-kernel-id kernel) session *ein:ipdb-sessions*)
(with-current-buffer buffer
(kill-all-local-variables)
(add-hook 'kill-buffer-hook
(apply-partially #'ein:ipdb-quit-session session) nil t)
(ein:ipdb-mode)
(setq comint-use-prompt-regexp t)
(setq comint-prompt-regexp (concat "^" (regexp-quote prompt)))
(setq comint-input-sender (apply-partially #'ein:ipdb-input-sender session))
(setq comint-prompt-read-only t)
(set (make-local-variable 'comint-output-filter-functions)
'(ansi-color-process-output))
(let ((proc (start-process "ein:ipdb" buffer "cat"))
(sentinel (lambda (process _event)
(when (memq (process-status process) '(exit signal))
(ein:ipdb-cleanup-session session)))))
(set-process-query-on-exit-flag proc nil)
(set-process-sentinel proc sentinel)
(set-marker (process-mark proc) (point))
(comint-output-filter proc (concat "\n" (ein:$ipdb-session-prompt session)))))
(pop-to-buffer buffer)))
(defun ein:ipdb-quit-session (session)
(let* ((kernel (ein:$ipdb-session-kernel session))
(msg (ein:kernel--get-msg kernel "input_reply" (list :value "exit"))))
(ein:websocket-send-stdin-channel kernel msg)))
(defun ein:ipdb-stop-session (session)
(awhen (get-buffer-process (ein:$ipdb-session-buffer session))
(when (process-live-p it)
(kill-process it))))
(defun ein:ipdb-cleanup-session (session)
(let ((kernel (ein:$ipdb-session-kernel session))
(notebook (ein:$ipdb-session-notebook session))
(buffer (ein:$ipdb-session-buffer session)))
(remhash (ein:$kernel-kernel-id kernel) *ein:ipdb-sessions*)
(when (buffer-live-p buffer)
(with-current-buffer buffer
(insert "\nFinished\n")))
(awhen (ein:notebook-buffer notebook)
(when (buffer-live-p it)
(pop-to-buffer it)))))
(defun ein:ipdb--handle-iopub-reply (kernel packet)
(cl-destructuring-bind
(&key content &allow-other-keys)
(ein:json-read-from-string packet)
(-when-let* ((session (ein:ipdb-get-session kernel))
(buffer (ein:$ipdb-session-buffer session))
(prompt (ein:$ipdb-session-prompt session))
(proc (get-buffer-process buffer))
(proc-live-p (process-live-p proc)))
(let ((text (plist-get content :text))
(ename (plist-get content :ename)))
(when (stringp text)
(comint-output-filter proc text))
(if (and (stringp ename) (string-match-p "bdbquit" ename))
(ein:ipdb-stop-session session)
(comint-output-filter proc prompt))))))
(defun ein:ipdb-input-sender (session proc input)
;; in case of eof, comint-input-sender-no-newline is t
(if comint-input-sender-no-newline
(ein:ipdb-quit-session session)
(when (process-live-p proc)
(with-current-buffer (process-buffer proc)
(let* ((buffer-read-only nil)
(kernel (ein:$ipdb-session-kernel session))
(content (list :value input))
(msg (ein:kernel--get-msg kernel "input_reply" content)))
(ein:websocket-send-stdin-channel kernel msg))))))
(define-derived-mode ein:ipdb-mode comint-mode "ein:debugger"
"Run an EIN debug session.
\\<ein:ipdb-mode-map>")
(provide 'ein-ipdb)