summaryrefslogtreecommitdiff
path: root/lisp/km-python.el
blob: c651fcc2c8ed42acad18c5da5daf9444fc8499af (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
;;; km-python.el --- Python extensions

;; Copyright (C) 2012-2019 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://gitlab.com/kyleam/emacs.d

;; 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'python)
(require 'km-util)

;;;###autoload
(defun km/toggle-ipython-shell ()
  "Switch between using python and IPython for interactive shell."
  (interactive)
  (setq python-shell-interpreter
        (if (string= python-shell-interpreter "python") "ipython" "python")))

;;;###autoload
(defun km/find-python-test-file-other-window (arg)
  "Open test file for the current Python file in another window.
If the file does not already exist, import the original Python
file. Unless ARG is non-nil, py.test is also imported."
  (interactive "P")
  (let* ((py-file (file-name-nondirectory buffer-file-name))
         (test-file (concat "test_" py-file)))
    (find-file-other-window test-file)
    (unless (file-exists-p test-file)
      (insert (format "import %s\n" (file-name-sans-extension py-file)))
      (unless arg
        (insert "import pytest\n")))))

;;;###autoload
(defun km/python-shell-send-function-or-paragraph-and-step ()
  "Send function or paragraph to Python shell.

Send function if point is inside one. Otherwise, send the current
paragraph. After evaluation, step to the next code line.

This is inspired by `ess-eval-function-or-paragraph-and-step'."
  (interactive)
  (if (km/python-inside-defun-p)
      (progn
        (python-shell-send-defun 1)
        (python-nav-end-of-defun))
    (let (end)
      (save-excursion
        (forward-paragraph)
        (setq end (point))
        (backward-paragraph)
        (python-shell-send-region (point) end))
      (goto-char end)))
  (km/python-next-code-line 1))

;;;###autoload
(defun km/python-shell-send-buffer-up-to-line ()
  "Send beginning of buffer to the current line to Python shell."
  (interactive)
  (python-shell-send-region (point-min) (line-end-position)))

(defun km/python-inside-defun-p ()
  ;; I don't use `python-nav-beginning-of-defun' because it will go to
  ;; the function even when the point is not inside of it.
  (when (python-info-current-defun)
    t))

(defun km/python-next-code-line (&optional arg skip-to-eob)
  "This is copied nearly exactly from `ess-next-code-line'."
  (interactive "p")
  (or arg (setq arg 1))
  (beginning-of-line)
  (let ((pos (point))
        (n 0)
        (inc (if (> arg 0) 1 -1)))
    (while (and (/= arg 0) (= n 0))
      (setq n (forward-line inc)); n=0 is success
      (if (not (fboundp 'comment-beginning))
          (while (and (= n 0)
                      (looking-at "\\s-*\\($\\|\\s<\\)"))
            (setq n (forward-line inc)))
        (comment-beginning)
        (beginning-of-line)
        (forward-comment (* inc (buffer-size))) ;; as suggested in info file
        )
      (if (or skip-to-eob
              (not (looking-at "[ \t\n]*\\'"))) ;; don't go to eob or whatever
          (setq arg (- arg inc))
        (goto-char pos)
        (setq arg 0)
        (forward-line 1));; stop at next empty line
      (setq pos (point)))
    (goto-char pos)
    n))

(defvar km/python-shell-current-string nil)

;;;###autoload
(defun km/python-shell-send-set-string (set)
  "Send previously set string to Python shell.
If a string has not been set previously or SET is non-nil, prompt
for a new string."
  (interactive "P")
  (when (or set (not km/python-shell-current-string))
      (let ((initial (and (use-region-p)
                          (buffer-substring-no-properties
                           (region-beginning) (region-end)))))
        (setq km/python-shell-current-string (read-string "Python command: " initial))))
  (python-shell-send-string km/python-shell-current-string))

(defun km/python-shell--read-buffer ()
  (let ((buf-alist (mapcar (lambda (b) (cons (buffer-name b) b))
                           (km/mode-buffers 'inferior-python-mode))))
    (if (= (length buf-alist) 1)
        (cdr (car buf-alist))
      (cdr (assoc-string (completing-read "Shell buffer: " buf-alist)
                         buf-alist)))))

(defun km/python-shell--find-prompt-start (move-func)
  "Find first line of input prompt.

`comint-forward-prompt' can end up at other points in input
entry, like '*':

    In [1]: for i in range(3):
       ...: *   print(i)

Return point at the beginning input entry."
  (let ((beg-prompt-re "\\(In \\[[0-9]+\\]:\\|>>> \\)"))
    (funcall move-func)
    (while (not (save-excursion
                  (beginning-of-line)
                  (looking-at-p beg-prompt-re)))
      (funcall move-func))
    (point-at-bol)))

;;;###autoload
(defun km/python-copy-last-shell-line-as-comment (&optional which-shell)
  "Insert last input and output Python shell as comment.
When the current buffer is not associated with a Python shell or
WHICH-SHELL is non-nil, prompt with all Python shell buffers."
  (interactive "P")
  (let* ((default-shell-buffer
           (get-buffer (format "*%s*" (python-shell-get-process-name t))))
         (shell-buffer (if (or which-shell (not default-shell-buffer))
                           (km/python-shell--read-buffer)
                         default-shell-buffer))
         (start-pos (point))
         text)
    (with-current-buffer shell-buffer
      (save-excursion
        (goto-char (point-max))
        (let* ((inhibit-field-text-motion t)
               (beg (km/python-shell--find-prompt-start
                     (lambda ()
                       (comint-previous-prompt 1))))
               (end (km/python-shell--find-prompt-start
                     ;; FIXME: If extra blank prompts exist at the end
                     ;; of shell, this will grab them (because
                     ;; `comint-next-prompt' skips over them).
                     (lambda ()
                       (end-of-line)
                       (comint-next-prompt 1)))))
          (setq text (buffer-substring-no-properties beg end)))))
    (insert text)
    (comment-region start-pos (point))))

(defun km/python-indent-post-self-insert-function ()
  "Adjust indentation after insert of specfic characters.
This is taken from `python-indent-post-self-insert-function'.
Unlike that function, it does not rely on `electric-indent-mode'
being turned on."
  (when (and (not (bolp))
             (let ((paren-start (python-syntax-context 'paren)))
               ;; Check that point is inside parens.
               (when paren-start
                 (not
                  ;; Filter the case where input is happening in the same
                  ;; line where the open paren is.
                  (= (line-number-at-pos)
                     (line-number-at-pos paren-start)))))
             ;; When content has been added before the closing paren or a
             ;; comma has been inserted, it's ok to do the trick.
             (or
              (memq (char-after) '(?\) ?\] ?\}))
              (eq (char-before) ?,)))
    (save-excursion
      (goto-char (line-beginning-position))
      (let ((indentation (python-indent-calculate-indentation)))
        (when (< (current-indentation) indentation)
          (indent-line-to indentation))))))


;;; Pydoc

(require 'pydoc)

(defvar km/pydoc-names nil
  "List of names that have been sucessfully loaded by `pydoc'.")

(defvar km/pydoc-names-file "~/.emacs.d/.pydoc-names"
  "File to save `km/pydoc-names' to.")

;;;###autoload
(defun km/pydoc ()
  "Run `pydoc', prompting with `km/pydoc-names'."
  (interactive)
  (let* ((default-directory "~/")
         (initial-name (and (use-region-p)
                            (buffer-substring-no-properties
                             (region-beginning)
                             (region-end))))
         (name (completing-read "Name: " km/pydoc-names nil nil
                                initial-name)))
    (pydoc name)))

(defun km/pydoc-store-name ()
  "Store the name for the current pydoc object."
  (with-current-buffer (pydoc-buffer)
    (unless (eq (plist-get pydoc-info :type) 'not-found)
      (cl-pushnew (substring-no-properties (car (cdr help-xref-stack-item)))
                  km/pydoc-names
                  :test #'string=))))

(defun km/pydoc-save-names-file (&optional file)
  "Save `km/pydoc-names' to FILE.
FILE is `km/pydoc-names-file' by default."
  (interactive
   (list
    (read-file-name (format "Save file (default %s): "
                            km/pydoc-names-file)
                    nil km/pydoc-names-file t)))
  (setq file (or file km/pydoc-names-file))
  (when (file-writable-p file)
    (with-temp-file file
      (let (print-length)
        (print (sort km/pydoc-names #'string-lessp)
               (current-buffer))))))

;;;###autoload
(defun km/pydoc-read-names-file (&optional file)
  "Read `km/pydoc-names-file' from FILE.
FILE is `km/pydoc-names-file' by default."
  (interactive
   (list
    (read-file-name (format "Read file (default %s): "
                            km/pydoc-names-file)
                    nil km/pydoc-names-file t)))
  (with-temp-buffer
    (insert-file-contents (or file km/pydoc-names-file))
    (setq km/pydoc-names (read (current-buffer)))))

(provide 'km-python)
;;; km-python.el ends here