将代码从 Python 缓冲区发送到从 M-x ansi-term 运行的 IPython 会话



假设我在 Emacs 中打开了一个带有 M-x ansi-term 的终端模拟器。这在 Emacs 中打开了一个缓冲区,其中包含我选择的 shell。假设我然后从这个外壳运行ipython。我可以在 Emacs 中使用 Python 代码从另一个缓冲区将代码发送到此ipython会话吗?如果是这样,怎么办?

我有一个用于此目的的次要模式(除了它不是特定于 IPython 的,我主要将其用于 shell 脚本):isend-mode。

以下是您将如何使用它:

  1. 打开ansi-term缓冲区:

    M-x ansi-term RET /usr/bin/ipython RET

  2. 使用要执行的代码打开缓冲区,并将其关联到解释器缓冲区:

    M-x isend-associate RET *ansi-term* RET

  3. 在 python 缓冲区中点击 C-RET,将当前行发送到ansi-term缓冲区中的解释器。

(defun send-buffer-to-ipython ()
  "Send current buffer to the running ipython process."
  (interactive)
  (let* ((ipython-buffer "*ansi-term*") ; the buffer name of your running terminal
         (proc (get-buffer-process ipython-buffer)))
    (unless proc
      (error "no process found"))
    (save-buffer)
    (process-send-string proc
                         (format "execfile("%s")n" (buffer-file-name)))
    (pop-to-buffer ipython-buffer)      ; show ipython and select it
    ;; (display-buffer ipython-buffer)  ; show ipython but don't select it
    ))

然后将命令send-buffer-to-ipython绑定到您喜欢的任何键。我把它绑定到C-c C-c

(define-key python-mode-map [?C-c ?C-c] 'send-buffer-to-ipython)

with python-mode.el

M-x 自定义变量 RET py-shell-name RET ansi-term

M-x ansi-term RET ipython RET

C-c C-c 来自 Python-buffer 而不是在 IPython shell 中执行

警告:在默认的 py-shell-name 之前有一个 shebang

https://launchpad.net/python-mode/trunk/6.0.12/+download/python-mode.el-6.0.12.tar.gz

最新更新