reddit / emacs

make the comment function do another command sometimes

nazazazazazazazazaku · 2022-11-02 22:16
Hello, I'm a beginner at Emacs, I tried to figure this out by myself at first but it's too confusing for me. I use God Mode on Emacs (vim-like modes) and I want to exit it when the comment-dwim command doesn't comment out a region, only when it does and indented comment or a comment in an empty line, so that I can write afterwards, without having to press esc. ​ Maybe there is some way to add the god-local-mode-pause function (the function to get into "insert mode", so as to say) to any of the comment related functions, however, I don't really know elisp, so any help is much appreciated. Tl;dr: is there any way to adhere a function to another so that when I execute one, with its name, it does another one as well? Edit: never mind, just learned what advising functions are for. The code I ended up with : (defadvice comment-indent (after comment-indent-after activate) (god-local-mode-pause)) (defadvice comment-add (after comment-add-after activate) (god-local-mode-pause)) ​
andyjda · 2022-11-03 00:02
Advice is indeed the way to go! Small note: your code is using the `advice.el` package. There is a newer package, `nadvice.el` that preserves the same functionalities but is a bit simpler and more readable. Your code would look like this: `(advice-add #'comment-indent :after #'god-local-mode-pause)` `(advice-add #'comment-add :after #'god-local-mode-pause)` Taking a look at `nadvice.el` could be useful, but feel free to pick whatever syntax/functions you prefer :)
nazazazazazazazazaku · 2022-11-06 12:25
Thank you very much!, but, [from what I've seen](https://emacs.stackexchange.com/questions/51470/define-advice-vs-advice-add-vs-other#comment79384_51471), defadvice is actually built upon advice-add, so as to make it more compact; plus, when I tried the code you recommended me, it didn't work with the comment-add function, it said that arguments were missing.