5

在 python 模式中,有一个名为 py-execute-region 的函数,它将突出显示的代码区域发送到 Python 缓冲区进行评估。评估后,光标位于 Python 缓冲区中,但我希望它保留在脚本缓冲区中,以便继续生成更多代码。我写了一个简单的建议函数:

(defadvice py-execute-region                                                
   (after py-execute-region-other-window activate)                          
   """ After execution, return cursor to script buffer """                  
   (other-window 1)                                                         
) 

但这根本没有任何作用。我尝试过其他变体,例如使用“around”而不是“after”;将变量设置为脚本缓冲区名称,然后弹出到缓冲区到该缓冲区和类似的东西。没有成功!我想知道这个机制对某人来说是否显而易见......谢谢!

4

4 回答 4

9

在这种情况下,解决方案似乎是

(custom-set-variables
 '(py-shell-switch-buffers-on-execute nil))
于 2009-12-05T10:43:59.420 回答
2

使用 around-advice 将函数包装在对 的调用中 save-window-excursion,这将在命令完成后恢复之前的窗口配置。

(defadvice py-execute-region
   (around preserve-window-configuration activate)
   "After execution, return cursor to script buffer"
   (save-window-excursion ad-do-it))

但请记住,如果尚未显示 Python 缓冲区,则在命令完成后仍将隐藏它。为了解决这个问题,您可以添加另一条建议以在最后调用 switch-to-buffer-other-window:

(defadvice py-execute-region
   (after show-pybuf-other-window activate)
   "After execution, show the python buffer in another window."
   (switch-to-buffer-other-window "[PYTHON BUFFER NAME]"))

另外,请确保不要"""triple quotes"""在 elisp 中使用。我不认为他们工作。

于 2009-09-13T19:30:32.943 回答
1

你在那里的东西对我来说很好。它应该自动激活,因此不需要单独激活。但是,您确实需要停用并重新激活建议才能使更改生效:

1) 定义和激活建议

2)它没有做你想要的,所以改变建议

3)停用它:(ad-deactivate 'py-execute-region)

4)重新激活它:(ad-activate 'py-execute-region)

第 4 步应该选择您在第 2 步中所做的更改。或者,您可以更改第 2 步中的代码,然后重新评估第 4 步中的代码(假设设置了激活标志)。

于 2009-09-13T12:51:22.753 回答
1

我实际上并没有尝试过,但我确实为 find-file 做了类似的事情,并且在那里我需要在调用 other-window 之前调用 interactive。我实际上对 Emacs Lisp 并没有真正的想法,但这可能会奏效。

(defadvice py-execute-region                                                
   (after py-execute-region-other-window activate)                          
   (interactive)
   (other-window 1)                                                         
)
于 2010-02-17T12:43:33.297 回答