bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#18729: [PATCH] subr.el (set-key): New macro making creating new bind


From: Michal Nazarewicz
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
User-agent: Notmuch/0.17+15~gb65ca8e (http://notmuchmail.org) Emacs/25.0.50.1 (x86_64-unknown-linux-gnu)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]