all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alex Kost <alezost@gmail.com>
To: "boyang" <wangoer@qq.com>
Cc: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: Why can't local-set-key (kbd "n")  in diff-mode ?
Date: Sat, 19 Nov 2016 12:20:18 +0300	[thread overview]
Message-ID: <87mvgvx1v1.fsf@gmail.com> (raw)
In-Reply-To: <tencent_6AA3205E02D723E33C153E6B@qq.com> (boyang's message of "Fri, 18 Nov 2016 17:31:53 +0800")

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	other threads:[~2016-11-19  9:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-18  9:31 Why can't local-set-key (kbd "n") in diff-mode ? boyang
2016-11-19  9:20 ` Alex Kost [this message]
2016-11-19 15:19   ` =?gb18030?B?Ym95YW5n?=

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mvgvx1v1.fsf@gmail.com \
    --to=alezost@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=wangoer@qq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.