2

我在 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))

上面的代码片段与 DDJ 建议的基本相同,只是做了一些小的修改,比如将光标移动到下一行和快捷方式。

我想修改行为,以便从光标所在的位置到遇到新行的所有行都被发送到 python 解释器。并且光标位置应该移动到空的换行符。这将模仿 Spyder 的行为。

更新 1因此,在使用以下代码更新我的 .emacs 之后。当我执行单个语句时,我可以获得所需的结果,例如M-x beginning-of-lineM-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")
4

1 回答 1

0

您可以编写一个函数来选择您想要的任何区域并调用elpy-shell-send-region-or-buffer发送它。

这是一段代码。

(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))
于 2015-08-13T05:18:50.530 回答