-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathein-python-send.el
160 lines (141 loc) · 5.79 KB
/
ein-python-send.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;; ein-python-send.el --- Ad hoc sending of code fragments to kernel -*- lexical-binding: t -*-
;; Copyright (C) 2012- The Authors
;; This file is NOT part of GNU Emacs.
;; ein-python-send.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-python-send.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-python-send.el. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; python parsing code by authors of elpy (Schaefer et al)
;;; Code:
(autoload 'ein:get-notebook "ein-notebook")
(defvar ein:python-send-map)
(defun ein:python-send--prepare (&optional reset)
(cl-assert (boundp 'ein:python-send-map) nil
"ein:python-send--prepare: %s not called"
"ein:python-send--init")
(unless (and (buffer-live-p (current-buffer))
(eq major-mode 'python-mode))
(error "ein:python-send--prepare: %s is not a python buffer" (buffer-name)))
(when (or (not (ein:get-notebook)) reset)
(aif (ein:notebook-opened-notebooks)
(let ((choice
(ein:completing-read
"Notebook: "
(mapcar (lambda (nb) (ein:$notebook-notebook-name nb)) it))))
(setq ein:%notebook% (seq-find
(lambda (nb)
(string= choice (ein:$notebook-notebook-name nb)))
it)))
(error "ein:python-send--prepare: No open notebooks"))))
(defun ein:python-send-region-or-buffer (&optional reset)
"Based on `elpy-shell--send-region-or-buffer-internal' by Schaefer et al."
(interactive "P")
(ein:python-send--prepare reset)
(if (use-region-p)
(let ((region (python-shell-buffer-substring
(region-beginning) (region-end))))
(when (string-match "\t" region)
(message "Region contained tabs, this might cause weird errors"))
;; python-shell-buffer-substring (intentionally?) does not accurately
;; respect (region-beginning); it always start on the first character
;; of the respective line even if that's before the region beginning
;; Here we post-process the output to remove the characters before
;; (region-beginning) and the start of the line. The end of the region
;; is handled correctly and needs no special treatment.
(let* ((bounds (save-excursion
(goto-char (region-beginning))
(bounds-of-thing-at-point 'line)))
(used-part (string-trim
(buffer-substring-no-properties
(car bounds)
(min (cdr bounds) (region-end)))))
(relevant-part (string-trim
(buffer-substring-no-properties
(max (car bounds) (region-beginning))
(min (cdr bounds) (region-end))))))
(setq region
;; replace just first match
(replace-regexp-in-string
(concat "\\(" (regexp-quote used-part) "\\)\\(?:.*\n?\\)*\\'")
relevant-part
region t t 1))
(ein:shared-output-eval-string (ein:get-kernel) region)))
(ein:shared-output-eval-string (ein:get-kernel) (buffer-string)))
(if (use-region-p)
(progn
(goto-char (region-end))
(deactivate-mark))
(goto-char (point-max))))
(defun ein:python-send-statement (&optional reset)
"Based on `elpy-shell-send-statement' by Schaefer et al."
(interactive "P")
(ein:python-send--prepare reset)
(python-nav-beginning-of-statement)
(unless (looking-at "[[:space:]]*$")
(let ((beg (save-excursion (beginning-of-line) (point)))
(end (progn (ein:python-send--nav-end-of-statement) (point))))
(unless (eq beg end)
(ein:shared-output-eval-string (ein:get-kernel)
(buffer-substring beg end))))))
(defun ein:python-send--nav-end-of-statement ()
"Based on `elpy-shell--nav-end-of-statement' by Schaefer et al."
(let ((continue t)
p)
(while (and (not (eq p (point))) continue)
;; is there another block at same indentation level?
(setq p (point))
(ein:python-send--nav-forward-block)
(if (eq p (point))
(progn
;; nope, go to the end of the block and done
(python-nav-end-of-block)
(setq continue nil))
(unless (eq 0 (string-match-p "\\s-*el\\(?:se:\\|if[^\w]\\)"
(thing-at-point 'line)))
(forward-line -1)
(while (and (or (eq (string-match-p "\\s-*$" (thing-at-point 'line)) 0)
(python-info-current-line-comment-p))
(not (eq (point) (point-min))))
(forward-line -1))
(setq continue nil)))))
(end-of-line))
(defun ein:python-send--nav-forward-block ()
"Based on `elpy-shell--nav-forward-block' by Schaefer et al.
Move to the next line indented like point. This will skip over lines and
statements with different indentation levels."
(interactive "^")
(let ((indent (current-column))
(start (point))
(cur nil))
(when (/= (% indent python-indent-offset)
0)
(setq indent (* (1+ (/ indent python-indent-offset))
python-indent-offset)))
(python-nav-forward-statement)
(while (and (< indent (current-indentation))
(not (eobp)))
(when (equal (point) cur)
(error "Statement does not finish"))
(setq cur (point))
(python-nav-forward-statement))
(when (< (current-indentation)
indent)
(goto-char start))))
(defun ein:python-send--init ()
(unless (boundp 'ein:python-send-map)
(require 'python)
(setq ein:python-send-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "e") 'ein:python-send-statement)
(define-key map (kbd "r") 'ein:python-send-region-or-buffer)
map))
(define-key python-mode-map (kbd "C-c C-/") ein:python-send-map)))
(provide 'ein-python-send)
;;; ein-python-send.el ends here