【问题标题】:Emacs Python "elpy" send code to interpreterEmacs Python“elpy”将代码发送到解释器
【发布时间】: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-lineM-x push-mark等单个语句时,我可以得到想要的结果...

但是当我以某种方式使用键盘快捷键C-c &lt;C-return&gt; 时,它会评估整个缓冲区。

(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")

【问题讨论】:

    标签: python emacs elpy


    【解决方案1】:

    您可以编写一个函数来选择您想要的任何区域并调用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))
    

    【讨论】:

    • 谢谢@ChillarAnand。也许我遗漏了一些东西,但是这个 sn-p 与我共享的行为相同。我正在寻找的行为是,如果我有 3 行 Python 代码,第 2 行和第 3 行之间有一个空格,并且光标在第 1 行。然后按 C-c C-RET 应该执行前 2 行。
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多