繁体   English   中英

关于python-mode函数的Emacs defadvice

[英]Emacs defadvice on python-mode function

在python-mode中,有一个名为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'; 将变量设置为脚本缓冲区名称,然后弹出缓冲区到该缓冲区和类似的东西。 没有成功! 我想知道这个机制对某人来说是否明显......谢谢!

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

(custom-set-variables
 '(py-shell-switch-buffers-on-execute nil))

使用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]"))

此外,请确保您不要在elisp中使用"""triple quotes""" 我不认为他们有效。

你有什么工作对我来说很好。 它应该自动激活,因此不需要单独激活。 但是,您需要取消激活并重新激活建议才能使更改生效:

1)定义并激活建议

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

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

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

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

我实际上没有试过这个,但我确实为find-file做了类似的事情,在那里我需要在调用other-window之前调用interactive。 我实际上并不知道Emacs Lisp,但这可能有用。

(defadvice py-execute-region                                                
   (after py-execute-region-other-window activate)                          
   (interactive)
   (other-window 1)                                                         
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM