unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
@ 2014-10-15  7:48 Michal Nazarewicz
  2014-10-15 14:20 ` Stefan Monnier
  2015-01-19 15:40 ` Michal Nazarewicz
  0 siblings, 2 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-10-15  7:48 UTC (permalink / raw)
  To: 18729


---
 lisp/ChangeLog |  6 +++++
 lisp/subr.el   | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

 I found this macro very convenient for my configuration file, so
 perhaps it's a good match for inclusion in Emacs?  It handles the
 majority of uses of 

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 6916143..a5c4632 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,9 @@
+2014-10-15  Michal Nazarewicz  <mina86@mina86.com>
+
+	* subr.el (set-key): New macro making creating new bindings more
+	concise and thus somehow easier/faster to type in user
+	configuration file.
+
 2014-10-15  Eli Zaretskii  <eliz@gnu.org>
 
 	* emacs-lisp/tabulated-list.el (tabulated-list-mode): Force
diff --git a/lisp/subr.el b/lisp/subr.el
index 585f936..9b1ceb3 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -836,6 +836,79 @@ cases is shared with all other buffers in the same major mode."
 	(signal 'wrong-type-argument (list 'arrayp key)))
     (define-key map key command)))
 
+(defun set-key--current-local-map ()
+  "Return current local map creating one if not set yet."
+  (or (current-local-map)
+      (let ((map (make-sparse-keymap)))
+        (use-local-map map)
+        map)))
+
+(defmacro set-key (keymap key &rest def)
+  "(set-key [KEYMAP] KEY . DEF)
+
+In KEYMAP, define key sequence KEY as DEF.
+
+KEYMAP can be :global (to mean global keymap, the default), :local (to mean
+the local keymap) or an unquoted symbol (to mean a keymap in given variable).
+
+KEY is anything `define-key' accepts as a key except that if KEYMAP was not
+given, KEY cannot be an unquoted symbol, i.e.:
+    (let ((key \"a\"))
+      (set-key         key self-insert-command)  ; will *not* work
+      (set-key :global key self-insert-command)) ; will work
+
+If DEF is a single unquoted symbol it will be quoted, otherwise if it is
+a single non-cons value it will not be quoted, otherwise it will be processed
+as a lambda (see below).  Thus the following do what one might expect:
+    (set-key \"a\" self-insert-command)
+        ;; same as (global-set-key \"a\" 'self-insert-command)
+    (set-key \"\\C-h\" [(backspace)])
+        ;; same as (global-set-key \"\\C-h\" [(backspace)])
+    (set-key \"\\C-d\" ())
+        ;; same as (global-set-key \"\\C-h\" ())
+However, the following will not work:
+    (let ((callback 'self-insert-command))
+      (set-key \"a\" callback))
+        ;; same as (global-set-key \"a\" 'callback)
+
+If DEF is a cons value its format is:
+    ([:args ARGS INTERACTIVE] . BODY)
+and results in the following lambda:
+    (lambda ARGS (interactive INTERACTIVE) . BODY)
+or if :args is not given (at which point DEF == BODY):
+    (lambda () (interactive) . BODY)
+For example:
+    (set-key \"\\C-B\" (goto-char (- (point) 2)))
+        ;; same as (global-set-key \"\\C-B\"
+        ;;           (lambda () (interactive) (goto-char (- (point) 2))))
+    (set-key \"\\C-B\" :args (n) \"P\" (goto-char (- (point) (* 2 n))))
+        ;; same as (global-set-key \"\\C-B\"
+        ;;           (lambda (n) (interactive \"P\")
+        ;;             (goto-char (- (point) (* 2 n)))))
+
+This macro is not a replacement for `define-key', `global-set-key' or
+`local-set-key', since it is not capable of dealing with some forms of DEFs
+that those functions accept.  Instead it is meant as a helper to use in user
+configuration file to make setting up bindings more concise especially when
+lambdas are used."
+  (setq keymap (cond ((eq :local keymap)  '(set-key--current-local-map))
+                     ((eq :global keymap) '(current-global-map))
+                     ((symbolp keymap) keymap)
+                     (t
+                      (setq def (cons key def) key keymap) ; shift args
+                      '(current-global-map))))
+  (unless def
+    (error "DEF argument missing"))
+  (list
+   'define-key keymap key
+   (cond ((or (cdr def) (consp (car def)))
+          (let ((args        (if (eq :args (car def)) (cadr def)))
+                (interactive (if (eq :args (car def)) (list (car (cddr def)))))
+                (body        (if (eq :args (car def)) (cdr (cddr def)) def)))
+            `(function (lambda ,args (interactive . ,interactive) ,@body))))
+         ((symbolp (car def)) (list 'quote (car def)))
+         ((car def)))))
+
 (defun global-unset-key (key)
   "Remove global binding of KEY.
 KEY is a string or vector representing a sequence of keystrokes."





^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
  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
  2015-01-19 15:40 ` Michal Nazarewicz
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2014-10-15 14:20 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: 18729

> +(defmacro set-key (keymap key &rest def)

I'm not convinced the added complexity is worth the trouble, I'm afraid.
I don't see a real benefit in

   (set-key :global KEY CMD)
   (set-key :local KEY CMD)
   (set-key MAP KEY CMD)

over

   (global-set-key KEY CMD)
   (local-set-key KEY CMD)
   (define-key MAP KEY CMD)
or
   (define-key global-map KEY CMD)
   (define-key (current-local-map) KEY CMD)
   (define-key MAP KEY CMD)

I also don't see the benefit of (:args ARGS INTERACTIVE . BODY)
over (lambda ARGS (interactive INTERACTIVE) . BODY).

The only thing I think is really valuable is things like

   (set-key ... KEY (dired "foo"))

For which there is also a precedent in easy-define-menu (where the CMD
can be either a symbol or an expression, in which case that expression
gets wrapped in (lambda () (interactive) ...)).

Maybe we could simply extend define-key to accept

   (define-key MAP KEY '(dired "foo"))


-- Stefan





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
  2014-10-15 14:20 ` Stefan Monnier
@ 2014-10-16 12:39   ` Michal Nazarewicz
  2014-10-16 14:50     ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Nazarewicz @ 2014-10-16 12:39 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 18729

On Wed, Oct 15 2014, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>> +(defmacro set-key (keymap key &rest def)

> I'm not convinced the added complexity is worth the trouble, I'm afraid.
> I don't see a real benefit in
>
>    (set-key :global KEY CMD)

You wouldn't normally put “:global” here…

>    (set-key :local KEY CMD)
>    (set-key MAP KEY CMD)
>
> over
>
>    (global-set-key KEY CMD)
>    (local-set-key KEY CMD)
>    (define-key MAP KEY CMD)
[…]

…but I can see your point and it's true that for those cases, the saving
is not that big.

> I also don't see the benefit of (:args ARGS INTERACTIVE . BODY)
> over (lambda ARGS (interactive INTERACTIVE) . BODY).
>
> The only thing I think is really valuable is things like
>
>    (set-key ... KEY (dired "foo"))

Indeed, this is the main reason I've created the macro (I would still
argue :args syntax is also convenient though).

> For which there is also a precedent in easy-define-menu (where the CMD
> can be either a symbol or an expression, in which case that expression
> gets wrapped in (lambda () (interactive) ...)).
>
> Maybe we could simply extend define-key to accept
>
>    (define-key MAP KEY '(dired "foo"))

So are you think about something like:

diff --git a/src/keymap.c b/src/keymap.c
index c7c7d19..4b8251b 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -786,6 +786,9 @@ store_in_keymap (Lisp_Object keymap, register Lisp_Object idx, Lisp_Object def)
       && (EQ (XCAR (def), Qmenu_item) || STRINGP (XCAR (def))))
     def = Fcons (XCAR (def), XCDR (def));
 
+  if (CONSP (def) && !EQ (XCAR (quoted), Qlambda) && !EQ (XCAR (quoted), Qclosure))
+    def = Fcons (Qlambda, Fcons (Qnil, Fcons (Fcons (Qinteractive, Qnil), def)));
+
   if (!CONSP (keymap) || !EQ (XCAR (keymap), Qkeymap))
     error ("attempt to define a key in a non-keymap");
 
?  Or am I looking at completely wrong place.

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.

--
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--





^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
  2014-10-16 12:39   ` Michal Nazarewicz
@ 2014-10-16 14:50     ` Stefan Monnier
  2014-10-27 17:52       ` Michal Nazarewicz
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2014-10-16 14:50 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: 18729

> (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.

>> (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.

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), ...


        Stefan





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
  2014-10-16 14:50     ` Stefan Monnier
@ 2014-10-27 17:52       ` Michal Nazarewicz
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2014-10-27 17:52 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 18729

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--





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#18729: [PATCH] subr.el (set-key): New macro making creating new bindings more concise.
  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
@ 2015-01-19 15:40 ` Michal Nazarewicz
  1 sibling, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2015-01-19 15:40 UTC (permalink / raw)
  To: 18729-done

One way or another, ‘set-key’ is probably not the desired direction, so
closing this bug.  I’l have to keep carrying it in my init.el. ;/

-- 
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--





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-01-19 15:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2015-01-19 15:40 ` Michal Nazarewicz

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).