2012-03-28 169 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)