Emacs defadvice on python-mode function

让人想犯罪 __ 提交于 2019-12-21 05:03:41

问题


In python-mode, there is a function called py-execute-region which sends a highlighted region of code to the Python buffer for evaluation. After evaluation, the cursor is in the Python buffer, but I would prefer that it remain in the script buffer so I can continue producing more code. I wrote a simple advising function:

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

But this does not do anything at all. I've tried other variants like using 'around' rather than 'after'; setting a variable to the script buffer name and then pop-to-buffer to that buffer and stuff like that. No success! I wonder if the mechanics of this is obvious to someone... Thanks!


回答1:


In this case the solution appears to be

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



回答2:


Use around-advice to wrap the function in a call to save-window-excursion, which will restore the previous window configuration after the command completes.

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

Keep in mind, though, that if the Python buffer wasn't already shown, it will still be hidden after the command completes. To remedy that, you can add another piece of advice to call switch-to-buffer-other-window at the end:

(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]"))

Also, make sure you don't use """triple quotes""" in elisp. I don't think they work.




回答3:


What you have there works fine for me. And it should auto-activate, so a separate activation should be unnecessary. However, you do need to de-active and re-activate advice for changes to take effect:

1) define and activate advice

2) it doesn't do what you want, so change the advice

3) deactivate it: (ad-deactivate 'py-execute-region)

4) reactivate it: (ad-activate 'py-execute-region)

Step 4 should pick up the changes you made in step 2. Alternately, you can change the code in step 2 and then just re-evaluate the code in step 4 (assuming the activate flag is set).




回答4:


I haven't actually tried this out, but I did do something similar for find-file, and over there I needed to call interactive before calling other-window. I actually have no real idea of Emacs Lisp, but this may work.

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


来源:https://stackoverflow.com/questions/1416882/emacs-defadvice-on-python-mode-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!