all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* global-set-key with function taking arguments
@ 2020-11-01  9:53 jai-bholeki via Users list for the GNU Emacs text editor
  2020-11-01 10:57 ` William Xu
  0 siblings, 1 reply; 13+ messages in thread
From: jai-bholeki via Users list for the GNU Emacs text editor @ 2020-11-01  9:53 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org

This is the way I set a keybinding
(global-set-key (kbd "C-H-<down>") 'transpose-paragraphs)

To set a keybinding with a function taking parameters, I use
(global-set-key (kbd "C-H-<up>")
( lambda () (interactive) (transpose-paragraphs -1) )
)

However, may one write
(global-set-key (kbd "C-H-<up>") '(transpose-paragraphs -1) )

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

* Re: global-set-key with function taking arguments
  2020-11-01  9:53 global-set-key with function taking arguments jai-bholeki via Users list for the GNU Emacs text editor
@ 2020-11-01 10:57 ` William Xu
  2020-11-01 11:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 13+ messages in thread
From: William Xu @ 2020-11-01 10:57 UTC (permalink / raw
  To: help-gnu-emacs

jai-bholeki via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> This is the way I set a keybinding
> (global-set-key (kbd "C-H-<down>") 'transpose-paragraphs)
>
> To set a keybinding with a function taking parameters, I use
> (global-set-key (kbd "C-H-<up>")
> ( lambda () (interactive) (transpose-paragraphs -1) )
> )
>
> However, may one write
> (global-set-key (kbd "C-H-<up>") '(transpose-paragraphs -1) )

You could define a macro wraper around: 

#+begin_src emacs-lisp
(defmacro my-make-interactive (&rest body)
  `(lambda ()
     (interactive)
     (progn ,@body)))

(global-set-key (kbd "C-H-<up>") (my-make-interactive (transpose-paragraphs -1)))
#+end_src

-- 
William




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

* Re: global-set-key with function taking arguments
  2020-11-01 10:57 ` William Xu
@ 2020-11-01 11:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 11:47     ` Christopher Dimech
  2020-11-01 13:05     ` William Xu
  0 siblings, 2 replies; 13+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-11-01 11:33 UTC (permalink / raw
  To: help-gnu-emacs

William Xu wrote:

> You could define a macro wraper around: 
>
> (defmacro my-make-interactive (&rest body)
>   `(lambda ()
>      (interactive)
>      (progn ,@body)))
>
> (global-set-key (kbd "C-H-<up>") (my-make-interactive (transpose-paragraphs -1)))

Well, maybe you could, but what would you gain from that?

Better to either use `lambda' or define your own function.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: global-set-key with function taking arguments
  2020-11-01 11:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-11-01 11:47     ` Christopher Dimech
  2020-11-01 12:21       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 13:05     ` William Xu
  1 sibling, 1 reply; 13+ messages in thread
From: Christopher Dimech @ 2020-11-01 11:47 UTC (permalink / raw
  To: moasenwood; +Cc: help-gnu-emacs

It looks to me that it is much better to define your own function so then you can use
describe-key.  But I have not checked it.


> Sent: Sunday, November 01, 2020 at 12:33 PM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: global-set-key with function taking arguments
>
> William Xu wrote:
>
> > You could define a macro wraper around:
> >
> > (defmacro my-make-interactive (&rest body)
> >   `(lambda ()
> >      (interactive)
> >      (progn ,@body)))
> >
> > (global-set-key (kbd "C-H-<up>") (my-make-interactive (transpose-paragraphs -1)))
>
> Well, maybe you could, but what would you gain from that?
>
> Better to either use `lambda' or define your own function.
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: global-set-key with function taking arguments
  2020-11-01 11:47     ` Christopher Dimech
@ 2020-11-01 12:21       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 12:28         ` Christopher Dimech
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-11-01 12:21 UTC (permalink / raw
  To: help-gnu-emacs

Christopher Dimech wrote:

> It looks to me that it is much better to define your own function
> so then you can use describe-key.

... you mean `describe-function'?

AND if it is a function you can also invoke it with M-x, from Lisp,
or even from other keybindings.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: global-set-key with function taking arguments
  2020-11-01 12:21       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-11-01 12:28         ` Christopher Dimech
  2020-11-01 12:57           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 13+ messages in thread
From: Christopher Dimech @ 2020-11-01 12:28 UTC (permalink / raw
  To: moasenwood; +Cc: help-gnu-emacs


> Sent: Sunday, November 01, 2020 at 1:21 PM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: global-set-key with function taking arguments
>
> Christopher Dimech wrote:
>
> > It looks to me that it is much better to define your own function
> > so then you can use describe-key.
>
> ... you mean `describe-function'?

No.  I mean describe-key.  If you try describe-key for a key binding
you are not going to get useful documentation with an anonymous function.
But I might be mistaken.  May one include documentation string for
lambda ()?

>
> AND if it is a function you can also invoke it with M-x, from Lisp,
> or even from other keybindings.
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: global-set-key with function taking arguments
  2020-11-01 12:28         ` Christopher Dimech
@ 2020-11-01 12:57           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 13:19             ` Christopher Dimech
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-11-01 12:57 UTC (permalink / raw
  To: help-gnu-emacs

Christopher Dimech wrote:

>> ... you mean `describe-function'?
>
> No. I mean describe-key. If you try describe-key for a key binding
> you are not going to get useful documentation with an
> anonymous function.

Ah, that's what you mean. No, you are right, and I agree!

> But I might be mistaken. May one include documentation string for
> lambda ()?

You can! But it doesn't work the same way. C-h f lambda RET

  (lambda ARGS [DOCSTRING] [INTERACTIVE] BODY)
 
  DOCSTRING is an optional documentation string.
  If present, it should describe how to call the function.
  But documentation strings are usually not useful in nameless functions.

IMO proper functions (defuns) should be used with keys, not lambdas.
Except for perhaps very simple or temporary solutions.

Because functions are up in the sky - heaven, almost. And keys are by
all means a way, one way, a good way, but not the _only_ way, to
reach them. Functions shouldn't be imprisoned down there, at the same
level as mere keys...

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: global-set-key with function taking arguments
  2020-11-01 11:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 11:47     ` Christopher Dimech
@ 2020-11-01 13:05     ` William Xu
  1 sibling, 0 replies; 13+ messages in thread
From: William Xu @ 2020-11-01 13:05 UTC (permalink / raw
  To: help-gnu-emacs

Emanuel Berg via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:

> William Xu wrote:
>
>> You could define a macro wraper around: 
>>
>> (defmacro my-make-interactive (&rest body)
>>   `(lambda ()
>>      (interactive)
>>      (progn ,@body)))
>>
>> (global-set-key (kbd "C-H-<up>") (my-make-interactive (transpose-paragraphs -1)))
>
> Well, maybe you could, but what would you gain from that?

Not really much, perhaps saving two parens (for writing and reading), compared with lambda. :)

I agree, for anything more than a few lines, a custom function would be better. 

-- 
William




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

* Re: global-set-key with function taking arguments
  2020-11-01 12:57           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-11-01 13:19             ` Christopher Dimech
  2020-11-01 13:37               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 13+ messages in thread
From: Christopher Dimech @ 2020-11-01 13:19 UTC (permalink / raw
  To: moasenwood; +Cc: help-gnu-emacs


> IMO proper functions (defuns) should be used with keys, not lambdas.
> Except for perhaps very simple or temporary solutions.

Agreed.  For simple keybindings using a few lines of Emacs In-Built Commands,
using Anonymous Function is sensible, rather than having to make a 'defun'.
Anonymous Functions are especially when you see no reason why one would
normally call it.  Because you may also document the Anonymous Function,
the 'lamdba' construct, Anonymous Functions become a pragmatic way of working.

Documentation for an Anonymous Function should have the same purpose as for a
'defun'.  Conssequently, Documentation for an Anonymous Function should not
lack much from documentation of a 'defun'.



> Sent: Sunday, November 01, 2020 at 1:57 PM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: global-set-key with function taking arguments
>
> Christopher Dimech wrote:
>
> >> ... you mean `describe-function'?
> >
> > No. I mean describe-key. If you try describe-key for a key binding
> > you are not going to get useful documentation with an
> > anonymous function.
>
> Ah, that's what you mean. No, you are right, and I agree!
>
> > But I might be mistaken. May one include documentation string for
> > lambda ()?
>
> You can! But it doesn't work the same way. C-h f lambda RET
>
>   (lambda ARGS [DOCSTRING] [INTERACTIVE] BODY)
>
>   DOCSTRING is an optional documentation string.
>   If present, it should describe how to call the function.
>   But documentation strings are usually not useful in nameless functions.
>
> IMO proper functions (defuns) should be used with keys, not lambdas.
> Except for perhaps very simple or temporary solutions.
>
> Because functions are up in the sky - heaven, almost. And keys are by
> all means a way, one way, a good way, but not the _only_ way, to
> reach them. Functions shouldn't be imprisoned down there, at the same
> level as mere keys...
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: global-set-key with function taking arguments
  2020-11-01 13:19             ` Christopher Dimech
@ 2020-11-01 13:37               ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 14:08                 ` Christopher Dimech
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-11-01 13:37 UTC (permalink / raw
  To: help-gnu-emacs

Christopher Dimech wrote:

>> IMO proper functions (defuns) should be used with keys, not
>> lambdas. Except for perhaps very simple or temporary solutions.
>
> Agreed. For simple keybindings using a few lines of Emacs In-Built
> Commands, using Anonymous Function is sensible, rather than having
> to make a 'defun'. Anonymous Functions are especially when you see
> no reason why one would normally call it. Because you may also
> document the Anonymous Function, the 'lamdba' construct, Anonymous
> Functions become a pragmatic way of working.

Yes but ... if it is such a small function containing just a few
lines, chances are it is something basic to you and your way of using
Emacs, and if it is, chances are, further, that you will want it
again, but from a slightly different setting, e.g., another
major mode.

If and when that happens, to avoid duplicate code, turn the lambda
into a defun and call that from two places, rather than
killing/yanking the original lambda...

> Documentation for an Anonymous Function should have the same
> purpose as for a 'defun'. Conssequently, Documentation for an
> Anonymous Function should not lack much from documentation of
> a 'defun'.

They seem to have a different focus, namely how to call it.
This makes sense as, having no name, I mean, yeah, how do you call
it?!

Again, see the note here on lambda docstrings:

  C-h f lambda RET

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: global-set-key with function taking arguments
  2020-11-01 13:37               ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-11-01 14:08                 ` Christopher Dimech
  2020-11-01 14:56                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 13+ messages in thread
From: Christopher Dimech @ 2020-11-01 14:08 UTC (permalink / raw
  To: moasenwood; +Cc: help-gnu-emacs

My only complaint was that you would not get a description when
using in a key binding.  But now I know that one can put a
description.

I usually use them when I write by own improved versions of Built-In
Functions.  Then use a keybinding with the original call using 'lambda'.

Using key bindings makes it easy to see how my new function behaves
when compared with the Built-In version.



> Sent: Sunday, November 01, 2020 at 2:37 PM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: global-set-key with function taking arguments
>
> Christopher Dimech wrote:
>
> >> IMO proper functions (defuns) should be used with keys, not
> >> lambdas. Except for perhaps very simple or temporary solutions.
> >
> > Agreed. For simple keybindings using a few lines of Emacs In-Built
> > Commands, using Anonymous Function is sensible, rather than having
> > to make a 'defun'. Anonymous Functions are especially when you see
> > no reason why one would normally call it. Because you may also
> > document the Anonymous Function, the 'lamdba' construct, Anonymous
> > Functions become a pragmatic way of working.
>
> Yes but ... if it is such a small function containing just a few
> lines, chances are it is something basic to you and your way of using
> Emacs, and if it is, chances are, further, that you will want it
> again, but from a slightly different setting, e.g., another
> major mode.
>
> If and when that happens, to avoid duplicate code, turn the lambda
> into a defun and call that from two places, rather than
> killing/yanking the original lambda...
>
> > Documentation for an Anonymous Function should have the same
> > purpose as for a 'defun'. Conssequently, Documentation for an
> > Anonymous Function should not lack much from documentation of
> > a 'defun'.
>
> They seem to have a different focus, namely how to call it.
> This makes sense as, having no name, I mean, yeah, how do you call
> it?!
>
> Again, see the note here on lambda docstrings:
>
>   C-h f lambda RET
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



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

* Re: global-set-key with function taking arguments
  2020-11-01 14:08                 ` Christopher Dimech
@ 2020-11-01 14:56                   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-11-01 23:38                     ` Corwin Brust
  0 siblings, 1 reply; 13+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-11-01 14:56 UTC (permalink / raw
  To: help-gnu-emacs

Christopher Dimech wrote:

> My only complaint was that you would not get a description when
> using in a key binding. But now I know that one can put
> a description.

Yeah, but they are not intended to be used in exactly the same way.
Please read: C-h f lambda RET

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: global-set-key with function taking arguments
  2020-11-01 14:56                   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-11-01 23:38                     ` Corwin Brust
  0 siblings, 0 replies; 13+ messages in thread
From: Corwin Brust @ 2020-11-01 23:38 UTC (permalink / raw
  To: Emanuel Berg, Help Gnu Emacs mailing list

On Sun, Nov 1, 2020 at 8:56 AM Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Christopher Dimech wrote:
>
> > My only complaint was that you would not get a description when
> > using in a key binding. But now I know that one can put
> > a description.
>
> Yeah, but they are not intended to be used in exactly the same way.
> Please read: C-h f lambda RET

Rebinding is something specific that is harder if one uses binds a
lambda to a key.

This example from the top of the Elisp Manual section 22.12 would be
difficult if `kill-line' has been implemented with a lambda:

  (define-key my-mode-map [remap kill-line] 'my-kill-line)

In addition to (info "(elisp)Controlling-Active-Maps") see
`substitute-key-definition' in (info "(elisp)Changing Key Bindings")
for other problematic cases.

>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>

Regards,
Corwin



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

end of thread, other threads:[~2020-11-01 23:38 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-01  9:53 global-set-key with function taking arguments jai-bholeki via Users list for the GNU Emacs text editor
2020-11-01 10:57 ` William Xu
2020-11-01 11:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-11-01 11:47     ` Christopher Dimech
2020-11-01 12:21       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-11-01 12:28         ` Christopher Dimech
2020-11-01 12:57           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-11-01 13:19             ` Christopher Dimech
2020-11-01 13:37               ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-11-01 14:08                 ` Christopher Dimech
2020-11-01 14:56                   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-11-01 23:38                     ` Corwin Brust
2020-11-01 13:05     ` William Xu

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.