2012-03-28 170 views
1

我一直在使用emacs一段时间,但不太熟悉lisp编程。几天之后,我开始在emacs上编写Python代码。我发现Python模式非常有用,我想进一步探索它。我在互联网上发现了一些emacs的嘴唇功能,用它们来使用户界面友好。我试图实现以下操作在emacs中使用python-mode切换缓冲区设置?

我通常用2个垂直窗口启动emacs,一个使用python source,另一个使用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

+0

希望如果有人知道答案..我只是想复制一个字符串,发送到其他缓冲区..做一些动作并返回到前一个缓冲区。 – Alok 2012-03-29 00:48:27

回答

3

您应该看看this question的第一个答案,并自定义py-shell-switch-buffers-on-execute变量。

这样你就不需要你的所有的自定义功能,使python-mode工作就像你想要的(即保持源缓冲区活动)

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一段合理的时间,而且我还没有遇到任何稳定性问题。

0

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

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