From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alex Kost Newsgroups: gmane.emacs.help Subject: Re: Why can't local-set-key (kbd "n") in diff-mode ? Date: Sat, 19 Nov 2016 12:20:18 +0300 Message-ID: <87mvgvx1v1.fsf@gmail.com> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1479547254 29105 195.159.176.226 (19 Nov 2016 09:20:54 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 19 Nov 2016 09:20:54 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux) Cc: help-gnu-emacs To: "boyang" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Nov 19 10:20:48 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c81pI-0006EK-BI for geh-help-gnu-emacs@m.gmane.org; Sat, 19 Nov 2016 10:20:48 +0100 Original-Received: from localhost ([::1]:40615 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c81pK-0001Ds-6P for geh-help-gnu-emacs@m.gmane.org; Sat, 19 Nov 2016 04:20:50 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50287) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c81os-0001Dc-Vd for help-gnu-emacs@gnu.org; Sat, 19 Nov 2016 04:20:23 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c81op-0002tZ-Rn for help-gnu-emacs@gnu.org; Sat, 19 Nov 2016 04:20:22 -0500 Original-Received: from mail-qt0-x233.google.com ([2607:f8b0:400d:c0d::233]:34436) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c81op-0002tT-ND for help-gnu-emacs@gnu.org; Sat, 19 Nov 2016 04:20:19 -0500 Original-Received: by mail-qt0-x233.google.com with SMTP id n6so177267429qtd.1 for ; Sat, 19 Nov 2016 01:20:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=H6x1PgWu9MeGseeJ5qUxeaB0LITkm9G8oT67L1u6r7c=; b=Kmjgkx52v2if59YZQGc+b4MmEg5zIzDnU+yg9Wi/BXmfHDaFVWolfSPnUOOtIFhMBF G/kxC8dMTz3bv4v+mQGJY1c4se1oFzyiiWBIUoN0SFLL/V7DwOgmuWvBGD6cP6YLQbZy MSQXMnLyZS743Hi5Tf0J65aszghry1GkYd2PfmblUd+qINB5fl0kFH15Wq8UgoFhvZp1 xXCzvtKsgC5P41T9kmfivYFzxaOqnCmOTpc8B86Ol7nT8wZApTBimNbk+jwwYBNlk9xT EYW7msM5F6eZxCIh7ikqP83GZi6qm42Wb3HdN3CWowgCVtfRvpL74JxkMovru/yBqLvi XVNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=H6x1PgWu9MeGseeJ5qUxeaB0LITkm9G8oT67L1u6r7c=; b=ED3AxDHq43ekL9FwV4VeuDflNsLSBB3FvdOWZhH6vqcoeVyevywkqP+eXvbJPvOnP3 0GCdwz0qaWtXoloJx31gRQ4PPrggxx99Rdl2z2fDX/qsSgwKYkvQjumf02Tx9jAx/idN 8e7RWFDrLR0EZSy6lt5bXnAGtG80q0TaKXx+S/3m8scpZ0GCHyr/tQdTUvo0PUWdE5/M UHqrcEozcbVxSIBBsgMDE1SRlIUmUGISZzixmZn3Q6yUSDSL+fY/t/xUeLq3byPOAzND qp8OAlaQ+J07KxU27dEIz712bvsiabJLlCypY7Dz4Y84XUOrnQKdMklbxQasQJz2K9/I 0Ebg== X-Gm-Message-State: AKaTC00fHcfvgBvHXJXA+6BWE81KAufyUIyLofZV3fe4rvjKuA6wV8u6bfQksPHrfJlbvw== X-Received: by 10.25.208.203 with SMTP id h194mr874611lfg.26.1479547218736; Sat, 19 Nov 2016 01:20:18 -0800 (PST) Original-Received: from leviafan ([217.107.192.156]) by smtp.gmail.com with ESMTPSA id 37sm3303681lfw.31.2016.11.19.01.20.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Nov 2016 01:20:18 -0800 (PST) In-Reply-To: (boyang's message of "Fri, 18 Nov 2016 17:31:53 +0800") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400d:c0d::233 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:111748 Archived-At: 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