2016-03-11 26 views
0

我想創建一個命令,如果區域發送,如果活動,如果不是,當前行/語句進行評估,點進一步到下一個語句。emacs elisp發送行如果沒有區域活動python模式

我開始與This solution. 現在我不能得到(蟒蛇殼送區)工作,雖然我不知道如何將區域的開始和結束傳遞給它。

到目前爲止,我有這樣的:

(defun my-python-send-region (&optional beg end) 
    (interactive) 
    (if (use-region-p) 
     (python-shell-send-region) (let ((beg (cond (beg beg) 
        ((region-active-p) 
        (region-beginning)) 
        (t (line-beginning-position)))) 
     (end (cond (end end) 
        ((region-active-p) 
        (copy-marker (region-end))) 
        (t (line-end-position))))) 
    (python-shell-send-region beg end) 
    (python-nav-forward-statement)))) 

(add-hook 'python-mode-hook 
     (lambda() 
    (define-key python-mode-map "\C-cn" 'my-python-send-region))) 

UPDATE: 安德烈亞斯和Legoscia的建議,我改變了結構的位。

現在,我得到一個錯誤(無效的功能:(setq BEG(點))爲:

(defun my-python-send-region (&optional beg end) 
    (interactive) 
    (if (use-region-p) 
    (python-shell-send-region (region-beginning) (region-end)) 
    ((setq beg (point)) 
    (python-nav-end-of-statement) 
    (setq end (point)) 
    (python-shell-send-region (beg) (end))) 
    (python-nav-forward-statement)))) 

然而,這個工程:

(defun my-python-send-region (&optional beg end) 
(interactive) 
(setq beg (point)) 
(python-nav-end-of-statement) 
(setq end (point)) 
(python-shell-send-region beg end)) 
+1

由於Pythons敏感性WRT空格,發送區域或行很容易出錯。而是使用命令在點上輸入Python形式 - 發送塊,語句,等等。 –

回答

2

可能工作的另一種方法是嘗試從melpa了全行或區域包。這個包設置了一些東西,所以如果你調用一個需要區域的命令,但是沒有定義區域,它將基本上設置一個等於當前行的區域。實質上,這會導致在當前行中未定義區域的情況下預期區域工作的命令。我有這個在我的init.org文件

Allow region oriented commands to work on the current line if no region is defined.

#+BEGIN_SRC emacs-lisp 
    (use-package whole-line-or-region 
     :ensure t 
     :diminish whole-line-or-region-mode 
     :config 
     (whole-line-or-region-mode t) 
     (make-variable-buffer-local 'whole-line-or-region-mode)) 
+0

它不會移動一行,但我可以通過記錄一個快速的宏來修復它,當我使用它很多 – TNT

1

在這一部分:

(if (use-region-p) 
    (python-shell-send-region) 

你需要將區域的開始和結束傳遞給python-shell-send-region。它只在交互調用時自動獲取這些值。當您從Lisp代碼調用它,你需要明確傳遞值:

(python-shell-send-region (region-beginning) (region-end)) 
1

更新答案:蟒蛇殼發送-defun函數並不總是派現聲明/線(it is not meant to do that),所以我用一個函數代替它從elpy

(defun python-shell-send-region-or-line nil 
    "Sends from python-mode buffer to a python shell, intelligently." 
    (interactive) 
    (cond ((region-active-p) 
    (setq deactivate-mark t) 
    (python-shell-send-region (region-beginning) (region-end)) 
) (t (python-shell-send-current-statement)))) 

(defun python-shell-send-current-statement() 
"Send current statement to Python shell. 
Taken from elpy-shell-send-current-statement" 
(interactive) 
(let ((beg (python-nav-beginning-of-statement)) 
    (end (python-nav-end-of-statement))) 
(python-shell-send-string (buffer-substring beg end))) 
(python-nav-forward-statement)) 

我使用cond for if以防萬一我想添加案例。設置停用標記是如果選擇了取消選擇區域。如果沒有選擇區域,我還會導航到python語句。

+0

當我使用這個沒有活動區域時,我得到錯誤:'錯誤的參數數量:(1.1 ),0 – TNT

+0

「參數的錯誤數目」來自上一個答案中的移動行結束,它需要參數nil。無論如何,已更新的答案已被刪除。 –

+0

這是非常好的 - 現在唯一的小問題是,即使選擇了一個區域,它也會跳出一條語句,如果持續不斷,可能會使您錯過部分代碼。 – TNT

相關問題