unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 45329@debbugs.gnu.org
Subject: bug#45329: 26.3; Provide equivalent of :filter for non-menu key bindings
Date: Sat, 19 Dec 2020 17:20:01 -0800 (PST)	[thread overview]
Message-ID: <5f36c96e-d41a-40eb-bb0c-30967ba29936@default> (raw)

See Emacs bug #24237.  Using an extended menu item key binding you can
create a key binding that is "dynamic", or "dynamically conditional",
even for a keyboard key. 

By "dynamic" is meant that whether or not the key is effectively bound
(non-nil) and, if so, which command it is effectively bound to, can be
conditional according to the context current at the time the key is
used.

For "whether", instead of binding a key to a command whose definition
does something conditional ...

(defvar toto nil "...")

(defun foo ()
  (interactive)
  (and toto  (message "FOO")))

(global-set-key "\C-f" 'foo)

... you can move the condition to the key binding itself, that is, bind
the key conditionally:

(defun toto-filter (cmd) (and toto  cmd))

(global-set-key "\C-b" '(menu-item "" foo :filter toto-filter))
(global-set-key "\C-f" '(menu-item "" bar :filter toto-filter))

(defun foo () (interactive) (message "FOO"))
(defun bar () (interactive) (message "BAR"))

When `toto' is nil, `C-b' and `C-f' are unbound.

In this case, we made use of the same filter for two different key
bindings.  Both commands, `foo' and `bar', have bindings that are, in
effect, conditional on the value of variable `toto'.

Instead of a filter returning nil sometimes (which effectively means the
key isn't bound then), it can return different commands.

Several examples and some explanation are given in bug #24237.

The feature request is to provide this same capability without using an
extended menu item, because the KEY arg to `global-set-key' or
`define-key' need not have anything to do with a menu.  Let KEY be
associated with a filter.

How (e.g. syntax) to do that is up for grabs.  I don't have a strong
opinion about that.  Apparently Emacs doesn't want to just document the
above use case as is, i.e., advertise that you can use an extended menu
item for this.  Fair enough.

One possibility for doing this is to just allow the filter function as
an optional arg (similarly for `define-key'):

 (global-set-key KEY COMMAND &optional FILTER-FN) 

On the other hand, the REAL-BINDING part of the extended menu item can
also be somewhat useful: "it is only used in cases such as `where-is'",
as the static or nominal binding.  If we want to allow for that then
this could be the signature:

 (global-set-key KEY COMMAND &optional FILTER-FN NOMINAL-COMMAND) 

When this feature is documented in the Elisp manual it would make sense
to point out that FILTER-FN here is exactly as in an extended menu item.
(And if we include NOMINAL-COMMAND, mention that that corresponds to
the REAL-BINDING of an extended menu item.)

In GNU Emacs 26.3 (build 1, x86_64-w64-mingw32)
 of 2019-08-29
Repository revision: 96dd0196c28bc36779584e47fffcca433c9309cd
Windowing system distributor `Microsoft Corp.', version 10.0.18362
Configured using:
 `configure --without-dbus --host=x86_64-w64-mingw32
 --without-compress-install 'CFLAGS=-O2 -static -g3''





                 reply	other threads:[~2020-12-20  1:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5f36c96e-d41a-40eb-bb0c-30967ba29936@default \
    --to=drew.adams@oracle.com \
    --cc=45329@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).