help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why can't local-set-key (kbd "n") in diff-mode ?


From: Alex Kost
Subject: Re: Why can't local-set-key (kbd "n") in diff-mode ?
Date: Sat, 19 Nov 2016 12:20:18 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux)

boyang (2016-11-18 17:31 +0800) wrote:

> Hi
Hello!

> Greetings,
> I'm a Emacs newbie, when I do the following:
> ----------------------------------------------------------------------------------------
> (add-hook 'diff-mode-hook
>                    (lambda ()
>                       (local-set-key (kbd "n") 'next-error-no-select)
>                       (local-set-key (kbd "p") 'previous-error-no-select)))
> ----------------------------------------------------------------------------------------

I would do it like this:

(with-eval-after-load 'diff-mode
  (define-key diff-mode-shared-map (kbd "n") 'next-error-no-select)
  (define-key diff-mode-shared-map (kbd "p") 'previous-error-no-select))

Such overriding of keys in the original maps always "works".  The only
thing you need is to find a map where the keys are bound.

> It doesn't work, when I press "n" in "*Diff*", it stills run "diff-hunk-next"
> when I "C-h k n", it still shows "diff-hunk-next".  Same also for "p".
>
> But
> 1. If I do the same thing in other mode, say "occur-mode-hook", it works in 
> that mode.
> 2. If I set other key other than "n" in "diff-mode-hook", say "x", it works.
>
> So is there somebody can tell me the reason behind this ?

Good question!  I've spent several minutes trying to understand this :-)

So if you look at the code ("M-x find-function diff-mode"), there is
this part:

  ;; Neat trick from Dave Love to add more bindings in read-only mode:
  (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map)))
    (add-to-list 'minor-mode-overriding-map-alist ro-bind)
    ...)

This means that the keys from 'diff-mode-shared-map' will override the
keys that you bind with 'local-set-key' (it modifies 'diff-mode-map').
Thus "n" and "p" are taken from 'diff-mode-shared-map'.  But if you bind
"x" key, it is taken from 'diff-mode-map' because 'diff-mode-shared-map'
does not have "x" key (look at "M-x find-variable
diff-mode-shared-map").

-- 
Alex



reply via email to

[Prev in Thread] Current Thread [Next in Thread]