all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Why can't local-set-key (kbd "n")  in diff-mode ?
@ 2016-11-18  9:31 boyang
  2016-11-19  9:20 ` Alex Kost
  0 siblings, 1 reply; 3+ messages in thread
From: boyang @ 2016-11-18  9:31 UTC (permalink / raw)
  To: help-gnu-emacs

Hi 


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)))
----------------------------------------------------------------------------------------

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 ?


Thank you


Cheers,
Boyang

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Why can't local-set-key (kbd "n")  in diff-mode ?
  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?=
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Kost @ 2016-11-19  9:20 UTC (permalink / raw)
  To: boyang; +Cc: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Why can't local-set-key (kbd "n")  in diff-mode ?
  2016-11-19  9:20 ` Alex Kost
@ 2016-11-19 15:19   ` =?gb18030?B?Ym95YW5n?=
  0 siblings, 0 replies; 3+ messages in thread
From: =?gb18030?B?Ym95YW5n?= @ 2016-11-19 15:19 UTC (permalink / raw)
  To: =?gb18030?B?QWxleCBLb3N0?=; +Cc: =?gb18030?B?aGVscC1nbnUtZW1hY3M=?=

[-- 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-11-19 15:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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?=

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.