I have a setup function that I want to run before I use pdb
/gud
to debug a Python script. I know about gud-mode-hook
which runs after the debugger starts (too late) and change-major-mode-hook
which, if I understand correctly, runs when you change major modes but before the major mode hook for the new mode (early enough, but will run in cases I don't want it to). How can I have a function called before a specific major mode is initialized?
So for my particular use case the flow would be something like:
M-x pdb
-> (my-setup-function)
is called -> pdb
starts (i.e., it's hooks are run etc.)
So far I have tried doing this with major mode hooks pdb-mode-hook
and gud-mode-hook
but then the setup doesn't happen in time, and is only available the next time I run pdb
. At one point I tried to do it with advice as well, but was not successful (potentially, because I did it wrong). This is what I tried (though I am confused as to whether or not this counts as advice
):
(add-function :before (pdb) #'my-setup-function)
...which I based on a snippet from the Emacs Lisp Reference manual:
(add-function :before (process-filter proc) #'my-tracing-function)
Obviously, I have not set this up right, and I get the message that (pdb) is not a valid place expression
when I evaluate it. Even though (because?) the documentation for add-function
is very detailed, I have a lot of trouble understanding how to use it properly.
change-major-mode-hook
? Eg,(defun my-setup-function () (when (eq major-mode 'python-mode) (do-my-setup-stuff)))
, and then(add-hook 'change-major-mode-hook #'my-setup-function)
.my-setup-function
whenever you change frompython-mode
to another mode? Maybe I am misunderstanding the chain of events, but trying it this way it seems like my function just never fires period.(defadvice :before ..
. Ugly, I know...defadvice
would be my answer as well. Why don't you share what you tried? Maybe someone will see what's wrong with it.change-major-mode-hook
is correct:major-mode
still has its original (pre-change) value at this time. stackoverflow.com/a/19295380 might be of interest too.