unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: =?gb18030?B?Ym95YW5n?= <wangoer@qq.com>
To: =?gb18030?B?QWxleCBLb3N0?= <alezost@gmail.com>
Cc: =?gb18030?B?aGVscC1nbnUtZW1hY3M=?= <help-gnu-emacs@gnu.org>
Subject: Re: Why can't local-set-key (kbd "n")  in diff-mode ?
Date: Sat, 19 Nov 2016 23:19:19 +0800	[thread overview]
Message-ID: <tencent_782A157E307BF44B297901C3@qq.com> (raw)
In-Reply-To: <87mvgvx1v1.fsf@gmail.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="gb18030", Size: 2398 bytes --]

Hi Alex, Thank you very much !  I will do what you said, and thank's for your explanation !

Boyang




------------------ Original ------------------
From: "Alex Kost"<alezost@gmail.com>; 
Date: 2016Äê11ÔÂ19ÈÕ(ÐÇÆÚÁù) ÏÂÎç5:20
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 ?



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 15:19 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
2016-11-19 15:19   ` =?gb18030?B?Ym95YW5n?= [this message]

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=tencent_782A157E307BF44B297901C3@qq.com \
    --to=wangoer@qq.com \
    --cc=alezost@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).