From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.help Subject: Re: I'd like to advise a /keybinding/, how do I do it? Date: Wed, 28 Apr 2021 10:33:31 -0400 Message-ID: References: <87lf930ymd.fsf@mbork.pl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17136"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:0pEbjORBWaz46cm+juoOSeuAkNQ= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Apr 28 16:34:11 2021 Return-path: Envelope-to: geh-help-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 1lblGU-0004Nl-S0 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 28 Apr 2021 16:34:10 +0200 Original-Received: from localhost ([::1]:40212 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lblGT-00038Q-VV for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 28 Apr 2021 10:34:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47828) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lblG3-000380-6C for help-gnu-emacs@gnu.org; Wed, 28 Apr 2021 10:33:43 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]:46858) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lblG1-0004d4-N4 for help-gnu-emacs@gnu.org; Wed, 28 Apr 2021 10:33:42 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1lblFx-0003o5-65 for help-gnu-emacs@gnu.org; Wed, 28 Apr 2021 16:33:37 +0200 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:129189 Archived-At: > The rationale is that I'm thinking about writing a minor mode which > could prevent a "finishing" action until the buffer is ready. > A "finishing" action is often bound to C-c C-c (sending an email and > commiting something to Git come to mind), so I'd like to advise whatever > C-c C-c calls to check the buffer for occurrences of a string like TODO. "advising the key-binding" would imply changing by side-effect the underlying major mode's keymap (BTW: yes, it can be done, by defining `lookup-key` as an lvalue (e.g. with gv-define-setter) after which you can use `add-function` to modify a key binding). Or you could do it by advising the command bound to the particular key (in which case it'll also affect invocations of that command that don't go through this key binding, e.g. via `M-x`). Or you could do it with a minor-mode map (i.e. without modifying any major mode keymap or command), where you bind `C-c C-c` to a command that either beeps or delegates to the command that it hid (e.g. which you could find for example with (let ((my-new-minor-mode nil)) (key-binding (this-command-keys)))) Or you could do it with a `pre-command-hook` which checks `this-command-keys`. Or the minor-mode map could use a "filtered menu-item" conditional key-binding. Or ... Stefan