Emacsに「elpy / jedi」をインストールしています。そして、DJJC-c C-RET
が提供するカスタマイズを使用して、個々の行を Python インタープリターに送信できるようになりました。以下カスタマイズです
(defun my-python-line ()
(interactive)
(save-excursion
(setq the_script_buffer (format (buffer-name)))
(end-of-line)
(kill-region (point) (progn (back-to-indentation) (point)))
;(setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
(setq the_py_buffer "*Python*")
(switch-to-buffer-other-window the_py_buffer)
(goto-char (buffer-end 1))
(yank)
(comint-send-input)
(switch-to-buffer-other-window the_script_buffer)
(yank)
)
(next-line)
)
(eval-after-load "elpy"
'(define-key elpy-mode-map (kbd "C-c <C-return>") 'my-python-line))
上記のスニペットは、DDJ が提案したものとほとんど同じですが、カーソルを次の行に移動したり、ショートカットを追加したりするなどの小さな変更が加えられています。
カーソルがある場所から新しい行までのすべての行がpythonインタープリターに送信されるように動作を変更したいと思います。カーソル位置は空の改行に移動する必要があります。これは、Spyder の動作を模倣します。
update 1したがって、次のコードで .emacs を更新した後。M-x beginning-of-line
、M-x push-mark
... のような個々のステートメントを実行すると、目的の結果を得ることができます
しかし、C-c <C-return>
何らかの方法でキーボード ショートカットを使用すると、バッファ全体が評価されます。
(defun forward-block (&optional φn)
(interactive "p")
(let ((φn (if (null φn) 1 φn)))
(search-forward-regexp "\n[\t\n ]*\n+" nil "NOERROR" φn)))
(defun elpy-shell-send-current-block ()
"Send current block to Python shell."
(interactive)
(beginning-of-line)
(push-mark)
(forward-block)
(elpy-shell-send-region-or-buffer)
(display-buffer (process-buffer (elpy-shell-get-or-create-process))
nil
'visible))
(eval-after-load "elpy"
'(define-key elpy-mode-map (kbd "C-c <C-return>") 'elpy-shell-send-current-block))
このショートカットを試している Python コードは、2 番目の print ステートメントにカーソルを置いた状態で下にあります。
import os
print("Line 1: Should Execute")
print("Line 2: Should Execute")
print("Line 3: Should Not Execute")