From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hong Xu Newsgroups: gmane.emacs.bugs Subject: bug#74999: [PATCH v4] Use `keymap*-set' over `global-set-key'/`define-key' in elisp intro Date: Thu, 26 Dec 2024 13:46:39 -0800 Message-ID: <20241226214814.52057-1-hong@topbug.net> References: <86ed1uzwhw.fsf@gnu.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38859"; mail-complaints-to="usenet@ciao.gmane.io" To: 74999@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 26 22:49:21 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tQvjM-0009wH-1I for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 26 Dec 2024 22:49:20 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tQvj5-0007QM-TO; Thu, 26 Dec 2024 16:49:03 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tQvj4-0007Q8-5L for bug-gnu-emacs@gnu.org; Thu, 26 Dec 2024 16:49:02 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tQvj3-0006Ew-TB for bug-gnu-emacs@gnu.org; Thu, 26 Dec 2024 16:49:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:References:In-Reply-To:Date:From:To:Subject; bh=jshizdoncLPo96gIQGgQ2Hau08kduzxrHwXs65f1I3Y=; b=hq8FAFapOY8u+iosM+bLhqcJM/b43CsUJIj3sBlBdiKELbLgMJ+0JFH5aup3ve+D4xq8LSE07YDmPo2pPlVixXTe/qfJeQYfxCMz7Jg//Xu9goRnNq7eb9rtkwQ409ZzfIWWpPJabgll3Wh96DxmW5Vy70RGsJNjGPDEnQHM2I2F676+JF6ym72IhNSYlpaR2ZPRTCP52xRSkgJB3MNYux+wb487rb5Mrh9af1ffd0uWNBtkSxv5dCMfZUapv8EH01arlv/Y/S42sGtI7YbKTkGex9rC+jRKwesD5k+21h31BGYZcDFRldTL98/vCH/rhS0SWs1lq2R6tbPD8ixvXA==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tQvj3-00045Y-NJ for bug-gnu-emacs@gnu.org; Thu, 26 Dec 2024 16:49:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Hong Xu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 26 Dec 2024 21:49:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74999 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 74999-submit@debbugs.gnu.org id=B74999.173524970515644 (code B ref 74999); Thu, 26 Dec 2024 21:49:01 +0000 Original-Received: (at 74999) by debbugs.gnu.org; 26 Dec 2024 21:48:25 +0000 Original-Received: from localhost ([127.0.0.1]:42534 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tQviS-00044F-Ba for submit@debbugs.gnu.org; Thu, 26 Dec 2024 16:48:25 -0500 Original-Received: from sender4-of-o52.zoho.com ([136.143.188.52]:21275) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tQviO-000445-Px for 74999@debbugs.gnu.org; Thu, 26 Dec 2024 16:48:22 -0500 ARC-Seal: i=1; a=rsa-sha256; t=1735249697; cv=none; d=zohomail.com; s=zohoarc; b=Z9KFrNZql++wkTdOLa4AIgl1v0q8LrsHfvudCEyi7g4qOsrQ/bKZH/xwbrrLrWU7/VzkTQvF54dd7/Ny9TzB95b2uBBlClVdwjTidS39F3QKVqKABR5BBICSbkNNYs/R1ILBmiGejHrWIDB7jPdjFReM3F67zZDZvCSXUxyE1IY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1735249697; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=jshizdoncLPo96gIQGgQ2Hau08kduzxrHwXs65f1I3Y=; b=YlKzXsTnHazyR6+VZZq1pPL61qsbaAeVu9NTvVwIh1/52SoBXDRw1MdKqBM3kNbXwjKB4TedgrCiCrysyRJJdu0m3O5xLygyk4TFZWljYMraa6zS2TqXtpIJ6MPcB5BI5iWQzFnpXSDD+ZVbk+u1KXQEFDtuVoLsAeoMATB9fR0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=topbug.net; spf=pass smtp.mailfrom=hong@topbug.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1735249697; s=zoho; d=topbug.net; i=hong@topbug.net; h=From:From:To:To:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To:Cc; bh=jshizdoncLPo96gIQGgQ2Hau08kduzxrHwXs65f1I3Y=; b=fi5HtRUMqfWWJfhyHJoTnDzzzp/kYApVhEeBerqftJcxKIm3nkXaAiptjVJijl0O oQJojQRn3WWSl2p4LEdpr9k4kcvqLE6dHIKmdoNoHpEjaz217Li90vPcvzwCwSLh4l/ t6ry0SOG/hRRY4OsugOhGwhq/k8eXjyo5MweTDgs= Original-Received: by mx.zohomail.com with SMTPS id 1735249695423525.5268683139931; Thu, 26 Dec 2024 13:48:15 -0800 (PST) X-Mailer: git-send-email 2.47.1 In-Reply-To: <86ed1uzwhw.fsf@gnu.org> X-ZohoMailClient: External X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:297794 Archived-At: * doc/lispintro/emacs-lisp-intro.texi (Key Bindings): Since `global-set-key' and `define-key' are considered legacy, we encourage `keymap-global-set' and `keymap-set' now. --- doc/lispintro/emacs-lisp-intro.texi | 119 +++++++++++++++++++--------- 1 file changed, 81 insertions(+), 38 deletions(-) diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi index 49916235fbf9..daffd6386133 100644 --- a/doc/lispintro/emacs-lisp-intro.texi +++ b/doc/lispintro/emacs-lisp-intro.texi @@ -13810,7 +13810,7 @@ Whitespace Bug If you wish, you can also install this key binding by evaluating it: @smallexample -(global-set-key "\C-c=" '@value{COUNT-WORDS}) +(keymap-global-set "C-c =" '@value{COUNT-WORDS}) @end smallexample To conduct the first test, set mark and point to the beginning and end @@ -14762,7 +14762,7 @@ count-words-in-defun Let's reuse @kbd{C-c =} as a convenient key binding: @smallexample -(global-set-key "\C-c=" 'count-words-defun) +(keymap-global-set "C-c =" 'count-words-defun) @end smallexample Now we can try out @code{count-words-defun}: install both @@ -17229,7 +17229,7 @@ Key Bindings @smallexample @group ;;; Compare windows -(global-set-key "\C-cw" 'compare-windows) +(keymap-global-set "C-c w" 'compare-windows) @end group @end smallexample @@ -17242,20 +17242,18 @@ Key Bindings This also shows how to set a key globally, for all modes. @cindex Setting a key globally -@cindex Global set key +@cindex Keymap global set @cindex Key setting globally -@findex global-set-key -The command is @code{global-set-key}. It is followed by the -key binding. In a @file{.emacs} file, the keybinding is written as -shown: @code{\C-c} stands for Control-C, which means to press the -control key and the @kbd{c} key at the same time. The @code{w} means -to press the @kbd{w} key. The key binding is surrounded by double -quotation marks. In documentation, you would write this as -@w{@kbd{C-c w}}. (If you were binding a @key{META} key, such as -@kbd{M-c}, rather than a @key{CTRL} key, you would write -@w{@code{\M-c}} in your @file{.emacs} file. @xref{Init Rebinding, , -Rebinding Keys in Your Init File, emacs, The GNU Emacs Manual}, for -details.) +@findex keymap-global-set +The key setting command is @code{keymap-global-set}. It is followed by +the key binding. In a @file{.emacs} file, the keybinding is written as +shown: @code{C-c} stands for Control-C, which means to press the control +key and the @kbd{c} key at the same time. The @code{w} means to press +the @kbd{w} key. The key binding is surrounded by double quotation +marks. (If you were binding a @key{META} key, rather than a @key{CTRL} +key, you would write @w{@code{M-c}} in your @file{.emacs} file. +@xref{Init Rebinding, , Rebinding Keys in Your Init File, emacs, The GNU +Emacs Manual}, for details.) The command invoked by the keys is @code{compare-windows}. Note that @code{compare-windows} is preceded by a single-quote; otherwise, Emacs @@ -17284,7 +17282,7 @@ Key Bindings @group ;;; Key binding for 'occur' ; I use occur a lot, so let's bind it to a key: -(global-set-key "\C-co" 'occur) +(keymap-global-set "C-c o" 'occur) @end group @end smallexample @@ -17296,7 +17294,7 @@ Key Bindings Matching lines are shown in a buffer called @file{*Occur*}. That buffer serves as a menu to jump to occurrences. -@findex global-unset-key +@findex keymap-global-unset @cindex Unbinding key @cindex Key unbinding @need 1250 @@ -17306,7 +17304,7 @@ Key Bindings @smallexample @group ;;; Unbind 'C-x f' -(global-unset-key "\C-xf") +(keymap-global-unset "C-x f") @end group @end smallexample @@ -17324,7 +17322,7 @@ Key Bindings @smallexample @group ;;; Rebind 'C-x C-b' for 'buffer-menu' -(global-set-key "\C-x\C-b" 'buffer-menu) +(keymap-global-set "C-x C-b" 'buffer-menu) @end group @end smallexample @@ -17336,33 +17334,79 @@ Key Bindings command, which not only lists the buffers, but moves point into that window. +@subsection Legacy Global Key Binding Commands + +@findex global-set-key +@cindex Global set key +Historically, keys are bound globally using a lower-level function, +@code{global-set-key}, which is now considered legacy. While you are +encouraged to use @code{keymap-global-set}, you likely would encounter +@code{global-set-key} in various places. The first example can be +rewritten using @code{global-set-key} as: + +@smallexample +@group +(global-set-key "\C-cw" 'compare-windows) +@end group +@end smallexample + +It is very similar to @code{keymap-global-set}, with the keybinding +following a slightly different format. Control-C is represented by +@code{\C-c}, instead of @code{C-c}. There is no space between key +strokes, like @code{\C-c} and @code{w} in this example. Despite the +difference, in documentation, this is still written as @w{@kbd{C-c w}} +for readability. + +@findex global-unset-key +Historically, keys are unbound globally using a lower-function, +@code{global-unset-key}, which is now considered legacy. Its key +binding format follows that of @code{global-set-key}. The above key +unbinding example can be rewritten as: +@smallexample +@group +;;; Unbind 'C-x f' +(global-unset-key "\C-xf") +@end group +@end smallexample + @node Keymaps @section Keymaps @cindex Keymaps @cindex Rebinding keys Emacs uses @dfn{keymaps} to record which keys call which commands. -When you use @code{global-set-key} to set the key binding for a single -command in all parts of Emacs, you are specifying the key binding in -@code{current-global-map}. +When you use @code{keymap-global-set} to set the key binding for a +single command in all parts of Emacs, you are specifying the key binding +in @code{current-global-map}. Specific modes, such as C mode or Text mode, have their own keymaps; the mode-specific keymaps override the global map that is shared by all buffers. -The @code{global-set-key} function binds, or rebinds, the global +The @code{keymap-global-set} function binds, or rebinds, the global keymap. For example, the following binds the key @kbd{C-x C-b} to the function @code{buffer-menu}: @smallexample -(global-set-key "\C-x\C-b" 'buffer-menu) +(keymap-global-set "C-x C-b" 'buffer-menu) @end smallexample -Mode-specific keymaps are bound using the @code{define-key} function, +Mode-specific keymaps are bound using the @code{keymap-set} function, which takes a specific keymap as an argument, as well as the key and -the command. For example, my @file{.emacs} file contains the -following expression to bind the @code{texinfo-insert-@@group} command -to @kbd{C-c C-c g}: +the command. For example, the following expression binds the +@code{texinfo-insert-@@group} command to @kbd{C-c C-c g}: + +@smallexample +@group +(keymap-set texinfo-mode-map "C-c C-c g" 'texinfo-insert-@@group) +@end group +@end smallexample + +Historically, keymaps are bound using a lower-level function, +@code{define-key}, which is now considered legacy. While you are +encouraged to use @code{keymap-set}, you likely would encounter +@code{define-key} in various places. The above key binding can be +rewritten using @code{define-key} as: @smallexample @group @@ -17396,9 +17440,9 @@ Keymaps write a function to insert a word; but I prefer key strokes consistent with other Texinfo mode key bindings.) -You will see numerous @code{define-key} expressions in -@file{loaddefs.el} as well as in the various mode libraries, such as -@file{cc-mode.el} and @file{lisp-mode.el}. +You will see numerous @code{keymap-set} and @code{define-key} +expressions in @file{loaddefs.el} as well as in the various mode +libraries, such as @file{cc-mode.el} and @file{lisp-mode.el}. @xref{Key Bindings, , Customizing Key Bindings, emacs, The GNU Emacs Manual}, and @ref{Keymaps, , Keymaps, elisp, The GNU Emacs Lisp @@ -17440,13 +17484,12 @@ Loading Files @need 1250 To replace the key binding for the default -@code{split-window-vertically}, you must also unset that key and bind -the keys to @code{split-window-quietly}, like this: +@code{split-window-vertically}, you must bind the keys to +@code{split-window-quietly}, like this: @smallexample @group -(global-unset-key "\C-x2") -(global-set-key "\C-x2" 'split-window-quietly) +(keymap-global-set "C-x 2" 'split-window-quietly) @end group @end smallexample @@ -17608,7 +17651,7 @@ Simple Extension this: @smallexample -(global-set-key [f6] 'line-to-top-of-window) +(keymap-global-set "" 'line-to-top-of-window) @end smallexample For more information, see @ref{Init Rebinding, , Rebinding Keys in @@ -18791,7 +18834,7 @@ the-the @group ;; Bind 'the-the' to C-c \ -(global-set-key "\C-c\\" 'the-the) +(keymap-global-set "C-c \\" 'the-the) @end group @end smallexample -- 2.47.1