2

I like doing PR reviews with Magit's diff, thus calling magit-diff-visit-file pretty often, providing a universal argument for the optional other-window parameter. To optimize the process instead of pressing C-- <Ret> i'd prefer just to use <Ret> key. What's the proper way to define such a behaviour?

I've seen people using defalias here and there, but not sure how's it going to work in terms of itself. I guess i can get away with defining a stub function and forward the call to magit-diff-visit-file, but i was thinking if there's a better way?

1 Answer 1

4

defalias is useful when you want to give some existing function an alternative name. But you want to change what a certain key does, and that's a different problem.

You have to bind "RET" to some command in some keymap, a problem that can be broken down into two smaller problems: "what keymap?" and "what command?".

To find the appropriate keymap you can use rgrep in the directory containing the Magit source to search for magit-diff-visit-file.

Turns out you actually have to modify two keymaps magit-file-section-map and magit-hunk-section-map and that instead of "RET" you should use [remap magit-visit-thing], though the former would work too.

(define-key magit-file-section-map [remap magit-visit-thing] '???)
(define-key magit-hunk-section-map [remap magit-visit-thing] '???)

Looking at the other matches and the code in close approximation suggests that no variation exists that behaves the way you want, but defining it is easy: copy magit-diff-visit-file, replace the body with a call to that function and adjust the name, signature and interactive form.

(defun magit-diff-visit-file-other-window (file)
  (interactive (list (--if-let (magit-file-at-point)
                         (expand-file-name it)
                       (user-error "No file at point"))))
  (magit-diff-visit-file file t))
1
  • Brilliant answer, thanks for your help Jonas!
    – 4lex1v
    Commented Sep 5, 2017 at 21:17

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.