From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id OOzdKmJBqF8XPQAA0tVLHw (envelope-from ) for ; Sun, 08 Nov 2020 19:05:06 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id wFu9JmJBqF/fcAAA1q6Kng (envelope-from ) for ; Sun, 08 Nov 2020 19:05:06 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 1D51F9402C8 for ; Sun, 8 Nov 2020 19:05:06 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id E1DC228C73; Sun, 8 Nov 2020 14:04:01 -0500 (EST) Received: from mail.hostpark.net (mail.hostpark.net [212.243.197.30]) by mail.notmuchmail.org (Postfix) with ESMTPS id B92242881E for ; Sun, 8 Nov 2020 14:03:15 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.hostpark.net (Postfix) with ESMTP id 81603165F2 for ; Sun, 8 Nov 2020 20:03:13 +0100 (CET) X-Virus-Scanned: by Hostpark/NetZone Mailprotection at hostpark.net Received: from mail.hostpark.net ([127.0.0.1]) by localhost (mail0.hostpark.net [127.0.0.1]) (amavisd-new, port 10124) with ESMTP id Zi_yiatHuB4p for ; Sun, 8 Nov 2020 20:03:13 +0100 (CET) Received: from customer (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.hostpark.net (Postfix) with ESMTPSA id EBE5D1667E for ; Sun, 8 Nov 2020 20:03:12 +0100 (CET) From: Jonas Bernoulli To: notmuch@notmuchmail.org Subject: [PATCH 26/27] emacs: avoid binding unnamed commands in keymaps Date: Sun, 8 Nov 2020 20:03:10 +0100 Message-Id: <20201108190311.1397-27-jonas@bernoul.li> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201108190311.1397-1-jonas@bernoul.li> References: <20201108190311.1397-1-jonas@bernoul.li> MIME-Version: 1.0 Message-ID-Hash: ELBNR75ZKI2UVT5G42IJ3AOPBHAPIJPB X-Message-ID-Hash: ELBNR75ZKI2UVT5G42IJ3AOPBHAPIJPB X-MailFrom: jonas@bernoul.li X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Scanner: ns3122888.ip-94-23-21.eu Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Spam-Score: -0.01 X-TUID: ej5YQTv3QzgB One should never bind unnamed commands in keymaps because doing that makes it needlessly hard for users to change these bindings. Replace such anonymous bindings with named commands that are generated using macros and some boilerplate. Using macros is better than using a simple loop because that makes it possible for `find-function' to find the definitions. Eat your boilerplate--it forms character. Admittedly this approach is quite ugly and it might be better to teach the original commands to support different buffers directly instead of requiring wrapper commands to do just that. Never-the-less as a short-term solution this is better than what we had before. --- emacs/notmuch-tree.el | 126 ++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 54 deletions(-) diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el index 7cc28b62..17863f6a 100644 --- a/emacs/notmuch-tree.el +++ b/emacs/notmuch-tree.el @@ -238,57 +238,83 @@ (defvar-local notmuch-tree-message-buffer nil if the user has loaded a different buffer in that window.") (put 'notmuch-tree-message-buffer 'permanent-local t) -(defun notmuch-tree-to-message-pane (func) - "Execute FUNC in message pane. - -This function returns a function (so can be used as a keybinding) -which executes function FUNC in the message pane if it is -open (if the message pane is closed it does nothing)." - `(lambda () - ,(concat "(In message pane) " (documentation func t)) +(defmacro notmuch-tree--define-do-in-message-window (name cmd) + "Define NAME as a command that calls CMD interactively in the message window. +If the message pane is closed then this command does nothing. +Avoid using this macro in new code; it will be removed." + `(defun ,name () + ,(concat "(In message window) " (documentation cmd t)) (interactive) (when (window-live-p notmuch-tree-message-window) (with-selected-window notmuch-tree-message-window - (call-interactively #',func))))) - -(defun notmuch-tree-inherit-from-message-pane (sym) - "Return value of SYM in message-pane if open, or tree-pane if not." + (call-interactively #',cmd))))) + +(notmuch-tree--define-do-in-message-window + notmuch-tree-previous-message-button + notmuch-show-previous-button) +(notmuch-tree--define-do-in-message-window + notmuch-tree-next-message-button + notmuch-show-next-button) +(notmuch-tree--define-do-in-message-window + notmuch-tree-toggle-message-process-crypto + notmuch-show-toggle-process-crypto) + +(defun notmuch-tree--message-process-crypto () + "Return value of `notmuch-show-process-crypto' in the message window. +If that window isn't alive, then return the current value. +Avoid using this function in new code; it will be removed." (if (window-live-p notmuch-tree-message-window) (with-selected-window notmuch-tree-message-window - (symbol-value sym)) - (symbol-value sym))) - -(defun notmuch-tree-close-message-pane-and (func) - "Close message pane and execute FUNC. - -This function returns a function (so can be used as a keybinding) -which closes the message pane if open and then executes function -FUNC." - `(lambda () - ,(concat "(Close message pane and) " (documentation func t)) + notmuch-show-process-crypto) + notmuch-show-process-crypto)) + +(defmacro notmuch-tree--define-close-message-window-and (name cmd) + "Define NAME as a variant of CMD. + +NAME determines the value of `notmuch-show-process-crypto' in the +message window, closes the window, and then call CMD interactively +with that value let-bound. If the message window does not exist, +then NAME behaves like CMD." + `(defun ,name () + ,(concat "(Close message pane and) " (documentation cmd t)) (interactive) (let ((notmuch-show-process-crypto - (notmuch-tree-inherit-from-message-pane 'notmuch-show-process-crypto))) + (notmuch-tree--message-process-crypto))) (notmuch-tree-close-message-window) - (call-interactively #',func)))) + (call-interactively #',cmd)))) + +(notmuch-tree--define-close-message-window-and + notmuch-tree-help + notmuch-help) +(notmuch-tree--define-close-message-window-and + notmuch-tree-new-mail + notmuch-mua-new-mail) +(notmuch-tree--define-close-message-window-and + notmuch-tree-jump-search + notmuch-jump-search) +(notmuch-tree--define-close-message-window-and + notmuch-tree-forward-message + notmuch-show-forward-message) +(notmuch-tree--define-close-message-window-and + notmuch-tree-reply-sender + notmuch-show-reply-sender) +(notmuch-tree--define-close-message-window-and + notmuch-tree-reply + notmuch-show-reply) +(notmuch-tree--define-close-message-window-and + notmuch-tree-view-raw-message + notmuch-show-view-raw-message) (defvar notmuch-tree-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map notmuch-common-keymap) - ;; The following override the global keymap. - ;; Override because we want to close message pane first. - (define-key map [remap notmuch-help] - (notmuch-tree-close-message-pane-and #'notmuch-help)) - ;; Override because we first close message pane and then close tree buffer. + ;; These bindings shadow common bindings with variants + ;; that additionally close the message window. (define-key map [remap notmuch-bury-or-kill-this-buffer] 'notmuch-tree-quit) - ;; Override because we close message pane after the search query is entered. - (define-key map [remap notmuch-search] 'notmuch-tree-to-search) - ;; Override because we want to close message pane first. - (define-key map [remap notmuch-mua-new-mail] - (notmuch-tree-close-message-pane-and #'notmuch-mua-new-mail)) - ;; Override because we want to close message pane first. - (define-key map [remap notmuch-jump-search] - (notmuch-tree-close-message-pane-and #'notmuch-jump-search)) + (define-key map [remap notmuch-search] 'notmuch-tree-to-search) + (define-key map [remap notmuch-help] 'notmuch-tree-help) + (define-key map [remap notmuch-mua-new-mail] 'notmuch-tree-new-mail) + (define-key map [remap notmuch-jump-search] 'notmuch-tree-jump-search) (define-key map "S" 'notmuch-search-from-tree-current-query) (define-key map "U" 'notmuch-unthreaded-from-tree-current-query) @@ -302,24 +328,16 @@ (defvar notmuch-tree-mode-map (define-key map "b" 'notmuch-show-resend-message) ;; these apply to the message pane - (define-key map (kbd "M-TAB") - (notmuch-tree-to-message-pane #'notmuch-show-previous-button)) - (define-key map (kbd "") - (notmuch-tree-to-message-pane #'notmuch-show-previous-button)) - (define-key map (kbd "TAB") - (notmuch-tree-to-message-pane #'notmuch-show-next-button)) - (define-key map "$" - (notmuch-tree-to-message-pane #'notmuch-show-toggle-process-crypto)) + (define-key map (kbd "M-TAB") 'notmuch-tree-previous-message-button) + (define-key map (kbd "") 'notmuch-tree-previous-message-button) + (define-key map (kbd "TAB") 'notmuch-tree-next-message-button) + (define-key map "$" 'notmuch-tree-toggle-message-process-crypto) ;; bindings from show (or elsewhere) but we close the message pane first. - (define-key map "f" - (notmuch-tree-close-message-pane-and #'notmuch-show-forward-message)) - (define-key map "r" - (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender)) - (define-key map "R" - (notmuch-tree-close-message-pane-and #'notmuch-show-reply)) - (define-key map "V" - (notmuch-tree-close-message-pane-and #'notmuch-show-view-raw-message)) + (define-key map "f" 'notmuch-tree-forward-message) + (define-key map "r" 'notmuch-tree-reply-sender) + (define-key map "R" 'notmuch-tree-reply) + (define-key map "V" 'notmuch-tree-view-raw-message) ;; The main tree view bindings (define-key map (kbd "RET") 'notmuch-tree-show-message) -- 2.29.1