0

I was using dumb-jump to go to the location where the identifier at point is defined. When it jumps to the function, jumped function shows up on the middle of the terminal window.


=> Instead when I use elpy-goto-definition it jumps to the function and shows it on the 1st line on the top of the terminal window.

[Q] Is it possible to make elpy-goto-definition to show the jumped function on the center of the terminal like dumb-jump?

1 Answer 1

1

Not beeing a user of elpy but looking at elpys source code, I would say you have several options to do this (there are proably more).

Calling elpy-goto-definition results in a call to elpy-goto-location which at its end calls (recenter 0) and this 0 is the culprit.

3 completely separate ways to change that:

first
create a new function (my-elpy-goto-definition) which just calls elpy-goto-definition and then does (recenter) without that 0. to use it you always call my-elpy-goto-definition instead of elpy-goto-definition (least intrusive solution, Problem: you need to redefine your shortcut keys)

second
copy and paste the whole function elpy-goto-location then remove the offending 0 and re-eval that defun, effectively replacing the old function with your modified one. (very intrusive solution, Problem: you will never automatically receive code updates/fixes to that function, you have to include them manually)

third
write an advice to either elpy-goto-definition or elpy-goto-location (depends on how globaly you want this behavior), which calls an (recenter) after that function has run. (medium intrusive solution, works automatic, without changing keybindings, I'd probably go with this).

Example code for third (advicing elpy-goto-location):

(defun my-recenter-elpy-goto (&rest r)
  (recenter))

(advice-add #'elpy-goto-location :after #'my-recenter-elpy-goto)
6
  • I made a keybinding for my-recenter-elpy-goto, it gives following error Wrong type argument: commandp, my-recenter-elpy-goto
    – alper
    Commented Mar 2, 2020 at 13:26
  • @alper my-recenter-elpy-goto is not meant to be bound to a key. the advice-add installs this function, so it will be called automatically
    – jue
    Commented Mar 2, 2020 at 13:42
  • 1
    @alper I don't see a reason why this message should appear. You only need to put those 3 lines from the solution into your init.el and everything should work. No additional keybindings, no nothing...
    – jue
    Commented Mar 2, 2020 at 18:33
  • xref-pop-marker-stack is for the get back, can I return into center for it as well?
    – alper
    Commented Mar 17, 2020 at 9:17
  • @alper probably, just try it. But there is also another way: via setting a hook. The hook variable is named: xref-after-return-hook, use: (add-hook 'xref-after-return-hook 'recenter) I have not tested this.
    – jue
    Commented Mar 19, 2020 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.