2016-03-11 19 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

相关问题