unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mina86@mina86.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 18729@debbugs.gnu.org
Subject: bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
Date: Mon, 27 Oct 2014 18:52:58 +0100	[thread overview]
Message-ID: <xa1tfve9o0w5.fsf@mina86.com> (raw)
In-Reply-To: <jwvppdsrscx.fsf-monnier+emacsbugs@gnu.org>

On Thu, Oct 16 2014, Stefan Monnier wrote:
>> (I would still argue :args syntax is also convenient though).
>
> The :args syntax has the problem that it can only be explained/defined
> by explaining how args work for functions and how `interactive' works,
> so from a conceptual point of view (say, explaining to someone who doesn't
> know Elisp yet) it's not really simpler than the full (lambda
> ...) form.  IOW the only gain is a few characters.

Admittedly, that was my main reason for set-key. ;)

>>> (define-key MAP KEY '(dired "foo"))
> [...]
>> Thing I'm worried about here is that the function will not get
>> byte-compiled, whereas with set-key macro it will.  Also, I'm not
>> entirely sure whether the function should use lexical-binding.
>
> Indeed, that's incompatible with lexical-binding.
> As you know, I find this to be a *major* downer.  But the only
> alternative would be to define a new macro (like you've done), which
> implies a larger change, and added complexity: since we're not
> going to deprecate define-key, it means we'd simply have yet another
> slightly different way to define key bindings.  I already dislike
> global-set-key and local-set-key, FWIW.
>
> I think if we want to make it easier for users to customize
> key-bindings, we should take a step back and look at the
> larger picture, trying to see exactly what it is that's hard.

The problem I was solving was that I'm too lazy to type “lambda” or
“interactive”. ;) Similarly to set-key I also have a add-lambda-hook
macro, e.g.:

        (add-lambda-hook 'kill-emacs-hook (auto-byte-compile-file t))

So for me it would make sense to have a more concise way of writing
lambda functions.  Perhaps for example:

------------- >8 -------------------------------------------------------
(defun lambda-builder (body &optional interactive)
  (let (args)
    (let ((split (memq '-> body)))
      (when split
        (while (not (eq body split))
          (push (car body) args)
          (setq body (cdr body)))
        (when (and interactive (not (symbolp (car args))))
          (setq interactive (list 'interactive (car args))
                args (cdr args)))
        (setq body (cdr body)
              args (nreverse args))))
    (cons 'lambda (cons args (if interactive (cons interactive body) body)))))

(defmacro λ (&rest body)
  (lambda-builder body))

(defmacro Λ (&rest body)
  (lambda-builder body '(interactive)))

(λ x y -> (+ x y))
=> (lambda (x y) (+ x y))
(Λ (dired "foo"))
=> (lambda nil (interactive) (dired "foo"))
(Λ x "p" -> (dired "foo"))
=> (lambda (x) (interactive "p") (insert (number-to-string x)))
------------- >8 -------------------------------------------------------

Admittedly though, typing Greek lambda is not easy on non-Greek layouts,
but my first choice of (\x y -> (+ x y)) or even (\ x y -> (+ x y))
won't work without possibly backwards incompatible changes to the
parser.  If anon thinks this makes sense, some other symbol could be
chosen as well.

> Maybe then we'll get a need for something that's substantially different
> from define-key (and in which your ideas can then easily be integrated).
>
> I'm thinking of things like tweaking key-bindings via Customize, and/or
> being able to specify keybindings for specific major modes (without
> needing to go through add-hook or eval-after-load), ...

If we are willing to assume that `foo-map' is defined when `foo-hook'
runs, this should be easily done.  For `foo-map' variables where this is
not the case `put' could be used.  Something like:

(defun set-key-in-hook (map key command)
  (if (or (not (symbolp map)) (boundp map))
      (define-key map key command)
    (let ((hook (or (get map 'define-key-hook)
                    (let ((name (symbol-name map)))
                      (when (string-match "^\\(..*\\)\\(?:-map\\)$" name)
                        (intern (concat (match-string 1 name) "-hook")))))))
      (if hook
          (add-hook hook (lambda () (define-key map key command)))
        (error "Unable to find hook for %s" map)))))

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +--<mpn@google.com>--<xmpp:mina86@jabber.org>--ooO--(_)--Ooo--





  reply	other threads:[~2014-10-27 17:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15  7:48 bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise Michal Nazarewicz
2014-10-15 14:20 ` Stefan Monnier
2014-10-16 12:39   ` Michal Nazarewicz
2014-10-16 14:50     ` Stefan Monnier
2014-10-27 17:52       ` Michal Nazarewicz [this message]
2015-01-19 15:40 ` Michal Nazarewicz

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=xa1tfve9o0w5.fsf@mina86.com \
    --to=mina86@mina86.com \
    --cc=18729@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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).