【发布时间】:2015-08-12 06:29:37
【问题描述】:
我在 Emacs 上安装了“elpy / jedi”。并且使用DJJ 提供的自定义,我现在可以使用C-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))
上面的 sn-p 与 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 代码如下,光标位于第二个打印语句处。
import os
print("Line 1: Should Execute")
print("Line 2: Should Execute")
print("Line 3: Should Not Execute")
【问题讨论】: