1

我使用 emacs 有一段时间了,但对 lisp 编程不是很熟悉。就在几天前,我开始在 emacs 上编写 Python 代码。我发现 python-mode 非常有用,我想进一步探索它。我在互联网上发现了一些 emacs 的嘴唇功能,对它们进行了一些修改以使界面用户友好。我正在尝试实现以下操作

我通常用 2 个垂直窗口启动 emacs,一个带有 python 源,另一个是 shell。我应该能够使用键盘绑定进行以下操作

  • 在缓冲区之间切换(工作)
  • 执行一个区域(工作),但用 shell 缓冲区替换源缓冲区。我想在原始 shell 缓冲区中执行选定区域。
  • 执行一行(工作)但与上述相同的问题。当我说,该行应该在 python shell 中执行而不替换任何缓冲区。所以复制该行,切换到python shell,执行行,切换回python源缓冲区。

我无法实现上述切换动作。以下是我的 init.el 文件中的代码

(defun goto-python-shell ()
  "Go to the python command window (start it if needed)"
  (interactive)
  (setq current-python-script-buffer (current-buffer))
  (if (boundp 'current-python-shell-buffer)
    (switch-to-buffer-other-window current-python-shell-buffer)
    (py-shell))
  (end-of-buffer)
 )

 (defun goto-python-source ()
   "switch back to source window"
   (interactive)
  (setq current-python-shell-buffer (current-buffer))
   (switch-to-buffer-other-window current-python-script-buffer)
 )

 (defun py-execute-statement-and-step ()
   "select a statement, submit as a region and then step forward"
   (interactive)
   (beginning-of-line 1)
   (let ((beg (point)))
     (py-next-statement 1)
     ; if last statement.
        (if (= (point) beg) (end-of-buffer ))
 ; (switch-to-buffer-other-window current-python-shell-buffer)
   (py-execute-region beg (point))
   (switch-to-buffer-other-window current-python-script-buffer)
   )
 )

 ; some key bindings
 (define-key python-mode-map (quote [f9]) 'py-execute-statement-and-step)
 ;(define-key python-mode-map (quote [f10]) `py-execute-region)
 ;py-shell-switch-buffers-on-execute
 (define-key python-mode-map (quote [f10]) `py-shell-switch-buffers-on-execute)
 (define-key python-mode-map (quote [f11]) `py-execute-buffer)
 (define-key python-mode-map (quote [f12]) `goto-python-shell)
 (define-key py-shell-map (quote [f12]) `goto-python-source)

请指教。

另外,由于我是 python 模式的新手,有人可以分享使用类似于上面的 python 模式的不错的初始化吗?

非常感谢您的帮助。

问候, AJ

4

3 回答 3

3

您应该查看此问题的第一个答案并自定义py-shell-switch-buffers-on-execute变量。

这样你就不需要所有的自定义函数来做python-mode你想要的工作(即保持源缓冲区处于活动状态)

于 2012-03-29T07:31:15.857 回答
2

我认为您正在尝试重新发明 Emacs 24 中可用的内容(至少在评估方面)。试试 Emacs 24。在编辑 Python 源代码时,您可以按C-c C-c评估缓冲区并按C-c C-r评估区域。您不必显式启动 Python shell。

我认为没有直接支持评估一条线和一步。您可以通过击键来实现它C-SPC C-n C-c C-r。您的重点将留在源代码上,无需在源代码和 shell 之间显式切换。

FWIW,我每天都在合理的时间内使用 Emacs 24,并且没有遇到任何稳定性问题。

于 2012-03-29T07:19:49.680 回答
0

以下更改就像一个魅力。f9 逐行执行, f10 执行基于区域的执行。在我禁用 py-shell-switch-buffers-on-execute 后,光标仍保留在脚本窗口中。

(defun py-execute-statement-and-step ()
  "select a statement, submit as a region and then step forward"
  (interactive)
  (beginning-of-line 1)
  (let ((beg (point)))
    (py-next-statement 1) 
    ; if last statement.
        (if (= (point) beg) (end-of-buffer ))
        (py-execute-region beg (point))
        (next-line)
  )
) 

(custom-set-variables
'(py-shell-switch-buffers-on-execute nil))

(define-key python-mode-map (quote [f9]) 'py-execute-statement-and-step)
(define-key python-mode-map (quote [f10]) `py-execute-region)
于 2012-03-30T01:34:48.963 回答