Emacs Python“;“埃尔比”;向解释器发送代码

Emacs Python“;“埃尔比”;向解释器发送代码,python,emacs,elpy,Python,Emacs,Elpy,我在Emacs上安装了“埃尔比/绝地”。使用由我提供的定制,我现在可以使用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)

我在Emacs上安装了“埃尔比/绝地”。使用由我提供的定制,我现在可以使用
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))
下面是我尝试此快捷方式的Python代码,光标位于第二个print语句

import os

print("Line 1: Should Execute")
print("Line 2: Should Execute")

print("Line 3: Should Not Execute")

您可以编写一个函数来选择所需的任何区域,并调用
elpy shell send region或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))

谢谢@chillarand。也许我遗漏了什么,但这个片段与我共享的片段有着相同的行为。我要寻找的行为是,如果我有3行Python代码,在第2行和第3行之间有一个空格,光标在第1行。然后按C-C-RET应执行前2行。
(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))